From 30ad15596d68ee8189e26372b3e124175909bf2a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 9 Apr 2025 09:53:57 +0100 Subject: [PATCH 1/4] chore: build hashes of scripts --- packages/astro/csp-hashes.js | 17 +++++++++++++++++ scripts/cmd/prebuild.js | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/astro/csp-hashes.js diff --git a/packages/astro/csp-hashes.js b/packages/astro/csp-hashes.js new file mode 100644 index 000000000000..950fa6727965 --- /dev/null +++ b/packages/astro/csp-hashes.js @@ -0,0 +1,17 @@ +// This file is code-generated, please don't change it manually +export default [ + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/only.prebuilt.ts + "ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/media.prebuilt.ts + "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/load.prebuilt.ts + "XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/visible.prebuilt.ts + "cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/idle.prebuilt.ts + "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/server/astro-island.prebuilt.ts + "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", + // /Users/ema/www/withastro/astro/packages/astro/src/runtime/server/astro-island.prebuilt-dev.ts + "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=" +]; \ No newline at end of file diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index 1c62e813549e..107e90b5c283 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -4,6 +4,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url'; import esbuild from 'esbuild'; import { red } from 'kleur/colors'; import { glob } from 'tinyglobby'; +import crypto from 'node:crypto'; function escapeTemplateLiterals(str) { return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${'); @@ -40,6 +41,8 @@ export default async function prebuild(...args) { return outURL; } + const hashes = new Map(); + async function prebuildFile(filepath) { let tscode = await fs.promises.readFile(filepath, 'utf-8'); // If we're bundling a client directive, modify the code to match `packages/astro/src/core/client-directive/build.ts`. @@ -99,17 +102,33 @@ export default async function prebuild(...args) { const code = result.build.outputFiles[0].text.trim(); const rootURL = new URL('../../', import.meta.url); const rel = path.relative(fileURLToPath(rootURL), filepath); + const generatedCode = escapeTemplateLiterals(code); const mod = `/** * This file is prebuilt from ${rel} * Do not edit this directly, but instead edit that file and rerun the prebuild * to generate this file. */ -export default \`${escapeTemplateLiterals(code)}\`;`; +export default \`${generatedCode}\`;`; const url = getPrebuildURL(filepath, result.dev); await fs.promises.writeFile(url, mod, 'utf-8'); + const hash = crypto.createHash('sha256').update(code).digest('base64'); + hashes.set(fileURLToPath(url), hash); } } await Promise.all(entryPoints.map(prebuildFile)); + const entries = []; + for (const [url, hash] of hashes.entries()) { + entries.push(`// ${url}\n "${hash}"`); + } + const content = `// This file is code-generated, please don't change it manually +export default [ + ${entries.join(',\n ')} +];`; + await fs.promises.writeFile( + path.join(fileURLToPath(import.meta.url), '../../../packages/astro/', 'csp-hashes.js'), + content, + 'utf-8', + ); } From 5f45492c9b83e829528a6c38ccffa61e721f8d27 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 9 Apr 2025 13:38:45 +0100 Subject: [PATCH 2/4] chore: fix changes --- packages/astro/csp-hashes.js | 21 +++++++-------------- scripts/cmd/prebuild.js | 9 +++------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/packages/astro/csp-hashes.js b/packages/astro/csp-hashes.js index 950fa6727965..4da298312c57 100644 --- a/packages/astro/csp-hashes.js +++ b/packages/astro/csp-hashes.js @@ -1,17 +1,10 @@ // This file is code-generated, please don't change it manually export default [ - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/only.prebuilt.ts - "ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/media.prebuilt.ts - "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/load.prebuilt.ts - "XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/visible.prebuilt.ts - "cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/client/idle.prebuilt.ts - "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/server/astro-island.prebuilt.ts - "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", - // /Users/ema/www/withastro/astro/packages/astro/src/runtime/server/astro-island.prebuilt-dev.ts - "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=" + "XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=", + "ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=", + "cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=", + "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", + "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", + "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", + "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=" ]; \ No newline at end of file diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index 107e90b5c283..1ab55edab1ff 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -41,7 +41,7 @@ export default async function prebuild(...args) { return outURL; } - const hashes = new Map(); + const hashes = []; async function prebuildFile(filepath) { let tscode = await fs.promises.readFile(filepath, 'utf-8'); @@ -113,15 +113,12 @@ export default \`${generatedCode}\`;`; const url = getPrebuildURL(filepath, result.dev); await fs.promises.writeFile(url, mod, 'utf-8'); const hash = crypto.createHash('sha256').update(code).digest('base64'); - hashes.set(fileURLToPath(url), hash); + hashes.push(hash); } } await Promise.all(entryPoints.map(prebuildFile)); - const entries = []; - for (const [url, hash] of hashes.entries()) { - entries.push(`// ${url}\n "${hash}"`); - } + const entries = hashes.map((hash) => `"${hash}"`); const content = `// This file is code-generated, please don't change it manually export default [ ${entries.join(',\n ')} From 2030f8ed6fea5c4b6958c052f8f9150129fcf71a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 9 Apr 2025 13:39:13 +0100 Subject: [PATCH 3/4] chore: fix changes --- packages/astro/csp-hashes.js | 4 ++-- scripts/cmd/prebuild.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/astro/csp-hashes.js b/packages/astro/csp-hashes.js index 4da298312c57..bdc5499f94d0 100644 --- a/packages/astro/csp-hashes.js +++ b/packages/astro/csp-hashes.js @@ -1,10 +1,10 @@ // This file is code-generated, please don't change it manually export default [ + "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", + "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", "XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=", "ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=", "cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=", - "GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=", - "HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=", "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=", "mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=" ]; \ No newline at end of file diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index 1ab55edab1ff..e0fd8b4d0dd9 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -118,6 +118,7 @@ export default \`${generatedCode}\`;`; } await Promise.all(entryPoints.map(prebuildFile)); + hashes.sort(); const entries = hashes.map((hash) => `"${hash}"`); const content = `// This file is code-generated, please don't change it manually export default [ From 8256c9ec2e0145e12c8635933690f59a82badf02 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 9 Apr 2025 13:41:24 +0100 Subject: [PATCH 4/4] chore: fix changes --- packages/astro/{ => src/core}/csp-hashes.js | 0 scripts/cmd/prebuild.js | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename packages/astro/{ => src/core}/csp-hashes.js (100%) diff --git a/packages/astro/csp-hashes.js b/packages/astro/src/core/csp-hashes.js similarity index 100% rename from packages/astro/csp-hashes.js rename to packages/astro/src/core/csp-hashes.js diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js index e0fd8b4d0dd9..e7bca657a3c6 100644 --- a/scripts/cmd/prebuild.js +++ b/scripts/cmd/prebuild.js @@ -125,7 +125,7 @@ export default [ ${entries.join(',\n ')} ];`; await fs.promises.writeFile( - path.join(fileURLToPath(import.meta.url), '../../../packages/astro/', 'csp-hashes.js'), + path.join(fileURLToPath(import.meta.url), '../../../packages/astro/src/core', 'csp-hashes.js'), content, 'utf-8', );