diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9661cc9..d63aff2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,10 @@ defaults: jobs: test-npm-package: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 @@ -20,5 +23,6 @@ jobs: cache: npm - run: npm install netlify-cli -g - run: npm install + - run: netlify build --offline - run: npm test - run: HOST='https://deploy-preview-${{ github.event.pull_request.number }}--csp-nonce.netlify.app' npm test diff --git a/index.js b/index.js index eca4ee6..991f348 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,10 @@ /* eslint-disable no-console */ -import fs, { copyFileSync } from "node:fs"; +import { copyFile, mkdir, writeFile } from "node:fs/promises"; import { dirname, resolve } from "node:path"; -import { fileURLToPath } from 'node:url' +import { fileURLToPath } from "node:url"; import { getBuildInfo } from "@netlify/build-info/node"; -const __dirname = dirname(fileURLToPath(import.meta.url)) +const __dirname = dirname(fileURLToPath(import.meta.url)); async function projectUsesNextJS() { for (const framework of (await getBuildInfo()).frameworks) { @@ -18,7 +18,6 @@ async function projectUsesNextJS() { export const onPreBuild = async ({ inputs, netlifyConfig, - utils, constants, }) => { const { build } = netlifyConfig; @@ -36,10 +35,9 @@ export const onPreBuild = async ({ // but 0 to 100 is also supported, along with a trailing % const distribution = build.environment.CSP_NONCE_DISTRIBUTION; if (!!distribution) { - const threshold = - distribution.endsWith("%") || parseFloat(distribution) > 1 - ? Math.max(parseFloat(distribution) / 100, 0) - : Math.max(parseFloat(distribution), 0); + const threshold = distribution.endsWith("%") || parseFloat(distribution) > 1 + ? Math.max(parseFloat(distribution) / 100, 0) + : Math.max(parseFloat(distribution), 0); console.log(` CSP_NONCE_DISTRIBUTION is set to ${threshold * 100}%`); if (threshold === 0) { console.log(` Skipping.`); @@ -50,13 +48,13 @@ export const onPreBuild = async ({ console.log(` Current working directory: ${process.cwd()}`); // make the directory in case it actually doesn't exist yet - await utils.run.command(`mkdir -p ${INTERNAL_EDGE_FUNCTIONS_SRC}`); + await mkdir(INTERNAL_EDGE_FUNCTIONS_SRC, {recursive: true}); console.log( - ` Writing nonce edge function to ${INTERNAL_EDGE_FUNCTIONS_SRC}...` + ` Writing nonce edge function to ${INTERNAL_EDGE_FUNCTIONS_SRC}...`, ); - copyFileSync( + await copyFile( resolve(__dirname, `./src/__csp-nonce.ts`), - `${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce.ts` + `${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce.ts`, ); const usesNext = await projectUsesNextJS(); @@ -69,21 +67,21 @@ export const onPreBuild = async ({ inputs.excludedPath.push("/_next/image"); } - fs.writeFileSync( + await writeFile( `${INTERNAL_EDGE_FUNCTIONS_SRC}/__csp-nonce-inputs.json`, - JSON.stringify(inputs, null, 2) + JSON.stringify(inputs, null, 2), ); // if no reportUri in config input, deploy function on site's behalf if (!inputs.reportUri) { // make the directory in case it actually doesn't exist yet - await utils.run.command(`mkdir -p ${INTERNAL_FUNCTIONS_SRC}`); + await mkdir(INTERNAL_FUNCTIONS_SRC, { recursive: true }); console.log( - ` Writing violations logging function to ${INTERNAL_FUNCTIONS_SRC}...` + ` Writing violations logging function to ${INTERNAL_FUNCTIONS_SRC}...`, ); - copyFileSync( + await copyFile( resolve(__dirname, `./src/__csp-violations.ts`), - `${INTERNAL_FUNCTIONS_SRC}/__csp-violations.ts` + `${INTERNAL_FUNCTIONS_SRC}/__csp-violations.ts`, ); } else { console.log(` Using ${inputs.reportUri} as report-uri directive...`); diff --git a/tests/integration/helpers.ts b/tests/integration/helpers.ts index 50f55a0..0ac822b 100644 --- a/tests/integration/helpers.ts +++ b/tests/integration/helpers.ts @@ -1,7 +1,3 @@ -import process from "node:process"; - -export const baseURL = process.env.HOST || "http://127.0.0.1:8888"; - import { Buffer } from "node:buffer"; import { ExecaError, execa } from "execa"; import getPort from "get-port";