json-stream-lite 1.0.0 → 1.0.1

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.
@@ -1,428 +0,0 @@
1
- import { ByteBuffer } from './byte-buffer.js';
2
- import { bytesToNumber, bytesToString } from './utils.js';
3
- const BYTE_MAP = {
4
- quotes: 34,
5
- a: 97,
6
- n: 110,
7
- t: 116,
8
- f: 102,
9
- u: 117,
10
- r: 114,
11
- l: 108,
12
- s: 115,
13
- zero: 48,
14
- nine: 57,
15
- minus: 45,
16
- dot: 46,
17
- plus: 43,
18
- e: 101,
19
- E: 69,
20
- space: 32,
21
- tab: 9,
22
- carriageReturn: 13,
23
- lineFeed: 10,
24
- leftBrace: 123,
25
- rightBrace: 125,
26
- leftSquare: 91,
27
- rightSquare: 93,
28
- comma: 44,
29
- colon: 58,
30
- };
31
- const isWhitespace = (byte) => {
32
- return (byte === BYTE_MAP.space ||
33
- byte === BYTE_MAP.tab ||
34
- byte === BYTE_MAP.carriageReturn ||
35
- byte === BYTE_MAP.lineFeed);
36
- };
37
- const isDigit = (byte) => {
38
- return byte !== null && byte >= BYTE_MAP.zero && byte <= BYTE_MAP.nine;
39
- };
40
- const isNumberStart = (byte) => {
41
- return (byte === BYTE_MAP.minus ||
42
- (byte !== null && byte >= BYTE_MAP.zero && byte <= BYTE_MAP.nine));
43
- };
44
- const isNumeric = (byte) => {
45
- return (isDigit(byte) ||
46
- byte === BYTE_MAP.dot || // .
47
- byte === BYTE_MAP.e || // e
48
- byte === BYTE_MAP.E || // E
49
- byte === BYTE_MAP.minus || // -
50
- byte === BYTE_MAP.plus // +
51
- );
52
- };
53
- export class JsonEntity {
54
- consumed = false;
55
- buffer;
56
- constructor(buffer) {
57
- this.buffer = buffer instanceof ByteBuffer ? buffer : new ByteBuffer(buffer);
58
- }
59
- get entityType() {
60
- return this.constructor.name;
61
- }
62
- set maxBufferSize(size) {
63
- this.buffer.maxBufferSize = size;
64
- }
65
- feed(...input) {
66
- for (const item of input) {
67
- this.buffer.feed(item);
68
- }
69
- }
70
- skipWhitespace() {
71
- while (isWhitespace(this.buffer.peek())) {
72
- this.buffer.next();
73
- }
74
- }
75
- read() {
76
- if (this.consumed) {
77
- throw new Error('JSON entity has already been consumed.');
78
- }
79
- const read = this.parse();
80
- this.consumed = true;
81
- if (this.buffer.canCompact())
82
- this.buffer.compact();
83
- return read;
84
- }
85
- async readAsync() {
86
- if (this.consumed) {
87
- throw new Error('JSON entity has already been consumed.');
88
- }
89
- while (!this.buffer.atEof()) {
90
- await this.buffer.readStream();
91
- const res = this.tryParse(() => this.read());
92
- if (res !== null) {
93
- return res;
94
- }
95
- }
96
- return this.read();
97
- }
98
- consume() {
99
- if (!this.consumed) {
100
- this.read();
101
- }
102
- }
103
- async consumeAsync() {
104
- if (!this.consumed) {
105
- await this.readAsync();
106
- }
107
- }
108
- tryParse(cb) {
109
- if (this.consumed) {
110
- throw new Error('JSON entity has already been consumed.');
111
- }
112
- this.buffer.locked = true;
113
- return this.buffer.resetOnFail(() => {
114
- const res = cb(this);
115
- this.buffer.locked = false;
116
- return res;
117
- }, () => {
118
- this.buffer.locked = false;
119
- });
120
- }
121
- }
122
- export class JsonString extends JsonEntity {
123
- parse() {
124
- const bytes = [];
125
- this.buffer.next(); // consume opening quotes
126
- while (this.buffer.peek() !== BYTE_MAP.quotes) {
127
- bytes.push(this.buffer.next());
128
- }
129
- this.buffer.next(); // consume closing quotes
130
- const result = bytesToString(new Uint8Array(bytes));
131
- return result;
132
- }
133
- }
134
- export class JsonNumber extends JsonEntity {
135
- parse() {
136
- const numberBytes = [];
137
- while (isNumeric(this.buffer.peek())) {
138
- numberBytes.push(this.buffer.next());
139
- }
140
- const result = bytesToNumber(new Uint8Array(numberBytes));
141
- return result;
142
- }
143
- }
144
- export class JsonBoolean extends JsonEntity {
145
- parse() {
146
- const next = this.buffer.next();
147
- if (next === BYTE_MAP.t) {
148
- this.buffer.expect(BYTE_MAP.r); // r
149
- this.buffer.expect(BYTE_MAP.u); // u
150
- this.buffer.expect(BYTE_MAP.e); // e
151
- return true;
152
- }
153
- else {
154
- this.buffer.expect(BYTE_MAP.a); // a
155
- this.buffer.expect(BYTE_MAP.l); // l
156
- this.buffer.expect(BYTE_MAP.s); // s
157
- this.buffer.expect(BYTE_MAP.e); // e
158
- return false;
159
- }
160
- }
161
- }
162
- export class JsonNull extends JsonEntity {
163
- parse() {
164
- this.buffer.expect(BYTE_MAP.n); // n
165
- this.buffer.expect(BYTE_MAP.u); // u
166
- this.buffer.expect(BYTE_MAP.l); // l
167
- this.buffer.expect(BYTE_MAP.l); // l
168
- return null;
169
- }
170
- }
171
- export class JsonValue extends JsonEntity {
172
- key;
173
- value;
174
- constructor(buffer, key) {
175
- super(buffer);
176
- this.key = key;
177
- }
178
- parse() {
179
- this.key?.consume();
180
- this.skipWhitespace();
181
- if (this.buffer.peek() === BYTE_MAP.colon) {
182
- this.buffer.next(); // consume :
183
- this.skipWhitespace();
184
- }
185
- if (this.buffer.peek() === BYTE_MAP.comma) {
186
- this.buffer.next(); // consume ,
187
- this.skipWhitespace();
188
- }
189
- const next = this.buffer.peek();
190
- if (next === BYTE_MAP.quotes) {
191
- return new JsonString(this.buffer);
192
- }
193
- else if (isNumberStart(next)) {
194
- return new JsonNumber(this.buffer);
195
- }
196
- else if (next === BYTE_MAP.t || next === BYTE_MAP.f) {
197
- return new JsonBoolean(this.buffer);
198
- }
199
- else if (next === BYTE_MAP.n) {
200
- return new JsonNull(this.buffer);
201
- }
202
- else if (next === BYTE_MAP.leftBrace) {
203
- return new JsonObject(this.buffer);
204
- }
205
- else if (next === BYTE_MAP.leftSquare) {
206
- return new JsonArray(this.buffer);
207
- }
208
- else {
209
- throw new Error('Unexpected token while parsing JSON value: ' +
210
- String.fromCharCode(next ?? 0));
211
- }
212
- }
213
- read() {
214
- if (this.value) {
215
- return this.value;
216
- }
217
- this.value = super.read();
218
- this.consumed = false;
219
- return this.value;
220
- }
221
- async readAsync() {
222
- if (this.value) {
223
- return this.value;
224
- }
225
- this.value = await super.readAsync();
226
- this.consumed = false;
227
- return this.value;
228
- }
229
- consume() {
230
- if (this.value && !this.value.consumed) {
231
- this.value.consume();
232
- }
233
- else {
234
- super.consume();
235
- }
236
- }
237
- async consumeAsync() {
238
- if (this.value && !this.value.consumed) {
239
- await this.value.consumeAsync();
240
- }
241
- else {
242
- await super.consumeAsync();
243
- }
244
- }
245
- }
246
- export class JsonObject extends JsonEntity {
247
- *members() {
248
- this.skipWhitespace();
249
- if (this.buffer.peek() === BYTE_MAP.leftBrace) {
250
- this.buffer.next(); // consume {
251
- }
252
- this.skipWhitespace();
253
- if (this.buffer.peek() === BYTE_MAP.rightBrace) {
254
- this.buffer.next(); // consume }
255
- return [];
256
- }
257
- while (this.buffer.peek() !== BYTE_MAP.rightBrace) {
258
- this.skipWhitespace();
259
- if (this.buffer.peek() === BYTE_MAP.comma) {
260
- this.buffer.next(); // consume ,
261
- this.skipWhitespace();
262
- }
263
- const key = new JsonString(this.buffer);
264
- const value = new JsonValue(this.buffer, key);
265
- yield [key, value];
266
- key.consume();
267
- value.consume();
268
- this.skipWhitespace();
269
- if (this.buffer.peek() === BYTE_MAP.comma) {
270
- this.buffer.next(); // consume ,
271
- this.skipWhitespace();
272
- }
273
- }
274
- this.buffer.expect(BYTE_MAP.rightBrace);
275
- this.consumed = true;
276
- }
277
- async *membersAsync() {
278
- while (!this.buffer.atEof()) {
279
- await this.buffer.readStream();
280
- const memberGen = this.members();
281
- let currentMember = null;
282
- while (true) {
283
- currentMember = this.tryParse(() => memberGen.next());
284
- if (currentMember === null) {
285
- break; // Need more data
286
- }
287
- if (currentMember.done) {
288
- return; // No more members
289
- }
290
- yield currentMember.value;
291
- // Make sure to consume the key and value asynchronously
292
- await currentMember.value[0].consumeAsync();
293
- await currentMember.value[1].consumeAsync();
294
- }
295
- }
296
- }
297
- [Symbol.iterator]() {
298
- return this.members();
299
- }
300
- [Symbol.asyncIterator]() {
301
- return this.membersAsync();
302
- }
303
- parse() {
304
- const obj = {};
305
- for (const [key, value] of this.members()) {
306
- obj[key.read()] = value.read().read();
307
- }
308
- return obj;
309
- }
310
- }
311
- export class JsonArray extends JsonEntity {
312
- *items() {
313
- this.skipWhitespace();
314
- if (this.buffer.peek() === BYTE_MAP.leftSquare) {
315
- this.buffer.next(); // consume [
316
- }
317
- this.skipWhitespace();
318
- if (this.buffer.peek() === BYTE_MAP.rightSquare) {
319
- this.buffer.next(); // consume ]
320
- return [];
321
- }
322
- while (this.buffer.peek() !== BYTE_MAP.rightSquare) {
323
- this.skipWhitespace();
324
- if (this.buffer.peek() === BYTE_MAP.comma) {
325
- this.buffer.next(); // consume ,
326
- this.skipWhitespace();
327
- }
328
- const value = new JsonValue(this.buffer).read();
329
- yield value;
330
- if (!value.consumed)
331
- value.read();
332
- this.skipWhitespace();
333
- }
334
- this.buffer.expect(BYTE_MAP.rightSquare);
335
- this.consumed = true;
336
- }
337
- async *itemsAsync() {
338
- while (!this.buffer.atEof()) {
339
- await this.buffer.readStream();
340
- const itemGen = this.items();
341
- let currentItem = null;
342
- while (true) {
343
- currentItem = this.tryParse(() => itemGen.next());
344
- if (currentItem === null) {
345
- break; // Need more data
346
- }
347
- if (currentItem.done) {
348
- return; // No more items
349
- }
350
- yield currentItem.value;
351
- await currentItem.value.consumeAsync();
352
- }
353
- }
354
- }
355
- [Symbol.iterator]() {
356
- return this.items();
357
- }
358
- [Symbol.asyncIterator]() {
359
- return this.itemsAsync();
360
- }
361
- parse() {
362
- const values = [];
363
- for (const value of this.items()) {
364
- values.push(value.read());
365
- }
366
- return values;
367
- }
368
- }
369
- export class JsonKeyValueParser {
370
- container;
371
- parentKey;
372
- constructor(container, parentKey) {
373
- this.container = container ?? new JsonValue();
374
- this.parentKey = parentKey;
375
- }
376
- tryParse(cb) {
377
- return this.container.tryParse(() => cb(this));
378
- }
379
- feed(...input) {
380
- for (const item of input) {
381
- this.container.feed(item);
382
- }
383
- }
384
- *parse() {
385
- if (this.container instanceof JsonObject) {
386
- for (const [keyEntity, valueEntity] of this.container.members()) {
387
- const key = keyEntity.read();
388
- const value = valueEntity.read();
389
- const finalKey = this.parentKey
390
- ? `${this.parentKey}.${key}`
391
- : key;
392
- if (value instanceof JsonArray || value instanceof JsonObject) {
393
- yield* new JsonKeyValueParser(value, finalKey).parse();
394
- }
395
- else {
396
- yield [finalKey, value.read()];
397
- }
398
- }
399
- }
400
- else if (this.container instanceof JsonArray) {
401
- let index = 0;
402
- for (const valueEntity of this.container.items()) {
403
- const value = valueEntity;
404
- const finalKey = this.parentKey
405
- ? `${this.parentKey}[${index}]`
406
- : `[${index}]`;
407
- if (value instanceof JsonArray || value instanceof JsonObject) {
408
- yield* new JsonKeyValueParser(value, finalKey).parse();
409
- }
410
- else {
411
- yield [finalKey, value.read()];
412
- }
413
- index++;
414
- }
415
- }
416
- else if (this.container instanceof JsonValue) {
417
- const value = this.container.read();
418
- if (!(value instanceof JsonArray) &&
419
- !(value instanceof JsonObject)) {
420
- throw new Error(`JsonValue does not contain a composite type. ${value.entityType} found.`);
421
- }
422
- yield* new JsonKeyValueParser(value, this.parentKey).parse();
423
- }
424
- }
425
- [Symbol.iterator]() {
426
- return this.parse();
427
- }
428
- }