From 0b1609ee88edcdbe63ce3c6878a1ec345872607e Mon Sep 17 00:00:00 2001 From: unimatrixgit <95320811+unimatrixgit@users.noreply.github.com> Date: Sun, 22 Nov 2026 19:28:27 +0100 Subject: [PATCH] Publish and validate the complete public Exchange module graph --- website/scripts/build.mjs | 9 ++---- website/scripts/public-exchange-assets.mjs | 28 +++++++++++++++++++ .../public-exchange-publication.test.mjs | 10 +++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 website/scripts/public-exchange-assets.mjs create mode 100644 website/tests/public-exchange-publication.test.mjs diff --git a/website/scripts/build.mjs b/website/scripts/build.mjs index 9eddef38..26c75b8f 100644 --- a/website/scripts/build.mjs +++ b/website/scripts/build.mjs @@ -1,3 +1,4 @@ +import {publishExchangeAssets} from './public-exchange-assets.mjs'; import {createHash} from 'node:crypto'; import {withRealmSwitch,REALM_SWITCH_CSS} from './realm-switch.mjs'; import {HOUSE_STYLE_CSS} from './house-style.mjs'; @@ -33,12 +34,8 @@ function preparePublicHtml(html){ } return result; } -// Publish the shared Exchange presentation on the public origin for strict CSP. -for(const [source,target] of [['cp-v2/cp-v2-exchange-design.mjs','codex-exchange.js'],['cp-v2/cp-v2-exchange-design.css','codex-exchange.css'],['organisation-tabs.mjs','codex-tabs.js'],['organisation-tabs.css','codex-tabs.css'],['jarvis-brand.mjs','jarvis-brand.js'],['cp-v2/cp-v2-exchange-cards.mjs','exchange-cards.js'],['cp-v2/cp-v2-exchange-cards.css','exchange-cards.css'],['item-lore-model.mjs','item-lore-model.mjs'],['item-lore-view.mjs','item-lore-view.mjs'],['item-lore.css','item-lore.css']]){ - let content=readFileSync(path.join(root,'control-panel/public',source),'utf8'); - if(target.endsWith('.js'))content=content.replaceAll('../item-lore-model.mjs','./item-lore-model.mjs').replaceAll('../item-lore-view.mjs','./item-lore-view.mjs').replaceAll('../jarvis-brand.mjs','./jarvis-brand.js').replaceAll('../organisation-tabs.mjs','./codex-tabs.js').replaceAll('./organisation-tabs.css','./codex-tabs.css').replaceAll('./cp-v2-exchange-design.css','./codex-exchange.css').replaceAll('./cp-v2-exchange-cards.mjs','./exchange-cards.js').replaceAll('./cp-v2-exchange-cards.css','./exchange-cards.css'); - writeFileSync(path.join(web,target),content); -} +// Publish the full shared Exchange module graph on its public origin. +publishExchangeAssets(path.join(root,'control-panel/public'),web); writeFileSync(path.join(web,'rates-and-charges.html'),bankRatesPage()); let assetCount=0;let assetWriter=null,shardSize=0;const shardNames=[];let shardTemporary;function openShard(){shardTemporary=path.join(dist,'server','.assets-build-'+process.pid);assetWriter=openSync(shardTemporary,'w');writeSync(assetWriter,'export default {');shardSize=0;}function closeShard(){writeSync(assetWriter,'};\n');closeSync(assetWriter);const hash=createHash('sha256').update(readFileSync(shardTemporary)).digest('hex'),name='assets-'+hash+'.mjs',target=path.join(dist,'server',name);if(existsSync(target))unlinkSync(shardTemporary);else renameSync(shardTemporary,target);shardNames.push(name);}function writeAsset(entry){if(shardSize>32*1024*1024){closeShard();openShard();}writeSync(assetWriter,entry);shardSize+=Buffer.byteLength(entry);}const types={'.html':'text/html; charset=utf-8','.css':'text/css; charset=utf-8','.js':'text/javascript; charset=utf-8','.mjs':'text/javascript; charset=utf-8','.json':'application/json; charset=utf-8','.webp':'image/webp','.png':'image/png','.mp4':'video/mp4','.pdf':'application/pdf','.zip':'application/zip','.svg':'image/svg+xml'}; function collect(directory){for(const entry of readdirSync(directory).sort()){const p=path.join(directory,entry);if(statSync(p).isDirectory())collect(p);else{const relative=path.relative(web,p).split(path.sep).join('/');if(/^assets\/seals\/[^/]+-provenance\.txt$/.test(relative)||/^assets\/organisation-deed(?:s)?-[a-z0-9-]+\.png$/.test(relative))continue;const type=types[path.extname(entry)];if(!type)throw Error('Unexpected asset: '+entry);const bytes=readFileSync(p);const payload=relative.endsWith('.html')?Buffer.from(preparePublicHtml(bytes.toString('utf8'))):relative==='portal-navigation.js'?Buffer.from(withRealmSwitch(withCanonicalMenu(withMenuBadges(bytes.toString('utf8'))))):relative==='portal-navigation.css'?Buffer.concat([bytes,Buffer.from(HOUSE_STYLE_CSS+REALM_SWITCH_CSS)]):bytes;writeAsset(JSON.stringify('/'+relative)+':['+JSON.stringify(type)+','+JSON.stringify(payload.toString('base64'))+'],');assetCount++}}} diff --git a/website/scripts/public-exchange-assets.mjs b/website/scripts/public-exchange-assets.mjs new file mode 100644 index 00000000..1f0392a9 --- /dev/null +++ b/website/scripts/public-exchange-assets.mjs @@ -0,0 +1,28 @@ +import {readFileSync,writeFileSync,renameSync} from 'node:fs'; +import path from 'node:path'; +export const EXCHANGE_ASSETS=[ + ['cp-v2/cp-v2-exchange-design.mjs','codex-exchange.js'],['cp-v2/cp-v2-exchange-design.css','codex-exchange.css'], + ['organisation-tabs.mjs','codex-tabs.js'],['organisation-tabs.css','codex-tabs.css'],['jarvis-brand.mjs','jarvis-brand.js'], + ['cp-v2/cp-v2-exchange-cards.mjs','exchange-cards.js'],['cp-v2/cp-v2-exchange-cards.css','exchange-cards.css'], + ...['item-lore-model.mjs','item-lore-view.mjs','item-lore.css','item-design-model.mjs','item-metadata-policy.mjs','tool-template-policy.mjs','equipment-template-policy.mjs','organisation-refit-policy.mjs','mining-tool-policy.mjs','kit-design-model.mjs'].map(name=>[name,name]) +]; +export function publishExchangeAssets(sourceDirectory,targetDirectory){ + const targets=new Map(EXCHANGE_ASSETS); + // Validate the complete import graph before publishing any member of it. + const prepared=EXCHANGE_ASSETS.map(([source,target])=>{ + let content=readFileSync(path.join(sourceDirectory,source),'utf8'); + if(/\.(?:mjs|js)$/.test(source)){ + for(const match of content.matchAll(/\b(?:from\s*|import\s*)(['"])(\.{1,2}\/[^'"]+)\1/g)){ + const dependency=path.posix.normalize(path.posix.join(path.posix.dirname(source),match[2])); + if(!targets.has(dependency))throw Error('Unpublished Exchange dependency: '+source+' → '+dependency); + } + content=content.replace(/(['"])(\.{1,2}\/[^'"]+)\1/g,(original,quote,relative)=>{ + const dependency=path.posix.normalize(path.posix.join(path.posix.dirname(source),relative)); + return targets.has(dependency)?quote+'./'+targets.get(dependency)+quote:original; + }); + } + return [target,content]; + }); + for(const [target,content]of prepared){const file=path.join(targetDirectory,target),temporary=file+'.build-'+process.pid;writeFileSync(temporary,content);renameSync(temporary,file);} + return prepared.map(([target])=>target); +} diff --git a/website/tests/public-exchange-publication.test.mjs b/website/tests/public-exchange-publication.test.mjs new file mode 100644 index 00000000..44c2203c --- /dev/null +++ b/website/tests/public-exchange-publication.test.mjs @@ -0,0 +1,10 @@ +import test from 'node:test';import assert from 'node:assert/strict'; +import {mkdtempSync,readFileSync,existsSync,rmSync,writeFileSync,mkdirSync}from'node:fs';import {tmpdir}from'node:os';import path from'node:path';import {pathToFileURL,fileURLToPath}from'node:url'; +import {publishExchangeAssets}from'../scripts/public-exchange-assets.mjs'; +test('the public Exchange publishes and resolves the same item-template import graph as the CP',async()=>{ + const folder=mkdtempSync(path.join(tmpdir(),'dune-public-exchange-')); + try{const files=publishExchangeAssets(fileURLToPath(new URL('../control-panel/public/',import.meta.url)),folder);assert(files.includes('item-design-model.mjs'));for(const file of files.filter(f=>/\.(mjs|js)$/.test(f))){const source=readFileSync(path.join(folder,file),'utf8');for(const match of source.matchAll(/\bfrom\s*['"](\.[^'"]+)['"]/g))assert(existsSync(path.resolve(folder,match[1])),file+' → '+match[1]);}const model=await import(pathToFileURL(path.join(folder,'item-design-model.mjs')));assert.equal(model.itemDesignTemplate('dr_s1_legendary_mk14_helmet').equipmentMark,14);assert.match(readFileSync(path.join(folder,'codex-exchange.js'),'utf8'),/from '\.\/item-design-model\.mjs'/);}finally{assert(path.resolve(folder).startsWith(path.resolve(tmpdir())+path.sep+'dune-'));rmSync(folder,{recursive:true,force:true});} +}); +test('a missing future import fails the publisher before replacing an existing public module',()=>{ + const folder=mkdtempSync(path.join(tmpdir(),'dune-exchange-import-')),source=path.join(folder,'source'),output=path.join(folder,'output');mkdirSync(path.join(source,'cp-v2'),{recursive:true});mkdirSync(output);writeFileSync(path.join(source,'cp-v2/cp-v2-exchange-design.mjs'),"import {missing} from '../not-published.mjs';");writeFileSync(path.join(output,'codex-exchange.js'),'existing release');try{assert.throws(()=>publishExchangeAssets(source,output),/Unpublished Exchange dependency/);assert.equal(readFileSync(path.join(output,'codex-exchange.js'),'utf8'),'existing release');}finally{assert(path.resolve(folder).startsWith(path.resolve(tmpdir())+path.sep+'dune-'));rmSync(folder,{recursive:true,force:true});} +});