Count embedded asset markers across streaming chunk boundaries
This commit is contained in:
parent
ec366ab1ad
commit
f2d98c9fd6
3 changed files with 19 additions and 4 deletions
8
website/tests/build-artifact-scan.mjs
Normal file
8
website/tests/build-artifact-scan.mjs
Normal file
|
|
@ -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.index<boundary).map(m=>m[0]);
|
||||
return {entries,carry:combined.slice(boundary)};
|
||||
}
|
||||
7
website/tests/build-artifact-scan.test.mjs
Normal file
7
website/tests/build-artifact-scan.test.mjs
Normal file
|
|
@ -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<text.length;i+=size){const scan=scanAssetChunk(carry+text.slice(i,i+size));count+=scan.entries.length;carry=scan.carry;}count+=(carry.match(ASSET_ENTRY)||[]).length;assert.equal(count,2,'offset '+offset+' chunk '+size);}
|
||||
}
|
||||
});
|
||||
|
|
@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
|
|||
import {createReadStream,statSync,readdirSync,readFileSync} from 'node:fs';
|
||||
import {MAIN_MENU} from '../scripts/main-menu.mjs';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {scanAssetChunk,ASSET_ENTRY} from './build-artifact-scan.mjs';
|
||||
|
||||
// Validate the production bundle without importing hundreds of megabytes into a test worker.
|
||||
test('built Worker embeds complete public assets and runtime without loading the bundle',async()=>{
|
||||
|
|
@ -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);}
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff