From 94b8687686c94f224b428f8ee27ae8a5db581153 Mon Sep 17 00:00:00 2001 From: Kyle McNally Date: Wed, 3 Apr 2024 11:15:25 -0400 Subject: [PATCH 1/5] closes #148 --- README.md | 15 +++++++++++++-- _/amazon/handlers/index.js | 14 +++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 762d902..40f1dcc 100644 --- a/README.md +++ b/README.md @@ -204,6 +204,17 @@ From what I can tell, `headless_shell` does not seem to include support for the Try marking this package as an external. Ref: https://webpack.js.org/configuration/externals/ +### I'm experiencing timeouts or failures closing Chromium + +This is a common issue. Chromium sometimes opens up more pages than you ask for. You can try the following + +```typescript +for (const page of await browser.pages()) { + await page.close(); +} +await browser.close(); +``` + ## Fonts The Amazon Linux 2 AWS Lambda runtime is not provisioned with any font faces. @@ -260,8 +271,8 @@ By default, this package uses `swiftshader`/`angle` to do CPU acceleration for W | `args` | `Array` | Provides a list of recommended additional [Chromium flags](https://github.com/GoogleChrome/chrome-launcher/blob/master/docs/chrome-flags-for-tools.md). | | `defaultViewport` | `Object` | Returns a sensible default viewport for serverless. | | `executablePath(location?: string)` | `Promise` | Returns the path the Chromium binary was extracted to. | -| `setHeadlessMode` | `void` | Sets the headless mode to either `true` or `"shell"` | -| `headless` | `true \| "shell"` | Returns `true` or `"shell"` depending on what version of chrome's headless you are running | +| `setHeadlessMode` | `void` | Sets the headless mode to either `true` or `"shell"` | +| `headless` | `true \| "shell"` | Returns `true` or `"shell"` depending on what version of chrome's headless you are running | | `setGraphicsMode` | `void` | Sets the graphics mode to either `true` or `false` | | `graphics` | `boolean` | Returns a boolean depending on whether webgl is enabled or disabled | diff --git a/_/amazon/handlers/index.js b/_/amazon/handlers/index.js index f6a5124..b8c07af 100644 --- a/_/amazon/handlers/index.js +++ b/_/amazon/handlers/index.js @@ -18,15 +18,8 @@ exports.handler = async (event, context) => { console.log("Chromium version", await browser.version()); - const contexts = [browser.defaultBrowserContext()]; - - while (contexts.length < event.length) { - contexts.push(await browser.createBrowserContext()); - } - - for (let context of contexts) { - const job = event.shift(); - const page = await context.newPage(); + for (let job of event) { + const page = await browser.newPage(); if (job.hasOwnProperty("url") === true) { await page.goto(job.url, { waitUntil: ["domcontentloaded", "load"] }); @@ -68,6 +61,9 @@ exports.handler = async (event, context) => { throw error.message; } finally { if (browser !== null) { + for (const page of await browser.pages()) { + await page.close(); + } await browser.close(); } } From 0f12e5f2d72c003fb8133e659f65e8f93e38257d Mon Sep 17 00:00:00 2001 From: Kyle McNally Date: Wed, 3 Apr 2024 11:33:02 -0400 Subject: [PATCH 2/5] closes #113 --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 40f1dcc..a1aeaf8 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,31 @@ for (const page of await browser.pages()) { await browser.close(); ``` +### I need Accessible pdf files + +This is due to the way @sparticuz/chromium is built. If you require accessible pdf's, you'll need to +recompile chromium yourself with the following patch. You can then use that binary with @sparticuz/chromium-min. + +_Note_: This will increase the time required to generate a PDF. + +```patch +diff --git a/_/ansible/plays/chromium.yml b/_/ansible/plays/chromium.yml +index b42c740..49111d7 100644 +--- a/_/ansible/plays/chromium.yml ++++ b/_/ansible/plays/chromium.yml +@@ -249,8 +249,9 @@ + blink_symbol_level = 0 + dcheck_always_on = false + disable_histogram_support = false +- enable_basic_print_dialog = false + enable_basic_printing = true ++ enable_pdf = true ++ enable_tagged_pdf = true + enable_keystone_registration_framework = false + enable_linux_installer = false + enable_media_remoting = false +``` + ## Fonts The Amazon Linux 2 AWS Lambda runtime is not provisioned with any font faces. From 4ce60bdd4dd10b6b0c9f27b64dc658cd98999009 Mon Sep 17 00:00:00 2001 From: Kyle McNally Date: Wed, 3 Apr 2024 13:07:27 -0400 Subject: [PATCH 3/5] more info on closing chromium --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index a1aeaf8..a5af33f 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,14 @@ for (const page of await browser.pages()) { await browser.close(); ``` +You can also try the following if one of the calls is hanging for some reason. + +```typescript +await Promise.race([browser.close(), browser.close(), browser.close()]); +``` + +Always `await browser.close()`, even if your script is returning an error. + ### I need Accessible pdf files This is due to the way @sparticuz/chromium is built. If you require accessible pdf's, you'll need to From e4de013045a13305b0fee0d8eccebc57dd67ebc4 Mon Sep 17 00:00:00 2001 From: Kyle McNally Date: Wed, 3 Apr 2024 13:27:08 -0400 Subject: [PATCH 4/5] update deps --- Makefile | 6 ++++++ _/amazon/handlers/index.js | 4 ++-- package-lock.json | 39 ++++++++++++++++++++++---------------- package.json | 8 ++++---- source/lambdafs.ts | 2 +- tsconfig.json | 8 ++++---- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index e2b53f4..5a247f2 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,12 @@ pretest: test: sam local invoke --template _/amazon/template.yml --event _/amazon/events/example.com.json node20 +test16: + sam local invoke --template _/amazon/template.yml --event _/amazon/events/example.com.json node16 + +test18: + sam local invoke --template _/amazon/template.yml --event _/amazon/events/example.com.json node18 + %.zip: npm install --fund=false --package-lock=false npm run build diff --git a/_/amazon/handlers/index.js b/_/amazon/handlers/index.js index b8c07af..9c12578 100644 --- a/_/amazon/handlers/index.js +++ b/_/amazon/handlers/index.js @@ -1,5 +1,5 @@ -const { ok } = require("assert"); -const { createHash } = require("crypto"); +const { ok } = require("node:assert"); +const { createHash } = require("node:crypto"); const puppeteer = require("puppeteer-core"); const chromium = require("@sparticuz/chromium"); diff --git a/package-lock.json b/package-lock.json index a8dac5d..5771173 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,28 +13,35 @@ "tar-fs": "^3.0.5" }, "devDependencies": { - "@tsconfig/node16": "^16.1.1", - "@tsconfig/strictest": "^2.0.3", + "@tsconfig/node16": "^16.1.3", + "@tsconfig/node20": "^20.1.4", + "@tsconfig/strictest": "^2.0.5", "@types/follow-redirects": "^1.14.4", - "@types/node": "^20.11.30", + "@types/node": "^20.12.3", "@types/tar-fs": "^2.0.4", "clean-modules": "^3.0.5", - "typescript": "^5.4.2" + "typescript": "^5.4.3" }, "engines": { "node": ">= 16" } }, "node_modules/@tsconfig/node16": { - "version": "16.1.1", - "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-16.1.1.tgz", - "integrity": "sha512-+pio93ejHN4nINX4pXqfnR/fPLRtJBaT4ORaa5RH0Oc1zoYmo2B2koG+M328CQhHKn1Wj6FcOxCDFXAot9NhvA==", + "version": "16.1.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-16.1.3.tgz", + "integrity": "sha512-9nTOUBn+EMKO6rtSZJk+DcqsfgtlERGT9XPJ5PRj/HNENPCBY1yu/JEj5wT6GLtbCLBO2k46SeXDaY0pjMqypw==", + "dev": true + }, + "node_modules/@tsconfig/node20": { + "version": "20.1.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node20/-/node20-20.1.4.tgz", + "integrity": "sha512-sqgsT69YFeLWf5NtJ4Xq/xAF8p4ZQHlmGW74Nu2tD4+g5fAsposc4ZfaaPixVu4y01BEiDCWLRDCvDM5JOsRxg==", "dev": true }, "node_modules/@tsconfig/strictest": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/@tsconfig/strictest/-/strictest-2.0.3.tgz", - "integrity": "sha512-MroLvRhMbqtXI5WBSwoomro6OQS4xnCoudUrMb20JO0vLKUs0bAaCEcvM/immEBSJjFAK1l6jW1oAO8q3Ancrg==", + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@tsconfig/strictest/-/strictest-2.0.5.tgz", + "integrity": "sha512-ec4tjL2Rr0pkZ5hww65c+EEPYwxOi4Ryv+0MtjeaSQRJyq322Q27eOQiFbuNgw2hpL4hB1/W/HBGk3VKS43osg==", "dev": true }, "node_modules/@types/follow-redirects": { @@ -47,9 +54,9 @@ } }, "node_modules/@types/node": { - "version": "20.11.30", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz", - "integrity": "sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==", + "version": "20.12.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.3.tgz", + "integrity": "sha512-sD+ia2ubTeWrOu+YMF+MTAB7E+O7qsMqAbMfW7DG3K1URwhZ5hN1pLlRVGbf4wDFzSfikL05M17EyorS86jShw==", "dev": true, "dependencies": { "undici-types": "~5.26.4" @@ -299,9 +306,9 @@ "dev": true }, "node_modules/typescript": { - "version": "5.4.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz", - "integrity": "sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==", + "version": "5.4.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.3.tgz", + "integrity": "sha512-KrPd3PKaCLr78MalgiwJnA25Nm8HAmdwN3mYUYZgG/wizIo9EainNVQI9/yDavtVFRN2h3k8uf3GLHuhDMgEHg==", "dev": true, "bin": { "tsc": "bin/tsc", diff --git a/package.json b/package.json index 4229032..58e0e78 100644 --- a/package.json +++ b/package.json @@ -40,13 +40,13 @@ "tar-fs": "^3.0.5" }, "devDependencies": { - "@tsconfig/node16": "^16.1.1", - "@tsconfig/strictest": "^2.0.3", + "@tsconfig/node20": "^20.1.4", + "@tsconfig/strictest": "^2.0.5", "@types/follow-redirects": "^1.14.4", - "@types/node": "^20.11.30", + "@types/node": "^20.12.3", "@types/tar-fs": "^2.0.4", "clean-modules": "^3.0.5", - "typescript": "^5.4.2" + "typescript": "^5.4.3" }, "engines": { "node": ">= 16" diff --git a/source/lambdafs.ts b/source/lambdafs.ts index 57a63f8..b032fa2 100644 --- a/source/lambdafs.ts +++ b/source/lambdafs.ts @@ -72,4 +72,4 @@ class LambdaFS { } } -export = LambdaFS; +export default LambdaFS; diff --git a/tsconfig.json b/tsconfig.json index d8c6685..54571c6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { - "extends": ["@tsconfig/node16/tsconfig", "@tsconfig/strictest"], + "extends": ["@tsconfig/node20/tsconfig", "@tsconfig/strictest"], "compilerOptions": { "declaration": true, - "lib": ["dom", "ES2021"], - "module": "Node16", - "moduleResolution": "Node16", + "lib": ["dom", "ES2023"], + "module": "NodeNext", + "moduleResolution": "NodeNext", "outDir": "build" }, "include": ["source"] From 41223a10e97e23fb5820dcd30e96dee844534472 Mon Sep 17 00:00:00 2001 From: Kyle McNally Date: Wed, 3 Apr 2024 14:42:23 -0400 Subject: [PATCH 5/5] Disable turning off accelerated graphics https://github.com/Sparticuz/chromium/issues/247 --- source/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/source/index.ts b/source/index.ts index a420692..1b5e70f 100644 --- a/source/index.ts +++ b/source/index.ts @@ -240,9 +240,11 @@ class Chromium { ]; // https://chromium.googlesource.com/chromium/src/+/main/docs/gpu/swiftshader.md - this.graphics - ? graphicsFlags.push("--use-gl=angle", "--use-angle=swiftshader") - : graphicsFlags.push("--disable-webgl"); + // Blocked by https://github.com/Sparticuz/chromium/issues/247 + //this.graphics + // ? graphicsFlags.push("--use-gl=angle", "--use-angle=swiftshader") + // : graphicsFlags.push("--disable-webgl"); + graphicsFlags.push("--use-gl=angle", "--use-angle=swiftshader"); const insecureFlags = [ "--allow-running-insecure-content", // https://source.chromium.org/search?q=lang:cpp+symbol:kAllowRunningInsecureContent&ss=chromium