forked from unimatrix/web
80 lines
10 KiB
JavaScript
80 lines
10 KiB
JavaScript
import {recipeCatalogue,recipeIcons} from './recipe-catalogue.mjs';
|
|
import {packageDeploymentScripts} from './deployment-package.mjs';
|
|
import {applySharedHeader,applySharedFooter,SHARED_FOOTER_SCRIPT,cleanLegacyChromeScript,assertSharedChrome,sharedFooterBrandScript} from './site-chrome.mjs';
|
|
import {SHARED_HEADER} from './portal-navigation.mjs';
|
|
import {sharedHomepageHeaderScript} from './homepage-header.mjs';
|
|
import {applyRealmPageDesign} from './realm-page-design.mjs';
|
|
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';
|
|
import {bankRatesPage} from './bank-rates-page.mjs';
|
|
import{withCanonicalMenu,MAIN_MENU}from'./main-menu.mjs';
|
|
import{withMenuBadges}from'./menu-updates.mjs';
|
|
import{updatePortalNavigation}from'./portal-navigation.mjs';
|
|
// Package the authored web and worker sources without rewriting their content.
|
|
import {readFileSync,writeFileSync as writePlain,renameSync,unlinkSync,openSync,writeSync,closeSync,readdirSync,statSync,mkdirSync,existsSync,rmSync,cpSync} from 'node:fs';import path from 'node:path';import {fileURLToPath} from 'node:url';
|
|
function writeFileSync(file,content){const temporary=file+'.build-'+process.pid;writePlain(temporary,content);renameSync(temporary,file);}
|
|
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'),web=path.join(root,'web'),dist=path.join(root,'dist');
|
|
const sharedRealmRuntime=readFileSync(path.join(web,'realm-design.js'),'utf8')+sharedHomepageHeaderScript()+sharedFooterBrandScript();
|
|
const sharedRealmCSS=Buffer.concat([readFileSync(path.join(web,'realm-design.css')),readFileSync(new URL('./homepage-header.css',import.meta.url)),readFileSync(new URL('./osticket-presentation.css',import.meta.url)),readFileSync(new URL('./mybb-presentation.css',import.meta.url)),readFileSync(new URL('./shared-footer.css',import.meta.url))]);
|
|
// Embedded workspaces execute the same runtime through their permitted origin.
|
|
writeFileSync(path.join(root,'control-panel/public/cp-v2/cp-v2-realm-design.mjs'),sharedRealmRuntime);
|
|
// CP entry documents are served directly by the existing CP process. Compile their
|
|
// shared chrome from the same templates as public pages, without altering forms.
|
|
function compileCPChrome(directory){for(const entry of readdirSync(directory,{withFileTypes:true})){const file=path.join(directory,entry.name);if(entry.isDirectory()){if(entry.name!=='native-editor')compileCPChrome(file);continue;}if(!entry.name.endsWith('.html'))continue;const old=readFileSync(file,'utf8');if(!old.includes('dune-site-header'))continue;const next=assertSharedChrome(applySharedFooter(applySharedHeader(old,SHARED_HEADER)),SHARED_HEADER,file);if(next!==old)writeFileSync(file,next);}}
|
|
compileCPChrome(path.join(root,'control-panel/public'));
|
|
// Fail before packaging if a public menu destination or illustration is missing.
|
|
for(const [label,href,icon] of MAIN_MENU){
|
|
const url=new URL(href);
|
|
if(url.hostname==='www.dunerealms.com'){
|
|
const relative=url.pathname==='/'?'index.html':decodeURIComponent(url.pathname.slice(1));
|
|
if(!existsSync(path.join(web,relative)))throw Error('Broken public menu link: '+label+' -> '+relative);
|
|
}
|
|
if(!existsSync(path.join(web,'assets',icon)))throw Error('Missing menu artwork: '+label+' -> '+icon);
|
|
}
|
|
for(const [page,dependencies] of [['shop.html',['shop-landing.css','oscommerce-embed.js']],['kits.html',['kits.css','kits.js']]]){
|
|
for(const file of dependencies)if(!existsSync(path.join(web,file)))throw Error('Missing '+page+' dependency: '+file);
|
|
}
|
|
// Menu badges are now published manually through Imperial Bureau.
|
|
const homepage=readFileSync(path.join(web,'index.html'),'utf8');
|
|
const legalNotice=homepage.match(/<aside class="legal-notice"[\s\S]*?<\/aside>/)?.[0];
|
|
if(!legalNotice)throw Error('Homepage legal notice is missing');
|
|
function preparePublicHtml(html,file){
|
|
html=applyRealmPageDesign(html,file);
|
|
// A single template owns the chrome; page content and service forms remain local.
|
|
html=applySharedHeader(html,SHARED_HEADER,file);
|
|
html=applySharedFooter(html);
|
|
// All page families retain one shared notice, including generated documents.
|
|
let result=file==='index.html'||/<html lang="en" data-dune-(?:portal-preview|realm-shell)>/.test(html)?html:updatePortalNavigation(html);
|
|
if(!result.includes('class="legal-notice"')){
|
|
if(!result.includes('</body>'))throw Error('Public HTML page missing closing body');
|
|
result=result.replace('</body>',legalNotice+'</body>');
|
|
}
|
|
return assertSharedChrome(result,SHARED_HEADER,file);
|
|
}
|
|
// 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'),applyRealmPageDesign(bankRatesPage(),'rates-and-charges.html'));
|
|
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)):relative==='realm-design.js'?Buffer.from(sharedRealmRuntime):relative==='realm-design.css'?sharedRealmCSS:relative==='portal-navigation.js'?Buffer.from(SHARED_FOOTER_SCRIPT+withRealmSwitch(withCanonicalMenu(withMenuBadges(cleanLegacyChromeScript(bytes.toString('utf8')))))+sharedRealmRuntime):relative==='portal-navigation.css'?Buffer.concat([bytes,Buffer.from(HOUSE_STYLE_CSS+REALM_SWITCH_CSS+readFileSync(path.join(web,"realm-comfort.css"),"utf8"))]):bytes;writeAsset(JSON.stringify('/'+relative)+':['+JSON.stringify(type)+','+JSON.stringify(payload.toString('base64'))+'],');assetCount++}}}
|
|
mkdirSync(path.join(dist,'server'),{recursive:true});mkdirSync(path.join(dist,'.openai'),{recursive:true});const workerTemporary=path.join(dist,'server/index.js.build-'+process.pid);process.once('exit',()=>{if(existsSync(workerTemporary))try{unlinkSync(workerTemporary);}catch{}});openShard();collect(web);const recipes=recipeCatalogue();for(const [url,image] of recipeIcons(recipes)){writeAsset(JSON.stringify(url)+":"+JSON.stringify(["image/png",image.toString("base64")])+",");assetCount++;}writeAsset(JSON.stringify('/recipe-catalogue.json')+':['+JSON.stringify(types['.json'])+','+JSON.stringify(Buffer.from(JSON.stringify(recipes)).toString('base64'))+'],');assetCount++;const buildCommit=process.env.GITHUB_SHA;if(buildCommit&&/^[0-9a-f]{40}$/.test(buildCommit)){writeAsset(JSON.stringify('/ci-build.json')+':'+JSON.stringify(['application/json; charset=utf-8',Buffer.from(JSON.stringify({commit:buildCommit})).toString('base64')])+',');}closeShard();
|
|
let api=['public-item-metadata.mjs','feed-api.mjs','exchange-api.mjs','forum-api.mjs','community-api.mjs','portal-api.mjs'].map(file=>readFileSync(path.join(root,'worker',file),'utf8').replace(/^import .*;$/gm,'').replace(/export /g,'')).join('\n');let runtime=readFileSync(path.join(root,'worker/runtime.mjs'),'utf8').replace(/^import .*;$/gm,'');
|
|
writeFileSync(path.join(dist,'server/index.js'),shardNames.map((name,i)=>'import DATA'+i+' from '+JSON.stringify('./'+name)+';').join('\n')+'\nconst ASSET_DATA=Object.assign({},'+shardNames.map((_,i)=>'DATA'+i).join(',')+');\n'+api+'\n'+runtime);cpSync(path.join(root,'.openai/hosting.json'),path.join(dist,'.openai/hosting.json'));console.log('Built Worker with',assetCount,'embedded website assets');
|
|
if(existsSync(path.join(root,'drizzle')))cpSync(path.join(root,'drizzle'),path.join(dist,'.openai/drizzle'),{recursive:true});
|
|
// Native applications consume the same compiled chrome as the public Worker.
|
|
// Operational fixes travel in the tested artifact rather than as live-only edits.
|
|
packageDeploymentScripts(path.join(dist,'deployment'),buildCommit||null);
|
|
|
|
// Native applications consume the same compiled chrome as the public Worker.
|
|
// nginx follows the atomically promoted current release, so no stale host copy
|
|
// can retain an old Alpha version, navigation badge or rates page.
|
|
const shared=path.join(dist,'shared-chrome');mkdirSync(shared,{recursive:true});
|
|
for(const name of ['portal-navigation.js','portal-navigation.css','realm-design.css','release.json','menu-new-items.json','rates-and-charges.html','bank-rates.css']){
|
|
let content=readFileSync(path.join(web,name));
|
|
if(name==='realm-design.css')content=sharedRealmCSS;
|
|
if(name==='portal-navigation.js')content=Buffer.from(SHARED_FOOTER_SCRIPT+withRealmSwitch(withCanonicalMenu(withMenuBadges(cleanLegacyChromeScript(content.toString('utf8')))))+sharedRealmRuntime);
|
|
if(name==='portal-navigation.css')content=Buffer.concat([content,Buffer.from(HOUSE_STYLE_CSS+REALM_SWITCH_CSS+readFileSync(path.join(web,"realm-comfort.css"),"utf8"))]);
|
|
if(name.endsWith('.html'))content=Buffer.from(preparePublicHtml(content.toString('utf8'),name));
|
|
writeFileSync(path.join(shared,name),content);
|
|
}
|

Staff