nanoodle 0.6.0 → 0.6.1

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
@@ -105,6 +105,11 @@ stays in the browser) both work, as do da.gd/TinyURL short links (resolved by
105
105
  reading redirect headers; no credentials are ever sent). Direct links decode
106
106
  fully offline. Quote the URL — `#` starts a comment in most shells.
107
107
 
108
+ Links mangled in transit (a character flipped or dropped by a chat app or a
109
+ copy/paste) are recovered best-effort: the graph's nodes and wires are salvaged
110
+ and a warning is printed. Only damage inside the graph itself makes a link
111
+ unrecoverable.
112
+
108
113
  ## Inputs, outputs, settings
109
114
 
110
115
  ```js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoodle",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Run nanoodle visual AI workflows from JavaScript — zero-dependency executor for saved noodle-graph.json files",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -690,6 +690,45 @@ export async function resizeCropImage(url, mode, tw, th, { fetch: fetchFn, signa
690
690
  return resizeCropImageFfmpeg(url, m, w, h, { fetch: fetchFn, signal });
691
691
  }
692
692
 
693
+ /* ---------- fit an oversized image under the inline send budget ------------ */
694
+
695
+ /** Ladder of long-edge caps tried in order; "fit" never upscales, so a dense
696
+ small image just re-encodes at its own size until a smaller rung shrinks it. */
697
+ const FIT_DIMS = [2048, 1448, 1024, 724, 512];
698
+ /** Headroom under MEDIA_INLINE_MAX for the JSON envelope around the data URL
699
+ (model id, prompt, opts) so a just-fitting image doesn't tip the whole body over. */
700
+ export const INLINE_IMAGE_BUDGET = MEDIA_INLINE_MAX - 128 * 1024;
701
+
702
+ /**
703
+ * Return `url` unchanged when it already fits the inline send budget; otherwise
704
+ * downscale it (aspect preserved) until it does. NanoGPT has no upload endpoint —
705
+ * media rides base64 inside a ~4.4 MB request body (verified live: 413
706
+ * FUNCTION_PAYLOAD_TOO_LARGE at 4.5 MB) — so an image over the budget is a
707
+ * guaranteed reject; a resized frame beats a dead node. http(s) URLs pass through
708
+ * (they ride by reference). Callers announce the shrink via onShrink/progress.
709
+ */
710
+ export async function fitImageInline(url, { budget = INLINE_IMAGE_BUDGET, fetch: fetchFn, signal, onShrink } = {}) {
711
+ if (typeof url !== "string" || !url.startsWith("data:") || url.length <= budget) return url;
712
+ for (const dim of FIT_DIMS) {
713
+ throwIfAborted(signal);
714
+ let out;
715
+ try {
716
+ out = await resizeCropImage(url, "fit", dim, dim, { fetch: fetchFn, signal });
717
+ } catch (e) {
718
+ // pure-PNG path refuses results still over MEDIA_INLINE_MAX at this rung → try smaller
719
+ if (e instanceof NanoodleError && /inline limit/i.test(e.message || "")) continue;
720
+ throw new NanoodleError(
721
+ "image is too large to send inline (~4 MB max) and couldn't be resized down (" +
722
+ (e && e.message ? e.message : e) + ") — use a smaller image");
723
+ }
724
+ if (out.length <= budget) {
725
+ if (onShrink) onShrink(dim);
726
+ return out;
727
+ }
728
+ }
729
+ throw new NanoodleError("image is too large to send inline even after resizing (~4 MB max) — use a smaller image");
730
+ }
731
+
693
732
  async function resizeCropImageFfmpeg(url, m, w, h, { fetch: fetchFn, signal } = {}) {
694
733
  return withTemp(async (dir) => {
695
734
  throwIfAborted(signal);
@@ -824,17 +863,37 @@ export async function extractVideoFrames(url, { count = 1, gap = 0.5, dir = "end
824
863
  const n = Math.max(1, Math.min(MAX_FRAMES, parseInt(count, 10) || 1));
825
864
  const stepSec = Number.isFinite(Number(gap)) ? Math.max(0, Number(gap)) : 0.5;
826
865
  const fromEnd = (dir || "end") === "end";
827
- const EPS = 0.04;
828
866
 
829
867
  return withTemp(async (dir) => {
830
868
  const inPath = await writeInput(dir, "in", url, fetchFn);
831
869
  throwIfAborted(signal);
832
870
  const pr = await runProc("ffprobe", [
833
- "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", inPath,
871
+ "-v", "error", "-select_streams", "v:0",
872
+ "-show_entries", "stream=duration,avg_frame_rate", "-show_entries", "format=duration",
873
+ "-of", "json", inPath,
834
874
  ], { signal });
835
- const dur = parseFloat(String(pr.stdout).trim());
836
- if (!isFinite(dur) || dur <= 0) throw new NanoodleError("video has no readable duration");
837
-
875
+ let probed = {};
876
+ try { probed = JSON.parse(String(pr.stdout)); } catch { /* handled by the finite checks below */ }
877
+ const v0 = (probed.streams && probed.streams[0]) || {};
878
+ // Prefer the VIDEO stream's duration: the container's format duration includes the
879
+ // audio track, which routinely outlasts the last video frame (-shortest muxes,
880
+ // padded AAC) — seeking near THAT end decodes zero frames.
881
+ const vdur = parseFloat(v0.duration);
882
+ const fdur = parseFloat((probed.format || {}).duration);
883
+ const dur = [vdur, fdur].filter((d) => isFinite(d) && d > 0).reduce((a, b) => Math.min(a, b), Infinity);
884
+ if (!isFinite(dur)) throw new NanoodleError("video has no readable duration");
885
+ // The last frame's PTS sits a full frame interval before the stream end, so the
886
+ // back-off from the end must exceed one frame or ffmpeg outputs nothing (pts >= t
887
+ // selection). 1.5 frames at the probed rate; 0.1s when the rate is unreadable.
888
+ const rate = String(v0.avg_frame_rate || "").split("/").map(Number);
889
+ const fps = rate.length === 2 && rate[0] > 0 && rate[1] > 0 ? rate[0] / rate[1] : 0;
890
+ const EPS = fps > 0 ? Math.max(0.04, 1.5 / fps) : 0.1;
891
+
892
+ const grab = async (t, framePath) => {
893
+ await runProc("ffmpeg", [
894
+ "-y", "-ss", String(t), "-i", inPath, "-frames:v", "1", "-q:v", "2", framePath,
895
+ ], { signal });
896
+ };
838
897
  const out = {};
839
898
  for (let i = 0; i < n; i++) {
840
899
  throwIfAborted(signal);
@@ -842,9 +901,14 @@ export async function extractVideoFrames(url, { count = 1, gap = 0.5, dir = "end
842
901
  let t = fromEnd ? (dur - EPS - i * stepSec) : (i * stepSec);
843
902
  t = Math.max(0, Math.min(Math.max(0, dur - EPS), t));
844
903
  const framePath = join(dir, `f${i + 1}.jpg`);
845
- await runProc("ffmpeg", [
846
- "-y", "-ss", String(t), "-i", inPath, "-frames:v", "1", "-q:v", "2", framePath,
847
- ], { signal });
904
+ try {
905
+ await grab(t, framePath);
906
+ } catch (e) {
907
+ // Reported durations can still overshoot the last decodable frame (VFR, sloppy
908
+ // muxes) — step back once before giving up.
909
+ if (t <= 0) throw e;
910
+ await grab(Math.max(0, t - 0.5), framePath);
911
+ }
848
912
  out["frame" + (i + 1)] = await dataUrlFromFile(framePath, "image/jpeg");
849
913
  }
850
914
  return out;
package/src/nodes.mjs CHANGED
@@ -4,7 +4,7 @@ import { IMG_PORT_RE, EDIT_IMG_RE, REF_PORT_RE, CLIP_PORT_RE, VID_PORT_RE, optio
4
4
  import { MEDIA_INLINE_MAX } from "./media.mjs";
5
5
  import {
6
6
  resizeCropImage, trimAudioToWav, extractAudioToWav,
7
- extractVideoFrames, concatVideos, muxSoundtrack, maskToSource,
7
+ extractVideoFrames, concatVideos, muxSoundtrack, maskToSource, fitImageInline,
8
8
  } from "./local-media.mjs";
9
9
 
10
10
  function mdl(n) {
@@ -27,6 +27,23 @@ function portIdx(name) {
27
27
  return m ? +m[1] : 1;
28
28
  }
29
29
 
30
+ /**
31
+ * Shrink an inline image under the ~4.4 MB request-body budget before a paid send —
32
+ * over it, NanoGPT 413s (FUNCTION_PAYLOAD_TOO_LARGE, verified live; there is no upload
33
+ * endpoint), so a downscaled image beats a dead node. Modern image models routinely
34
+ * return 4K PNGs (~13 MB as base64), which killed every generate→animate/edit chain.
35
+ * Browser hosts may inject ctx.fitImage (canvas path); default is local-media's
36
+ * (pure-JS for PNG, ffmpeg otherwise). http(s) URLs and already-fitting images pass
37
+ * through untouched.
38
+ */
39
+ async function fitImage(url, ctx, what) {
40
+ if (url == null) return url;
41
+ const fit = (ctx && ctx.fitImage) || fitImageInline;
42
+ const out = await fit(url, mediaOpts(ctx));
43
+ if (out !== url && ctx && ctx.progress) ctx.progress(what + " resized to fit the ~4 MB send limit");
44
+ return out;
45
+ }
46
+
30
47
  function collectPorts(inp, re) {
31
48
  return Object.keys(inp)
32
49
  .filter((k) => re.test(k))
@@ -487,7 +504,7 @@ export const RUNNERS = {
487
504
 
488
505
  async llm(n, inp, ctx) {
489
506
  const prompt = promptOf(n, inp, "no prompt");
490
- const imgs = collectPorts(inp, IMG_PORT_RE);
507
+ const imgs = await Promise.all(collectPorts(inp, IMG_PORT_RE).map((u) => fitImage(u, ctx, "wired image")));
491
508
  // hosted audio (music/tts nodes return https CDN URLs verbatim) → download + inline as base64:
492
509
  // the chat input_audio part carries bytes, never a URL
493
510
  let audioPart = null;
@@ -511,9 +528,10 @@ export const RUNNERS = {
511
528
  async vision(n, inp, ctx) {
512
529
  if (!inp.image) throw new NanoodleError("no image input");
513
530
  const q = (n.fields.q || "Describe this image.").trim();
531
+ const img = await fitImage(inp.image, ctx, "image");
514
532
  const messages = [{
515
533
  role: "user",
516
- content: [{ type: "text", text: q }, { type: "image_url", image_url: { url: inp.image } }],
534
+ content: [{ type: "text", text: q }, { type: "image_url", image_url: { url: img } }],
517
535
  }];
518
536
  return { text: await ctx.chat(messages, mdl(n), {}) };
519
537
  },
@@ -546,6 +564,7 @@ export const RUNNERS = {
546
564
  }
547
565
  const prompt = promptOf(n, inp);
548
566
  if (!prompt && !/upscal/i.test(n.fields.model || "")) throw new NanoodleError("no edit instruction");
567
+ imgs = await Promise.all(imgs.map((u) => fitImage(u, ctx, "source image")));
549
568
  guardRefsSize(imgs);
550
569
  const src = imgs.length > 1 ? imgs : imgs[0]; // array → multi-image composite; string → single edit
551
570
  return { image: await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: src, extra: imgExtra(n) }) };
@@ -553,7 +572,7 @@ export const RUNNERS = {
553
572
 
554
573
  async draw(n, inp, ctx) {
555
574
  const prompt = promptOf(n, inp, "no prompt");
556
- const imgs = collectPorts(inp, IMG_PORT_RE);
575
+ const imgs = await Promise.all(collectPorts(inp, IMG_PORT_RE).map((u) => fitImage(u, ctx, "wired image")));
557
576
  guardRefsSize(imgs);
558
577
  const messages = chatMessages(n, prompt, imgs, null);
559
578
  const res = await ctx.chatImage(messages, mdl(n), {});
@@ -589,8 +608,8 @@ export const RUNNERS = {
589
608
  if (!inp.image) throw new NanoodleError("no image input");
590
609
  const prompt = promptOf(n, inp);
591
610
  const opts = { ...videoDims(n, ctx), lora: loraParams(n), extra: n.fields.modelOpts || {} };
592
- if (inp.endframe) opts.last_image = inp.endframe;
593
- return { video: await ctx.video(mdl(n), prompt, opts, inp.image) };
611
+ if (inp.endframe) opts.last_image = await fitImage(inp.endframe, ctx, "end frame");
612
+ return { video: await ctx.video(mdl(n), prompt, opts, await fitImage(inp.image, ctx, "source image")) };
594
613
  },
595
614
 
596
615
  async vedit(n, inp, ctx) {
@@ -614,6 +633,7 @@ export const RUNNERS = {
614
633
  // ctx.trimAudio lets a browser host inject its Web Audio trimmer so the retry bytes match
615
634
  // its built-in runner exactly; default is the local-media trimmer (pure-JS WAV, ffmpeg).
616
635
  const trim = ctx.trimAudio || ((url, start, len, rate) => trimAudioToWav(url, start, len, rate, mediaOpts(ctx)));
636
+ const img = await fitImage(inp.image, ctx, "portrait image");
617
637
  let trimSec = null;
618
638
  for (let attempt = 0; attempt < 2; attempt++) {
619
639
  let opts;
@@ -636,7 +656,7 @@ export const RUNNERS = {
636
656
  Object.assign(opts, videoDims(n, ctx));
637
657
  opts.extra = n.fields.modelOpts || {};
638
658
  try {
639
- return { video: await ctx.video(mdl(n), prompt, opts, inp.image) };
659
+ return { video: await ctx.video(mdl(n), prompt, opts, img) };
640
660
  } catch (e) {
641
661
  if (attempt > 0) throw e;
642
662
  const msg = (e && e.message) || "";
package/src/share.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { gunzip } from "./zlib.mjs";
1
+ import { gunzip, gunzipLax } from "./zlib.mjs";
2
2
  import { base64ToBytes } from "./media.mjs";
3
3
  import { NanoodleError } from "./errors.mjs";
4
4
 
@@ -44,11 +44,69 @@ async function gunzipText(buf, what) {
44
44
  catch { throw new NanoodleError(`share link: ${what} payload is not valid gzip data — the link may be truncated`); }
45
45
  }
46
46
 
47
+ /* ---- best-effort salvage for damaged links ----------------------------------
48
+ Links get mangled in transit all the time — chat apps, line wraps, and manual
49
+ copy/paste flip or drop a character, which breaks the gzip CRC (and often a
50
+ few JSON characters) while leaving most of the payload intact. Executors only
51
+ need `nodes` and `links`, so when strict decoding fails we lax-decompress
52
+ (trailer ignored, partial output kept) and pull those two arrays out of the
53
+ damaged text. Cosmetic editor state (view, nid/lid) is sacrificed; damage
54
+ inside the graph itself still fails with the original error. Results carry
55
+ `recovered: true` so callers can warn. */
56
+
57
+ /** Index of the bracket closing text[i] (a "[" or "{"), string-aware; -1 when unbalanced. */
58
+ function matchBracket(text, i) {
59
+ const open = text[i];
60
+ if (open !== "[" && open !== "{") return -1;
61
+ let depth = 0, inStr = false;
62
+ for (let j = i; j < text.length; j++) {
63
+ const c = text[j];
64
+ if (inStr) {
65
+ if (c === "\\") j++;
66
+ else if (c === '"') inStr = false;
67
+ } else if (c === '"') inStr = true;
68
+ else if (c === "[" || c === "{") depth++;
69
+ else if (c === "]" || c === "}") { depth--; if (!depth) return j; }
70
+ }
71
+ return -1;
72
+ }
73
+
74
+ /** Parse the value of `"key": …` out of possibly-damaged JSON text; null when no occurrence parses. */
75
+ function extractJsonValue(text, key) {
76
+ const needle = `"${key}"`;
77
+ for (let from = 0; ;) {
78
+ const at = text.indexOf(needle, from);
79
+ if (at === -1) return null;
80
+ from = at + 1;
81
+ let j = at + needle.length;
82
+ while (j < text.length && /\s/.test(text[j])) j++;
83
+ if (text[j] !== ":") continue;
84
+ j++;
85
+ while (j < text.length && /\s/.test(text[j])) j++;
86
+ const end = matchBracket(text, j);
87
+ if (end === -1) continue;
88
+ try { return JSON.parse(text.slice(j, end + 1)); } catch { /* damaged here — try the next occurrence */ }
89
+ }
90
+ }
91
+
92
+ function salvageGraph(text) {
93
+ if (!text) return null;
94
+ const nodes = extractJsonValue(text, "nodes");
95
+ if (!Array.isArray(nodes) || !nodes.length || !nodes.every((n) => n && typeof n === "object" && typeof n.type === "string")) return null;
96
+ const links = extractJsonValue(text, "links");
97
+ return { v: 1, nodes, links: Array.isArray(links) ? links : [] };
98
+ }
99
+
100
+ const laxText = (bytes) => (bytes && bytes.length ? utf8.decode(bytes) : null);
101
+
47
102
  /**
48
103
  * Decode a share fragment ("#g=…", "g=…", "#a=…", …) to its graph.
49
104
  * Async since v0.4: gzip decoding goes through DecompressionStream in the
50
105
  * browser, which has no synchronous form.
51
- * @returns {Promise<{ graph: object, kind: "g"|"j"|"a", app: { name?, lang?, hasFiles: boolean }|null }>}
106
+ * @returns {Promise<{ graph: object, kind: "g"|"j"|"a", app: { name?, lang?, hasFiles: boolean }|null, recovered?: true }>}
107
+ * `recovered: true` marks a damaged link whose graph was salvaged best-effort
108
+ * (nodes + links only — cosmetic editor state is dropped); warn the user and
109
+ * suggest re-copying the link.
52
110
  */
53
111
  export async function decodeShareFragment(fragment) {
54
112
  let f = String(fragment);
@@ -59,30 +117,62 @@ export async function decodeShareFragment(fragment) {
59
117
  "Open the link in a browser and use 🔗 Share to mint a #g= workflow link instead.");
60
118
  }
61
119
  if (f.startsWith("g=")) {
62
- return { graph: parseJson(await gunzipText(b64urlToBytes(f.slice(2), "#g="), "#g="), "#g="), kind: "g", app: null };
120
+ const buf = b64urlToBytes(f.slice(2), "#g=");
121
+ let text = null, strictErr;
122
+ try { text = await gunzipText(buf, "#g="); } catch (e) { strictErr = e; }
123
+ if (text !== null) {
124
+ try { return { graph: parseJson(text, "#g="), kind: "g", app: null }; }
125
+ catch (e) { strictErr = e; }
126
+ } else {
127
+ text = laxText(await gunzipLax(buf));
128
+ }
129
+ const graph = salvageGraph(text);
130
+ if (!graph) throw strictErr;
131
+ return { graph, kind: "g", app: null, recovered: true };
63
132
  }
64
133
  if (f.startsWith("j=")) {
65
- return { graph: parseJson(utf8.decode(b64urlToBytes(f.slice(2), "#j=")), "#j="), kind: "j", app: null };
134
+ const text = utf8.decode(b64urlToBytes(f.slice(2), "#j="));
135
+ try { return { graph: parseJson(text, "#j="), kind: "j", app: null }; }
136
+ catch (e) {
137
+ const graph = salvageGraph(text);
138
+ if (!graph) throw e;
139
+ return { graph, kind: "j", app: null, recovered: true };
140
+ }
66
141
  }
67
142
  if (f.startsWith("a=")) {
68
143
  const tag = f.slice(2);
69
- const json = tag[0] === "u"
70
- ? utf8.decode(b64urlToBytes(tag.slice(1), "#a=u"))
71
- : await gunzipText(b64urlToBytes(tag, "#a="), "#a=");
72
- const payload = parseJson(json, "#a=");
73
- if (!payload || typeof payload !== "object" || !payload.graph) {
74
- throw new NanoodleError("share link: #a= app payload has no graph in it");
144
+ let json = null, strictErr;
145
+ if (tag[0] === "u") {
146
+ json = utf8.decode(b64urlToBytes(tag.slice(1), "#a=u"));
147
+ } else {
148
+ const buf = b64urlToBytes(tag, "#a=");
149
+ try { json = await gunzipText(buf, "#a="); }
150
+ catch (e) { strictErr = e; json = laxText(await gunzipLax(buf)); }
151
+ }
152
+ if (!strictErr) {
153
+ let payload;
154
+ try { payload = parseJson(json, "#a="); } catch (e) { strictErr = e; payload = null; }
155
+ if (payload) {
156
+ if (typeof payload !== "object" || !payload.graph) {
157
+ throw new NanoodleError("share link: #a= app payload has no graph in it");
158
+ }
159
+ // files/samples/lang are play.html presentation — executors run graphs, not apps.
160
+ return {
161
+ graph: payload.graph,
162
+ kind: "a",
163
+ app: {
164
+ ...(typeof payload.name === "string" && payload.name ? { name: payload.name } : {}),
165
+ ...(typeof payload.lang === "string" && payload.lang ? { lang: payload.lang } : {}),
166
+ hasFiles: !!payload.files,
167
+ },
168
+ };
169
+ }
75
170
  }
76
- // files/samples/lang are play.html presentation — executors run graphs, not apps.
77
- return {
78
- graph: payload.graph,
79
- kind: "a",
80
- app: {
81
- ...(typeof payload.name === "string" && payload.name ? { name: payload.name } : {}),
82
- ...(typeof payload.lang === "string" && payload.lang ? { lang: payload.lang } : {}),
83
- hasFiles: !!payload.files,
84
- },
85
- };
171
+ // salvage: the app payload nests its graph — prefer the intact "graph" object, else its nodes/links
172
+ const nested = json != null ? extractJsonValue(json, "graph") : null;
173
+ const graph = nested && typeof nested === "object" && Array.isArray(nested.nodes) ? nested : salvageGraph(json);
174
+ if (!graph) throw strictErr;
175
+ return { graph, kind: "a", app: { hasFiles: false }, recovered: true };
86
176
  }
87
177
  throw new NanoodleError(`share link: no #g=/#j=/#a= fragment found in "${fragment}"`);
