onionskin-cli 0.0.3 → 0.0.6
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/CHANGELOG.md +198 -0
- package/INSTALL.md +5 -1
- package/NOTICE +34 -0
- package/README.md +102 -34
- package/docs/contracts/batch.schema.json +27 -4
- package/docs/contracts/ops.schema.json +117 -5
- package/package.json +4 -2
- package/scripts/checker/check.py +24 -5
- package/scripts/checker/requirements.txt +482 -5
- package/scripts/requirements.txt +482 -5
- package/scripts/rewriter/apple/main.swift +1 -1
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +1 -1
- package/skills/onionskin/SKILL.md +12 -9
- package/skills/onionskin/reference/batch.md +5 -3
- package/skills/onionskin/reference/formats.md +5 -1
- package/skills/onionskin/reference/ops.md +48 -4
- package/skills/onionskin/reference/troubleshooting.md +31 -0
- package/src/agent-detect.js +28 -2
- package/src/agent-prompt.js +12 -1
- package/src/agent-runner.js +110 -20
- package/src/anchor-work.js +154 -0
- package/src/anchor-worker.js +38 -0
- package/src/anchors.js +61 -11
- package/src/apply.js +119 -47
- package/src/batch.js +32 -8
- package/src/check-worker.js +8 -5
- package/src/checker.js +238 -33
- package/src/cli.js +248 -42
- package/src/constants.js +34 -1
- package/src/diff.js +3 -1
- package/src/doctor.js +12 -3
- package/src/docx.js +464 -87
- package/src/edit-hash.js +19 -0
- package/src/fsutil.js +14 -6
- package/src/history-worker.js +8 -8
- package/src/history.js +144 -31
- package/src/html-active.js +384 -0
- package/src/html-envelope.js +51 -0
- package/src/html-parser.js +8 -0
- package/src/html-render-worker.js +25 -0
- package/src/html-render.js +939 -0
- package/src/html-utf8.js +65 -0
- package/src/html-write-worker.js +14 -0
- package/src/html-write.js +380 -0
- package/src/html.js +1259 -0
- package/src/local.js +68 -4
- package/src/merge-package.js +1166 -0
- package/src/merge-pptx.js +98 -38
- package/src/merge-proof.js +1019 -0
- package/src/merge-worker.js +60 -0
- package/src/merge-xlsx.js +84 -29
- package/src/merge.js +286 -83
- package/src/nfc.js +46 -0
- package/src/notes.js +122 -0
- package/src/offload.js +419 -1
- package/src/open-worker.js +27 -0
- package/src/ops/common.js +2 -1
- package/src/ops/docx-table.js +5 -3
- package/src/ops/docx.js +58 -29
- package/src/ops/html.js +394 -0
- package/src/ops/pptx.js +82 -23
- package/src/ops/run.js +23 -3
- package/src/ops/runs.js +215 -13
- package/src/ops/schema.js +8 -1
- package/src/ops/xlsx.js +121 -10
- package/src/ops-worker.js +17 -0
- package/src/package.js +43 -4
- package/src/person.js +2 -1
- package/src/pptx.js +323 -23
- package/src/question.js +10 -3
- package/src/quick-plan.js +13 -6
- package/src/quick.js +1 -1
- package/src/redline-check.js +12 -2
- package/src/redline-worker.js +10 -14
- package/src/reply.js +46 -19
- package/src/retain.js +223 -0
- package/src/review.js +32 -18
- package/src/rewrite-check.js +2 -2
- package/src/rewriter.js +38 -8
- package/src/sanitize.js +16 -3
- package/src/server-entry.js +4 -3
- package/src/server.js +175 -62
- package/src/session.js +1151 -210
- package/src/setup.js +20 -2
- package/src/stop-gate.js +34 -0
- package/src/suggest-reply.js +42 -7
- package/src/suggest.js +14 -29
- package/src/text.js +161 -11
- package/src/update.js +517 -0
- package/src/voice-guide.js +15 -9
- package/src/voice-learn.js +15 -4
- package/src/voice-sample-worker.js +7 -6
- package/src/voice-store.js +11 -4
- package/src/voice-worker.js +4 -6
- package/src/voice.js +2 -1
- package/src/worker-host.js +23 -0
- package/src/xlsx.js +378 -97
- package/src/xml-tree.js +6 -3
- package/src/xml.js +82 -34
- package/src/zip-write.js +10 -5
- package/src/zip.js +73 -18
- package/vendor/parse5/LICENSE.txt +32 -0
- package/vendor/parse5/parse5.mjs +7822 -0
- package/vendor/parse5.lock.json +23 -0
- package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
- package/web/dist/assets/index-DnbLtHhG.css +1 -0
- package/web/dist/index.html +2 -2
- package/web/dist/assets/index-BCAEKiOG.css +0 -1
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
// What is active in an HTML file (PLAN-html R1): the one table the render copy (src/html-render.js, later), the agent's ops and the check
|
|
4
|
+
// on every write use. It is an allowlist, not a list of attacks: an element or attribute is allowed only when it is named here, a URL only
|
|
5
|
+
// when its kind is named for its attribute, CSS only in the plain form below. Everything else is "active", whatever it is: present and
|
|
6
|
+
// unchanged it is left alone (the file had it), added or changed by a write it refuses the whole write (inventoryDelta).
|
|
7
|
+
// Pure: no parser here. The functions read a parse5 tree by its shape (nodeName, tagName, namespaceURI, attrs, childNodes, the content of
|
|
8
|
+
// a template, the value of a text node), so the same table serves every module that holds a tree.
|
|
9
|
+
|
|
10
|
+
export const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
11
|
+
|
|
12
|
+
/** The fixed sentence for a refused write (R1). */
|
|
13
|
+
export const ACTIVE_REFUSED = "The agent tried to add code that runs in a browser, so nothing was written.";
|
|
14
|
+
|
|
15
|
+
// Elements a write may add or change: document structure, text and its formatting, lists, tables, images, links, and the legacy
|
|
16
|
+
// formatting Office and email exports use (`font`, `center`, Word's `o:p`). Not here, so refused when added: script, noscript, template,
|
|
17
|
+
// style sheets by link, frames, objects, forms and their controls, media, canvas, SVG and MathML (another namespace), base, and every
|
|
18
|
+
// element this table does not name. `style` and `meta` are here with their own rules (CSS below; meta: no http-equiv).
|
|
19
|
+
const ELEMENTS = new Set(
|
|
20
|
+
[
|
|
21
|
+
"html", "head", "body", "title", "meta", "style",
|
|
22
|
+
"address", "article", "aside", "footer", "header", "h1", "h2", "h3", "h4", "h5", "h6", "hgroup", "main", "nav", "section", "search",
|
|
23
|
+
"blockquote", "dd", "div", "dl", "dt", "figcaption", "figure", "hr", "li", "menu", "ol", "p", "pre", "ul",
|
|
24
|
+
"a", "abbr", "b", "bdi", "bdo", "br", "cite", "code", "data", "dfn", "em", "i", "kbd", "mark", "q", "rp", "rt", "ruby", "s", "samp",
|
|
25
|
+
"small", "span", "strong", "sub", "sup", "time", "u", "var", "wbr", "del", "ins",
|
|
26
|
+
"img", "picture",
|
|
27
|
+
"caption", "col", "colgroup", "table", "tbody", "td", "tfoot", "th", "thead", "tr",
|
|
28
|
+
"details", "summary",
|
|
29
|
+
"center", "font", "big", "tt", "strike", "nobr", "o:p",
|
|
30
|
+
],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// Attributes allowed on every allowed element.
|
|
34
|
+
const GLOBAL_ATTRS = new Set(["class", "id", "title", "lang", "dir", "style", "hidden", "translate", "align", "valign", "width", "height", "bgcolor", "nowrap", "border"]);
|
|
35
|
+
// Attributes allowed on one element only (URL-bearing ones are listed in URL_ATTRS too).
|
|
36
|
+
const ELEMENT_ATTRS = {
|
|
37
|
+
a: ["href"],
|
|
38
|
+
img: ["src", "alt"],
|
|
39
|
+
td: ["colspan", "rowspan", "headers", "scope", "abbr"],
|
|
40
|
+
th: ["colspan", "rowspan", "headers", "scope", "abbr"],
|
|
41
|
+
col: ["span"],
|
|
42
|
+
colgroup: ["span"],
|
|
43
|
+
table: ["cellpadding", "cellspacing", "summary"],
|
|
44
|
+
ol: ["start", "reversed"],
|
|
45
|
+
li: ["value"],
|
|
46
|
+
time: ["datetime"],
|
|
47
|
+
del: ["datetime"],
|
|
48
|
+
ins: ["datetime"],
|
|
49
|
+
data: ["value"],
|
|
50
|
+
meta: ["charset", "name", "content"],
|
|
51
|
+
font: ["color", "face", "size"],
|
|
52
|
+
br: ["clear"],
|
|
53
|
+
details: ["open"],
|
|
54
|
+
html: ["xmlns"],
|
|
55
|
+
};
|
|
56
|
+
const PER_ELEMENT = new Map(Object.entries(ELEMENT_ATTRS).map(([k, v]) => [k, new Set(v)]));
|
|
57
|
+
|
|
58
|
+
/** The URL-bearing allowed attributes: "link" (followed when clicked) or "load" (fetched when shown), and the kinds each allows. */
|
|
59
|
+
const URL_ATTRS = { "a href": "link", "img src": "load" };
|
|
60
|
+
export const URL_KINDS = {
|
|
61
|
+
link: new Set(["fragment", "relative", "http", "https", "mailto", "tel"]),
|
|
62
|
+
load: new Set(["relative", "data-image"]),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** Template delimiters (R1): refused in added text or attribute values when the file loads any script. */
|
|
66
|
+
const TEMPLATE = /\{\{|\{%|<%|\$\{/;
|
|
67
|
+
|
|
68
|
+
/** Is an element of this name and namespace on the allowlist? Only HTML-namespace elements are. */
|
|
69
|
+
export function elementAllowed(name, namespace = HTML_NS) {
|
|
70
|
+
return namespace === HTML_NS && ELEMENTS.has(String(name));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Is attribute `name` allowed on allowed element `element`? Names compare as the parser reports them (lowercase). */
|
|
74
|
+
export function attributeAllowed(element, name) {
|
|
75
|
+
name = String(name);
|
|
76
|
+
return GLOBAL_ATTRS.has(name) || PER_ELEMENT.get(String(element))?.has(name) === true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// a raster image as a data: URL (no SVG in v1, R5): the media type, optional parameters, then the comma
|
|
80
|
+
const DATA_IMAGE = /^data:image\/(?:png|jpeg|jpg|gif|webp|avif|bmp|x-icon|vnd\.microsoft\.icon)(?:;[a-z0-9=._+-]*)*,/i;
|
|
81
|
+
const SCHEME = /^([a-zA-Z][a-zA-Z0-9+.-]*):/;
|
|
82
|
+
/**
|
|
83
|
+
* The kind of a URL as an attribute value holds it (character references already decoded by the parser): "fragment" ("" or "#..."),
|
|
84
|
+
* "relative" (a path with no scheme and no host), "data-image", "http", "https", "mailto", "tel", or "other". A value is "other" when a
|
|
85
|
+
* browser could read it as something else than it looks: any control character (browsers drop tabs and newlines anywhere in a URL), a
|
|
86
|
+
* backslash (read as a slash), a leading "//" (another host), or a scheme not named here.
|
|
87
|
+
*/
|
|
88
|
+
export function urlKind(value) {
|
|
89
|
+
// trimmed of HTML whitespace by index: a regex for the trailing run tried every start inside a run (quadratic in its length)
|
|
90
|
+
const raw = String(value ?? "");
|
|
91
|
+
const htmlSpace = (c) => c === 32 || c === 9 || c === 10 || c === 12 || c === 13;
|
|
92
|
+
let a = 0;
|
|
93
|
+
let b = raw.length;
|
|
94
|
+
while (a < b && htmlSpace(raw.charCodeAt(a))) a++;
|
|
95
|
+
while (b > a && htmlSpace(raw.charCodeAt(b - 1))) b--;
|
|
96
|
+
const v = raw.slice(a, b);
|
|
97
|
+
if (/[\u0000-\u001f\u007f\\]/.test(v)) return "other";
|
|
98
|
+
if (v === "" || v.startsWith("#")) return "fragment";
|
|
99
|
+
if (v.startsWith("//")) return "other";
|
|
100
|
+
const m = SCHEME.exec(v);
|
|
101
|
+
if (!m) return "relative";
|
|
102
|
+
const s = m[1].toLowerCase();
|
|
103
|
+
if (s === "http" || s === "https" || s === "mailto" || s === "tel") return s;
|
|
104
|
+
if (s === "data") return DATA_IMAGE.test(v) ? "data-image" : "other";
|
|
105
|
+
return "other";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The CSS rule (R1): CSS in a style attribute or a style element is plain when it has no backslash (escapes could spell any name), no
|
|
110
|
+
* @import, no image-set() or src() (they load without url()), no expression(), behavior or -moz-binding, and every url() is relative or a
|
|
111
|
+
* data: image. Returns {ok, loads: [url]}.
|
|
112
|
+
*/
|
|
113
|
+
export function cssPlain(css) {
|
|
114
|
+
const s = String(css ?? "");
|
|
115
|
+
const loads = [];
|
|
116
|
+
if (/\\|@import|image-set\s*\(|\bsrc\s*\(|expression\s*\(|behavior\s*:|-moz-binding/i.test(s)) return { ok: false, loads };
|
|
117
|
+
// url( ... ) read by index, as the regex /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)"'\s]*))\s*\)/gi read it (that regex backtracked over every
|
|
118
|
+
// split of a whitespace run between its two \s*: quadratic in the run). What is left must hold no url( this cannot read: not plain.
|
|
119
|
+
// JavaScript's \s, as the regex read it: ASCII by code, anything else by the regex (rare, so cheap)
|
|
120
|
+
const space = (c) => {
|
|
121
|
+
const k = c.charCodeAt(0);
|
|
122
|
+
return k < 128 ? k === 32 || (k >= 9 && k <= 13) : /\s/.test(c);
|
|
123
|
+
};
|
|
124
|
+
const isUrl = (i) => (s.charCodeAt(i) | 32) === 117 && (s.charCodeAt(i + 1) | 32) === 114 && (s.charCodeAt(i + 2) | 32) === 108; // u r l, any case
|
|
125
|
+
// where each position's next stop is (an unquoted value ends at ), a quote or whitespace; a quoted one at its quote), found once
|
|
126
|
+
// backwards, so a run of unclosed url( is not scanned again from each one
|
|
127
|
+
let stop = null;
|
|
128
|
+
const stops = () => {
|
|
129
|
+
if (stop) return stop;
|
|
130
|
+
const n = s.length;
|
|
131
|
+
stop = { any: new Int32Array(n + 1), dq: new Int32Array(n + 1), sq: new Int32Array(n + 1) };
|
|
132
|
+
stop.any[n] = stop.dq[n] = stop.sq[n] = n;
|
|
133
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
134
|
+
const c = s[i];
|
|
135
|
+
stop.any[i] = c === ")" || c === '"' || c === "'" || space(c) ? i : stop.any[i + 1];
|
|
136
|
+
stop.dq[i] = c === '"' ? i : stop.dq[i + 1];
|
|
137
|
+
stop.sq[i] = c === "'" ? i : stop.sq[i + 1];
|
|
138
|
+
}
|
|
139
|
+
return stop;
|
|
140
|
+
};
|
|
141
|
+
let rest = "";
|
|
142
|
+
let from = 0;
|
|
143
|
+
for (let i = 0; i + 3 < s.length; ) {
|
|
144
|
+
if (!(isUrl(i) && s[i + 3] === "(")) {
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
let j = i + 4;
|
|
149
|
+
while (j < s.length && space(s[j])) j++;
|
|
150
|
+
let val = null;
|
|
151
|
+
let k = j;
|
|
152
|
+
if (s[j] === '"' || s[j] === "'") {
|
|
153
|
+
const close = j + 1 <= s.length ? (s[j] === '"' ? stops().dq : stops().sq)[j + 1] : s.length;
|
|
154
|
+
if (close < s.length) [val, k] = [s.slice(j + 1, close), close + 1];
|
|
155
|
+
} else {
|
|
156
|
+
k = stops().any[j];
|
|
157
|
+
val = s.slice(j, k);
|
|
158
|
+
}
|
|
159
|
+
let m = k;
|
|
160
|
+
while (val !== null && m < s.length && space(s[m])) m++;
|
|
161
|
+
if (val === null || s[m] !== ")") {
|
|
162
|
+
i++;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
loads.push(val);
|
|
166
|
+
rest += s.slice(from, i);
|
|
167
|
+
from = i = m + 1;
|
|
168
|
+
}
|
|
169
|
+
rest += s.slice(from);
|
|
170
|
+
for (let i = 0; i + 3 < rest.length; i++) {
|
|
171
|
+
if (!((rest.charCodeAt(i) | 32) === 117 && (rest.charCodeAt(i + 1) | 32) === 114 && (rest.charCodeAt(i + 2) | 32) === 108)) continue;
|
|
172
|
+
let j = i + 3;
|
|
173
|
+
while (j < rest.length && space(rest[j])) j++;
|
|
174
|
+
if (rest[j] === "(") return { ok: false, loads }; // a url( this cannot read: not plain
|
|
175
|
+
}
|
|
176
|
+
return { ok: loads.every((u) => URL_KINDS.load.has(urlKind(u))), loads };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---- the display set (the render copy, src/html-render.js) --------------------------------------------------------------------------
|
|
180
|
+
// A second set, for SHOWING a page, not for writing one. Why it differs: the render copy is shown in a frame that runs no script, sends
|
|
181
|
+
// nothing and navigates nowhere, so it may show controls a write may never add (a page with a sign-up form should look like itself; the
|
|
182
|
+
// controls are shown disabled and never inside a `form`), and it shows less than a write may keep: no `meta` (the copy's one meta is the
|
|
183
|
+
// CSP the server adds first), no link target (the renderer keeps it as `data-osk-href` for display only) and no `src` (the renderer inlines
|
|
184
|
+
// a local or `data:` image itself, or shows a placeholder). The renderer, not this table, handles `href`, `src` and `style`.
|
|
185
|
+
const DISPLAY_EXTRA = ["input", "button", "select", "option", "optgroup", "textarea", "label", "fieldset", "legend", "output", "meter", "progress"];
|
|
186
|
+
const DISPLAY_ELEMENTS = new Set([...ELEMENTS, ...DISPLAY_EXTRA].filter((t) => t !== "meta"));
|
|
187
|
+
const DISPLAY_ATTRS = {
|
|
188
|
+
input: ["type", "value", "placeholder", "checked", "size", "readonly", "multiple", "min", "max", "step"],
|
|
189
|
+
button: ["value"],
|
|
190
|
+
select: ["multiple", "size"],
|
|
191
|
+
option: ["value", "selected", "label"],
|
|
192
|
+
optgroup: ["label"],
|
|
193
|
+
textarea: ["rows", "cols", "placeholder", "readonly", "wrap"],
|
|
194
|
+
meter: ["value", "min", "max", "low", "high", "optimum"],
|
|
195
|
+
progress: ["value", "max"],
|
|
196
|
+
style: ["media"],
|
|
197
|
+
};
|
|
198
|
+
const DISPLAY_PER_ELEMENT = new Map(Object.entries(DISPLAY_ATTRS).map(([k, v]) => [k, new Set(v)]));
|
|
199
|
+
/** The `type` values an input keeps in the render copy; any other (`image` loads its `src`) is not kept. */
|
|
200
|
+
export const DISPLAY_INPUT_TYPES = new Set(["text", "search", "email", "url", "tel", "number", "password", "date", "time", "datetime-local", "month", "week", "color", "range", "checkbox", "radio", "submit", "reset", "button", "file", "hidden"]);
|
|
201
|
+
/** Form controls: the render copy shows each one `disabled`. */
|
|
202
|
+
export const DISPLAY_CONTROLS = new Set(["input", "button", "select", "textarea", "fieldset"]);
|
|
203
|
+
|
|
204
|
+
/** May the render copy show an element of this name and namespace? HTML namespace only, as for writes. */
|
|
205
|
+
export function displayElementAllowed(name, namespace = HTML_NS) {
|
|
206
|
+
return namespace === HTML_NS && DISPLAY_ELEMENTS.has(String(name));
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** May the render copy keep attribute `name` on element `element`? Never `href`, `src`, `style` or `name` (the renderer's own rules). */
|
|
210
|
+
export function displayAttributeAllowed(element, name) {
|
|
211
|
+
name = String(name);
|
|
212
|
+
if (name === "href" || name === "src" || name === "style" || name === "name" || element === "meta") return false;
|
|
213
|
+
return attributeAllowed(element, name) || DISPLAY_PER_ELEMENT.get(String(element))?.has(name) === true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---- the inventory --------------------------------------------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
const kidsOf = (n) => (n.nodeName === "template" && n.content ? n.content.childNodes : n.childNodes || []);
|
|
219
|
+
const KEEP = 2048; // a value longer than this is kept by its hash (an inventory never holds a whole script or a megabyte attribute)
|
|
220
|
+
const sha = (s) => crypto.createHash("sha256").update(String(s)).digest("hex").slice(0, 32);
|
|
221
|
+
const keep = (s) => (s.length <= KEEP ? s : `#${sha(s)}`);
|
|
222
|
+
// an entry's key: its fields as a JSON array (one string per entry, never split: a field may hold any character)
|
|
223
|
+
const keyOf = (...fields) => JSON.stringify(fields);
|
|
224
|
+
const FRAMES = new Set(["iframe", "frame", "frameset", "object", "embed", "applet", "portal", "fencedframe"]);
|
|
225
|
+
const FORMS = new Set(["form", "input", "button", "select", "textarea", "option", "optgroup", "datalist", "fieldset", "legend", "label", "output", "isindex", "keygen"]);
|
|
226
|
+
const RAW = new Set(["script", "style", "xmp", "iframe", "noembed", "noframes", "plaintext", "noscript"]);
|
|
227
|
+
|
|
228
|
+
/** The reason code of a disallowed element. */
|
|
229
|
+
const elementCode = (tag, ns) => (tag === "script" ? "script" : ns !== HTML_NS ? "element" : FRAMES.has(tag) ? "frame" : FORMS.has(tag) ? "form" : "element");
|
|
230
|
+
|
|
231
|
+
// The place of an element for the positional part of the inventory: a hash of the tag names from the inventory's root down to it (no
|
|
232
|
+
// indexes, so elements inserted or deleted before it do not move it), two 32-bit lanes.
|
|
233
|
+
const placeOf = (parent, tag) => {
|
|
234
|
+
let a = parent[0] ^ 0x811c9dc5;
|
|
235
|
+
let b = parent[1] ^ 0x01000193;
|
|
236
|
+
for (let i = 0; i < tag.length; i++) {
|
|
237
|
+
a = Math.imul(a ^ tag.charCodeAt(i), 0x01000193);
|
|
238
|
+
b = Math.imul(b ^ tag.charCodeAt(i), 0x5bd1e995);
|
|
239
|
+
}
|
|
240
|
+
a = Math.imul(a ^ (a >>> 15), 0x2c1b3c6d) ^ 0x3e;
|
|
241
|
+
b = Math.imul(b ^ (b >>> 13), 0x297a2d39) ^ 0x3e;
|
|
242
|
+
return [a | 0, b | 0];
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* A summary of what is active in a parsed document (a parse5 tree), in two parts:
|
|
247
|
+
* - entries: canonical and order-independent: every element outside the allowlist (with all its attributes and, for script and other
|
|
248
|
+
* raw-text elements, its body), every attribute outside the allowlist, every link target and every load target (a URL attribute or a
|
|
249
|
+
* CSS url()), CSS that is not plain, and (when the file holds any script) every text or attribute value with a template delimiter.
|
|
250
|
+
* [{key, kind, code?, url?, urlKind?, n}] sorted by key: what was added or removed, and the links History lists.
|
|
251
|
+
* - seq: positional: every one of those entries that is not allowed (a `code`, templates aside) in document order, each with the
|
|
252
|
+
* element that owns it: the place of the owner (the tag names from the root down to it, placeOf), its tag and its `id`. Two
|
|
253
|
+
* inventories whose seq differ other than by items removed moved, swapped, copied or changed something active (outside review
|
|
254
|
+
* 029e51b, html-active.js:282: two `onclick` values swapped between elements kept the same entries).
|
|
255
|
+
* Returns {scripted, entries, seq}. Iterative: any depth. Values over 2,048 characters are kept by hash.
|
|
256
|
+
*/
|
|
257
|
+
export function inventory(tree) {
|
|
258
|
+
const counts = new Map(); // key -> {kind, code, url, urlKind, n}
|
|
259
|
+
const seq = [];
|
|
260
|
+
let own = null; // the active items of the element being read, sorted into seq after it
|
|
261
|
+
const add = (key, rec) => {
|
|
262
|
+
const e = counts.get(key);
|
|
263
|
+
if (e) e.n++;
|
|
264
|
+
else counts.set(key, { key, ...rec, n: 1 });
|
|
265
|
+
if (rec.code && rec.kind !== "template" && own) own.push(key);
|
|
266
|
+
};
|
|
267
|
+
const tpl = []; // [where, value] of values with a template delimiter, kept when the file turns out to hold a script
|
|
268
|
+
let scripted = false;
|
|
269
|
+
const ROOT = [0, 0];
|
|
270
|
+
const stack = [tree];
|
|
271
|
+
const places = [ROOT];
|
|
272
|
+
while (stack.length) {
|
|
273
|
+
const n = stack.pop();
|
|
274
|
+
const up = places.pop();
|
|
275
|
+
const ks = kidsOf(n);
|
|
276
|
+
const here = n.tagName === undefined ? up : placeOf(up, `${n.namespaceURI === HTML_NS ? "" : `${n.namespaceURI}|`}${n.tagName}`);
|
|
277
|
+
for (let i = ks.length - 1; i >= 0; i--) (stack.push(ks[i]), places.push(here));
|
|
278
|
+
if (n.nodeName === "#text") {
|
|
279
|
+
const p = n.parentNode;
|
|
280
|
+
const raw = p && ((p.namespaceURI === HTML_NS && RAW.has(p.tagName)) || p.tagName === "script" || p.tagName === "style");
|
|
281
|
+
if (!raw && TEMPLATE.test(n.value)) tpl.push(["text", n.value]);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (n.tagName === undefined) continue;
|
|
285
|
+
const tag = n.tagName;
|
|
286
|
+
const ns = n.namespaceURI;
|
|
287
|
+
const attrs = n.attrs || [];
|
|
288
|
+
own = [];
|
|
289
|
+
// owner and key as separate fields (an id can hold any character, so no joined string is split later: outside review of 4d2b2fc)
|
|
290
|
+
const owner = keyOf(`${here[0] >>> 0}.${here[1] >>> 0}`, tag, attrs.find((a) => a.name === "id" && !a.prefix)?.value ?? "");
|
|
291
|
+
const flush = () => {
|
|
292
|
+
for (const key of own.sort()) seq.push({ owner, key });
|
|
293
|
+
own = null;
|
|
294
|
+
};
|
|
295
|
+
if (tag === "script") scripted = true; // an SVG script runs too
|
|
296
|
+
if (!elementAllowed(tag, ns)) {
|
|
297
|
+
// its own text (a script or style body, in HTML or SVG): a changed body is a changed entry
|
|
298
|
+
const body = kidsOf(n).filter((c) => c.nodeName === "#text").map((c) => c.value).join("");
|
|
299
|
+
const sig = attrs.map((a) => [`${a.prefix ? `${a.prefix}:` : ""}${a.name}`, keep(a.value)]).sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : x[1] < y[1] ? -1 : x[1] > y[1] ? 1 : 0));
|
|
300
|
+
add(keyOf("el", ns === HTML_NS ? "" : ns, tag, sig, body ? sha(body) : ""), { kind: "element", code: elementCode(tag, ns) });
|
|
301
|
+
for (const a of attrs) if (TEMPLATE.test(a.value)) tpl.push(["attr", a.value]);
|
|
302
|
+
flush();
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
for (const a of attrs) {
|
|
306
|
+
const name = a.name; // an allowed element is an HTML one: its attributes have no namespace prefix
|
|
307
|
+
if (TEMPLATE.test(a.value)) tpl.push(["attr", a.value]);
|
|
308
|
+
const urlRole = URL_ATTRS[`${tag} ${name}`];
|
|
309
|
+
if (urlRole) {
|
|
310
|
+
const k = urlKind(a.value);
|
|
311
|
+
add(keyOf("url", urlRole, tag, name, keep(a.value)), { kind: urlRole, url: keep(a.value), urlKind: k, code: URL_KINDS[urlRole].has(k) ? undefined : `${urlRole}_url` });
|
|
312
|
+
} else if (name === "style") {
|
|
313
|
+
const c = cssPlain(a.value);
|
|
314
|
+
if (!c.ok) add(keyOf("css", "attr", sha(a.value)), { kind: "css", code: "css" });
|
|
315
|
+
for (const u of c.loads) add(keyOf("url", "load", "css", "url", keep(u)), { kind: "load", url: keep(u), urlKind: urlKind(u), code: URL_KINDS.load.has(urlKind(u)) ? undefined : "load_url" });
|
|
316
|
+
} else if (!attributeAllowed(tag, name)) {
|
|
317
|
+
add(keyOf("attr", tag, name, keep(a.value)), { kind: "attribute", code: /^on/i.test(name) ? "event_handler" : "attribute" });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (tag === "style" && ns === HTML_NS) {
|
|
321
|
+
const body = kidsOf(n).map((c) => c.value ?? "").join("");
|
|
322
|
+
const c = cssPlain(body);
|
|
323
|
+
if (!c.ok) add(keyOf("css", "style", sha(body)), { kind: "css", code: "css" });
|
|
324
|
+
for (const u of c.loads) add(keyOf("url", "load", "css", "url", keep(u)), { kind: "load", url: keep(u), urlKind: urlKind(u), code: URL_KINDS.load.has(urlKind(u)) ? undefined : "load_url" });
|
|
325
|
+
}
|
|
326
|
+
flush();
|
|
327
|
+
}
|
|
328
|
+
for (const [where, v] of tpl) add(keyOf("tpl", where, sha(v)), { kind: "template", code: "template" });
|
|
329
|
+
const entries = [...counts.values()].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)).map((e) => Object.fromEntries(Object.entries(e).filter(([, v]) => v !== undefined)));
|
|
330
|
+
return { scripted, entries, seq };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Reason codes, in the order a refusal names the first one found. Neutral words only: never any of the file's content. */
|
|
334
|
+
export const REASONS = ["script", "event_handler", "frame", "form", "element", "attribute", "link_url", "load_url", "css", "template"];
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* What a write added or changed, from the inventories before and after: `added` and `removed` entries (a count that grew or shrank is
|
|
338
|
+
* that many), `links` ({added, removed} link and load targets, for History), and `refused` with `reason` (one REASONS code) and `reasons`
|
|
339
|
+
* when anything added is active, or when the active items after are not the ones before in the same order on the same owners less some
|
|
340
|
+
* removed (seq: a move, a swap or a copy between elements). A changed target is a removed entry and an added one: refused only when the
|
|
341
|
+
* new one's kind is not allowed for its attribute (R1), listed in `links` either way. Text with a template delimiter counts only when
|
|
342
|
+
* either version holds a script.
|
|
343
|
+
*/
|
|
344
|
+
export function inventoryDelta(before, after) {
|
|
345
|
+
const count = (inv) => new Map(inv.entries.map((e) => [e.key, e]));
|
|
346
|
+
const A = count(before);
|
|
347
|
+
const B = count(after);
|
|
348
|
+
const added = [];
|
|
349
|
+
const removed = [];
|
|
350
|
+
for (const [k, e] of B) {
|
|
351
|
+
const d = e.n - (A.get(k)?.n || 0);
|
|
352
|
+
if (d > 0) added.push({ ...e, n: d });
|
|
353
|
+
}
|
|
354
|
+
for (const [k, e] of A) {
|
|
355
|
+
const d = e.n - (B.get(k)?.n || 0);
|
|
356
|
+
if (d > 0) removed.push({ ...e, n: d });
|
|
357
|
+
}
|
|
358
|
+
const scripted = before.scripted || after.scripted;
|
|
359
|
+
const codes = new Set(added.filter((e) => e.code && (e.kind !== "template" || scripted)).map((e) => e.code));
|
|
360
|
+
// positional: the active items after must be the ones before, in order, less some removed (a subsequence); each one not found names
|
|
361
|
+
// its entry's code. Ceiling: an owner is told by its place, tag and id, so an item moved to another element of the same tag, place and
|
|
362
|
+
// id with no other active item between is not seen (the same code runs, on a neighbor). Wrapping an owner in a new element is a move.
|
|
363
|
+
const codeOf = new Map([...A, ...B].map(([k, e]) => [k, e.code]));
|
|
364
|
+
const bs = after.seq || [];
|
|
365
|
+
const as = before.seq || [];
|
|
366
|
+
for (let i = 0, j = 0; j < bs.length; j++) {
|
|
367
|
+
while (i < as.length && (as[i].owner !== bs[j].owner || as[i].key !== bs[j].key)) i++;
|
|
368
|
+
if (i < as.length) i++;
|
|
369
|
+
else {
|
|
370
|
+
const c = codeOf.get(bs[j].key);
|
|
371
|
+
if (c) codes.add(c);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
const reasons = REASONS.filter((r) => codes.has(r));
|
|
375
|
+
const isUrl = (e) => e.kind === "link" || e.kind === "load";
|
|
376
|
+
return {
|
|
377
|
+
refused: reasons.length > 0,
|
|
378
|
+
reason: reasons[0] ?? null,
|
|
379
|
+
reasons,
|
|
380
|
+
added,
|
|
381
|
+
removed,
|
|
382
|
+
links: { added: added.filter(isUrl).map((e) => ({ kind: e.kind, url: e.url, urlKind: e.urlKind })), removed: removed.filter(isUrl).map((e) => ({ kind: e.kind, url: e.url, urlKind: e.urlKind })) },
|
|
383
|
+
};
|
|
384
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The answer of GET /api/html-view (docs/contracts/routes.md): one binary body the page reads without parsing a large JSON. Pure, no
|
|
2
|
+
// imports: the server encodes it (src/server.js) and the page decodes it (web/src/HtmlHost.jsx), from this same file.
|
|
3
|
+
//
|
|
4
|
+
// Layout (little-endian):
|
|
5
|
+
// 0 4 bytes "OSKV"
|
|
6
|
+
// 4 u32 length of the header JSON in bytes (H)
|
|
7
|
+
// 8 H bytes header JSON (UTF-8): {version, plain, reason, stats, texts: n, editable: m, ident: k}
|
|
8
|
+
// padding to a multiple of 4
|
|
9
|
+
// n x i32 texts: the copy's n-th text node is source text node texts[n], or -1 (src/html-render.js)
|
|
10
|
+
// m x i32 editable: the source text nodes the person may type in (src/html.js editableTextIds)
|
|
11
|
+
// k x i32 ident: their identities, 6 per editable node in the same order (src/html.js editIdentities), or none (k = 0)
|
|
12
|
+
// rest the copy (UTF-8): set once as the frame's srcdoc
|
|
13
|
+
// `texts` and `editable` hold one entry per text node (200,000 at the limits), so they travel as Int32Arrays: the page takes views on the
|
|
14
|
+
// one ArrayBuffer it received, nothing is copied or parsed.
|
|
15
|
+
|
|
16
|
+
const MAGIC = [0x4f, 0x53, 0x4b, 0x56]; // "OSKV"
|
|
17
|
+
|
|
18
|
+
/** The parts of the body: [prefix (magic, header, padding, both arrays), copy]. The server writes them in turn (no 40 MB concatenation). */
|
|
19
|
+
export function encodeHtmlView({ version, plain, reason, stats, texts, editable, ident = [], copy }) {
|
|
20
|
+
const head = new TextEncoder().encode(JSON.stringify({ version, plain: !!plain, reason: reason ?? null, stats: stats ?? null, texts: texts.length, editable: editable.length, ident: ident.length }));
|
|
21
|
+
const at = (8 + head.length + 3) & ~3;
|
|
22
|
+
const count = texts.length + editable.length + ident.length;
|
|
23
|
+
const prefix = new Uint8Array(at + 4 * count);
|
|
24
|
+
prefix.set(MAGIC, 0);
|
|
25
|
+
new DataView(prefix.buffer).setUint32(4, head.length, true);
|
|
26
|
+
prefix.set(head, 8);
|
|
27
|
+
const ints = new Int32Array(prefix.buffer, at, count);
|
|
28
|
+
ints.set(texts, 0);
|
|
29
|
+
ints.set(editable, texts.length);
|
|
30
|
+
ints.set(ident, texts.length + editable.length);
|
|
31
|
+
return [prefix, copy];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The page's side: {version, plain, reason, stats, texts, editable, ident (Int32Arrays), copy (Uint8Array)}; throws on a bad body. */
|
|
35
|
+
export function decodeHtmlView(buf) {
|
|
36
|
+
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
|
37
|
+
if (u8.length < 8 || MAGIC.some((b, i) => u8[i] !== b)) throw new Error("not an HTML view");
|
|
38
|
+
const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
39
|
+
const len = dv.getUint32(4, true);
|
|
40
|
+
if (8 + len > u8.length) throw new Error("a cut HTML view");
|
|
41
|
+
const head = JSON.parse(new TextDecoder().decode(u8.subarray(8, 8 + len)));
|
|
42
|
+
const at = (8 + len + 3) & ~3;
|
|
43
|
+
const n = head.texts;
|
|
44
|
+
const m = head.editable;
|
|
45
|
+
const k = head.ident ?? 0;
|
|
46
|
+
const end = at + 4 * (n + m + k);
|
|
47
|
+
if (!Number.isInteger(n) || !Number.isInteger(m) || !Number.isInteger(k) || end > u8.length) throw new Error("a cut HTML view");
|
|
48
|
+
// an Int32Array view needs an aligned offset in its buffer: copied only when the body's own offset is not (it is for a fetched body)
|
|
49
|
+
const ints = (u8.byteOffset + at) % 4 === 0 ? new Int32Array(u8.buffer, u8.byteOffset + at, n + m + k) : new Int32Array(u8.slice(at, end).buffer);
|
|
50
|
+
return { version: head.version, plain: head.plain, reason: head.reason, stats: head.stats, texts: ints.subarray(0, n), editable: ints.subarray(n, n + m), ident: ints.subarray(n + m), copy: u8.subarray(end) };
|
|
51
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The one module that imports the vendored HTML parser (vendor/parse5/parse5.mjs, pinned by vendor/parse5.lock.json) and installs it
|
|
2
|
+
// in src/html.js. Only modules that run in worker threads import this one (src/anchor-worker.js), so the server's own thread never
|
|
3
|
+
// parses a page: test/html-parser-thread.test.js fails if src/server.js or src/session.js reach it.
|
|
4
|
+
import * as P5 from "../vendor/parse5/parse5.mjs";
|
|
5
|
+
import { installParser } from "./html.js";
|
|
6
|
+
|
|
7
|
+
installParser(P5);
|
|
8
|
+
export { P5 };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import "./html-parser.js"; // this worker reads HTML files: the parser lives here, never on the server's thread
|
|
2
|
+
import { editableTextIds, editIdentities, readHtml } from "./html.js";
|
|
3
|
+
import { makeAssetReader, plainCopy, renderCopy } from "./html-render.js";
|
|
4
|
+
|
|
5
|
+
// The render copy of one version of an HTML file (src/offload.js renderOffThread, PLAN-html R12), run by src/worker-host.js within a
|
|
6
|
+
// time and memory limit: the file read again, the copy built (local images and stylesheets read here, src/html-render.js), and when the
|
|
7
|
+
// copy does not survive its own re-parse (R2), the plain text view instead. `job` = {bytes, fileDir}. Answers {ok: true, plain, reason,
|
|
8
|
+
// bytes, texts, stats, editable, ident} or {ok: false, error, code, copyable}. `bytes` is the copy (UTF-8), for the page to set as `srcdoc` once;
|
|
9
|
+
// `editable` the source text nodes the person may type in (src/html.js editableTextIds), `ident` their identities (editIdentities).
|
|
10
|
+
// `plain: true` in the job: the plain copy only (the page asks for it after its view stopped responding, R6).
|
|
11
|
+
export function work({ bytes, fileDir, plain = false }) {
|
|
12
|
+
try {
|
|
13
|
+
const read = readHtml(Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength));
|
|
14
|
+
let r = plain ? null : renderCopy(read, { fileDir, readAsset: makeAssetReader(), editable: true });
|
|
15
|
+
let reason = plain ? "asked" : null;
|
|
16
|
+
if (!r || r.plain) {
|
|
17
|
+
reason = r ? r.reason : reason;
|
|
18
|
+
r = plainCopy(read);
|
|
19
|
+
}
|
|
20
|
+
const editable = editableTextIds(read);
|
|
21
|
+
return { ok: true, plain: reason !== null, reason, bytes: new Uint8Array(r.bytes.buffer, r.bytes.byteOffset, r.bytes.byteLength), texts: r.texts, stats: r.stats, editable, ident: editIdentities(read, editable) };
|
|
22
|
+
} catch (err) {
|
|
23
|
+
return { ok: false, code: typeof err?.code === "string" ? err.code : null, copyable: err?.copyable === true, error: String(err?.message || err).slice(0, 200) };
|
|
24
|
+
}
|
|
25
|
+
}
|