autotel-message-contract 0.1.0
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/README.md +166 -0
- package/dist/index.cjs +453 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +173 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +414 -0
- package/dist/index.js.map +1 -0
- package/dist/serializer.cjs +47 -0
- package/dist/serializer.cjs.map +1 -0
- package/dist/serializer.d.cts +50 -0
- package/dist/serializer.d.cts.map +1 -0
- package/dist/serializer.d.ts +50 -0
- package/dist/serializer.d.ts.map +1 -0
- package/dist/serializer.js +44 -0
- package/dist/serializer.js.map +1 -0
- package/package.json +73 -0
- package/src/contract.test.ts +279 -0
- package/src/contract.ts +409 -0
- package/src/diff.ts +61 -0
- package/src/index.ts +53 -0
- package/src/reader.ts +105 -0
- package/src/serializer.test.ts +32 -0
- package/src/serializer.ts +89 -0
- package/src/snapshot-storage.ts +113 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { defaultSerializer, jsonSerializer } from "./serializer.js";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/diff.ts
|
|
6
|
+
/**
|
|
7
|
+
* Minimal line diff for snapshot failures. The goal is a message that points
|
|
8
|
+
* at *what moved* — a renamed field, a switched date format, a dropped value —
|
|
9
|
+
* not a full diff engine. Lines only in the approved file are marked `-`, lines
|
|
10
|
+
* only in the actual output are marked `+`, and a little surrounding context is
|
|
11
|
+
* kept so the change is readable in a terminal.
|
|
12
|
+
*/
|
|
13
|
+
function lineDiff(approved, actual) {
|
|
14
|
+
const a = approved.split("\n");
|
|
15
|
+
const b = actual.split("\n");
|
|
16
|
+
const lcs = longestCommonSubsequence(a, b);
|
|
17
|
+
const out = [];
|
|
18
|
+
let i = 0;
|
|
19
|
+
let j = 0;
|
|
20
|
+
for (const [ai, bj] of lcs) {
|
|
21
|
+
while (i < ai) out.push(`- ${a[i++]}`);
|
|
22
|
+
while (j < bj) out.push(`+ ${b[j++]}`);
|
|
23
|
+
out.push(` ${a[i++]}`);
|
|
24
|
+
j++;
|
|
25
|
+
}
|
|
26
|
+
while (i < a.length) out.push(`- ${a[i++]}`);
|
|
27
|
+
while (j < b.length) out.push(`+ ${b[j++]}`);
|
|
28
|
+
return out.join("\n");
|
|
29
|
+
}
|
|
30
|
+
/** Indices `[i, j]` of lines common to both, longest such subsequence. */
|
|
31
|
+
function longestCommonSubsequence(a, b) {
|
|
32
|
+
const n = a.length;
|
|
33
|
+
const m = b.length;
|
|
34
|
+
const table = Array.from({ length: n + 1 }, () => Array.from({ length: m + 1 }, () => 0));
|
|
35
|
+
for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) table[i][j] = a[i] === b[j] ? table[i + 1][j + 1] + 1 : Math.max(table[i + 1][j], table[i][j + 1]);
|
|
36
|
+
const pairs = [];
|
|
37
|
+
let i = 0;
|
|
38
|
+
let j = 0;
|
|
39
|
+
while (i < n && j < m) if (a[i] === b[j]) {
|
|
40
|
+
pairs.push([i, j]);
|
|
41
|
+
i++;
|
|
42
|
+
j++;
|
|
43
|
+
} else if (table[i + 1][j] >= table[i][j + 1]) i++;
|
|
44
|
+
else j++;
|
|
45
|
+
return pairs;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/reader.ts
|
|
50
|
+
function isStandardSchema(reader) {
|
|
51
|
+
return typeof reader === "object" && reader !== null && "~standard" in reader && typeof reader["~standard"]?.validate === "function";
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Run a reader against a deserialized value. Never throws — a thrown parse
|
|
55
|
+
* error or reported issues become `{ ok: false, issues }` so the caller can
|
|
56
|
+
* build a single, legible failure message.
|
|
57
|
+
*/
|
|
58
|
+
async function read(reader, value) {
|
|
59
|
+
if (isStandardSchema(reader)) try {
|
|
60
|
+
const result = await reader["~standard"].validate(value);
|
|
61
|
+
if (result.issues && result.issues.length > 0) return {
|
|
62
|
+
ok: false,
|
|
63
|
+
issues: result.issues.map((issue) => formatIssue(issue.message, issue.path))
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
value: result.value,
|
|
68
|
+
issues: []
|
|
69
|
+
};
|
|
70
|
+
} catch (error) {
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
issues: [errorMessage(error)]
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
value: reader(value),
|
|
80
|
+
issues: []
|
|
81
|
+
};
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return {
|
|
84
|
+
ok: false,
|
|
85
|
+
issues: [errorMessage(error)]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function formatIssue(message, path) {
|
|
90
|
+
if (Array.isArray(path) && path.length > 0) return `${path.map(String).join(".")}: ${message}`;
|
|
91
|
+
return message;
|
|
92
|
+
}
|
|
93
|
+
function errorMessage(error) {
|
|
94
|
+
if (error instanceof Error) return error.message;
|
|
95
|
+
return String(error);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/snapshot-storage.ts
|
|
100
|
+
/**
|
|
101
|
+
* Snapshot storage.
|
|
102
|
+
*
|
|
103
|
+
* A snapshot is just a file you commit: the serialized shape of a message,
|
|
104
|
+
* reviewed once and from then on compared on every run. There is no broker, no
|
|
105
|
+
* registry, no service to start — a format change shows up in a normal diff,
|
|
106
|
+
* reviewed like any other code. This mirrors the "approved file" convention
|
|
107
|
+
* (`<name>.approved.txt`) familiar from approval testing.
|
|
108
|
+
*
|
|
109
|
+
* By default the file lives in a `__contracts__` directory beside the test that
|
|
110
|
+
* created it, resolved from the call stack so you do not have to thread paths
|
|
111
|
+
* around. Override `dir` / `path` when you keep snapshots elsewhere.
|
|
112
|
+
*/
|
|
113
|
+
/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */
|
|
114
|
+
const UPDATE_ENV_VARS = [
|
|
115
|
+
"AUTOTEL_CONTRACT_UPDATE",
|
|
116
|
+
"UPDATE_CONTRACTS",
|
|
117
|
+
"UPDATE_SNAPSHOTS"
|
|
118
|
+
];
|
|
119
|
+
function isUpdateMode(env = process.env) {
|
|
120
|
+
return UPDATE_ENV_VARS.some((name) => {
|
|
121
|
+
const value = env[name];
|
|
122
|
+
return value === "1" || value === "true";
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
const DEFAULT_DIR_NAME = "__contracts__";
|
|
126
|
+
const APPROVED_SUFFIX = ".approved.txt";
|
|
127
|
+
/** Resolve the absolute file path for a snapshot. */
|
|
128
|
+
function resolveSnapshotPath(location) {
|
|
129
|
+
if (location.path) return path.isAbsolute(location.path) ? location.path : path.resolve(process.cwd(), location.path);
|
|
130
|
+
const dir = location.dir ?? defaultSnapshotDir();
|
|
131
|
+
return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);
|
|
132
|
+
}
|
|
133
|
+
function sanitize(name) {
|
|
134
|
+
return name.replaceAll(/[^\w.@-]+/g, "_");
|
|
135
|
+
}
|
|
136
|
+
function readSnapshot(location) {
|
|
137
|
+
const filePath = resolveSnapshotPath(location);
|
|
138
|
+
if (!existsSync(filePath)) return {
|
|
139
|
+
exists: false,
|
|
140
|
+
path: filePath
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
exists: true,
|
|
144
|
+
content: readFileSync(filePath, "utf8"),
|
|
145
|
+
path: filePath
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function writeSnapshot(location, content) {
|
|
149
|
+
const filePath = resolveSnapshotPath(location);
|
|
150
|
+
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
151
|
+
writeFileSync(filePath, content, "utf8");
|
|
152
|
+
return filePath;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Best-effort `__contracts__` directory beside the calling test file, found by
|
|
156
|
+
* walking the stack past this module and the contract internals. Falls back to
|
|
157
|
+
* `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).
|
|
158
|
+
*/
|
|
159
|
+
function defaultSnapshotDir() {
|
|
160
|
+
const callerFile = callerOutsidePackage();
|
|
161
|
+
const base = callerFile ? path.dirname(callerFile) : process.cwd();
|
|
162
|
+
return path.join(base, DEFAULT_DIR_NAME);
|
|
163
|
+
}
|
|
164
|
+
function callerOutsidePackage() {
|
|
165
|
+
const stack = (/* @__PURE__ */ new Error("stack probe")).stack;
|
|
166
|
+
if (!stack) return void 0;
|
|
167
|
+
const lines = stack.split("\n").slice(1);
|
|
168
|
+
for (const line of lines) {
|
|
169
|
+
const file = (line.match(/\((.*?):\d+:\d+\)/) ?? line.match(/at (.*?):\d+:\d+/))?.[1];
|
|
170
|
+
if (!file) continue;
|
|
171
|
+
if (file.includes("node:")) continue;
|
|
172
|
+
if (file.includes(`${path.join("autotel-message-contract", "src")}`)) continue;
|
|
173
|
+
if (file.includes(`${path.join("autotel-message-contract", "dist")}`)) continue;
|
|
174
|
+
if (file.includes("node_modules")) continue;
|
|
175
|
+
return file.startsWith("file://") ? fileUrlToPath(file) : file;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function fileUrlToPath(url) {
|
|
179
|
+
return decodeURIComponent(url.replace(/^file:\/\//, ""));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/contract.ts
|
|
184
|
+
/**
|
|
185
|
+
* The message contract DSL.
|
|
186
|
+
*
|
|
187
|
+
* Every check starts from {@link messageContract} and reads as a sentence:
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* // Pin the serialized shape — fail when it drifts.
|
|
191
|
+
* await messageContract()
|
|
192
|
+
* .given(new OrderPlaced(orderId, 'Alice', placedAt))
|
|
193
|
+
* .whenSerialized()
|
|
194
|
+
* .thenContractIsUnchanged();
|
|
195
|
+
*
|
|
196
|
+
* // Prove a newer reader still reads what an older writer produced.
|
|
197
|
+
* await messageContract()
|
|
198
|
+
* .given(orderPlacedV1)
|
|
199
|
+
* .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn
|
|
200
|
+
* .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* A **snapshot check** confirms a message still serializes exactly as approved,
|
|
204
|
+
* so nothing reading it downstream breaks. A **compatibility check** confirms an
|
|
205
|
+
* older and a newer version can still read each other's data. Both are ordinary
|
|
206
|
+
* unit tests — no broker, no registry, no running service.
|
|
207
|
+
*/
|
|
208
|
+
/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */
|
|
209
|
+
var ContractViolationError = class extends Error {
|
|
210
|
+
name = "ContractViolationError";
|
|
211
|
+
constructor(message) {
|
|
212
|
+
super(message);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
const SNAPSHOT_SOURCE = Symbol("autotel-message-contract.snapshot-source");
|
|
216
|
+
/**
|
|
217
|
+
* Point a compatibility check at a previously approved snapshot instead of a
|
|
218
|
+
* live in-memory message instance.
|
|
219
|
+
*/
|
|
220
|
+
function approvedSnapshot(location) {
|
|
221
|
+
return {
|
|
222
|
+
[SNAPSHOT_SOURCE]: true,
|
|
223
|
+
location
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function isApprovedSnapshotSource(value) {
|
|
227
|
+
return typeof value === "object" && value !== null && SNAPSHOT_SOURCE in value && value[SNAPSHOT_SOURCE] === true;
|
|
228
|
+
}
|
|
229
|
+
/** Start a contract check. */
|
|
230
|
+
function messageContract(options = {}) {
|
|
231
|
+
return new GivenStep(options);
|
|
232
|
+
}
|
|
233
|
+
var GivenStep = class {
|
|
234
|
+
options;
|
|
235
|
+
constructor(options) {
|
|
236
|
+
this.options = options;
|
|
237
|
+
}
|
|
238
|
+
/** The message under contract. */
|
|
239
|
+
given(message) {
|
|
240
|
+
return new WhenStep(message, this.options);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
var WhenStep = class {
|
|
244
|
+
message;
|
|
245
|
+
options;
|
|
246
|
+
constructor(message, options) {
|
|
247
|
+
this.message = message;
|
|
248
|
+
this.options = options;
|
|
249
|
+
}
|
|
250
|
+
/** Serialize the message; the next step pins or inspects the result. */
|
|
251
|
+
whenSerialized() {
|
|
252
|
+
if (isApprovedSnapshotSource(this.message)) throw new ContractViolationError("Cannot serialize an approved snapshot source. Use .whenDeserializedAs(...) to run a compatibility check, or pass a live message instance to .given(...).");
|
|
253
|
+
const serializer = this.options.serializer ?? defaultSerializer;
|
|
254
|
+
return new SnapshotStep(serializer.serialize(this.message), serializer, this.options);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Round-trip the message through a reader that models a *different version*
|
|
258
|
+
* (a Standard Schema such as Zod/Valibot, or a parse function). The next step
|
|
259
|
+
* asserts the versions stay compatible.
|
|
260
|
+
*/
|
|
261
|
+
whenDeserializedAs(reader) {
|
|
262
|
+
const serializer = this.options.serializer ?? defaultSerializer;
|
|
263
|
+
return new CompatibilityStep(this.message, reader, serializer, this.options);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
var SnapshotStep = class {
|
|
267
|
+
serialized;
|
|
268
|
+
serializer;
|
|
269
|
+
options;
|
|
270
|
+
constructor(serialized, serializer, options) {
|
|
271
|
+
this.serialized = serialized;
|
|
272
|
+
this.serializer = serializer;
|
|
273
|
+
this.options = options;
|
|
274
|
+
}
|
|
275
|
+
/** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */
|
|
276
|
+
get output() {
|
|
277
|
+
return this.serialized;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Compare the serialized output against the approved snapshot. On first run
|
|
281
|
+
* (or in update mode) it writes the approved file and passes; afterwards it
|
|
282
|
+
* fails with a diff when the shape drifts.
|
|
283
|
+
*/
|
|
284
|
+
thenContractIsUnchanged(snapshotName) {
|
|
285
|
+
const location = this.resolveLocation(snapshotName);
|
|
286
|
+
const existing = readSnapshot(location);
|
|
287
|
+
const update = this.options.update ?? isUpdateMode();
|
|
288
|
+
if (!existing.exists || update) {
|
|
289
|
+
writeSnapshot(location, this.serialized);
|
|
290
|
+
if (!existing.exists) return;
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (existing.content !== this.serialized) throw new ContractViolationError(`Message contract drifted from its approved snapshot.\n serializer: ${this.serializer.name}\n snapshot: ${existing.path}\n\n${lineDiff(existing.content ?? "", this.serialized)}\n\nIf this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 to update the approved file, then review and commit it.`);
|
|
294
|
+
}
|
|
295
|
+
resolveLocation(snapshotName) {
|
|
296
|
+
if (snapshotName) return { name: snapshotName };
|
|
297
|
+
const configured = this.options.snapshot;
|
|
298
|
+
if (typeof configured === "string") return { name: configured };
|
|
299
|
+
if (configured) return configured;
|
|
300
|
+
throw new ContractViolationError("A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) or to thenContractIsUnchanged('OrderPlaced').");
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
var CompatibilityStep = class {
|
|
304
|
+
source;
|
|
305
|
+
reader;
|
|
306
|
+
serializer;
|
|
307
|
+
options;
|
|
308
|
+
constructor(source, reader, serializer, options) {
|
|
309
|
+
this.source = source;
|
|
310
|
+
this.reader = reader;
|
|
311
|
+
this.serializer = serializer;
|
|
312
|
+
this.options = options;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* The reader models a **newer** version; confirm it still reads what an older
|
|
316
|
+
* writer produced (stored events, in-flight messages). Optionally assert on
|
|
317
|
+
* the upgraded value — e.g. that a newly-added field defaults sensibly.
|
|
318
|
+
*/
|
|
319
|
+
async thenBackwardCompatible(assert) {
|
|
320
|
+
return this.check("backward", assert);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* The reader models an **older** version; confirm a consumer that has not
|
|
324
|
+
* upgraded yet still reads what the newer writer produces, so you can ship the
|
|
325
|
+
* new shape before every reader has caught up.
|
|
326
|
+
*/
|
|
327
|
+
async thenForwardCompatible(assert) {
|
|
328
|
+
return this.check("forward", assert);
|
|
329
|
+
}
|
|
330
|
+
async check(direction, assert) {
|
|
331
|
+
const serialized = this.resolveSourceSerialized();
|
|
332
|
+
const deserializedSource = this.serializer.deserialize(serialized);
|
|
333
|
+
const outcome = await read(this.reader, deserializedSource);
|
|
334
|
+
if (!outcome.ok) throw new ContractViolationError(`Not ${direction}-compatible: ${direction === "backward" ? "the newer reader" : "the older reader"} rejected a message ${direction === "backward" ? "an older writer" : "a newer writer"} produced.\n serializer: ${this.serializer.name}\n serialized: ${truncate(serialized)}\n issues:\n${outcome.issues.map((m) => ` - ${m}`).join("\n")}`);
|
|
335
|
+
this.assertStructuralCompatibility(deserializedSource, this.serializer.deserialize(this.serializer.serialize(outcome.value)), direction, serialized);
|
|
336
|
+
if (assert) await assert(outcome.value);
|
|
337
|
+
return outcome.value;
|
|
338
|
+
}
|
|
339
|
+
resolveSourceSerialized() {
|
|
340
|
+
if (isApprovedSnapshotSource(this.source)) {
|
|
341
|
+
const existing = readSnapshot(this.resolveSnapshotLocation(this.source.location));
|
|
342
|
+
if (!existing.exists || existing.content === void 0) throw new ContractViolationError(`Cannot read approved snapshot for compatibility check.\n snapshot: ${existing.path}\n\nRecord it first with .whenSerialized().thenContractIsUnchanged(), or point approvedSnapshot(...) at an existing file.`);
|
|
343
|
+
return existing.content;
|
|
344
|
+
}
|
|
345
|
+
return this.serializer.serialize(this.source);
|
|
346
|
+
}
|
|
347
|
+
resolveSnapshotLocation(location) {
|
|
348
|
+
if (typeof location === "string") return { name: location };
|
|
349
|
+
if (location) return location;
|
|
350
|
+
const configured = this.options.snapshot;
|
|
351
|
+
if (typeof configured === "string") return { name: configured };
|
|
352
|
+
if (configured) return configured;
|
|
353
|
+
throw new ContractViolationError("A snapshot location is required for approvedSnapshot(). Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).");
|
|
354
|
+
}
|
|
355
|
+
assertStructuralCompatibility(sourceValue, targetValue, direction, serialized) {
|
|
356
|
+
const mismatches = [];
|
|
357
|
+
compareSharedStructure(sourceValue, targetValue, "$", mismatches);
|
|
358
|
+
if (mismatches.length === 0) return;
|
|
359
|
+
throw new ContractViolationError(`Not ${direction}-compatible: shared fields changed meaning across versions.\n serializer: ${this.serializer.name}\n serialized: ${truncate(serialized)}\n mismatches:\n${mismatches.map((issue) => ` - ${issue}`).join("\n")}`);
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
function truncate(value, max = 400) {
|
|
363
|
+
return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;
|
|
364
|
+
}
|
|
365
|
+
function compareSharedStructure(sourceValue, targetValue, path, mismatches) {
|
|
366
|
+
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
367
|
+
const sourceKeys = Object.keys(sourceValue);
|
|
368
|
+
const targetKeys = Object.keys(targetValue);
|
|
369
|
+
const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));
|
|
370
|
+
const targetOnly = targetKeys.filter((key) => !(key in sourceValue));
|
|
371
|
+
if (sourceOnly.length > 0 && targetOnly.length > 0) {
|
|
372
|
+
mismatches.push(`${path}: structural incompatibility [source-only: ${sourceOnly.join(", ")}, target-only: ${targetOnly.join(", ")}]`);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) compareSharedStructure(sourceValue[key], targetValue[key], joinPath(path, key), mismatches);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {
|
|
379
|
+
if (sourceValue.length !== targetValue.length) {
|
|
380
|
+
mismatches.push(`${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
for (const [index, sourceItem] of sourceValue.entries()) compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
if (!deepEqual(sourceValue, targetValue)) mismatches.push(`${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`);
|
|
387
|
+
}
|
|
388
|
+
function isPlainObject(value) {
|
|
389
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
390
|
+
const proto = Object.getPrototypeOf(value);
|
|
391
|
+
return proto === Object.prototype || proto === null;
|
|
392
|
+
}
|
|
393
|
+
function deepEqual(left, right) {
|
|
394
|
+
if (Object.is(left, right)) return true;
|
|
395
|
+
if (Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((value, index) => deepEqual(value, right[index]));
|
|
396
|
+
if (isPlainObject(left) && isPlainObject(right)) {
|
|
397
|
+
const leftKeys = Object.keys(left);
|
|
398
|
+
const rightKeys = Object.keys(right);
|
|
399
|
+
return leftKeys.length === rightKeys.length && leftKeys.every((key) => key in right && deepEqual(left[key], right[key]));
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
function formatValue(value) {
|
|
404
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
405
|
+
if (value === null || typeof value === "number" || typeof value === "boolean" || value === void 0) return String(value);
|
|
406
|
+
return JSON.stringify(value);
|
|
407
|
+
}
|
|
408
|
+
function joinPath(path, key) {
|
|
409
|
+
return path === "$" ? `$.${key}` : `${path}.${key}`;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
//#endregion
|
|
413
|
+
export { ContractViolationError, approvedSnapshot, defaultSerializer, isUpdateMode, jsonSerializer, lineDiff, messageContract, read, readSnapshot, resolveSnapshotPath, writeSnapshot };
|
|
414
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/diff.ts","../src/reader.ts","../src/snapshot-storage.ts","../src/contract.ts"],"sourcesContent":["/**\n * Minimal line diff for snapshot failures. The goal is a message that points\n * at *what moved* — a renamed field, a switched date format, a dropped value —\n * not a full diff engine. Lines only in the approved file are marked `-`, lines\n * only in the actual output are marked `+`, and a little surrounding context is\n * kept so the change is readable in a terminal.\n */\nexport function lineDiff(approved: string, actual: string): string {\n const a = approved.split('\\n');\n const b = actual.split('\\n');\n const lcs = longestCommonSubsequence(a, b);\n\n const out: string[] = [];\n let i = 0;\n let j = 0;\n for (const [ai, bj] of lcs) {\n while (i < ai) out.push(`- ${a[i++]}`);\n while (j < bj) out.push(`+ ${b[j++]}`);\n out.push(` ${a[i++]}`);\n j++;\n }\n while (i < a.length) out.push(`- ${a[i++]}`);\n while (j < b.length) out.push(`+ ${b[j++]}`);\n\n return out.join('\\n');\n}\n\n/** Indices `[i, j]` of lines common to both, longest such subsequence. */\nfunction longestCommonSubsequence(\n a: string[],\n b: string[],\n): Array<[number, number]> {\n const n = a.length;\n const m = b.length;\n const table: number[][] = Array.from({ length: n + 1 }, () =>\n Array.from({ length: m + 1 }, () => 0),\n );\n for (let i = n - 1; i >= 0; i--) {\n for (let j = m - 1; j >= 0; j--) {\n table[i][j] =\n a[i] === b[j]\n ? table[i + 1][j + 1] + 1\n : Math.max(table[i + 1][j], table[i][j + 1]);\n }\n }\n const pairs: Array<[number, number]> = [];\n let i = 0;\n let j = 0;\n while (i < n && j < m) {\n if (a[i] === b[j]) {\n pairs.push([i, j]);\n i++;\n j++;\n } else if (table[i + 1][j] >= table[i][j + 1]) {\n i++;\n } else {\n j++;\n }\n }\n return pairs;\n}\n","/**\n * Readers — the \"as this version\" side of a compatibility check.\n *\n * In a JVM contract library you write `whenDeserializedAs(NewType.class)` and\n * reflection does the rest. TypeScript types are erased at runtime, so there is\n * no class to hand over. Instead you describe the *reader*: the thing that\n * accepts a deserialized value and either produces a typed result or rejects\n * it. Two shapes are accepted, in order of how most TS codebases already model\n * a message version:\n *\n * 1. A **Standard Schema** (Zod ≥3.24, Valibot, ArkType, …) — anything exposing\n * the `~standard` interface. This is the recommended form: the schema is the\n * version, and it already lives next to your message type.\n * 2. A plain **parse function** `(value) => T` that throws on incompatible input.\n *\n * A reader that accepts the value proves compatibility; a reader that throws or\n * reports issues proves the versions have drifted apart.\n */\n\n/** The subset of the Standard Schema v1 interface we rely on. */\nexport interface StandardSchemaLike<Output = unknown> {\n readonly '~standard': {\n readonly version: 1;\n readonly vendor: string;\n readonly validate: (value: unknown) =>\n | StandardResult<Output>\n | Promise<StandardResult<Output>>;\n };\n}\n\ninterface StandardResult<Output> {\n value?: Output;\n issues?: ReadonlyArray<{ readonly message: string; readonly path?: unknown }>;\n}\n\n/** A bare parse function: returns the typed value or throws. */\nexport type ParseFn<Output = unknown> = (value: unknown) => Output;\n\n/** Either accepted reader form. */\nexport type Reader<Output = unknown> =\n | StandardSchemaLike<Output>\n | ParseFn<Output>;\n\nfunction isStandardSchema(reader: Reader): reader is StandardSchemaLike {\n return (\n typeof reader === 'object' &&\n reader !== null &&\n '~standard' in reader &&\n typeof (reader as StandardSchemaLike)['~standard']?.validate === 'function'\n );\n}\n\nexport interface ReadOutcome<Output = unknown> {\n ok: boolean;\n /** Present when `ok`. */\n value?: Output;\n /** Human-readable reasons the reader rejected the value. */\n issues: string[];\n}\n\n/**\n * Run a reader against a deserialized value. Never throws — a thrown parse\n * error or reported issues become `{ ok: false, issues }` so the caller can\n * build a single, legible failure message.\n */\nexport async function read<Output>(\n reader: Reader<Output>,\n value: unknown,\n): Promise<ReadOutcome<Output>> {\n if (isStandardSchema(reader)) {\n try {\n const result = await reader['~standard'].validate(value);\n if (result.issues && result.issues.length > 0) {\n return {\n ok: false,\n issues: result.issues.map(\n (issue) => formatIssue(issue.message, issue.path),\n ),\n };\n }\n return { ok: true, value: result.value as Output, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n }\n\n try {\n const parsed = (reader as ParseFn<Output>)(value);\n return { ok: true, value: parsed, issues: [] };\n } catch (error) {\n return { ok: false, issues: [errorMessage(error)] };\n }\n}\n\nfunction formatIssue(message: string, path: unknown): string {\n if (Array.isArray(path) && path.length > 0) {\n return `${path.map(String).join('.')}: ${message}`;\n }\n return message;\n}\n\nfunction errorMessage(error: unknown): string {\n if (error instanceof Error) return error.message;\n return String(error);\n}\n","import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Snapshot storage.\n *\n * A snapshot is just a file you commit: the serialized shape of a message,\n * reviewed once and from then on compared on every run. There is no broker, no\n * registry, no service to start — a format change shows up in a normal diff,\n * reviewed like any other code. This mirrors the \"approved file\" convention\n * (`<name>.approved.txt`) familiar from approval testing.\n *\n * By default the file lives in a `__contracts__` directory beside the test that\n * created it, resolved from the call stack so you do not have to thread paths\n * around. Override `dir` / `path` when you keep snapshots elsewhere.\n */\n\n/** Set any of these (e.g. `AUTOTEL_CONTRACT_UPDATE=1`) to (re)write approved files. */\nconst UPDATE_ENV_VARS = [\n 'AUTOTEL_CONTRACT_UPDATE',\n 'UPDATE_CONTRACTS',\n 'UPDATE_SNAPSHOTS',\n] as const;\n\nexport function isUpdateMode(env: NodeJS.ProcessEnv = process.env): boolean {\n return UPDATE_ENV_VARS.some((name) => {\n const value = env[name];\n return value === '1' || value === 'true';\n });\n}\n\nexport interface SnapshotLocation {\n /** Directory holding the approved file. */\n dir?: string;\n /** Logical name; the file becomes `<name>.approved.txt`. */\n name: string;\n /** Fully-qualified path; when set, `dir`/`name` are ignored for placement. */\n path?: string;\n}\n\nconst DEFAULT_DIR_NAME = '__contracts__';\nconst APPROVED_SUFFIX = '.approved.txt';\n\n/** Resolve the absolute file path for a snapshot. */\nexport function resolveSnapshotPath(location: SnapshotLocation): string {\n if (location.path) {\n return path.isAbsolute(location.path)\n ? location.path\n : path.resolve(process.cwd(), location.path);\n }\n const dir = location.dir ?? defaultSnapshotDir();\n return path.join(dir, `${sanitize(location.name)}${APPROVED_SUFFIX}`);\n}\n\nfunction sanitize(name: string): string {\n // Keep it filesystem-safe while preserving readable type names.\n return name.replaceAll(/[^\\w.@-]+/g, '_');\n}\n\nexport interface ReadSnapshotResult {\n exists: boolean;\n content?: string;\n path: string;\n}\n\nexport function readSnapshot(location: SnapshotLocation): ReadSnapshotResult {\n const filePath = resolveSnapshotPath(location);\n if (!existsSync(filePath)) return { exists: false, path: filePath };\n return { exists: true, content: readFileSync(filePath, 'utf8'), path: filePath };\n}\n\nexport function writeSnapshot(\n location: SnapshotLocation,\n content: string,\n): string {\n const filePath = resolveSnapshotPath(location);\n mkdirSync(path.dirname(filePath), { recursive: true });\n writeFileSync(filePath, content, 'utf8');\n return filePath;\n}\n\n/**\n * Best-effort `__contracts__` directory beside the calling test file, found by\n * walking the stack past this module and the contract internals. Falls back to\n * `<cwd>/__contracts__` when the caller cannot be determined (e.g. bundled).\n */\nfunction defaultSnapshotDir(): string {\n const callerFile = callerOutsidePackage();\n const base = callerFile ? path.dirname(callerFile) : process.cwd();\n return path.join(base, DEFAULT_DIR_NAME);\n}\n\nfunction callerOutsidePackage(): string | undefined {\n const stack = new Error('stack probe').stack;\n if (!stack) return undefined;\n const lines = stack.split('\\n').slice(1);\n for (const line of lines) {\n const match = line.match(/\\((.*?):\\d+:\\d+\\)/) ?? line.match(/at (.*?):\\d+:\\d+/);\n const file = match?.[1];\n if (!file) continue;\n if (file.includes('node:')) continue;\n // Skip frames inside this package's own source/dist.\n if (file.includes(`${path.join('autotel-message-contract', 'src')}`)) continue;\n if (file.includes(`${path.join('autotel-message-contract', 'dist')}`)) continue;\n if (file.includes('node_modules')) continue;\n return file.startsWith('file://') ? fileUrlToPath(file) : file;\n }\n return undefined;\n}\n\nfunction fileUrlToPath(url: string): string {\n return decodeURIComponent(url.replace(/^file:\\/\\//, ''));\n}\n","/**\n * The message contract DSL.\n *\n * Every check starts from {@link messageContract} and reads as a sentence:\n *\n * ```ts\n * // Pin the serialized shape — fail when it drifts.\n * await messageContract()\n * .given(new OrderPlaced(orderId, 'Alice', placedAt))\n * .whenSerialized()\n * .thenContractIsUnchanged();\n *\n * // Prove a newer reader still reads what an older writer produced.\n * await messageContract()\n * .given(orderPlacedV1)\n * .whenDeserializedAs(OrderPlacedV2) // a Zod schema or parse fn\n * .thenBackwardCompatible((v2) => expect(v2.coupon).toBeUndefined());\n * ```\n *\n * A **snapshot check** confirms a message still serializes exactly as approved,\n * so nothing reading it downstream breaks. A **compatibility check** confirms an\n * older and a newer version can still read each other's data. Both are ordinary\n * unit tests — no broker, no registry, no running service.\n */\nimport { lineDiff } from './diff.js';\nimport { read, type Reader } from './reader.js';\nimport {\n defaultSerializer,\n type MessageSerializer,\n} from './serializer.js';\nimport {\n isUpdateMode,\n readSnapshot,\n type SnapshotLocation,\n writeSnapshot,\n} from './snapshot-storage.js';\n\n/** Thrown when a contract check fails. Message is pre-formatted for a test runner. */\nexport class ContractViolationError extends Error {\n override readonly name = 'ContractViolationError';\n constructor(message: string) {\n super(message);\n }\n}\n\nexport interface MessageContractOptions {\n /** Serializer producing the bytes you ship. Defaults to deterministic JSON. */\n serializer?: MessageSerializer<string>;\n /**\n * Where approved files live and what they are named. A bare string is used as\n * the logical name; an object gives full control over `dir`/`path`.\n */\n snapshot?: string | SnapshotLocation;\n /** Override update-mode detection (default reads env, e.g. AUTOTEL_CONTRACT_UPDATE=1). */\n update?: boolean;\n}\n\nconst SNAPSHOT_SOURCE = Symbol('autotel-message-contract.snapshot-source');\n\nexport interface ApprovedSnapshotSource {\n readonly [SNAPSHOT_SOURCE]: true;\n readonly location?: string | SnapshotLocation;\n}\n\n/**\n * Point a compatibility check at a previously approved snapshot instead of a\n * live in-memory message instance.\n */\nexport function approvedSnapshot(\n location?: string | SnapshotLocation,\n): ApprovedSnapshotSource {\n return {\n [SNAPSHOT_SOURCE]: true,\n location,\n };\n}\n\nfunction isApprovedSnapshotSource(value: unknown): value is ApprovedSnapshotSource {\n return (\n typeof value === 'object' &&\n value !== null &&\n SNAPSHOT_SOURCE in value &&\n (value as ApprovedSnapshotSource)[SNAPSHOT_SOURCE] === true\n );\n}\n\n/** Start a contract check. */\nexport function messageContract(options: MessageContractOptions = {}): GivenStep {\n return new GivenStep(options);\n}\n\nclass GivenStep {\n constructor(private readonly options: MessageContractOptions) {}\n\n /** The message under contract. */\n given<T>(message: T | ApprovedSnapshotSource): WhenStep<T> {\n return new WhenStep(message, this.options);\n }\n}\n\nclass WhenStep<T> {\n constructor(\n private readonly message: T | ApprovedSnapshotSource,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** Serialize the message; the next step pins or inspects the result. */\n whenSerialized(): SnapshotStep {\n if (isApprovedSnapshotSource(this.message)) {\n throw new ContractViolationError(\n 'Cannot serialize an approved snapshot source. ' +\n 'Use .whenDeserializedAs(...) to run a compatibility check, or ' +\n 'pass a live message instance to .given(...).',\n );\n }\n const serializer = this.options.serializer ?? defaultSerializer;\n const serialized = serializer.serialize(this.message);\n return new SnapshotStep(serialized, serializer, this.options);\n }\n\n /**\n * Round-trip the message through a reader that models a *different version*\n * (a Standard Schema such as Zod/Valibot, or a parse function). The next step\n * asserts the versions stay compatible.\n */\n whenDeserializedAs<Output>(reader: Reader<Output>): CompatibilityStep<Output> {\n const serializer = this.options.serializer ?? defaultSerializer;\n return new CompatibilityStep(this.message, reader, serializer, this.options);\n }\n}\n\nclass SnapshotStep {\n constructor(\n private readonly serialized: string,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /** The serialized bytes, for ad-hoc assertions outside the snapshot flow. */\n get output(): string {\n return this.serialized;\n }\n\n /**\n * Compare the serialized output against the approved snapshot. On first run\n * (or in update mode) it writes the approved file and passes; afterwards it\n * fails with a diff when the shape drifts.\n */\n thenContractIsUnchanged(snapshotName?: string): void {\n const location = this.resolveLocation(snapshotName);\n const existing = readSnapshot(location);\n const update = this.options.update ?? isUpdateMode();\n\n if (!existing.exists || update) {\n const path = writeSnapshot(location, this.serialized);\n if (!existing.exists) {\n // First run: record and pass, leaving the file to be reviewed/committed.\n return;\n }\n // Update mode: rewrite and pass even if it changed.\n void path;\n return;\n }\n\n if (existing.content !== this.serialized) {\n throw new ContractViolationError(\n `Message contract drifted from its approved snapshot.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `${lineDiff(existing.content ?? '', this.serialized)}\\n\\n` +\n `If this change is intentional, re-run with AUTOTEL_CONTRACT_UPDATE=1 ` +\n `to update the approved file, then review and commit it.`,\n );\n }\n }\n\n private resolveLocation(snapshotName?: string): SnapshotLocation {\n if (snapshotName) return { name: snapshotName };\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n throw new ContractViolationError(\n `A snapshot name is required. Pass one to messageContract({ snapshot: 'OrderPlaced' }) ` +\n `or to thenContractIsUnchanged('OrderPlaced').`,\n );\n }\n}\n\nclass CompatibilityStep<Output> {\n constructor(\n private readonly source: unknown,\n private readonly reader: Reader<Output>,\n private readonly serializer: MessageSerializer<string>,\n private readonly options: MessageContractOptions,\n ) {}\n\n /**\n * The reader models a **newer** version; confirm it still reads what an older\n * writer produced (stored events, in-flight messages). Optionally assert on\n * the upgraded value — e.g. that a newly-added field defaults sensibly.\n */\n async thenBackwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('backward', assert);\n }\n\n /**\n * The reader models an **older** version; confirm a consumer that has not\n * upgraded yet still reads what the newer writer produces, so you can ship the\n * new shape before every reader has caught up.\n */\n async thenForwardCompatible(\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n return this.check('forward', assert);\n }\n\n private async check(\n direction: 'backward' | 'forward',\n assert?: (value: Output) => void | Promise<void>,\n ): Promise<Output> {\n const serialized = this.resolveSourceSerialized();\n const deserializedSource = this.serializer.deserialize(serialized);\n const outcome = await read(this.reader, deserializedSource);\n\n if (!outcome.ok) {\n const writer = direction === 'backward' ? 'an older writer' : 'a newer writer';\n const readerLabel =\n direction === 'backward' ? 'the newer reader' : 'the older reader';\n throw new ContractViolationError(\n `Not ${direction}-compatible: ${readerLabel} rejected a message ${writer} produced.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` issues:\\n${outcome.issues.map((m) => ` - ${m}`).join('\\n')}`,\n );\n }\n\n this.assertStructuralCompatibility(\n deserializedSource,\n this.serializer.deserialize(this.serializer.serialize(outcome.value)),\n direction,\n serialized,\n );\n\n if (assert) await assert(outcome.value as Output);\n return outcome.value as Output;\n }\n\n private resolveSourceSerialized(): string {\n if (isApprovedSnapshotSource(this.source)) {\n const location = this.resolveSnapshotLocation(this.source.location);\n const existing = readSnapshot(location);\n if (!existing.exists || existing.content === undefined) {\n throw new ContractViolationError(\n `Cannot read approved snapshot for compatibility check.\\n` +\n ` snapshot: ${existing.path}\\n\\n` +\n `Record it first with .whenSerialized().thenContractIsUnchanged(), ` +\n `or point approvedSnapshot(...) at an existing file.`,\n );\n }\n return existing.content;\n }\n return this.serializer.serialize(this.source);\n }\n\n private resolveSnapshotLocation(\n location?: string | SnapshotLocation,\n ): SnapshotLocation {\n if (typeof location === 'string') return { name: location };\n if (location) return location;\n\n const configured = this.options.snapshot;\n if (typeof configured === 'string') return { name: configured };\n if (configured) return configured;\n\n throw new ContractViolationError(\n 'A snapshot location is required for approvedSnapshot(). ' +\n `Pass approvedSnapshot('OrderPlaced_v1') or configure messageContract({ snapshot: 'OrderPlaced_v1' }).`,\n );\n }\n\n private assertStructuralCompatibility(\n sourceValue: unknown,\n targetValue: unknown,\n direction: 'backward' | 'forward',\n serialized: string,\n ): void {\n const mismatches: string[] = [];\n compareSharedStructure(sourceValue, targetValue, '$', mismatches);\n\n if (mismatches.length === 0) return;\n\n throw new ContractViolationError(\n `Not ${direction}-compatible: shared fields changed meaning across versions.\\n` +\n ` serializer: ${this.serializer.name}\\n` +\n ` serialized: ${truncate(serialized)}\\n` +\n ` mismatches:\\n${mismatches.map((issue) => ` - ${issue}`).join('\\n')}`,\n );\n }\n}\n\nfunction truncate(value: string, max = 400): string {\n return value.length > max ? `${value.slice(0, max)}… (${value.length} chars)` : value;\n}\n\nfunction compareSharedStructure(\n sourceValue: unknown,\n targetValue: unknown,\n path: string,\n mismatches: string[],\n): void {\n if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {\n const sourceKeys = Object.keys(sourceValue);\n const targetKeys = Object.keys(targetValue);\n const sourceOnly = sourceKeys.filter((key) => !(key in targetValue));\n const targetOnly = targetKeys.filter((key) => !(key in sourceValue));\n\n if (sourceOnly.length > 0 && targetOnly.length > 0) {\n mismatches.push(\n `${path}: structural incompatibility ` +\n `[source-only: ${sourceOnly.join(', ')}, target-only: ${targetOnly.join(', ')}]`,\n );\n return;\n }\n\n for (const key of sourceKeys.filter((candidate) => candidate in targetValue).toSorted()) {\n compareSharedStructure(\n sourceValue[key],\n targetValue[key],\n joinPath(path, key),\n mismatches,\n );\n }\n return;\n }\n\n if (Array.isArray(sourceValue) && Array.isArray(targetValue)) {\n if (sourceValue.length !== targetValue.length) {\n mismatches.push(\n `${path}: array length differs (${sourceValue.length} vs ${targetValue.length})`,\n );\n return;\n }\n\n for (const [index, sourceItem] of sourceValue.entries()) {\n compareSharedStructure(sourceItem, targetValue[index], `${path}[${index}]`, mismatches);\n }\n return;\n }\n\n if (!deepEqual(sourceValue, targetValue)) {\n mismatches.push(\n `${path}: value differs (${formatValue(sourceValue)} vs ${formatValue(targetValue)})`,\n );\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nfunction deepEqual(left: unknown, right: unknown): boolean {\n if (Object.is(left, right)) return true;\n\n if (Array.isArray(left) && Array.isArray(right)) {\n return (\n left.length === right.length &&\n left.every((value, index) => deepEqual(value, right[index]))\n );\n }\n\n if (isPlainObject(left) && isPlainObject(right)) {\n const leftKeys = Object.keys(left);\n const rightKeys = Object.keys(right);\n return (\n leftKeys.length === rightKeys.length &&\n leftKeys.every((key) => key in right && deepEqual(left[key], right[key]))\n );\n }\n\n return false;\n}\n\nfunction formatValue(value: unknown): string {\n if (typeof value === 'string') return JSON.stringify(value);\n if (\n value === null ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === undefined\n ) {\n return String(value);\n }\n return JSON.stringify(value);\n}\n\nfunction joinPath(path: string, key: string): string {\n return path === '$' ? `$.${key}` : `${path}.${key}`;\n}\n\nexport type {\n GivenStep,\n WhenStep,\n SnapshotStep,\n CompatibilityStep,\n};\n"],"mappings":";;;;;;;;;;;;AAOA,SAAgB,SAAS,UAAkB,QAAwB;CACjE,MAAM,IAAI,SAAS,MAAM,IAAI;CAC7B,MAAM,IAAI,OAAO,MAAM,IAAI;CAC3B,MAAM,MAAM,yBAAyB,GAAG,CAAC;CAEzC,MAAM,MAAgB,CAAC;CACvB,IAAI,IAAI;CACR,IAAI,IAAI;CACR,KAAK,MAAM,CAAC,IAAI,OAAO,KAAK;EAC1B,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,OAAO,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE,MAAM;EACrC,IAAI,KAAK,KAAK,EAAE,MAAM;EACtB;CACF;CACA,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAC3C,OAAO,IAAI,EAAE,QAAQ,IAAI,KAAK,KAAK,EAAE,MAAM;CAE3C,OAAO,IAAI,KAAK,IAAI;AACtB;;AAGA,SAAS,yBACP,GACA,GACyB;CACzB,MAAM,IAAI,EAAE;CACZ,MAAM,IAAI,EAAE;CACZ,MAAM,QAAoB,MAAM,KAAK,EA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EACnC,OACE,SAAS,WAAW,UAAU,UAC9B,SAAS,OAAO,QAAQ,OAAO,SAAS,UAAU,KAAK,MAAM,MAAM,IAAI,CAAC;CAE5E;CAEA,OAAO;AACT;AAEA,SAAS,YAAY,OAAwB;CAC3C,IAAI,OAAO,UAAU,UAAU,OAAO,KAAK,UAAU,KAAK;CAC1D,IACE,UAAU,QACV,OAAO,UAAU,YACjB,OAAO,UAAU,aACjB,UAAU,QAEV,OAAO,OAAO,KAAK;CAErB,OAAO,KAAK,UAAU,KAAK;AAC7B;AAEA,SAAS,SAAS,MAAc,KAAqB;CACnD,OAAO,SAAS,MAAM,KAAK,QAAQ,GAAG,KAAK,GAAG;AAChD"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
|
|
3
|
+
//#region src/serializer.ts
|
|
4
|
+
/**
|
|
5
|
+
* Recursively sort object keys so two values with the same fields serialize
|
|
6
|
+
* identically regardless of insertion order. Arrays keep their order (it is
|
|
7
|
+
* semantically meaningful); plain objects are reordered; class instances,
|
|
8
|
+
* Maps, Sets, Dates, etc. are left untouched so the serializer can decide.
|
|
9
|
+
*/
|
|
10
|
+
function sortKeysDeep(value) {
|
|
11
|
+
if (Array.isArray(value)) return value.map((element) => sortKeysDeep(element));
|
|
12
|
+
if (value !== null && typeof value === "object" && isPlainObject(value)) {
|
|
13
|
+
const sorted = {};
|
|
14
|
+
for (const key of Object.keys(value).toSorted()) sorted[key] = sortKeysDeep(value[key]);
|
|
15
|
+
return sorted;
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function isPlainObject(value) {
|
|
20
|
+
const proto = Object.getPrototypeOf(value);
|
|
21
|
+
return proto === Object.prototype || proto === null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The default serializer: `JSON.stringify` with deterministic key ordering.
|
|
25
|
+
* Good enough to pin most events and commands; swap it for your own when the
|
|
26
|
+
* bytes you ship differ (custom date formats, snake_case, omitted nulls…).
|
|
27
|
+
*/
|
|
28
|
+
function jsonSerializer(options = {}) {
|
|
29
|
+
const { indent = 2, sortKeys = true } = options;
|
|
30
|
+
return {
|
|
31
|
+
name: sortKeys ? "json" : "json (key-order preserved)",
|
|
32
|
+
serialize(value) {
|
|
33
|
+
const prepared = sortKeys ? sortKeysDeep(value) : value;
|
|
34
|
+
return JSON.stringify(prepared, void 0, indent);
|
|
35
|
+
},
|
|
36
|
+
deserialize(serialized) {
|
|
37
|
+
return JSON.parse(serialized);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** The serializer used when a contract does not specify one. */
|
|
42
|
+
const defaultSerializer = jsonSerializer();
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.defaultSerializer = defaultSerializer;
|
|
46
|
+
exports.jsonSerializer = jsonSerializer;
|
|
47
|
+
//# sourceMappingURL=serializer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.cjs","names":[],"sources":["../src/serializer.ts"],"sourcesContent":["/**\n * Message serializers.\n *\n * A contract pins the bytes a message becomes *once serialized* — the exact\n * shape a consumer, a queue, or a stored event reads. The only way that\n * snapshot is meaningful is if it is produced by the **same serializer your\n * application ships with**. Pin a shape your consumers never see and you have\n * pinned nothing.\n *\n * So a {@link MessageSerializer} is a tiny, explicit seam: `serialize` /\n * `deserialize`. The default is JSON with deterministic key ordering (so a\n * snapshot does not churn when object construction order changes), but you are\n * encouraged to pass your app's real serializer — `superjson`, `devalue`, a\n * snake_case Jackson-equivalent, a protobuf codec — so the snapshot records the\n * exact bytes you put on the wire.\n */\n\n/** A reversible mapping between an in-memory value and its serialized form. */\nexport interface MessageSerializer<Serialized = string> {\n /** Human-facing name, surfaced in snapshot headers and failure messages. */\n readonly name: string;\n /** Turn a value into its serialized form (the bytes you ship). */\n serialize(value: unknown): Serialized;\n /** Turn a serialized form back into a value. */\n deserialize(serialized: Serialized): unknown;\n}\n\n/**\n * Recursively sort object keys so two values with the same fields serialize\n * identically regardless of insertion order. Arrays keep their order (it is\n * semantically meaningful); plain objects are reordered; class instances,\n * Maps, Sets, Dates, etc. are left untouched so the serializer can decide.\n */\nfunction sortKeysDeep(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map((element) => sortKeysDeep(element));\n }\n if (value !== null && typeof value === 'object' && isPlainObject(value)) {\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(value).toSorted()) {\n sorted[key] = sortKeysDeep((value as Record<string, unknown>)[key]);\n }\n return sorted;\n }\n return value;\n}\n\nfunction isPlainObject(value: object): boolean {\n const proto = Object.getPrototypeOf(value);\n return proto === Object.prototype || proto === null;\n}\n\nexport interface JsonSerializerOptions {\n /**\n * Pretty-print with this indent. Defaults to `2` so the approved file reads\n * cleanly in a diff. Set to `0` to pin the compact bytes you actually ship.\n */\n indent?: number;\n /**\n * Sort object keys deterministically before serializing. Defaults to `true`\n * so a snapshot reflects *fields*, not construction order. Turn it off when\n * key order is itself part of the contract.\n */\n sortKeys?: boolean;\n}\n\n/**\n * The default serializer: `JSON.stringify` with deterministic key ordering.\n * Good enough to pin most events and commands; swap it for your own when the\n * bytes you ship differ (custom date formats, snake_case, omitted nulls…).\n */\nexport function jsonSerializer(\n options: JsonSerializerOptions = {},\n): MessageSerializer<string> {\n const { indent = 2, sortKeys = true } = options;\n return {\n name: sortKeys ? 'json' : 'json (key-order preserved)',\n serialize(value) {\n const prepared = sortKeys ? sortKeysDeep(value) : value;\n return JSON.stringify(prepared, undefined, indent);\n },\n deserialize(serialized) {\n return JSON.parse(serialized) as unknown;\n },\n };\n}\n\n/** The serializer used when a contract does not specify one. */\nexport const defaultSerializer: MessageSerializer<string> = jsonSerializer();\n"],"mappings":";;;;;;;;;AAiCA,SAAS,aAAa,OAAyB;CAC7C,IAAI,MAAM,QAAQ,KAAK,GACrB,OAAO,MAAM,KAAK,YAAY,aAAa,OAAO,CAAC;CAErD,IAAI,UAAU,QAAQ,OAAO,UAAU,YAAY,cAAc,KAAK,GAAG;EACvE,MAAM,SAAkC,CAAC;EACzC,KAAK,MAAM,OAAO,OAAO,KAAK,KAAK,CAAC,CAAC,SAAS,GAC5C,OAAO,OAAO,aAAc,MAAkC,IAAI;EAEpE,OAAO;CACT;CACA,OAAO;AACT;AAEA,SAAS,cAAc,OAAwB;CAC7C,MAAM,QAAQ,OAAO,eAAe,KAAK;CACzC,OAAO,UAAU,OAAO,aAAa,UAAU;AACjD;;;;;;AAqBA,SAAgB,eACd,UAAiC,CAAC,GACP;CAC3B,MAAM,EAAE,SAAS,GAAG,WAAW,SAAS;CACxC,OAAO;EACL,MAAM,WAAW,SAAS;EAC1B,UAAU,OAAO;GACf,MAAM,WAAW,WAAW,aAAa,KAAK,IAAI;GAClD,OAAO,KAAK,UAAU,UAAU,QAAW,MAAM;EACnD;EACA,YAAY,YAAY;GACtB,OAAO,KAAK,MAAM,UAAU;EAC9B;CACF;AACF;;AAGA,MAAa,oBAA+C,eAAe"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region src/serializer.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Message serializers.
|
|
4
|
+
*
|
|
5
|
+
* A contract pins the bytes a message becomes *once serialized* — the exact
|
|
6
|
+
* shape a consumer, a queue, or a stored event reads. The only way that
|
|
7
|
+
* snapshot is meaningful is if it is produced by the **same serializer your
|
|
8
|
+
* application ships with**. Pin a shape your consumers never see and you have
|
|
9
|
+
* pinned nothing.
|
|
10
|
+
*
|
|
11
|
+
* So a {@link MessageSerializer} is a tiny, explicit seam: `serialize` /
|
|
12
|
+
* `deserialize`. The default is JSON with deterministic key ordering (so a
|
|
13
|
+
* snapshot does not churn when object construction order changes), but you are
|
|
14
|
+
* encouraged to pass your app's real serializer — `superjson`, `devalue`, a
|
|
15
|
+
* snake_case Jackson-equivalent, a protobuf codec — so the snapshot records the
|
|
16
|
+
* exact bytes you put on the wire.
|
|
17
|
+
*/
|
|
18
|
+
/** A reversible mapping between an in-memory value and its serialized form. */
|
|
19
|
+
interface MessageSerializer<Serialized = string> {
|
|
20
|
+
/** Human-facing name, surfaced in snapshot headers and failure messages. */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
/** Turn a value into its serialized form (the bytes you ship). */
|
|
23
|
+
serialize(value: unknown): Serialized;
|
|
24
|
+
/** Turn a serialized form back into a value. */
|
|
25
|
+
deserialize(serialized: Serialized): unknown;
|
|
26
|
+
}
|
|
27
|
+
interface JsonSerializerOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Pretty-print with this indent. Defaults to `2` so the approved file reads
|
|
30
|
+
* cleanly in a diff. Set to `0` to pin the compact bytes you actually ship.
|
|
31
|
+
*/
|
|
32
|
+
indent?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Sort object keys deterministically before serializing. Defaults to `true`
|
|
35
|
+
* so a snapshot reflects *fields*, not construction order. Turn it off when
|
|
36
|
+
* key order is itself part of the contract.
|
|
37
|
+
*/
|
|
38
|
+
sortKeys?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The default serializer: `JSON.stringify` with deterministic key ordering.
|
|
42
|
+
* Good enough to pin most events and commands; swap it for your own when the
|
|
43
|
+
* bytes you ship differ (custom date formats, snake_case, omitted nulls…).
|
|
44
|
+
*/
|
|
45
|
+
declare function jsonSerializer(options?: JsonSerializerOptions): MessageSerializer<string>;
|
|
46
|
+
/** The serializer used when a contract does not specify one. */
|
|
47
|
+
declare const defaultSerializer: MessageSerializer<string>;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { JsonSerializerOptions, MessageSerializer, defaultSerializer, jsonSerializer };
|
|
50
|
+
//# sourceMappingURL=serializer.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.d.cts","names":[],"sources":["../src/serializer.ts"],"mappings":";;AAkBA;;;;;;;;;;;;;AAMoC;AA4BpC;;UAlCiB,iBAAA;EAuCf;EAAA,SArCS,IAAA;EAmDK;EAjDd,SAAA,CAAU,KAAA,YAAiB,UAAA;;EAE3B,WAAA,CAAY,UAAA,EAAY,UAAU;AAAA;AAAA,UA4BnB,qBAAA;EAqBd;;AAAiB;AAepB;EA/BE,MAAA;;;AA+B+C;;;EAzB/C,QAAQ;AAAA;;;;;;iBAQM,cAAA,CACd,OAAA,GAAS,qBAAA,GACR,iBAAiB;;cAeP,iBAAA,EAAmB,iBAAiB"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region src/serializer.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Message serializers.
|
|
4
|
+
*
|
|
5
|
+
* A contract pins the bytes a message becomes *once serialized* — the exact
|
|
6
|
+
* shape a consumer, a queue, or a stored event reads. The only way that
|
|
7
|
+
* snapshot is meaningful is if it is produced by the **same serializer your
|
|
8
|
+
* application ships with**. Pin a shape your consumers never see and you have
|
|
9
|
+
* pinned nothing.
|
|
10
|
+
*
|
|
11
|
+
* So a {@link MessageSerializer} is a tiny, explicit seam: `serialize` /
|
|
12
|
+
* `deserialize`. The default is JSON with deterministic key ordering (so a
|
|
13
|
+
* snapshot does not churn when object construction order changes), but you are
|
|
14
|
+
* encouraged to pass your app's real serializer — `superjson`, `devalue`, a
|
|
15
|
+
* snake_case Jackson-equivalent, a protobuf codec — so the snapshot records the
|
|
16
|
+
* exact bytes you put on the wire.
|
|
17
|
+
*/
|
|
18
|
+
/** A reversible mapping between an in-memory value and its serialized form. */
|
|
19
|
+
interface MessageSerializer<Serialized = string> {
|
|
20
|
+
/** Human-facing name, surfaced in snapshot headers and failure messages. */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
/** Turn a value into its serialized form (the bytes you ship). */
|
|
23
|
+
serialize(value: unknown): Serialized;
|
|
24
|
+
/** Turn a serialized form back into a value. */
|
|
25
|
+
deserialize(serialized: Serialized): unknown;
|
|
26
|
+
}
|
|
27
|
+
interface JsonSerializerOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Pretty-print with this indent. Defaults to `2` so the approved file reads
|
|
30
|
+
* cleanly in a diff. Set to `0` to pin the compact bytes you actually ship.
|
|
31
|
+
*/
|
|
32
|
+
indent?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Sort object keys deterministically before serializing. Defaults to `true`
|
|
35
|
+
* so a snapshot reflects *fields*, not construction order. Turn it off when
|
|
36
|
+
* key order is itself part of the contract.
|
|
37
|
+
*/
|
|
38
|
+
sortKeys?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The default serializer: `JSON.stringify` with deterministic key ordering.
|
|
42
|
+
* Good enough to pin most events and commands; swap it for your own when the
|
|
43
|
+
* bytes you ship differ (custom date formats, snake_case, omitted nulls…).
|
|
44
|
+
*/
|
|
45
|
+
declare function jsonSerializer(options?: JsonSerializerOptions): MessageSerializer<string>;
|
|
46
|
+
/** The serializer used when a contract does not specify one. */
|
|
47
|
+
declare const defaultSerializer: MessageSerializer<string>;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { JsonSerializerOptions, MessageSerializer, defaultSerializer, jsonSerializer };
|
|
50
|
+
//# sourceMappingURL=serializer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.d.ts","names":[],"sources":["../src/serializer.ts"],"mappings":";;AAkBA;;;;;;;;;;;;;AAMoC;AA4BpC;;UAlCiB,iBAAA;EAuCf;EAAA,SArCS,IAAA;EAmDK;EAjDd,SAAA,CAAU,KAAA,YAAiB,UAAA;;EAE3B,WAAA,CAAY,UAAA,EAAY,UAAU;AAAA;AAAA,UA4BnB,qBAAA;EAqBd;;AAAiB;AAepB;EA/BE,MAAA;;;AA+B+C;;;EAzB/C,QAAQ;AAAA;;;;;;iBAQM,cAAA,CACd,OAAA,GAAS,qBAAA,GACR,iBAAiB;;cAeP,iBAAA,EAAmB,iBAAiB"}
|