Skip to content

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

preview bridge: match stand-in URLs given as URL objects or percent-encoded, and post media that CSS inlined

[._.]/ Adam Eivydaa8ed4ab5be
diff --git a/server/services/musicVideo/documentModules.js b/server/services/musicVideo/documentModules.js
index c39f52a59e..4d87abb77e 100644
--- a/server/services/musicVideo/documentModules.js
+++ b/server/services/musicVideo/documentModules.js
@@ -12,6 +12,19 @@ import { ServerError } from '../../lib/errorHandler.js';
*/
export const PREVIEW_DOCUMENT_BASE = 'https://document.portos.invalid/';
+/**
+ * The document-relative path a preview media URL names, or the value unchanged when it is not one of
+ * ours. Runs inside the preview bootstrap (injected via toString, so it must stay self-contained):
+ * a stand-in-base URL — string or URL object — loses the reserved origin, its query and hash, and its
+ * percent-encoding, matching how the document's own files are keyed.
+ */
+export function previewDocumentPath(value, base) {
+ if (typeof URL !== 'undefined' && value instanceof URL) value = value.href;
+ if (typeof value !== 'string' || !value.startsWith(base)) return value;
+ const path = value.slice(base.length).split(/[?#]/)[0];
+ try { return decodeURIComponent(path); } catch { return path; }
+}
+
// `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;
diff --git a/server/services/musicVideo/documentModules.test.js b/server/services/musicVideo/documentModules.test.js
index 2c8d9cd7bc..0f097a7f13 100644
--- a/server/services/musicVideo/documentModules.test.js
+++ b/server/services/musicVideo/documentModules.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule } from './documentModules.js';
+import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule, previewDocumentPath } 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) }]));
@@ -27,3 +27,29 @@ describe('inlineDocumentModule', () => {
expect(lib).not.toContain('import.meta');
});
});
+
+describe('previewDocumentPath', () => {
+ const base = PREVIEW_DOCUMENT_BASE;
+ it('maps stand-in-base URLs to document paths, from strings and URL objects alike', () => {
+ expect(previewDocumentPath(`${base}assets/photo-abc.jpg`, base)).toBe('assets/photo-abc.jpg');
+ expect(previewDocumentPath(new URL('photo-abc.jpg', `${base}assets/index.js`), base)).toBe('assets/photo-abc.jpg');
+ });
+
+ it('drops the query and hash, then decodes the path like the server keys its files', () => {
+ expect(previewDocumentPath(new URL('My Clip ü.jpg?v=2#t', `${base}media/x.js`).href, base)).toBe('media/My Clip ü.jpg');
+ expect(previewDocumentPath(`${base}media/a%3Fb.jpg`, base)).toBe('media/a?b.jpg');
+ expect(previewDocumentPath(`${base}media/bad%E0.jpg`, base)).toBe('media/bad%E0.jpg');
+ });
+
+ it('leaves everything else untouched', () => {
+ expect(previewDocumentPath('assets/photo.jpg', base)).toBe('assets/photo.jpg');
+ expect(previewDocumentPath('blob:null/1234', base)).toBe('blob:null/1234');
+ expect(previewDocumentPath('https://example.com/x.jpg', base)).toBe('https://example.com/x.jpg');
+ expect(previewDocumentPath(null, base)).toBe(null);
+ });
+
+ it('is self-contained, so the preview bootstrap can inline it', () => {
+ const inlined = new Function(`return (${previewDocumentPath.toString()})`)();
+ expect(inlined(`${base}assets/photo-abc.jpg`, base)).toBe('assets/photo-abc.jpg');
+ });
+});
diff --git a/server/services/musicVideo/documentPreview.js b/server/services/musicVideo/documentPreview.js
index d92c7f20e1..a3f9826be5 100644
--- a/server/services/musicVideo/documentPreview.js
+++ b/server/services/musicVideo/documentPreview.js
@@ -1,4 +1,4 @@
-import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule } from './documentModules.js';
+import { PREVIEW_DOCUMENT_BASE, inlineDocumentModule, previewDocumentPath } from './documentModules.js';
import { musicVideoMediaMode } from '../../lib/musicVideoMediaPolicy.js';
/**
* Music Video — the in-app live preview of a composition document.
@@ -67,8 +67,10 @@ const BOOTSTRAP = `(() => {
// 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 local = (value) => (${previewDocumentPath.toString()})(value, DOC_BASE);
+ // A stand-in-base URL is already a decoded document path (a literal ? or # in it is part of the name);
+ // anything else is a relative src whose query and hash are not.
+ const keyOf = (value) => { const path = local(value); return path !== value ? String(path) : String(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]) {
@@ -96,7 +98,7 @@ const BOOTSTRAP = `(() => {
if (!message || typeof message !== 'object') return;
if (message.type === 'portos-mv:assets' && !resolved) {
for (const [key, blob] of Object.entries(message.files || {})) {
- if (blob instanceof Blob) map.set(keyOf(key), URL.createObjectURL(blob));
+ if (blob instanceof Blob) map.set(String(key).replace(/^\\.\\//, ''), URL.createObjectURL(blob));
}
resolved = true;
for (const [el, desc, value] of pending.splice(0)) desc.set.call(el, map.get(keyOf(value)) || value);
@@ -135,12 +137,10 @@ export async function buildDocumentPreview(project, { draft = false } = {}) {
generated: project.composition?.document?.source?.kind === 'generated' });
let inlined = 0;
- const inlinedKeys = new Set();
const dataUrl = async (rel) => {
const file = files.get(rel);
if (!file || file.size > INLINE_FILE_MAX || inlined + file.size > INLINE_TOTAL_MAX) return null;
inlined += file.size;
- inlinedKeys.add(rel);
return `data:${documentMimeType(rel)};base64,${(await readFile(file.abs)).toString('base64')}`;
};
const rewriteCss = async (css) => {
@@ -208,7 +208,9 @@ export async function buildDocumentPreview(project, { draft = false } = {}) {
assets.push({ key: scene.media.src, url: `/data/${m.kind === 'video' ? 'videos' : 'images'}/${encodeURIComponent(name)}`, bytes: null });
}
for (const [rel, file] of files) {
- if (inlinedKeys.has(rel) || !BRIDGED.has(extname(rel).toLowerCase())) continue;
+ // Media is posted even when CSS or an <img> tag already inlined it as data:, because document code
+ // can also reach the same file through a relative src or a bundler URL (new URL(…, import.meta.url)).
+ if (!BRIDGED.has(extname(rel).toLowerCase())) continue;
assets.push({ key: rel, url: `/api/music-video/${encodeURIComponent(project.id)}/composition/document/file?path=${encodeURIComponent(rel)}${draft ? '&draft=1' : ''}`, bytes: file.size });
}
return { html, assets, width: frame.width, height: frame.height, fps: clock.fps, durationSec: clock.durationSec };