Skip to content

Commit

Permalink
Merge pull request #139 from Sparticuz/feature/116
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz authored Aug 18, 2023
2 parents aeee592 + 11d63b6 commit 27ba2ee
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 492 deletions.
74 changes: 37 additions & 37 deletions .github/workflows/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ name: AWS Lambda CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:
name: Build Lambda Layer
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Install Packages
run: npm ci
- name: Install Packages
run: npm ci

- name: Compile Typescript
run: npm run build
- name: Compile Typescript
run: npm run build

- name: Create Lambda Layer
run: make chromium.zip
- name: Create Lambda Layer
run: make chromium.zip

- name: Upload Layer Artifact
uses: actions/upload-artifact@v3
with:
name: chromium
path: chromium.zip
- name: Upload Layer Artifact
uses: actions/upload-artifact@v3
with:
name: chromium
path: chromium.zip

execute:
name: Lambda (Node ${{ matrix.version }}.x)
Expand All @@ -46,27 +46,27 @@ jobs:
- 16
- 18
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Setup AWS SAM CLI
uses: aws-actions/setup-sam@v2
- name: Setup AWS SAM CLI
uses: aws-actions/setup-sam@v2

- name: Download Layer Artifact
uses: actions/download-artifact@v3
with:
name: chromium
- name: Download Layer Artifact
uses: actions/download-artifact@v3
with:
name: chromium

- name: Provision Layer
run: unzip chromium.zip -d _/amazon/code
- name: Provision Layer
run: unzip chromium.zip -d _/amazon/code

- name: Install test dependencies
run: npm install --prefix _/amazon/handlers puppeteer-core --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false
- name: Install test dependencies
run: npm install --prefix _/amazon/handlers puppeteer-core --bin-links=false --fund=false --omit=optional --omit=dev --package-lock=false --save=false

- name: Invoke Lambda on SAM
run: sam local invoke --template _/amazon/template.yml --event _/amazon/events/${{ matrix.event }}.json node${{ matrix.version }} 2>&1 | (grep 'Error' && exit 1 || exit 0)
- name: Invoke Lambda on SAM
run: sam local invoke --template _/amazon/template.yml --event _/amazon/events/${{ matrix.event }}.json node${{ matrix.version }} 2>&1 | (grep 'Error' && exit 1 || exit 0)
113 changes: 56 additions & 57 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,70 @@ jobs:
name: Build and release
runs-on: ubuntu-latest
steps:

# Install jq so I can edit package.json from the command line
- run: sudo apt-get install jq -y
- run: sudo apt-get install jq -y

- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org/
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org/

- run: npm ci
- run: npm ci

- run: npm run build
- run: npm run build

- name: Release chromium on npmjs
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- name: Release chromium on npmjs
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

- name: Create Lambda Layer
run: make chromium-${{ github.ref_name }}-layer.zip
- name: Create Lambda Layer
run: make chromium-${{ github.ref_name }}-layer.zip

# Change the package name to chromium-min,
# delete the bin folder from the files array
# so that it doesn't publish the binaries to -min
- name: Cleanup and prepare for chromium-min
run: |
jq '.name="@sparticuz/chromium-min"' package.json > .package.json
jq 'del(.files[] | select(. == "bin"))' .package.json > package.json
jq '.homepage="https://github.com/Sparticuz/chromium#-min-package"' package.json > .package.json
mv .package.json package.json
rm package-lock.json
npm install
# Change the package name to chromium-min,
# delete the bin folder from the files array
# so that it doesn't publish the binaries to -min
- name: Cleanup and prepare for chromium-min
run: |
jq '.name="@sparticuz/chromium-min"' package.json > .package.json
jq 'del(.files[] | select(. == "bin"))' .package.json > package.json
jq '.homepage="https://github.com/Sparticuz/chromium#-min-package"' package.json > .package.json
mv .package.json package.json
rm package-lock.json
npm install
- name: Release chromium-min on npmjs
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
- name: Release chromium-min on npmjs
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

