Serve nested CP component imports through the bounded module router

This commit is contained in:
unimatrixgit 2026-11-16 22:36:38 +01:00
commit a14ab1fdd0
2 changed files with 3 additions and 3 deletions

View file

@ -281,8 +281,8 @@ export function createPanel(db,settings={},dependencies={}){
if(path==='/politics-cp')return new Response(request.method==='HEAD'?null:politicsCPDocument(await readFile(new URL('./public/cp-v2/index.html',import.meta.url),'utf8')),{headers:{'Content-Type':'text/html; charset=utf-8','Cache-Control':'no-store','X-Content-Type-Options':'nosniff'}});
// All authored cp-v2 browser modules share this bounded public directory.
// New components must not require a second, manually maintained route list.
if(/^\/v2\/cp-v2-[a-z0-9-]+\.(?:mjs|css)$/.test(path)){
try{const bytes=await readFile(new URL('./public/cp-v2/'+path.slice('/v2/'.length),import.meta.url));return new Response(request.method==='HEAD'?null:bytes,{headers:{'Content-Type':path.endsWith('.css')?'text/css; charset=utf-8':'text/javascript; charset=utf-8','Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff'}});}catch(error){if(error.code==='ENOENT')return new Response('Not found',{status:404});throw error;}
if(/^\/(?:v2|cp-v2)\/cp-v2-[a-z0-9-]+\.(?:mjs|css)$/.test(path)){
try{const bytes=await readFile(new URL('./public/cp-v2/'+path.slice(path.lastIndexOf('/')+1),import.meta.url));return new Response(request.method==='HEAD'?null:bytes,{headers:{'Content-Type':path.endsWith('.css')?'text/css; charset=utf-8':'text/javascript; charset=utf-8','Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff'}});}catch(error){if(error.code==='ENOENT')return new Response('Not found',{status:404});throw error;}
}
if(['/v2/citizen-directory.mjs','/v2/citizen-directory.css','/v2/organisation-permissions.mjs'].includes(path))return new Response(request.method==='HEAD'?null:await readFile(new URL('./public/cp-v2/'+path.slice('/v2/'.length),import.meta.url)),{headers:{'Content-Type':path.endsWith('.css')?'text/css; charset=utf-8':'text/javascript; charset=utf-8','Cache-Control':'private, no-store','X-Content-Type-Options':'nosniff'}});
if(['/','/index.html','/v2','/v2/','/v2/cp-v2-politics-pages.mjs','/v2/cp-v2-auth.mjs','/v2/cp-v2-access.mjs','/v2/citizens-registry.mjs','/v2/cp-v2-page-template.mjs','/v2/imperial-permissions.mjs','/v2/honours-seals.mjs','/v2/cp-v2-planned-pages.mjs','/v2/cp-v2-choam-pages.mjs','/v2/cp-v2-bureau-pages.mjs','/v2/cp-v2-service-pages.mjs','/v2/cp-v2-newsroom.mjs','/v2/cp-v2-moderation.mjs','/v2/cp-v2-native-services.mjs','/v2/cp-v2-exchange-authority.mjs','/v2/cp-v2-crysknife.mjs','/v2/cp-v2-post-menu.mjs','/v2/cp-v2-postbank-foundation.mjs','/v2/cp-v2-postbank-payments.mjs','/v2/cp-v2-postbank-admin.mjs','/v2/cp-v2-authorisations.mjs','/v2/cp-v2-tax-treasury.mjs','/v2/cp-v2-reserve-ledger.mjs','/v2/cp-v2-reserve-entry.mjs','/v2/cp-v2-postbank-credit.mjs','/v2/cp-v2-postbank-beneficiaries.mjs','/v2/cp-v2-postbank-statement-archive.mjs','/v2/cp-v2-bank-pages.mjs','/v2/cp-v2-post-pages.mjs','/v2/cp-v2-code-redemption.mjs','/v2/cp-v2-rewards.mjs','/v2/cp-v2-user-commerce.mjs','/v2/cp-v2-user-pages.mjs','/v2/cp-v2-user-api.mjs','/v2/cp-v2-security.mjs','/v2/cp-v2-passport.mjs','/v2/cp-v2-personal-records.mjs','/v2/cp-v2-design-pages.mjs','/v2/cp-v2-design-catalog.mjs','/v2/cp-v2-forge-pages.mjs','/v2/cp-v2-hosting.mjs','/v2/cp-v2-community-treasury.mjs','/v2/cp-v2-item-register.mjs','/v2/cp-v2-kit-directorate.mjs','/v2/cp-v2-campaigns.mjs','/v2/cp-v2-imperial-revenue.mjs','/v2/cp-v2-read-register.mjs','/v2/cp-v2-bureau-data.mjs','/v2/cp-v2-company-actions.mjs','/v2/v2.css','/v2/v2.js'].includes(path)){

View file

@ -3,7 +3,7 @@ import {createPanel} from '../control-panel/server.mjs';import {LocalD1} from '.
test('every shipped CP browser component is reachable through the actual panel router',async()=>{
const db=new LocalD1(),app=createPanel(db);try{
const files=readdirSync(new URL('../control-panel/public/cp-v2/',import.meta.url)).filter(name=>/^cp-v2-[a-z0-9-]+\.mjs$/.test(name));
for(const file of files){const response=await app(new Request('https://cp.dunerealms.com/v2/'+file));assert.equal(response.status,200,file);assert.match(response.headers.get('Content-Type'),/javascript/);assert((await response.text()).length>0,file);}
for(const prefix of ['/v2/','/cp-v2/'])for(const file of files){const response=await app(new Request('https://cp.dunerealms.com'+prefix+file));assert.equal(response.status,200,prefix+file);assert.match(response.headers.get('Content-Type'),/javascript/);assert((await response.text()).length>0,file);}
for(const path of ['/v2/cp-v2-missing.mjs','/v2/../server.mjs','/v2/cp-v2-..%2fserver.mjs','/v2/cp-v2-x.env'])assert.equal((await app(new Request('https://cp.dunerealms.com'+path))).status,404,path);
}finally{db.close();}
});