@hyperframes/producer 0.10.6 → 0.10.7

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/README.md CHANGED
@@ -55,6 +55,14 @@ server never listens without a token. It listens on `127.0.0.1` unless you pass
55
55
  token yourself. `createProducerApp` without `authToken`, and `createRenderHandlers`
56
56
  always, check no caller: serve them only behind your own authentication.
57
57
 
58
+ `startServer` also answers `GET /health` on a second port, `PRODUCER_HEALTH_PORT` (default
59
+ `9848`), from its own thread, so a health probe is answered while a render keeps the main
60
+ thread busy. That port listens on the same address as the server itself, so it is loopback
61
+ only by default: a probe from another host needs `hostname` or `PRODUCER_HOST` set to an
62
+ address that host can reach, which also needs a token. A connection there that does not send
63
+ a whole request within a few seconds, or sits idle, is closed, and open connections are
64
+ capped at 64. Set `PRODUCER_DISABLE_HEALTH_WORKER=1` to leave the second port closed.
65
+
58
66
  A request's `projectDir` must resolve, symlinks included, inside
59
67
  `projectsDir` (`PRODUCER_PROJECTS_DIR`, default: the working directory). Its `entryFile`
60
68
  must stay inside that project, and `outputPath` must be inside `rendersDir`
@@ -1987,7 +1987,7 @@ async function parseSubCompositions(html, projectDir2, downloadDir2, visited = /
1987
1987
  }
1988
1988
  return { subCompositions };
1989
1989
  }
