@tinyshed/tinystore 0.1.0-rc.5 → 0.1.0-rc.7

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 (122) hide show
  1. package/README.md +37 -37
  2. package/bin/tinystore.js +12 -1
  3. package/dist/backup.d.ts +33 -0
  4. package/dist/backup.js +49 -0
  5. package/dist/blobs.d.ts +160 -79
  6. package/dist/blobs.js +510 -217
  7. package/dist/channels.d.ts +90 -0
  8. package/dist/channels.js +291 -0
  9. package/dist/clock.js +3 -3
  10. package/dist/config.d.ts +209 -86
  11. package/dist/config.js +581 -538
  12. package/dist/connection.d.ts +64 -2
  13. package/dist/connection.js +201 -16
  14. package/dist/console.d.ts +27 -3
  15. package/dist/console.js +165 -12
  16. package/dist/dotenv.d.ts +7 -0
  17. package/dist/dotenv.js +79 -0
  18. package/dist/errors.d.ts +61 -1
  19. package/dist/errors.js +81 -1
  20. package/dist/handles.d.ts +44 -2
  21. package/dist/handles.js +81 -2
  22. package/dist/index.d.ts +17 -11
  23. package/dist/index.js +5 -3
  24. package/dist/instruments.d.ts +177 -0
  25. package/dist/instruments.js +819 -0
  26. package/dist/jobs.d.ts +335 -239
  27. package/dist/jobs.js +716 -536
  28. package/dist/json.d.ts +2 -0
  29. package/dist/json.js +122 -0
  30. package/dist/kv.d.ts +130 -243
  31. package/dist/kv.js +273 -640
  32. package/dist/limits.d.ts +80 -0
  33. package/dist/limits.js +139 -0
  34. package/dist/locks.d.ts +62 -0
  35. package/dist/locks.js +237 -0
  36. package/dist/logger.d.ts +45 -9
  37. package/dist/logger.js +150 -38
  38. package/dist/metrics.d.ts +210 -209
  39. package/dist/metrics.js +319 -556
  40. package/dist/once.d.ts +19 -18
  41. package/dist/once.js +74 -63
  42. package/dist/query.d.ts +181 -0
  43. package/dist/query.js +788 -0
  44. package/dist/records.d.ts +241 -150
  45. package/dist/records.js +507 -291
  46. package/dist/runtime/addon.d.ts +14 -0
  47. package/dist/runtime/addon.js +69 -0
  48. package/dist/runtime/bun.js +5 -7
  49. package/dist/runtime/node.js +5 -7
  50. package/dist/runtime/pipe.d.ts +17 -0
  51. package/dist/runtime/pipe.js +133 -0
  52. package/dist/runtime/shared.d.ts +6 -0
  53. package/dist/runtime/shared.js +19 -0
  54. package/dist/runtime.d.ts +5 -2
  55. package/dist/search.d.ts +189 -0
  56. package/dist/search.js +281 -0
  57. package/dist/session.d.ts +31 -3
  58. package/dist/session.js +153 -41
  59. package/dist/sql.d.ts +228 -78
  60. package/dist/sql.js +745 -240
  61. package/dist/store.d.ts +244 -24
  62. package/dist/store.js +270 -67
  63. package/dist/time.d.ts +5 -1
  64. package/dist/time.js +22 -2
  65. package/dist/tx.d.ts +61 -0
  66. package/dist/tx.js +314 -0
  67. package/dist/wire/codec.d.ts +23 -7
  68. package/dist/wire/codec.js +56 -35
  69. package/dist/wire/frame.d.ts +2 -0
  70. package/dist/wire/frame.js +31 -14
  71. package/dist/wire/messages.d.ts +259 -0
  72. package/dist/wire/messages.js +22 -0
  73. package/dist/wire/msgpack.d.ts +49 -1
  74. package/dist/wire/msgpack.js +293 -56
  75. package/dist/wire/protocol.d.ts +2683 -0
  76. package/dist/wire/protocol.js +10846 -0
  77. package/package.json +39 -38
  78. package/src/backup.ts +81 -0
  79. package/src/blobs.ts +678 -301
  80. package/src/channels.ts +377 -0
  81. package/src/clock.ts +4 -4
  82. package/src/config.ts +902 -612
  83. package/src/connection.ts +279 -21
  84. package/src/console.ts +180 -12
  85. package/src/dotenv.ts +82 -0
  86. package/src/errors.ts +105 -0
  87. package/src/handles.ts +157 -8
  88. package/src/index.ts +158 -52
  89. package/src/instruments.ts +1042 -0
  90. package/src/jobs.ts +1017 -733
  91. package/src/json.ts +133 -0
  92. package/src/kv.ts +424 -847
  93. package/src/limits.ts +202 -0
  94. package/src/locks.ts +320 -0
  95. package/src/logger.ts +209 -40
  96. package/src/metrics.ts +557 -752
  97. package/src/once.ts +86 -83
  98. package/src/query.ts +1005 -0
  99. package/src/records.ts +795 -418
  100. package/src/runtime/addon.ts +99 -0
  101. package/src/runtime/bun.ts +5 -7
  102. package/src/runtime/node.ts +5 -7
  103. package/src/runtime/pipe.ts +178 -0
  104. package/src/runtime/shared.ts +20 -0
  105. package/src/runtime.ts +5 -2
  106. package/src/search.ts +480 -0
  107. package/src/session.ts +187 -41
  108. package/src/sql.ts +993 -306
  109. package/src/store.ts +473 -78
  110. package/src/time.ts +48 -3
  111. package/src/tx.ts +426 -0
  112. package/src/wire/codec.ts +78 -37
  113. package/src/wire/frame.ts +44 -14
  114. package/src/wire/messages.ts +25 -0
  115. package/src/wire/msgpack.ts +327 -56
  116. package/src/wire/protocol.ts +11709 -0
  117. package/dist/limiter.d.ts +0 -45
  118. package/dist/limiter.js +0 -68
  119. package/dist/quota.d.ts +0 -44
  120. package/dist/quota.js +0 -98
  121. package/src/limiter.ts +0 -99
  122. package/src/quota.ts +0 -137
