@norskvideo/ctl-sdk 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/browser.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from "./iframe-protocol.js";
1
2
  export * from "./manifest-schema.js";
2
3
  export * from "./parsing.js";
3
4
  export * from "./product-error.js";
package/browser.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Browser-safe surface: types, schemas, parsers, validators.
2
2
  // Anything with Node-only deps (fs, child_process, express) lives in the
3
3
  // default entry only.
4
+ export * from "./iframe-protocol.js";
4
5
  export * from "./manifest-schema.js";
5
6
  export * from "./parsing.js";
6
7
  export * from "./product-error.js";
@@ -1,10 +1,5 @@
1
- export type SubmitResult = {
2
- ok: true;
3
- formValues: Record<string, unknown>;
4
- } | {
5
- ok: false;
6
- error?: string;
7
- };
1
+ import { type SubmitResult } from "../iframe-protocol.js";
2
+ export type { SubmitResult };
8
3
  export type ProductIframeHandle = {
9
4
  submit: () => Promise<SubmitResult>;
10
5
  };
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from "react";
3
- const PROTOCOL_VERSION = 1;
3
+ import { IFRAME_PROTOCOL_VERSION, parseChildMessage, } from "../iframe-protocol.js";
4
4
  const SUBMIT_TIMEOUT_MS = 30_000;
