82 lines
8.2 KiB
JavaScript
82 lines
8.2 KiB
JavaScript
'use strict';
|
|
const {test}=require('node:test'),assert=require('node:assert/strict');
|
|
const fs=require('node:fs'),os=require('node:os'),path=require('node:path'),http=require('node:http'),zlib=require('node:zlib');
|
|
const {Store}=require('../../src/modules/dashboard/store.cjs');
|
|
const {createDashboard}=require('../../src/app/dashboard.cjs');
|
|
const {createAssetDelivery}=require('../../src/app/asset-delivery.cjs');
|
|
async function fixture(t){
|
|
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'asset-delivery-')),store=new Store(dir),server=createDashboard({store,outputDir:dir});
|
|
await new Promise(r=>server.listen(0,'127.0.0.1',r));
|
|
t.after(async()=>{server.closeAllConnections();await new Promise(r=>server.close(r));fs.rmSync(dir,{recursive:true,force:true});});
|
|
return (route,headers={},method='GET')=>new Promise((resolve,reject)=>{const request=http.request({hostname:'127.0.0.1',port:server.address().port,path:route,headers,method},response=>{const parts=[];response.on('data',part=>parts.push(part));response.on('end',()=>resolve({status:response.statusCode,headers:response.headers,bytes:Buffer.concat(parts)}));});request.on('error',reject);request.end();});
|
|
}
|
|
test('public assets compress losslessly and conditional reload sends no body while APIs stay private',async t=>{
|
|
const get=await fixture(t),plain=await get('/app.js'),compressed=await get('/app.js',{'Accept-Encoding':'br, gzip'});
|
|
assert.equal(compressed.status,200);assert.equal(compressed.headers['content-encoding'],'br');
|
|
assert.deepEqual(zlib.brotliDecompressSync(compressed.bytes),plain.bytes);assert.ok(compressed.bytes.length<plain.bytes.length/2);
|
|
assert.match(compressed.headers.vary,/Accept-Encoding/i);assert.ok(plain.headers.etag);assert.match(plain.headers['cache-control'],/must-revalidate/);
|
|
const unchanged=await get('/app.js',{'If-None-Match':plain.headers.etag,'Accept-Encoding':'gzip'});
|
|
assert.equal(unchanged.status,304);assert.equal(unchanged.bytes.length,0);
|
|
const api=await get('/api/me',{'Accept-Encoding':'br'});assert.equal(api.status,401);assert.equal(api.headers['cache-control'],'no-store');assert.equal(api.headers.etag,undefined);
|
|
const html=await get('/');assert.match(html.headers['cache-control'],/must-revalidate/);assert.ok(html.bytes.includes(Buffer.from('<!doctype html>')));
|
|
});
|
|
test('asset encoding negotiation honors q=0 and HEAD returns metadata without payload',async t=>{
|
|
const get=await fixture(t),gzip=await get('/app.css',{'Accept-Encoding':'br;q=0, gzip;q=0.7'});
|
|
assert.equal(gzip.headers['content-encoding'],'gzip');assert.ok(zlib.gunzipSync(gzip.bytes).length>gzip.bytes.length);
|
|
const plain=await get('/app.css',{'Accept-Encoding':'br;q=0, gzip;q=0'});assert.equal(plain.headers['content-encoding'],undefined);
|
|
const preferred=await get('/app.css',{'Accept-Encoding':'gzip;q=0.1, identity;q=1'});assert.equal(preferred.headers['content-encoding'],undefined);
|
|
const head=await get('/app.css',{'Accept-Encoding':'gzip'},'HEAD');assert.equal(head.status,200);assert.equal(head.bytes.length,0);assert.equal(Number(head.headers['content-length']),gzip.bytes.length);
|
|
const none=await get('/app.css',{'Accept-Encoding':'br;q=0,gzip;q=0,identity;q=0,*;q=0'});assert.equal(none.status,406);
|
|
const preview=await get('/storage-preview.js',{'Accept-Encoding':'gzip'});assert.match(preview.headers['content-security-policy'],/frame-ancestors 'self'/);assert.equal(preview.headers['access-control-allow-origin'],'*');
|
|
});
|
|
test('public asset cache revalidates changed files and concurrent reloads agree',async t=>{
|
|
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'asset-edit-')),file=path.join(dir,'app.js'),deliver=createAssetDelivery({maxBytes:4096});fs.writeFileSync(file,'old application');t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));
|
|
async function get(etag){const headers={};let status,bytes;await deliver({method:'GET',headers:{'if-none-match':etag}},{setHeader:(key,value)=>headers[key]=value,writeHead:value=>status=value,end:value=>bytes=value},{path:file,type:'text/javascript'});return {headers,status,bytes};}
|
|
const first=await get();fs.writeFileSync(file,'new application with changed size');
|
|
const results=await Promise.all(Array.from({length:8},()=>get(first.headers.ETag)));
|
|
for(const result of results){assert.equal(result.status,200);assert.equal(result.bytes.toString(),'new application with changed size');assert.notEqual(result.headers.ETag,first.headers.ETag);}
|
|
assert.equal((await get(results[0].headers.ETag)).status,304);
|
|
});
|
|
|
|
test('asset URLs become immutable only when their content fingerprint is verified',async t=>{
|
|
const get=await fixture(t),plain=await get('/app.js');
|
|
const fingerprint=plain.headers['x-asset-version'];
|
|
assert.match(fingerprint,/^[0-9a-f]{64}$/);
|
|
assert.equal(plain.headers['cache-control'],'public, max-age=0, must-revalidate');
|
|
|
|
const versioned=await get('/app.js?v='+fingerprint);
|
|
assert.equal(versioned.status,200);
|
|
assert.equal(versioned.headers['x-asset-version'],fingerprint);
|
|
assert.equal(versioned.headers['cache-control'],'public, max-age=31536000, immutable');
|
|
|
|
const untrusted=await get('/app.js?v=not-the-content-fingerprint');
|
|
assert.equal(untrusted.status,200);
|
|
assert.equal(untrusted.headers['cache-control'],'public, max-age=0, must-revalidate');
|
|
});
|
|
|
|
test('HTML uses actual fingerprint URLs while the document itself always revalidates',async t=>{
|
|
const get=await fixture(t),page=await get('/'),html=page.bytes.toString();
|
|
const urls=[...html.matchAll(/(?:src|href)="(\/[^"#]+)"/g)].map(match=>match[1]);assert.ok(urls.length>0&&urls.length<20,'core shell stays below twenty eager assets');
|
|
for(const url of urls){assert.match(url,/\?v=[a-f0-9]{64}$/);const asset=await get(url);assert.equal(asset.status,200);assert.match(asset.headers['cache-control'],/immutable/);assert.equal(new URL(url,'http://local').searchParams.get('v'),asset.headers['x-asset-version']);}
|
|
const versioned=await get('/?v='+page.headers['x-asset-version']);assert.match(versioned.headers['cache-control'],/must-revalidate/);assert.doesNotMatch(versioned.headers['cache-control'],/immutable/);
|
|
});
|
|
|
|
test('HTML publishes fingerprint URLs for lazy modules without eagerly loading optional pages',async t=>{
|
|
const get=await fixture(t),page=await get('/'),html=page.bytes.toString();
|
|
const encoded=/<meta name="app-modules" content="([^"]+)"/.exec(html)?.[1];assert.ok(encoded,'lazy module manifest must be available in the document');
|
|
const manifest=JSON.parse(encoded.replaceAll('"','"').replaceAll('<','<').replaceAll('>','>').replaceAll('&','&'));
|
|
for(const route of ['/assistant.js','/business-home.js','/order-rules.js']){
|
|
assert.ok(manifest.urls[route]);assert.equal(html.includes('<script src="'+manifest.urls[route]+'"'),false);
|
|
const asset=await get(manifest.urls[route]);assert.equal(asset.status,200);assert.match(asset.headers['cache-control'],/immutable/);assert.equal(new URL(manifest.urls[route],'http://local').searchParams.get('v'),asset.headers['x-asset-version']);
|
|
}
|
|
});
|
|
|
|
test('changing one dependency changes its URL and HTML ETag without invalidating other assets',async t=>{
|
|
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'asset-dependency-'));t.after(()=>fs.rmSync(dir,{recursive:true,force:true}));
|
|
const documentPath=path.join(dir,'index.html'),one=path.join(dir,'one.js'),two=path.join(dir,'two.css');fs.writeFileSync(documentPath,'<!doctype html><link href="/two.css" rel="stylesheet"><script src="/one.js" defer></script>');fs.writeFileSync(one,'old()');fs.writeFileSync(two,'body{}');
|
|
const assets={'/one.js':{path:one,type:'text/javascript'},'/two.css':{path:two,type:'text/css'}},deliver=createAssetDelivery({resolveAsset:url=>assets[url]});
|
|
async function get(tag){const headers={};let bytes,status;await deliver({url:'/',method:'GET',headers:{'if-none-match':tag}},{setHeader:(key,value)=>headers[key]=value,writeHead:value=>status=value,end:value=>bytes=value},{path:documentPath,type:'text/html'});return {headers,status,bytes};}
|
|
const first=await get();fs.writeFileSync(one,'updated()');const second=await get(first.headers.ETag);assert.equal(second.status,200);assert.notEqual(second.headers.ETag,first.headers.ETag);
|
|
const refs=value=>[...value.bytes.toString().matchAll(/(?:src|href)="([^"]+)"/g)].map(match=>match[1]);const a=refs(first),b=refs(second);assert.equal(a[0],b[0]);assert.notEqual(a[1],b[1]);assert.equal((await get(second.headers.ETag)).status,304);
|
|
});
|