decklight 0.1.0 → 0.3.0

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.
Files changed (139) hide show
  1. package/LICENSE +202 -21
  2. package/README.md +105 -183
  3. package/SPEC.md +861 -74
  4. package/cli/agents.mjs +206 -0
  5. package/cli/associate.mjs +279 -0
  6. package/cli/audit.mjs +424 -0
  7. package/cli/bundle.mjs +210 -45
  8. package/cli/deckfile.mjs +233 -0
  9. package/cli/decklight.mjs +216 -5
  10. package/cli/dev.mjs +455 -0
  11. package/cli/edit.mjs +733 -63
  12. package/cli/extension.mjs +108 -0
  13. package/cli/git.mjs +141 -0
  14. package/cli/import.mjs +398 -0
  15. package/cli/init.mjs +465 -121
  16. package/cli/loader.mjs +263 -0
  17. package/cli/marketplace.mjs +730 -0
  18. package/cli/pdf.mjs +156 -0
  19. package/cli/pkg.mjs +69 -0
  20. package/cli/plugin.mjs +646 -0
  21. package/cli/port-conflict.mjs +94 -0
  22. package/cli/present.mjs +560 -0
  23. package/cli/publish.mjs +342 -0
  24. package/cli/qr.mjs +334 -0
  25. package/cli/rec.mjs +12 -8
  26. package/cli/remote.mjs +149 -0
  27. package/cli/report-bug.mjs +97 -0
  28. package/cli/restore.mjs +174 -0
  29. package/cli/serve.mjs +247 -0
  30. package/cli/sign.mjs +216 -0
  31. package/cli/skill-content.mjs +222 -0
  32. package/cli/skills.mjs +338 -0
  33. package/cli/supervise.mjs +57 -0
  34. package/cli/theme.mjs +201 -0
  35. package/cli/units.mjs +488 -0
  36. package/cli/upgrade.mjs +250 -0
  37. package/cli/util.mjs +90 -0
  38. package/cli/wizard.mjs +432 -0
  39. package/cli/zip.mjs +121 -0
  40. package/dist/decklight.css +949 -72
  41. package/dist/decklight.js +220 -103
  42. package/dist/decklight.js.map +4 -4
  43. package/docs/architecture.svg +1 -1
  44. package/package.json +9 -6
  45. package/themes/README.md +6 -4
  46. package/themes/aurora.css +4 -1
  47. package/themes/berry.css +4 -1
  48. package/themes/carbon.css +4 -1
  49. package/themes/citrus.css +4 -1
  50. package/themes/coastal.css +4 -1
  51. package/themes/cosmos.css +4 -1
  52. package/themes/dune.css +4 -1
  53. package/themes/eclipse.css +4 -1
  54. package/themes/ember.css +4 -1
  55. package/themes/fjord.css +4 -1
  56. package/themes/gallery.html +3 -11
  57. package/themes/glacier.css +4 -1
  58. package/themes/graphite.css +4 -1
  59. package/themes/harvest.css +4 -1
  60. package/themes/ibm-modern.css +4 -1
  61. package/themes/ink.css +4 -1
  62. package/themes/latte.css +4 -1
  63. package/themes/linen.css +4 -1
  64. package/themes/meadow.css +4 -1
  65. package/themes/metropolis.css +4 -1
  66. package/themes/midnight.css +4 -1
  67. package/themes/mint.css +4 -1
  68. package/themes/moss.css +4 -1
  69. package/themes/obsidian.css +4 -1
  70. package/themes/orchid.css +4 -1
  71. package/themes/packs.json +4 -31
  72. package/themes/paper.css +4 -1
  73. package/themes/peony.css +4 -1
  74. package/themes/porcelain.css +4 -1
  75. package/themes/reveal-beige.css +4 -1
  76. package/themes/reveal-black.css +4 -1
  77. package/themes/reveal-blood.css +4 -1
  78. package/themes/reveal-dracula.css +4 -1
  79. package/themes/reveal-league.css +4 -1
  80. package/themes/reveal-moon.css +4 -1
  81. package/themes/reveal-night.css +4 -1
  82. package/themes/reveal-serif.css +4 -1
  83. package/themes/reveal-simple.css +4 -1
  84. package/themes/reveal-sky.css +4 -1
  85. package/themes/reveal-solarized.css +4 -1
  86. package/themes/reveal-white.css +4 -1
  87. package/themes/sepia.css +4 -1
  88. package/themes/seriph.css +4 -1
  89. package/themes/slate.css +4 -1
  90. package/themes/storm.css +4 -1
  91. package/themes/synthwave.css +4 -1
  92. package/themes/velvet.css +4 -1
  93. package/themes/yamabuki.css +120 -0
  94. package/tools/args.mjs +32 -0
  95. package/tools/bridge.mjs +30 -0
  96. package/tools/chrome.mjs +52 -0
  97. package/tools/color.mjs +110 -0
  98. package/tools/deck-html.mjs +138 -0
  99. package/tools/elevenlabs-tts.mjs +248 -0
  100. package/tools/escape.mjs +40 -0
  101. package/tools/extension-check-runner.mjs +41 -0
  102. package/tools/extension-check.mjs +266 -0
  103. package/tools/gemini-tts.mjs +44 -4
  104. package/tools/lipsync-engines.mjs +81 -0
  105. package/tools/lipsync-server.mjs +290 -0
  106. package/tools/lipsync.mjs +181 -0
  107. package/tools/local-voice.mjs +209 -0
  108. package/tools/ooxml.mjs +151 -0
  109. package/tools/pptx.mjs +320 -0
  110. package/tools/publish-site-voices.mjs +5 -1
  111. package/tools/publish-targets.mjs +143 -0
  112. package/tools/publish-voices.mjs +230 -0
  113. package/tools/shot.mjs +129 -0
  114. package/tools/theme-check.mjs +139 -0
  115. package/tools/tts-engines.mjs +455 -0
  116. package/tools/tts-setup.mjs +257 -0
  117. package/tools/veo.mjs +204 -0
  118. package/tools/video.mjs +316 -0
  119. package/tools/visemes.mjs +26 -0
  120. package/tools/voiceover-server.mjs +0 -0
  121. package/tools/voiceover.mjs +73 -39
  122. package/tools/zip.mjs +143 -0
  123. package/docs/demo.svg +0 -124
  124. package/themes/aliens.css +0 -84
  125. package/themes/apple2.css +0 -126
  126. package/themes/blade-runner.css +0 -83
  127. package/themes/c64.css +0 -85
  128. package/themes/friends.css +0 -82
  129. package/themes/gameboy.css +0 -118
  130. package/themes/genesis.css +0 -84
  131. package/themes/godfather.css +0 -82
  132. package/themes/ibm-oldschool.css +0 -84
  133. package/themes/miami-vice.css +0 -84
  134. package/themes/pulp-fiction.css +0 -84
  135. package/themes/severance.css +0 -82
  136. package/themes/snes.css +0 -82
  137. package/themes/star-wars.css +0 -85
  138. package/themes/stranger-things.css +0 -84
  139. package/themes/terminator.css +0 -84
