@tinyshed/tinystore 0.1.0-rc.5 → 0.1.0-rc.6
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 +37 -37
- package/bin/tinystore.js +12 -1
- package/dist/backup.d.ts +33 -0
- package/dist/backup.js +49 -0
- package/dist/blobs.d.ts +160 -79
- package/dist/blobs.js +510 -217
- package/dist/channels.d.ts +90 -0
- package/dist/channels.js +291 -0
- package/dist/clock.js +3 -3
- package/dist/config.d.ts +209 -86
- package/dist/config.js +581 -538
- package/dist/connection.d.ts +64 -2
- package/dist/connection.js +201 -16
- package/dist/console.d.ts +27 -3
- package/dist/console.js +165 -12
- package/dist/dotenv.d.ts +7 -0
- package/dist/dotenv.js +79 -0
- package/dist/errors.d.ts +61 -1
- package/dist/errors.js +81 -1
- package/dist/handles.d.ts +44 -2
- package/dist/handles.js +81 -2
- package/dist/index.d.ts +17 -11
- package/dist/index.js +5 -3
- package/dist/instruments.d.ts +177 -0
- package/dist/instruments.js +819 -0
- package/dist/jobs.d.ts +335 -239
- package/dist/jobs.js +716 -536
- package/dist/json.d.ts +2 -0
- package/dist/json.js +122 -0
- package/dist/kv.d.ts +130 -243
- package/dist/kv.js +273 -640
- package/dist/limits.d.ts +80 -0
- package/dist/limits.js +139 -0
- package/dist/locks.d.ts +62 -0
- package/dist/locks.js +237 -0
- package/dist/logger.d.ts +45 -9
- package/dist/logger.js +150 -38
- package/dist/metrics.d.ts +210 -209
- package/dist/metrics.js +319 -556
- package/dist/once.d.ts +19 -18
- package/dist/once.js +74 -63
- package/dist/query.d.ts +181 -0
- package/dist/query.js +788 -0
- package/dist/records.d.ts +241 -150
- package/dist/records.js +507 -291
- package/dist/runtime/addon.d.ts +14 -0
- package/dist/runtime/addon.js +69 -0
- package/dist/runtime/bun.js +5 -7
- package/dist/runtime/node.js +5 -7
- package/dist/runtime/pipe.d.ts +17 -0
- package/dist/runtime/pipe.js +133 -0
- package/dist/runtime/shared.d.ts +6 -0
- package/dist/runtime/shared.js +19 -0
- package/dist/runtime.d.ts +5 -2
- package/dist/search.d.ts +189 -0
- package/dist/search.js +281 -0
- package/dist/session.d.ts +31 -3
- package/dist/session.js +153 -41
- package/dist/sql.d.ts +228 -78
- package/dist/sql.js +745 -240
- package/dist/store.d.ts +244 -24
- package/dist/store.js +270 -67
- package/dist/time.d.ts +5 -1
- package/dist/time.js +22 -2
- package/dist/tx.d.ts +61 -0
- package/dist/tx.js +314 -0
- package/dist/wire/codec.d.ts +23 -7
- package/dist/wire/codec.js +56 -35
- package/dist/wire/frame.d.ts +2 -0
- package/dist/wire/frame.js +31 -14
- package/dist/wire/messages.d.ts +259 -0
- package/dist/wire/messages.js +22 -0
- package/dist/wire/msgpack.d.ts +49 -1
- package/dist/wire/msgpack.js +293 -56
- package/dist/wire/protocol.d.ts +2683 -0
- package/dist/wire/protocol.js +10846 -0
- package/package.json +39 -38
- package/src/backup.ts +81 -0
- package/src/blobs.ts +678 -301
- package/src/channels.ts +377 -0
- package/src/clock.ts +4 -4
- package/src/config.ts +902 -612
- package/src/connection.ts +279 -21
- package/src/console.ts +180 -12
- package/src/dotenv.ts +82 -0
- package/src/errors.ts +105 -0
- package/src/handles.ts +157 -8
- package/src/index.ts +158 -52
- package/src/instruments.ts +1042 -0
- package/src/jobs.ts +1017 -733
- package/src/json.ts +133 -0
- package/src/kv.ts +424 -847
- package/src/limits.ts +202 -0
- package/src/locks.ts +320 -0
- package/src/logger.ts +209 -40
- package/src/metrics.ts +557 -752
- package/src/once.ts +86 -83
- package/src/query.ts +1005 -0
- package/src/records.ts +795 -418
- package/src/runtime/addon.ts +99 -0
- package/src/runtime/bun.ts +5 -7
- package/src/runtime/node.ts +5 -7
- package/src/runtime/pipe.ts +178 -0
- package/src/runtime/shared.ts +20 -0
- package/src/runtime.ts +5 -2
- package/src/search.ts +480 -0
- package/src/session.ts +187 -41
- package/src/sql.ts +993 -306
- package/src/store.ts +473 -78
- package/src/time.ts +48 -3
- package/src/tx.ts +426 -0
- package/src/wire/codec.ts +78 -37
- package/src/wire/frame.ts +44 -14
- package/src/wire/messages.ts +25 -0
- package/src/wire/msgpack.ts +327 -56
- package/src/wire/protocol.ts +11709 -0
- package/dist/limiter.d.ts +0 -45
- package/dist/limiter.js +0 -68
- package/dist/quota.d.ts +0 -44
- package/dist/quota.js +0 -98
- package/src/limiter.ts +0 -99
- package/src/quota.ts +0 -137
package/dist/wire/msgpack.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
/** How deeply maps and arrays may nest in a message. */
|
|
2
2
|
export declare const maxDepth = 8;
|
|
3
|
+
/**
|
|
4
|
+
* An int64 of its high 32 bits, signed, and its low 32, unsigned: a number
|
|
5
|
+
* when a number holds it exactly, else a bigint.
|
|
6
|
+
*/
|
|
7
|
+
export declare function int64Of(high: number, low: number): number | bigint;
|
|
3
8
|
/** Writes one message, its buffer growing as it must. */
|
|
4
9
|
export declare class Writer {
|
|
5
10
|
#private;
|
|
@@ -7,6 +12,30 @@ export declare class Writer {
|
|
|
7
12
|
get length(): number;
|
|
8
13
|
/** A copy of what was written. */
|
|
9
14
|
bytes(): Uint8Array;
|
|
15
|
+
/** Where the bytes are written, with room past them: the same array until a write outgrows it. */
|
|
16
|
+
get buffer(): Uint8Array;
|
|
17
|
+
/** Makes room for n bytes the caller writes into the buffer, and says where they begin. */
|
|
18
|
+
reserve(n: number): number;
|
|
19
|
+
/** Writes bytes as they are, a message another writer made. */
|
|
20
|
+
raw(bytes: Uint8Array): void;
|
|
21
|
+
/** Forgets what was written past a length. */
|
|
22
|
+
truncate(length: number): void;
|
|
23
|
+
/** Writes a map's count of 15 pairs or fewer in the byte reserved for it. */
|
|
24
|
+
mapAt(at: number, n: number): void;
|
|
25
|
+
/**
|
|
26
|
+
* Keeps the byte a message's count of fields goes in, and says where: a
|
|
27
|
+
* field left undefined is not written, so the count is known only once
|
|
28
|
+
* the fields are.
|
|
29
|
+
*/
|
|
30
|
+
openMap(): number;
|
|
31
|
+
/** Writes a field's number, one byte, as the schema keeps every number. */
|
|
32
|
+
field(key: number): void;
|
|
33
|
+
/**
|
|
34
|
+
* Writes a message's count of fields in the byte openMap kept. Past
|
|
35
|
+
* fifteen the count is a map 16's three bytes, and the fields move up to
|
|
36
|
+
* make room: the profile writes the shortest head, so none is kept wider.
|
|
37
|
+
*/
|
|
38
|
+
closeMap(at: number, n: number): void;
|
|
10
39
|
nil(): void;
|
|
11
40
|
bool(v: boolean): void;
|
|
12
41
|
/**
|
|
@@ -44,7 +73,10 @@ export declare function typeOf(b: number): Type;
|
|
|
44
73
|
*/
|
|
45
74
|
export declare class Reader {
|
|
46
75
|
#private;
|
|
47
|
-
|
|
76
|
+
/** Reads a message that is the bytes from `at` to `end`: all of them unless said. */
|
|
77
|
+
constructor(bytes: Uint8Array, at?: number, end?: number);
|
|
78
|
+
/** Turns to another message, for a reader kept from one answer to the next. */
|
|
79
|
+
reset(bytes: Uint8Array, at: number, end: number): void;
|
|
48
80
|
/** Refuses bytes left after the one value a message is. */
|
|
49
81
|
end(): void;
|
|
50
82
|
/** The next value's type, without reading it. */
|
|
@@ -53,6 +85,12 @@ export declare class Reader {
|
|
|
53
85
|
uint64(): bigint;
|
|
54
86
|
/** Reads a signed 64-bit integer in any encoding of its value, a bigint. */
|
|
55
87
|
int64(): bigint;
|
|
88
|
+
/**
|
|
89
|
+
* Reads an integer in any encoding of its value: a number when a number
|
|
90
|
+
* holds it exactly, and a bigint only past that, so that the integers a
|
|
91
|
+
* program reads by the thousand make none.
|
|
92
|
+
*/
|
|
93
|
+
integer(): number | bigint;
|
|
56
94
|
/** Reads an unsigned integer a number holds exactly. */
|
|
57
95
|
uint(): number;
|
|
58
96
|
/** Reads an integer a number holds exactly. */
|
|
@@ -69,6 +107,8 @@ export declare class Reader {
|
|
|
69
107
|
str(): string;
|
|
70
108
|
/** Reads a bin as a view of the body; an empty one is an empty array. */
|
|
71
109
|
bin(): Uint8Array;
|
|
110
|
+
/** Reads a bin as bytes of its own, which outlive the body. */
|
|
111
|
+
bytes(): Uint8Array;
|
|
72
112
|
/**
|
|
73
113
|
* Reads an array's count and enters it; leave once its elements are read.
|
|
74
114
|
* A count past the bytes left is refused before anything is made of it,
|
|
@@ -78,6 +118,14 @@ export declare class Reader {
|
|
|
78
118
|
/** Reads a map's count and enters it, each entry taking two bytes at least. */
|
|
79
119
|
map(): number;
|
|
80
120
|
leave(): void;
|
|
121
|
+
/**
|
|
122
|
+
* Reads a message's count of fields and enters its map, for a reader
|
|
123
|
+
* written out a field at a time: field() reads each number, and leave()
|
|
124
|
+
* ends the map.
|
|
125
|
+
*/
|
|
126
|
+
message(): number;
|
|
127
|
+
/** Reads the next field's number in the message entered last, refusing one it showed before. */
|
|
128
|
+
field(): number;
|
|
81
129
|
/**
|
|
82
130
|
* Reads a map with unsigned keys, a message or a field of one, calling each
|
|
83
131
|
* with its key and the value next: it reads the value, or returns false to
|
package/dist/wire/msgpack.js
CHANGED
|
@@ -37,16 +37,31 @@ const encoder = new TextEncoder();
|
|
|
37
37
|
// fatal refuses bytes that are not UTF-8, and ignoreBOM keeps a U+FEFF that
|
|
38
38
|
// begins a str as the character it is, where the default drops it
|
|
39
39
|
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
40
|
+
// Where a 64-bit integer or a float is laid out. A DataView over each
|
|
41
|
+
// message's own bytes cost more than the message: asking a small array for
|
|
42
|
+
// its buffer, or for a view of a part of it, makes the engine move it to one,
|
|
43
|
+
// for every call and answer. So a small part is read as a copy, and short
|
|
44
|
+
// text is written a byte at a time.
|
|
45
|
+
const eight = new DataView(new ArrayBuffer(8));
|
|
46
|
+
const eightBytes = new Uint8Array(eight.buffer);
|
|
40
47
|
const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
48
|
+
/** The integers a number holds exactly reach 2^53 - 1: 21 bits above an int64's low 32. */
|
|
49
|
+
const HIGH_MOST = 0x1fffff;
|
|
50
|
+
/**
|
|
51
|
+
* An int64 of its high 32 bits, signed, and its low 32, unsigned: a number
|
|
52
|
+
* when a number holds it exactly, else a bigint.
|
|
53
|
+
*/
|
|
54
|
+
export function int64Of(high, low) {
|
|
55
|
+
const exact = high <= HIGH_MOST && (high > -HIGH_MOST - 1 || (high === -HIGH_MOST - 1 && low !== 0));
|
|
56
|
+
return exact ? high * 0x1_0000_0000 + low : (BigInt(high) << 32n) | BigInt(low);
|
|
57
|
+
}
|
|
41
58
|
const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
|
|
42
59
|
/** Writes one message, its buffer growing as it must. */
|
|
43
60
|
export class Writer {
|
|
44
61
|
#bytes;
|
|
45
|
-
#view;
|
|
46
62
|
#length = 0;
|
|
47
63
|
constructor(capacity = 128) {
|
|
48
64
|
this.#bytes = new Uint8Array(capacity);
|
|
49
|
-
this.#view = new DataView(this.#bytes.buffer);
|
|
50
65
|
}
|
|
51
66
|
get length() {
|
|
52
67
|
return this.#length;
|
|
@@ -55,6 +70,55 @@ export class Writer {
|
|
|
55
70
|
bytes() {
|
|
56
71
|
return this.#bytes.slice(0, this.#length);
|
|
57
72
|
}
|
|
73
|
+
/** Where the bytes are written, with room past them: the same array until a write outgrows it. */
|
|
74
|
+
get buffer() {
|
|
75
|
+
return this.#bytes;
|
|
76
|
+
}
|
|
77
|
+
/** Makes room for n bytes the caller writes into the buffer, and says where they begin. */
|
|
78
|
+
reserve(n) {
|
|
79
|
+
return this.#room(n);
|
|
80
|
+
}
|
|
81
|
+
/** Writes bytes as they are, a message another writer made. */
|
|
82
|
+
raw(bytes) {
|
|
83
|
+
const at = this.#room(bytes.length);
|
|
84
|
+
this.#bytes.set(bytes, at);
|
|
85
|
+
}
|
|
86
|
+
/** Forgets what was written past a length. */
|
|
87
|
+
truncate(length) {
|
|
88
|
+
this.#length = length;
|
|
89
|
+
}
|
|
90
|
+
/** Writes a map's count of 15 pairs or fewer in the byte reserved for it. */
|
|
91
|
+
mapAt(at, n) {
|
|
92
|
+
this.#bytes[at] = FIXMAP | n;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Keeps the byte a message's count of fields goes in, and says where: a
|
|
96
|
+
* field left undefined is not written, so the count is known only once
|
|
97
|
+
* the fields are.
|
|
98
|
+
*/
|
|
99
|
+
openMap() {
|
|
100
|
+
return this.#room(1);
|
|
101
|
+
}
|
|
102
|
+
/** Writes a field's number, one byte, as the schema keeps every number. */
|
|
103
|
+
field(key) {
|
|
104
|
+
this.#byte(key);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Writes a message's count of fields in the byte openMap kept. Past
|
|
108
|
+
* fifteen the count is a map 16's three bytes, and the fields move up to
|
|
109
|
+
* make room: the profile writes the shortest head, so none is kept wider.
|
|
110
|
+
*/
|
|
111
|
+
closeMap(at, n) {
|
|
112
|
+
if (n <= 15) {
|
|
113
|
+
this.#bytes[at] = FIXMAP | n;
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const end = this.#length;
|
|
117
|
+
this.#room(2);
|
|
118
|
+
this.#bytes.copyWithin(at + 3, at + 1, end);
|
|
119
|
+
this.#bytes[at] = MAP16;
|
|
120
|
+
this.#two(at + 1, n);
|
|
121
|
+
}
|
|
58
122
|
/** Makes room for n bytes at the end and says where they begin. */
|
|
59
123
|
#room(n) {
|
|
60
124
|
const at = this.#length;
|
|
@@ -65,9 +129,8 @@ export class Writer {
|
|
|
65
129
|
size *= 2;
|
|
66
130
|
}
|
|
67
131
|
const grown = new Uint8Array(size);
|
|
68
|
-
grown.set(this.#bytes
|
|
132
|
+
grown.set(this.#bytes);
|
|
69
133
|
this.#bytes = grown;
|
|
70
|
-
this.#view = new DataView(grown.buffer);
|
|
71
134
|
}
|
|
72
135
|
this.#length = need;
|
|
73
136
|
return at;
|
|
@@ -78,6 +141,22 @@ export class Writer {
|
|
|
78
141
|
const at = this.#room(1);
|
|
79
142
|
this.#bytes[at] = b;
|
|
80
143
|
}
|
|
144
|
+
/** Writes two bytes at a place, the high one first; a byte keeps the low eight bits. */
|
|
145
|
+
#two(at, v) {
|
|
146
|
+
this.#bytes[at] = v >>> 8;
|
|
147
|
+
this.#bytes[at + 1] = v;
|
|
148
|
+
}
|
|
149
|
+
#four(at, v) {
|
|
150
|
+
this.#bytes[at] = v >>> 24;
|
|
151
|
+
this.#bytes[at + 1] = v >>> 16;
|
|
152
|
+
this.#bytes[at + 2] = v >>> 8;
|
|
153
|
+
this.#bytes[at + 3] = v;
|
|
154
|
+
}
|
|
155
|
+
/** Writes the low 64 bits of an integer: a negative one in two's complement. */
|
|
156
|
+
#eight(at, v) {
|
|
157
|
+
eight.setBigUint64(0, v);
|
|
158
|
+
this.#bytes.set(eightBytes, at);
|
|
159
|
+
}
|
|
81
160
|
nil() {
|
|
82
161
|
this.#byte(NIL);
|
|
83
162
|
}
|
|
@@ -97,7 +176,7 @@ export class Writer {
|
|
|
97
176
|
if (v > MAX_SAFE) {
|
|
98
177
|
const at = this.#room(9);
|
|
99
178
|
this.#bytes[at] = UINT64;
|
|
100
|
-
this.#
|
|
179
|
+
this.#eight(at + 1, v);
|
|
101
180
|
return;
|
|
102
181
|
}
|
|
103
182
|
v = Number(v);
|
|
@@ -116,17 +195,17 @@ export class Writer {
|
|
|
116
195
|
else if (v <= 0xffff) {
|
|
117
196
|
const at = this.#room(3);
|
|
118
197
|
this.#bytes[at] = UINT16;
|
|
119
|
-
this.#
|
|
198
|
+
this.#two(at + 1, v);
|
|
120
199
|
}
|
|
121
200
|
else if (v <= 0xffff_ffff) {
|
|
122
201
|
const at = this.#room(5);
|
|
123
202
|
this.#bytes[at] = UINT32;
|
|
124
|
-
this.#
|
|
203
|
+
this.#four(at + 1, v);
|
|
125
204
|
}
|
|
126
205
|
else {
|
|
127
206
|
const at = this.#room(9);
|
|
128
207
|
this.#bytes[at] = UINT64;
|
|
129
|
-
this.#
|
|
208
|
+
this.#eight(at + 1, BigInt(v));
|
|
130
209
|
}
|
|
131
210
|
}
|
|
132
211
|
/**
|
|
@@ -147,7 +226,7 @@ export class Writer {
|
|
|
147
226
|
if (v < MIN_SAFE) {
|
|
148
227
|
const at = this.#room(9);
|
|
149
228
|
this.#bytes[at] = INT64;
|
|
150
|
-
this.#
|
|
229
|
+
this.#eight(at + 1, v);
|
|
151
230
|
return;
|
|
152
231
|
}
|
|
153
232
|
v = Number(v);
|
|
@@ -164,35 +243,39 @@ export class Writer {
|
|
|
164
243
|
else if (v >= -0x80) {
|
|
165
244
|
const at = this.#room(2);
|
|
166
245
|
this.#bytes[at] = INT8;
|
|
167
|
-
this.#
|
|
246
|
+
this.#bytes[at + 1] = v;
|
|
168
247
|
}
|
|
169
248
|
else if (v >= -0x8000) {
|
|
170
249
|
const at = this.#room(3);
|
|
171
250
|
this.#bytes[at] = INT16;
|
|
172
|
-
this.#
|
|
251
|
+
this.#two(at + 1, v);
|
|
173
252
|
}
|
|
174
253
|
else if (v >= -0x8000_0000) {
|
|
175
254
|
const at = this.#room(5);
|
|
176
255
|
this.#bytes[at] = INT32;
|
|
177
|
-
this.#
|
|
256
|
+
this.#four(at + 1, v);
|
|
178
257
|
}
|
|
179
258
|
else {
|
|
180
259
|
const at = this.#room(9);
|
|
181
260
|
this.#bytes[at] = INT64;
|
|
182
|
-
this.#
|
|
261
|
+
this.#eight(at + 1, BigInt(v));
|
|
183
262
|
}
|
|
184
263
|
}
|
|
185
264
|
/** Writes a float 64 with its bits: -0 → cb 80 00 00 00 00 00 00 00. */
|
|
186
265
|
float(v) {
|
|
187
266
|
const at = this.#room(9);
|
|
188
267
|
this.#bytes[at] = FLOAT64;
|
|
189
|
-
|
|
268
|
+
eight.setFloat64(0, v);
|
|
269
|
+
this.#bytes.set(eightBytes, at + 1);
|
|
190
270
|
}
|
|
191
271
|
/**
|
|
192
272
|
* Writes text as UTF-8. A string holding a lone surrogate has no UTF-8 to
|
|
193
273
|
* write and is refused, rather than changed into U+FFFD.
|
|
194
274
|
*/
|
|
195
275
|
str(s) {
|
|
276
|
+
if (s.length <= shortText && this.#ascii(s)) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
196
279
|
if (!s.isWellFormed()) {
|
|
197
280
|
throw new InvalidError('a string with a lone surrogate, which UTF-8 cannot spell');
|
|
198
281
|
}
|
|
@@ -209,6 +292,23 @@ export class Writer {
|
|
|
209
292
|
this.#length = at + shortest + written;
|
|
210
293
|
this.#strHeader(at, written);
|
|
211
294
|
}
|
|
295
|
+
/** Writes text of ASCII alone, a byte a character, or nothing: it says which. */
|
|
296
|
+
#ascii(s) {
|
|
297
|
+
const n = s.length;
|
|
298
|
+
const header = headerOf(n);
|
|
299
|
+
const start = this.#length;
|
|
300
|
+
const at = this.#room(header + n) + header;
|
|
301
|
+
for (let i = 0; i < n; i++) {
|
|
302
|
+
const c = s.charCodeAt(i);
|
|
303
|
+
if (c > 0x7f) {
|
|
304
|
+
this.#length = start;
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
this.#bytes[at + i] = c;
|
|
308
|
+
}
|
|
309
|
+
this.#strHeader(start, n);
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
212
312
|
#strHeader(at, n) {
|
|
213
313
|
if (n <= 31) {
|
|
214
314
|
this.#bytes[at] = FIXSTR | n;
|
|
@@ -219,11 +319,11 @@ export class Writer {
|
|
|
219
319
|
}
|
|
220
320
|
else if (n <= 0xffff) {
|
|
221
321
|
this.#bytes[at] = STR16;
|
|
222
|
-
this.#
|
|
322
|
+
this.#two(at + 1, n);
|
|
223
323
|
}
|
|
224
324
|
else {
|
|
225
325
|
this.#bytes[at] = STR32;
|
|
226
|
-
this.#
|
|
326
|
+
this.#four(at + 1, n);
|
|
227
327
|
}
|
|
228
328
|
}
|
|
229
329
|
bin(b) {
|
|
@@ -238,13 +338,13 @@ export class Writer {
|
|
|
238
338
|
else if (n <= 0xffff) {
|
|
239
339
|
at = this.#room(3 + n);
|
|
240
340
|
this.#bytes[at] = BIN16;
|
|
241
|
-
this.#
|
|
341
|
+
this.#two(at + 1, n);
|
|
242
342
|
at += 3;
|
|
243
343
|
}
|
|
244
344
|
else {
|
|
245
345
|
at = this.#room(5 + n);
|
|
246
346
|
this.#bytes[at] = BIN32;
|
|
247
|
-
this.#
|
|
347
|
+
this.#four(at + 1, n);
|
|
248
348
|
at += 5;
|
|
249
349
|
}
|
|
250
350
|
this.#bytes.set(b, at);
|
|
@@ -262,15 +362,19 @@ export class Writer {
|
|
|
262
362
|
else if (n <= 0xffff) {
|
|
263
363
|
const at = this.#room(3);
|
|
264
364
|
this.#bytes[at] = two;
|
|
265
|
-
this.#
|
|
365
|
+
this.#two(at + 1, n);
|
|
266
366
|
}
|
|
267
367
|
else {
|
|
268
368
|
const at = this.#room(5);
|
|
269
369
|
this.#bytes[at] = four;
|
|
270
|
-
this.#
|
|
370
|
+
this.#four(at + 1, n);
|
|
271
371
|
}
|
|
272
372
|
}
|
|
273
373
|
}
|
|
374
|
+
/** Text up to this long is written a byte at a time while it is ASCII. */
|
|
375
|
+
const shortText = 64;
|
|
376
|
+
/** A part of a message up to this long is read as a copy, a longer one as a view. */
|
|
377
|
+
const smallPart = 256;
|
|
274
378
|
/** The bytes a str's header takes for a length. */
|
|
275
379
|
function headerOf(n) {
|
|
276
380
|
if (n <= 31) {
|
|
@@ -325,26 +429,43 @@ const typeNames = {
|
|
|
325
429
|
*/
|
|
326
430
|
export class Reader {
|
|
327
431
|
#bytes;
|
|
328
|
-
#
|
|
329
|
-
#
|
|
432
|
+
#at;
|
|
433
|
+
#end;
|
|
330
434
|
#depth = 0;
|
|
331
|
-
|
|
435
|
+
/** The field numbers under 31 each message being read has shown, in bits, by its depth. */
|
|
436
|
+
#seen = new Array(maxDepth + 1).fill(0);
|
|
437
|
+
/** The numbers past those, rare enough to be kept in a set, made when one comes. */
|
|
438
|
+
#others = [];
|
|
439
|
+
/** Reads a message that is the bytes from `at` to `end`: all of them unless said. */
|
|
440
|
+
constructor(bytes, at = 0, end = bytes.length) {
|
|
332
441
|
this.#bytes = bytes;
|
|
333
|
-
this.#
|
|
442
|
+
this.#at = at;
|
|
443
|
+
this.#end = end;
|
|
444
|
+
}
|
|
445
|
+
/** Turns to another message, for a reader kept from one answer to the next. */
|
|
446
|
+
reset(bytes, at, end) {
|
|
447
|
+
this.#bytes = bytes;
|
|
448
|
+
this.#at = at;
|
|
449
|
+
this.#end = end;
|
|
450
|
+
this.#depth = 0;
|
|
334
451
|
}
|
|
335
452
|
/** Refuses bytes left after the one value a message is. */
|
|
336
453
|
end() {
|
|
337
|
-
if (this.#at !== this.#
|
|
338
|
-
throw fail(`${this.#
|
|
454
|
+
if (this.#at !== this.#end) {
|
|
455
|
+
throw fail(`${this.#end - this.#at} bytes after the message`);
|
|
339
456
|
}
|
|
340
457
|
}
|
|
341
458
|
/** The next value's type, without reading it. */
|
|
342
459
|
type() {
|
|
343
|
-
const b = this.#
|
|
460
|
+
const b = this.#peek();
|
|
344
461
|
return b === undefined ? 'invalid' : typeOf(b);
|
|
345
462
|
}
|
|
463
|
+
/** The next byte, or undefined at the message's end: past it lie another message's bytes. */
|
|
464
|
+
#peek() {
|
|
465
|
+
return this.#at < this.#end ? this.#bytes[this.#at] : undefined;
|
|
466
|
+
}
|
|
346
467
|
#next() {
|
|
347
|
-
const b = this.#
|
|
468
|
+
const b = this.#peek();
|
|
348
469
|
if (b === undefined) {
|
|
349
470
|
throw fail('the body ends inside a value');
|
|
350
471
|
}
|
|
@@ -352,7 +473,7 @@ export class Reader {
|
|
|
352
473
|
return b;
|
|
353
474
|
}
|
|
354
475
|
#take(n) {
|
|
355
|
-
const left = this.#
|
|
476
|
+
const left = this.#end - this.#at;
|
|
356
477
|
if (n > left) {
|
|
357
478
|
throw fail(`${n} bytes asked for where ${left} are left`);
|
|
358
479
|
}
|
|
@@ -360,6 +481,24 @@ export class Reader {
|
|
|
360
481
|
this.#at += n;
|
|
361
482
|
return at;
|
|
362
483
|
}
|
|
484
|
+
/** Reads two bytes as an unsigned integer, the high one first. */
|
|
485
|
+
#two() {
|
|
486
|
+
const at = this.#take(2);
|
|
487
|
+
return (this.#bytes[at] << 8) | this.#bytes[at + 1];
|
|
488
|
+
}
|
|
489
|
+
#four() {
|
|
490
|
+
const at = this.#take(4);
|
|
491
|
+
const bytes = this.#bytes;
|
|
492
|
+
return (((bytes[at] << 24) | (bytes[at + 1] << 16) | (bytes[at + 2] << 8) | bytes[at + 3]) >>> 0);
|
|
493
|
+
}
|
|
494
|
+
/** Lays the next eight bytes out to be read as 64 bits. */
|
|
495
|
+
#eight() {
|
|
496
|
+
const at = this.#take(8);
|
|
497
|
+
for (let i = 0; i < 8; i++) {
|
|
498
|
+
eightBytes[i] = this.#bytes[at + i];
|
|
499
|
+
}
|
|
500
|
+
return eight;
|
|
501
|
+
}
|
|
363
502
|
/**
|
|
364
503
|
* Reads any encoding of an integer as a bigint; unsigned says it came in the
|
|
365
504
|
* unsigned family, so that a uint 64 past the int 64 range reads as its value
|
|
@@ -376,19 +515,19 @@ export class Reader {
|
|
|
376
515
|
case UINT8:
|
|
377
516
|
return BigInt(this.#bytes[this.#take(1)]);
|
|
378
517
|
case UINT16:
|
|
379
|
-
return BigInt(this.#
|
|
518
|
+
return BigInt(this.#two());
|
|
380
519
|
case UINT32:
|
|
381
|
-
return BigInt(this.#
|
|
520
|
+
return BigInt(this.#four());
|
|
382
521
|
case UINT64:
|
|
383
|
-
return this.#
|
|
522
|
+
return this.#eight().getBigUint64(0);
|
|
384
523
|
case INT8:
|
|
385
|
-
return BigInt(this.#
|
|
524
|
+
return BigInt((this.#bytes[this.#take(1)] << 24) >> 24);
|
|
386
525
|
case INT16:
|
|
387
|
-
return BigInt(
|
|
526
|
+
return BigInt((this.#two() << 16) >> 16);
|
|
388
527
|
case INT32:
|
|
389
|
-
return BigInt(this.#
|
|
528
|
+
return BigInt(this.#four() | 0);
|
|
390
529
|
case INT64:
|
|
391
|
-
return this.#
|
|
530
|
+
return this.#eight().getBigInt64(0);
|
|
392
531
|
}
|
|
393
532
|
throw fail(`${typeNames[typeOf(b)]} where ${want} belongs`);
|
|
394
533
|
}
|
|
@@ -408,8 +547,59 @@ export class Reader {
|
|
|
408
547
|
}
|
|
409
548
|
return v;
|
|
410
549
|
}
|
|
550
|
+
/**
|
|
551
|
+
* Reads an integer in any encoding of its value: a number when a number
|
|
552
|
+
* holds it exactly, and a bigint only past that, so that the integers a
|
|
553
|
+
* program reads by the thousand make none.
|
|
554
|
+
*/
|
|
555
|
+
integer() {
|
|
556
|
+
const b = this.#peek();
|
|
557
|
+
if (b !== undefined && (b <= 0x7f || b >= NEGFIX)) {
|
|
558
|
+
this.#at++;
|
|
559
|
+
return b <= 0x7f ? b : b - 0x100;
|
|
560
|
+
}
|
|
561
|
+
switch (b) {
|
|
562
|
+
case UINT8:
|
|
563
|
+
this.#at++;
|
|
564
|
+
return this.#bytes[this.#take(1)];
|
|
565
|
+
case UINT16:
|
|
566
|
+
this.#at++;
|
|
567
|
+
return this.#two();
|
|
568
|
+
case UINT32:
|
|
569
|
+
this.#at++;
|
|
570
|
+
return this.#four();
|
|
571
|
+
case INT8:
|
|
572
|
+
this.#at++;
|
|
573
|
+
return (this.#bytes[this.#take(1)] << 24) >> 24;
|
|
574
|
+
case INT16:
|
|
575
|
+
this.#at++;
|
|
576
|
+
return (this.#two() << 16) >> 16;
|
|
577
|
+
case INT32:
|
|
578
|
+
this.#at++;
|
|
579
|
+
return this.#four() | 0;
|
|
580
|
+
case INT64:
|
|
581
|
+
this.#at++;
|
|
582
|
+
return int64Of(this.#four() | 0, this.#four());
|
|
583
|
+
case UINT64: {
|
|
584
|
+
this.#at++;
|
|
585
|
+
const high = this.#four();
|
|
586
|
+
if (high > 0x7fff_ffff) {
|
|
587
|
+
this.#at -= 5;
|
|
588
|
+
return this.int64();
|
|
589
|
+
}
|
|
590
|
+
return int64Of(high, this.#four());
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return this.int64();
|
|
594
|
+
}
|
|
411
595
|
/** Reads an unsigned integer a number holds exactly. */
|
|
412
596
|
uint() {
|
|
597
|
+
// the one byte nearly every integer takes, read without a bigint
|
|
598
|
+
const b = this.#peek();
|
|
599
|
+
if (b !== undefined && b <= 0x7f) {
|
|
600
|
+
this.#at++;
|
|
601
|
+
return b;
|
|
602
|
+
}
|
|
413
603
|
const v = this.uint64();
|
|
414
604
|
if (v > MAX_SAFE) {
|
|
415
605
|
throw fail(`${v}, past what a number holds exactly, where a number belongs`);
|
|
@@ -418,6 +608,11 @@ export class Reader {
|
|
|
418
608
|
}
|
|
419
609
|
/** Reads an integer a number holds exactly. */
|
|
420
610
|
int() {
|
|
611
|
+
const b = this.#peek();
|
|
612
|
+
if (b !== undefined && (b <= 0x7f || b >= NEGFIX)) {
|
|
613
|
+
this.#at++;
|
|
614
|
+
return b <= 0x7f ? b : b - 0x100;
|
|
615
|
+
}
|
|
421
616
|
const v = this.int64();
|
|
422
617
|
if (v > MAX_SAFE || v < MIN_SAFE) {
|
|
423
618
|
throw fail(`${v}, past what a number holds exactly, where a number belongs`);
|
|
@@ -432,7 +627,7 @@ export class Reader {
|
|
|
432
627
|
const t = this.type();
|
|
433
628
|
if (t === 'float') {
|
|
434
629
|
this.#at++;
|
|
435
|
-
return this.#
|
|
630
|
+
return this.#eight().getFloat64(0);
|
|
436
631
|
}
|
|
437
632
|
if (t !== 'int') {
|
|
438
633
|
throw fail(`${typeNames[t]} where a float belongs`);
|
|
@@ -474,17 +669,18 @@ export class Reader {
|
|
|
474
669
|
n = this.#bytes[this.#take(1)];
|
|
475
670
|
}
|
|
476
671
|
else if (b === STR16) {
|
|
477
|
-
n = this.#
|
|
672
|
+
n = this.#two();
|
|
478
673
|
}
|
|
479
674
|
else if (b === STR32) {
|
|
480
|
-
n = this.#
|
|
675
|
+
n = this.#four();
|
|
481
676
|
}
|
|
482
677
|
else {
|
|
483
678
|
throw fail(`${typeNames[typeOf(b)]} where a str belongs`);
|
|
484
679
|
}
|
|
485
680
|
const at = this.#take(n);
|
|
681
|
+
const stop = at + n;
|
|
486
682
|
try {
|
|
487
|
-
return decoder.decode(this.#bytes.
|
|
683
|
+
return decoder.decode(n <= smallPart ? this.#bytes.slice(at, stop) : this.#bytes.subarray(at, stop));
|
|
488
684
|
}
|
|
489
685
|
catch {
|
|
490
686
|
throw fail('a str that is not UTF-8');
|
|
@@ -492,22 +688,28 @@ export class Reader {
|
|
|
492
688
|
}
|
|
493
689
|
/** Reads a bin as a view of the body; an empty one is an empty array. */
|
|
494
690
|
bin() {
|
|
691
|
+
const n = this.#binLength();
|
|
692
|
+
const at = this.#take(n);
|
|
693
|
+
return this.#bytes.subarray(at, at + n);
|
|
694
|
+
}
|
|
695
|
+
/** Reads a bin as bytes of its own, which outlive the body. */
|
|
696
|
+
bytes() {
|
|
697
|
+
const n = this.#binLength();
|
|
698
|
+
const at = this.#take(n);
|
|
699
|
+
return this.#bytes.slice(at, at + n);
|
|
700
|
+
}
|
|
701
|
+
#binLength() {
|
|
495
702
|
const b = this.#next();
|
|
496
|
-
let n;
|
|
497
703
|
if (b === BIN8) {
|
|
498
|
-
|
|
704
|
+
return this.#bytes[this.#take(1)];
|
|
499
705
|
}
|
|
500
|
-
|
|
501
|
-
|
|
706
|
+
if (b === BIN16) {
|
|
707
|
+
return this.#two();
|
|
502
708
|
}
|
|
503
|
-
|
|
504
|
-
|
|
709
|
+
if (b === BIN32) {
|
|
710
|
+
return this.#four();
|
|
505
711
|
}
|
|
506
|
-
|
|
507
|
-
throw fail(`${typeNames[typeOf(b)]} where a bin belongs`);
|
|
508
|
-
}
|
|
509
|
-
const at = this.#take(n);
|
|
510
|
-
return this.#bytes.subarray(at, at + n);
|
|
712
|
+
throw fail(`${typeNames[typeOf(b)]} where a bin belongs`);
|
|
511
713
|
}
|
|
512
714
|
/**
|
|
513
715
|
* Reads an array's count and enters it; leave once its elements are read.
|
|
@@ -531,15 +733,15 @@ export class Reader {
|
|
|
531
733
|
n = b & 0x0f;
|
|
532
734
|
}
|
|
533
735
|
else if (b === two) {
|
|
534
|
-
n = this.#
|
|
736
|
+
n = this.#two();
|
|
535
737
|
}
|
|
536
738
|
else if (b === four) {
|
|
537
|
-
n = this.#
|
|
739
|
+
n = this.#four();
|
|
538
740
|
}
|
|
539
741
|
else {
|
|
540
742
|
throw fail(`${typeNames[typeOf(b)]} where ${typeNames[kind]} belongs`);
|
|
541
743
|
}
|
|
542
|
-
const left = this.#
|
|
744
|
+
const left = this.#end - this.#at;
|
|
543
745
|
if (n * each > left) {
|
|
544
746
|
throw fail(`${typeNames[kind]} of ${n} elements in ${left} bytes`);
|
|
545
747
|
}
|
|
@@ -549,6 +751,41 @@ export class Reader {
|
|
|
549
751
|
this.#depth++;
|
|
550
752
|
return n;
|
|
551
753
|
}
|
|
754
|
+
/**
|
|
755
|
+
* Reads a message's count of fields and enters its map, for a reader
|
|
756
|
+
* written out a field at a time: field() reads each number, and leave()
|
|
757
|
+
* ends the map.
|
|
758
|
+
*/
|
|
759
|
+
message() {
|
|
760
|
+
const n = this.map();
|
|
761
|
+
this.#seen[this.#depth] = 0;
|
|
762
|
+
this.#others[this.#depth]?.clear();
|
|
763
|
+
return n;
|
|
764
|
+
}
|
|
765
|
+
/** Reads the next field's number in the message entered last, refusing one it showed before. */
|
|
766
|
+
field() {
|
|
767
|
+
const key = this.uint();
|
|
768
|
+
const depth = this.#depth;
|
|
769
|
+
if (key < 31) {
|
|
770
|
+
const bit = 1 << key;
|
|
771
|
+
const seen = this.#seen[depth];
|
|
772
|
+
if ((seen & bit) !== 0) {
|
|
773
|
+
throw fail(`key ${key} twice`);
|
|
774
|
+
}
|
|
775
|
+
this.#seen[depth] = seen | bit;
|
|
776
|
+
return key;
|
|
777
|
+
}
|
|
778
|
+
let others = this.#others[depth];
|
|
779
|
+
if (others === undefined) {
|
|
780
|
+
others = new Set();
|
|
781
|
+
this.#others[depth] = others;
|
|
782
|
+
}
|
|
783
|
+
if (others.has(key)) {
|
|
784
|
+
throw fail(`key ${key} twice`);
|
|
785
|
+
}
|
|
786
|
+
others.add(key);
|
|
787
|
+
return key;
|
|
788
|
+
}
|
|
552
789
|
/**
|
|
553
790
|
* Reads a map with unsigned keys, a message or a field of one, calling each
|
|
554
791
|
* with its key and the value next: it reads the value, or returns false to
|
|
@@ -617,7 +854,7 @@ export class Reader {
|
|
|
617
854
|
this.str();
|
|
618
855
|
return;
|
|
619
856
|
case 'bin':
|
|
620
|
-
this
|
|
857
|
+
this.#take(this.#binLength());
|
|
621
858
|
return;
|
|
622
859
|
case 'array':
|
|
623
860
|
this.items(() => this.skip());
|
|
@@ -643,7 +880,7 @@ export class Reader {
|
|
|
643
880
|
}
|
|
644
881
|
}
|
|
645
882
|
#describe() {
|
|
646
|
-
const b = this.#
|
|
883
|
+
const b = this.#peek();
|
|
647
884
|
if (b === undefined) {
|
|
648
885
|
return 'the body ends where a value belongs';
|
|
649
886
|
}
|