From 236bbd9cb9b721e9b68bcae43c6b16ec95dbc61a Mon Sep 17 00:00:00 2001 From: jake champion Date: Tue, 12 Nov 2024 12:46:06 +0000 Subject: [PATCH] fix: use node apis instead of posix functions in order to support non-posix environments resolves #83 --- .github/workflows/test.yml | 10 +++++++--- index.js | 34 ++++++++++++++++------------------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9661cc9..8dbb0da 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: npm test - - run: HOST='https://deploy-preview-${{ github.event.pull_request.number }}--csp-nonce.netlify.app' npm test + - run: netlify build --offline + - run: npm test -- --reporter=verbose --reporter=hanging-process --reporter=github-actions + - run: HOST='https://deploy-preview-${{ github.event.pull_request.number }}--csp-nonce.netlify.app' npm test -- --reporter=verbose --reporter=hanging-process --reporter=github-actions 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...`);