88
178
  }
@@ -102,7 +192,7 @@ function fragmentOf(url) {
102
192
  *
103
193
  * @param {string} input
104
194
  * @param {{ fetch?: typeof fetch, maxHops?: number }} [opts]
105
- * @returns {Promise<{ graph: object, kind: "g"|"j"|"a", app: object|null, url: string }>}
195
+ * @returns {Promise<{ graph: object, kind: "g"|"j"|"a", app: object|null, url: string, recovered?: true }>}
106
196
  */
107
197
  export async function decodeShareUrl(input, opts = {}) {
108
198
  const s = String(input).trim();
package/src/workflow.mjs CHANGED
@@ -117,7 +117,10 @@ export class Workflow {
117
117
  */
118
118
  static async load(src, opts = {}) {
119
119
  if (isShareRef(src)) {
120
- const { graph } = await decodeShareUrl(src, { fetch: opts.fetch });
120
+ const { graph, recovered } = await decodeShareUrl(src, { fetch: opts.fetch });
121
+ if (recovered && !opts.quiet) {
122
+ warnGraph("share link was damaged (usually a copy/paste artifact) — recovered the graph's nodes and wires best-effort; re-copy the link from the editor for a pristine version");
123
+ }
121
124
  return new Workflow(graph, opts);
122
125
  }
123
126
  if (typeof src === "string" && /^\s*[\[{]/.test(src)) {
@@ -374,7 +377,13 @@ export class Workflow {
374
377
 
375
378
  _coerceInput(entry, value, mediaOpts) {
376
379
  if (MEDIA_KINDS.has(entry.kind)) {
377
- return coerceMediaInput(value, `input "${entry.key}"`, mediaOpts);
380
+ // Plain image inputs skip the load-time inline cap: every image-sending node
381
+ // downscales an oversized image to the send budget (fitImage), so a big photo
382
+ // is a shrink, not a refusal. Audio/video (and inpaint, whose mask must keep
383
+ // the source's dimensions) can't be shrunk that way — keep their early,
384
+ // before-any-spend guard.
385
+ const opts = entry.kind === "image" ? { ...mediaOpts, enforceInlineMax: false } : mediaOpts;
386
+ return coerceMediaInput(value, `input "${entry.key}"`, opts);
378
387
  }
379
388
  if (entry.kind === "choice") {
380
389
  const v = String(value);
package/src/zlib.mjs CHANGED
@@ -35,10 +35,68 @@ export async function gunzip(bytes) {
35
35
  return streamZlib.gunzip(bytes);
36
36
  }
37
37
 
38
+ /** Offset of the deflate body inside a gzip member, or -1 when the header is not gzip. */
39
+ function gzipBodyStart(b) {
40
+ if (b.length < 11 || b[0] !== 0x1f || b[1] !== 0x8b || b[2] !== 8) return -1;
41
+ const flg = b[3];
42
+ let i = 10;
43
+ if (flg & 4) { if (i + 2 > b.length) return -1; i += 2 + (b[i] | (b[i + 1] << 8)); } // FEXTRA
44
+ if (flg & 8) { while (i < b.length && b[i] !== 0) i++; i++; } // FNAME
45
+ if (flg & 16) { while (i < b.length && b[i] !== 0) i++; i++; } // FCOMMENT
46
+ if (flg & 2) i += 2; // FHCRC
47
+ return i < b.length ? i : -1;
48
+ }
49
+
50
+ async function streamInflateRawPartial(body) {
51
+ const chunks = [];
52
+ try {
53
+ const reader = new Blob([body]).stream().pipeThrough(new DecompressionStream("deflate-raw")).getReader();
54
+ try {
55
+ for (;;) {
56
+ const { done, value } = await reader.read();
57
+ if (done) break;
58
+ chunks.push(value);
59
+ }
60
+ } catch { /* keep whatever decompressed before the stream errored */ }
61
+ } catch { return null; }
62
+ let len = 0;
63
+ for (const c of chunks) len += c.length;
64
+ if (!len) return null;
65
+ const out = new Uint8Array(len);
66
+ let o = 0;
67
+ for (const c of chunks) { out.set(c, o); o += c.length; }
68
+ return out;
69
+ }
70
+
71
+ /**
72
+ * Best-effort gunzip for damaged members: inflates the raw deflate body and
73
+ * ignores the CRC32/ISIZE trailer entirely, returning partial output on
74
+ * truncation. null when nothing decompressible remains.
75
+ */
76
+ export async function gunzipLax(bytes) {
77
+ const start = gzipBodyStart(bytes);
78
+ if (start < 0) return null;
79
+ // The 8-byte trailer is junk to a raw-deflate decoder; dropping it up front
80
+ // keeps strict stream implementations from erroring on trailing garbage.
81
+ // (When the payload is truncated mid-body this trims real data — the
82
+ // partial-output paths below still salvage everything before the cut.)
83
+ const body = bytes.subarray(start, Math.max(start + 1, bytes.length - 8));
84
+ const z = await nodeZlib();
85
+ if (z) {
86
+ try { return new Uint8Array(z.inflateRawSync(body, { finishFlush: z.constants.Z_SYNC_FLUSH })); }
87
+ catch { return null; }
88
+ }
89
+ return streamInflateRawPartial(body);
90
+ }
91
+
38
92
  // The browser implementations, exported so tests can exercise them in Node
39
93
  // (which also ships Compression/DecompressionStream) without hiding node:zlib.
40
94
  export const streamZlib = {
41
95
  inflate: (b) => pipeThrough(b, new DecompressionStream("deflate")),
42
96
  deflate: (b) => pipeThrough(b, new CompressionStream("deflate")),
43
97
  gunzip: (b) => pipeThrough(b, new DecompressionStream("gzip")),
98
+ gunzipLax: (b) => {
99
+ const start = gzipBodyStart(b);
100
+ return start < 0 ? Promise.resolve(null) : streamInflateRawPartial(b.subarray(start, Math.max(start + 1, b.length - 8)));
101
+ },
44
102
  };