Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 164 to 165

Compare

+94 −0 in 1 place
new file, 94 lines
1+/** JSON.stringify-compatible chunks for canonical data, without a whole-value string.
2+ * Individual input strings and object key lists still belong to the caller.
3+ */
4+export function* jsonValueChunks(input: unknown): IterableIterator<string> {
5+ const active = new Set<object>();
6+ function* string(value: string): IterableIterator<string> {
7+ yield '"';
8+ for (let from = 0; from < value.length; ) {
9+ let end = Math.min(from + 16384, value.length);
10+ const last = value.charCodeAt(end - 1),
11+ next = value.charCodeAt(end);
12+ if (last >= 0xd800 && last <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) end--;
13+ yield JSON.stringify(value.slice(from, end)).slice(1, -1);
14+ from = end;
15+ }
16+ yield '"';
17+ }
18+ const normalize = (value: unknown, key: string): unknown => {
19+ if (value !== null && typeof value === "object") {
20+ const toJSON = (value as { toJSON?: unknown }).toJSON;
21+ if (typeof toJSON === "function") return toJSON.call(value, key);
22+ }
23+ return value;
24+ };
25+ const omitted = (value: unknown) => value === undefined || typeof value === "function" || typeof value === "symbol";
26+ function* encode(value: unknown): IterableIterator<string> {
27+ if (typeof value === "string") yield* string(value);
28+ else if (value === null || typeof value === "boolean" || typeof value === "number") yield JSON.stringify(value);
29+ else if (typeof value !== "object" || value === null) throw Error("unsupported canonical JSON value");
30+ else {
31+ if (active.has(value)) throw Error("cyclic canonical JSON value");
32+ if (active.size >= 128) throw Error("canonical JSON depth exceeds bound");
33+ active.add(value);
34+ try {
35+ if (Array.isArray(value)) {
36+ yield "[";
37+ const length = value.length;
38+ for (let i = 0; i < length; i++) {
39+ if (i) yield ",";
40+ const child = normalize(value[i], String(i));
41+ yield* encode(omitted(child) ? null : child);
42+ }
43+ yield "]";
44+ } else {
45+ yield "{";
46+ let first = true;
47+ for (const key of Object.keys(value)) {
48+ const child = normalize((value as Record<string, unknown>)[key], key);
49+ if (omitted(child)) continue;
50+ if (!first) yield ",";
51+ first = false;
52+ yield* string(key);
53+ yield ":";
54+ yield* encode(child);
55+ }
56+ yield "}";
57+ }
58+ } finally {
59+ active.delete(value);
60+ }
61+ }
62+ }
63+ yield* encode(normalize(input, ""));
64+}
66+/** Exact serialized-content equality, retaining only the current pair of chunks. */
67+export function equalJsonValues(left: unknown, right: unknown): boolean {
68+ const a = jsonValueChunks(left),
69+ b = jsonValueChunks(right);
70+ let x = a.next(),
71+ y = b.next(),
72+ i = 0,
73+ j = 0;
74+ try {
75+ for (;;) {
76+ while (!x.done && i === x.value.length) {
77+ x = a.next();
78+ i = 0;
79+ }
80+ while (!y.done && j === y.value.length) {
81+ y = b.next();
82+ j = 0;
83+ }
84+ if (x.done || y.done) return x.done === y.done;
85+ const length = Math.min(x.value.length - i, y.value.length - j);
86+ if (x.value.slice(i, i + length) !== y.value.slice(j, j + length)) return false;
87+ i += length;
88+ j += length;
89+ }
90+ } finally {
91+ a.return?.();
92+ b.return?.();
93+ }
94+}
MCP
compare on this site's /mcp
CLI
abso diff 164 165 src/engine/json-value-chunks.ts --json
JSON
/christopher/abso-core/compare/164..165?file=src%2Fengine%2Fjson-value-chunks.ts&format=json