@@ -43,18 +43,37 @@ const encoder = new TextEncoder()
43
43
  // begins a str as the character it is, where the default drops it
44
44
  const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
45
45
 
46
+ // Where a 64-bit integer or a float is laid out. A DataView over each
47
+ // message's own bytes cost more than the message: asking a small array for
48
+ // its buffer, or for a view of a part of it, makes the engine move it to one,
49
+ // for every call and answer. So a small part is read as a copy, and short
50
+ // text is written a byte at a time.
51
+ const eight = new DataView(new ArrayBuffer(8))
52
+ const eightBytes = new Uint8Array(eight.buffer)
53
+
46
54
  const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER)
55
+
56
+ /** The integers a number holds exactly reach 2^53 - 1: 21 bits above an int64's low 32. */
57
+ const HIGH_MOST = 0x1fffff
58
+
59
+ /**
60
+ * An int64 of its high 32 bits, signed, and its low 32, unsigned: a number
61
+ * when a number holds it exactly, else a bigint.
62
+ */
63
+ export function int64Of(high: number, low: number): number | bigint {
64
+ const exact =
65
+ high <= HIGH_MOST && (high > -HIGH_MOST - 1 || (high === -HIGH_MOST - 1 && low !== 0))
66
+ return exact ? high * 0x1_0000_0000 + low : (BigInt(high) << 32n) | BigInt(low)
67
+ }
47
68
  const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER)
48
69
 
49
70
  /** Writes one message, its buffer growing as it must. */
