Skip to content

Commit

Permalink
Merge pull request #252 from Sparticuz/fix/misc-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz committed Apr 4, 2024
2 parents bb95235 + 41223a1 commit d0543e6
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 41 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,50 @@ 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();
```

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
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.
Expand Down Expand Up @@ -260,8 +304,8 @@ By default, this package uses `swiftshader`/`angle` to do CPU acceleration for W
| `args` | `Array<string>` | 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<string>` | 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 |

Expand Down
18 changes: 7 additions & 11 deletions _/amazon/handlers/index.js
Original file line number Diff line number Diff line change
@@ -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");

Expand All @@ -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"] });
Expand Down Expand Up @@ -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();
}
}
Expand Down
39 changes: 23 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion source/lambdafs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ class LambdaFS {
}
}

export = LambdaFS;
export default LambdaFS;
8 changes: 4 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down

0 comments on commit d0543e6

Please sign in to comment.