@dev.fast/diffr 0.1.1
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/LICENSE +22 -0
- package/README.md +45 -0
- package/bin/fetch.mjs +86 -0
- package/dist/contract.d.ts +177 -0
- package/dist/contract.js +158 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/stream.d.ts +3 -0
- package/dist/stream.js +10 -0
- package/package.json +47 -0
- package/pins.json +7 -0
- package/src/contract.ts +372 -0
- package/src/index.ts +2 -0
- package/src/stream.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021-2025 Wilfred Hughes
|
|
4
|
+
Copyright (c) 2026 dev.fast
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @dev.fast/diffr
|
|
2
|
+
|
|
3
|
+
diffr's NDJSON wire contract as TypeScript with zod validators, plus a
|
|
4
|
+
fetcher for the matching release binary. The package version is the diffr
|
|
5
|
+
release it describes.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { decodeStructuralDiffEvent } from "@dev.fast/diffr";
|
|
9
|
+
for await (const line of lines) {
|
|
10
|
+
const event = decodeStructuralDiffEvent(line); // throws on any shape drift
|
|
11
|
+
}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx --package @dev.fast/diffr@0.1.1 diffr-fetch --into ./bin
|
|
16
|
+
npx --package @dev.fast/diffr@0.1.1 diffr-fetch --into ./bin --check
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Supports macOS arm64 and Linux x64. `--check` never downloads; `--required`
|
|
20
|
+
makes network failures fatal. Without it, network failures warn and exit 0.
|
|
21
|
+
Hash mismatches and invalid archives always fail.
|
|
22
|
+
|
|
23
|
+
## Files that change together
|
|
24
|
+
|
|
25
|
+
A wire-format change is not done until every row is updated.
|
|
26
|
+
|
|
27
|
+
| Source of truth | Mirror | Checked by |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `src/protocol/mod.rs`, `src/pairing.rs` | `npm/diffr/src/contract.ts` | `contract.test.ts` runs the built `diffr` and validates every record |
|
|
30
|
+
| `src/protocol/mod.rs` `VERSION` | `contract.ts` `STRUCTURAL_DIFF_BASE_WIRE_VERSION` | `version.test.ts` |
|
|
31
|
+
| `docs/streaming.md` | `contract.ts` doc comments | review |
|
|
32
|
+
| `src/protocol/mod.rs` | `tui/packages/hunk/src/diffr/wire.ts` (v3 only) | `tui` tests on `tui/test/fixtures/comparison.ndjson` |
|
|
33
|
+
| this package's version | consumers' exact-version dependency (Review: `packages/review-protocol` and `packages/review`) | the consumer's bump PR |
|
|
34
|
+
|
|
35
|
+
`contract.ts` imports only zod. Consumers concatenate it into generated
|
|
36
|
+
source where zod is the only external, so keep it that way.
|
|
37
|
+
|
|
38
|
+
## Releasing
|
|
39
|
+
|
|
40
|
+
1. Merge the Rust and `contract.ts` change together.
|
|
41
|
+
2. Tag the release; wait for the archives.
|
|
42
|
+
3. From `npm/diffr`, `npm run pin` writes `pins.json` for this version.
|
|
43
|
+
4. Build the Rust binary, then run `bun install --frozen-lockfile`,
|
|
44
|
+
`bun run typecheck`, and `bun test` in the package directory.
|
|
45
|
+
5. `npm publish --access public` builds the package before packing.
|
package/bin/fetch.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { parseArgs } from "node:util";
|
|
8
|
+
|
|
9
|
+
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
10
|
+
const targets = { "darwin-arm64": "aarch64-apple-darwin", "linux-x64": "x86_64-unknown-linux-gnu" };
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
await main();
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error(`diffr-fetch: ${error.message}`);
|
|
16
|
+
process.exitCode = 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function main() {
|
|
20
|
+
const { values } = parseArgs({ options: {
|
|
21
|
+
into: { type: "string" },
|
|
22
|
+
check: { type: "boolean" },
|
|
23
|
+
required: { type: "boolean" },
|
|
24
|
+
pins: { type: "string", default: join(root, "pins.json") },
|
|
25
|
+
} });
|
|
26
|
+
if (!values.into) throw new Error("--into <dir> is required");
|
|
27
|
+
const into = resolve(values.into);
|
|
28
|
+
const { version } = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
29
|
+
const pins = JSON.parse(readFileSync(values.pins, "utf8"));
|
|
30
|
+
if (pins.version !== version) throw new Error(`pins.json is for ${pins.version}, package is ${version}; run \`npm run pin\``);
|
|
31
|
+
const target = targets[`${process.platform}-${process.arch}`];
|
|
32
|
+
if (!target) throw new Error(`no diffr release for ${process.platform}-${process.arch}`);
|
|
33
|
+
const expected = pins.sha256?.[target];
|
|
34
|
+
if (typeof expected !== "string" || !/^[a-f0-9]{64}$/.test(expected)) {
|
|
35
|
+
throw new Error(`pins.json has no valid sha256 for ${target}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const binary = join(into, "diffr");
|
|
39
|
+
const stampPath = join(into, "diffr.stamp.json");
|
|
40
|
+
let installed = false;
|
|
41
|
+
try {
|
|
42
|
+
const stamp = JSON.parse(readFileSync(stampPath, "utf8"));
|
|
43
|
+
const stat = lstatSync(binary);
|
|
44
|
+
installed = stat.isFile() && (stat.mode & 0o111) !== 0 && stamp.version === version && stamp.target === target;
|
|
45
|
+
} catch { /* Missing or damaged installations are fetched again. */ }
|
|
46
|
+
if (installed) {
|
|
47
|
+
console.log(`diffr ${version} (${target}) already at ${binary}`);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (values.check) throw new Error(`${binary} is missing or not diffr ${version} (${target})`);
|
|
51
|
+
|
|
52
|
+
const asset = `diffr-${version}-${target}.tar.gz`;
|
|
53
|
+
const url = `https://github.com/devdotfast/diffr/releases/download/${version}/${asset}`;
|
|
54
|
+
let bytes;
|
|
55
|
+
try {
|
|
56
|
+
const response = await fetch(url, { redirect: "follow", signal: AbortSignal.timeout(120_000) });
|
|
57
|
+
if (!response.ok) throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
58
|
+
bytes = Buffer.from(await response.arrayBuffer());
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (values.required) throw new Error(`could not download ${url}: ${error.message}`);
|
|
61
|
+
console.warn(`diffr-fetch: could not download ${url}: ${error.message}; continuing without a bundled diffr`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const actual = createHash("sha256").update(bytes).digest("hex");
|
|
65
|
+
if (actual !== expected) throw new Error(`${asset} sha256 ${actual} does not match pinned ${expected}`);
|
|
66
|
+
|
|
67
|
+
mkdirSync(into, { recursive: true });
|
|
68
|
+
const staging = mkdtempSync(join(into, ".diffr-"));
|
|
69
|
+
try {
|
|
70
|
+
const archive = join(staging, "archive.tar.gz");
|
|
71
|
+
writeFileSync(archive, bytes);
|
|
72
|
+
const tar = spawnSync("tar", ["-xzf", archive, "-C", staging, "diffr"], { encoding: "utf8" });
|
|
73
|
+
const extracted = join(staging, "diffr");
|
|
74
|
+
if (tar.status !== 0 || !lstatSync(extracted).isFile()) {
|
|
75
|
+
throw new Error(`${asset} does not contain a regular diffr file at its root`);
|
|
76
|
+
}
|
|
77
|
+
chmodSync(extracted, 0o755);
|
|
78
|
+
const stamp = join(staging, "stamp.json");
|
|
79
|
+
writeFileSync(stamp, `${JSON.stringify({ version, target }, null, 2)}\n`);
|
|
80
|
+
renameSync(extracted, binary);
|
|
81
|
+
renameSync(stamp, stampPath);
|
|
82
|
+
} finally {
|
|
83
|
+
rmSync(staging, { recursive: true, force: true });
|
|
84
|
+
}
|
|
85
|
+
console.log(`diffr ${version} (${target}) installed at ${binary}`);
|
|
86
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-maintained mirror of diffr's wire format. These files change together:
|
|
3
|
+
* src/protocol/mod.rs and src/pairing.rs (the Rust source of truth)
|
|
4
|
+
* docs/streaming.md (the documented shapes)
|
|
5
|
+
* npm/diffr/src/contract.ts (this file)
|
|
6
|
+
* tui/packages/hunk/src/diffr/wire.ts (the terminal UI's copy, v3 only)
|
|
7
|
+
* contract.test.ts runs the built binary and validates every record against
|
|
8
|
+
* these schemas; version.test.ts checks the Rust VERSION constant.
|
|
9
|
+
*
|
|
10
|
+
* This file may import only zod: consumers concatenate it into generated
|
|
11
|
+
* source where zod is the sole external.
|
|
12
|
+
* Serde omits default/empty fields rather than sending null. Lines are zero-based,
|
|
13
|
+
* columns are UTF-8 byte offsets, and ranges are half-open.
|
|
14
|
+
*/
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
/** A pairing always has at least one side; additions/deletions omit the other. */
|
|
17
|
+
export type StructuralPairing<T> = {
|
|
18
|
+
lhs: T;
|
|
19
|
+
rhs: T;
|
|
20
|
+
} | {
|
|
21
|
+
lhs: T;
|
|
22
|
+
rhs?: never;
|
|
23
|
+
} | {
|
|
24
|
+
rhs: T;
|
|
25
|
+
lhs?: never;
|
|
26
|
+
};
|
|
27
|
+
export declare const STRUCTURAL_DIFF_WIRE_VERSION = 4;
|
|
28
|
+
/** Base stream version without --stream-annotations. */
|
|
29
|
+
export declare const STRUCTURAL_DIFF_BASE_WIRE_VERSION = 3;
|
|
30
|
+
export type StructuralDiffEvent = {
|
|
31
|
+
type: "start";
|
|
32
|
+
version: number;
|
|
33
|
+
lhs: StructuralSnapshot;
|
|
34
|
+
rhs: StructuralSnapshot;
|
|
35
|
+
files: StructuralFileChange[];
|
|
36
|
+
} | ({
|
|
37
|
+
type: "file";
|
|
38
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
39
|
+
visibility?: StructuralVisibility;
|
|
40
|
+
} & StructuralOutcome) | {
|
|
41
|
+
type: "annotations";
|
|
42
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
43
|
+
annotations: {
|
|
44
|
+
region_id: number;
|
|
45
|
+
label: string;
|
|
46
|
+
}[];
|
|
47
|
+
error?: StructuralProblem;
|
|
48
|
+
} | {
|
|
49
|
+
type: "complete";
|
|
50
|
+
succeeded: number;
|
|
51
|
+
failed: number;
|
|
52
|
+
aborted?: StructuralProblem;
|
|
53
|
+
};
|
|
54
|
+
export declare const StructuralDiffEventSchema: z.ZodType<StructuralDiffEvent>;
|
|
55
|
+
/** A file record contains exactly one result or error. */
|
|
56
|
+
export type StructuralOutcome = {
|
|
57
|
+
diff: StructuralDiff;
|
|
58
|
+
error?: never;
|
|
59
|
+
} | {
|
|
60
|
+
error: StructuralProblem;
|
|
61
|
+
diff?: never;
|
|
62
|
+
};
|
|
63
|
+
export declare const StructuralOutcomeSchema: z.ZodType<StructuralOutcome>;
|
|
64
|
+
export type StructuralSnapshot = {
|
|
65
|
+
type: "revision";
|
|
66
|
+
rev: string;
|
|
67
|
+
} | {
|
|
68
|
+
type: "index";
|
|
69
|
+
} | {
|
|
70
|
+
type: "working_tree";
|
|
71
|
+
} | {
|
|
72
|
+
type: "empty_tree";
|
|
73
|
+
} | {
|
|
74
|
+
type: "path";
|
|
75
|
+
path: string;
|
|
76
|
+
};
|
|
77
|
+
export declare const StructuralSnapshotSchema: z.ZodType<StructuralSnapshot>;
|
|
78
|
+
export type StructuralProblem = {
|
|
79
|
+
code: string;
|
|
80
|
+
message: string;
|
|
81
|
+
};
|
|
82
|
+
export declare const StructuralProblemSchema: z.ZodType<StructuralProblem>;
|
|
83
|
+
export type StructuralFileChange = {
|
|
84
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
85
|
+
status: StructuralFileStatus;
|
|
86
|
+
tags?: string[];
|
|
87
|
+
};
|
|
88
|
+
export declare const StructuralFileChangeSchema: z.ZodType<StructuralFileChange>;
|
|
89
|
+
export type StructuralFileStatus = "added" | "deleted" | "modified" | "renamed" | "copied" | "type_changed";
|
|
90
|
+
export declare const StructuralFileStatusSchema: z.ZodType<StructuralFileStatus>;
|
|
91
|
+
export type StructuralFileRef = {
|
|
92
|
+
path: string;
|
|
93
|
+
oid: string;
|
|
94
|
+
mode: string;
|
|
95
|
+
};
|
|
96
|
+
export declare const StructuralFileRefSchema: z.ZodType<StructuralFileRef>;
|
|
97
|
+
export type StructuralVisibility = {
|
|
98
|
+
collapsed?: boolean;
|
|
99
|
+
label?: string;
|
|
100
|
+
};
|
|
101
|
+
export declare const StructuralVisibilitySchema: z.ZodType<StructuralVisibility>;
|
|
102
|
+
/** Structural changed lines, independent of presentation and fold state. */
|
|
103
|
+
export type StructuralChanges = {
|
|
104
|
+
base: [number, number][];
|
|
105
|
+
head: [number, number][];
|
|
106
|
+
};
|
|
107
|
+
export declare const StructuralChangesSchema: z.ZodType<StructuralChanges>;
|
|
108
|
+
export declare function structuralChangeCounts(changes: StructuralChanges): StructuralLineCounts;
|
|
109
|
+
export type StructuralDiff = ({
|
|
110
|
+
type: "text";
|
|
111
|
+
stats: StructuralStats;
|
|
112
|
+
structural_changes: StructuralChanges;
|
|
113
|
+
} & StructuralPairing<StructuralSource>) | ({
|
|
114
|
+
type: "binary";
|
|
115
|
+
} & StructuralPairing<StructuralBinaryRef>);
|
|
116
|
+
export declare const StructuralDiffSchema: z.ZodType<StructuralDiff>;
|
|
117
|
+
export type StructuralSource = {
|
|
118
|
+
text: string;
|
|
119
|
+
syntax?: StructuralSyntaxSpan[];
|
|
120
|
+
regions?: StructuralRegion[];
|
|
121
|
+
};
|
|
122
|
+
export declare const StructuralSourceSchema: z.ZodType<StructuralSource>;
|
|
123
|
+
export type StructuralBinaryRef = {
|
|
124
|
+
size: number;
|
|
125
|
+
};
|
|
126
|
+
export declare const StructuralBinaryRefSchema: z.ZodType<StructuralBinaryRef>;
|
|
127
|
+
export type StructuralSyntaxSpan = {
|
|
128
|
+
line: number;
|
|
129
|
+
start_column: number;
|
|
130
|
+
end_column: number;
|
|
131
|
+
capture: string;
|
|
132
|
+
};
|
|
133
|
+
export declare const StructuralSyntaxSpanSchema: z.ZodType<StructuralSyntaxSpan>;
|
|
134
|
+
/** Region identity, collapse identity, and leaf alignment identity are distinct. */
|
|
135
|
+
export type StructuralRegion = {
|
|
136
|
+
id: number;
|
|
137
|
+
fold_state_id: number;
|
|
138
|
+
tags?: string[];
|
|
139
|
+
visibility?: StructuralVisibility;
|
|
140
|
+
} & StructuralSourceRange & StructuralNode;
|
|
141
|
+
export declare const StructuralRegionSchema: z.ZodType<StructuralRegion>;
|
|
142
|
+
export type StructuralNode = {
|
|
143
|
+
kind: "leaf";
|
|
144
|
+
alignment_id: number;
|
|
145
|
+
changed?: StructuralSpan[];
|
|
146
|
+
} | {
|
|
147
|
+
kind: "fold";
|
|
148
|
+
children: StructuralRegion[];
|
|
149
|
+
};
|
|
150
|
+
export declare const StructuralNodeSchema: z.ZodType<StructuralNode>;
|
|
151
|
+
export type StructuralSpan = {
|
|
152
|
+
line: number;
|
|
153
|
+
start_column: number;
|
|
154
|
+
end_column: number;
|
|
155
|
+
};
|
|
156
|
+
export declare const StructuralSpanSchema: z.ZodType<StructuralSpan>;
|
|
157
|
+
export type StructuralSourceRange = {
|
|
158
|
+
start: StructuralPos;
|
|
159
|
+
end: StructuralPos;
|
|
160
|
+
};
|
|
161
|
+
export declare const StructuralSourceRangeSchema: z.ZodType<StructuralSourceRange>;
|
|
162
|
+
export type StructuralPos = {
|
|
163
|
+
line: number;
|
|
164
|
+
column: number;
|
|
165
|
+
};
|
|
166
|
+
export declare const StructuralPosSchema: z.ZodType<StructuralPos>;
|
|
167
|
+
export type StructuralStats = {
|
|
168
|
+
textual: StructuralLineCounts;
|
|
169
|
+
visible: StructuralLineCounts;
|
|
170
|
+
fallback?: StructuralProblem;
|
|
171
|
+
};
|
|
172
|
+
export declare const StructuralStatsSchema: z.ZodType<StructuralStats>;
|
|
173
|
+
export type StructuralLineCounts = {
|
|
174
|
+
added: number;
|
|
175
|
+
removed: number;
|
|
176
|
+
};
|
|
177
|
+
export declare const StructuralLineCountsSchema: z.ZodType<StructuralLineCounts>;
|
package/dist/contract.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-maintained mirror of diffr's wire format. These files change together:
|
|
3
|
+
* src/protocol/mod.rs and src/pairing.rs (the Rust source of truth)
|
|
4
|
+
* docs/streaming.md (the documented shapes)
|
|
5
|
+
* npm/diffr/src/contract.ts (this file)
|
|
6
|
+
* tui/packages/hunk/src/diffr/wire.ts (the terminal UI's copy, v3 only)
|
|
7
|
+
* contract.test.ts runs the built binary and validates every record against
|
|
8
|
+
* these schemas; version.test.ts checks the Rust VERSION constant.
|
|
9
|
+
*
|
|
10
|
+
* This file may import only zod: consumers concatenate it into generated
|
|
11
|
+
* source where zod is the sole external.
|
|
12
|
+
* Serde omits default/empty fields rather than sending null. Lines are zero-based,
|
|
13
|
+
* columns are UTF-8 byte offsets, and ranges are half-open.
|
|
14
|
+
*/
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
function structuralPairingSchema(value) {
|
|
17
|
+
return z.union([
|
|
18
|
+
z.object({ lhs: value, rhs: value }),
|
|
19
|
+
z.object({ lhs: value, rhs: z.never().optional() }),
|
|
20
|
+
z.object({ rhs: value, lhs: z.never().optional() }),
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
const structuralU32 = z.number().int().min(0).max(0xffff_ffff);
|
|
24
|
+
export const STRUCTURAL_DIFF_WIRE_VERSION = 4;
|
|
25
|
+
/** Base stream version without --stream-annotations. */
|
|
26
|
+
export const STRUCTURAL_DIFF_BASE_WIRE_VERSION = 3;
|
|
27
|
+
export const StructuralDiffEventSchema = z.lazy(() => z.union([
|
|
28
|
+
z.object({
|
|
29
|
+
type: z.literal("start"),
|
|
30
|
+
version: structuralU32,
|
|
31
|
+
lhs: StructuralSnapshotSchema,
|
|
32
|
+
rhs: StructuralSnapshotSchema,
|
|
33
|
+
files: z.array(StructuralFileChangeSchema),
|
|
34
|
+
}),
|
|
35
|
+
z
|
|
36
|
+
.object({
|
|
37
|
+
type: z.literal("file"),
|
|
38
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
39
|
+
visibility: StructuralVisibilitySchema.optional(),
|
|
40
|
+
})
|
|
41
|
+
.and(StructuralOutcomeSchema),
|
|
42
|
+
z.object({
|
|
43
|
+
type: z.literal("annotations"),
|
|
44
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
45
|
+
annotations: z.array(z.object({ region_id: structuralU32, label: z.string() })),
|
|
46
|
+
error: StructuralProblemSchema.optional(),
|
|
47
|
+
}),
|
|
48
|
+
z.object({
|
|
49
|
+
type: z.literal("complete"),
|
|
50
|
+
succeeded: structuralU32,
|
|
51
|
+
failed: structuralU32,
|
|
52
|
+
aborted: StructuralProblemSchema.optional(),
|
|
53
|
+
}),
|
|
54
|
+
]));
|
|
55
|
+
export const StructuralOutcomeSchema = z.lazy(() => z.union([
|
|
56
|
+
z.object({ diff: StructuralDiffSchema, error: z.never().optional() }),
|
|
57
|
+
z.object({ error: StructuralProblemSchema, diff: z.never().optional() }),
|
|
58
|
+
]));
|
|
59
|
+
export const StructuralSnapshotSchema = z.lazy(() => z.union([
|
|
60
|
+
z.object({ type: z.literal("revision"), rev: z.string() }),
|
|
61
|
+
z.object({ type: z.literal("index") }),
|
|
62
|
+
z.object({ type: z.literal("working_tree") }),
|
|
63
|
+
z.object({ type: z.literal("empty_tree") }),
|
|
64
|
+
z.object({ type: z.literal("path"), path: z.string() }),
|
|
65
|
+
]));
|
|
66
|
+
export const StructuralProblemSchema = z.lazy(() => z.object({ code: z.string(), message: z.string() }));
|
|
67
|
+
export const StructuralFileChangeSchema = z.lazy(() => z.object({
|
|
68
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
69
|
+
status: StructuralFileStatusSchema,
|
|
70
|
+
tags: z.array(z.string()).optional(),
|
|
71
|
+
}));
|
|
72
|
+
export const StructuralFileStatusSchema = z.lazy(() => z.union([
|
|
73
|
+
z.literal("added"),
|
|
74
|
+
z.literal("deleted"),
|
|
75
|
+
z.literal("modified"),
|
|
76
|
+
z.literal("renamed"),
|
|
77
|
+
z.literal("copied"),
|
|
78
|
+
z.literal("type_changed"),
|
|
79
|
+
]));
|
|
80
|
+
export const StructuralFileRefSchema = z.lazy(() => z.object({ path: z.string(), oid: z.string(), mode: z.string() }));
|
|
81
|
+
export const StructuralVisibilitySchema = z.lazy(() => z.object({
|
|
82
|
+
collapsed: z.boolean().optional(),
|
|
83
|
+
label: z.string().optional(),
|
|
84
|
+
}));
|
|
85
|
+
const structuralRangesSchema = z
|
|
86
|
+
.array(z.tuple([structuralU32, structuralU32]))
|
|
87
|
+
.refine((ranges) => ranges.every(([start, end], index) => start < end && (index === 0 || ranges[index - 1][1] < start)), "Expected sorted, coalesced, nonempty half-open ranges");
|
|
88
|
+
export const StructuralChangesSchema = z.object({
|
|
89
|
+
base: structuralRangesSchema,
|
|
90
|
+
head: structuralRangesSchema,
|
|
91
|
+
});
|
|
92
|
+
export function structuralChangeCounts(changes) {
|
|
93
|
+
const count = (ranges) => ranges.reduce((sum, [start, end]) => sum + end - start, 0);
|
|
94
|
+
return { added: count(changes.head), removed: count(changes.base) };
|
|
95
|
+
}
|
|
96
|
+
export const StructuralDiffSchema = z.lazy(() => z.union([
|
|
97
|
+
z
|
|
98
|
+
.object({
|
|
99
|
+
type: z.literal("text"),
|
|
100
|
+
stats: StructuralStatsSchema,
|
|
101
|
+
structural_changes: StructuralChangesSchema,
|
|
102
|
+
})
|
|
103
|
+
.and(structuralPairingSchema(StructuralSourceSchema)),
|
|
104
|
+
z
|
|
105
|
+
.object({ type: z.literal("binary") })
|
|
106
|
+
.and(structuralPairingSchema(StructuralBinaryRefSchema)),
|
|
107
|
+
]));
|
|
108
|
+
export const StructuralSourceSchema = z.lazy(() => z.object({
|
|
109
|
+
text: z.string(),
|
|
110
|
+
syntax: z.array(StructuralSyntaxSpanSchema).optional(),
|
|
111
|
+
regions: z.array(StructuralRegionSchema).optional(),
|
|
112
|
+
}));
|
|
113
|
+
export const StructuralBinaryRefSchema = z.lazy(() => z.object({ size: z.number().int().nonnegative() }));
|
|
114
|
+
export const StructuralSyntaxSpanSchema = z.lazy(() => z.object({
|
|
115
|
+
line: structuralU32,
|
|
116
|
+
start_column: structuralU32,
|
|
117
|
+
end_column: structuralU32,
|
|
118
|
+
capture: z.string(),
|
|
119
|
+
}));
|
|
120
|
+
export const StructuralRegionSchema = z.lazy(() => z
|
|
121
|
+
.object({
|
|
122
|
+
id: structuralU32,
|
|
123
|
+
fold_state_id: structuralU32,
|
|
124
|
+
tags: z.array(z.string()).optional(),
|
|
125
|
+
visibility: StructuralVisibilitySchema.optional(),
|
|
126
|
+
})
|
|
127
|
+
.and(StructuralSourceRangeSchema)
|
|
128
|
+
.and(StructuralNodeSchema));
|
|
129
|
+
export const StructuralNodeSchema = z.lazy(() => z.union([
|
|
130
|
+
z.object({
|
|
131
|
+
kind: z.literal("leaf"),
|
|
132
|
+
alignment_id: structuralU32,
|
|
133
|
+
changed: z.array(StructuralSpanSchema).optional(),
|
|
134
|
+
}),
|
|
135
|
+
z.object({
|
|
136
|
+
kind: z.literal("fold"),
|
|
137
|
+
children: z.array(StructuralRegionSchema),
|
|
138
|
+
}),
|
|
139
|
+
]));
|
|
140
|
+
export const StructuralSpanSchema = z.lazy(() => z.object({
|
|
141
|
+
line: structuralU32,
|
|
142
|
+
start_column: structuralU32,
|
|
143
|
+
end_column: structuralU32,
|
|
144
|
+
}));
|
|
145
|
+
export const StructuralSourceRangeSchema = z.lazy(() => z.object({ start: StructuralPosSchema, end: StructuralPosSchema }));
|
|
146
|
+
export const StructuralPosSchema = z.lazy(() => z.object({
|
|
147
|
+
line: structuralU32,
|
|
148
|
+
column: structuralU32,
|
|
149
|
+
}));
|
|
150
|
+
export const StructuralStatsSchema = z.lazy(() => z.object({
|
|
151
|
+
textual: StructuralLineCountsSchema,
|
|
152
|
+
visible: StructuralLineCountsSchema,
|
|
153
|
+
fallback: StructuralProblemSchema.optional(),
|
|
154
|
+
}));
|
|
155
|
+
export const StructuralLineCountsSchema = z.lazy(() => z.object({
|
|
156
|
+
added: structuralU32,
|
|
157
|
+
removed: structuralU32,
|
|
158
|
+
}));
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/stream.d.ts
ADDED
package/dist/stream.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { StructuralDiffEventSchema } from "./contract.js";
|
|
2
|
+
/** JSON is untrusted until the complete nested Rust wire contract validates. */
|
|
3
|
+
export function decodeStructuralDiffEvent(line) {
|
|
4
|
+
try {
|
|
5
|
+
return StructuralDiffEventSchema.parse(JSON.parse(line));
|
|
6
|
+
}
|
|
7
|
+
catch (cause) {
|
|
8
|
+
throw new Error("Malformed diffr protocol record.", { cause });
|
|
9
|
+
}
|
|
10
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dev.fast/diffr",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "diffr's NDJSON wire contract, validators, and release binary fetcher",
|
|
5
|
+
"repository": "https://github.com/devdotfast/diffr",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json",
|
|
15
|
+
"./src/*": "./src/*"
|
|
16
|
+
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"diffr-fetch": "bin/fetch.mjs"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src",
|
|
23
|
+
"!src/*.test.ts",
|
|
24
|
+
"!src/test-binary.ts",
|
|
25
|
+
"bin",
|
|
26
|
+
"pins.json",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -p tsconfig.build.json",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"test": "bun test",
|
|
33
|
+
"pin": "node scripts/pin.mjs",
|
|
34
|
+
"prepack": "npm run build"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"zod": "^4.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/bun": "1.3.10",
|
|
41
|
+
"typescript": "5.9.3",
|
|
42
|
+
"zod": "4.1.12"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/pins.json
ADDED
package/src/contract.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-maintained mirror of diffr's wire format. These files change together:
|
|
3
|
+
* src/protocol/mod.rs and src/pairing.rs (the Rust source of truth)
|
|
4
|
+
* docs/streaming.md (the documented shapes)
|
|
5
|
+
* npm/diffr/src/contract.ts (this file)
|
|
6
|
+
* tui/packages/hunk/src/diffr/wire.ts (the terminal UI's copy, v3 only)
|
|
7
|
+
* contract.test.ts runs the built binary and validates every record against
|
|
8
|
+
* these schemas; version.test.ts checks the Rust VERSION constant.
|
|
9
|
+
*
|
|
10
|
+
* This file may import only zod: consumers concatenate it into generated
|
|
11
|
+
* source where zod is the sole external.
|
|
12
|
+
* Serde omits default/empty fields rather than sending null. Lines are zero-based,
|
|
13
|
+
* columns are UTF-8 byte offsets, and ranges are half-open.
|
|
14
|
+
*/
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
/** A pairing always has at least one side; additions/deletions omit the other. */
|
|
18
|
+
export type StructuralPairing<T> =
|
|
19
|
+
| { lhs: T; rhs: T }
|
|
20
|
+
| { lhs: T; rhs?: never }
|
|
21
|
+
| { rhs: T; lhs?: never };
|
|
22
|
+
|
|
23
|
+
function structuralPairingSchema<T>(
|
|
24
|
+
value: z.ZodType<T>,
|
|
25
|
+
): z.ZodType<StructuralPairing<T>> {
|
|
26
|
+
return z.union([
|
|
27
|
+
z.object({ lhs: value, rhs: value }),
|
|
28
|
+
z.object({ lhs: value, rhs: z.never().optional() }),
|
|
29
|
+
z.object({ rhs: value, lhs: z.never().optional() }),
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const structuralU32 = z.number().int().min(0).max(0xffff_ffff);
|
|
34
|
+
|
|
35
|
+
export const STRUCTURAL_DIFF_WIRE_VERSION = 4;
|
|
36
|
+
/** Base stream version without --stream-annotations. */
|
|
37
|
+
export const STRUCTURAL_DIFF_BASE_WIRE_VERSION = 3;
|
|
38
|
+
|
|
39
|
+
export type StructuralDiffEvent =
|
|
40
|
+
| {
|
|
41
|
+
type: "start";
|
|
42
|
+
version: number;
|
|
43
|
+
lhs: StructuralSnapshot;
|
|
44
|
+
rhs: StructuralSnapshot;
|
|
45
|
+
files: StructuralFileChange[];
|
|
46
|
+
}
|
|
47
|
+
| ({
|
|
48
|
+
type: "file";
|
|
49
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
50
|
+
visibility?: StructuralVisibility;
|
|
51
|
+
} & StructuralOutcome)
|
|
52
|
+
| {
|
|
53
|
+
type: "annotations";
|
|
54
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
55
|
+
annotations: { region_id: number; label: string }[];
|
|
56
|
+
error?: StructuralProblem;
|
|
57
|
+
}
|
|
58
|
+
| {
|
|
59
|
+
type: "complete";
|
|
60
|
+
succeeded: number;
|
|
61
|
+
failed: number;
|
|
62
|
+
aborted?: StructuralProblem;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const StructuralDiffEventSchema: z.ZodType<StructuralDiffEvent> = z.lazy(
|
|
66
|
+
() =>
|
|
67
|
+
z.union([
|
|
68
|
+
z.object({
|
|
69
|
+
type: z.literal("start"),
|
|
70
|
+
version: structuralU32,
|
|
71
|
+
lhs: StructuralSnapshotSchema,
|
|
72
|
+
rhs: StructuralSnapshotSchema,
|
|
73
|
+
files: z.array(StructuralFileChangeSchema),
|
|
74
|
+
}),
|
|
75
|
+
z
|
|
76
|
+
.object({
|
|
77
|
+
type: z.literal("file"),
|
|
78
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
79
|
+
visibility: StructuralVisibilitySchema.optional(),
|
|
80
|
+
})
|
|
81
|
+
.and(StructuralOutcomeSchema),
|
|
82
|
+
z.object({
|
|
83
|
+
type: z.literal("annotations"),
|
|
84
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
85
|
+
annotations: z.array(
|
|
86
|
+
z.object({ region_id: structuralU32, label: z.string() }),
|
|
87
|
+
),
|
|
88
|
+
error: StructuralProblemSchema.optional(),
|
|
89
|
+
}),
|
|
90
|
+
z.object({
|
|
91
|
+
type: z.literal("complete"),
|
|
92
|
+
succeeded: structuralU32,
|
|
93
|
+
failed: structuralU32,
|
|
94
|
+
aborted: StructuralProblemSchema.optional(),
|
|
95
|
+
}),
|
|
96
|
+
]),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
/** A file record contains exactly one result or error. */
|
|
100
|
+
export type StructuralOutcome =
|
|
101
|
+
| { diff: StructuralDiff; error?: never }
|
|
102
|
+
| { error: StructuralProblem; diff?: never };
|
|
103
|
+
|
|
104
|
+
export const StructuralOutcomeSchema: z.ZodType<StructuralOutcome> = z.lazy(
|
|
105
|
+
() =>
|
|
106
|
+
z.union([
|
|
107
|
+
z.object({ diff: StructuralDiffSchema, error: z.never().optional() }),
|
|
108
|
+
z.object({ error: StructuralProblemSchema, diff: z.never().optional() }),
|
|
109
|
+
]),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
export type StructuralSnapshot =
|
|
113
|
+
| { type: "revision"; rev: string }
|
|
114
|
+
| { type: "index" }
|
|
115
|
+
| { type: "working_tree" }
|
|
116
|
+
| { type: "empty_tree" }
|
|
117
|
+
| { type: "path"; path: string };
|
|
118
|
+
|
|
119
|
+
export const StructuralSnapshotSchema: z.ZodType<StructuralSnapshot> = z.lazy(
|
|
120
|
+
() =>
|
|
121
|
+
z.union([
|
|
122
|
+
z.object({ type: z.literal("revision"), rev: z.string() }),
|
|
123
|
+
z.object({ type: z.literal("index") }),
|
|
124
|
+
z.object({ type: z.literal("working_tree") }),
|
|
125
|
+
z.object({ type: z.literal("empty_tree") }),
|
|
126
|
+
z.object({ type: z.literal("path"), path: z.string() }),
|
|
127
|
+
]),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
export type StructuralProblem = { code: string; message: string };
|
|
131
|
+
|
|
132
|
+
export const StructuralProblemSchema: z.ZodType<StructuralProblem> = z.lazy(
|
|
133
|
+
() => z.object({ code: z.string(), message: z.string() }),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
export type StructuralFileChange = {
|
|
137
|
+
file: StructuralPairing<StructuralFileRef>;
|
|
138
|
+
status: StructuralFileStatus;
|
|
139
|
+
tags?: string[];
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const StructuralFileChangeSchema: z.ZodType<StructuralFileChange> =
|
|
143
|
+
z.lazy(() =>
|
|
144
|
+
z.object({
|
|
145
|
+
file: structuralPairingSchema(StructuralFileRefSchema),
|
|
146
|
+
status: StructuralFileStatusSchema,
|
|
147
|
+
tags: z.array(z.string()).optional(),
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
export type StructuralFileStatus =
|
|
152
|
+
| "added"
|
|
153
|
+
| "deleted"
|
|
154
|
+
| "modified"
|
|
155
|
+
| "renamed"
|
|
156
|
+
| "copied"
|
|
157
|
+
| "type_changed";
|
|
158
|
+
|
|
159
|
+
export const StructuralFileStatusSchema: z.ZodType<StructuralFileStatus> =
|
|
160
|
+
z.lazy(() =>
|
|
161
|
+
z.union([
|
|
162
|
+
z.literal("added"),
|
|
163
|
+
z.literal("deleted"),
|
|
164
|
+
z.literal("modified"),
|
|
165
|
+
z.literal("renamed"),
|
|
166
|
+
z.literal("copied"),
|
|
167
|
+
z.literal("type_changed"),
|
|
168
|
+
]),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
export type StructuralFileRef = { path: string; oid: string; mode: string };
|
|
172
|
+
|
|
173
|
+
export const StructuralFileRefSchema: z.ZodType<StructuralFileRef> = z.lazy(
|
|
174
|
+
() => z.object({ path: z.string(), oid: z.string(), mode: z.string() }),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
export type StructuralVisibility = { collapsed?: boolean; label?: string };
|
|
178
|
+
|
|
179
|
+
export const StructuralVisibilitySchema: z.ZodType<StructuralVisibility> =
|
|
180
|
+
z.lazy(() =>
|
|
181
|
+
z.object({
|
|
182
|
+
collapsed: z.boolean().optional(),
|
|
183
|
+
label: z.string().optional(),
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
/** Structural changed lines, independent of presentation and fold state. */
|
|
188
|
+
export type StructuralChanges = {
|
|
189
|
+
base: [number, number][];
|
|
190
|
+
head: [number, number][];
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const structuralRangesSchema = z
|
|
194
|
+
.array(z.tuple([structuralU32, structuralU32]))
|
|
195
|
+
.refine(
|
|
196
|
+
(ranges) =>
|
|
197
|
+
ranges.every(
|
|
198
|
+
([start, end], index) =>
|
|
199
|
+
start < end && (index === 0 || ranges[index - 1][1] < start),
|
|
200
|
+
),
|
|
201
|
+
"Expected sorted, coalesced, nonempty half-open ranges",
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
export const StructuralChangesSchema: z.ZodType<StructuralChanges> = z.object({
|
|
205
|
+
base: structuralRangesSchema,
|
|
206
|
+
head: structuralRangesSchema,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
export function structuralChangeCounts(
|
|
210
|
+
changes: StructuralChanges,
|
|
211
|
+
): StructuralLineCounts {
|
|
212
|
+
const count = (ranges: [number, number][]) =>
|
|
213
|
+
ranges.reduce((sum, [start, end]) => sum + end - start, 0);
|
|
214
|
+
|
|
215
|
+
return { added: count(changes.head), removed: count(changes.base) };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export type StructuralDiff =
|
|
219
|
+
| ({
|
|
220
|
+
type: "text";
|
|
221
|
+
stats: StructuralStats;
|
|
222
|
+
structural_changes: StructuralChanges;
|
|
223
|
+
} & StructuralPairing<StructuralSource>)
|
|
224
|
+
| ({ type: "binary" } & StructuralPairing<StructuralBinaryRef>);
|
|
225
|
+
|
|
226
|
+
export const StructuralDiffSchema: z.ZodType<StructuralDiff> = z.lazy(() =>
|
|
227
|
+
z.union([
|
|
228
|
+
z
|
|
229
|
+
.object({
|
|
230
|
+
type: z.literal("text"),
|
|
231
|
+
stats: StructuralStatsSchema,
|
|
232
|
+
structural_changes: StructuralChangesSchema,
|
|
233
|
+
})
|
|
234
|
+
.and(structuralPairingSchema(StructuralSourceSchema)),
|
|
235
|
+
z
|
|
236
|
+
.object({ type: z.literal("binary") })
|
|
237
|
+
.and(structuralPairingSchema(StructuralBinaryRefSchema)),
|
|
238
|
+
]),
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
export type StructuralSource = {
|
|
242
|
+
text: string;
|
|
243
|
+
syntax?: StructuralSyntaxSpan[];
|
|
244
|
+
regions?: StructuralRegion[];
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
export const StructuralSourceSchema: z.ZodType<StructuralSource> = z.lazy(() =>
|
|
248
|
+
z.object({
|
|
249
|
+
text: z.string(),
|
|
250
|
+
syntax: z.array(StructuralSyntaxSpanSchema).optional(),
|
|
251
|
+
regions: z.array(StructuralRegionSchema).optional(),
|
|
252
|
+
}),
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
export type StructuralBinaryRef = { size: number };
|
|
256
|
+
|
|
257
|
+
export const StructuralBinaryRefSchema: z.ZodType<StructuralBinaryRef> = z.lazy(
|
|
258
|
+
() => z.object({ size: z.number().int().nonnegative() }),
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
export type StructuralSyntaxSpan = {
|
|
262
|
+
line: number;
|
|
263
|
+
start_column: number;
|
|
264
|
+
end_column: number;
|
|
265
|
+
capture: string;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export const StructuralSyntaxSpanSchema: z.ZodType<StructuralSyntaxSpan> =
|
|
269
|
+
z.lazy(() =>
|
|
270
|
+
z.object({
|
|
271
|
+
line: structuralU32,
|
|
272
|
+
start_column: structuralU32,
|
|
273
|
+
end_column: structuralU32,
|
|
274
|
+
capture: z.string(),
|
|
275
|
+
}),
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
/** Region identity, collapse identity, and leaf alignment identity are distinct. */
|
|
279
|
+
export type StructuralRegion = {
|
|
280
|
+
id: number;
|
|
281
|
+
fold_state_id: number;
|
|
282
|
+
tags?: string[];
|
|
283
|
+
visibility?: StructuralVisibility;
|
|
284
|
+
} & StructuralSourceRange &
|
|
285
|
+
StructuralNode;
|
|
286
|
+
|
|
287
|
+
export const StructuralRegionSchema: z.ZodType<StructuralRegion> = z.lazy(() =>
|
|
288
|
+
z
|
|
289
|
+
.object({
|
|
290
|
+
id: structuralU32,
|
|
291
|
+
fold_state_id: structuralU32,
|
|
292
|
+
tags: z.array(z.string()).optional(),
|
|
293
|
+
visibility: StructuralVisibilitySchema.optional(),
|
|
294
|
+
})
|
|
295
|
+
.and(StructuralSourceRangeSchema)
|
|
296
|
+
.and(StructuralNodeSchema),
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
export type StructuralNode =
|
|
300
|
+
| { kind: "leaf"; alignment_id: number; changed?: StructuralSpan[] }
|
|
301
|
+
| { kind: "fold"; children: StructuralRegion[] };
|
|
302
|
+
|
|
303
|
+
export const StructuralNodeSchema: z.ZodType<StructuralNode> = z.lazy(() =>
|
|
304
|
+
z.union([
|
|
305
|
+
z.object({
|
|
306
|
+
kind: z.literal("leaf"),
|
|
307
|
+
alignment_id: structuralU32,
|
|
308
|
+
changed: z.array(StructuralSpanSchema).optional(),
|
|
309
|
+
}),
|
|
310
|
+
z.object({
|
|
311
|
+
kind: z.literal("fold"),
|
|
312
|
+
children: z.array(StructuralRegionSchema),
|
|
313
|
+
}),
|
|
314
|
+
]),
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
export type StructuralSpan = {
|
|
318
|
+
line: number;
|
|
319
|
+
start_column: number;
|
|
320
|
+
end_column: number;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
export const StructuralSpanSchema: z.ZodType<StructuralSpan> = z.lazy(() =>
|
|
324
|
+
z.object({
|
|
325
|
+
line: structuralU32,
|
|
326
|
+
start_column: structuralU32,
|
|
327
|
+
end_column: structuralU32,
|
|
328
|
+
}),
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
export type StructuralSourceRange = {
|
|
332
|
+
start: StructuralPos;
|
|
333
|
+
end: StructuralPos;
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
export const StructuralSourceRangeSchema: z.ZodType<StructuralSourceRange> =
|
|
337
|
+
z.lazy(() =>
|
|
338
|
+
z.object({ start: StructuralPosSchema, end: StructuralPosSchema }),
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
export type StructuralPos = { line: number; column: number };
|
|
342
|
+
|
|
343
|
+
export const StructuralPosSchema: z.ZodType<StructuralPos> = z.lazy(() =>
|
|
344
|
+
z.object({
|
|
345
|
+
line: structuralU32,
|
|
346
|
+
column: structuralU32,
|
|
347
|
+
}),
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
export type StructuralStats = {
|
|
351
|
+
textual: StructuralLineCounts;
|
|
352
|
+
visible: StructuralLineCounts;
|
|
353
|
+
fallback?: StructuralProblem;
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
export const StructuralStatsSchema: z.ZodType<StructuralStats> = z.lazy(() =>
|
|
357
|
+
z.object({
|
|
358
|
+
textual: StructuralLineCountsSchema,
|
|
359
|
+
visible: StructuralLineCountsSchema,
|
|
360
|
+
fallback: StructuralProblemSchema.optional(),
|
|
361
|
+
}),
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
export type StructuralLineCounts = { added: number; removed: number };
|
|
365
|
+
|
|
366
|
+
export const StructuralLineCountsSchema: z.ZodType<StructuralLineCounts> =
|
|
367
|
+
z.lazy(() =>
|
|
368
|
+
z.object({
|
|
369
|
+
added: structuralU32,
|
|
370
|
+
removed: structuralU32,
|
|
371
|
+
}),
|
|
372
|
+
);
|
package/src/index.ts
ADDED
package/src/stream.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type StructuralDiffEvent, StructuralDiffEventSchema } from "./contract.js";
|
|
2
|
+
|
|
3
|
+
/** JSON is untrusted until the complete nested Rust wire contract validates. */
|
|
4
|
+
export function decodeStructuralDiffEvent(line: string): StructuralDiffEvent {
|
|
5
|
+
try {
|
|
6
|
+
return StructuralDiffEventSchema.parse(JSON.parse(line));
|
|
7
|
+
} catch (cause) {
|
|
8
|
+
throw new Error("Malformed diffr protocol record.", { cause });
|
|
9
|
+
}
|
|
10
|
+
}
|