running puppeteer code in firebase cloud functions

烈酒焚心 提交于 2019-12-23 18:17:50

问题


I'm working on a personal project which is an app where an user can input their address & credit card info and click a button to buy a parking permit for one of the university that I visit often.

I want to host my front end in firebase's cloud storage and I want to create a cloud function where puppeteer code can run with the information that I save to the firebase's realtime database.

Is it possible for firebase cloud function to run puppeteer code that buy a parking permit?

Since puppeteer doesn't work with cloud functions, can I use heruoku to host the puppeteer code?


回答1:


It's currently not possible to use Puppeteer with the Cloud Functions node 6 runtime because the server instances that run deployed code are lacking a shared library that's required by Puppeteer. You can read about that in this GitHub issue. Also see this issue.

As of the node 8 runtime release, the required libraries for puppeteer are available.




回答2:


It's now possible to run Puppeteer inside Cloud Functions (as of August 13, 2018).

Note: some of these commands are in the "beta" SDK so will no doubt change in the future. The quick start guide contains the latest documentation.

1) At the time of writing, you need to use Node 8 and the beta components:

gcloud components update
gcloud components install beta

2) There's a "headless Chrome" example in the Node examples which shows how to create screenshot as a PNG (there are other options available though).

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
cd nodejs-docs-samples/functions/headless-chrome

3) Deploy the component

gcloud beta functions deploy screenshot --runtime nodejs8 --trigger-http

4) Finally, you'll need to increase the memory allocation. By default, Cloud Functions only get 256Mb of memory so you'll get an error if you try to run the Puppeteer without first changing the memory settings. Open your project in the Cloud Console, select Cloud Functions, select your function and click edit. 512Mb wasn't enough for me so I went up to 2Gb.




回答3:


I had a similar problem and I have used https://www.browserless.io/ to help with this issue. They also have a docker image for private use, which could be a way to run headless chrome with puppeteer.

Instead of lauching puppeteer with the local browser you can connect to puppeteer on their instance with the endpoint on their service like this:

const puppeteer = require('puppeteer');

puppeteer.connect({
  browserWSEndpoint: 'wss://chrome.browserless.io'
}).then(async browser => {
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'screenshot.png'});
  page.close();
});

If you want to use await and async with the cloud functions you need to transpile to ES5 before uploading. See for a solution with typescript as the compiler: https://github.com/ultrasaurus/firebase-functions-typescript




回答4:


As of now you can use puppeteer in firebase cloud functions with nodejs. When Google Cloud Functions was first released, the only runtime that it supported was Node.js version 6 and the OS was missing several packages that made it difficult to run Chrome in this fashion. But this was made possible by the release of Node.js 8 runtime on App Engine standard and which was the same runtime used for Google Cloud Functions too. Check out the official blog post that announced it: link



来源:https://stackoverflow.com/questions/48667933/running-puppeteer-code-in-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!