Sparticuz / chrome-aws-lambda

This package has been deprecated in favor of @sparticuz/chromium
https://www.github.com/sparticuz/chromium
MIT License
139 stars 21 forks source link

Can not work goto function in Firebase functions emulator. #31

Closed tuclicks closed 2 years ago

tuclicks commented 2 years ago

Environment

package.json

 "dependencies": {
    "@sparticuz/chrome-aws-lambda": "^17.1.1",
    "firebase-admin": "^10.2.0",
    "firebase-functions": "^3.23.0",
    "puppeteer-core": "~17.1.1",
    "stripe": "^10.7.0"
  },
  "devDependencies": {
    "puppeteer": "^17.1.2",
    "typescript": "^4.6.4"
  },

Thank you for your wonderful project. The sample code doesn't work in the Firebase Functions emulator. timeout on goto function. but, work in production environment.

Looking at the devtool, the request doesn't seem to fire.

import * as functions from 'firebase-functions'
const chromium = require('@sparticuz/chrome-aws-lambda');

module.exports = functions.region('asia-northeast1').https.onRequest(async (req , resp) => {

  const isEmulator = process.env.FUNCTIONS_EMULATOR
  if(!isEmulator){
    resp.status(404).send('Page Not Found.')
    return
  }

  let browser = null;
  try {
    const puppeteerArgs = isEmulator ? [
      '--disable-gpu',
      '--disable-dev-shm-usage',
      '--disable-setuid-sandbox',
      '--no-first-run',
      '--no-sandbox',
      '--no-zygote',
      '--single-process',
      '--lang=ja',//日本語化
      '--auto-open-devtools-for-tabs',//デベロッパーツール起動
    ] : chromium.args.concat(['--lang=ja'])
    browser = await chromium.puppeteer.launch({
      args: puppeteerArgs,
      defaultViewport: chromium.defaultViewport,
      executablePath: isEmulator ? '/Applications/Chromium.app/Contents/MacOS/Chromium' : await chromium.executablePath,
      headless: isEmulator ? false : chromium.headless,
      ignoreHTTPSErrors: true,
    });

    let page = await browser.newPage();

    await page.goto('https://github.com/'); //<- This is not working.

    const title = await page.title();
    resp.send(title)
  } catch (error) {
    if(!(error instanceof Error)){
      console.error(error)
      resp.status(500).send('不明なエラー')
    }else{
      resp.status(500).send(error?.message)
    }
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }
})

I tried other code. I have another error. 'socket hang up'

module.exports = functions.region('asia-northeast1').https.onRequest(async (req , resp) => {

  const isEmulator = process.env.FUNCTIONS_EMULATOR
  if(!isEmulator){
    resp.status(404).send('Page Not Found.')
    return
  }

  let browser = null;
  try {
    browser = await chromium.puppeteer.launch({
      args: chromium.args.concat(['--lang=ja']),
      defaultViewport: chromium.defaultViewport,
      executablePath: isEmulator ? '/Applications/Chromium.app/Contents/MacOS/Chromium' : await chromium.executablePath,
      headless: isEmulator ? false : chromium.headless,
      ignoreHTTPSErrors: true,
    });

    let page
    const pages = await browser.pages();
    if(pages.length){
      page = pages[0]
      console.log(`url:${page.url()}`)
    }else{
      page = await browser.newPage()
    }

    await page.goto('https://github.com'); //<- This is not working.

    const title = await page.title();
    resp.send(title)
  } catch (error) {
    if(!(error instanceof Error)){
      console.error(error)
      resp.status(500).send('不明なエラー')
    }else{
      resp.status(500).send(error?.message)
    }
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }
})
>  ErrorEvent {
>    [Symbol(kTarget)]: WebSocket {
>      _events: [Object: null prototype] { open: [Function], error: [Function] },
>      _eventsCount: 2,
>      _maxListeners: undefined,
>      _binaryType: 'nodebuffer',
>      _closeCode: 1006,
>      _closeFrameReceived: false,
>      _closeFrameSent: false,
>      _closeMessage: <Buffer >,
>      _closeTimer: null,
>      _extensions: {},
>      _paused: false,
>      _protocol: '',
>      _readyState: 3,
>      _receiver: null,
>      _sender: null,
>      _socket: null,
>      _bufferedAmount: 0,
>      _isServer: false,
>      _redirects: 0,
>      _url: 'ws://127.0.0.1:62165/devtools/browser/341eba6e-3fa7-4d6e-a38d-28e304f6c9eb',
>      _originalUnixSocket: false,
>      _originalSecure: false,
>      _originalHostOrSocketPath: '127.0.0.1:62165',
>      _req: null,
>      [Symbol(kCapture)]: false
>    },
>    [Symbol(kType)]: 'error',
>    [Symbol(kError)]: Error: socket hang up
>        at connResetException (node:internal/errors:691:14)
>        at Socket.socketOnEnd (node:_http_client:466:23)
>        at Socket.emit (node:events:538:35)
>        at endReadableNT (node:internal/streams/readable:1345:12)
>        at processTicksAndRejections (node:internal/process/task_queues:83:21) {
>      code: 'ECONNRESET'
>    },
>    [Symbol(kMessage)]: 'socket hang up'
>  }
tuclicks commented 2 years ago

Sorry. I fixed it by updating chromium.