1990
- function promoteCssImportsToLinkTags(html) {
1990
+ function promoteCssImportsToLinkTags(html, budget) {
1991
1991
  const { document: document2 } = parseHTML4(html);
1992
1992
  const head = document2.querySelector("head");
1993
1993
  if (!head) return html;
@@ -1996,18 +1996,16 @@ function promoteCssImportsToLinkTags(html) {
1996
1996
  const styleEls = document2.querySelectorAll("style");
1997
1997
  for (const styleEl of styleEls) {
1998
1998
  const original = styleEl.textContent || "";
1999
- let modified = original;
2000
- let match;
2001
- importRe.lastIndex = 0;
2002
- while ((match = importRe.exec(original)) !== null) {
1999
+ const rulesBetweenImports = [];
2000
+ let keptTo = 0;
2001
+ for (const match of original.matchAll(importRe)) {
2003
2002
  const url = match[1] ?? "";
2004
2003
  if (!url.startsWith("http://") && !url.startsWith("https://")) continue;
2005
- if (seenUrls.has(url)) {
2006
- modified = modified.replace(match[0], "");
2007
- continue;
2008
- }
2004
+ rulesBetweenImports.push(original.slice(keptTo, match.index));
2005
+ keptTo = match.index + match[0].length;
2006
+ if (seenUrls.has(url)) continue;
2009
2007
  seenUrls.add(url);
2010
- modified = modified.replace(match[0], "");
2008
+ budget.chargeElements(ENTRY_PAGE, 2);
2011
2009
  const preload = document2.createElement("link");
2012
2010
  preload.setAttribute("rel", "preload");
2013
2011
  preload.setAttribute("href", url);
@@ -2018,8 +2016,8 @@ function promoteCssImportsToLinkTags(html) {
2018
2016
  link.setAttribute("href", url);
2019
2017
  head.appendChild(link);
2020
2018
  }
2021
- if (modified !== original) {
2022
- styleEl.textContent = modified;
2019
+ if (keptTo > 0) {
2020
+ styleEl.textContent = rulesBetweenImports.join("") + original.slice(keptTo);
2023
2021
  }
2024
2022
  }
2025
2023
  return document2.toString();
@@ -3066,7 +3064,7 @@ async function compileWithAssets(projectDir2, htmlPath2, downloadDir2, options2,
3066
3064
  ...options2.abortSignal ? [options2.abortSignal] : []
3067
3065
  ]);
3068
3066
  const fontSheetHtml = await inlineExternalFontStylesheets(
3069
- promoteCssImportsToLinkTags(sanitizedHtml),
3067
+ promoteCssImportsToLinkTags(sanitizedHtml, budget),
3070
3068
  budget,
3071
3069
  remoteFetches
3072
3070
  );
@@ -4314,7 +4314,7 @@ async function parseSubCompositions(html, projectDir, downloadDir, visited = /*
4314
4314
  }
4315
4315
  return { subCompositions };
4316
4316
  }
4317
- function promoteCssImportsToLinkTags(html) {
4317
+ function promoteCssImportsToLinkTags(html, budget) {
4318
4318
  const { document: document2 } = parseHTML5(html);
4319
4319
  const head = document2.querySelector("head");
4320
4320
  if (!head) return html;
@@ -4323,18 +4323,16 @@ function promoteCssImportsToLinkTags(html) {
4323
4323
  const styleEls = document2.querySelectorAll("style");
4324
4324
  for (const styleEl of styleEls) {
4325
4325
  const original = styleEl.textContent || "";
4326
- let modified = original;
4327
- let match;
4328
- importRe.lastIndex = 0;
4329
- while ((match = importRe.exec(original)) !== null) {
4326
+ const rulesBetweenImports = [];
4327
+ let keptTo = 0;
4328
+ for (const match of original.matchAll(importRe)) {
4330
4329
  const url = match[1] ?? "";
4331
4330
  if (!url.startsWith("http://") && !url.startsWith("https://")) continue;
4332
- if (seenUrls.has(url)) {
4333
- modified = modified.replace(match[0], "");
4334
- continue;
4335
- }
4331
+ rulesBetweenImports.push(original.slice(keptTo, match.index));
4332
+ keptTo = match.index + match[0].length;
4333
+ if (seenUrls.has(url)) continue;
4336
4334
  seenUrls.add(url);
4337
- modified = modified.replace(match[0], "");
4335
+ budget.chargeElements(ENTRY_PAGE, 2);
4338
4336
  const preload = document2.createElement("link");
4339
4337
  preload.setAttribute("rel", "preload");
4340
4338
  preload.setAttribute("href", url);
@@ -4345,8 +4343,8 @@ function promoteCssImportsToLinkTags(html) {
4345
4343
  link.setAttribute("href", url);
4346
4344
  head.appendChild(link);
4347
4345
  }
4348
- if (modified !== original) {
4349
- styleEl.textContent = modified;
4346
+ if (keptTo > 0) {
4347
+ styleEl.textContent = rulesBetweenImports.join("") + original.slice(keptTo);
4350
4348
  }
4351
4349
  }
4352
4350
  return document2.toString();
@@ -5393,7 +5391,7 @@ async function compileWithAssets(projectDir, htmlPath, downloadDir, options, ass
5393
5391
  ...options.abortSignal ? [options.abortSignal] : []
5394
5392
  ]);
5395
5393
  const fontSheetHtml = await inlineExternalFontStylesheets(
5396
- promoteCssImportsToLinkTags(sanitizedHtml),
5394
+ promoteCssImportsToLinkTags(sanitizedHtml, budget),
5397
5395
  budget,
5398
5396
  remoteFetches
5399
5397
  );
@@ -3,10 +3,14 @@ import { createServer } from "node:http";
3
3
  import { parentPort, workerData } from "node:worker_threads";
4
4
  var startTime = Date.now();
5
5
  var port = Number(workerData?.port ?? 9848);
6
+ var hostname = typeof workerData?.hostname === "string" && workerData.hostname ? workerData.hostname : "127.0.0.1";
7
+ var closeAfterMs = Number(workerData?.closeAfterMs) > 0 ? Number(workerData.closeAfterMs) : 5e3;
8
+ var MAX_CONNECTIONS = 64;
6
9
  if (!parentPort) {
7
10
  throw new Error("[healthWorkerThread] must run inside a worker_thread");
8
11
  }
9
- var server = createServer((req, res) => {
12
+ var options = { connectionsCheckingInterval: Math.min(1e3, closeAfterMs) };
13
+ var server = createServer(options, (req, res) => {
10
14
  if (req.method === "GET" && (req.url === "/health" || req.url?.startsWith("/health?"))) {
11
15
  const body = JSON.stringify({
12
16
  status: "ok",
@@ -25,9 +29,11 @@ var server = createServer((req, res) => {
25
29
  res.writeHead(404, { "Content-Type": "text/plain" });
26
30
  res.end("Not found");
27
31
  });
28
- server.keepAliveTimeout = 0;
29
- server.requestTimeout = 0;
30
- server.headersTimeout = 0;
32
+ server.headersTimeout = closeAfterMs;
33
+ server.requestTimeout = closeAfterMs;
34
+ server.keepAliveTimeout = closeAfterMs;
35
+ server.setTimeout(closeAfterMs * 2);
36
+ server.maxConnections = MAX_CONNECTIONS;
31
37
  server.on("error", (err) => {
32
38
  parentPort?.postMessage({
33
39
  type: "listen-error",
@@ -35,7 +41,7 @@ server.on("error", (err) => {
35
41
  });
36
42
  setTimeout(() => parentPort?.close(), 50);
37
43
  });
38
- server.listen(port, "0.0.0.0", () => {
44
+ server.listen(port, hostname, () => {
39
45
  parentPort?.postMessage({ type: "listening", port });
40
46
  });
41
47
  parentPort.on("message", (msg) => {
package/dist/index.js CHANGED
@@ -9116,7 +9116,7 @@ async function parseSubCompositions(html, projectDir, downloadDir, visited = /*
9116
9116
  }
9117
9117
  return { subCompositions };
9118
9118
  }
9119
- function promoteCssImportsToLinkTags(html) {
9119
+ function promoteCssImportsToLinkTags(html, budget) {
9120
9120
  const { document: document2 } = parseHTML5(html);
9121
9121
  const head = document2.querySelector("head");
9122
9122
  if (!head) return html;
@@ -9125,18 +9125,16 @@ function promoteCssImportsToLinkTags(html) {
9125
9125
  const styleEls = document2.querySelectorAll("style");
9126
9126
  for (const styleEl of styleEls) {
9127
9127
  const original = styleEl.textContent || "";
9128
- let modified = original;
9129
- let match;
9130
- importRe.lastIndex = 0;
9131
- while ((match = importRe.exec(original)) !== null) {
9128
+ const rulesBetweenImports = [];
9129
+ let keptTo = 0;
9130
+ for (const match of original.matchAll(importRe)) {
9132
9131
  const url = match[1] ?? "";
9133
9132
  if (!url.startsWith("http://") && !url.startsWith("https://")) continue;
9134
- if (seenUrls.has(url)) {
9135
- modified = modified.replace(match[0], "");
9136
- continue;
9137
- }
9133
+ rulesBetweenImports.push(original.slice(keptTo, match.index));
9134
+ keptTo = match.index + match[0].length;
9135
+ if (seenUrls.has(url)) continue;
9138
9136
  seenUrls.add(url);
9139
- modified = modified.replace(match[0], "");
9137
+ budget.chargeElements(ENTRY_PAGE, 2);
9140
9138
  const preload = document2.createElement("link");
9141
9139
  preload.setAttribute("rel", "preload");
9142
9140
  preload.setAttribute("href", url);
@@ -9147,8 +9145,8 @@ function promoteCssImportsToLinkTags(html) {
9147
9145
  link.setAttribute("href", url);
9148
9146
  head.appendChild(link);
9149
9147
  }
9150
- if (modified !== original) {
9151
- styleEl.textContent = modified;
9148
+ if (keptTo > 0) {
9149
+ styleEl.textContent = rulesBetweenImports.join("") + original.slice(keptTo);
9152
9150
  }
9153
9151
  }
9154
9152
  return document2.toString();
@@ -10198,7 +10196,7 @@ async function compileWithAssets(projectDir, htmlPath, downloadDir, options, ass
10198
10196
  ...options.abortSignal ? [options.abortSignal] : []
10199
10197
  ]);
10200
10198
  const fontSheetHtml = await inlineExternalFontStylesheets(
10201
- promoteCssImportsToLinkTags(sanitizedHtml),
10199
+ promoteCssImportsToLinkTags(sanitizedHtml, budget),
10202
10200
  budget,
10203
10201
  remoteFetches
10204
10202
  );
@@ -24226,7 +24224,11 @@ async function startHealthWorker(options = {}) {
24226
24224
  );
24227
24225
  }
24228
24226
  const worker = new Worker4(entry, {
24229
- workerData: { port },
24227
+ workerData: {
24228
+ port,
24229
+ hostname: options.hostname || "127.0.0.1",
24230
+ closeAfterMs: options.closeAfterMs
24231
+ },
24230
24232
  // Keep stdio inherited so the worker's console logs land in the pod logs
24231
24233
  // alongside the main thread's.
24232
24234
  stdout: false,
@@ -25208,7 +25210,7 @@ function startServer(options = {}) {
25208
25210
  server.setTimeout(0);
25209
25211
  server.requestTimeout = 0;
25210
25212
  server.keepAliveTimeout = 0;
25211
- const healthWorkerPromise = process.env.PRODUCER_DISABLE_HEALTH_WORKER === "1" ? Promise.resolve(null) : startHealthWorker({ logger: log }).catch((err) => {
25213
+ const healthWorkerPromise = process.env.PRODUCER_DISABLE_HEALTH_WORKER === "1" ? Promise.resolve(null) : startHealthWorker({ logger: log, hostname }).catch((err) => {
25212
25214
  log.error(`[server] health worker failed to start: ${err.message}`);
25213
25215
  return null;
25214
25216
  });
@@ -9138,7 +9138,7 @@ async function parseSubCompositions(html, projectDir, downloadDir, visited = /*
9138
9138
  }
9139
9139
  return { subCompositions };
9140
9140
  }
9141
- function promoteCssImportsToLinkTags(html) {
9141
+ function promoteCssImportsToLinkTags(html, budget) {
9142
9142
  const { document: document2 } = parseHTML5(html);
9143
9143
  const head = document2.querySelector("head");
9144
9144
  if (!head) return html;
@@ -9147,18 +9147,16 @@ function promoteCssImportsToLinkTags(html) {
9147
9147
  const styleEls = document2.querySelectorAll("style");
9148
9148
  for (const styleEl of styleEls) {
9149
9149
  const original = styleEl.textContent || "";
9150
- let modified = original;
9151
- let match;
9152
- importRe.lastIndex = 0;
9153
- while ((match = importRe.exec(original)) !== null) {
9150
+ const rulesBetweenImports = [];
9151
+ let keptTo = 0;
9152
+ for (const match of original.matchAll(importRe)) {
9154
9153
  const url = match[1] ?? "";
9155
9154
  if (!url.startsWith("http://") && !url.startsWith("https://")) continue;
9156
- if (seenUrls.has(url)) {
9157
- modified = modified.replace(match[0], "");
9158
- continue;
9159
- }
9155
+ rulesBetweenImports.push(original.slice(keptTo, match.index));
9156
+ keptTo = match.index + match[0].length;
9157
+ if (seenUrls.has(url)) continue;
9160
9158
  seenUrls.add(url);
9161
- modified = modified.replace(match[0], "");
9159
+ budget.chargeElements(ENTRY_PAGE, 2);
9162
9160
  const preload = document2.createElement("link");
9163
9161
  preload.setAttribute("rel", "preload");
9164
9162
  preload.setAttribute("href", url);
@@ -9169,8 +9167,8 @@ function promoteCssImportsToLinkTags(html) {
9169
9167
  link.setAttribute("href", url);
9170
9168
  head.appendChild(link);
9171
9169
  }
9172
- if (modified !== original) {
9173
- styleEl.textContent = modified;
9170
+ if (keptTo > 0) {
9171
+ styleEl.textContent = rulesBetweenImports.join("") + original.slice(keptTo);
9174
9172
  }
9175
9173
  }
9176
9174
  return document2.toString();
@@ -10217,7 +10215,7 @@ async function compileWithAssets(projectDir, htmlPath, downloadDir, options, ass
10217
10215
  ...options.abortSignal ? [options.abortSignal] : []
10218
10216
  ]);
10219
10217
  const fontSheetHtml = await inlineExternalFontStylesheets(
10220
- promoteCssImportsToLinkTags(sanitizedHtml),
10218
+ promoteCssImportsToLinkTags(sanitizedHtml, budget),
10221
10219
  budget,
10222
10220
  remoteFetches
10223
10221
  );
@@ -19068,7 +19066,11 @@ async function startHealthWorker(options = {}) {
19068
19066
  );
19069
19067
  }
19070
19068
  const worker = new Worker4(entry, {
19071
- workerData: { port },
19069
+ workerData: {
19070
+ port,
19071
+ hostname: options.hostname || "127.0.0.1",
19072
+ closeAfterMs: options.closeAfterMs
19073
+ },
19072
19074
  // Keep stdio inherited so the worker's console logs land in the pod logs
19073
19075
  // alongside the main thread's.
19074
19076
  stdout: false,
@@ -20514,7 +20516,7 @@ function startServer(options = {}) {
20514
20516
  server.setTimeout(0);
20515
20517
  server.requestTimeout = 0;
20516
20518
  server.keepAliveTimeout = 0;
20517
- const healthWorkerPromise = process.env.PRODUCER_DISABLE_HEALTH_WORKER === "1" ? Promise.resolve(null) : startHealthWorker({ logger: log }).catch((err) => {
20519
+ const healthWorkerPromise = process.env.PRODUCER_DISABLE_HEALTH_WORKER === "1" ? Promise.resolve(null) : startHealthWorker({ logger: log, hostname }).catch((err) => {
20518
20520
  log.error(`[server] health worker failed to start: ${err.message}`);
20519
20521
  return null;
20520
20522
  });
@@ -17,13 +17,6 @@
17
17
  * the probe `timeoutSeconds` (5s in prod prior to the companion change)
18
18
  * triggers a SIGKILL even when the process is still alive — just busy.
19
19
  *
20
- * Today's incident (2026-06-26): an infinite GSAP timeline caused the
21
- * distributed planner to try to enumerate ~300_000_000_000 frames, and
22
- * the sidecar got killed mid-arithmetic. Miguel's upstream `plan()`
23
- * duration guard kills that input class at the source. This module is
24
- * defense-in-depth: future wedge classes (sync I/O on video-heavy comps,
25
- * runaway loops, GC pauses) shouldn't kill an otherwise-alive pod either.
26
- *
27
20
  * Contract
28
21
  * --------
29
22
  *
@@ -33,10 +26,10 @@
33
26
  * which is a strict subset of process liveness. If the entire Node process
34
27
  * is dead the OS tears down both threads' sockets simultaneously, so the
35
28
  * worker_thread's listener stops answering and k8s correctly kills the pod.
36
- * - Listening on `0.0.0.0` is intentional: this is the probe entry point and
37
- * the sidecar already exposes other ports for in-pod traffic only. The
38
- * endpoint returns the same shape the main-thread `/health` always returned
39
- * so existing observability keeps working.
29
+ * - The listener binds the address it is given (`startServer` passes the main
30
+ * server's own), loopback when given none, and answers in the shape the
31
+ * main-thread `/health` always returned. A connection with no whole request
32
+ * within a few seconds, or idle after one, is closed; connections are capped.
40
33
  *
41
34
  * The main thread still serves `/health` on the main port (9847) for
42
35
  * backwards compatibility; k8s probe config in `heygen-com/app` can migrate
@@ -45,6 +38,10 @@
45
38
  export interface HealthWorkerOptions {
46
39
  /** Port the worker_thread health endpoint listens on. Default 9848 / env. */
47
40
  port?: number;
41
+ /** Address the endpoint listens on. Default `127.0.0.1`. */
42
+ hostname?: string;
43
+ /** How long a connection may go without a whole request, or sit idle. Default 5000 ms. */
44
+ closeAfterMs?: number;
48
45
  /**
49
46
  * Optional logger; falls back to console. Note: the worker thread itself
50
47
  * cannot use the parent's logger directly (separate isolate), so it logs
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Worker-thread entry for the off-main-thread /health endpoint.
3
3
  *
4
- * Runs a minimal `node:http` server bound to `workerData.port` that answers
4
+ * Runs a minimal `node:http` server bound to `workerData.port` on `workerData.hostname` that answers
5
5
  * `GET /health` with `{ status, uptime, timestamp, thread: "worker" }`.
6
6
  *
7
7
  * Lifecycle:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/producer",
3
- "version": "0.10.6",
3
+ "version": "0.10.7",
4
4
  "description": "HTML-to-video rendering engine using Chrome's BeginFrame API",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,12 +35,12 @@
35
35
  "postcss": "^8.4.0",
36
36
  "puppeteer": "~25.13.0",
37
37
  "puppeteer-core": "^25.13.0",
38
- "@hyperframes/engine": "0.10.6",
39
- "@hyperframes/parsers": "0.10.6",
40
- "@hyperframes/studio-server": "0.10.6",
41
- "@hyperframes/share": "0.10.6",
42
- "@hyperframes/lint": "0.10.6",
43
- "@hyperframes/core": "0.10.6"
38
+ "@hyperframes/core": "0.10.7",
39
+ "@hyperframes/engine": "0.10.7",
40
+ "@hyperframes/lint": "0.10.7",
41
+ "@hyperframes/parsers": "0.10.7",
42
+ "@hyperframes/share": "0.10.7",
43
+ "@hyperframes/studio-server": "0.10.7"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/node": "^25.0.10",
@@ -49,7 +49,7 @@
49
49
  "tsx": "^4.21.0",
50
50
  "typescript": "^5.7.2",
51
51
  "vitest": "^4.1.12",
52
- "@hyperframes/aws-lambda": "0.10.6"
52
+ "@hyperframes/aws-lambda": "0.10.7"
53
53
  },
54
54
  "engines": {
55
55
  "node": ">=22.19.0"