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.
- package/CHANGELOG.md +144 -0
- package/INSTALL.md +25 -0
- package/LICENSE +21 -0
- package/NOTICE +49 -0
- package/README.md +148 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +67 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +288 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1912 -0
- package/src/settings.js +161 -0
- package/src/setup.js +199 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-BUtofdtr.js +197 -0
- package/web/dist/assets/index-Dfizlg0i.css +1 -0
- package/web/dist/index.html +14 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { normText } from "./text.js";
|
|
2
|
+
import { walkXml } from "./xml.js";
|
|
3
|
+
import { openZip } from "./zip.js";
|
|
4
|
+
import { nodeChecks, validatePackage } from "./package.js";
|
|
5
|
+
import { parseDocxParagraphs } from "./docx.js";
|
|
6
|
+
import { fmtNote } from "./fmt.js";
|
|
7
|
+
|
|
8
|
+
// The check a tracked-changes copy must pass before it is written (docs/contracts/tracked-changes.md). Written from the markup contract,
|
|
9
|
+
// not from the builder: it shares no code with src/redline.js or src/xml-tree.js. It resolves the copy's revisions twice, the way Word's
|
|
10
|
+
// Accept All and Reject All do for the forms the contract allows, and compares the results with the two versions:
|
|
11
|
+
// accept: paragraph for paragraph equal to AFTER on raw text, body path, style, alignment and run formatting; every other part byte-identical;
|
|
12
|
+
// reject: equal to BEFORE on raw text and body path; the paragraphs whose formatting differs must number exactly `counts.formatting`.
|
|
13
|
+
// It is strict on purpose: any revision form the builder never writes fails the check, never passes it.
|
|
14
|
+
|
|
15
|
+
const MARK_PARENTS = new Set(["w:rPr"]);
|
|
16
|
+
const NEVER_WRITTEN = new Set(["w:pPrChange", "w:rPrChange", "w:moveFrom", "w:moveTo", "w:moveFromRangeStart", "w:moveToRangeStart", "w:cellIns", "w:cellDel", "w:cellMerge", "w:numberingChange", "w:sectPrChange", "w:tblPrChange", "w:tblGridChange", "w:trPrChange", "w:tcPrChange", "w:tblPrExChange", "w:customXmlInsRangeStart", "w:customXmlDelRangeStart"]);
|
|
17
|
+
const REV = new Set(["w:ins", "w:del"]);
|
|
18
|
+
const TEXTISH = new Set(["w:r", "w:t", "w:delText", "w:tab", "w:br", "w:cr", "w:drawing", "w:pict", "w:hyperlink", "w:fldSimple", "w:sdt", "w:smartTag", "w:instrText"]);
|
|
19
|
+
const REV_ATTRS = ["w:id", "w:author", "w:date"];
|
|
20
|
+
|
|
21
|
+
class CheckFail extends Error {}
|
|
22
|
+
const fail = (why) => {
|
|
23
|
+
throw new CheckFail(why);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// ---- a small element tree of its own (not lossless: it only has to be read back by parseDocxParagraphs) ----------------------------
|
|
27
|
+
|
|
28
|
+
function parseDom(xml) {
|
|
29
|
+
const root = { name: "#root", attrs: {}, kids: [] };
|
|
30
|
+
const stack = [root];
|
|
31
|
+
let head = "";
|
|
32
|
+
const decl = /^\s*<\?xml[^?]*\?>/.exec(xml);
|
|
33
|
+
if (decl) head = decl[0];
|
|
34
|
+
walkXml(xml, {
|
|
35
|
+
onOpen(name, attrs) {
|
|
36
|
+
const el = { name, attrs, kids: [], parent: stack[stack.length - 1] };
|
|
37
|
+
stack[stack.length - 1].kids.push(el);
|
|
38
|
+
stack.push(el);
|
|
39
|
+
},
|
|
40
|
+
onClose(name) {
|
|
41
|
+
const el = stack.pop();
|
|
42
|
+
if (!el || el.name !== name) fail(`the copy's XML does not nest (${name})`);
|
|
43
|
+
},
|
|
44
|
+
onText(t) {
|
|
45
|
+
if (stack.length > 1) stack[stack.length - 1].kids.push(t);
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
if (stack.length !== 1) fail("the copy's XML is not closed");
|
|
49
|
+
return { root, head };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const escText = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
53
|
+
const escAttr = (s) => escText(s).replace(/"/g, """);
|
|
54
|
+
function serialize(node) {
|
|
55
|
+
if (typeof node === "string") return escText(node);
|
|
56
|
+
if (node.name === "#root") return node.kids.map(serialize).join("");
|
|
57
|
+
const attrs = Object.entries(node.attrs).map(([k, v]) => ` ${k}="${escAttr(v)}"`).join("");
|
|
58
|
+
return node.kids.length ? `<${node.name}${attrs}>${node.kids.map(serialize).join("")}</${node.name}>` : `<${node.name}${attrs}/>`;
|
|
59
|
+
}
|
|
60
|
+
const elKids = (el) => el.kids.filter((k) => typeof k !== "string");
|
|
61
|
+
function* walk(el) {
|
|
62
|
+
for (const k of el.kids) {
|
|
63
|
+
if (typeof k === "string") continue;
|
|
64
|
+
yield k;
|
|
65
|
+
yield* walk(k);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const hasText = (el) => [...walk(el)].some((d) => TEXTISH.has(d.name) && !inside(d, el, "w:pPr"));
|
|
69
|
+
function inside(d, top, name) {
|
|
70
|
+
for (let p = d.parent; p && p !== top; p = p.parent) if (p.name === name) return true;
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const isMark = (el) => REV.has(el.name) && el.parent && MARK_PARENTS.has(el.parent.name) && el.parent.parent?.name === "w:pPr";
|
|
74
|
+
|
|
75
|
+
// ---- resolving ------------------------------------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Accept (keep = "ins") or reject (keep = "del") our revisions in a document.xml string; `isOurs(el)` says which revision elements are ours.
|
|
79
|
+
* A paragraph whose own mark is removed (deleted mark on accept, inserted mark on reject) must be empty once its runs are resolved, and is
|
|
80
|
+
* dropped: the next paragraph keeps its own properties (contract M7, M8). Throws CheckFail on any form the contract does not allow.
|
|
81
|
+
*/
|
|
82
|
+
function resolve(xml, keep, isOurs) {
|
|
83
|
+
const { root, head } = parseDom(xml);
|
|
84
|
+
const doomed = [];
|
|
85
|
+
const visit = (el) => {
|
|
86
|
+
for (let i = 0; i < el.kids.length; i++) {
|
|
87
|
+
const k = el.kids[i];
|
|
88
|
+
if (typeof k === "string") continue;
|
|
89
|
+
if (NEVER_WRITTEN.has(k.name)) fail(`a revision form the copy never uses: ${k.name}`);
|
|
90
|
+
if (REV.has(k.name) && isOurs(k)) {
|
|
91
|
+
if (isMark(k)) {
|
|
92
|
+
if (k.kids.length) fail("a paragraph-mark revision has content");
|
|
93
|
+
const p = k.parent.parent.parent;
|
|
94
|
+
if (p?.name !== "w:p") fail("a paragraph-mark revision outside a paragraph");
|
|
95
|
+
if (k.name !== `w:${keep}`) doomed.push(p);
|
|
96
|
+
el.kids.splice(i--, 1);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (k.name === `w:${keep}`) {
|
|
100
|
+
// unwrap: w:ins keeps its runs on accept, w:del gives its runs back (as w:t) on reject
|
|
101
|
+
for (const r of elKids(k)) if (r.name !== "w:r") fail(`a revision holds ${r.name}, not a run`);
|
|
102
|
+
if (keep === "del") for (const d of walk(k)) if (d.name === "w:delText") d.name = "w:t";
|
|
103
|
+
for (const r of k.kids) if (typeof r !== "string") r.parent = el;
|
|
104
|
+
el.kids.splice(i, 1, ...k.kids);
|
|
105
|
+
i--;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
el.kids.splice(i--, 1);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
visit(k);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
visit(root);
|
|
115
|
+
for (const p of doomed) {
|
|
116
|
+
if (hasText(p)) fail("a paragraph whose mark is removed still has text after resolving");
|
|
117
|
+
const list = p.parent.kids;
|
|
118
|
+
const at = list.indexOf(p);
|
|
119
|
+
const next = list.slice(at + 1).find((x) => typeof x !== "string");
|
|
120
|
+
if (next?.name !== "w:p") fail("a paragraph whose mark is removed has no paragraph after it");
|
|
121
|
+
list.splice(at, 1);
|
|
122
|
+
}
|
|
123
|
+
const left = [...walk(root)].filter((d) => REV.has(d.name) && isOurs(d));
|
|
124
|
+
if (left.length) fail("revisions are left after resolving");
|
|
125
|
+
return head + serialize(root);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const allOurs = () => true;
|
|
129
|
+
export const acceptOurs = (xml, isOurs = allOurs) => resolve(xml, "ins", isOurs);
|
|
130
|
+
export const rejectOurs = (xml, isOurs = allOurs) => resolve(xml, "del", isOurs);
|
|
131
|
+
|
|
132
|
+
// ---- markup shape -------------------------------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
/** Every w:id="N" in an XML part. */
|
|
135
|
+
function idsIn(xml, into) {
|
|
136
|
+
walkXml(xml, {
|
|
137
|
+
onOpen(_, a) {
|
|
138
|
+
if (a["w:id"] != null) into.add(a["w:id"]);
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
return into;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function checkShape(root, isOurs, { author, date }) {
|
|
145
|
+
const seen = new Set();
|
|
146
|
+
const touched = new Set(); // paragraphs holding one of our revisions
|
|
147
|
+
for (const el of walk(root)) {
|
|
148
|
+
if (NEVER_WRITTEN.has(el.name)) fail(`a revision form the copy never uses: ${el.name}`);
|
|
149
|
+
if (!REV.has(el.name) || !isOurs(el)) continue;
|
|
150
|
+
const keys = Object.keys(el.attrs);
|
|
151
|
+
if (keys.join(" ") !== REV_ATTRS.join(" ")) fail(`a revision's attributes are not w:id, w:author, w:date in that order: ${keys.join(" ")}`);
|
|
152
|
+
if (!/^\d{1,10}$/.test(el.attrs["w:id"]) || seen.has(el.attrs["w:id"])) fail("a revision id is not a unique number");
|
|
153
|
+
seen.add(el.attrs["w:id"]);
|
|
154
|
+
if (el.attrs["w:author"] !== author) fail("a revision has another author");
|
|
155
|
+
if (el.attrs["w:date"] !== date) fail("a revision has another date");
|
|
156
|
+
let p = el.parent;
|
|
157
|
+
while (p && p.name !== "w:p") p = p.parent;
|
|
158
|
+
if (!p) fail("a revision outside a paragraph");
|
|
159
|
+
touched.add(p);
|
|
160
|
+
if (isMark(el)) {
|
|
161
|
+
if (elKids(el.parent)[0] !== el) fail("a paragraph-mark revision is not first in the mark's properties");
|
|
162
|
+
const pPr = el.parent.parent;
|
|
163
|
+
const after = elKids(pPr).slice(elKids(pPr).indexOf(el.parent) + 1);
|
|
164
|
+
if (after.some((x) => x.name !== "w:sectPr")) fail("the mark's properties are not in their place in the paragraph properties");
|
|
165
|
+
if (elKids(p)[0] !== pPr) fail("the paragraph properties are not first in the paragraph");
|
|
166
|
+
} else {
|
|
167
|
+
if (el.parent !== p) fail("a run revision is not directly in its paragraph");
|
|
168
|
+
for (const r of elKids(el)) {
|
|
169
|
+
if (r.name !== "w:r") fail(`a revision holds ${r.name}, not a run`);
|
|
170
|
+
for (const d of walk(r)) {
|
|
171
|
+
if (el.name === "w:del" && d.name === "w:t") fail("deleted text is not in w:delText");
|
|
172
|
+
if (el.name === "w:ins" && d.name === "w:delText") fail("inserted text is in w:delText");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Word drops leading and trailing spaces of a text element without xml:space="preserve"; the round trip below would not notice
|
|
178
|
+
for (const p of touched)
|
|
179
|
+
for (const d of walk(p)) {
|
|
180
|
+
if (d.name !== "w:t" && d.name !== "w:delText") continue;
|
|
181
|
+
const t = d.kids.join("");
|
|
182
|
+
if (/^\s|\s$/.test(t) && d.attrs["xml:space"] !== "preserve") fail("a text piece with spaces at its edge is not marked to keep them");
|
|
183
|
+
}
|
|
184
|
+
return seen;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ---- comparing ----------------------------------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
const fmtOf = (p) => ({ props: p.fmt, style: p.style, align: p.align });
|
|
190
|
+
const same = (x, y) => JSON.stringify(x) === JSON.stringify(y);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* {ok: true} or {ok: false, why}. `before`, `after`, `out` are DOCX bytes; `counts.formatting` is what the builder said Reject All keeps;
|
|
194
|
+
* `author` and `date` are what every one of our revisions must carry. Never throws on a bad copy: a crash in here is a failed check.
|
|
195
|
+
*/
|
|
196
|
+
export function verifyTracked({ before, after, out, counts, author, date }) {
|
|
197
|
+
try {
|
|
198
|
+
try {
|
|
199
|
+
validatePackage(out, "docx");
|
|
200
|
+
} catch (err) {
|
|
201
|
+
fail(`the copy is not a valid package: ${err.message}`);
|
|
202
|
+
}
|
|
203
|
+
if (nodeChecks(after, out, "docx").some((f) => f.severity === "error")) fail("the package checks found an error");
|
|
204
|
+
const za = openZip(after);
|
|
205
|
+
const zb = openZip(before);
|
|
206
|
+
const zo = openZip(out);
|
|
207
|
+
if (zo.names.join("\0") !== za.names.join("\0")) fail("the copy's parts differ from the agent's version");
|
|
208
|
+
for (const n of za.names) if (n !== "word/document.xml" && !zo.read(n).equals(za.read(n))) fail(`a part other than the main text differs: ${n}`);
|
|
209
|
+
const xa = za.readText("word/document.xml");
|
|
210
|
+
const xb = zb.readText("word/document.xml");
|
|
211
|
+
const xo = zo.readText("word/document.xml");
|
|
212
|
+
// revisions already in either version are not ours; every id in any part is an input id
|
|
213
|
+
const inputIds = new Set();
|
|
214
|
+
for (const z of [za, zb]) for (const n of z.names) if (/\.xml$/i.test(n)) idsIn(z.readText(n), inputIds);
|
|
215
|
+
const isOurs = (el) => !inputIds.has(el.attrs["w:id"]);
|
|
216
|
+
const ours = checkShape(parseDom(xo).root, isOurs, { author, date });
|
|
217
|
+
if (!ours.size) fail("the copy has no revisions");
|
|
218
|
+
const revCount = (x) => {
|
|
219
|
+
let n = 0;
|
|
220
|
+
walkXml(x, { onOpen: (name) => REV.has(name) && n++ });
|
|
221
|
+
return n;
|
|
222
|
+
};
|
|
223
|
+
if (revCount(xo) !== ours.size + revCount(xa)) fail("the copy has revisions that are neither ours nor the file's own");
|
|
224
|
+
const maxIn = Math.max(0, ...[...inputIds].filter((v) => /^\d+$/.test(v)).map(Number));
|
|
225
|
+
for (const id of ours) if (Number(id) <= maxIn) fail("a revision id is not above every id in the file");
|
|
226
|
+
|
|
227
|
+
const A = parseDocxParagraphs(xa);
|
|
228
|
+
const acc = parseDocxParagraphs(acceptOurs(xo, isOurs));
|
|
229
|
+
if (acc.length !== A.length) fail(`Accept All gives ${acc.length} paragraphs, the agent's version has ${A.length}`);
|
|
230
|
+
acc.forEach((p, i) => {
|
|
231
|
+
const q = A[i];
|
|
232
|
+
if (p.text !== q.text) fail(`Accept All: paragraph ${i + 1} text differs`);
|
|
233
|
+
if (!same(p.path, q.path)) fail(`Accept All: paragraph ${i + 1} is in another place`);
|
|
234
|
+
if (!same(fmtOf(p), fmtOf(q))) fail(`Accept All: paragraph ${i + 1} formatting differs`);
|
|
235
|
+
});
|
|
236
|
+
const B = parseDocxParagraphs(xb);
|
|
237
|
+
const rej = parseDocxParagraphs(rejectOurs(xo, isOurs));
|
|
238
|
+
if (rej.length !== B.length) fail(`Reject All gives ${rej.length} paragraphs, the earlier version has ${B.length}`);
|
|
239
|
+
let formatting = 0;
|
|
240
|
+
rej.forEach((p, i) => {
|
|
241
|
+
const q = B[i];
|
|
242
|
+
if (p.text !== q.text) fail(`Reject All: paragraph ${i + 1} text differs`);
|
|
243
|
+
if (!same(p.path, q.path)) fail(`Reject All: paragraph ${i + 1} is in another place`);
|
|
244
|
+
if (normText(q.text) && fmtNote(fmtOf(q), fmtOf(p))) formatting++;
|
|
245
|
+
});
|
|
246
|
+
if (formatting !== (counts?.formatting ?? 0)) fail(`Reject All keeps ${formatting} formatting changes, the copy says ${counts?.formatting ?? 0}`);
|
|
247
|
+
return { ok: true };
|
|
248
|
+
} catch (err) {
|
|
249
|
+
return { ok: false, why: err instanceof CheckFail ? err.message : `the check stopped: ${String(err?.message || err).slice(0, 200)}` };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
3
|
+
import { buildTracked } from "./redline.js";
|
|
4
|
+
import { verifyTracked } from "./redline-check.js";
|
|
5
|
+
import { env } from "./constants.js";
|
|
6
|
+
import { openZip } from "./zip.js";
|
|
7
|
+
import { replaceEntry } from "./zip-write.js";
|
|
8
|
+
|
|
9
|
+
// One tracked-changes copy, off the server thread: build it (src/redline.js), then check it (src/redline-check.js). One message back:
|
|
10
|
+
// {ok: true, bytes, counts} | {refused: [codes]} | {check_failed: why}. The copy's bytes only leave this worker after the check passed.
|
|
11
|
+
// ONIONSKIN_TEST_TAMPER_EXPORT=drop-del (tests only) removes one deletion from the built copy before the check, to prove the check is
|
|
12
|
+
// what stands between the builder and the disk; it can only make a copy fail, never pass.
|
|
13
|
+
const { before, after, author, date } = workerData;
|
|
14
|
+
let msg;
|
|
15
|
+
try {
|
|
16
|
+
const b = fs.readFileSync(before);
|
|
17
|
+
const a = fs.readFileSync(after);
|
|
18
|
+
const r = buildTracked({ before: b, after: a, author, date });
|
|
19
|
+
if (r.refused) msg = { refused: r.refused };
|
|
20
|
+
else {
|
|
21
|
+
let out = r.bytes;
|
|
22
|
+
if (env("TEST_TAMPER_EXPORT", "") === "drop-del") out = replaceEntry(out, "word/document.xml", openZip(out).readText("word/document.xml").replace(/<w:del [^>]*>[\s\S]*?<\/w:del>/, ""));
|
|
23
|
+
const v = verifyTracked({ before: b, after: a, out, counts: r.counts, author, date });
|
|
24
|
+
msg = v.ok ? { ok: true, bytes: out, counts: r.counts } : { check_failed: v.why };
|
|
25
|
+
}
|
|
26
|
+
} catch (err) {
|
|
27
|
+
msg = { check_failed: `the copy could not be made: ${String(err?.message || err).slice(0, 200)}` };
|
|
28
|
+
}
|
|
29
|
+
parentPort.postMessage(msg);
|