- name: Create Chromium Pack
run: |
cd bin
tar -cvf chromium-${{ github.ref_name }}-pack.tar *
mv chromium-${{ github.ref_name }}-pack.tar ..
cd ..
- name: Create Chromium Pack
run: |
cd bin
tar -cvf chromium-${{ github.ref_name }}-pack.tar *
mv chromium-${{ github.ref_name }}-pack.tar ..
cd ..
- name: Upload items to Github Release
uses: ncipollo/release-action@v1.12.0
with:
tag: ${{ github.ref_name }}
body: |
# [@sparticuz/chromium ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium), [@sparticuz/chromium-min ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium-min)
The `chromium-${{ github.ref_name }}-layer.zip` file may be uploaded directly as a layer in AWS Lambda using the following code
```
bucketName="chromiumUploadBucket" && \
aws s3 cp chromium-${{ github.ref_name }}-layer.zip "s3://${bucketName}/chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" && \
aws lambda publish-layer-version --layer-name chromium --description "Chromium ${{ github.ref_name }}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64
```
The `chromium-${{ github.ref_name }}-pack.tar` file may be uploaded to any https endpoint and the remote location may be used as the `input` variable in the `chromium.executablePath(input)` function.
artifacts: "chromium-${{ github.ref_name }}-layer.zip,chromium-${{ github.ref_name }}-pack.tar"
prerelease: false
draft: true
generateReleaseNotes: true
token: ${{ github.token }}
owner: Sparticuz
repo: chromium
- name: Upload items to Github Release
uses: ncipollo/release-action@v1.12.0
with:
tag: ${{ github.ref_name }}
body: |
# [@sparticuz/chromium ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium), [@sparticuz/chromium-min ${{ github.ref_name }}](https://www.npmjs.com/package/@sparticuz/chromium-min)
The `chromium-${{ github.ref_name }}-layer.zip` file may be uploaded directly as a layer in AWS Lambda using the following code
```
bucketName="chromiumUploadBucket" && \
aws s3 cp chromium-${{ github.ref_name }}-layer.zip "s3://${bucketName}/chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" && \
aws lambda publish-layer-version --layer-name chromium --description "Chromium ${{ github.ref_name }}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-${{ github.ref_name }}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64
```
The `chromium-${{ github.ref_name }}-pack.tar` file may be uploaded to any https endpoint and the remote location may be used as the `input` variable in the `chromium.executablePath(input)` function.
artifacts: "chromium-${{ github.ref_name }}-layer.zip,chromium-${{ github.ref_name }}-pack.tar"
prerelease: false
draft: true
generateReleaseNotes: true
token: ${{ github.token }}
owner: Sparticuz
repo: chromium
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:
npm pack
mkdir -p nodejs/node_modules/@sparticuz/chromium/
tar --directory nodejs/node_modules/@sparticuz/chromium/ --extract --file sparticuz-chromium-*.tgz --strip-components=1
npx clean-modules --directory nodejs --include "**/*.d.ts" "**/@types/**" "**/*.@(yaml|yml)" --yes
npx clean-modules --directory nodejs "**/*.d.ts" "**/@types/**" "**/*.@(yaml|yml)" --yes
rm sparticuz-chromium-*.tgz
mkdir -p $(dir $@)
zip -9 --filesync --move --recurse-paths $@ nodejs
Expand Down
36 changes: 22 additions & 14 deletions _/amazon/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ok } = require('assert');
const { createHash } = require('crypto');
const { ok } = require("assert");
const { createHash } = require("crypto");
const puppeteer = require("puppeteer-core");
const chromium = require('@sparticuz/chromium');
const chromium = require("@sparticuz/chromium");

exports.handler = async (event, context) => {
let browser = null;
Expand All @@ -15,9 +15,9 @@ exports.handler = async (event, context) => {
ignoreHTTPSErrors: true,
});

const contexts = [
browser.defaultBrowserContext(),
];
console.log("Chromium verion", await browser.version());

const contexts = [browser.defaultBrowserContext()];

while (contexts.length < event.length) {
contexts.push(await browser.createIncognitoBrowserContext());
Expand All @@ -27,23 +27,31 @@ exports.handler = async (event, context) => {
const job = event.shift();
const page = await context.newPage();

if (job.hasOwnProperty('url') === true) {
await page.goto(job.url, { waitUntil: ['domcontentloaded', 'load'] });
if (job.hasOwnProperty("url") === true) {
await page.goto(job.url, { waitUntil: ["domcontentloaded", "load"] });

if (job.hasOwnProperty('expected') === true) {
if (job.expected.hasOwnProperty('title') === true) {
ok(await page.title() === job.expected.title, `Title assertion failed.`);
if (job.hasOwnProperty("expected") === true) {
if (job.expected.hasOwnProperty("title") === true) {
ok(
(await page.title()) === job.expected.title,
`Title assertion failed.`
);
}

if (job.expected.hasOwnProperty('screenshot') === true) {
if (job.expected.hasOwnProperty('remove') === true) {
if (job.expected.hasOwnProperty("screenshot") === true) {
if (job.expected.hasOwnProperty("remove") === true) {
await page.evaluate((selector) => {
document.getElementById(selector).remove();
}, job.expected.remove);
}
const screenshot = await page.screenshot();
// console.log(screenshot.toString('base64'), createHash('sha1').update(screenshot.toString('base64')).digest('hex'));
ok(createHash('sha1').update(screenshot.toString('base64')).digest('hex') === job.expected.screenshot, `Screenshot assertion failed.`);
ok(
createHash("sha1")
.update(screenshot.toString("base64"))
.digest("hex") === job.expected.screenshot,
`Screenshot assertion failed.`
);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions _/amazon/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Resources:
layer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sparticuz-chromium
ContentUri: code/
CompatibleRuntimes:
- nodejs16.x
- nodejs18.x
LayerName: sparticuz-chromium
ContentUri: code/
CompatibleRuntimes:
- nodejs16.x
- nodejs18.x

node16:
Type: AWS::Serverless::Function
Expand Down
2 changes: 1 addition & 1 deletion _/ansible/inventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ instance_size=c6i.12xlarge
ansible_connection=ssh
ansible_python_interpreter=auto_silent
ansible_ssh_private_key_file=ansible.pem
chromium_revision=1148114
chromium_revision=1160321
Binary file modified bin/chromium.br
100644 → 100755
Binary file not shown.
Binary file modified bin/swiftshader.tar.br
Binary file not shown.
Loading

0 comments on commit 27ba2ee

Please sign in to comment.