This commit is contained in:
yanlong.wang 2024-04-11 18:28:51 +08:00
parent 77174f1511
commit e17ef6dba0
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37
4 changed files with 21 additions and 15 deletions

View File

@ -1,9 +1,14 @@
const {join} = require('path');
let config = {};
if (!process.env.FUNCTIONS_EMULATOR) {
config = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
}
/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
module.exports = config;

View File

@ -19,7 +19,7 @@
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log",
"gcp-build": "node node_modules/puppeteer/install.js"
"gcp-build": "node node_modules/puppeteer/install.mjs"
},
"engines": {
"node": "20"

View File

@ -30,3 +30,5 @@ Object.assign(exports, registry.exportGrouped({
}));
registry.title = 'url2text';
registry.version = '0.1.0';
process.on('unhandledRejection', () => 'no big deal');

View File

@ -1,4 +1,4 @@
import { AsyncService, Defer, HashManager, marshalErrorLike } from 'civkit';
import { AssertionFailureError, AsyncService, Defer, HashManager, marshalErrorLike } from 'civkit';
import { container, singleton } from 'tsyringe';
import puppeteer, { Browser } from 'puppeteer';
import { Logger } from '../shared/services/logger';
@ -72,7 +72,6 @@ export class PuppeteerControl extends AsyncService {
}
this.browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
this.browser.once('disconnected', () => {
this.logger.warn(`Browser disconnected`);
@ -212,20 +211,20 @@ function giveSnapshot() {
});
return r;
}).catch((err) => {
this.logger.warn(`Failed to goto ${url}`, { err: marshalErrorLike(err) });
return Promise.reject(new AssertionFailureError({
message: `Failed to goto ${url}: ${err}`,
cause: err,
}));
}).finally(() => {
finalized = true;
});
gotoPromise.catch((err) => {
this.logger.warn(`Browsing of ${url} not fully done`, { err: marshalErrorLike(err) });
}).finally(() => {
finalized = true;
});
try {
while (true) {
await Promise.race([nextSnapshotDeferred.promise, gotoPromise]);
if (finalized) {
await gotoPromise;
yield { ...snapshot, screenshot };
break;
}