puppeteer.launch 在 Cloud Functions 中抛出错误

puppeteer.launch in Cloud Functions throwing error

我一直在使用 React 和 Puppeteer 开发 gcloud 函数。我和我的团队遇到了这个我们似乎无法解决的问题。我们几乎到处都看过了。

云函数

index.js

const functions = require("firebase-functions");
const puppeteer = require("puppeteer");

exports.searchFor = functions.runWith({memory: "1GB"}).https.onCall(async (data, context) => {
    try {
        const browser = await puppeteer.launch({headless: true, args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
        ]})
    } catch (error) {
        return error
    }

})

客户

import firebase from 'firebase/app'
import "firebase/functions";
import {functions} from "../firebase";
import {parse} from "query-string";
import {RouteComponentProps} from "react-router";
import {searchQuery} from "../functions/tasks"
import {withRouter} from 'react-router-dom';
import React from "react";

// with proper values, of course
const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: "",
}

const app = firebase.initializeApp(firebaseConfig);

const searchFor = firebase.functions(app).httpsCallable("searchFor")

export const Search = withRouter(props => <Searcher {...props}/>);

class Searcher extends React.Component<React.PropsWithChildren<RouteComponentProps<any>>> {

    private location = this.props.location;

    private searchQuery = parse(this.location.search)

    private testSearch = async (): void => {
        return await searchFor({data: "test"}).then((res) => {
            return res.data
        })
    }

    public componentDidMount = (): void => {
        this.testSearch().then((out) => {
            console.log(out)
        })
    }

    public render = (): JSX.Element => {
        return (
            <div id="search">
            </div>
        );
    }

}

输出

云函数中,await puppeteer.launch()报错。在控制台中,只有一个空对象。如果我们不在云函数中使用 try catch 块,控制台输出 error INTERNAL 并且状态变为 500。我们正在为我们的引擎使用 Node JS 10

tl;博士

const browser = await puppeteer.launch({headless: true, args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
]})

在 Google 云函数中抛出未知错误,我们不知道如何修复它。

更新

我们的团队找到了妥协方案。我们将 Puppeteer 降级到 v1.19.0,并将引擎切换到 Node 8,一切都按预期工作