diff --git a/website/tests/build-artifact-scan.mjs b/website/tests/build-artifact-scan.mjs new file mode 100644 index 00000000..b3cbfa57 --- /dev/null +++ b/website/tests/build-artifact-scan.mjs @@ -0,0 +1,8 @@ +export const ASSET_ENTRY=/"\/(?:[^"\\]|\\.)+":\["[^"\\]+","/g; +export function scanAssetChunk(combined,overlap=512){ + const boundary=Math.max(0,combined.length-overlap); + // Match the full buffer, then assign a marker to the chunk containing its + // start. Cutting the buffer first loses markers that straddle the boundary. + const entries=[...combined.matchAll(ASSET_ENTRY)].filter(m=>m.indexm[0]); + return {entries,carry:combined.slice(boundary)}; +} diff --git a/website/tests/build-artifact-scan.test.mjs b/website/tests/build-artifact-scan.test.mjs new file mode 100644 index 00000000..d48162ae --- /dev/null +++ b/website/tests/build-artifact-scan.test.mjs @@ -0,0 +1,7 @@ +import test from 'node:test';import assert from 'node:assert/strict';import {scanAssetChunk,ASSET_ENTRY} from './build-artifact-scan.mjs'; +test('streaming asset count retains markers crossing the safe-boundary cut',()=>{ + for(let offset=0;offset<100;offset++){ + const text='x'.repeat(offset)+'"/index.html":["text/html; charset=utf-8","'+'A'.repeat(2500)+'"],"/portal-navigation.js":["text/javascript; charset=utf-8","'+'B'.repeat(1700)+'"]'; + for(const size of [513,550,1024]){let carry='',count=0;for(let i=0;i{ @@ -11,16 +12,15 @@ test('built Worker embeds complete public assets and runtime without loading the let count=0,prefix='',suffix='',carry=''; const required=new Set(['/index.html','/kits.html','/manual-data.json','/assets/item-0.webp','/kits.js','/kits.css']); const found=new Set(); - const entry=/"\/(?:[^"\\]|\\.)+":\["[^"\\]+","/g; + const entry=ASSET_ENTRY; for await(const chunk of createReadStream(path,{highWaterMark:65536})){ const text=chunk.toString('latin1'); if(prefix.length<256)prefix+=text.slice(0,256-prefix.length); suffix=(suffix+text).slice(-4096); const combined=carry+text; - const safe=combined.slice(0,Math.max(0,combined.length-512)); - const entries=safe.match(entry)||[];count+=entries.length; + const scanned=scanAssetChunk(combined),entries=scanned.entries;count+=entries.length; for(const match of entries){const key=JSON.parse(match.slice(0,match.indexOf(':')));if(required.has(key))found.add(key);} - carry=combined.slice(safe.length); + carry=scanned.carry; } const remaining=carry.match(entry)||[];count+=remaining.length; for(const match of remaining){const key=JSON.parse(match.slice(0,match.indexOf(':')));if(required.has(key))found.add(key);}