50
71
  export class Writer {
51
72
  #bytes: Uint8Array
52
- #view: DataView
53
73
  #length = 0
54
74
 
55
75
  constructor(capacity = 128) {
56
76
  this.#bytes = new Uint8Array(capacity)
57
- this.#view = new DataView(this.#bytes.buffer)
58
77
  }
59
78
 
60
79
  get length(): number {
@@ -66,6 +85,63 @@ export class Writer {
66
85
  return this.#bytes.slice(0, this.#length)
67
86
  }
68
87
 
88
+ /** Where the bytes are written, with room past them: the same array until a write outgrows it. */
89
+ get buffer(): Uint8Array {
90
+ return this.#bytes
91
+ }
92
+
93
+ /** Makes room for n bytes the caller writes into the buffer, and says where they begin. */
94
+ reserve(n: number): number {
95
+ return this.#room(n)
96
+ }
97
+
98
+ /** Writes bytes as they are, a message another writer made. */
99
+ raw(bytes: Uint8Array): void {
100
+ const at = this.#room(bytes.length)
101
+ this.#bytes.set(bytes, at)
102
+ }
103
+
104
+ /** Forgets what was written past a length. */
105
+ truncate(length: number): void {
106
+ this.#length = length
107
+ }
108
+
109
+ /** Writes a map's count of 15 pairs or fewer in the byte reserved for it. */
110
+ mapAt(at: number, n: number): void {
111
+ this.#bytes[at] = FIXMAP | n
112
+ }
113
+
114
+ /**
115
+ * Keeps the byte a message's count of fields goes in, and says where: a
116
+ * field left undefined is not written, so the count is known only once
117
+ * the fields are.
118
+ */
119
+ openMap(): number {
120
+ return this.#room(1)
121
+ }
122
+
123
+ /** Writes a field's number, one byte, as the schema keeps every number. */
124
+ field(key: number): void {
125
+ this.#byte(key)
126
+ }
127
+
128
+ /**
129
+ * Writes a message's count of fields in the byte openMap kept. Past
130
+ * fifteen the count is a map 16's three bytes, and the fields move up to
131
+ * make room: the profile writes the shortest head, so none is kept wider.
132
+ */
133
+ closeMap(at: number, n: number): void {
134
+ if (n <= 15) {
135
+ this.#bytes[at] = FIXMAP | n
136
+ return
137
+ }
138
+ const end = this.#length
139
+ this.#room(2)
140
+ this.#bytes.copyWithin(at + 3, at + 1, end)
141
+ this.#bytes[at] = MAP16
142
+ this.#two(at + 1, n)
143
+ }
144
+
69
145
  /** Makes room for n bytes at the end and says where they begin. */
70
146
  #room(n: number): number {
71
147
  const at = this.#length
@@ -76,9 +152,8 @@ export class Writer {
76
152
  size *= 2
77
153
  }
78
154
  const grown = new Uint8Array(size)
79
- grown.set(this.#bytes.subarray(0, at))
155
+ grown.set(this.#bytes)
80
156
  this.#bytes = grown
81
- this.#view = new DataView(grown.buffer)
82
157
  }
83
158
  this.#length = need
84
159
  return at
@@ -91,6 +166,25 @@ export class Writer {
91
166
  this.#bytes[at] = b
92
167
  }
93
168
 
169
+ /** Writes two bytes at a place, the high one first; a byte keeps the low eight bits. */
170
+ #two(at: number, v: number): void {
171
+ this.#bytes[at] = v >>> 8
172
+ this.#bytes[at + 1] = v
173
+ }
174
+
175
+ #four(at: number, v: number): void {
176
+ this.#bytes[at] = v >>> 24
177
+ this.#bytes[at + 1] = v >>> 16
178
+ this.#bytes[at + 2] = v >>> 8
179
+ this.#bytes[at + 3] = v
180
+ }
181
+
182
+ /** Writes the low 64 bits of an integer: a negative one in two's complement. */
183
+ #eight(at: number, v: bigint): void {
184
+ eight.setBigUint64(0, v)
185
+ this.#bytes.set(eightBytes, at)
186
+ }
187
+
94
188
  nil(): void {
95
189
  this.#byte(NIL)
96
190
  }
