diff --git a/.github/workflows/aws.yml b/.github/workflows/aws.yml index 2993af5..5753ccc 100644 --- a/.github/workflows/aws.yml +++ b/.github/workflows/aws.yml @@ -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) @@ -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) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e89bcee..c8d1e29 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/_/amazon/handlers/index.js b/_/amazon/handlers/index.js index e1cddf6..f41b3dc 100644 --- a/_/amazon/handlers/index.js +++ b/_/amazon/handlers/index.js @@ -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; @@ -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()); @@ -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.` + ); } } } diff --git a/_/amazon/template.yml b/_/amazon/template.yml index 207ed91..046f903 100644 --- a/_/amazon/template.yml +++ b/_/amazon/template.yml @@ -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 diff --git a/tsconfig.json b/tsconfig.json index c8652df..3fd39c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,17 +1,10 @@ { - "extends": [ - "@tsconfig/node16/tsconfig", - "@tsconfig/strictest" - ], + "extends": ["@tsconfig/node16/tsconfig", "@tsconfig/strictest"], "compilerOptions": { "declaration": true, - "lib": [ - "dom" - ], + "lib": ["dom", "ES2021"], "module": "commonjs", - "outDir": "build", + "outDir": "build" }, - "include": [ - "source" - ] + "include": ["source"] }