@photon-grove/evt 0.0.0-stage → 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.
Files changed (124) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +122 -2
  3. package/dist/aggregate.d.ts +83 -0
  4. package/dist/aggregate.d.ts.map +1 -0
  5. package/dist/aggregate.js +92 -0
  6. package/dist/aggregate.js.map +1 -0
  7. package/dist/base64.d.ts +8 -0
  8. package/dist/base64.d.ts.map +1 -0
  9. package/dist/base64.js +67 -0
  10. package/dist/base64.js.map +1 -0
  11. package/dist/codec.d.ts +56 -0
  12. package/dist/codec.d.ts.map +1 -0
  13. package/dist/codec.js +90 -0
  14. package/dist/codec.js.map +1 -0
  15. package/dist/commit-id.d.ts +3 -0
  16. package/dist/commit-id.d.ts.map +1 -0
  17. package/dist/commit-id.js +43 -0
  18. package/dist/commit-id.js.map +1 -0
  19. package/dist/dynamodb/head.d.ts +34 -0
  20. package/dist/dynamodb/head.d.ts.map +1 -0
  21. package/dist/dynamodb/head.js +62 -0
  22. package/dist/dynamodb/head.js.map +1 -0
  23. package/dist/dynamodb/index.d.ts +2 -0
  24. package/dist/dynamodb/index.d.ts.map +1 -0
  25. package/dist/dynamodb/index.js +4 -0
  26. package/dist/dynamodb/index.js.map +1 -0
  27. package/dist/dynamodb/item.d.ts +68 -0
  28. package/dist/dynamodb/item.d.ts.map +1 -0
  29. package/dist/dynamodb/item.js +263 -0
  30. package/dist/dynamodb/item.js.map +1 -0
  31. package/dist/envelope.d.ts +15 -0
  32. package/dist/envelope.d.ts.map +1 -0
  33. package/dist/envelope.js +59 -0
  34. package/dist/envelope.js.map +1 -0
  35. package/dist/errors.d.ts +111 -0
  36. package/dist/errors.d.ts.map +1 -0
  37. package/dist/errors.js +139 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/event.d.ts +47 -0
  40. package/dist/event.d.ts.map +1 -0
  41. package/dist/event.js +64 -0
  42. package/dist/event.js.map +1 -0
  43. package/dist/fields.d.ts +31 -0
  44. package/dist/fields.d.ts.map +1 -0
  45. package/dist/fields.js +105 -0
  46. package/dist/fields.js.map +1 -0
  47. package/dist/index.d.ts +14 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +16 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/json.d.ts +47 -0
  52. package/dist/json.d.ts.map +1 -0
  53. package/dist/json.js +458 -0
  54. package/dist/json.js.map +1 -0
  55. package/dist/memory/index.d.ts +2 -0
  56. package/dist/memory/index.d.ts.map +1 -0
  57. package/dist/memory/index.js +3 -0
  58. package/dist/memory/index.js.map +1 -0
  59. package/dist/memory/repository.d.ts +34 -0
  60. package/dist/memory/repository.d.ts.map +1 -0
  61. package/dist/memory/repository.js +219 -0
  62. package/dist/memory/repository.js.map +1 -0
  63. package/dist/metadata.d.ts +47 -0
  64. package/dist/metadata.d.ts.map +1 -0
  65. package/dist/metadata.js +75 -0
  66. package/dist/metadata.js.map +1 -0
  67. package/dist/repository.d.ts +102 -0
  68. package/dist/repository.d.ts.map +1 -0
  69. package/dist/repository.js +131 -0
  70. package/dist/repository.js.map +1 -0
  71. package/dist/retry.d.ts +25 -0
  72. package/dist/retry.d.ts.map +1 -0
  73. package/dist/retry.js +50 -0
  74. package/dist/retry.js.map +1 -0
  75. package/dist/snapshots.d.ts +21 -0
  76. package/dist/snapshots.d.ts.map +1 -0
  77. package/dist/snapshots.js +35 -0
  78. package/dist/snapshots.js.map +1 -0
  79. package/dist/store.d.ts +103 -0
  80. package/dist/store.d.ts.map +1 -0
  81. package/dist/store.js +246 -0
  82. package/dist/store.js.map +1 -0
  83. package/dist/testing/equal.d.ts +22 -0
  84. package/dist/testing/equal.d.ts.map +1 -0
  85. package/dist/testing/equal.js +121 -0
  86. package/dist/testing/equal.js.map +1 -0
  87. package/dist/testing/index.d.ts +3 -0
  88. package/dist/testing/index.d.ts.map +1 -0
  89. package/dist/testing/index.js +5 -0
  90. package/dist/testing/index.js.map +1 -0
  91. package/dist/testing/scenario.d.ts +136 -0
  92. package/dist/testing/scenario.d.ts.map +1 -0
  93. package/dist/testing/scenario.js +307 -0
  94. package/dist/testing/scenario.js.map +1 -0
  95. package/dist/utf8.d.ts +17 -0
  96. package/dist/utf8.d.ts.map +1 -0
  97. package/dist/utf8.js +82 -0
  98. package/dist/utf8.js.map +1 -0
  99. package/package.json +56 -4
  100. package/src/aggregate.ts +196 -0
  101. package/src/base64.ts +80 -0
  102. package/src/codec.ts +139 -0
  103. package/src/commit-id.ts +50 -0
  104. package/src/dynamodb/head.ts +93 -0
  105. package/src/dynamodb/index.ts +21 -0
  106. package/src/dynamodb/item.ts +332 -0
  107. package/src/envelope.ts +67 -0
  108. package/src/errors.ts +214 -0
  109. package/src/event.ts +117 -0
  110. package/src/fields.ts +134 -0
  111. package/src/globals.d.ts +39 -0
  112. package/src/index.ts +75 -0
  113. package/src/json.ts +546 -0
  114. package/src/memory/index.ts +3 -0
  115. package/src/memory/repository.ts +313 -0
  116. package/src/metadata.ts +120 -0
  117. package/src/repository.ts +247 -0
  118. package/src/retry.ts +74 -0
  119. package/src/snapshots.ts +42 -0
  120. package/src/store.ts +377 -0
  121. package/src/testing/equal.ts +146 -0
  122. package/src/testing/index.ts +14 -0
  123. package/src/testing/scenario.ts +406 -0
  124. package/src/utf8.ts +92 -0