@@ -112,7 +206,7 @@ export class Writer {
112
206
  if (v > MAX_SAFE) {
113
207
  const at = this.#room(9)
114
208
  this.#bytes[at] = UINT64
115
- this.#view.setBigUint64(at + 1, v)
209
+ this.#eight(at + 1, v)
116
210
  return
117
211
  }
118
212
  v = Number(v)
@@ -131,15 +225,15 @@ export class Writer {
131
225
  } else if (v <= 0xffff) {
132
226
  const at = this.#room(3)
133
227
  this.#bytes[at] = UINT16
134
- this.#view.setUint16(at + 1, v)
228
+ this.#two(at + 1, v)
135
229
  } else if (v <= 0xffff_ffff) {
136
230
  const at = this.#room(5)
137
231
  this.#bytes[at] = UINT32
138
- this.#view.setUint32(at + 1, v)
232
+ this.#four(at + 1, v)
139
233
  } else {
140
234
  const at = this.#room(9)
141
235
  this.#bytes[at] = UINT64
142
- this.#view.setBigUint64(at + 1, BigInt(v))
236
+ this.#eight(at + 1, BigInt(v))
143
237
  }
144
238
  }
145
239
 
@@ -161,7 +255,7 @@ export class Writer {
161
255
  if (v < MIN_SAFE) {
162
256
  const at = this.#room(9)
163
257
  this.#bytes[at] = INT64
164
- this.#view.setBigInt64(at + 1, v)
258
+ this.#eight(at + 1, v)
165
259
  return
166
260
  }
167
261
  v = Number(v)
@@ -176,19 +270,19 @@ export class Writer {
176
270
  } else if (v >= -0x80) {
177
271
  const at = this.#room(2)
178
272
  this.#bytes[at] = INT8
179
- this.#view.setInt8(at + 1, v)
273
+ this.#bytes[at + 1] = v
180
274
  } else if (v >= -0x8000) {
181
275
  const at = this.#room(3)
182
276
  this.#bytes[at] = INT16
183
- this.#view.setInt16(at + 1, v)
277
+ this.#two(at + 1, v)
184
278
  } else if (v >= -0x8000_0000) {
185
279
  const at = this.#room(5)
186
280
  this.#bytes[at] = INT32
187
- this.#view.setInt32(at + 1, v)
281
+ this.#four(at + 1, v)
188
282
  } else {
189
283
  const at = this.#room(9)
190
284
  this.#bytes[at] = INT64
191
- this.#view.setBigInt64(at + 1, BigInt(v))
285
+ this.#eight(at + 1, BigInt(v))
192
286
  }
193
287
  }
194
288
 