package/cli/audit.mjs ADDED
@@ -0,0 +1,424 @@
1
+ // Copyright 2026 Gilles Philippart
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * The ingredients label — what a deck will execute, itemised.
6
+ *
7
+ * MARKETPLACE.md INTEGRITY calls this the canonical-shape audit, and its value
8
+ * comes from what a decklight deck *is*: content is HTML, themes are CSS,
9
+ * charts are JSON read by the runtime, casts are JSON read by the player. A
10
+ * well-formed deck therefore contains a very short list of executable things,
11
+ * and everything else that executes is worth naming.
12
+ *
13
+ * **An inventory, never a verdict** (PRESENT). There is no "safe", no green
14
+ * check, no score. The scan is a set of heuristics over a file an attacker may
15
+ * have edited; a verdict would manufacture confidence the mechanism cannot
16
+ * back, and the first person to trust it would be the one it fails. A list of
17
+ * what will run makes no such promise and is still the thing you needed.
18
+ *
19
+ * What it does catch is the realistic attack this whole wave exists for:
20
+ * someone appends a `<script>` to a bundled deck before forwarding it. That
21
+ * block matches nothing in the canonical shape, so it gets named, with its
22
+ * line and its opening bytes, before the first slide renders. The same holds
23
+ * for the vector that never needs a block at all: an inline `onerror=` handler
24
+ * or a `javascript:` href, which the CSP's unavoidable `'unsafe-inline'`
25
+ * would happily run — `executableAttributes` names those.
26
+ *
27
+ * The crypto path — a Sigstore signature over the whole file — is
28
+ * INTEGRITY#SIGNING and is separate on purpose: this audit needs no signature,
29
+ * no network, and no cooperation from whoever produced the file.
30
+ */
31
+
32
+ import { readFileSync, existsSync } from 'node:fs';
33
+ import { createHash } from 'node:crypto';
34
+ import path from 'node:path';
35
+ import { scripts, headStyles } from './upgrade.mjs';
36
+ import { PKG_ROOT, inlineRuntime } from './pkg.mjs';
37
+
38
+ const sha256 = (s) => createHash('sha256').update(s).digest('hex');
39
+
40
+ /**
41
+ * The deck's own claim about which runtime it carries.
42
+ *
43
+ * Read from the build banner, because minification renames the exported
44
+ * `version` const and the banner is the one place a tool can read it back out
45
+ * of an inlined bundle (build.mjs says so where it writes it). This is the
46
+ * deck's claim, not a verified fact — the hash below is what makes it checkable.
47
+ */
48
+ export function runtimeVersion(html) {
49
+ return /\/\*!\s*Decklight v(\d+\.\d+\.\d+[^\s*]*)/.exec(html)?.[1] ?? null;
50
+ }
51
+
52
+ /**
53
+ * The byte-exact inlined runtime the installed package would produce.
54
+ *
55
+ * `bundle`, `init`, `import` and `upgrade` all inline `dist/decklight.js`
56
+ * through `inlineRuntime` — the same function called here, rather than a
57
+ * reproduction of it, because a reproduction is what drifted: `import` once
58
+ * escaped it slightly differently and every imported deck audited as DIFFERS.
59
+ * They vary only in the whitespace around the payload, which is why the
60
+ * comparison trims.
61
+ */
62
+ export function installedRuntime(root = PKG_ROOT) {
63
+ const file = path.join(root, 'dist/decklight.js');
64
+ if (!existsSync(file)) return null;
65
+ const js = inlineRuntime(readFileSync(file, 'utf8'));
66
+ return { text: js, hash: sha256(js.trim()), version: runtimeVersion(js) };
67
+ }
68
+
69
+ /** 1-based line of a byte offset — the coordinate a person can act on. */
70
+ const lineAt = (html, index) => html.slice(0, index).split('\n').length;
71
+
72
+ /** First non-blank line of a block, clipped — enough to recognise it by. */
73
+ function snippet(inner, max = 88) {
74
+ const line = inner.split('\n').map((l) => l.trim()).find(Boolean) ?? '';
75
+ return line.length > max ? line.slice(0, max - 1) + '…' : line;
76
+ }
77
+
78
+ /**
79
+ * A `<script>` whose entire body is one `Decklight.init(…)` call — the boot
80
+ * line every deck has, written by `init` and preserved by `bundle`.
81
+ *
82
+ * Matched by SHAPE, not by "mentions Decklight.init": `Decklight.init();
83
+ * fetch('//evil')` mentions it too. Comments and an optional assignment are
84
+ * allowed because that is what the scaffolder writes (`const deck =
85
+ * Decklight.init({…})`); anything after the call's closing paren is not.
86
+ */
87
+ export function isBootCall(inner) {
88
+ const bare = inner
89
+ .replace(/\/\*[\s\S]*?\*\//g, ' ')
90
+ .replace(/(^|[^:])\/\/[^\n]*/g, '$1 ')
91
+ .trim();
92
+ const m = /^(?:(?:const|let|var)\s+[A-Za-z_$][\w$]*\s*=\s*)?Decklight\s*\.\s*init\s*\(/.exec(bare);
93
+ if (!m) return false;
94
+ // Walk to the matching paren; whatever follows may be at most a semicolon.
95
+ // A greedy regex to the LAST ")" cannot do this job — `Decklight.init();
96
+ // fetch("//evil")` starts and ends exactly right — and a walker that does
97
+ // not know about strings flags `{ alt: "Acme (Inc)" }`, which is a false
98
+ // positive on a deck that did nothing wrong. So: strings are skipped.
99
+ let depth = 0;
100
+ for (let i = m[0].length - 1; i < bare.length; i++) {
101
+ const c = bare[i];
102
+ if (c === '"' || c === "'" || c === '`') {
103
+ for (i++; i < bare.length && bare[i] !== c; i++) if (bare[i] === '\\') i++;
104
+ continue;
105
+ }
106
+ if (c === '(') depth++;
107
+ else if (c === ')' && --depth === 0) return /^\s*;?\s*$/.test(bare.slice(i + 1));
108
+ }
109
+ return false;
110
+ }
111
+
112
+ /** Does this block define the runtime? (upgrade's fallback locator, verbatim.) */
113
+ const definesRuntime = (s) => /(?:\bvar\s+|\bwindow\.)Decklight\s*=/.test(s.inner);
114
+
115
+ /**
116
+ * Sort every `<script>` in `html` into what it is.
117
+ *
118
+ * kinds: `runtime` (inlined engine) · `runtime-src` (the engine by reference,
119
+ * a source deck) · `data` (`type="application/json"` — casts, viseme sidecars,
120
+ * voice manifests) · `template` (`type="text/template"` — never executed by any
121
+ * browser) · `boot` (the `Decklight.init` call) · `external` (someone else's
122
+ * `src=`) · `unaccounted` (executes, matches nothing above).
123
+ */
124
+ export function classifyScripts(html) {
125
+ const all = scripts(html);
126
+ const marked = all.find((s) => /\bdata-decklight-runtime\b/i.test(s.attrs));
127
+ // Unmarked decks (bundle writes a bare <script>): the runtime is the block
128
+ // defining Decklight before the init call — upgrade's fallback, so the two
129
+ // commands always agree on which block is the engine.
130
+ let fallback = null;
131
+ if (!marked) {
132
+ const initAt = all.findIndex((s) =>
133
+ !/\bsrc\s*=/i.test(s.attrs) && /Decklight\.init\s*\(/.test(s.inner) && !definesRuntime(s));
134
+ for (let i = initAt - 1; i >= 0; i--) {
135
+ if (!/\bsrc\s*=/i.test(all[i].attrs) && definesRuntime(all[i])) { fallback = all[i]; break; }
136
+ }
137
+ }
138
+ const runtimeBlock = marked ?? fallback;
139
+
140
+ return all.map((s) => {
141
+ const type = /\btype\s*=\s*["']([^"']+)["']/i.exec(s.attrs)?.[1]?.toLowerCase() ?? '';
142
+ const src = /\bsrc\s*=\s*["']([^"']+)["']/i.exec(s.attrs)?.[1] ?? null;
143
+ // start/end are the block's byte range in `html` — carried so a caller can
144
+ // act on a block, not merely name it (PRESENT#STRICT splices on them).
145
+ const at = { line: lineAt(html, s.start), bytes: s.inner.length, start: s.start, end: s.end };
146
+ if (s === runtimeBlock) return { kind: 'runtime', ...at, inner: s.inner };
147
+ if (src) {
148
+ // a source deck loads dist/decklight.js by reference; anything else with
149
+ // a src is a third party and is named as one
150
+ return /(^|\/)decklight(\.min)?\.js(\?|#|$)/i.test(src)
151
+ ? { kind: 'runtime-src', ...at, src }
152
+ : { kind: 'external', ...at, src, snippet: src };
153
+ }
154
+ // Data, not findings. A JSON block is inert until the runtime reads it —
155
+ // no browser executes it — so casts, visemes and voice manifests are
156
+ // inventory lines, never suspects.
157
+ if (type === 'application/json') return { kind: 'data', ...at, subtype: dataSubtype(s.attrs) };
158
+ if (type === 'text/template') return { kind: 'template', ...at };
159
+ // A type the browser does not recognise as JS is not executed either, but
160
+ // it is unusual enough in a deck to be worth naming rather than filing.
161
+ if (type && !/^(text\/javascript|application\/javascript|module)$/.test(type)) {
162
+ return { kind: 'unaccounted', ...at, snippet: `type="${type}" — ${snippet(s.inner)}` };
163
+ }
164
+ if (isBootCall(s.inner)) return { kind: 'boot', ...at };
165
+ return { kind: 'unaccounted', ...at, snippet: snippet(s.inner) };
166
+ });
167
+ }
168
+
169
+ function dataSubtype(attrs) {
170
+ if (/\bdata-decklight-visemes\b/i.test(attrs)) return 'visemes';
171
+ if (/\bdata-decklight-voices\b/i.test(attrs)) return 'voice manifest';
172
+ return 'cast';
173
+ }
174
+
175
+ /** Attributes whose value a browser treats as a URL to follow or load. */
176
+ const URL_ATTRS = /^(href|src|action|formaction|xlink:href|data)$/i;
177
+
178
+ /**
179
+ * The value the way a browser would read it before deciding what it is:
180
+ * character references decoded, then the whitespace and control characters
181
+ * browsers ignore inside a scheme dropped. `jav&#x61;script:` and
182
+ * `java\tscript:` both resolve to `javascript:` in a browser, so they resolve
183
+ * to it here too — matching on spelling would be a scanner an attacker chooses
184
+ * the spelling for.
185
+ */
186
+ function resolvedValue(value) {
187
+ const cp = (n) => (n >= 0 && n <= 0x10ffff ? String.fromCodePoint(n) : '');
188
+ const named = { lt: '<', gt: '>', quot: '"', apos: "'", amp: '&', colon: ':', semi: ';', tab: '\t', newline: '\n' };
189
+ return value
190
+ .replace(/&#x([0-9a-f]+);?/gi, (_, h) => cp(parseInt(h, 16)))
191
+ .replace(/&#(\d+);?/g, (_, d) => cp(Number(d)))
192
+ .replace(/&([a-z]+);/gi, (_, n) => named[n.toLowerCase()] ?? `&${n};`)
193
+ .replace(/[\u0000-\u0020]/g, '')
194
+ .toLowerCase();
195
+ }
196
+
197
+ /** Blank a block's inner text, length- and line-preserving, like maskComments. */
198
+ const blankInner = (html, re) =>
199
+ html.replace(re, (_, open, inner, close) => open + inner.replace(/[^\n]/g, ' ') + close);
200
+
201
+ /**
202
+ * Executable ATTRIBUTES — what no `<script>` scan can see.
203
+ *
204
+ * `script-src 'unsafe-inline'` (which a bundled deck forces, cli/present.mjs)
205
+ * does not only permit script blocks: it is exactly what lets an inline
206
+ * `onerror=` handler or a `javascript:` href run too. A deck's canonical shape
207
+ * contains neither — content is markup, behaviour lives in the runtime — so
208
+ * every one found is named, the same way an unaccounted block is.
209
+ *
210
+ * Named, in the one scan the label and the stripper share (PRESENT#STRICT):
211
+ * - `on*` handler attributes carrying a value (`onerror=`, `onclick=`, …)
212
+ * - URL attributes (href, src, action, formaction, xlink:href, data) whose
213
+ * value resolves to `javascript:` or `data:text/html`
214
+ * - `srcdoc` — an inline document that inherits the deck's CSP, so script
215
+ * inside it runs under the same `'unsafe-inline'` that lets the deck boot
216
+ *
217
+ * Deliberately NOT named: a handler *mentioned* in prose, an HTML comment, an
218
+ * escaped code sample (`&lt;img onerror=…&gt;` never forms a tag), or the
219
+ * runtime's own strings. Everything that is not an attribute on a real tag is
220
+ * masked before the scan — a label that cried wolf on a slide ABOUT XSS would
221
+ * be turned off within a day. Still a heuristic over markup, like everything
222
+ * here: an inventory, never a verdict.
223
+ */
224
+ export function executableAttributes(html) {
225
+ // Comments first (the same mask scripts() applies), then script and style
226
+ // text, so the tag walk below only ever reads markup. Every mask preserves
227
+ // length and line breaks, so offsets found here index the original file.
228
+ let masked = html.replace(/<!--[\s\S]*?-->/g, (m) => m.replace(/[^\n]/g, ' '));
229
+ masked = blankInner(masked, /(<script\b[^>]*>)([\s\S]*?)(<\/script>)/gi);
230
+ masked = blankInner(masked, /(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi);
231
+
232
+ const found = [];
233
+ const open = /<([a-zA-Z][^\s/>]*)/g;
234
+ let m;
235
+ while ((m = open.exec(masked))) {
236
+ // Walk to the tag's own `>` — quote-aware, because an attribute value may
237
+ // legitimately contain one (`data-chart` JSON does).
238
+ let i = m.index + m[0].length;
239
+ while (i < masked.length && masked[i] !== '>') {
240
+ const c = masked[i];
241
+ if (c === '"' || c === "'") {
242
+ i = masked.indexOf(c, i + 1);
243
+ if (i === -1) { i = masked.length; break; }
244
+ }
245
+ i++;
246
+ }
247
+ const attrsStart = m.index + m[0].length;
248
+ // Sequential, so a value is consumed by its own attribute's match and its
249
+ // contents are never re-read as attribute names — `alt="use onclick= here"`
250
+ // is one attribute, not two.
251
+ const attrRe = /([^\s"'<>/=]+)(\s*=\s*("([^"]*)"|'([^']*)'|[^\s>]*))?/g;
252
+ const attrsText = masked.slice(attrsStart, Math.min(i, masked.length));
253
+ let a;
254
+ while ((a = attrRe.exec(attrsText))) {
255
+ const name = a[1];
256
+ const value = a[4] ?? a[5] ?? a[3] ?? '';
257
+ const what =
258
+ /^on[a-z]+$/i.test(name) && a[2] !== undefined ? 'handler'
259
+ : URL_ATTRS.test(name) && /^(javascript:|data:text\/html)/.test(resolvedValue(value)) ? 'executable URL'
260
+ : /^srcdoc$/i.test(name) && value.trim() ? 'inline document'
261
+ : null;
262
+ if (!what) continue;
263
+ const start = attrsStart + a.index;
264
+ const end = start + a[0].length;
265
+ found.push({
266
+ kind: what, tag: m[1].toLowerCase(), attr: name.toLowerCase(),
267
+ line: lineAt(html, start), start, end, snippet: snippet(html.slice(start, end)),
268
+ });
269
+ }
270
+ open.lastIndex = i + 1;
271
+ }
272
+ return found;
273
+ }
274
+
275
+ /**
276
+ * The whole label, as data. `present` prints it; `--check` exits on it.
277
+ *
278
+ * `runtime.state` is deliberately three-valued. A version this install does not
279
+ * have cannot be hash-checked here at all, and saying so is the honest answer —
280
+ * see the note on hashes in the PR: this package can only vouch for the build it
281
+ * ships, and inventing hashes for versions it has never seen would be the exact
282
+ * manufactured confidence the inventory refuses elsewhere.
283
+ */
284
+ export function auditDeck(html, { installed = installedRuntime() } = {}) {
285
+ const blocks = classifyScripts(html);
286
+ const handlers = executableAttributes(html);
287
+ const rt = blocks.find((b) => b.kind === 'runtime');
288
+ const rtSrc = blocks.find((b) => b.kind === 'runtime-src');
289
+ const version = runtimeVersion(html);
290
+
291
+ let runtime;
292
+ if (rt) {
293
+ const hash = sha256(rt.inner.trim());
294
+ const state = !installed ? 'uncheckable'
295
+ : hash === installed.hash ? 'matches'
296
+ : version && installed.version && version !== installed.version ? 'other-version'
297
+ : 'differs';
298
+ runtime = { kind: 'inline', version, hash, state, installedVersion: installed?.version ?? null };
299
+ } else if (rtSrc) {
300
+ // A source deck's engine is a separate file this audit never sees. Naming
301
+ // that limit beats hashing the empty <script> tag and reporting a match.
302
+ runtime = { kind: 'external', version, src: rtSrc.src, state: 'not-inlined' };
303
+ } else {
304
+ runtime = { kind: 'missing', version, state: 'not-found' };
305
+ }
306
+
307
+ const count = (k) => blocks.filter((b) => b.kind === k).length;
308
+ return {
309
+ runtime,
310
+ themes: headStyles(html).filter((s) => /\bdata-theme\b/i.test(s.attrs)).length,
311
+ counts: {
312
+ runtime: count('runtime') + count('runtime-src'),
313
+ data: count('data'),
314
+ template: count('template'),
315
+ boot: count('boot'),
316
+ unaccounted: count('unaccounted') + count('external'),
317
+ handlers: handlers.length,
318
+ },
319
+ unaccounted: blocks.filter((b) => b.kind === 'unaccounted' || b.kind === 'external'),
320
+ handlers,
321
+ blocks,
322
+ };
323
+ }
324
+
325
+ /**
326
+ * The same inventory, with the unaccounted blocks taken out (PRESENT#STRICT).
327
+ *
328
+ * Pure: it takes HTML and returns HTML. Nothing here touches the filesystem,
329
+ * which is the whole design — `present` strips on the way OUT, so the deck you
330
+ * were sent is still the deck you were sent, byte for byte, and you can hand
331
+ * the same file to someone else or diff it against the original.
332
+ *
333
+ * What survives is exactly what the label calls accounted for: the runtime
334
+ * (inlined or by `src`), the `Decklight.init` boot call, JSON data blocks and
335
+ * templates. That set is not a convenience — it is what a deck needs to render
336
+ * itself. Builds, layouts, themes and `data-chart` JSON are markup and CSS and
337
+ * attributes, so they are never in the blast radius to begin with.
338
+ *
339
+ * What it removes is what `unaccounted` and `external` mean: a block that
340
+ * executes and matches nothing in the canonical shape. Each becomes a fixed
341
+ * comment of the same shape — fixed on purpose, since interpolating any of the
342
+ * removed bytes into the page is how a stripper hands the payload a second way
343
+ * in, and the terminal is where the details belong anyway (never the
344
+ * audience-facing page).
345
+ *
346
+ * Executable ATTRIBUTES go in the same pass, because the label names them
347
+ * (`executableAttributes` is the one scanner both share): an inline `onerror=`
348
+ * handler, a `javascript:` href, an inline `srcdoc` document. An attribute is
349
+ * spliced OUT of its tag with nothing in its place — a comment cannot live
350
+ * inside a tag, and the fixed-text rule holds either way: none of the removed
351
+ * bytes enter the page. Widening the label without this stripper (or the
352
+ * reverse) would let the printed inventory and the served bytes disagree,
353
+ * which is worse than a limit you can read — so they move together.
354
+ *
355
+ * The honest limit that remains: both are heuristics over markup. A parser
356
+ * trick this scan does not see is a parser trick strict serves, which is why
357
+ * the label is an inventory and never a verdict.
358
+ */
359
+ export function stripUnaccounted(html) {
360
+ const blocks = classifyScripts(html);
361
+ const strippedBlocks = blocks.filter((b) => b.kind === 'unaccounted' || b.kind === 'external');
362
+ // An attribute inside a block that is going away rides out with the block's
363
+ // own splice — splicing its range a second time would corrupt what survives.
364
+ const attrs = executableAttributes(html)
365
+ .filter((a) => !strippedBlocks.some((b) => a.start >= b.start && a.end <= b.end));
366
+ const splices = [
367
+ ...strippedBlocks.map((b) => ({
368
+ at: b, start: b.start, end: b.end,
369
+ insert: `<!-- decklight strict mode: script block removed (${b.bytes} B) -->`,
370
+ })),
371
+ ...attrs.map((a) => ({ at: a, start: a.start, end: a.end, insert: '' })),
372
+ ];
373
+ let out = html;
374
+ // Back to front: every splice changes the offsets after it, none before it.
375
+ for (const s of [...splices].sort((a, b) => b.start - a.start)) {
376
+ out = `${out.slice(0, s.start)}${s.insert}${out.slice(s.end)}`;
377
+ }
378
+ return { html: out, stripped: splices.sort((a, b) => a.start - b.start).map((s) => s.at) };
379
+ }
380
+
381
+ const RUNTIME_LINE = {
382
+ matches: (r) => `runtime ${r.version ?? '(unversioned)'} — sha256 ${r.hash.slice(0, 12)}, identical to this install`,
383
+ differs: (r) => `runtime ${r.version ?? '(unversioned)'} — sha256 ${r.hash.slice(0, 12)}, DIFFERS from this install's build of ${r.installedVersion}`,
384
+ 'other-version': (r) => `runtime ${r.version} — sha256 ${r.hash.slice(0, 12)}, not comparable here (this install ships ${r.installedVersion})`,
385
+ uncheckable: (r) => `runtime ${r.version ?? '(unversioned)'} — sha256 ${r.hash.slice(0, 12)}, no installed build to compare against`,
386
+ 'not-inlined': (r) => `runtime ${r.version ?? '(unknown version)'} — loaded from ${r.src}, not inlined, so not hashed here`,
387
+ 'not-found': () => 'no decklight runtime found in this file',
388
+ };
389
+
390
+ /** The printed label: plain lines, no verdict, caller decides where they go. */
391
+ export function formatLabel(report, { indent = ' ' } = {}) {
392
+ const { counts, unaccounted, handlers } = report;
393
+ const lines = [`${indent}ingredients — what this file will execute`];
394
+ lines.push(`${indent} ${RUNTIME_LINE[report.runtime.state](report.runtime)}`);
395
+
396
+ const inert = [];
397
+ if (counts.data) inert.push(`${counts.data} data block${counts.data === 1 ? '' : 's'}`);
398
+ if (report.themes) inert.push(`${report.themes} theme style${report.themes === 1 ? '' : 's'}`);
399
+ if (counts.template) inert.push(`${counts.template} template${counts.template === 1 ? '' : 's'}`);
400
+ if (counts.boot) inert.push(`${counts.boot} Decklight.init call`);
401
+ if (inert.length) lines.push(`${indent} ${inert.join(' · ')} — read by the runtime, not executed as script`);
402
+
403
+ if (!counts.unaccounted) {
404
+ lines.push(`${indent} 0 unaccounted script blocks`);
405
+ } else {
406
+ lines.push(`${indent} ${counts.unaccounted} unaccounted script block${counts.unaccounted === 1 ? '' : 's'} — this file runs code that is not the runtime:`);
407
+ for (const b of unaccounted) {
408
+ const size = b.src ? `src=${b.src}` : `${b.bytes} B`;
409
+ lines.push(`${indent} line ${String(b.line).padStart(5)} ${size.padEnd(10)} ${b.snippet}`);
410
+ }
411
+ }
412
+ // Attributes are the other executable surface `script-src 'unsafe-inline'`
413
+ // lets through, and the zero line is as load-bearing as the findings: it is
414
+ // what says this class of content was examined at all.
415
+ if (!counts.handlers) {
416
+ lines.push(`${indent} 0 inline handlers or executable URLs in attributes`);
417
+ } else {
418
+ lines.push(`${indent} ${counts.handlers} executable attribute${counts.handlers === 1 ? '' : 's'} — this file runs code outside any script block:`);
419
+ for (const h of handlers) {
420
+ lines.push(`${indent} line ${String(h.line).padStart(5)} ${`<${h.tag}>`.padEnd(10)} ${h.snippet}`);
421
+ }
422
+ }
423
+ return lines;
424
+ }