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,154 @@
|
|
|
1
|
+
// The document reading and anchor work of one request, as plain data in and out, with no session state: building anchors from the page's
|
|
2
|
+
// (Send, a comment queued during a run, a quick fix's Use), moving stored ones onto newer bytes (done, revert), resolving them for the page
|
|
3
|
+
// (resolve), placing a quick fix's selection (suggest), the review cards' check (done of a review) and the outline of a batch for an agent
|
|
4
|
+
// Onionskin started. The server runs it in a warm worker thread with a time limit (src/anchor-worker.js, src/offload.js anchorsOffThread),
|
|
5
|
+
// so a crafted file never holds the server's thread; the synchronous Sessions methods tests call run it on their own thread. Either way
|
|
6
|
+
// it runs under a scan budget (src/text.js scanBudget) as a second line: past it an anchor is made weaker or resolves as moved or removed.
|
|
7
|
+
import { buildDurable, readAll, resolveDurableAny, resolveView, unitsFor, viewDurable } from "./anchors.js";
|
|
8
|
+
import { durableFromEngine, headingBefore, resolveDurable } from "./docx.js";
|
|
9
|
+
import { capDurable, cutText } from "./retain.js";
|
|
10
|
+
import { OUTLINE_FILE_MAX, cardsFrom, outlineOf } from "./review.js";
|
|
11
|
+
import { buildUseReply, placeOf, selectionOf } from "./suggest-reply.js";
|
|
12
|
+
import { normText, scanBudget } from "./text.js";
|
|
13
|
+
import { parseA1 } from "./ops/common.js";
|
|
14
|
+
import { validatePackage } from "./package.js";
|
|
15
|
+
import { CapError } from "./xml.js";
|
|
16
|
+
import { unknownFormat } from "./constants.js";
|
|
17
|
+
|
|
18
|
+
/** A stored durable anchor re-resolved against `units` (the bytes the agent will read next); null when the unit is gone. Without a durable, one is built from the browser's anchor. */
|
|
19
|
+
export function reanchor(units, c) {
|
|
20
|
+
const d = c.durable;
|
|
21
|
+
if (!d) return buildDurable(units, c.anchor);
|
|
22
|
+
const r = resolveDurableAny(units, d);
|
|
23
|
+
if (r.method === "none") return null;
|
|
24
|
+
if (units.format === "docx") return capDurable({ ...d, ...r, format: "docx", quote: d.quote, prefix: d.prefix, suffix: d.suffix, paraText: r.text, engineDelta: r.engineDelta });
|
|
25
|
+
if (units.format === "xlsx") return buildDurable(units, { location: { sheetIndex: r.sheetIndex, sheetName: r.sheetName, range: r.range } });
|
|
26
|
+
if (units.format === "pptx") return buildDurable(units, { location: { slideIndex: r.slideIndex, slideId: r.slideId, shapeId: r.shapeId ?? undefined, shapeName: r.shapeName, cell: d.cell || undefined }, quote: d.quote, prefix: d.prefix, suffix: d.suffix, hitKind: d.quote ? undefined : "shape" });
|
|
27
|
+
// the resolved place in the page's own offsets (src/html.js pageLocation), which htmlDurable reads
|
|
28
|
+
if (units.format === "html") return buildDurable(units, { location: r.page?.start != null ? r.page : { nodeId: r.nodeId }, quote: d.quote, prefix: d.prefix, suffix: d.suffix, hitKind: d.quote ? undefined : "element" });
|
|
29
|
+
return unknownFormat(units.format, "reanchor");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The page's anchor made durable, with a PowerPoint pin's slide as its fallback (a pin on a deleted shape stays on its slide). */
|
|
33
|
+
function built(units, anchor, pin) {
|
|
34
|
+
const d = buildDurable(units, anchor);
|
|
35
|
+
if (pin && units.format === "pptx" && d?.shapeId != null) d.pinFallback = buildDurable(units, { location: { slideIndex: d.slideIndex, slideId: d.slideId } });
|
|
36
|
+
return d;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The view the page reported at Send ({format, ...}), made durable against `units`. */
|
|
40
|
+
export function viewOf(units, vs) {
|
|
41
|
+
if (!vs) return null;
|
|
42
|
+
if (units.format === "docx" && vs.format !== "docx") return { format: units.format, raw: vs };
|
|
43
|
+
if (units.format !== "docx") return viewDurable(units, vs) || { format: units.format, raw: vs };
|
|
44
|
+
const d = durableFromEngine(units.paragraphs, { blockPath: vs.blockPath }, { quote: vs.quote });
|
|
45
|
+
return { format: "docx", durable: d, offsetPx: Number(vs.offsetPx) || 0, zoom: vs.zoom ?? "fit", fraction: vs.fraction };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** What the page's resolve shows: every comment's anchor (by id) and the view, in terms of `units`. */
|
|
49
|
+
export function resolveAllOf(units, comments, view) {
|
|
50
|
+
const out = { anchors: {}, view: null };
|
|
51
|
+
for (const c of comments) {
|
|
52
|
+
let r = resolveDurableAny(units, c.durable);
|
|
53
|
+
if (r.method === "none" && units.format === "pptx" && c.durable.pinFallback) {
|
|
54
|
+
const f = resolveDurableAny(units, c.durable.pinFallback);
|
|
55
|
+
if (f.method !== "none") r = { ...f, tieLost: true };
|
|
56
|
+
}
|
|
57
|
+
if (units.format === "docx" && r.method !== "none") r.heading = headingBefore(units.paragraphs, r.ordinal);
|
|
58
|
+
if (typeof r.text === "string") r.text = cutText(r.text, 4000); // what the page shows of it; resolved text is never stored
|
|
59
|
+
out.anchors[c.id] = r;
|
|
60
|
+
}
|
|
61
|
+
if (units.format === "pptx") out.slides = units.deck.slides.map((x) => ({ index: x.index, id: x.id }));
|
|
62
|
+
if (units.format === "docx") {
|
|
63
|
+
if (view?.durable) out.view = { ...resolveDurable(units.paragraphs, view.durable), offsetPx: view.offsetPx, zoom: view.zoom, fraction: view.fraction };
|
|
64
|
+
} else out.view = resolveView(units, view);
|
|
65
|
+
if (units.format === "xlsx") out.sheets = units.wb.sheets.map((x) => ({ index: x.index, name: x.name, sheetId: x.sheetId }));
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* When the work did not finish (its time limit, a crash, too many at once), a comment still goes out, weaker, the way a spent budget
|
|
71
|
+
* leaves it: an Excel anchor keeps its sheet and address, with no hash and no context (src/xlsx.js resolves such an anchor by its
|
|
72
|
+
* address); a Word or PowerPoint one is "unresolved location" with the page's quote (its place in the paragraph needs the file). Never an
|
|
73
|
+
* error that loses the comment.
|
|
74
|
+
*/
|
|
75
|
+
export function weakDurable(format, anchor) {
|
|
76
|
+
const loc = anchor?.location;
|
|
77
|
+
// HTML: the page's quote, its context and the node id it named (a hint for that version only): resolved by searching the quote
|
|
78
|
+
// (src/html.js resolveHtml), never by a path it does not have
|
|
79
|
+
if (format === "html") {
|
|
80
|
+
if (!loc || !Number.isInteger(loc.nodeId)) return null;
|
|
81
|
+
const quote = anchor.hitKind ? "" : cutText(normText(String(anchor.quote ?? "")), 2000);
|
|
82
|
+
const text = anchor.hitKind ? cutText(normText(String(anchor.quote ?? "")), 2000) : "";
|
|
83
|
+
return { format: "html", nodeId: loc.nodeId, path: null, tag: null, elementId: null, kind: "element", flatAt: 0, textHash: null, text, quote, prefix: cutText(normText(String(anchor.prefix ?? "")).slice(-40), 40), suffix: cutText(normText(String(anchor.suffix ?? "")), 40), start: null, end: null, heading: "", fallback: true };
|
|
84
|
+
}
|
|
85
|
+
if (format !== "xlsx" || !loc || typeof loc.sheetName !== "string") return null;
|
|
86
|
+
const range = typeof loc.range === "string" ? cutText(loc.range, 40) : "";
|
|
87
|
+
const base = { format: "xlsx", sheetId: null, sheetName: cutText(loc.sheetName, 200), sheetIndex: Number.isInteger(loc.sheetIndex) ? loc.sheetIndex : 0, range, textHash: null, names: [], weak: true };
|
|
88
|
+
const q = parseA1(range);
|
|
89
|
+
if (!q) return { ...base, cell: false, text: "", ctx: null };
|
|
90
|
+
return { ...base, cell: true, r0: q.r0, c0: q.c0, r1: q.r1, c1: q.c1, text: "", values: [], rangeHash: null, ctx: { rowHeader: "", colHeader: "", left: "", right: "", up: "", down: "", rowCells: [] } };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function one(units, t, bytes, sha) {
|
|
94
|
+
switch (t.op) {
|
|
95
|
+
case "build":
|
|
96
|
+
return built(units, t.anchor, t.pin);
|
|
97
|
+
case "reanchor":
|
|
98
|
+
return reanchor(units, t.c);
|
|
99
|
+
case "view":
|
|
100
|
+
return viewOf(units, t.vs);
|
|
101
|
+
case "use": {
|
|
102
|
+
// a quick fix's Use: where the stored suggestion goes in these bytes, and the one reply that writes it (the widening's searches are paid from the same budget)
|
|
103
|
+
const d = buildDurable(units, t.anchor);
|
|
104
|
+
const p = placeOf(units, d, t.rec.quote);
|
|
105
|
+
return { durable: d, place: { ok: p.ok, code: p.code }, reply: p.ok ? buildUseReply(t.rec, t.id, p.text) : null };
|
|
106
|
+
}
|
|
107
|
+
case "select":
|
|
108
|
+
return selectionOf(units, buildDurable(units, t.anchor), t.anchor, t.picked, t.limits);
|
|
109
|
+
case "resolveAll":
|
|
110
|
+
return resolveAllOf(units, t.comments, t.view);
|
|
111
|
+
case "cards":
|
|
112
|
+
return cardsFrom(t.list, { bytes, sha, format: units.format, units: t.units, index: t.index, reviewId: t.reviewId });
|
|
113
|
+
default:
|
|
114
|
+
throw new Error(`unknown task ${String(t.op).slice(0, 40)}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One request's work on one version of the file. `bytes` is the version (a Buffer, or a function that returns it, read only when the
|
|
120
|
+
* reading of `sha` is not kept); `validate` runs the package check first (bytes a request carried); `check` reads every part within the
|
|
121
|
+
* readers' caps first (bytes about to become the file); `tasks` is a
|
|
122
|
+
* list of {op, ...}; `outline` adds the outline of the file for an agent Onionskin started. Returns {read: {ok: true} | {cap} | {invalid},
|
|
123
|
+
* results: one per task (a task that throws gives {error})}, and `outline` ({outline, more} of src/review.js outlineOf, or null).
|
|
124
|
+
*/
|
|
125
|
+
export function anchorWork({ format, bytes, sha, check = false, validate = false, tasks = [], outline = false, budget }) {
|
|
126
|
+
let units;
|
|
127
|
+
try {
|
|
128
|
+
// bytes from a request: the package check first, as the open worker does (an HTML file has no package: readHtml checks its size,
|
|
129
|
+
// encoding and limits as it reads it)
|
|
130
|
+
if (validate && format !== "html") validatePackage(bytes, format);
|
|
131
|
+
units = unitsFor(format, bytes, sha);
|
|
132
|
+
if (check) readAll(units);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
return { read: err instanceof CapError ? { cap: err.message } : { invalid: String(err?.message || err).slice(0, 200) }, results: tasks.map(() => ({ error: "unreadable" })) };
|
|
135
|
+
}
|
|
136
|
+
const results = scanBudget(budget, () =>
|
|
137
|
+
tasks.map((t) => {
|
|
138
|
+
try {
|
|
139
|
+
return one(units, t, bytes, sha);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
return { error: String(err?.message || err).slice(0, 200) };
|
|
142
|
+
}
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
let all = null;
|
|
146
|
+
if (outline) {
|
|
147
|
+
try {
|
|
148
|
+
all = outlineOf(format, typeof bytes === "function" ? null : bytes, OUTLINE_FILE_MAX, units);
|
|
149
|
+
} catch {
|
|
150
|
+
all = null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { read: { ok: true }, results, ...(outline ? { outline: all } : {}) };
|
|
154
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { anchorWork } from "./anchor-work.js";
|
|
2
|
+
import "./html-parser.js"; // this worker reads HTML files: the parser lives here, never on the server's thread
|
|
3
|
+
import { env } from "./constants.js";
|
|
4
|
+
import { checkRegularOwned, readDoc, sha256 } from "./fsutil.js";
|
|
5
|
+
|
|
6
|
+
// The server's anchor worker (src/offload.js anchorsOffThread), run by src/worker-host.js and kept warm: it answers job after job, each
|
|
7
|
+
// on a version of a file, and keeps the last reading (src/anchors.js unitsFor), so an ordinary Send reads the file once. A job is
|
|
8
|
+
// {id, job: {format, source: {bytes} | {path, sha?}, check, tasks, outline}}; the answer {id, ok: true, sha, r} (src/anchor-work.js) |
|
|
9
|
+
// {id, ok: false, error}. A `path` is read here (a regular file the user owns), never on the server's thread; a `sha` it must have.
|
|
10
|
+
// The budget is ten times the server's own (src/session.js SCAN_BUDGET): all of it took 0.4 s (50 hashes of a cell of 31 million "é")
|
|
11
|
+
// to 1.3 s (500 anchors in a paragraph of decomposed letters) of CPU here, at a load average of 20. The time limit around this thread is what
|
|
12
|
+
// bounds the work, and ordinary large ranges (fifty of 1,000 x 20 cells of 30-character labels) all keep their hash.
|
|
13
|
+
// ONIONSKIN_TEST_ANCHOR_DELAY_MS (tests only) blocks this thread for that long first, the way a slow job would.
|
|
14
|
+
export const WORKER_BUDGET = 400_000_000;
|
|
15
|
+
|
|
16
|
+
function source({ bytes, path, sha }) {
|
|
17
|
+
if (bytes) return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
18
|
+
checkRegularOwned(path);
|
|
19
|
+
const b = readDoc(path);
|
|
20
|
+
if (sha && sha256(b) !== sha) throw Object.assign(new Error("the file is not the version asked for"), { stale: true });
|
|
21
|
+
return b;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function serve(port) {
|
|
25
|
+
port.on("message", ({ id, job }) => {
|
|
26
|
+
const delay = Number(env("TEST_ANCHOR_DELAY_MS", 0));
|
|
27
|
+
if (delay > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay);
|
|
28
|
+
let answer;
|
|
29
|
+
try {
|
|
30
|
+
const bytes = source(job.source);
|
|
31
|
+
const sha = job.source.sha || sha256(bytes);
|
|
32
|
+
answer = { id, ok: true, sha, r: anchorWork({ ...job, bytes, sha, validate: !!job.source.bytes, budget: WORKER_BUDGET }) };
|
|
33
|
+
} catch (err) {
|
|
34
|
+
answer = { id, ok: false, stale: !!err?.stale, error: String(err?.message || err).slice(0, 200) };
|
|
35
|
+
}
|
|
36
|
+
port.postMessage(answer);
|
|
37
|
+
});
|
|
38
|
+
}
|
package/src/anchors.js
CHANGED
|
@@ -2,24 +2,59 @@ import { normText } from "./text.js";
|
|
|
2
2
|
import { readDocxParagraphs, resolveDurable, durableFromEngine } from "./docx.js";
|
|
3
3
|
import { parseDeck, pptxDurable, resolvePptx } from "./pptx.js";
|
|
4
4
|
import { a1, a1Range, ch, parseWorkbook, resolveXlsx, sheetRows, xlsxDurable } from "./xlsx.js";
|
|
5
|
+
import { capDurable } from "./retain.js";
|
|
6
|
+
import { elementPath, htmlDurable, ownHash, pathString, readHtml, resolveHtml, visibleText } from "./html.js";
|
|
7
|
+
import { unknownFormat } from "./constants.js";
|
|
5
8
|
|
|
6
9
|
// One seam per format for the three things the server does with the bytes it holds: read the units, build a durable anchor from an
|
|
7
|
-
// engine location (at Send) and re-resolve one against newer bytes (after `done`). DOCX lives in docx.js, XLSX in xlsx.js, PPTX in pptx.js
|
|
10
|
+
// engine location (at Send) and re-resolve one against newer bytes (after `done`). DOCX lives in docx.js, XLSX in xlsx.js, PPTX in pptx.js,
|
|
11
|
+
// HTML in html.js (whose parser exists only in a worker thread: src/html-parser.js; on the server's thread reading an HTML file throws).
|
|
8
12
|
|
|
9
13
|
export function readUnits(format, bytes) {
|
|
10
14
|
if (format === "docx") return { format, paragraphs: readDocxParagraphs(bytes) };
|
|
11
15
|
if (format === "xlsx") return { format, wb: parseWorkbook(bytes) };
|
|
12
16
|
if (format === "pptx") return { format, deck: parseDeck(bytes) };
|
|
13
|
-
|
|
17
|
+
if (format === "html") return { format, read: readHtml(bytes) };
|
|
18
|
+
return unknownFormat(format, "readUnits");
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
/**
|
|
21
|
+
/** Reads the lazy parts of `units` too (every slide, every sheet), so a version past a cap is refused now, not by a later reader. */
|
|
22
|
+
export function readAll(units) {
|
|
23
|
+
if (units.format === "pptx") units.deck.ensure();
|
|
24
|
+
if (units.format === "xlsx") for (const sheet of units.wb.sheets) units.wb.sheetCells(sheet);
|
|
25
|
+
return units;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The last version read, by its content hash: the server reads one version several times in a row (a Send, then the outline of the batch
|
|
29
|
+
// it delivers; the end of a run, then the batch it queues and the page's next resolve), so it is read once. Consumers never change the
|
|
30
|
+
// units (lazy caches on them are keyed by the same content). Dropped after KEEP_MS so a large file is not held in memory for nothing.
|
|
31
|
+
// Ceiling: one entry; two sessions read in turn read each other's versions again.
|
|
32
|
+
const KEEP_MS = 60_000;
|
|
33
|
+
let kept = null;
|
|
34
|
+
/** readUnits of the version whose sha256 is `sha`, reusing the last reading of it; `bytes` is the content or a function that returns it. */
|
|
35
|
+
export function unitsFor(format, bytes, sha) {
|
|
36
|
+
if (kept && kept.format === format && kept.sha === sha) return kept.units;
|
|
37
|
+
const units = readUnits(format, typeof bytes === "function" ? bytes() : bytes);
|
|
38
|
+
if (kept) clearTimeout(kept.timer);
|
|
39
|
+
kept = { format, sha, units, timer: setTimeout(() => (kept = null), KEEP_MS) };
|
|
40
|
+
kept.timer.unref?.();
|
|
41
|
+
return units;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The browser's captured anchor ({location, quote, prefix, suffix, paraText, hitKind}) -> durable anchor from the written bytes, or null.
|
|
46
|
+
* A page anchor whose quote was cut where it was stored (src/retain.js, `cut`) is the paragraph or shape it sits in: a range of the cut
|
|
47
|
+
* text would point at a shorter one. What is built is cut to the stored caps (src/retain.js capDurable).
|
|
48
|
+
*/
|
|
17
49
|
export function buildDurable(units, anchor) {
|
|
18
50
|
if (!anchor?.location) return null;
|
|
19
51
|
const { format } = units;
|
|
20
|
-
|
|
21
|
-
if (format === "
|
|
22
|
-
|
|
52
|
+
const whole = anchor.cut === true;
|
|
53
|
+
if (format === "docx") return capDurable(durableFromEngine(units.paragraphs, anchor.location, { quote: anchor.paraText || whole ? "" : anchor.quote, prefix: anchor.prefix, suffix: anchor.suffix, paraText: anchor.paraText }));
|
|
54
|
+
if (format === "xlsx") return capDurable(xlsxDurable(units.wb, anchor.location));
|
|
55
|
+
if (format === "pptx") return capDurable(pptxDurable(units.deck, anchor.location, { quote: anchor.quote, prefix: anchor.prefix, suffix: anchor.suffix, text: anchor.quote, shapeWide: !!anchor.hitKind || whole }));
|
|
56
|
+
if (format === "html") return capDurable(htmlDurable(units.read, anchor));
|
|
57
|
+
return unknownFormat(format, "buildDurable");
|
|
23
58
|
}
|
|
24
59
|
|
|
25
60
|
/** Re-resolve a stored durable anchor against new units. DOCX results keep their own shape (see docx.js). */
|
|
@@ -27,7 +62,9 @@ export function resolveDurableAny(units, durable) {
|
|
|
27
62
|
if (!durable) return { method: "none" };
|
|
28
63
|
if (units.format === "docx") return resolveDurable(units.paragraphs, durable);
|
|
29
64
|
if (units.format === "xlsx") return resolveXlsx(units.wb, durable);
|
|
30
|
-
return resolvePptx(units.deck, durable);
|
|
65
|
+
if (units.format === "pptx") return resolvePptx(units.deck, durable);
|
|
66
|
+
if (units.format === "html") return resolveHtml(units.read, durable);
|
|
67
|
+
return unknownFormat(units.format, "resolveDurableAny");
|
|
31
68
|
}
|
|
32
69
|
|
|
33
70
|
/**
|
|
@@ -144,8 +181,10 @@ export function changedRegions(format, preBytes, postBytes) {
|
|
|
144
181
|
} else if (format === "xlsx") {
|
|
145
182
|
const A = parseWorkbook(preBytes);
|
|
146
183
|
const B = parseWorkbook(postBytes);
|
|
184
|
+
const namesB = new Map(); // sheet name -> how many sheets of B have it
|
|
185
|
+
for (const y of B.sheets) namesB.set(y.name, (namesB.get(y.name) || 0) + 1);
|
|
147
186
|
for (const sb of B.sheets) {
|
|
148
|
-
const sa = A.sheets.find((x) => x.name === sb.name) || A.sheets.find((x) => x.sheetId === sb.sheetId && !
|
|
187
|
+
const sa = A.sheets.find((x) => x.name === sb.name) || A.sheets.find((x) => x.sheetId === sb.sheetId && !(namesB.get(x.name) > (x.name === sb.name ? 1 : 0)));
|
|
149
188
|
const rowsB = sheetRows(B, sb);
|
|
150
189
|
if (!sa) {
|
|
151
190
|
for (const row of rowsB) push({ kind: "added", sheetId: sb.sheetId, sheetName: sb.name, range: a1Range({ r0: row.r, c0: row.cells[0].c, r1: row.r, c1: row.cells[row.cells.length - 1].c }) });
|
|
@@ -173,18 +212,29 @@ export function changedRegions(format, preBytes, postBytes) {
|
|
|
173
212
|
const B = parseDeck(postBytes);
|
|
174
213
|
A.ensure();
|
|
175
214
|
B.ensure();
|
|
215
|
+
const slidesA = new Map();
|
|
216
|
+
for (const x of A.slides) if (!slidesA.has(x.id)) slidesA.set(x.id, x);
|
|
176
217
|
for (const sb of B.slides) {
|
|
177
|
-
const sa =
|
|
218
|
+
const sa = slidesA.get(sb.id);
|
|
178
219
|
if (!sa) {
|
|
179
220
|
push({ kind: "added", slideId: sb.id });
|
|
180
221
|
continue;
|
|
181
222
|
}
|
|
223
|
+
const inA = new Map(); // id#ordinal -> the first such shape of the slide before, built once per slide
|
|
224
|
+
for (const x of sa.shapes) if (!inA.has(`${x.id}#${x.ordinal}`)) inA.set(`${x.id}#${x.ordinal}`, x);
|
|
182
225
|
for (const sh of sb.shapes) {
|
|
183
|
-
const old =
|
|
226
|
+
const old = inA.get(`${sh.id}#${sh.ordinal}`);
|
|
184
227
|
if (!old) push({ kind: "added", slideId: sb.id, shapeId: sh.id });
|
|
185
228
|
else if (old.hash !== sh.hash) push({ kind: "changed", slideId: sb.id, shapeId: sh.id });
|
|
186
229
|
}
|
|
187
230
|
}
|
|
188
|
-
}
|
|
231
|
+
} else if (format === "html") {
|
|
232
|
+
// elements that hold text (and images, other leaves) by tag and own text, in order: what is new or different in the post version
|
|
233
|
+
const A = readHtml(preBytes);
|
|
234
|
+
const B = readHtml(postBytes);
|
|
235
|
+
const key = (u) => `${u.tag}\u0000${ownHash(u.node)}`;
|
|
236
|
+
const { kind } = diffSeq(A.units.map(key), B.units.map(key));
|
|
237
|
+
B.units.forEach((u, i) => kind[i] !== "same" && push({ kind: kind[i], nodeId: u.nodeId, path: pathString(elementPath(u.node)), text: normText(visibleText(B, u.node)).slice(0, 160) }));
|
|
238
|
+
} else unknownFormat(format, "changedRegions");
|
|
189
239
|
return { items, truncated: items.length >= MAX_CHANGES };
|
|
190
240
|
}
|
package/src/apply.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import {
|
|
3
|
+
import { MAX_DOC_BYTES, PRODUCT, REFUSED_EXTENSIONS, env, formatOfExt } from "./constants.js";
|
|
4
4
|
import { checkRegularOwned, readDoc, replaceGuarded, sha256, Snapshots, WriteConflict, writeSiblingCopy } from "./fsutil.js";
|
|
5
5
|
import { hasDelivery, loadTicketRecord, parseTicket } from "./lease.js";
|
|
6
|
+
import { NOT_YOUR_FILE, agentMayUse, isAgentHolder } from "./agent-detect.js";
|
|
6
7
|
import { validatePackage } from "./package.js";
|
|
7
8
|
import { cleanFlag, hasControlChars } from "./sanitize.js";
|
|
8
9
|
import crypto from "node:crypto";
|
|
9
10
|
import { ensureStateDir, sessionKey } from "./paths.js";
|
|
10
11
|
import { demote, summarize, runOps } from "./ops/run.js";
|
|
12
|
+
import { htmlWriteOffThread } from "./offload.js";
|
|
11
13
|
import { validateOps } from "./ops/schema.js";
|
|
12
14
|
|
|
13
15
|
export const APPLY_EXIT = { ok: 0, error: 1, invalid: 2, not_applied: 3, partial: 4, write_refused: 5 };
|
|
@@ -61,6 +63,12 @@ function readJsonFile(file, what) {
|
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
|
|
66
|
+
/** The format of a file by its extension: an HTML file (.html or .htm) is "html"; null when Onionskin does not edit that kind of file. */
|
|
67
|
+
export function formatOfPath(real) {
|
|
68
|
+
return formatOfExt(path.extname(real));
|
|
69
|
+
}
|
|
70
|
+
const ONLY = "Only .docx, .xlsx, .pptx, .html and .htm files are supported";
|
|
71
|
+
|
|
64
72
|
const anchorMap = (batch) => new Map((batch?.untrusted_document_evidence?.anchors || []).map((a) => [a.id, a]));
|
|
65
73
|
|
|
66
74
|
/**
|
|
@@ -83,13 +91,60 @@ function outsideChangeOf(j) {
|
|
|
83
91
|
|
|
84
92
|
const refusal = (message) => new ApplyError(message, APPLY_EXIT.write_refused);
|
|
85
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Who calls, checked before the file is read: an agent Onionskin started (ONIONSKIN_HOLDER onionskin-bg-* or onionskin-term-*) acts only on
|
|
96
|
+
* its own file and only as itself (no --holder of another). Returns the holder that goes with the claim: --holder, else ONIONSKIN_HOLDER,
|
|
97
|
+
* else the holder the server saved with the ticket.
|
|
98
|
+
*/
|
|
99
|
+
function callerHolder(a, real, ticket) {
|
|
100
|
+
const envHolder = env("HOLDER", "");
|
|
101
|
+
if (!agentMayUse(envHolder, real)) throw refusal(NOT_YOUR_FILE);
|
|
102
|
+
if (isAgentHolder(envHolder) && a.holder && a.holder !== envHolder) throw refusal("--holder cannot name another holder for an agent Onionskin started. Nothing was written.");
|
|
103
|
+
return a.holder || envHolder || (ticket ? loadTicketRecord(ticket.batch_id)?.holder : "") || "";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The server's state for the file, asked before the file is read. With a ticket the delivered batch is handed out only to its holder, and a
|
|
108
|
+
* ticket the server does not accept ends the command here, with no op run against the file. Returns {up, session, batch}.
|
|
109
|
+
*/
|
|
110
|
+
async function sessionState({ server, real, holder, ticket, wantBatch, report = "apply" }) {
|
|
111
|
+
const up = await server.healthy();
|
|
112
|
+
if (!up) return { up, session: null, batch: null };
|
|
113
|
+
const q = holder ? `&holder=${encodeURIComponent(holder)}` : "";
|
|
114
|
+
const r = await server.req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(real)}${q}` });
|
|
115
|
+
const body = (t) => {
|
|
116
|
+
try {
|
|
117
|
+
return server.json(t) || {};
|
|
118
|
+
} catch {
|
|
119
|
+
return {};
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
if (r.status === 403 && body(r.text).code === "not_your_file") throw refusal(NOT_YOUR_FILE);
|
|
123
|
+
const session = r.status === 200 ? server.json(r.text) : null;
|
|
124
|
+
if (!session) return { up, session, batch: null };
|
|
125
|
+
if (ticket) {
|
|
126
|
+
const b = await server.req({ method: "GET", path: `/cli/batch?file=${encodeURIComponent(real)}${q}&batch_id=${encodeURIComponent(ticket.batch_id)}&lease_token=${encodeURIComponent(ticket.token)}` });
|
|
127
|
+
if (b.status !== 200) {
|
|
128
|
+
const j = body(b.text);
|
|
129
|
+
if (j.code === "not_your_file") throw refusal(NOT_YOUR_FILE);
|
|
130
|
+
const note = j.code === "missing_lease" ? `Cannot tell which holder this ticket belongs to: set ${PRODUCT.envPrefix}_HOLDER or pass --holder. Nothing was written.` : `The server does not accept this ticket for this file (${j.code === "no_batch" ? "no batch is delivered" : "it is not the current delivery's"}). Nothing was written. Run \`${PRODUCT.bin} status <file> --ticket\` (it answers only the agent that polled this batch) or poll again.`;
|
|
131
|
+
// the same write_error a refused write reports, from the server's own content-free words; no op ran, so there are no results
|
|
132
|
+
throw new ApplyError(note, APPLY_EXIT.write_refused, { schema: `onionskin.${report}-report/1`, file: real, written: false, write_path: "none", note, write_error: { status: b.status, code: typeof j.code === "string" ? j.code : null, message: typeof j.error === "string" ? j.error.slice(0, 300) : `server answered ${b.status}` } });
|
|
133
|
+
}
|
|
134
|
+
return { up, session, batch: server.json(b.text).batch };
|
|
135
|
+
}
|
|
136
|
+
if (!wantBatch) return { up, session, batch: null };
|
|
137
|
+
const b = await server.req({ method: "GET", path: `/cli/batch?file=${encodeURIComponent(real)}${q}` });
|
|
138
|
+
return { up, session, batch: b.status === 200 ? server.json(b.text).batch : null };
|
|
139
|
+
}
|
|
140
|
+
|
|
86
141
|
/**
|
|
87
142
|
* Where a write may go, and under which credentials. Fails closed: a ticket, a delivered batch or a saved delivery for the file means the
|
|
88
143
|
* write goes through the live session or does not happen. Standalone writes (no review session at all) need the explicit --standalone flag
|
|
89
144
|
* and are refused while a live session or a saved delivery exists for the file.
|
|
90
145
|
* Returns {via: "session", claim} or {via: "standalone"}; throws ApplyError(write_refused).
|
|
91
146
|
*/
|
|
92
|
-
async function resolveWriteTarget({ a, real,
|
|
147
|
+
async function resolveWriteTarget({ a, real, session, up, ticket, holder }) {
|
|
93
148
|
const live = !!session && session.state !== "closed";
|
|
94
149
|
const delivered = live && ["working", "queued"].includes(session.state);
|
|
95
150
|
const saved = hasDelivery(real);
|
|
@@ -100,7 +155,6 @@ async function resolveWriteTarget({ a, real, server, session, up, ticket }) {
|
|
|
100
155
|
if (session.state === "closed") throw refusal(`The session for this file is closed; the delivery named by --lease no longer exists. Nothing was written.`);
|
|
101
156
|
if (!delivered) throw refusal(`This file has an open ${PRODUCT.name} session in state "${session.state}": no batch is delivered, so the ticket is void. Nothing was written. Run \`${PRODUCT.bin} poll\` and wait for the user's Send.`);
|
|
102
157
|
if (session.delivered_batch !== ticket.batch_id) throw refusal(`The ticket belongs to batch ${ticket.batch_id}, but the delivered batch is ${session.delivered_batch}: your delivery was taken back, finished or handed on. Nothing was written. Run \`${PRODUCT.bin} poll\` again.`);
|
|
103
|
-
const holder = a.holder || env("HOLDER", "") || loadTicketRecord(ticket.batch_id)?.holder;
|
|
104
158
|
if (!holder) throw refusal(`Cannot tell which holder this ticket belongs to: set ${PRODUCT.envPrefix}_HOLDER or pass --holder. Nothing was written.`);
|
|
105
159
|
return { via: "session", claim: { holder, lease_token: ticket.token, batch_id: ticket.batch_id } };
|
|
106
160
|
}
|
|
@@ -163,13 +217,24 @@ export async function applyCommand(rest, server, inline = null) {
|
|
|
163
217
|
if (hasControlChars(real)) throw new ApplyError("This file's path contains control or bidirectional-text characters. Rename or move the file and run apply again.");
|
|
164
218
|
const ext = path.extname(real).toLowerCase();
|
|
165
219
|
if (REFUSED_EXTENSIONS.has(ext)) throw new ApplyError(`${ext} (macro-enabled) files are refused`);
|
|
166
|
-
const format =
|
|
167
|
-
if (!
|
|
220
|
+
const format = formatOfPath(real);
|
|
221
|
+
if (!format) throw new ApplyError(ONLY);
|
|
168
222
|
|
|
169
223
|
const doc = inline ? inline.ops : readJsonFile(a.ops, "ops file");
|
|
170
224
|
const v = validateOps(doc);
|
|
171
225
|
if (!v.ok) throw new ApplyError("the ops file does not match the schema", APPLY_EXIT.invalid, { schema: "onionskin.apply-report/1", status: "invalid_ops", errors: v.errors });
|
|
172
226
|
|
|
227
|
+
// the only credential is the ticket (--lease): not the batch JSON, not whatever the last poll of this file saved
|
|
228
|
+
const ticket = parseTicket(a.lease);
|
|
229
|
+
if (a.lease && !ticket) throw new ApplyError("--lease wants the value printed in the batch's commands (<batch id>:<token>)", APPLY_EXIT.invalid);
|
|
230
|
+
if (ticket && a.standalone) throw new ApplyError("--lease and --standalone contradict each other", APPLY_EXIT.invalid);
|
|
231
|
+
// authorization before the file is read: who calls, what the server holds for the file, and (with a ticket) that the server accepts it.
|
|
232
|
+
// Nothing below reads, parses or runs an op on the document until then, so a refusal carries nothing of it.
|
|
233
|
+
const holder = callerHolder(a, real, ticket);
|
|
234
|
+
const st = await sessionState({ server, real, holder, ticket, wantBatch: !inline?.batch && !a.batch });
|
|
235
|
+
const { up, session } = st;
|
|
236
|
+
const batch = inline?.batch ?? (a.batch ? readJsonFile(a.batch, "batch file") : st.batch);
|
|
237
|
+
|
|
173
238
|
checkRegularOwned(real);
|
|
174
239
|
let bytes;
|
|
175
240
|
try {
|
|
@@ -178,35 +243,32 @@ export async function applyCommand(rest, server, inline = null) {
|
|
|
178
243
|
if (err.code === "TOO_LARGE") throw new ApplyError(`document too large: ${err.message}`);
|
|
179
244
|
throw err;
|
|
180
245
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
246
|
+
// an HTML file is no package: the driver reads it in the ops worker and refuses every op of a file it cannot read (src/ops/html.js)
|
|
247
|
+
if (format !== "html") {
|
|
248
|
+
try {
|
|
249
|
+
validatePackage(bytes, format);
|
|
250
|
+
} catch (err) {
|
|
251
|
+
throw new ApplyError(`Not a valid .${format}: ${err.message}`);
|
|
252
|
+
}
|
|
185
253
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (up) {
|
|
193
|
-
const r = await server.req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(real)}` });
|
|
194
|
-
if (r.status === 200) session = server.json(r.text);
|
|
195
|
-
if (session && !batch) {
|
|
196
|
-
const b = await server.req({ method: "GET", path: `/cli/batch?file=${encodeURIComponent(real)}` });
|
|
197
|
-
if (b.status === 200) batch = server.json(b.text).batch;
|
|
254
|
+
// a run inside the server (src/local.js) reads the bytes within the readers' caps before the engine does, on the server's thread
|
|
255
|
+
if (server.checkBytes && format !== "html") {
|
|
256
|
+
try {
|
|
257
|
+
server.checkBytes(bytes, format);
|
|
258
|
+
} catch (err) {
|
|
259
|
+
throw new ApplyError(String(err?.message || err)); // nothing ran: the local runner hands the batch to the agent
|
|
198
260
|
}
|
|
199
261
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (a.lease && !ticket) throw new ApplyError("--lease wants the value printed in the batch's commands (<batch id>:<token>)", APPLY_EXIT.invalid);
|
|
203
|
-
if (ticket && a.standalone) throw new ApplyError("--lease and --standalone contradict each other", APPLY_EXIT.invalid);
|
|
262
|
+
const baseSha = sha256(bytes);
|
|
263
|
+
// where the write must go (no op has run yet): through the server when a session tracks this file (it owns the lease, the compare-and-swap and the snapshots)
|
|
204
264
|
let target = { via: "none" };
|
|
205
|
-
if (!a.dryRun) target = await resolveWriteTarget({ a, real,
|
|
265
|
+
if (!a.dryRun) target = await resolveWriteTarget({ a, real, session, up, ticket, holder });
|
|
206
266
|
const claim = target.claim;
|
|
207
267
|
const viaSession = target.via === "session";
|
|
208
268
|
|
|
209
|
-
|
|
269
|
+
// a run inside the server (src/local.js) has the engine work in a worker thread (server.runOps), off the server's own thread
|
|
270
|
+
const run = server.runOps ?? runOps;
|
|
271
|
+
const { results: ran, bytes: out, warnings, blocked } = await run({ bytes, format, name: path.basename(real), ops: doc.ops, partial: a.partial, anchors: anchorMap(batch) });
|
|
210
272
|
const results = ran;
|
|
211
273
|
const refuseWrite = (note, extra = {}) => {
|
|
212
274
|
// nothing reached disk, so no op may read `applied`
|
|
@@ -245,7 +307,7 @@ export async function applyCommand(rest, server, inline = null) {
|
|
|
245
307
|
// what a real run would do: atomic writes only when every op applies, --partial writes the ops that apply
|
|
246
308
|
const writes = (r) => !!r.bytes && r.results.some((x) => x.status === "applied");
|
|
247
309
|
const wouldWrite = a.partial ? writes({ bytes: out, results }) : failed === 0 && !!out;
|
|
248
|
-
const wouldWritePartial = a.partial ? wouldWrite : failed === 0 ? wouldWrite : writes(await
|
|
310
|
+
const wouldWritePartial = a.partial ? wouldWrite : failed === 0 ? wouldWrite : writes(await run({ bytes, format, name: path.basename(real), ops: doc.ops, partial: true, anchors: anchorMap(batch) }));
|
|
249
311
|
report.would_write = wouldWrite;
|
|
250
312
|
report.would_write_partial = wouldWritePartial;
|
|
251
313
|
report.note = `dry run: nothing was written. A real run in ${report.mode} mode would ${wouldWrite ? "write the file" : "write nothing"}${a.partial ? "" : `; with --partial it would ${wouldWritePartial ? "write the ops that apply" : "write nothing"}`}.`;
|
|
@@ -312,7 +374,7 @@ export function parseCommitArgs(rest) {
|
|
|
312
374
|
* The base the candidate was edited from is, unless --base says otherwise, the hash the server last wrote or delivered for the batch;
|
|
313
375
|
* if the file moved since, the commit is refused with `conflict` and the agent starts over from a fresh copy.
|
|
314
376
|
*/
|
|
315
|
-
export async function commitCommand(rest, server) {
|
|
377
|
+
export async function commitCommand(rest, server, { check = htmlWriteOffThread } = {}) {
|
|
316
378
|
const a = parseCommitArgs(rest);
|
|
317
379
|
let real;
|
|
318
380
|
try {
|
|
@@ -323,35 +385,34 @@ export async function commitCommand(rest, server) {
|
|
|
323
385
|
if (hasControlChars(real)) throw new ApplyError("This file's path contains control or bidirectional-text characters. Rename or move the file and run commit again.");
|
|
324
386
|
const ext = path.extname(real).toLowerCase();
|
|
325
387
|
if (REFUSED_EXTENSIONS.has(ext)) throw new ApplyError(`${ext} (macro-enabled) files are refused`);
|
|
326
|
-
const format =
|
|
327
|
-
if (!
|
|
388
|
+
const format = formatOfPath(real);
|
|
389
|
+
if (!format) throw new ApplyError(ONLY);
|
|
328
390
|
let candidatePath;
|
|
329
391
|
try {
|
|
330
392
|
candidatePath = fs.realpathSync(path.resolve(a.from));
|
|
331
393
|
} catch {
|
|
332
394
|
throw new ApplyError(`candidate file not found: ${JSON.stringify(a.from.slice(0, 200))}`, APPLY_EXIT.invalid);
|
|
333
395
|
}
|
|
396
|
+
const ticket = parseTicket(a.lease);
|
|
397
|
+
if (a.lease && !ticket) throw new ApplyError("--lease wants the value printed in the batch's commands (<batch id>:<token>)", APPLY_EXIT.invalid);
|
|
398
|
+
if (ticket && a.standalone) throw new ApplyError("--lease and --standalone contradict each other", APPLY_EXIT.invalid);
|
|
399
|
+
const holder = callerHolder(a, real, ticket);
|
|
334
400
|
if (candidatePath === real) throw new ApplyError("--from is the document itself: edit a COPY (cp the file to a temp path first) and commit that", APPLY_EXIT.invalid);
|
|
335
401
|
const cst = fs.statSync(candidatePath);
|
|
336
402
|
if (!cst.isFile() || cst.size > MAX_DOC_BYTES) throw new ApplyError(`candidate ${a.from} is not a regular file under ${MAX_DOC_BYTES / 1024 / 1024} MB`, APPLY_EXIT.invalid);
|
|
337
403
|
if (a.base && !/^[0-9a-f]{64}$/.test(a.base)) throw new ApplyError("--base wants a sha256 in lower-case hex", APPLY_EXIT.invalid);
|
|
338
404
|
const candidate = fs.readFileSync(candidatePath);
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
405
|
+
// an HTML candidate is checked as a whole new version (src/html-write.js): by the server for a session, below for a standalone commit
|
|
406
|
+
if (format !== "html") {
|
|
407
|
+
try {
|
|
408
|
+
validatePackage(candidate, format);
|
|
409
|
+
} catch (err) {
|
|
410
|
+
throw new ApplyError(`The candidate is not a valid .${format}: ${err.message}. Nothing was written.`, APPLY_EXIT.write_refused);
|
|
411
|
+
}
|
|
343
412
|
}
|
|
344
|
-
const ticket = parseTicket(a.lease);
|
|
345
|
-
if (a.lease && !ticket) throw new ApplyError("--lease wants the value printed in the batch's commands (<batch id>:<token>)", APPLY_EXIT.invalid);
|
|
346
|
-
if (ticket && a.standalone) throw new ApplyError("--lease and --standalone contradict each other", APPLY_EXIT.invalid);
|
|
347
413
|
|
|
348
|
-
|
|
349
|
-
const
|
|
350
|
-
if (up) {
|
|
351
|
-
const r = await server.req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(real)}` });
|
|
352
|
-
if (r.status === 200) session = server.json(r.text);
|
|
353
|
-
}
|
|
354
|
-
const target = await resolveWriteTarget({ a, real, server, session, up, ticket });
|
|
414
|
+
const { up, session } = await sessionState({ server, real, holder, ticket, wantBatch: false, report: "commit" });
|
|
415
|
+
const target = await resolveWriteTarget({ a, real, session, up, ticket, holder });
|
|
355
416
|
const report = { schema: "onionskin.commit-report/1", file: real, format, written: false, write_path: target.via, candidate_sha256: sha256(candidate) };
|
|
356
417
|
if (target.via === "session") {
|
|
357
418
|
const w = await server.req({ method: "POST", path: "/cli/commit" }, { file: real, bytes_b64: candidate.toString("base64"), ...(a.base ? { base_sha256: a.base } : {}), ...target.claim });
|
|
@@ -361,9 +422,20 @@ export async function commitCommand(rest, server) {
|
|
|
361
422
|
}
|
|
362
423
|
if (!a.base) throw new ApplyError("a standalone commit needs --base <sha256 of the file the candidate was edited from> so a newer version is never overwritten", APPLY_EXIT.invalid);
|
|
363
424
|
checkRegularOwned(real);
|
|
364
|
-
const
|
|
425
|
+
const disk = readDoc(real);
|
|
426
|
+
let out = candidate;
|
|
427
|
+
if (format === "html") {
|
|
428
|
+
// R1, R12: the same check as the server's, in a worker, against exactly the bytes whose hash is --base (a disk that differs now and
|
|
429
|
+
// is put back before the write would otherwise let a change the base forbids through); the compare-and-swap of standaloneWrite runs
|
|
430
|
+
// after the await, against --base again. A refusal here writes no copy: the candidate was never checked.
|
|
431
|
+
if (sha256(disk) !== a.base) return { exit: APPLY_EXIT.write_refused, report: { ...report, note: "The file on disk is not the version --base names (someone changed it outside Onionskin); nothing was checked and nothing was written.", status: "outside_change", outside_change: { instruction: OUTSIDE_INSTRUCTION, copy_path: null, copy_name: null, copy_error: "no copy was saved: the candidate was not checked against the version on disk" } } };
|
|
432
|
+
const c = await check({ before: disk, after: candidate });
|
|
433
|
+
if (!c.ok) return { exit: APPLY_EXIT.write_refused, report: { ...report, note: `${c.reason} Nothing was written.`, write_error: { status: 422, code: c.code, message: c.reason, ...(c.reasons ? { reasons: c.reasons } : {}) } } };
|
|
434
|
+
out = c.bytes;
|
|
435
|
+
}
|
|
436
|
+
const w = standaloneWrite(real, disk, out, a.base, "before commit (standalone)");
|
|
365
437
|
if (w.refused) return { exit: APPLY_EXIT.write_refused, report: { ...report, note: w.refused, ...(w.outside ? { status: "outside_change", outside_change: w.outside } : {}) } };
|
|
366
|
-
return { exit: APPLY_EXIT.ok, report: { ...report, written: true, new_sha256:
|
|
438
|
+
return { exit: APPLY_EXIT.ok, report: { ...report, written: true, new_sha256: sha256(out), snapshot: w.snapshot, acl: w.acl, note: "standalone (explicit --standalone): written atomically; the previous version is snapshotted in the state dir" } };
|
|
367
439
|
}
|
|
368
440
|
|
|
369
441
|
/** The compact form of an apply or commit report (`--summary`): per-op statuses, flags, written, ok. The full report is the default. */
|