@@ -196,7 +290,8 @@ export class Writer {
196
290
  float(v: number): void {
197
291
  const at = this.#room(9)
198
292
  this.#bytes[at] = FLOAT64
199
- this.#view.setFloat64(at + 1, v)
293
+ eight.setFloat64(0, v)
294
+ this.#bytes.set(eightBytes, at + 1)
200
295
  }
201
296
 
202
297
  /**
@@ -204,6 +299,9 @@ export class Writer {
204
299
  * write and is refused, rather than changed into U+FFFD.
205
300
  */
206
301
  str(s: string): void {
302
+ if (s.length <= shortText && this.#ascii(s)) {
303
+ return
304
+ }
207
305
  if (!s.isWellFormed()) {
208
306
  throw new InvalidError('a string with a lone surrogate, which UTF-8 cannot spell')
209
307
  }
@@ -221,6 +319,24 @@ export class Writer {
221
319
  this.#strHeader(at, written)
222
320
  }
223
321
 
322
+ /** Writes text of ASCII alone, a byte a character, or nothing: it says which. */
323
+ #ascii(s: string): boolean {
324
+ const n = s.length
325
+ const header = headerOf(n)
326
+ const start = this.#length
327
+ const at = this.#room(header + n) + header
328
+ for (let i = 0; i < n; i++) {
329
+ const c = s.charCodeAt(i)
330
+ if (c > 0x7f) {
331
+ this.#length = start
332
+ return false
333
+ }
334
+ this.#bytes[at + i] = c
335
+ }
336
+ this.#strHeader(start, n)
337
+ return true
338
+ }
339
+
224
340
  #strHeader(at: number, n: number): void {
225
341
  if (n <= 31) {
226
342
  this.#bytes[at] = FIXSTR | n
@@ -229,10 +345,10 @@ export class Writer {
229
345
  this.#bytes[at + 1] = n
230
346
  } else if (n <= 0xffff) {
231
347
  this.#bytes[at] = STR16
232
- this.#view.setUint16(at + 1, n)
348
+ this.#two(at + 1, n)
233
349
  } else {
234
350
  this.#bytes[at] = STR32
235
- this.#view.setUint32(at + 1, n)
351
+ this.#four(at + 1, n)
236
352
  }
237
353
  }
238
354
 
@@ -247,12 +363,12 @@ export class Writer {
247
363
  } else if (n <= 0xffff) {
248
364
  at = this.#room(3 + n)
249
365
  this.#bytes[at] = BIN16
250
- this.#view.setUint16(at + 1, n)
366
+ this.#two(at + 1, n)
251
367
  at += 3
252
368
  } else {
253
369
  at = this.#room(5 + n)
254
370
  this.#bytes[at] = BIN32
255
- this.#view.setUint32(at + 1, n)
371
+ this.#four(at + 1, n)
256
372
  at += 5
257
373
  }
258
374
  this.#bytes.set(b, at)
@@ -272,15 +388,20 @@ export class Writer {
272
388
  } else if (n <= 0xffff) {
273
389
  const at = this.#room(3)
274
390
  this.#bytes[at] = two
275
- this.#view.setUint16(at + 1, n)
391
+ this.#two(at + 1, n)
276
392
  } else {
277
393
  const at = this.#room(5)
278
394
  this.#bytes[at] = four
279
- this.#view.setUint32(at + 1, n)
395
+ this.#four(at + 1, n)
280
396
  }
281
397
  }
282
398
  }
283
399
 
400
+ /** Text up to this long is written a byte at a time while it is ASCII. */
401
+ const shortText = 64
402
+ /** A part of a message up to this long is read as a copy, a longer one as a view. */
403
+ const smallPart = 256
404
+
284
405
  /** The bytes a str's header takes for a length. */
