Skip to content

Commit a7c4a86

Browse files
jrusso1020claude
andauthored
refactor(core): one function writes the another-site mark for every page server (#9128)
The six servers that answer a page to another site each built the same meta tag and placed it with insertWhereScriptsFirstRun. markForAnotherSite in the core compiler now does both, so the tag and its place cannot drift apart between servers. insertWhereScriptsFirstRun is no longer exported. No behaviour change. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent c772385 commit a7c4a86

7 files changed

Lines changed: 21 additions & 45 deletions

File tree

‎packages/cli/src/utils/compositionServer.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ import { getMimeType, openRangeEnd, refuseServiceWorkerScripts } from "@hyperfra
1010
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
1111
import {
1212
injectTagsAtHeadStart,
13-
insertWhereScriptsFirstRun,
13+
markForAnotherSite,
1414
} from "@hyperframes/core/compiler/html-document";
15-
import {
16-
PREVIEW_FOR_ANOTHER_SITE_META,
17-
dropServiceWorkers,
18-
} from "@hyperframes/core/studio-preview-mark";
15+
import { dropServiceWorkers } from "@hyperframes/core/studio-preview-mark";
1916
import {
2017
PREVIEW_ALLOWED_HOSTS_ENV,
2118
previewAllowedHostsFromEnv,
@@ -226,10 +223,7 @@ async function markDocumentForAnotherSite(c: Context): Promise<void> {
226223
const headers = new Headers(c.res.headers);
227224
headers.delete("content-length");
228225
headers.delete("etag");
229-
const marked = insertWhereScriptsFirstRun(
230-
await c.res.text(),
231-
`<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`,
232-
);
226+
const marked = markForAnotherSite(await c.res.text());
233227
c.res = undefined;
234228
c.res = new Response(marked, { status: 200, headers });
235229
}

‎packages/cli/src/utils/staticProjectServer.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
import { Duplex, Writable } from "node:stream";
99
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
1010
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
11-
import { insertWhereScriptsFirstRun } from "@hyperframes/core/compiler/html-document";
12-
import { PREVIEW_FOR_ANOTHER_SITE_META } from "@hyperframes/core/studio-preview-mark";
11+
import { markForAnotherSite } from "@hyperframes/core/compiler/html-document";
1312
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
1413
import { isAbsolute, relative, resolve } from "node:path";
1514
import {
@@ -297,10 +296,6 @@ async function serveProxyRequest(
297296
}
298297
}
299298

300-
function markForAnotherSite(html: string): string {
301-
return insertWhereScriptsFirstRun(html, `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`);
302-
}
303-
304299
/** A load by another site is sandboxed, and the documents it gets are marked so their runtime offers it no editing port. */
305300
/** A `?hf-proxy=` copy runs ffmpeg on this machine, so another site may not ask for one. */
306301
function proxyRequestedByAnotherSite(req: IncomingMessage): boolean {

‎packages/core/src/compiler/htmlDocument.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { acceleratedCanvasTrackerSource } from "../pageMotionBlur.js";
2+
import { PREVIEW_FOR_ANOTHER_SITE_META } from "../studioPreviewMark.js";
23
import { findSceneTemplate, templateContent } from "@hyperframes/parsers/sub-composition-validity";
34
import { parseHTML } from "@hyperframes/parsers/linkedom";
45
import {
@@ -419,7 +420,11 @@ export function insertRuntimeTag(html: string, tag: string, atHeadStart = false)
419420
return (atHeadStart ? insertWhereScriptsFirstRun : injectTagsAtHeadStart)(withRuntime, tracker);
420421
}
421422

422-
export function insertWhereScriptsFirstRun(html: string, tags: string): string {
423+
export function markForAnotherSite(html: string): string {
424+
return insertWhereScriptsFirstRun(html, `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`);
425+
}
426+
427+
function insertWhereScriptsFirstRun(html: string, tags: string): string {
423428
const at = headContentStart(html);
424429
const opensWithMarkup = html.charAt(at - 1) === ">";
425430
return `${html.slice(0, at)}${opensWithMarkup ? "\n" : ""}${tags}${html.slice(at)}`;

‎packages/engine/src/services/fileServer.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ import { readFileSync, openSync, fstatSync, closeSync, statSync, constants } fro
1212
import { join, extname } from "node:path";
1313
import { hostname as machineName, networkInterfaces } from "node:os";
1414
import { injectScriptsIntoHtml } from "@hyperframes/core/compiler";
15-
import { insertWhereScriptsFirstRun } from "@hyperframes/core/compiler/html-document";
16-
import {
17-
PREVIEW_FOR_ANOTHER_SITE_META,
18-
serviceWorkerScriptFetch,
19-
} from "@hyperframes/core/studio-preview-mark";
15+
import { markForAnotherSite } from "@hyperframes/core/compiler/html-document";
16+
import { serviceWorkerScriptFetch } from "@hyperframes/core/studio-preview-mark";
2017
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
2118
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";
2219
import { answerOwnLoopbackHostOnly } from "../utils/loopbackOnly.js";
@@ -121,8 +118,6 @@ function readProjectFile(paths: { projectPath: string; compiledPath: string | nu
121118
);
122119
}
123120

124-
const OFFER_NO_FRAMING_SITE_AN_EDITING_PORT = `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`;
125-
126121
function nameInHost(host: string): string {
127122
const bracketed = /^\[([^\]]+)\]/.exec(host);
128123
return (bracketed ? bracketed[1] : host.split(":")[0])?.toLowerCase() ?? "";
@@ -208,7 +203,7 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
208203
relativePath === "index.html"
209204
? injectScriptsIntoHtml(rawHtml, headScripts, bodyScripts, stripEmbeddedRuntime)
210205
: rawHtml;
211-
return c.text(insertWhereScriptsFirstRun(html, OFFER_NO_FRAMING_SITE_AN_EDITING_PORT), 200, {
206+
return c.text(markForAnotherSite(html), 200, {
212207
"Content-Type": contentType,
213208
});
214209
}

‎packages/inspector/src/service.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ import {
4242
NO_FRAMING_HEADERS,
4343
servableProjectFile,
4444
} from "./pageServer";
45-
import {
46-
PREVIEW_FOR_ANOTHER_SITE_META,
47-
serviceWorkerScriptFetch,
48-
} from "../../core/src/studioPreviewMark";
49-
import { insertWhereScriptsFirstRun } from "../../core/src/compiler/htmlDocument";
45+
import { serviceWorkerScriptFetch } from "../../core/src/studioPreviewMark";
46+
import { markForAnotherSite } from "../../core/src/compiler/htmlDocument";
5047
import { Sources, fingerprint } from "./sources";
5148
import type { InspectRequest, Snippet } from "./types";
5249
import { foreignWritablePath, trustedCacheRoot } from "../../core/src/cacheDir";
@@ -241,12 +238,11 @@ export class Inspector {
241238
});
242239
if (fingerprint(this.project) !== digest)
243240
throw new Error("source_changed_during_load: retry against a stable project");
244-
const servedHtml = insertWhereScriptsFirstRun(
241+
const servedHtml = markForAnotherSite(
245242
html.replace(
246243
'data-hyperframes-preview-runtime="1" src=""',
247244
'data-hyperframes-preview-runtime="1" src="/__hf-runtime.js"',
248245
),
249-
`<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`,
250246
);
251247
const serve: PageHandler = (req, res) => {
252248
if (!this.host || !isOwnPageHost(req.headers.host, this.host.port))

‎packages/producer/src/services/fileServer.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ import { promisify } from "node:util";
3333
import { Readable } from "node:stream";
3434
import { join, extname, resolve, sep } from "node:path";
3535
import { injectScriptsAtHeadStart, injectScriptsIntoHtml } from "@hyperframes/core/compiler";
36-
import { insertWhereScriptsFirstRun } from "@hyperframes/core/compiler/html-document";
37-
import {
38-
PREVIEW_FOR_ANOTHER_SITE_META,
39-
serviceWorkerScriptFetch,
40-
} from "@hyperframes/core/studio-preview-mark";
36+
import { markForAnotherSite } from "@hyperframes/core/compiler/html-document";
37+
import { serviceWorkerScriptFetch } from "@hyperframes/core/studio-preview-mark";
4138
import { fpsToNumber, type Fps, terminalText } from "@hyperframes/core";
4239
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
4340
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
@@ -742,8 +739,6 @@ export interface FileServerHandle {
742739
addPreHeadScript: (script: string) => void;
743740
}
744741

745-
const OFFER_NO_FRAMING_SITE_AN_EDITING_PORT = `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`;
746-
747742
/**
748743
* Set before the Hyperframes runtime executes so render/probe pages can avoid
749744
* preview-only initialization work that mutates the live visual timeline.
@@ -895,7 +890,7 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
895890
html = isIndex
896891
? injectScriptsIntoHtml(html, headScripts, bodyScripts, stripEmbeddedRuntime)
897892
: html;
898-
html = insertWhereScriptsFirstRun(html, OFFER_NO_FRAMING_SITE_AN_EDITING_PORT);
893+
html = markForAnotherSite(html);
899894
return c.text(html, 200, { "Content-Type": contentType });
900895
}
901896

‎packages/studio-server/src/createStudioApi.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Hono, type Context, type Next } from "hono";
22
import { routePath } from "hono/route";
3-
import { insertWhereScriptsFirstRun } from "@hyperframes/core/compiler/html-document";
4-
import { PREVIEW_FOR_ANOTHER_SITE_META } from "@hyperframes/core/studio-preview-mark";
3+
import { markForAnotherSite } from "@hyperframes/core/compiler/html-document";
54
import type { StudioApiAdapter, StudioRefusal, StudioRefusalWhy } from "./types.js";
65
import { registerProjectRoutes } from "./routes/projects.js";
76
import { registerFileRoutes } from "./routes/files.js";
@@ -74,10 +73,7 @@ async function markPreviewForAnotherSite(c: Context) {
7473
const headers = new Headers(c.res.headers);
7574
headers.delete("content-length");
7675
headers.delete("etag");
77-
const marked = insertWhereScriptsFirstRun(
78-
await c.res.text(),
79-
`<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`,
80-
);
76+
const marked = markForAnotherSite(await c.res.text());
8177
c.res = undefined;
8278
c.res = new Response(marked, { status: 200, headers });
8379
}

0 commit comments

Comments
 (0)