Skip to content

fix: use node apis instead of posix functions in order to support non-posix environments #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
34 changes: 16 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -18,7 +18,6 @@ async function projectUsesNextJS() {
export const onPreBuild = async ({
inputs,
netlifyConfig,
utils,
constants,
}) => {
const { build } = netlifyConfig;
Expand All @@ -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.`);
Expand All @@ -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();
Expand All @@ -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...`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/netlify/plugin-csp-nonce#readme",
"scripts": {
"build": "tsc src/*.ts --noEmit --strict --lib es2018,dom",
"test": "vitest"
"test": "vitest --test-timeout=60000 --hook-timeout=60000"
},
"dependencies": {
"@netlify/build-info": "^7.15.2"
Expand Down
12 changes: 7 additions & 5 deletions tests/integration/helpers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -47,6 +43,7 @@ export const serve = async ({

if (Buffer.isBuffer(data)) {
const message = stripAnsi(data.toString("utf8"));
console.log({message})

if (
message.includes(
Expand All @@ -61,11 +58,16 @@ export const serve = async ({
/◈ Loaded edge function (?<name>[\w-]+)/.exec(message)?.groups
?.name ?? null;

console.log({match})

if (match !== null && functionsReady.has(match)) {
console.log('debug 1')
functionsReady.set(match, true);
}
}


console.log('hasServerStarted', hasServerStarted)
console.log('Array.from(functionsReady.values()).every(Boolean)', Array.from(functionsReady.values()).every(Boolean))
if (
hasServerStarted &&
Array.from(functionsReady.values()).every(Boolean)
Expand Down
Loading
Loading