onionskin-cli 0.0.0-stage → 0.0.2

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 (117) hide show
  1. package/CHANGELOG.md +144 -0
  2. package/INSTALL.md +25 -0
  3. package/LICENSE +21 -0
  4. package/NOTICE +49 -0
  5. package/README.md +148 -2
  6. package/docs/contracts/batch.schema.json +839 -0
  7. package/docs/contracts/history.schema.json +126 -0
  8. package/docs/contracts/ops.schema.json +541 -0
  9. package/package.json +67 -4
  10. package/scripts/checker/check.py +411 -0
  11. package/scripts/checker/requirements.txt +5 -0
  12. package/scripts/install-check.mjs +9 -0
  13. package/scripts/requirements.txt +5 -0
  14. package/scripts/rewriter/apple/main.swift +135 -0
  15. package/scripts/rewriter/apple/onionskin-fm +0 -0
  16. package/scripts/rewriter/apple/onionskin-fm.json +6 -0
  17. package/skills/onionskin/SKILL.md +117 -0
  18. package/skills/onionskin/reference/batch.md +94 -0
  19. package/skills/onionskin/reference/formats.md +15 -0
  20. package/skills/onionskin/reference/ops.md +253 -0
  21. package/skills/onionskin/reference/troubleshooting.md +73 -0
  22. package/skills/onionskin/reference/voice-guide.md +86 -0
  23. package/skills/onionskin/voice.example.md +51 -0
  24. package/src/agent-detect.js +149 -0
  25. package/src/agent-events.js +159 -0
  26. package/src/agent-prompt.js +27 -0
  27. package/src/agent-runner.js +593 -0
  28. package/src/analyze.js +17 -0
  29. package/src/anchors.js +190 -0
  30. package/src/apply.js +387 -0
  31. package/src/auth.js +130 -0
  32. package/src/batch.js +322 -0
  33. package/src/check-worker.js +8 -0
  34. package/src/checker.js +293 -0
  35. package/src/cli.js +516 -0
  36. package/src/constants.js +20 -0
  37. package/src/diff.js +267 -0
  38. package/src/doctor.js +288 -0
  39. package/src/docx.js +438 -0
  40. package/src/fmt.js +30 -0
  41. package/src/fsutil.js +348 -0
  42. package/src/history-worker.js +16 -0
  43. package/src/history.js +576 -0
  44. package/src/lease.js +79 -0
  45. package/src/local.js +242 -0
  46. package/src/merge-pptx.js +198 -0
  47. package/src/merge-xlsx.js +453 -0
  48. package/src/merge.js +410 -0
  49. package/src/offload.js +34 -0
  50. package/src/ops/common.js +48 -0
  51. package/src/ops/docx-table.js +323 -0
  52. package/src/ops/docx.js +503 -0
  53. package/src/ops/engine.js +41 -0
  54. package/src/ops/pptx.js +336 -0
  55. package/src/ops/run.js +115 -0
  56. package/src/ops/runs.js +127 -0
  57. package/src/ops/schema.js +109 -0
  58. package/src/ops/xlsx.js +277 -0
  59. package/src/package.js +189 -0
  60. package/src/paths.js +64 -0
  61. package/src/person.js +66 -0
  62. package/src/pptx.js +194 -0
  63. package/src/question.js +47 -0
  64. package/src/quick-plan.js +107 -0
  65. package/src/quick.js +164 -0
  66. package/src/redline-check.js +251 -0
  67. package/src/redline-worker.js +29 -0
  68. package/src/redline.js +438 -0
  69. package/src/reply.js +410 -0
  70. package/src/review.js +274 -0
  71. package/src/rewrite-check.js +1014 -0
  72. package/src/rewriter-cleanup.js +105 -0
  73. package/src/rewriter-setup.js +104 -0
  74. package/src/rewriter.js +396 -0
  75. package/src/sanitize.js +42 -0
  76. package/src/sentence-span.js +25 -0
  77. package/src/server-entry.js +13 -0
  78. package/src/server.js +576 -0
  79. package/src/session.js +1912 -0
  80. package/src/settings.js +161 -0
  81. package/src/setup.js +199 -0
  82. package/src/stamp-words.js +10 -0
  83. package/src/stamps.js +59 -0
  84. package/src/style.js +110 -0
  85. package/src/suggest-reply.js +84 -0
  86. package/src/suggest.js +261 -0
  87. package/src/text.js +31 -0
  88. package/src/tracked-words.js +43 -0
  89. package/src/voice-guide.js +182 -0
  90. package/src/voice-learn.js +314 -0
  91. package/src/voice-sample-worker.js +95 -0
  92. package/src/voice-samples.js +329 -0
  93. package/src/voice-store.js +305 -0
  94. package/src/voice-worker.js +28 -0
  95. package/src/voice.js +297 -0
  96. package/src/xlsx.js +454 -0
  97. package/src/xml-tree.js +118 -0
  98. package/src/xml.js +127 -0
  99. package/src/zip-write.js +74 -0
  100. package/src/zip.js +88 -0
  101. package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
  102. package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
  103. package/vendor/engine/collab-BVynXMqc.js +2591 -0
  104. package/vendor/engine/collab.js +2 -0
  105. package/vendor/engine/core.js +4 -0
  106. package/vendor/engine/default-worker-DogNT09A.js +35625 -0
  107. package/vendor/engine/embed.js +107 -0
  108. package/vendor/engine/index.js +5 -0
  109. package/vendor/engine/office-engine.embed.js +184 -0
  110. package/vendor/engine/office-engine.worker.js +36030 -0
  111. package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
  112. package/vendor/engine-node/LICENSE +21 -0
  113. package/vendor/engine-node/engine-node.mjs +46618 -0
  114. package/vendor/engine.lock.json +26 -0
  115. package/web/dist/assets/index-BUtofdtr.js +197 -0
  116. package/web/dist/assets/index-Dfizlg0i.css +1 -0
  117. package/web/dist/index.html +14 -0