5
5
  function resolveSrc(productName, configScreenUrl) {
6
6
  const path = configScreenUrl.startsWith("/") ? configScreenUrl : `/${configScreenUrl}`;
@@ -27,13 +27,14 @@ export const ProductIframe = forwardRef(function ProductIframe({ productName, co
27
27
  const onMessage = (event) => {
28
28
  if (event.source !== iframeRef.current?.contentWindow)
29
29
  return;
30
- const data = event.data;
31
- if (!data || typeof data !== "object")
30
+ const msg = parseChildMessage(event.data);
31
+ if (!msg)
32
32
  return;
33
- if (data.v !== PROTOCOL_VERSION)
34
- return;
35
- const payload = data.payload ?? {};
36
- switch (data.type) {
33
+ // Payload fields stay runtime-checked despite the typed union: the wire
34
+ // is a product we don't control, and legacy shapes (option-a
35
+ // submit-result) predate it.
36
+ const payload = (msg.payload ?? {});
37
+ switch (msg.type) {
37
38
  case "ready":
38
39
  case "resize":
39
40
  if (typeof payload.height === "number" && payload.height > 0)
@@ -88,16 +89,19 @@ export const ProductIframe = forwardRef(function ProductIframe({ productName, co
88
89
  const launchContextRef = useRef(launchContext);
89
90
  launchContextRef.current = launchContext;
90
91
  const sendInit = useCallback(() => {
91
- iframeRef.current?.contentWindow?.postMessage({
92
- v: PROTOCOL_VERSION,
92
+ const init = {
93
+ v: IFRAME_PROTOCOL_VERSION,
93
94
  type: "init",
94
95
  payload: {
95
96
  instanceContext: { basename },
96
97
  theme: "dark",
97
98
  locale: "en",
99
+ // The prop is a verbatim-forwarded record by design; the protocol
100
+ // types the keys the products read.
98
101
  ...(launchContextRef.current ? { launchContext: launchContextRef.current } : {}),
99
102
  },
100
- }, "*");
103
+ };
104
+ iframeRef.current?.contentWindow?.postMessage(init, "*");
101
105
  }, [basename]);
102
106
  useImperativeHandle(ref, () => ({
103
107
  submit: () => new Promise((resolve) => {
@@ -106,7 +110,8 @@ export const ProductIframe = forwardRef(function ProductIframe({ productName, co
106
110
  return;
107
111
  }
108
112
  submitResolverRef.current = resolve;
109
- iframeRef.current.contentWindow.postMessage({ v: PROTOCOL_VERSION, type: "submit", payload: {} }, "*");
113
+ const submit = { v: IFRAME_PROTOCOL_VERSION, type: "submit", payload: {} };
114
+ iframeRef.current.contentWindow.postMessage(submit, "*");
110
115
  setTimeout(() => {
111
116
  if (submitResolverRef.current === resolve) {
112
117
  submitResolverRef.current = null;
@@ -0,0 +1,34 @@
1
+ import type { Request, RequestHandler, Response } from "express";
2
+ export declare const HOP_BY_HOP_HEADERS: ReadonlySet<string>;
3
+ /** Forwardable copy of an incoming request's headers: hop-by-hop, host and
4
+ * content-length stripped (fetch derives the latter two from the target URL
5
+ * and the buffered body). */
6
+ export declare function buildForwardHeaders(req: Request): Headers;
7
+ /** Buffer the request body (small JSON control messages — buffering is robust
8
+ * across the fetch duplex-body edge cases). GET/HEAD and empty bodies yield
9
+ * undefined. */
10
+ export declare function readRequestBody(req: Request): Promise<Uint8Array | undefined>;
11
+ export type ForwardResponseOptions = {
12
+ /** Transform response header values before relaying (e.g. re-prefix a
13
+ * root-absolute redirect Location). Return the value to send. */
14
+ rewriteHeader?: (key: string, value: string) => string;
15
+ /** Observe mid-stream body failures (the response is ended regardless). */
16
+ onStreamError?: (err: unknown) => void;
17
+ };
18
+ /** Relay an upstream fetch Response: status, headers (hop-by-hop plus
19
+ * content-length/content-encoding stripped — fetch already decompressed the
20
+ * body), then the streamed body. */
21
+ export declare function forwardUpstreamResponse(res: Response, upstream: globalThis.Response, opts?: ForwardResponseOptions): Promise<void>;
22
+ export type ProxyHttpOptions = ForwardResponseOptions & {
23
+ /** Abort the upstream fetch (headers AND body) after this long. Defaults to
24
+ * 30s; pass 0 for no timeout (long-lived streams). */
25
+ timeoutMs?: number;
26
+ };
27
+ /** Proxy one HTTP request to `targetUrl`, streaming the response body back.
28
+ * Never throws into express: an unreachable/booting upstream surfaces as a
29
+ * 502 the caller retries. */
30
+ export declare function proxyHttp(req: Request, res: Response, targetUrl: string, opts?: ProxyHttpOptions): Promise<void>;
31
+ /** Express handler proxying whatever path it is mounted on to
32
+ * `resolveBase(req) + req.originalUrl` (so /live/api/<id>/x reaches
33
+ * <studio>/live/api/<id>/x). */
34
+ export declare function makeHttpProxy(resolveBase: (req: Request) => string, opts?: ProxyHttpOptions): RequestHandler;
package/http-proxy.js ADDED
@@ -0,0 +1,131 @@
1
+ // Dependency-free fetch-based reverse-proxy core, shared by the runner's
2
+ // product proxy (proxy-middleware.ts) and by instance-side product backends
3
+ // fronting a launched Studio (lifted from funke's live-proxy.ts).
4
+ //
5
+ // Zero new dependency is deliberate: `bun build --target=bun` can mis-bundle a
6
+ // fat CJS proxy dep, only caught in the image. A hand-rolled proxy bundles
7
+ // trivially. WebSocket upgrades are OUT of scope: bun's node:http "upgrade"
8
+ // socket is not writable back to the client, so an in-process WS relay cannot
9
+ // forward the 101 — bridge upstream WS feeds by consuming them as a client and
10
+ // re-emitting same-origin SSE instead (see sse.ts).
11
+ import { Readable } from "node:stream";
12
+ // RFC 7230 §6.1 hop-by-hop headers, which must not travel end-to-end. Includes
13
+ // both "trailer" (the actual header) and "trailers" (the TE value, present in
14
+ // one of the copies this replaces) so neither survives in either direction.
15
+ export const HOP_BY_HOP_HEADERS = new Set([
16
+ "connection",
17
+ "keep-alive",
18
+ "proxy-authenticate",
19
+ "proxy-authorization",
20
+ "te",
21
+ "trailer",
22
+ "trailers",
23
+ "transfer-encoding",
24
+ "upgrade",
25
+ ]);
26
+ // fetch decompresses upstream bodies and sets host/content-length itself, so
27
+ // the originals must not be relayed alongside the transformed message.
28
+ const REQUEST_ONLY_STRIP = new Set(["host", "content-length"]);
29
+ const RESPONSE_ONLY_STRIP = new Set(["content-length", "content-encoding"]);
30
+ /** Forwardable copy of an incoming request's headers: hop-by-hop, host and
31
+ * content-length stripped (fetch derives the latter two from the target URL
32
+ * and the buffered body). */
33
+ export function buildForwardHeaders(req) {
34
+ const headers = new Headers();
35
+ for (const [key, value] of Object.entries(req.headers)) {
36
+ const lower = key.toLowerCase();
37
+ if (HOP_BY_HOP_HEADERS.has(lower) || REQUEST_ONLY_STRIP.has(lower))
38
+ continue;
39
+ if (Array.isArray(value)) {
40
+ for (const v of value)
41
+ headers.append(key, v);
42
+ }
43
+ else if (value !== undefined) {
44
+ headers.set(key, value);
45
+ }
46
+ }
47
+ return headers;
48
+ }
49
+ /** Buffer the request body (small JSON control messages — buffering is robust
50
+ * across the fetch duplex-body edge cases). GET/HEAD and empty bodies yield
51
+ * undefined. */
52
+ export function readRequestBody(req) {
53
+ const method = req.method.toUpperCase();
54
+ if (method === "GET" || method === "HEAD")
55
+ return Promise.resolve(undefined);
56
+ return new Promise((resolve, reject) => {
57
+ const chunks = [];
58
+ req.on("data", (c) => chunks.push(c));
59
+ req.on("end", () => {
60
+ const buf = Buffer.concat(chunks);
61
+ resolve(buf.byteLength > 0 ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : undefined);
62
+ });
63
+ req.on("error", reject);
64
+ });
65
+ }
66
+ /** Relay an upstream fetch Response: status, headers (hop-by-hop plus
67
+ * content-length/content-encoding stripped — fetch already decompressed the
68
+ * body), then the streamed body. */
69
+ export async function forwardUpstreamResponse(res, upstream, opts = {}) {
70
+ res.status(upstream.status);
71
+ upstream.headers.forEach((value, key) => {
72
+ const lower = key.toLowerCase();
73
+ if (HOP_BY_HOP_HEADERS.has(lower) || RESPONSE_ONLY_STRIP.has(lower))
74
+ return;
75
+ res.setHeader(key, opts.rewriteHeader ? opts.rewriteHeader(lower, value) : value);
76
+ });
77
+ if (!upstream.body) {
78
+ res.end();
79
+ return;
80
+ }
81
+ const nodeStream = Readable.fromWeb(upstream.body);
82
+ await new Promise((resolve) => {
83
+ nodeStream.on("error", (err) => {
84
+ opts.onStreamError?.(err);
85
+ if (!res.headersSent)
86
+ res.status(502);
87
+ res.end();
88
+ resolve();
89
+ });
90
+ res.on("close", resolve);
91
+ nodeStream.pipe(res);
92
+ });
93
+ }
94
+ const DEFAULT_PROXY_TIMEOUT_MS = 30_000;
95
+ /** Proxy one HTTP request to `targetUrl`, streaming the response body back.
96
+ * Never throws into express: an unreachable/booting upstream surfaces as a
97
+ * 502 the caller retries. */
98
+ export async function proxyHttp(req, res, targetUrl, opts = {}) {
99
+ const headers = buildForwardHeaders(req);
100
+ const body = await readRequestBody(req);
101
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_PROXY_TIMEOUT_MS;
102
+ let upstream;
103
+ try {
104
+ upstream = await fetch(targetUrl, {
105
+ method: req.method.toUpperCase(),
106
+ headers,
107
+ body: body,
108
+ redirect: "manual",
109
+ ...(timeoutMs > 0 ? { signal: AbortSignal.timeout(timeoutMs) } : {}),
110
+ });
111
+ }
112
+ catch (err) {
113
+ if (!res.headersSent) {
114
+ res.status(502).json({ error: "bad gateway", message: err instanceof Error ? err.message : String(err) });
115
+ }
116
+ else {
117
+ res.end();
118
+ }
119
+ return;
120
+ }
121
+ await forwardUpstreamResponse(res, upstream, opts);
122
+ }
123
+ /** Express handler proxying whatever path it is mounted on to
124
+ * `resolveBase(req) + req.originalUrl` (so /live/api/<id>/x reaches
125
+ * <studio>/live/api/<id>/x). */
126
+ export function makeHttpProxy(resolveBase, opts = {}) {
127
+ return (req, res) => {
128
+ const target = resolveBase(req).replace(/\/+$/, "") + req.originalUrl;
129
+ void proxyHttp(req, res, target, opts);
130
+ };
131
+ }
@@ -0,0 +1,112 @@
1
+ export declare const IFRAME_PROTOCOL_VERSION = 1;
2
+ export type IframeTheme = "light" | "dark";
3
+ export type InstanceContext = {
4
+ /** Path prefix where the product is mounted on the runner's origin (e.g.
5
+ * `/products/norsk-probe`) — feed it to the product router's basename. */
6
+ basename?: string;
7
+ };
8
+ /** Present only for the instance-launch config screen: the product template
9
+ * being launched (name + parameters), so the screen can prefill. */
10
+ export type LaunchContext = {
11
+ productTemplateName?: string;
12
+ parameters?: {
13
+ name: string;
14
+ default?: string;
15
+ }[];
16
+ };
17
+ /** The iframe reports the form values it captured; the runner POSTs them to
18
+ * the product's /api/product-template to get the tar. formValues is
19
+ * product-specific. */
20
+ export type SubmitResult = {
21
+ ok: true;
22
+ formValues: Record<string, unknown>;
23
+ } | {
24
+ ok: false;
25
+ error?: string;
26
+ };
27
+ export type IframeChildMessage = {
28
+ v: 1;
29
+ type: "ready";
30
+ payload: {
31
+ height: number;
32
+ };
33
+ } | {
34
+ v: 1;
35
+ type: "resize";
36
+ payload: {
37
+ height: number;
38
+ };
39
+ } | {
40
+ v: 1;
41
+ type: "validity";
42
+ payload: {
43
+ valid: boolean;
44
+ errors?: string[];
45
+ };
46
+ } | {
47
+ v: 1;
48
+ type: "suggest-name";
49
+ payload: {
50
+ name: string;
51
+ };
52
+ } | {
53
+ v: 1;
54
+ type: "dirty";
55
+ payload: {
56
+ dirty: boolean;
57
+ };
58
+ } | {
59
+ v: 1;
60
+ type: "submit-result";
61
+ payload: SubmitResult;
62
+ } | {
63
+ v: 1;
64
+ type: "request-cancel";
65
+ payload: Record<string, never>;
66
+ };
67
+ export type IframeParentMessage = {
68
+ v: 1;
69
+ type: "init";
70
+ payload: {
71
+ instanceContext?: InstanceContext;
72
+ theme?: IframeTheme;
73
+ locale?: string;
74
+ launchContext?: LaunchContext;
75
+ };
76
+ } | {
77
+ v: 1;
78
+ type: "submit";
79
+ payload: Record<string, never>;
80
+ } | {
81
+ v: 1;
82
+ type: "cancel";
83
+ payload: Record<string, never>;
84
+ } | {
85
+ v: 1;
86
+ type: "theme-change";
87
+ payload: {
88
+ theme: IframeTheme;
89
+ };
90
+ };
91
+ export declare function parseParentMessage(data: unknown): IframeParentMessage | null;
92
+ export declare function parseChildMessage(data: unknown): IframeChildMessage | null;
93
+ export type IframeWindowLike = {
94
+ parent: {
95
+ postMessage(message: unknown, targetOrigin: string): void;
96
+ };
97
+ addEventListener(type: "message", listener: (event: {
98
+ data: unknown;
99
+ }) => void): void;
100
+ removeEventListener(type: "message", listener: (event: {
101
+ data: unknown;
102
+ }) => void): void;
103
+ document: {
104
+ body: {
105
+ scrollHeight: number;
106
+ };
107
+ };
108
+ };
109
+ export declare const isEmbedded: (win?: IframeWindowLike) => boolean;
110
+ export declare const sendToParent: (msg: IframeChildMessage, win?: IframeWindowLike) => void;
111
+ export declare const onParentMessage: (handler: (msg: IframeParentMessage) => void, win?: IframeWindowLike) => (() => void);
112
+ export declare const reportHeight: (win?: IframeWindowLike) => void;
@@ -0,0 +1,44 @@
1
+ // The runner<->product iframe postMessage contract (v1), single-sourced for
2
+ // BOTH sides: the runner's ProductIframe component builds IframeParentMessage
3
+ // values, and a product's config screen imports the child-side helpers below
4
+ // instead of hand-copying them from the iframe-integration doc.
5
+ //
6
+ // Standalone usage (product opened outside the runner): window.parent ===
7
+ // window, so every send becomes a no-op.
8
+ export const IFRAME_PROTOCOL_VERSION = 1;
9
+ // Envelope guard shared by both directions: any v1 object with a string type
10
+ // passes (matching the product copies' onParentMessage filter); per-field
11
+ // payload validation stays with the consumer, which knows its own defaults.
12
+ function isEnvelope(data) {
13
+ return (data !== null &&
14
+ typeof data === "object" &&
15
+ data.v === IFRAME_PROTOCOL_VERSION &&
16
+ typeof data.type === "string");
17
+ }
18
+ export function parseParentMessage(data) {
19
+ return isEnvelope(data) ? data : null;
20
+ }
21
+ export function parseChildMessage(data) {
22
+ return isEnvelope(data) ? data : null;
23
+ }
24
+ // globalThis, not window: node-side consumers (dev-kit, backend) pull this
25
+ // module in via the browser barrel and typecheck without the DOM lib.
26
+ const realWindow = () => globalThis.window;
27
+ export const isEmbedded = (win = realWindow()) => win.parent !== win;
28
+ export const sendToParent = (msg, win = realWindow()) => {
29
+ if (!isEmbedded(win))
30
+ return;
31
+ win.parent.postMessage(msg, "*");
32
+ };
33
+ export const onParentMessage = (handler, win = realWindow()) => {
34
+ const listener = (event) => {
35
+ const msg = parseParentMessage(event.data);
36
+ if (msg)
37
+ handler(msg);
38
+ };
39
+ win.addEventListener("message", listener);
40
+ return () => win.removeEventListener("message", listener);
41
+ };
42
+ export const reportHeight = (win = realWindow()) => {
43
+ sendToParent({ v: 1, type: "resize", payload: { height: win.document.body.scrollHeight } }, win);
44
+ };
package/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from "./capabilities-router.js";
3
3
  export * from "./cjs-interop.js";
4
4
  export * from "./dev-url.js";
5
5
  export * from "./docker-runner.js";
6
+ export * from "./http-proxy.js";
6
7
  export * from "./license-registration.js";
7
8
  export * from "./license-stager.js";
8
9
  export * from "./license-v2.js";
@@ -13,3 +14,5 @@ export * from "./product-health-monitor.js";
13
14
  export * from "./product-service.js";
14
15
  export * from "./product-template-materials.js";
15
16
  export * from "./proxy-middleware.js";
17
+ export * from "./route-error.js";
18
+ export * from "./sse.js";
package/index.js CHANGED
@@ -5,6 +5,7 @@ export * from "./capabilities-router.js";
5
5
  export * from "./cjs-interop.js";
6
6
  export * from "./dev-url.js";
7
7
  export * from "./docker-runner.js";
8
+ export * from "./http-proxy.js";
8
9
  export * from "./license-registration.js";
9
10
  export * from "./license-stager.js";
10
11
  export * from "./license-v2.js";
@@ -15,3 +16,5 @@ export * from "./product-health-monitor.js";
15
16
  export * from "./product-service.js";
16
17
  export * from "./product-template-materials.js";
17
18
  export * from "./proxy-middleware.js";
19
+ export * from "./route-error.js";
20
+ export * from "./sse.js";
@@ -26,3 +26,5 @@ export declare const SeedSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
26
26
  }, z.core.$strict>>;
27
27
  export type ManifestSeed = z.infer<typeof SeedSchema>;
28
28
  export declare function parseManifestSeed(raw: unknown): Result<ManifestSeed, string>;
29
+ export declare const bareTag: (ref: string) => string;
30
+ export declare const repoOf: (ref: string) => string;
package/manifest-seed.js CHANGED
@@ -38,3 +38,8 @@ export function parseManifestSeed(raw) {
38
38
  const parsed = SeedSchema.safeParse(raw);
39
39
  return parsed.success ? ok(parsed.data) : err(z.prettifyError(parsed.error));
40
40
  }
41
+ // The seed carries FULL repo:tag refs; every product's version.ts splits them
42
+ // into the uniform MEDIA_*/STUDIO_* vocabulary (RFC 0001 §5). Split on the
43
+ // LAST colon so registry:port refs keep the port on the repo side.
44
+ export const bareTag = (ref) => ref.slice(ref.lastIndexOf(":") + 1);
45
+ export const repoOf = (ref) => ref.slice(0, ref.lastIndexOf(":"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norskvideo/ctl-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -1,10 +1 @@
1
- /**
2
- * Error class for product-template lifecycle issues. Codes cover both the
3
- * "store + extract" path (used by ctl's ProductTemplateService — extraction,
4
- * manifest parsing, in-use guarding) and the simpler "store raw bytes"
5
- * path (used by mgr — name validation + bytes persistence).
6
- */
7
- export declare class ProductTemplateError extends Error {
8
- code: "INVALID_NAME" | "NAME_CONFLICT" | "FILE_NOT_FOUND" | "EXTRACTION_FAILED" | "MANIFEST_MISSING" | "MANIFEST_INVALID" | "COMPOSE_MISSING" | "PARAMETERS_INVALID" | "NOT_FOUND" | "IN_USE" | "PRODUCT_MISMATCH";
9
- constructor(code: "INVALID_NAME" | "NAME_CONFLICT" | "FILE_NOT_FOUND" | "EXTRACTION_FAILED" | "MANIFEST_MISSING" | "MANIFEST_INVALID" | "COMPOSE_MISSING" | "PARAMETERS_INVALID" | "NOT_FOUND" | "IN_USE" | "PRODUCT_MISMATCH", message: string);
10
- }
1
+ export { ProductTemplateError } from "@norskvideo/ctl-product-template-schema/read";
@@ -1,14 +1,4 @@
1
- /**
2
- * Error class for product-template lifecycle issues. Codes cover both the
3
- * "store + extract" path (used by ctl's ProductTemplateService — extraction,
4
- * manifest parsing, in-use guarding) and the simpler "store raw bytes"
5
- * path (used by mgr — name validation + bytes persistence).
6
- */
7
- export class ProductTemplateError extends Error {
8
- code;
9
- constructor(code, message) {
10
- super(message);
11
- this.code = code;
12
- this.name = "ProductTemplateError";
13
- }
14
- }
1
+ // Moved to the schema package (the error taxonomy lives with the contract it
2
+ // reports on see @norskvideo/ctl-product-template-schema/read). Re-exported
3
+ // here so existing importers of @norskvideo/ctl-sdk keep working.
4
+ export { ProductTemplateError } from "@norskvideo/ctl-product-template-schema/read";
@@ -1,48 +1,11 @@
1
1
  import { logger } from "@norskvideo/ctl-foundation";
2
- const HOP_BY_HOP = new Set([
3
- "connection",
4
- "keep-alive",
5
- "proxy-authenticate",
6
- "proxy-authorization",
7
- "te",
8
- "trailers",
9
- "transfer-encoding",
10
- "upgrade",
11
- "host",
12
- "content-length",
13
- ]);
2
+ import { buildForwardHeaders, forwardUpstreamResponse, readRequestBody } from "./http-proxy.js";
14
3
  function targetBaseUrl(reg) {
15
4
  if (reg.spec.kind === "container") {
16
5
  return reg.port !== undefined ? `http://127.0.0.1:${reg.port}` : null;
17
6
  }
18
7
  return reg.spec.url.replace(/\/$/, "");
19
8
  }
20
- function buildOutgoingHeaders(req) {
21
- const out = {};
22
- for (const [key, value] of Object.entries(req.headers)) {
23
- if (HOP_BY_HOP.has(key.toLowerCase()))
24
- continue;
25
- if (value === undefined)
26
- continue;
27
- out[key] = Array.isArray(value) ? value.join(", ") : value;
28
- }
29
- return out;
30
- }
31
- async function readRequestBody(req) {
32
- if (req.method === "GET" || req.method === "HEAD")
33
- return undefined;
34
- return new Promise((resolve, reject) => {
35
- const chunks = [];
36
- req.on("data", (chunk) => chunks.push(chunk));
37
- req.on("end", () => {
38
- if (!chunks.length)
39
- return resolve(undefined);
40
- const buf = Buffer.concat(chunks);
41
- resolve(new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength));
42
- });
43
- req.on("error", reject);
44
- });
45
- }
46
9
  export function createProductProxyMiddleware(svc) {
47
10
  return async (req, res, next) => {
48
11
  const name = req.params.name;
@@ -60,7 +23,7 @@ export function createProductProxyMiddleware(svc) {
60
23
  return;
61
24
  }
62
25
  const targetUrl = `${base}${req.url}`;
63
- const headers = buildOutgoingHeaders(req);
26
+ const headers = buildForwardHeaders(req);
64
27
  const body = await readRequestBody(req);
65
28
  let upstream;
66
29
  try {
@@ -76,37 +39,13 @@ export function createProductProxyMiddleware(svc) {
76
39
  res.status(502).json({ status: "error", message: `upstream fetch failed: ${String(e)}` });
77
40
  return;
78
41
  }
79
- res.status(upstream.status);
80
42
  const productPrefix = `/products/${encodeURIComponent(name)}`;
81
- upstream.headers.forEach((value, key) => {
82
- const lower = key.toLowerCase();
83
- if (HOP_BY_HOP.has(lower))
84
- return;
43
+ await forwardUpstreamResponse(res, upstream, {
85
44
  // Rewrite root-absolute redirect Locations so 3xx responses don't
86
45
  // escape the product prefix and land on the runner SPA. Relative
87
46
  // (non-/-prefixed) and full-URL Locations pass through unchanged.
88
- if (lower === "location" && value.startsWith("/") && !value.startsWith("//")) {
89
- res.setHeader(key, `${productPrefix}${value}`);
90
- return;
91
- }
92
- res.setHeader(key, value);
47
+ rewriteHeader: (key, value) => key === "location" && value.startsWith("/") && !value.startsWith("//") ? `${productPrefix}${value}` : value,
48
+ onStreamError: (e) => logger.warn(`Proxy stream from ${targetUrl} broke: ${String(e)}`),
93
49
  });
94
- if (!upstream.body) {
95
- res.end();
96
- return;
97
- }
98
- const reader = upstream.body.getReader();
99
- try {
100
- while (true) {
101
- const { done, value } = await reader.read();
102
- if (done)
103
- break;
104
- res.write(value);
105
- }
106
- }
107
- catch (e) {
108
- logger.warn(`Proxy stream from ${targetUrl} broke: ${String(e)}`);
109
- }
110
- res.end();
111
50
  };
112
51
  }
@@ -0,0 +1,12 @@
1
+ import type { Response } from "express";
2
+ export type RouteErrorClassification = {
3
+ status: number;
4
+ error: string;
5
+ };
6
+ export type RouteErrorClassifier = (err: unknown) => RouteErrorClassification | undefined;
7
+ export declare function sendRouteError(res: Response, err: unknown, fallbackError: string, classify?: RouteErrorClassifier): void;
8
+ /** Run a route body, mapping any throw or rejection to the error shape above.
9
+ * `fallbackError` is the route's stable failure label (e.g.
10
+ * "product-template generation failed"); `classify` upgrades known error
11
+ * classes to their own status + label (e.g. a seed error to a 400). */
12
+ export declare function guardRoute(res: Response, fallbackError: string, fn: () => void | Promise<void>, classify?: RouteErrorClassifier): void;
package/route-error.js ADDED
@@ -0,0 +1,26 @@
1
+ export function sendRouteError(res, err, fallbackError, classify) {
2
+ if (res.headersSent) {
3
+ res.end();
4
+ return;
5
+ }
6
+ const classified = classify?.(err);
7
+ res.status(classified?.status ?? 500).json({
8
+ error: classified?.error ?? fallbackError,
9
+ message: err instanceof Error ? err.message : String(err),
10
+ });
11
+ }
12
+ /** Run a route body, mapping any throw or rejection to the error shape above.
13
+ * `fallbackError` is the route's stable failure label (e.g.
14
+ * "product-template generation failed"); `classify` upgrades known error
15
+ * classes to their own status + label (e.g. a seed error to a 400). */
16
+ export function guardRoute(res, fallbackError, fn, classify) {
17
+ try {
18
+ const out = fn();
19
+ if (out instanceof Promise) {
20
+ out.catch((err) => sendRouteError(res, err, fallbackError, classify));
21
+ }
22
+ }
23
+ catch (err) {
24
+ sendRouteError(res, err, fallbackError, classify);
25
+ }
26
+ }
package/sse.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { Request, RequestHandler } from "express";
2
+ export type PollingSseOptions<T> = {
3
+ /** Produce one snapshot for one client tick. Receives the request so the
4
+ * upstream can be addressed per-request (e.g. ?studioBase=). A throw or
5
+ * rejection is swallowed; the next tick retries. */
6
+ snapshot: (req: Request) => T | Promise<T>;
7
+ /** Time between frames. Defaults to 1s (funke's STREAM_INTERVAL_MS). */
8
+ intervalMs?: number;
9
+ };
10
+ /** SSE feed: emit a `data: <json>` frame immediately, then every intervalMs
11
+ * until the client disconnects. */
12
+ export declare function createPollingSseHandler<T>(opts: PollingSseOptions<T>): RequestHandler;
package/sse.js ADDED
@@ -0,0 +1,42 @@
1
+ // Polling Server-Sent-Events bridge, lifted from funke's /api/metrics/stream.
2
+ //
3
+ // Why SSE and not a proxied WebSocket: bun's node:http "upgrade" socket is not
4
+ // writable back to the client, so neither a hand-rolled raw-socket bridge nor
5
+ // http-proxy's .ws() can forward the 101 in-process. A product backend that
6
+ // needs an upstream WS feed (e.g. Studio's /live/firehose) therefore consumes
7
+ // it as a CLIENT and re-emits snapshots same-origin over SSE — a plain
8
+ // long-lived GET that also rides existing /api/* reverse-proxy locations with
9
+ // no Upgrade handshake and no new dependency.
10
+ /** SSE feed: emit a `data: <json>` frame immediately, then every intervalMs
11
+ * until the client disconnects. */
12
+ export function createPollingSseHandler(opts) {
13
+ const intervalMs = opts.intervalMs ?? 1000;
14
+ return (req, res) => {
15
+ res.set({
16
+ "Content-Type": "text/event-stream",
17
+ "Cache-Control": "no-cache, no-transform",
18
+ Connection: "keep-alive",
19
+ });
20
+ res.flushHeaders?.();
21
+ let closed = false;
22
+ const tick = async () => {
23
+ if (closed)
24
+ return;
25
+ try {
26
+ const value = await opts.snapshot(req);
27
+ if (!closed)
28
+ res.write(`data: ${JSON.stringify(value)}\n\n`);
29
+ }
30
+ catch {
31
+ // Transient upstream failures are swallowed; the next tick retries.
32
+ }
33
+ };
34
+ void tick();
35
+ const timer = setInterval(() => void tick(), intervalMs);
36
+ req.on("close", () => {
37
+ closed = true;
38
+ clearInterval(timer);
39
+ res.end();
40
+ });
41
+ };
42
+ }