285
406
  function headerOf(n: number): number {
286
407
  if (n <= 31) {
@@ -340,31 +461,50 @@ const typeNames: Record<Type, string> = {
340
461
  * ProtocolError. What it returns of the body, a bin, is a view of the body.
341
462
  */
342
463
  export class Reader {
343
- readonly #bytes: Uint8Array
344
- readonly #view: DataView
345
- #at = 0
464
+ #bytes: Uint8Array
465
+ #at: number
466
+ #end: number
346
467
  #depth = 0
468
+ /** The field numbers under 31 each message being read has shown, in bits, by its depth. */
469
+ readonly #seen = new Array<number>(maxDepth + 1).fill(0)
470
+ /** The numbers past those, rare enough to be kept in a set, made when one comes. */
471
+ readonly #others: (Set<number> | undefined)[] = []
347
472
 
348
- constructor(bytes: Uint8Array) {
473
+ /** Reads a message that is the bytes from `at` to `end`: all of them unless said. */
474
+ constructor(bytes: Uint8Array, at = 0, end = bytes.length) {
349
475
  this.#bytes = bytes
350
- this.#view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
476
+ this.#at = at
477
+ this.#end = end
478
+ }
479
+
480
+ /** Turns to another message, for a reader kept from one answer to the next. */
481
+ reset(bytes: Uint8Array, at: number, end: number): void {
482
+ this.#bytes = bytes
483
+ this.#at = at
484
+ this.#end = end
485
+ this.#depth = 0
351
486
  }
352
487
 
353
488
  /** Refuses bytes left after the one value a message is. */
354
489
  end(): void {
355
- if (this.#at !== this.#bytes.length) {
356
- throw fail(`${this.#bytes.length - this.#at} bytes after the message`)
490
+ if (this.#at !== this.#end) {
491
+ throw fail(`${this.#end - this.#at} bytes after the message`)
357
492
  }
358
493
  }
359
494
 
360
495
  /** The next value's type, without reading it. */
361
496
  type(): Type {
362
- const b = this.#bytes[this.#at]
497
+ const b = this.#peek()
363
498
  return b === undefined ? 'invalid' : typeOf(b)
364
499
  }
365
500
 
501
+ /** The next byte, or undefined at the message's end: past it lie another message's bytes. */
502
+ #peek(): number | undefined {
503
+ return this.#at < this.#end ? this.#bytes[this.#at] : undefined
504
+ }
505
+
366
506
  #next(): number {
367
- const b = this.#bytes[this.#at]
507
+ const b = this.#peek()
368
508
  if (b === undefined) {
369
509
  throw fail('the body ends inside a value')
370
510
  }
@@ -373,7 +513,7 @@ export class Reader {
373
513
  }
374
514
 
375
515
  #take(n: number): number {
376
- const left = this.#bytes.length - this.#at
516
+ const left = this.#end - this.#at
377
517
  if (n > left) {
378
518
  throw fail(`${n} bytes asked for where ${left} are left`)
379
519
  }
@@ -382,6 +522,29 @@ export class Reader {
382
522
  return at
383
523
  }
384
524
 
525
+ /** Reads two bytes as an unsigned integer, the high one first. */
526
+ #two(): number {
527
+ const at = this.#take(2)
528
+ return (this.#bytes[at]! << 8) | this.#bytes[at + 1]!
529
+ }
530
+
531
+ #four(): number {
532
+ const at = this.#take(4)
533
+ const bytes = this.#bytes
534
+ return (
535
+ ((bytes[at]! << 24) | (bytes[at + 1]! << 16) | (bytes[at + 2]! << 8) | bytes[at + 3]!) >>> 0
536
+ )
537
+ }
538
+
539
+ /** Lays the next eight bytes out to be read as 64 bits. */
540
+ #eight(): DataView {
541
+ const at = this.#take(8)
542
+ for (let i = 0; i < 8; i++) {
543
+ eightBytes[i] = this.#bytes[at + i]!
544
+ }
545
+ return eight
546
+ }
547
+
385
548
  /**
386
549
  * Reads any encoding of an integer as a bigint; unsigned says it came in the
387
550
  * unsigned family, so that a uint 64 past the int 64 range reads as its value
@@ -398,19 +561,19 @@ export class Reader {
398
561
  case UINT8:
399
562
  return BigInt(this.#bytes[this.#take(1)]!)
400
563
  case UINT16:
401
- return BigInt(this.#view.getUint16(this.#take(2)))
564
+ return BigInt(this.#two())
402
565
  case UINT32:
403
- return BigInt(this.#view.getUint32(this.#take(4)))
566
+ return BigInt(this.#four())
404
567
  case UINT64:
405
- return this.#view.getBigUint64(this.#take(8))
568
+ return this.#eight().getBigUint64(0)
406
569
  case INT8:
407
- return BigInt(this.#view.getInt8(this.#take(1)))
570
+ return BigInt((this.#bytes[this.#take(1)]! << 24) >> 24)
408
571
  case INT16:
409
- return BigInt(this.#view.getInt16(this.#take(2)))
572
+ return BigInt((this.#two() << 16) >> 16)
410
573
  case INT32:
411
- return BigInt(this.#view.getInt32(this.#take(4)))
574
+ return BigInt(this.#four() | 0)
412
575
  case INT64:
413
- return this.#view.getBigInt64(this.#take(8))
576
+ return this.#eight().getBigInt64(0)
414
577
  }
415
578
  throw fail(`${typeNames[typeOf(b)]} where ${want} belongs`)
416
579
  }
@@ -433,8 +596,60 @@ export class Reader {
433
596
  return v
434
597
  }
435
598
 
599
+ /**
600
+ * Reads an integer in any encoding of its value: a number when a number
601
+ * holds it exactly, and a bigint only past that, so that the integers a
602
+ * program reads by the thousand make none.
603
+ */
604
+ integer(): number | bigint {
605
+ const b = this.#peek()
606
+ if (b !== undefined && (b <= 0x7f || b >= NEGFIX)) {
607
+ this.#at++
608
+ return b <= 0x7f ? b : b - 0x100
609
+ }
610
+ switch (b) {
611
+ case UINT8:
612
+ this.#at++
613
+ return this.#bytes[this.#take(1)] as number
614
+ case UINT16:
615
+ this.#at++
616
+ return this.#two()
617
+ case UINT32:
618
+ this.#at++
619
+ return this.#four()
620
+ case INT8:
621
+ this.#at++
622
+ return ((this.#bytes[this.#take(1)] as number) << 24) >> 24
623
+ case INT16:
624
+ this.#at++
625
+ return (this.#two() << 16) >> 16
626
+ case INT32:
627
+ this.#at++
628
+ return this.#four() | 0
629
+ case INT64:
630
+ this.#at++
631
+ return int64Of(this.#four() | 0, this.#four())
632
+ case UINT64: {
633
+ this.#at++
634
+ const high = this.#four()
635
+ if (high > 0x7fff_ffff) {
636
+ this.#at -= 5
637
+ return this.int64()
638
+ }
639
+ return int64Of(high, this.#four())
640
+ }
641
+ }
642
+ return this.int64()
643
+ }
644
+
436
645
  /** Reads an unsigned integer a number holds exactly. */
437
646
  uint(): number {
647
+ // the one byte nearly every integer takes, read without a bigint
648
+ const b = this.#peek()
649
+ if (b !== undefined && b <= 0x7f) {
650
+ this.#at++
651
+ return b
652
+ }
438
653
  const v = this.uint64()
439
654
  if (v > MAX_SAFE) {
440
655
  throw fail(`${v}, past what a number holds exactly, where a number belongs`)
@@ -444,6 +659,11 @@ export class Reader {
444
659
 
445
660
  /** Reads an integer a number holds exactly. */
446
661
  int(): number {
662
+ const b = this.#peek()
663
+ if (b !== undefined && (b <= 0x7f || b >= NEGFIX)) {
664
+ this.#at++
665
+ return b <= 0x7f ? b : b - 0x100
666
+ }
447
667
  const v = this.int64()
448
668
  if (v > MAX_SAFE || v < MIN_SAFE) {
449
669
  throw fail(`${v}, past what a number holds exactly, where a number belongs`)
@@ -459,7 +679,7 @@ export class Reader {
459
679
  const t = this.type()
460
680
  if (t === 'float') {
461
681
  this.#at++
462
- return this.#view.getFloat64(this.#take(8))
682
+ return this.#eight().getFloat64(0)
463
683
  }
464
684
  if (t !== 'int') {
465
685
  throw fail(`${typeNames[t]} where a float belongs`)
@@ -502,15 +722,18 @@ export class Reader {
502
722
  } else if (b === STR8) {
503
723
  n = this.#bytes[this.#take(1)]!
504
724
  } else if (b === STR16) {
505
- n = this.#view.getUint16(this.#take(2))
725
+ n = this.#two()
506
726
  } else if (b === STR32) {
507
- n = this.#view.getUint32(this.#take(4))
727
+ n = this.#four()
508
728
  } else {
509
729
  throw fail(`${typeNames[typeOf(b)]} where a str belongs`)
510
730
  }
511
731
  const at = this.#take(n)
732
+ const stop = at + n
512
733
  try {
513
- return decoder.decode(this.#bytes.subarray(at, at + n))
734
+ return decoder.decode(
735
+ n <= smallPart ? this.#bytes.slice(at, stop) : this.#bytes.subarray(at, stop),
736
+ )
514
737
  } catch {
515
738
  throw fail('a str that is not UTF-8')
516
739
  }
@@ -518,19 +741,30 @@ export class Reader {
518
741
 
519
742
  /** Reads a bin as a view of the body; an empty one is an empty array. */
520
743
  bin(): Uint8Array {
744
+ const n = this.#binLength()
745
+ const at = this.#take(n)
746
+ return this.#bytes.subarray(at, at + n)
747
+ }
748
+
749
+ /** Reads a bin as bytes of its own, which outlive the body. */
750
+ bytes(): Uint8Array {
751
+ const n = this.#binLength()
752
+ const at = this.#take(n)
753
+ return this.#bytes.slice(at, at + n)
754
+ }
755
+
756
+ #binLength(): number {
521
757
  const b = this.#next()
522
- let n: number
523
758
  if (b === BIN8) {
524
- n = this.#bytes[this.#take(1)]!
525
- } else if (b === BIN16) {
526
- n = this.#view.getUint16(this.#take(2))
527
- } else if (b === BIN32) {
528
- n = this.#view.getUint32(this.#take(4))
529
- } else {
530
- throw fail(`${typeNames[typeOf(b)]} where a bin belongs`)
759
+ return this.#bytes[this.#take(1)]!
531
760
  }
532
- const at = this.#take(n)
533
- return this.#bytes.subarray(at, at + n)
761
+ if (b === BIN16) {
762
+ return this.#two()
763
+ }
764
+ if (b === BIN32) {
765
+ return this.#four()
766
+ }
767
+ throw fail(`${typeNames[typeOf(b)]} where a bin belongs`)
534
768
  }
535
769
 
536
770
  /**
@@ -557,13 +791,13 @@ export class Reader {
557
791
  if ((b & 0xf0) === fix) {
558
792
  n = b & 0x0f
559
793
  } else if (b === two) {
560
- n = this.#view.getUint16(this.#take(2))
794
+ n = this.#two()
561
795
  } else if (b === four) {
562
- n = this.#view.getUint32(this.#take(4))
796
+ n = this.#four()
563
797
  } else {
564
798
  throw fail(`${typeNames[typeOf(b)]} where ${typeNames[kind]} belongs`)
565
799
  }
566
- const left = this.#bytes.length - this.#at
800
+ const left = this.#end - this.#at
567
801
  if (n * each > left) {
568
802
  throw fail(`${typeNames[kind]} of ${n} elements in ${left} bytes`)
569
803
  }
@@ -574,6 +808,43 @@ export class Reader {
574
808
  return n
575
809
  }
576
810
 
811
+ /**
812
+ * Reads a message's count of fields and enters its map, for a reader
813
+ * written out a field at a time: field() reads each number, and leave()
814
+ * ends the map.
815
+ */
816
+ message(): number {
817
+ const n = this.map()
818
+ this.#seen[this.#depth] = 0
819
+ this.#others[this.#depth]?.clear()
820
+ return n
821
+ }
822
+
823
+ /** Reads the next field's number in the message entered last, refusing one it showed before. */
824
+ field(): number {
825
+ const key = this.uint()
826
+ const depth = this.#depth
827
+ if (key < 31) {
828
+ const bit = 1 << key
829
+ const seen = this.#seen[depth]!
830
+ if ((seen & bit) !== 0) {
831
+ throw fail(`key ${key} twice`)
832
+ }
833
+ this.#seen[depth] = seen | bit
834
+ return key
835
+ }
836
+ let others = this.#others[depth]
837
+ if (others === undefined) {
838
+ others = new Set()
839
+ this.#others[depth] = others
840
+ }
841
+ if (others.has(key)) {
842
+ throw fail(`key ${key} twice`)
843
+ }
844
+ others.add(key)
845
+ return key
846
+ }
847
+
577
848
  /**
578
849
  * Reads a map with unsigned keys, a message or a field of one, calling each
579
850
  * with its key and the value next: it reads the value, or returns false to
@@ -645,7 +916,7 @@ export class Reader {
645
916
  this.str()
646
917
  return
647
918
  case 'bin':
648
- this.bin()
919
+ this.#take(this.#binLength())
649
920
  return
650
921
  case 'array':
651
922
  this.items(() => this.skip())
@@ -672,7 +943,7 @@ export class Reader {
672
943
  }
673
944
 
674
945
  #describe(): string {
675
- const b = this.#bytes[this.#at]
946
+ const b = this.#peek()
676
947
  if (b === undefined) {
677
948
  return 'the body ends where a value belongs'
678
949
  }