package/src/json.ts ADDED
@@ -0,0 +1,546 @@
1
+ // evt's JSON reader and writer. The writer produces Go's encoding/json bytes, which every stored
2
+ // document must match, and the reader keeps what JSON.parse loses: each number's text, and object
3
+ // keys in the order they were written.
4
+
5
+ import {base64Encode} from './base64.js'
6
+ import {EvtError} from './errors.js'
7
+ import {compareUtf8, isHighSurrogate, isLowSurrogate} from './utf8.js'
8
+
9
+ /** A value read by {@link parseJson}. Objects are Maps, so integer-like keys keep their order. */
10
+ export type JsonValue = null | boolean | string | JsonNumber | JsonValue[] | Map<string, JsonValue>
11
+
12
+ const NUMBER = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/
13
+
14
+ const INTEGER = /^-?(?:0|[1-9]\d*)$/
15
+
16
+ /** A JSON number kept as its literal text, so 2^53 + 1 or a 40-digit decimal stays exact. */
17
+ export class JsonNumber {
18
+ readonly text: string
19
+
20
+ constructor(text: string) {
21
+ if (!NUMBER.test(text)) {
22
+ throw new EvtError('invalidArgument', `not a JSON number: ${text}`)
23
+ }
24
+
25
+ this.text = text
26
+ }
27
+
28
+ /** Whether the literal is an integer: no fraction and no exponent, as Go's int64 requires. */
29
+ get isInteger(): boolean {
30
+ return INTEGER.test(this.text)
31
+ }
32
+ }
33
+
34
+ /** Already-encoded JSON the writer copies as it is. The caller vouches for its bytes. */
35
+ export class RawJson {
36
+ constructor(readonly text: string) {}
37
+ }
38
+
39
+ /**
40
+ * Encodes a value the way Go's `json.Marshal` encodes the same shape:
41
+ *
42
+ * - A plain object is a struct: its own properties in order, skipping `undefined` ones. An
43
+ * integer-like key (`"10"`) is refused, because the engine has already moved it to the front.
44
+ * - A `Map` is a Go map, written with its keys sorted by their UTF-8 bytes. Keys are strings, or
45
+ * integers (`number` or `bigint`), which are written as text and sorted as text, like Go's.
46
+ * - A `bigint` is an integer, a `Uint8Array` is a `[]byte` (padded base64), and a
47
+ * {@link JsonNumber} is written as its text.
48
+ * - Strings escape `<`, `>`, `&`, U+2028 and U+2029, and a lone surrogate becomes `\ufffd`, as Go
49
+ * writes invalid UTF-8.
50
+ *
51
+ * Anything else, such as a `Date`, a class instance or a number that isn't finite, is refused.
52
+ */
53
+ export function stringifyJson(value: unknown): string {
54
+ const out: string[] = []
55
+ // The arrays and objects being written, innermost last. The writer keeps its own stack, as the
56
+ // reader does, so a deep value is written without overflowing the call stack.
57
+ const open: Writing[] = []
58
+ const writing = new Set<object>()
59
+ let next = value
60
+ for (;;) {
61
+ const opened = writeValue(next, out)
62
+ if (opened !== undefined) {
63
+ if (writing.has(opened.value)) {
64
+ throw new EvtError('invalidArgument', 'cannot encode a cyclic value as JSON')
65
+ }
66
+
67
+ writing.add(opened.value)
68
+ open.push(opened)
69
+ }
70
+
71
+ // Move to the next entry of the innermost open container, closing each one that is done.
72
+ for (;;) {
73
+ const top = open.at(-1)
74
+ if (top === undefined) {
75
+ return out.join('')
76
+ }
77
+
78
+ const entry = top.entries[top.next]
79
+ if (entry === undefined) {
80
+ out.push(top.close)
81
+ writing.delete(top.value)
82
+ open.pop()
83
+ continue
84
+ }
85
+
86
+ if (top.next > 0) {
87
+ out.push(',')
88
+ }
89
+ top.next++
90
+
91
+ const [key, entryValue] = entry
92
+ if (key !== undefined) {
93
+ out.push(quote(key), ':')
94
+ }
95
+ next = entryValue
96
+ break
97
+ }
98
+ }
99
+ }
100
+
101
+ // An array or object being written: its entries (keyed for an object), the next one to write,
102
+ // and the character that closes it.
103
+ interface Writing {
104
+ value: object
105
+ entries: [string | undefined, unknown][]
106
+ next: number
107
+ close: string
108
+ }
109
+
110
+ // Writes a scalar, or the opening of an array or object, which it returns for the caller to fill.
111
+ function writeValue(value: unknown, out: string[]): Writing | undefined {
112
+ switch (typeof value) {
113
+ case 'string':
114
+ out.push(quote(value))
115
+ return undefined
116
+ case 'boolean':
117
+ out.push(value ? 'true' : 'false')
118
+ return undefined
119
+ case 'bigint':
120
+ out.push(value.toString())
121
+ return undefined
122
+ case 'number':
123
+ out.push(formatNumber(value))
124
+ return undefined
125
+ case 'object':
126
+ if (value === null) {
127
+ out.push('null')
128
+ return undefined
129
+ }
130
+
131
+ return writeObject(value, out)
132
+ default:
133
+ throw new EvtError('invalidArgument', `cannot encode a ${typeof value} as JSON`)
134
+ }
135
+ }
136
+
137
+ function writeObject(value: object, out: string[]): Writing | undefined {
138
+ if (value instanceof JsonNumber || value instanceof RawJson) {
139
+ out.push(value.text)
140
+ return undefined
141
+ }
142
+ if (value instanceof Uint8Array) {
143
+ out.push('"', base64Encode(value), '"')
144
+ return undefined
145
+ }
146
+ if (Array.isArray(value)) {
147
+ out.push('[')
148
+ return {value, entries: arrayEntries(value), next: 0, close: ']'}
149
+ }
150
+
151
+ const entries = value instanceof Map ? mapEntries(value) : structEntries(value)
152
+ out.push('{')
153
+
154
+ return {value, entries, next: 0, close: '}'}
155
+ }
156
+
157
+ function arrayEntries(value: unknown[]): [undefined, unknown][] {
158
+ return Array.from(value, (entry) => {
159
+ if (entry === undefined) {
160
+ throw new EvtError('invalidArgument', 'cannot encode undefined in an array as JSON')
161
+ }
162
+
163
+ return [undefined, entry]
164
+ })
165
+ }
166
+
167
+ function mapEntries(value: Map<unknown, unknown>): [string, unknown][] {
168
+ const entries: [string, unknown][] = []
169
+ const seen = new Set<string>()
170
+ for (const [key, entry] of value) {
171
+ const text = mapKey(key)
172
+ if (seen.has(text)) {
173
+ throw new EvtError('invalidArgument', `map key ${text} appears twice once written as text`)
174
+ }
175
+
176
+ seen.add(text)
177
+ entries.push([text, entry])
178
+ }
179
+
180
+ entries.sort(([a], [b]) => compareUtf8(a, b))
181
+
182
+ return entries
183
+ }
184
+
185
+ function mapKey(key: unknown): string {
186
+ if (typeof key === 'string') {
187
+ return key
188
+ }
189
+ if (typeof key === 'bigint' || (typeof key === 'number' && Number.isSafeInteger(key))) {
190
+ return key.toString()
191
+ }
192
+
193
+ throw new EvtError('invalidArgument', 'map keys must be strings or integers')
194
+ }
195
+
196
+ function structEntries(value: object): [string, unknown][] {
197
+ const prototype: unknown = Object.getPrototypeOf(value)
198
+ if (prototype !== Object.prototype && prototype !== null) {
199
+ const name = value.constructor.name || 'object'
200
+ throw new EvtError('invalidArgument', `cannot encode a ${name} as JSON`)
201
+ }
202
+
203
+ const entries: [string, unknown][] = []
204
+ for (const [key, entry] of Object.entries(value)) {
205
+ if (isArrayIndex(key)) {
206
+ throw new EvtError(
207
+ 'invalidArgument',
208
+ `object key "${key}" is integer-like, so its order is lost: use a Map`,
209
+ )
210
+ }
211
+ if (entry !== undefined) {
212
+ entries.push([key, entry])
213
+ }
214
+ }
215
+
216
+ return entries
217
+ }
218
+
219
+ // The keys an engine enumerates first, in numeric order: canonical integers below 2^32 - 1.
220
+ function isArrayIndex(key: string): boolean {
221
+ return /^(?:0|[1-9]\d{0,9})$/.test(key) && Number(key) < 4294967295
222
+ }
223
+
224
+ function formatNumber(value: number): string {
225
+ if (!Number.isFinite(value)) {
226
+ throw new EvtError('invalidArgument', `cannot encode ${value} as JSON`)
227
+ }
228
+
229
+ // Both write the shortest text that reads back, with an exponent below 1e-6 and from 1e21. A
230
+ // number is usually an integer in a payload, so negative zero (from 0 * -1, say) is written as
231
+ // 0, as a Go int is, not as the -0 of a Go float64.
232
+ return value.toString()
233
+ }
234
+
235
+ const SHORT_ESCAPES: Record<number, string> = {
236
+ 0x08: '\\b',
237
+ 0x09: '\\t',
238
+ 0x0a: '\\n',
239
+ 0x0c: '\\f',
240
+ 0x0d: '\\r',
241
+ 0x22: '\\"',
242
+ 0x5c: '\\\\',
243
+ }
244
+
245
+ // <, > and &, which Go escapes so the output is safe in HTML, and the two line separators.
246
+ const HTML_ESCAPES = new Set([0x26, 0x3c, 0x3e, 0x2028, 0x2029])
247
+
248
+ /** Quotes a string with Go's escaping. */
249
+ export function quote(text: string): string {
250
+ let out = '"'
251
+ let start = 0
252
+ for (let i = 0; i < text.length; i++) {
253
+ const unit = text.charCodeAt(i)
254
+ let escaped: string
255
+ if (isHighSurrogate(unit) && isLowSurrogate(text.charCodeAt(i + 1))) {
256
+ i++
257
+ continue
258
+ } else if (unit >= 0xd800 && unit <= 0xdfff) {
259
+ escaped = '\\ufffd'
260
+ } else if (unit < 0x20 || unit === 0x22 || unit === 0x5c || HTML_ESCAPES.has(unit)) {
261
+ escaped = SHORT_ESCAPES[unit] ?? `\\u${unit.toString(16).padStart(4, '0')}`
262
+ } else {
263
+ continue
264
+ }
265
+
266
+ out += text.slice(start, i) + escaped
267
+ start = i + 1
268
+ }
269
+
270
+ return out + text.slice(start) + '"'
271
+ }
272
+
273
+ // Go's decoder refuses documents nested deeper than this. The parser keeps its own stack, so a
274
+ // deep document is refused cleanly instead of overflowing the call stack.
275
+ const MAX_DEPTH = 10_000
276
+
277
+ /** How {@link parseJson} treats an object that repeats a key. */
278
+ export interface ParseOptions {
279
+ /**
280
+ * `last` (the default) keeps the key's first place and its last value. `refuse` refuses the
281
+ * document, which is how evt reads its own documents: Go merges a repeated key into the earlier
282
+ * value and Rust refuses a repeated field, so no reading of one matches both.
283
+ */
284
+ duplicateKeys?: 'last' | 'refuse'
285
+ }
286
+
287
+ /**
288
+ * Parses JSON without losing anything `JSON.parse` loses. Objects come back as Maps in written
289
+ * order, and numbers come back as {@link JsonNumber}. An escaped lone surrogate reads as U+FFFD,
290
+ * as it does in Go.
291
+ */
292
+ export function parseJson(text: string, options: ParseOptions = {}): JsonValue {
293
+ const parser = new Parser(text, options.duplicateKeys === 'refuse')
294
+ parser.skipWhitespace()
295
+ const value = parser.value()
296
+ parser.skipWhitespace()
297
+ if (parser.pos < text.length) {
298
+ throw parser.error('unexpected text after the value')
299
+ }
300
+
301
+ return value
302
+ }
303
+
304
+ // An array or object being read, and for an object the key its next value goes under.
305
+ type Open = {array: JsonValue[]} | {object: Map<string, JsonValue>; key: string}
306
+
307
+ class Parser {
308
+ pos = 0
309
+
310
+ constructor(
311
+ private readonly text: string,
312
+ private readonly refuseDuplicates: boolean,
313
+ ) {}
314
+
315
+ value(): JsonValue {
316
+ const open: Open[] = []
317
+ for (;;) {
318
+ let value: JsonValue
319
+ const unit = this.text.charCodeAt(this.pos)
320
+ if (unit === 0x7b || unit === 0x5b) {
321
+ // { or [
322
+ if (open.length >= MAX_DEPTH) {
323
+ throw this.error('exceeded max depth')
324
+ }
325
+
326
+ this.pos++
327
+ this.skipWhitespace()
328
+ const empty = this.text.charCodeAt(this.pos) === unit + 2
329
+ if (!empty) {
330
+ open.push(unit === 0x5b ? {array: []} : {object: new Map(), key: this.key()})
331
+ continue
332
+ }
333
+
334
+ this.pos++
335
+ value = unit === 0x5b ? [] : new Map()
336
+ } else {
337
+ value = this.scalar(unit)
338
+ }
339
+
340
+ // Add the value to the innermost open container, and close each container that ends here.
341
+ for (;;) {
342
+ const top = open.at(-1)
343
+ if (top === undefined) {
344
+ return value
345
+ }
346
+
347
+ if ('array' in top) {
348
+ top.array.push(value)
349
+ } else if (this.refuseDuplicates && top.object.has(top.key)) {
350
+ throw this.error(`the key "${top.key}" appears twice`)
351
+ } else {
352
+ top.object.set(top.key, value)
353
+ }
354
+
355
+ this.skipWhitespace()
356
+ if (this.text.charCodeAt(this.pos) === 0x2c) {
357
+ this.pos++
358
+ this.skipWhitespace()
359
+ if ('object' in top) {
360
+ top.key = this.key()
361
+ }
362
+ break
363
+ }
364
+
365
+ if ('array' in top) {
366
+ this.expect(0x5d, "',' or ']'")
367
+ value = top.array
368
+ } else {
369
+ this.expect(0x7d, "',' or '}'")
370
+ value = top.object
371
+ }
372
+ open.pop()
373
+ }
374
+ }
375
+ }
376
+
377
+ private scalar(unit: number): JsonValue {
378
+ switch (unit) {
379
+ case 0x22: // "
380
+ return this.string()
381
+ case 0x74: // t
382
+ return this.literal('true', true)
383
+ case 0x66: // f
384
+ return this.literal('false', false)
385
+ case 0x6e: // n
386
+ return this.literal('null', null)
387
+ default:
388
+ return this.number()
389
+ }
390
+ }
391
+
392
+ // An object key and its colon, leaving the position at the value.
393
+ private key(): string {
394
+ if (this.text.charCodeAt(this.pos) !== 0x22) {
395
+ throw this.error('expected an object key')
396
+ }
397
+
398
+ const key = this.string()
399
+ this.skipWhitespace()
400
+ this.expect(0x3a, "':'")
401
+ this.skipWhitespace()
402
+
403
+ return key
404
+ }
405
+
406
+ private string(): string {
407
+ let out = ''
408
+ this.pos++
409
+ let start = this.pos
410
+ for (;;) {
411
+ if (this.pos >= this.text.length) {
412
+ throw this.error('unterminated string')
413
+ }
414
+
415
+ const unit = this.text.charCodeAt(this.pos)
416
+ if (unit === 0x22) {
417
+ out += this.text.slice(start, this.pos)
418
+ this.pos++
419
+ return out
420
+ }
421
+ if (unit < 0x20) {
422
+ throw this.error('control character in string')
423
+ }
424
+ if (unit === 0x5c) {
425
+ out += this.text.slice(start, this.pos) + this.escape()
426
+ start = this.pos
427
+ continue
428
+ }
429
+ if (unit >= 0xd800 && unit <= 0xdfff) {
430
+ // A raw surrogate that isn't half of a pair is invalid UTF-8 to Go.
431
+ if (isHighSurrogate(unit) && isLowSurrogate(this.text.charCodeAt(this.pos + 1))) {
432
+ this.pos += 2
433
+ continue
434
+ }
435
+
436
+ out += this.text.slice(start, this.pos) + '\ufffd'
437
+ this.pos++
438
+ start = this.pos
439
+ continue
440
+ }
441
+
442
+ this.pos++
443
+ }
444
+ }
445
+
446
+ private escape(): string {
447
+ const unit = this.text.charCodeAt(this.pos + 1)
448
+ this.pos += 2
449
+ switch (unit) {
450
+ case 0x22:
451
+ return '"'
452
+ case 0x5c:
453
+ return '\\'
454
+ case 0x2f:
455
+ return '/'
456
+ case 0x62:
457
+ return '\b'
458
+ case 0x66:
459
+ return '\f'
460
+ case 0x6e:
461
+ return '\n'
462
+ case 0x72:
463
+ return '\r'
464
+ case 0x74:
465
+ return '\t'
466
+ case 0x75: {
467
+ const high = this.hex()
468
+ if (!isHighSurrogate(high)) {
469
+ return isLowSurrogate(high) ? '\ufffd' : String.fromCharCode(high)
470
+ }
471
+
472
+ // A high surrogate pairs only with an escaped low surrogate right after it.
473
+ if (this.text.startsWith('\\u', this.pos)) {
474
+ const save = this.pos
475
+ this.pos += 2
476
+ const low = this.hex()
477
+ if (isLowSurrogate(low)) {
478
+ return String.fromCharCode(high, low)
479
+ }
480
+
481
+ this.pos = save
482
+ }
483
+
484
+ return '\ufffd'
485
+ }
486
+ default:
487
+ this.pos -= 2
488
+ throw this.error('invalid escape')
489
+ }
490
+ }
491
+
492
+ private hex(): number {
493
+ const digits = this.text.slice(this.pos, this.pos + 4)
494
+ if (!/^[0-9a-fA-F]{4}$/.test(digits)) {
495
+ throw this.error('invalid \\u escape')
496
+ }
497
+
498
+ this.pos += 4
499
+ return Number.parseInt(digits, 16)
500
+ }
501
+
502
+ private number(): JsonNumber {
503
+ const match = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y
504
+ match.lastIndex = this.pos
505
+ const found = match.exec(this.text)
506
+ if (found === null) {
507
+ throw this.error('unexpected character')
508
+ }
509
+
510
+ this.pos += found[0].length
511
+ return new JsonNumber(found[0])
512
+ }
513
+
514
+ private literal<T>(word: string, value: T): T {
515
+ if (!this.text.startsWith(word, this.pos)) {
516
+ throw this.error('unexpected character')
517
+ }
518
+
519
+ this.pos += word.length
520
+ return value
521
+ }
522
+
523
+ private expect(unit: number, what: string): void {
524
+ if (this.text.charCodeAt(this.pos) !== unit) {
525
+ throw this.error(`expected ${what}`)
526
+ }
527
+
528
+ this.pos++
529
+ }
530
+
531
+ skipWhitespace(): void {
532
+ for (;;) {
533
+ const unit = this.text.charCodeAt(this.pos)
534
+ if (unit !== 0x20 && unit !== 0x09 && unit !== 0x0a && unit !== 0x0d) {
535
+ return
536
+ }
537
+
538
+ this.pos++
539
+ }
540
+ }
541
+
542
+ error(message: string): EvtError {
543
+ const where = this.pos >= this.text.length ? 'at the end' : `at offset ${this.pos}`
544
+ return new EvtError('invalidJson', `invalid JSON ${where}: ${message}`)
545
+ }
546
+ }
@@ -0,0 +1,3 @@
1
+ // @photon-grove/evt/memory: the in-memory repository, for tests and local development.
2
+
3
+ export {type HeadFixtures, MemRepository, headFixtures} from './repository.js'