Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix composition preview crash on bundler asset URLs built from import.meta.url
[._.]/ Adam Eivy237c613849a7
diff --git a/server/services/musicVideo/documentModules.js b/server/services/musicVideo/documentModules.jsindex 831f306cde..c39f52a59e 100644--- a/server/services/musicVideo/documentModules.js+++ b/server/services/musicVideo/documentModules.js@@ -4,6 +4,32 @@ import { posix } from 'node:path';import { parse } from '@babel/parser';import { ServerError } from '../../lib/errorHandler.js';+/**+ * A data: module has no usable `import.meta.url`, so bundler asset URLs built from it — Vite's+ * `new URL('photo-abc.jpg', import.meta.url)` — throw "cannot be parsed as a URL" in the preview.+ * Each inlined module instead sees a stand-in base under this reserved origin, at its own path in+ * the document; the preview bootstrap maps URLs under it back to document files (see documentPreview.js).+ */+export const PREVIEW_DOCUMENT_BASE = 'https://document.portos.invalid/';++// `import.meta.url` member expressions anywhere in the module (computed `import.meta['url']` included).+function importMetaUrlRanges(node, out = []) {+ if (!node || typeof node !== 'object') return out;+ if (Array.isArray(node)) { for (const child of node) importMetaUrlRanges(child, out); return out; }+ if (node.type === 'MemberExpression' && node.object?.type === 'MetaProperty'+ && node.object.meta?.name === 'import' && node.object.property?.name === 'meta'+ && ((!node.computed && node.property?.name === 'url') || (node.computed && node.property?.value === 'url'))) {+ out.push({ start: node.start, end: node.end });+ return out;+ }+ for (const key of Object.keys(node)) {+ if (key === 'loc' || key === 'start' || key === 'end' || key === 'extra' || key === 'comments' || key.endsWith('Comments')) continue;+ const child = node[key];+ if (child && typeof child === 'object') importMetaUrlRanges(child, out);+ }+ return out;+}+export async function inlineDocumentModule(entry, files, stack = new Set(), cache = new Map()) {if (cache.has(entry)) return cache.get(entry);const file = files.get(entry);@@ -20,6 +46,8 @@ export async function inlineDocumentModule(entry, files, stack = new Set(), cachconst url = await inlineDocumentModule(key, files, nextStack, cache);replacements.push({ start: node.source.start, end: node.source.end, value: JSON.stringify(url) });}+ const base = JSON.stringify(`${PREVIEW_DOCUMENT_BASE}${entry}`);+ for (const range of importMetaUrlRanges(ast.program)) replacements.push({ ...range, value: base });const expandedSize = replacements.reduce((size, item) => size + item.value.length - (item.end - item.start), source.length);if (expandedSize > 24 * 1024 * 1024) throw new ServerError('The expanded module graph exceeds the preview budget', { status: 413, code: 'COMPOSITION_MODULE_TOO_LARGE' });let output = source;diff --git a/server/services/musicVideo/documentModules.test.js b/server/services/musicVideo/documentModules.test.jsnew file mode 100644index 0000000000..2c8d9cd7bc--- /dev/null+++ b/server/services/musicVideo/documentModules.test.js@@ -0,0 +1,29 @@+import { describe, it, expect } from 'vitest';+import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule } from './documentModules.js';++const decode = (url) => Buffer.from(url.slice(url.indexOf(',') + 1), 'base64').toString('utf8');+const graph = (entries) => new Map(Object.entries(entries).map(([k, v]) => [k, { data: Buffer.from(v) }]));++describe('inlineDocumentModule', () => {+ it('gives each inlined module a stand-in import.meta.url at its own document path', async () => {+ // Vite's default form for a JS-referenced asset under base './'+ const files = graph({ 'assets/index.js': 'const a = new URL(`photo-abc.jpg`, import.meta.url).href; const b = import.meta["url"]; export { a, b };' });+ const out = decode(await inlineDocumentModule('assets/index.js', files));+ const base = JSON.stringify(`${PREVIEW_DOCUMENT_BASE}assets/index.js`);+ expect(out).toBe(`const a = new URL(\`photo-abc.jpg\`, ${base}).href; const b = ${base}; export { a, b };`);+ // and that base resolves the asset to a URL the preview bootstrap maps back to the document file+ expect(new URL('photo-abc.jpg', JSON.parse(base)).href).toBe(`${PREVIEW_DOCUMENT_BASE}assets/photo-abc.jpg`);+ });++ it('rewrites import.meta.url in imported modules too, and leaves other import.meta fields alone', async () => {+ const files = graph({+ 'main.js': "import { u } from './lib/u.js'; console.log(u, import.meta.env);",+ 'lib/u.js': 'export const u = new URL("../media/clip.mp4", import.meta.url).href;',+ });+ const main = decode(await inlineDocumentModule('main.js', files));+ expect(main).toContain('import.meta.env');+ const lib = decode(main.match(/"data:text\/javascript;base64,([^"]+)"/)[0].slice(1, -1));+ expect(lib).toContain(JSON.stringify(`${PREVIEW_DOCUMENT_BASE}lib/u.js`));+ expect(lib).not.toContain('import.meta');+ });+});diff --git a/server/services/musicVideo/documentPreview.js b/server/services/musicVideo/documentPreview.jsindex ea1af80144..d92c7f20e1 100644--- a/server/services/musicVideo/documentPreview.js+++ b/server/services/musicVideo/documentPreview.js@@ -1,4 +1,4 @@-import { inlineDocumentModule } from './documentModules.js';+import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule } from './documentModules.js';import { musicVideoMediaMode } from '../../lib/musicVideoMediaPolicy.js';/*** Music Video — the in-app live preview of a composition document.@@ -64,8 +64,12 @@ const BOOTSTRAP = `(() => {let resolveAssets;window.PORTOS_MV_PREVIEW = true;window.PORTOS_MV_ASSETS = new Promise((resolve) => { resolveAssets = resolve; });- const keyOf = (value) => String(value).replace(/^\\.\\//, '').split(/[?#]/)[0];- const relative = (value) => typeof value === 'string' && value && !/^[a-z][a-z0-9+.-]*:/i.test(value) && !value.startsWith('/');+ // Bundler asset URLs resolved against an inlined module's stand-in import.meta.url (documentModules.js)+ // land under this origin; they name document files just like a relative src does.+ const DOC_BASE = ${JSON.stringify(PREVIEW_DOCUMENT_BASE)};+ const local = (value) => typeof value === 'string' && value.startsWith(DOC_BASE) ? value.slice(DOC_BASE.length) : value;+ const keyOf = (value) => String(local(value)).replace(/^\\.\\//, '').split(/[?#]/)[0];+ const relative = (value) => { value = local(value); return typeof value === 'string' && value && !/^[a-z][a-z0-9+.-]*:/i.test(value) && !value.startsWith('/'); };const pending = [];for (const Ctor of [HTMLMediaElement, HTMLImageElement, HTMLSourceElement]) {const desc = Object.getOwnPropertyDescriptor(Ctor.prototype, 'src');