1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! What:
//! Reading and writing the compact JSON records both wrappers share,
//! with the
//! incumbent's `JSON.parse` and `JSON.stringify` semantics.
//! Why:
//! Lock,
//! owner,
//! journal and capture records written by either wrapper are read by the
//! other.
//! A record must be read the way the incumbent reads it (numbers are doubles,
//! a
//! whole number in safe-integer range is an integer,
//! the last duplicate key wins) and
//! written with the same field order and escaping.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! const value: unknown = JSON.parse(text); const bytes = `${JSON.stringify(record)}\n`;
//! ```
/// What:
/// `Map` is a JSON object's key/value table and `Value` any JSON value.
/// Why:
/// Records are untrusted until each field is checked,
/// so they are parsed into the
/// general form first.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type Value = unknown;
/// ```
use ;
/// The largest whole number a double holds exactly,
/// JavaScript's `Number.MAX_SAFE_INTEGER`.
pub const MAX_SAFE_INTEGER: i64 = 9_007_199_254_740_991;
/// What:
/// Parse record text into a JSON object,
/// or nothing for any other document.
/// `Option<Map<String, Value>>` is "an object,
/// or nothing".
/// Why:
/// Every record is an object;
/// an array,
/// a scalar or text that is not JSON is malformed.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function parseObject(text: string): Record<string, unknown> | undefined;
/// ```
/// What:
/// Decode record bytes the way a `fatal` `TextDecoder` does:
/// valid UTF-8 only,
/// with one
/// leading byte-order mark removed.
/// Why:
/// The incumbent decodes owner and journal records with `new TextDecoder('utf-8',
/// { fatal: true })`,
/// which rejects invalid bytes and drops a leading mark.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// new TextDecoder('utf-8', { fatal: true }).decode(bytes)
/// ```
/// What:
/// Read a field as text,
/// or nothing when it is absent or not a string.
/// Why:
/// `typeof value.key === 'string'` is the incumbent's check.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// typeof value[key] === 'string' ? value[key] : undefined
/// ```
/// What:
/// Read a field as a JavaScript safe integer,
/// or nothing.
/// `i64` is a signed 64-bit integer.
/// Why:
/// `typeof value === 'number' && Number.isSafeInteger(value)` accepts `1`,
/// `1.0` and
/// `1e0` alike,
/// because JavaScript has only doubles;
/// anything with a fraction or beyond
/// 2^53 - 1 is refused.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// typeof value[key] === 'number' && Number.isSafeInteger(value[key]) ? value[key] : undefined
/// ```
/// What:
/// The safe-integer reading of one JSON value.
/// Why:
/// Shared by object fields and array items.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function safeInteger(value: unknown): number | undefined;
/// ```
/// What:
/// Keep a whole number only when it lies within the safe range.
/// Why:
/// `Number.isSafeInteger` rejects magnitudes beyond 2^53 - 1.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// Math.abs(whole) <= Number.MAX_SAFE_INTEGER ? whole : undefined
/// ```
/// What:
/// Whether a field holds exactly the JSON number `expected`.
/// Why:
/// `value.schemaVersion !== 1` compares doubles,
/// so `1.0` matches `1`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// value[key] === expected
/// ```
/// What:
/// Builder of one compact JSON object in insertion order,
/// as `JSON.stringify` writes it.
/// A `struct` groups named fields;
/// this one holds the text written so far.
/// Why:
/// Records must list their fields in the incumbent's order so the bytes match for the
/// same values;
/// escaping comes from `serde_json`,
/// which escapes the same characters
/// `JSON.stringify` does.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// JSON.stringify({ a: 1, b: 'x' })
/// ```
/// Methods that append one field each and finish the object.
/// What:
/// Quote text as a JSON string literal.
/// Why:
/// `serde_json` escapes `"`,
/// `\` and control characters as `JSON.stringify` does;
/// serializing a `&str` cannot fail.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// JSON.stringify(text)
/// ```
/// What:
/// A JSON array of text items.
/// Why:
/// Pathspec and path lists in journal records.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// JSON.stringify(items)
/// ```
/// Parsing and writing controls stay out of the release executable.