@@ -0,0 +1,109 @@
1
+ import fs from "node:fs";
2
+ import { fileURLToPath } from "node:url";
3
+
4
+ /**
5
+ * Strict validator for docs/contracts/ops.schema.json. Hand-rolled on purpose: the schema uses a small subset of JSON Schema
6
+ * (type, enum, const, required, properties, additionalProperties:false, items, min/max, pattern, minProperties, anyOf, oneOf, $ref),
7
+ * so a dependency would cost more than these ~90 lines. Unknown fields are always errors.
8
+ */
9
+ const OPS_SCHEMA_PATH = fileURLToPath(new URL("../../docs/contracts/ops.schema.json", import.meta.url));
10
+ const SCHEMA = JSON.parse(fs.readFileSync(OPS_SCHEMA_PATH, "utf8"));
11
+
12
+ const typeOf = (v) => (v === null ? "null" : Array.isArray(v) ? "array" : Number.isInteger(v) ? "integer" : typeof v);
13
+ const isType = (v, t) => (t === "number" ? typeof v === "number" && Number.isFinite(v) : t === "integer" ? Number.isInteger(v) : typeOf(v) === t);
14
+
15
+ function resolve(schema) {
16
+ if (schema.$ref) {
17
+ const name = schema.$ref.replace("#/$defs/", "");
18
+ if (!SCHEMA.$defs[name]) throw new Error(`schema bug: unknown $ref ${schema.$ref}`);
19
+ return SCHEMA.$defs[name];
20
+ }
21
+ return schema;
22
+ }
23
+
24
+ function check(value, schema, at, errors) {
25
+ schema = resolve(schema);
26
+ if ("const" in schema && value !== schema.const) return errors.push(`${at}: must be ${JSON.stringify(schema.const)}`);
27
+ if (schema.enum && !schema.enum.includes(value)) return errors.push(`${at}: must be one of ${schema.enum.map((x) => JSON.stringify(x)).join(", ")}`);
28
+ if (schema.type) {
29
+ const types = [].concat(schema.type);
30
+ if (!types.some((t) => isType(value, t))) return errors.push(`${at}: must be ${types.join(" or ")}, got ${typeOf(value)}`);
31
+ }
32
+ if (typeof value === "string") {
33
+ if (schema.maxLength != null && [...value].length > schema.maxLength) errors.push(`${at}: longer than ${schema.maxLength} characters`);
34
+ if (schema.minLength != null && value.length < schema.minLength) errors.push(`${at}: must not be empty`);
35
+ if (schema.pattern && !new RegExp(schema.pattern).test(value)) errors.push(`${at}: does not match ${schema.pattern}`);
36
+ }
37
+ if (typeof value === "number") {
38
+ if (schema.minimum != null && value < schema.minimum) errors.push(`${at}: must be >= ${schema.minimum}`);
39
+ if (schema.maximum != null && value > schema.maximum) errors.push(`${at}: must be <= ${schema.maximum}`);
40
+ }
41
+ if (Array.isArray(value)) {
42
+ if (schema.minItems != null && value.length < schema.minItems) errors.push(`${at}: needs at least ${schema.minItems} item(s)`);
43
+ if (schema.maxItems != null && value.length > schema.maxItems) errors.push(`${at}: at most ${schema.maxItems} items`);
44
+ if (schema.items) value.forEach((v, i) => check(v, schema.items, `${at}[${i}]`, errors));
45
+ }
46
+ if (value && typeof value === "object" && !Array.isArray(value)) {
47
+ const props = schema.properties || {};
48
+ for (const k of schema.required || []) if (!(k in value)) errors.push(`${at}: missing required field "${k}"`);
49
+ if (schema.minProperties != null && Object.keys(value).length < schema.minProperties) errors.push(`${at}: needs at least ${schema.minProperties} field(s)`);
50
+ for (const [k, v] of Object.entries(value)) {
51
+ if (k in props) check(v, props[k], `${at}.${k}`, errors);
52
+ else if (schema.additionalProperties === false) errors.push(`${at}: unknown field "${k}"`);
53
+ }
54
+ }
55
+ if (schema.anyOf && !schema.anyOf.some((s) => { const e = []; check(value, s, at, e); return !e.length; })) errors.push(`${at}: does not match any allowed shape`);
56
+ if (schema.oneOf) {
57
+ // discriminated by "op": validate against the one branch that names this op, so the message is about that op
58
+ const branch = value && typeof value === "object" ? schema.oneOf.find((s) => resolve(s).properties?.op?.const === value.op) : null;
59
+ if (branch) check(value, branch, at, errors);
60
+ else errors.push(`${at}: unknown op ${JSON.stringify(value && value.op)}`);
61
+ }
62
+ }
63
+
64
+ const DOC_OPS = new Set(["replace_text", "insert_paragraph_after", "insert_paragraph_before", "delete_paragraph", "set_paragraph_style", "format_text"]);
65
+
66
+ /** Rules JSON Schema cannot say without heavy machinery: exclusive field groups and what `expect` must contain. */
67
+ function semantic(op, at, errors) {
68
+ if (DOC_OPS.has(op.op)) {
69
+ const t = op.target || {};
70
+ const n = ["anchor_id", "para_id", "body_path", "text_hash"].filter((k) => k in t);
71
+ if (!n.length) errors.push(`${at}.target: give anchor_id, or para_id, or body_path with text_hash, or text_hash`);
72
+ if ("anchor_id" in t && n.length > 1) errors.push(`${at}.target: anchor_id cannot be combined with other locator fields`);
73
+ }
74
+ if (op.op === "set_cells" || op.op === "format_cells") {
75
+ const t = op.target || {};
76
+ if ("anchor_id" in t ? "sheet" in t || "range" in t : !("sheet" in t && "range" in t)) errors.push(`${at}.target: give anchor_id alone, or sheet with range`);
77
+ }
78
+ if (op.op === "set_shape_text" || op.op === "replace_text_in_shape") {
79
+ const t = op.target || {};
80
+ if ("anchor_id" in t ? "slide_id" in t || "shape_id" in t : !("slide_id" in t && "shape_id" in t)) errors.push(`${at}.target: give anchor_id alone, or slide_id with shape_id`);
81
+ }
82
+ if (op.op === "replace_text") {
83
+ const mode = "text" in op ? "text" : "find" in op ? "find" : null;
84
+ if ("text" in op && ("find" in op || "replace" in op || "occurrence" in op || "all" in op)) errors.push(`${at}: use either text, or find with replace`);
85
+ else if (mode === "find" && !("replace" in op)) errors.push(`${at}: find needs replace`);
86
+ else if (!mode) errors.push(`${at}: give text, or find with replace`);
87
+ if (op.all && op.occurrence) errors.push(`${at}: all and occurrence are exclusive`);
88
+ }
89
+ if (op.op === "replace_text_in_shape" && op.all && op.occurrence) errors.push(`${at}: all and occurrence are exclusive`);
90
+ if (op.op === "duplicate_slide") {
91
+ if ("to" in op !== "where" in op) errors.push(`${at}: give to and where together, or neither (the copy then follows its slide)`);
92
+ const ids = (op.shapes || []).map((s) => s.shape_id);
93
+ if (new Set(ids).size !== ids.length) errors.push(`${at}.shapes: each shape_id at most once`);
94
+ }
95
+ if (op.op === "format_all" || op.op === "replace_all_text") return;
96
+ const e = op.expect || {};
97
+ if (op.op === "add_sheet") {
98
+ if (e.absent !== true || "quote" in e || "text_hash" in e) errors.push(`${at}.expect: add_sheet needs exactly { "absent": true }`);
99
+ } else if ("absent" in e) errors.push(`${at}.expect: absent is only for add_sheet`);
100
+ else if (!("quote" in e) && !("text_hash" in e)) errors.push(`${at}.expect: give quote or text_hash (what the target looked like when you read it)`);
101
+ }
102
+
103
+ /** Returns {ok, errors:[...strings]}. Never throws on bad input. */
104
+ export function validateOps(doc) {
105
+ const errors = [];
106
+ check(doc, SCHEMA, "$", errors);
107
+ if (!errors.length) doc.ops.forEach((op, i) => semantic(op, `$.ops[${i}]`, errors));
108
+ return { ok: errors.length === 0, errors: errors.slice(0, 50) };
109
+ }
@@ -0,0 +1,277 @@
1
+ import { checkExpect, colIndex, colLetters, engineFailure, fail, parseA1, refused } from "./common.js";
2
+ import { openEngine } from "./engine.js";
3
+ import { findAll, partialWords } from "./runs.js";
4
+ import { a1Range, parseWorkbook } from "../xlsx.js";
5
+
6
+ /**
7
+ * XLSX driver (headless Engine: xlsx.set, xlsx.format, xlsx.sheetOp, xlsx.bookOp, xlsx.replace). Cell text for `expect` is what a
8
+ * user would see in the formula bar: the formula when the cell has one, else the stored value. The engine refuses (with a code)
9
+ * structural edits it cannot do losslessly; that code is surfaced as the op's `code`.
10
+ */
11
+
12
+ export async function open({ bytes, name, anchors = new Map() }) {
13
+ const h = await openEngine(bytes, name);
14
+ return { h, sheets: h.info.xlsx.sheets.map((s) => ({ ...s })), loaded: new Map(), name, anchors, bytes };
15
+ }
16
+
17
+ /** Merged ranges of a sheet as the file was delivered (no op merges or unmerges), read from the bytes on first use. */
18
+ function mergesOf(st, sheetName) {
19
+ st.wb ??= parseWorkbook(st.bytes);
20
+ const sheet = st.wb.sheets.find((s) => s.name === sheetName) ?? st.wb.sheets.find((s) => s.name.toLowerCase() === sheetName.toLowerCase());
21
+ return sheet ? st.wb.sheetCells(sheet).merges : [];
22
+ }
23
+
24
+ /** `{sheet, range}` as written, or `{anchor_id}` resolved through the delivered batch (the locator carries exactly those two fields). */
25
+ function rangeTarget(st, target) {
26
+ if (!("anchor_id" in target)) return { target };
27
+ const a = st.anchors.get(target.anchor_id);
28
+ if (!a) return { error: `anchor ${target.anchor_id} is not in the batch (pass --batch batch.json, or run inside the session that delivered it)` };
29
+ if (!a.locator?.sheet || !a.locator?.range) return { error: `anchor ${target.anchor_id} does not name a cell or range` };
30
+ return { target: { sheet: a.locator.sheet, range: a.locator.range } };
31
+ }
32
+
33
+ async function load(st, i) {
34
+ if (!st.loaded.has(i)) {
35
+ const done = st.h.waitFor((ev, d) => ev === "xlsx:progress" && d.sheet === i && d.done);
36
+ await st.h.engine.call("xlsx.load", [i]);
37
+ st.loaded.set(i, await done);
38
+ }
39
+ return st.loaded.get(i);
40
+ }
41
+
42
+ const sheetIndex = (st, name) => {
43
+ const exact = st.sheets.findIndex((s) => s.name === name);
44
+ if (exact >= 0) return exact;
45
+ const ci = st.sheets.filter((s) => s.name.toLowerCase() === name.toLowerCase());
46
+ return ci.length === 1 ? st.sheets.indexOf(ci[0]) : -1;
47
+ };
48
+
49
+ /** the text of one cell as typed ("" for an empty cell) */
50
+ async function cellText(st, sh, r, c) {
51
+ const d = await st.h.engine.call("xlsx.cell", [sh, r, c]);
52
+ if (!d) return "";
53
+ return d.formula ?? (d.value == null ? "" : String(d.value));
54
+ }
55
+
56
+ /** cells of a rectangle: columns joined by TAB, rows by LF (the shape `expect` uses) */
57
+ async function regionText(st, sh, { r0, c0, r1, c1 }) {
58
+ const rows = [];
59
+ for (let r = r0; r <= r1; r++) {
60
+ const cells = [];
61
+ for (let c = c0; c <= c1; c++) cells.push(await cellText(st, sh, r, c));
62
+ rows.push(cells.join("\t"));
63
+ }
64
+ return rows.join("\n");
65
+ }
66
+
67
+ /** non-empty cells' coordinates in rows [r0,r1] */
68
+ async function usedCells(st, sh, r0, r1) {
69
+ const out = [];
70
+ const b = await st.h.engine.call("xlsx.block", [sh, r0, r1 + 1]);
71
+ for (const row of b.rows) for (const c of row.cells) out.push({ r: row.r, c: c[0] });
72
+ return out;
73
+ }
74
+ /** a row as typed, from column A to its last non-empty cell */
75
+ async function rowText(st, sh, r) {
76
+ const used = (await usedCells(st, sh, r, r)).filter((x) => x.r === r);
77
+ const last = used.length ? Math.max(...used.map((x) => x.c)) : -1;
78
+ return regionText(st, sh, { r0: r, c0: 0, r1: r, c1: last }).then((t) => (last < 0 ? "" : t));
79
+ }
80
+ /** a column from row 1 to its last non-empty cell, cells joined by LF */
81
+ async function colText(st, sh, c) {
82
+ const { rows } = await load(st, sh);
83
+ const lines = [];
84
+ for (let r0 = 0; r0 < rows; r0 += 500) {
85
+ const b = await st.h.engine.call("xlsx.block", [sh, r0, Math.min(rows, r0 + 500)]);
86
+ for (const row of b.rows) if (row.cells.some((x) => x[0] === c)) lines[row.r] = await cellText(st, sh, row.r, c);
87
+ }
88
+ return Array.from({ length: lines.length }, (_, i) => lines[i] ?? "").join("\n");
89
+ }
90
+
91
+ const input = (v) => (v === null ? "" : typeof v === "boolean" ? (v ? "TRUE" : "FALSE") : String(v));
92
+
93
+ const STRUCTURAL = new Set(["insert_rows", "delete_rows", "insert_columns", "delete_columns"]);
94
+
95
+ /** Phase 1 (before any op ran): sheet and range exist, `expect` holds. Async because it reads cells through the engine. */
96
+ export async function bind(st, op) {
97
+ const b = bindShape(st, op);
98
+ if (!b.ok) return b;
99
+ // Addresses and `expect` are about the file as it is now. After rows or columns moved they would mean something else.
100
+ if (b.sh !== undefined && st.shifted?.has(b.sh)) return refused("after_structural_edit", "an earlier op of this file inserted or deleted rows/columns on this sheet; send this op in a separate apply call");
101
+ const bad = await checkRegion(st, b);
102
+ if (bad) return bad;
103
+ if (STRUCTURAL.has(op.op)) (st.shifted ??= new Set()).add(b.sh);
104
+ if (op.op === "replace_all_text") {
105
+ const hits = await allMatches(st, op);
106
+ const total = hits.reduce((n, x) => n + x.n, 0);
107
+ // `t` is the formula when a cell has one, so a hit whose text starts with "=" would rewrite a formula
108
+ const extra = { partial_words: hits.reduce((n, x) => n + partialWords(x.t, op.find, op.match_case !== false), 0), in_formulas: hits.reduce((n, x) => n + (x.t.startsWith("=") ? x.n : 0), 0) };
109
+ if (total !== op.expect.count) return fail("expect_mismatch", `found ${total} occurrence(s), expected ${op.expect.count}`, { found: total, ...extra });
110
+ Object.assign(b, extra);
111
+ b.hits = hits;
112
+ }
113
+ return b;
114
+ }
115
+
116
+ function bindShape(st, op) {
117
+ switch (op.op) {
118
+ case "set_cells":
119
+ case "format_cells": {
120
+ const rt = rangeTarget(st, op.target);
121
+ if (rt.error) return fail("anchor_not_found", rt.error);
122
+ const target = rt.target;
123
+ const sh = sheetIndex(st, target.sheet);
124
+ if (sh < 0) return fail("anchor_not_found", `no sheet named "${target.sheet}"`);
125
+ const rect = parseA1(target.range);
126
+ if (!rect) return fail("anchor_not_found", `bad range ${target.range}`);
127
+ if (op.op === "set_cells") {
128
+ const rows = rect.r1 - rect.r0 + 1;
129
+ const cols = rect.c1 - rect.c0 + 1;
130
+ const v = op.values;
131
+ if (!Array.isArray(v)) {
132
+ if (rows * cols !== 1) return refused("values_shape", `a scalar value fits one cell, ${target.range} has ${rows * cols}`);
133
+ } else if (v.length !== rows || v.some((r) => r.length !== cols)) return refused("values_shape", `values must be ${rows} row(s) of ${cols} cell(s) to fill ${target.range}`);
134
+ }
135
+ if (op.op === "set_cells") {
136
+ // a merged range shows only its top-left cell: a value in any other cell of it is hidden from the person (an empty write is harmless)
137
+ const flat = Array.isArray(op.values) ? op.values : [[op.values]];
138
+ for (const m of mergesOf(st, target.sheet)) {
139
+ const hit = flat.flatMap((row, i) => row.map((v, j) => ({ r: rect.r0 + i, c: rect.c0 + j, v }))).find((x) => x.r >= m.r0 && x.r <= m.r1 && x.c >= m.c0 && x.c <= m.c1 && !(x.r === m.r0 && x.c === m.c0) && input(x.v) !== "");
140
+ if (hit) return refused("merged_cell", `${colLetters(hit.c)}${hit.r + 1} is inside the merged range ${a1Range(m)}, which shows only its top-left cell ${colLetters(m.c0)}${m.r0 + 1}: write there, or pick a cell outside the range`);
141
+ }
142
+ }
143
+ return { ok: true, op, sh, rect, phase: "region" };
144
+ }
145
+ case "insert_rows":
146
+ case "delete_rows": {
147
+ const sh = sheetIndex(st, op.target.sheet);
148
+ if (sh < 0) return fail("anchor_not_found", `no sheet named "${op.target.sheet}"`);
149
+ return { ok: true, op, sh, at: op.target.at - 1, n: op.target.count || 1, phase: "rows" };
150
+ }
151
+ case "insert_columns":
152
+ case "delete_columns": {
153
+ const sh = sheetIndex(st, op.target.sheet);
154
+ if (sh < 0) return fail("anchor_not_found", `no sheet named "${op.target.sheet}"`);
155
+ return { ok: true, op, sh, at: colIndex(op.target.at), n: op.target.count || 1, phase: "cols" };
156
+ }
157
+ case "add_sheet":
158
+ if (sheetIndex(st, op.target.name) >= 0) return fail("expect_mismatch", `a sheet named "${op.target.name}" already exists`);
159
+ return { ok: true, op, phase: "book" };
160
+ case "rename_sheet": {
161
+ const sh = sheetIndex(st, op.target.sheet);
162
+ if (sh < 0) return fail("anchor_not_found", `no sheet named "${op.target.sheet}"`);
163
+ const bad = checkExpect(op.expect, st.sheets[sh].name, "exact");
164
+ if (bad) return bad;
165
+ if (sheetIndex(st, op.new_name) >= 0 && st.sheets[sheetIndex(st, op.new_name)].name.toLowerCase() !== st.sheets[sh].name.toLowerCase()) return refused("sheet_name_taken", `a sheet named "${op.new_name}" already exists`);
166
+ return { ok: true, op, sh, phase: "book" };
167
+ }
168
+ case "replace_all_text":
169
+ return { ok: true, op, phase: "all" };
170
+ default:
171
+ return refused("unsupported_op", `${op.op} does not apply to a .xlsx`);
172
+ }
173
+ }
174
+
175
+ /** expect, checked against the live state right before the edit */
176
+ async function checkRegion(st, b) {
177
+ const { op } = b;
178
+ let text;
179
+ if (b.sh !== undefined) await load(st, b.sh);
180
+ if (b.phase === "region") text = await regionText(st, b.sh, b.rect);
181
+ else if (b.phase === "rows") {
182
+ const parts = [];
183
+ for (let r = b.at; r < b.at + (op.op === "delete_rows" ? b.n : 1); r++) parts.push(await rowText(st, b.sh, r));
184
+ text = parts.join("\n");
185
+ } else if (b.phase === "cols") {
186
+ const parts = [];
187
+ for (let c = b.at; c < b.at + (op.op === "delete_columns" ? b.n : 1); c++) parts.push(await colText(st, b.sh, c));
188
+ text = parts.join("\n");
189
+ } else return null;
190
+ return checkExpect(op.expect, text, "exact");
191
+ }
192
+
193
+ async function allMatches(st, op) {
194
+ const out = [];
195
+ for (let sh = 0; sh < st.sheets.length; sh++) {
196
+ const { rows } = await load(st, sh);
197
+ for (let r0 = 0; r0 < rows; r0 += 500) {
198
+ const b = await st.h.engine.call("xlsx.block", [sh, r0, Math.min(rows, r0 + 500)]);
199
+ for (const row of b.rows)
200
+ for (const c of row.cells) {
201
+ const t = await cellText(st, sh, row.r, c[0]);
202
+ const n = findAll(t, op.find, op.match_case !== false).length;
203
+ if (n) out.push({ sh, r: row.r, c: c[0], n, t });
204
+ }
205
+ }
206
+ }
207
+ return out;
208
+ }
209
+
210
+ export async function exec(st, b) {
211
+ const { op } = b;
212
+ try {
213
+ if (b.sh !== undefined) await load(st, b.sh);
214
+ if (op.op === "replace_all_text") {
215
+ const total = b.hits.reduce((n, x) => n + x.n, 0);
216
+ const byFormula = [];
217
+ for (const x of b.hits) {
218
+ const needle = op.match_case !== false ? op.find : null;
219
+ const next = needle ? x.t.split(op.find).join(op.replace) : x.t.replace(new RegExp(op.find.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi"), () => op.replace);
220
+ byFormula.push({ sh: x.sh, r: x.r, c: x.c, input: next });
221
+ }
222
+ for (const sh of new Set(byFormula.map((x) => x.sh))) await st.h.engine.call("xlsx.set", [sh, byFormula.filter((x) => x.sh === sh).map(({ r, c, input }) => ({ r, c, input }))]);
223
+ if (byFormula.length !== b.hits.length) return { ...refused("incomplete_replace", "not every matching cell was rewritten"), dirty: true };
224
+ return { status: "applied", detail: { replaced: total, cells: byFormula.length } };
225
+ }
226
+ switch (op.op) {
227
+ case "set_cells": {
228
+ const flat = Array.isArray(op.values) ? op.values : [[op.values]];
229
+ const cells = [];
230
+ flat.forEach((row, i) => row.forEach((v, j) => cells.push({ r: b.rect.r0 + i, c: b.rect.c0 + j, input: input(v) })));
231
+ await st.h.engine.call("xlsx.set", [b.sh, cells]);
232
+ return { status: "applied", detail: { cells: cells.length } };
233
+ }
234
+ case "format_cells": {
235
+ const s = op.set;
236
+ const patch = {};
237
+ if ("number_format" in s) patch.numFmt = s.number_format;
238
+ if ("bold" in s) patch.b = s.bold;
239
+ if ("italic" in s) patch.i = s.italic;
240
+ if ("fill" in s) patch.fill = s.fill.toLowerCase();
241
+ if ("font_color" in s) patch.color = s.font_color.toLowerCase();
242
+ await st.h.engine.call("xlsx.format", [b.sh, b.rect, patch]);
243
+ return { status: "applied" };
244
+ }
245
+ case "insert_rows":
246
+ case "delete_rows":
247
+ case "insert_columns":
248
+ case "delete_columns": {
249
+ const kind = { insert_rows: "insertRows", delete_rows: "deleteRows", insert_columns: "insertCols", delete_columns: "deleteCols" }[op.op];
250
+ await st.h.engine.call("xlsx.sheetOp", [b.sh, { op: kind, at: b.at, n: b.n }]);
251
+ st.loaded.delete(b.sh); // the sheet's rows moved: later reads must not use cached sizes
252
+ return { status: "applied" };
253
+ }
254
+ case "add_sheet": {
255
+ const r = await st.h.engine.call("xlsx.bookOp", [{ op: "add", name: op.target.name, after: st.sheets.length - 1 }]);
256
+ if (r.sheets) st.sheets = r.sheets.map((s) => ({ ...s }));
257
+ if (sheetIndex(st, op.target.name) < 0) return { ...refused("sheet_not_added", "the engine did not add the sheet under that name"), dirty: true };
258
+ return { status: "applied" };
259
+ }
260
+ case "rename_sheet": {
261
+ const r = await st.h.engine.call("xlsx.bookOp", [{ op: "rename", index: b.sh, name: op.new_name }]);
262
+ if (r.sheets) st.sheets = r.sheets.map((s) => ({ ...s }));
263
+ return { status: "applied" };
264
+ }
265
+ }
266
+ } catch (err) {
267
+ return engineFailure(err);
268
+ }
269
+ return refused("unsupported_op", op.op);
270
+ }
271
+
272
+ export async function save(st) {
273
+ const blob = await st.h.engine.call("save", []);
274
+ return Buffer.from(await blob.arrayBuffer());
275
+ }
276
+
277
+ export const close = (st) => st.h.engine.dispose();
package/src/package.js ADDED
@@ -0,0 +1,189 @@
1
+ import path from "node:path";
2
+ import { FORMATS } from "./constants.js";
3
+ import { openZip, ZipError } from "./zip.js";
4
+ import { walkXml } from "./xml.js";
5
+ import { runDeepCheck } from "./checker.js";
6
+ import { cleanFlag } from "./sanitize.js";
7
+
8
+ const MAX_TYPES_BYTES = 4 * 1024 * 1024; // [Content_Types].xml of a real document is a few KB
9
+ // Cumulative work of the relationship scan: a hostile archive can hold thousands of individually allowed .rels parts.
10
+ const MAX_RELS_PARTS = 5000;
11
+ const MAX_RELS_BYTES = 64 * 1024 * 1024;
12
+
13
+ /** Throws unless `bytes` is an OOXML ZIP whose main part content type matches `format`. Returns the opened zip. */
14
+ export function validatePackage(bytes, format) {
15
+ const want = FORMATS[format];
16
+ if (!want) throw new ZipError(`unsupported format: ${format}`);
17
+ const zip = openZip(bytes);
18
+ const ct = zip.readText("[Content_Types].xml", MAX_TYPES_BYTES);
19
+ if (ct == null) throw new ZipError("missing [Content_Types].xml");
20
+ let found = false;
21
+ walkXml(ct, {
22
+ onOpen(name, attrs) {
23
+ if (name === "Override" && attrs.PartName === want.part) {
24
+ if (/macroEnabled/i.test(attrs.ContentType || "")) throw new ZipError("macro-enabled documents are refused");
25
+ if (attrs.ContentType === want.main) found = true;
26
+ }
27
+ },
28
+ });
29
+ if (!found) throw new ZipError(`content types do not describe a .${format} main part`);
30
+ return zip;
31
+ }
32
+
33
+ function relTargets(zip) {
34
+ const missing = [];
35
+ let parts = 0;
36
+ let bytes = 0;
37
+ for (const name of zip.names) {
38
+ if (!name.endsWith(".rels")) continue;
39
+ if (++parts > MAX_RELS_PARTS) throw new ZipError(`too many relationship parts (more than ${MAX_RELS_PARTS})`);
40
+ const xml = zip.readText(name);
41
+ if (xml == null) continue;
42
+ bytes += xml.length;
43
+ if (bytes > MAX_RELS_BYTES) throw new ZipError("the relationship parts are too large in total");
44
+ const srcDir = path.posix.dirname(path.posix.dirname(name)); // word/_rels/x.rels -> word
45
+ const base = srcDir === "." ? "" : srcDir;
46
+ walkXml(xml, {
47
+ onOpen(tag, attrs) {
48
+ if (tag !== "Relationship" || attrs.TargetMode === "External" || !attrs.Target) return;
49
+ const target = attrs.Target.startsWith("/") ? attrs.Target.slice(1) : path.posix.normalize(path.posix.join(base, attrs.Target));
50
+ const clean = target.split("#")[0];
51
+ if (clean && !zip.entries.has(clean)) missing.push({ rels: name, target: clean });
52
+ },
53
+ });
54
+ }
55
+ return missing;
56
+ }
57
+
58
+ const DOCX_COUNTED = ["w:drawing", "w:fldChar", "w:sdt", "m:oMath", "w:hyperlink", "w:bookmarkStart"];
59
+
60
+ function countElements(xml, names) {
61
+ const counts = Object.fromEntries(names.map((n) => [n, 0]));
62
+ walkXml(xml, {
63
+ onOpen(name) {
64
+ if (name in counts) counts[name]++;
65
+ },
66
+ });
67
+ return counts;
68
+ }
69
+
70
+ const SEVERITY = { invalid_package: "error", part_removed: "error", relationship_target_missing: "error", count_decreased: "warn", part_added: "info", slide_removed: "warn" };
71
+
72
+ /** The relationships of one part: [{id, type (the last segment of the type URI), target (a part path, or the URL when external), external}]. */
73
+ export function partRels(zip, part) {
74
+ const out = [];
75
+ const xml = zip.readText(part.replace(/([^/]+)$/, "_rels/$1.rels"));
76
+ if (xml == null) return out;
77
+ const base = path.posix.dirname(part);
78
+ walkXml(xml, {
79
+ onOpen(tag, a) {
80
+ if (tag !== "Relationship" || !a.Type || !a.Target) return;
81
+ const external = a.TargetMode === "External";
82
+ const target = external ? a.Target : a.Target.startsWith("/") ? a.Target.slice(1) : path.posix.normalize(path.posix.join(base === "." ? "" : base, a.Target.split("#")[0]));
83
+ out.push({ id: a.Id, type: a.Type.split("/").pop(), target, external });
84
+ },
85
+ });
86
+ return out;
87
+ }
88
+
89
+ /** p:sldId id -> slide part, in the order of ppt/presentation.xml; empty when it cannot be read (then nothing counts as a deleted slide). */
90
+ function slideParts(zip) {
91
+ const rels = new Map(partRels(zip, "ppt/presentation.xml").map((r) => [r.id, r.target]));
92
+ const out = new Map();
93
+ for (const [tag] of (zip.readText("ppt/presentation.xml") || "").matchAll(/<(?:\w+:)?sldId\b[^>]*>/g)) {
94
+ const id = /\sid="(\d+)"/.exec(tag)?.[1];
95
+ const rid = /\sr:id="([^"]+)"/.exec(tag)?.[1];
96
+ if (id && rels.has(rid)) out.set(id, rels.get(rid));
97
+ }
98
+ return out;
99
+ }
100
+
101
+ /**
102
+ * The parts of the slides a deck lost: each slide (by its p:sldId id) the presentation listed before and does not list after, its own notes
103
+ * slide and comments, and the .rels of all of them. Gone from the package, they are reported as `slide_removed` (a warning: a slide is gone,
104
+ * as asked or not), not as lost parts (an error, which recommends Revert).
105
+ * A later added slide may get the same part name (the engine takes the first free one): that part is then a different slide, and the deep
106
+ * check's comparison of its old and new content means nothing.
107
+ */
108
+ function removedSlideParts(pre, post) {
109
+ const out = new Set();
110
+ const after = slideParts(post);
111
+ const add = (part) => (out.add(part), out.add(part.replace(/([^/]+)$/, "_rels/$1.rels")));
112
+ for (const [id, part] of slideParts(pre)) {
113
+ if (after.has(id)) continue;
114
+ add(part);
115
+ for (const x of partRels(pre, part)) if (!x.external && (x.type === "notesSlide" || x.type === "comments")) add(x.target);
116
+ }
117
+ return out;
118
+ }
119
+
120
+ const relKey = (m) => `${m.rels}\0${m.target}`;
121
+
122
+ /** The checks before cleaning: flag values are still the document's own strings (checkPackages dedupes on them, then cleans). */
123
+ function rawNodeChecks(preBytes, postBytes, format) {
124
+ const flags = [];
125
+ let post;
126
+ try {
127
+ post = validatePackage(postBytes, format);
128
+ } catch (err) {
129
+ return [{ code: "invalid_package", severity: "error", detail: err.message }];
130
+ }
131
+ try {
132
+ return scanDifferences(openZip(preBytes), post, format, flags);
133
+ } catch (err) {
134
+ if (!(err instanceof ZipError)) throw err;
135
+ // a budget or limit hit while scanning: stated, never read as clean
136
+ return [{ code: "invalid_package", severity: "error", detail: `the package checks stopped: ${err.message}` }];
137
+ }
138
+ }
139
+
140
+ function scanDifferences(pre, post, format, flags) {
141
+ const gone = format === "pptx" ? removedSlideParts(pre, post) : new Set();
142
+ for (const n of pre.names) if (!post.entries.has(n)) flags.push({ code: gone.has(n) ? "slide_removed" : "part_removed", part: n });
143
+ for (const n of post.names) if (!pre.entries.has(n)) flags.push({ code: "part_added", part: n });
144
+ // differential: a relationship already dangling in the input is not the agent's damage
145
+ const preMissing = new Set(relTargets(pre).map(relKey));
146
+ for (const m of relTargets(post)) if (!preMissing.has(relKey(m))) flags.push({ code: "relationship_target_missing", ...m });
147
+ if (format === "docx") {
148
+ const a = countElements(pre.readText("word/document.xml") || "", DOCX_COUNTED);
149
+ const b = countElements(post.readText("word/document.xml") || "", DOCX_COUNTED);
150
+ for (const k of DOCX_COUNTED) if (b[k] < a[k]) flags.push({ code: "count_decreased", element: k, before: a[k], after: b[k] });
151
+ }
152
+ return flags.map((f) => ({ ...f, severity: SEVERITY[f.code] || "warn" }));
153
+ }
154
+
155
+ /** The Node-only checks (always available): part list, relationship targets, element counts. Each flag carries a severity and is cleaned. */
156
+ export function nodeChecks(preBytes, postBytes, format) {
157
+ return rawNodeChecks(preBytes, postBytes, format).map(cleanFlag);
158
+ }
159
+
160
+ /** The one sentence about the deeper check. A LibreOffice check still running is "pending", never "passed". */
161
+ export const deepNotice = (skipped = [], pending = false) =>
162
+ `Deeper check ran${skipped.length ? `; skipped: ${skipped.map((x) => `${x.check} (${x.reason})`).join(", ")}` : ""}${pending ? "; the LibreOffice conversion check is still running (pending), the Activity list updates when it finishes" : ""}.`;
163
+
164
+ const flagKey = (f) => JSON.stringify([f.code, f.part, f.element, f.rels, f.target, f.id]);
165
+
166
+ /**
167
+ * Node checks plus the deeper Python/LibreOffice check when a Python with lxml is available.
168
+ * Returns {flags, checks}: `flags` are findings (severity info/warn/error); `checks` says what ran, so a skipped deep check is never silent.
169
+ */
170
+ export function checkPackages(preBytes, postBytes, format, opts = {}) {
171
+ const node = rawNodeChecks(preBytes, postBytes, format);
172
+ if (node.some((f) => f.code === "invalid_package")) return { flags: node.map(cleanFlag), checks: { node: "ran", deep: { status: "skipped", reason: "the package is invalid" } } };
173
+ const deep = runDeepCheck(preBytes, postBytes, format, { deferSoffice: !!opts.deferSoffice });
174
+ // the deep check counts per part; its numbers replace the coarse whole-document counts
175
+ const base = deep.ran ? node.filter((f) => f.code !== "count_decreased") : node;
176
+ // the deep check compares parts by name: a deleted slide's parts are lost parts to it, and a slide added later under a freed name is the
177
+ // deleted one with fewer elements. Both are the slide delete the node check already told apart; any other finding on such a part stays.
178
+ const gone = format === "pptx" ? removedSlideParts(openZip(preBytes), openZip(postBytes)) : new Set();
179
+ const deepFlags = deep.flags.filter((f) => !(gone.has(f.part) && f.code === "count_decreased")).map((f) => (f.code === "part_removed" && gone.has(f.part) ? { code: "slide_removed", severity: SEVERITY.slide_removed, part: f.part } : f));
180
+ const seen = new Set();
181
+ // dedupe on the raw values, then clean: cleaning first could merge distinct flags
182
+ const flags = [...base, ...deepFlags].filter((f) => !seen.has(flagKey(f)) && seen.add(flagKey(f))).map(cleanFlag);
183
+ const checks = {
184
+ node: "ran",
185
+ deep: deep.ran ? { status: "ran", checks: deep.checksRun, skipped: deep.skipped, ...(deep.sofficePending ? { soffice: "pending" } : {}) } : { status: "skipped", reason: deep.reason, code: deep.code },
186
+ };
187
+ checks.notice = deep.ran ? deepNotice(deep.skipped, deep.sofficePending) : `Deeper check skipped: ${deep.reason}. Only the built-in checks ran.`;
188
+ return { flags, checks };
189
+ }
package/src/paths.js ADDED
@@ -0,0 +1,64 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { PRODUCT, env } from "./constants.js";
6
+
7
+ export function stateDir() {
8
+ const o = env("STATE_DIR");
9
+ return o ? path.resolve(o) : path.join(os.homedir(), PRODUCT.stateDirName);
10
+ }
11
+ export const socketPath = () => path.join(stateDir(), "s.sock");
12
+ export const serverRecordPath = () => path.join(stateDir(), "server.json");
13
+ export const sessionKey = (realpath) => crypto.createHash("sha256").update(realpath).digest("hex").slice(0, 16);
14
+
15
+ /** Creates the state dir 0700 and verifies it is ours, a real directory (not a symlink) and not group/world accessible. */
16
+ export function ensureStateDir() {
17
+ const dir = stateDir();
18
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
19
+ const st = fs.lstatSync(dir);
20
+ if (!st.isDirectory() || st.isSymbolicLink()) throw new Error(`${dir} is not a plain directory`);
21
+ if (st.uid !== process.getuid()) throw new Error(`${dir} is not owned by the current user`);
22
+ if (st.mode & 0o077) {
23
+ fs.chmodSync(dir, 0o700);
24
+ if (fs.lstatSync(dir).mode & 0o077) throw new Error(`${dir} is accessible by others and could not be fixed`);
25
+ }
26
+ return dir;
27
+ }
28
+
29
+ export function sub(...parts) {
30
+ const dir = path.join(stateDir(), ...parts);
31
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
32
+ return dir;
33
+ }
34
+
35
+ /**
36
+ * Writes every byte of `data` to `fd`: fs.writeSync may accept fewer bytes than asked (a short write), so loop until all are written.
37
+ * Zero progress is an error (a full or failing device), never a reason to spin or to carry on with a truncated file.
38
+ * `write` is injectable for tests.
39
+ */
40
+ export function writeFully(fd, data, write = fs.writeSync) {
41
+ const buf = typeof data === "string" ? Buffer.from(data) : data;
42
+ let off = 0;
43
+ while (off < buf.length) {
44
+ const n = write(fd, buf, off, buf.length - off);
45
+ if (!(n > 0)) throw Object.assign(new Error(`short write: the device accepted ${off} of ${buf.length} bytes and then no more`), { code: "EIO_SHORT_WRITE" });
46
+ off += n;
47
+ }
48
+ }
49
+
50
+ /** State files: temp file created exclusively with mode 0600, then renamed over the target. */
51
+ export function writeStateFile(file, data, write = fs.writeSync) {
52
+ const tmp = `${file}.${process.pid}.${crypto.randomBytes(6).toString("hex")}.tmp`;
53
+ const fd = fs.openSync(tmp, "wx", 0o600);
54
+ try {
55
+ writeFully(fd, data, write);
56
+ fs.fsyncSync(fd);
57
+ } catch (err) {
58
+ fs.closeSync(fd);
59
+ fs.rmSync(tmp, { force: true }); // a failed write must not leave a truncated temp file behind
60
+ throw err;
61
+ }
62
+ fs.closeSync(fd);
63
+ fs.renameSync(tmp, file);
64
+ }