@depup/stream-json 1.9.1-depup.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.
package/Parser.js ADDED
@@ -0,0 +1,545 @@
1
+ 'use strict';
2
+
3
+ const Utf8Stream = require('./utils/Utf8Stream');
4
+
5
+ const patterns = {
6
+ value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
7
+ string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
8
+ key1: /^(?:[\"\}]|\s{1,256})/,
9
+ colon: /^(?:\:|\s{1,256})/,
10
+ comma: /^(?:[\,\]\}]|\s{1,256})/,
11
+ ws: /^\s{1,256}/,
12
+ numberStart: /^\d/,
13
+ numberDigit: /^\d{0,256}/,
14
+ numberFraction: /^[\.eE]/,
15
+ numberExponent: /^[eE]/,
16
+ numberExpSign: /^[-+]/
17
+ };
18
+ const MAX_PATTERN_SIZE = 16;
19
+
20
+ let noSticky = true;
21
+ try {
22
+ new RegExp('.', 'y');
23
+ noSticky = false;
24
+ } catch (e) {
25
+ // suppress
26
+ }
27
+
28
+ !noSticky &&
29
+ Object.keys(patterns).forEach(key => {
30
+ let src = patterns[key].source.slice(1); // lop off ^
31
+ if (src.slice(0, 3) === '(?:' && src.slice(-1) === ')') {
32
+ src = src.slice(3, -1);
33
+ }
34
+ patterns[key] = new RegExp(src, 'y');
35
+ });
36
+
37
+ patterns.numberFracStart = patterns.numberExpStart = patterns.numberStart;
38
+ patterns.numberFracDigit = patterns.numberExpDigit = patterns.numberDigit;
39
+
40
+ const values = {true: true, false: false, null: null},
41
+ expected = {object: 'objectStop', array: 'arrayStop', '': 'done'};
42
+
43
+ // long hexadecimal codes: \uXXXX
44
+ const fromHex = s => String.fromCharCode(parseInt(s.slice(2), 16));
45
+
46
+ // short codes: \b \f \n \r \t \" \\ \/
47
+ const codes = {b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', '"': '"', '\\': '\\', '/': '/'};
48
+
49
+ class Parser extends Utf8Stream {
50
+ static make(options) {
51
+ return new Parser(options);
52
+ }
53
+
54
+ constructor(options) {
55
+ super(Object.assign({}, options, {readableObjectMode: true}));
56
+
57
+ this._packKeys = this._packStrings = this._packNumbers = this._streamKeys = this._streamStrings = this._streamNumbers = true;
58
+ if (options) {
59
+ 'packValues' in options && (this._packKeys = this._packStrings = this._packNumbers = options.packValues);
60
+ 'packKeys' in options && (this._packKeys = options.packKeys);
61
+ 'packStrings' in options && (this._packStrings = options.packStrings);
62
+ 'packNumbers' in options && (this._packNumbers = options.packNumbers);
63
+ 'streamValues' in options && (this._streamKeys = this._streamStrings = this._streamNumbers = options.streamValues);
64
+ 'streamKeys' in options && (this._streamKeys = options.streamKeys);
65
+ 'streamStrings' in options && (this._streamStrings = options.streamStrings);
66
+ 'streamNumbers' in options && (this._streamNumbers = options.streamNumbers);
67
+ this._jsonStreaming = options.jsonStreaming;
68
+ }
69
+ !this._packKeys && (this._streamKeys = true);
70
+ !this._packStrings && (this._streamStrings = true);
71
+ !this._packNumbers && (this._streamNumbers = true);
72
+
73
+ this._done = false;
74
+ this._expect = this._jsonStreaming ? 'done' : 'value';
75
+ this._stack = [];
76
+ this._parent = '';
77
+ this._open_number = false;
78
+ this._accumulator = '';
79
+ }
80
+
81
+ _flush(callback) {
82
+ this._done = true;
83
+ super._flush(error => {
84
+ if (error) return callback(error);
85
+ if (this._open_number) {
86
+ if (this._streamNumbers) {
87
+ this.push({name: 'endNumber'});
88
+ }
89
+ this._open_number = false;
90
+ if (this._packNumbers) {
91
+ this.push({name: 'numberValue', value: this._accumulator});
92
+ this._accumulator = '';
93
+ }
94
+ }
95
+ callback(null);
96
+ });
97
+ }
98
+
99
+ _processBuffer(callback) {
100
+ let match,
101
+ value,
102
+ index = 0;
103
+ main: for (;;) {
104
+ switch (this._expect) {
105
+ case 'value1':
106
+ case 'value':
107
+ patterns.value1.lastIndex = index;
108
+ match = patterns.value1.exec(this._buffer);
109
+ if (!match) {
110
+ if (this._done || index + MAX_PATTERN_SIZE < this._buffer.length) {
111
+ if (index < this._buffer.length) return callback(new Error('Parser cannot parse input: expected a value'));
112
+ return callback(new Error('Parser has expected a value'));
113
+ }
114
+ break main; // wait for more input
115
+ }
116
+ value = match[0];
117
+ switch (value) {
118
+ case '"':
119
+ this._streamStrings && this.push({name: 'startString'});
120
+ this._expect = 'string';
121
+ break;
122
+ case '{':
123
+ this.push({name: 'startObject'});
124
+ this._stack.push(this._parent);
125
+ this._parent = 'object';
126
+ this._expect = 'key1';
127
+ break;
128
+ case '[':
129
+ this.push({name: 'startArray'});
130
+ this._stack.push(this._parent);
131
+ this._parent = 'array';
132
+ this._expect = 'value1';
133
+ break;
134
+ case ']':
135
+ if (this._expect !== 'value1') return callback(new Error("Parser cannot parse input: unexpected token ']'"));
136
+ if (this._open_number) {
137
+ this._streamNumbers && this.push({name: 'endNumber'});
138
+ this._open_number = false;
139
+ if (this._packNumbers) {
140
+ this.push({name: 'numberValue', value: this._accumulator});
141
+ this._accumulator = '';
142
+ }
143
+ }
144
+ this.push({name: 'endArray'});
145
+ this._parent = this._stack.pop();
146
+ this._expect = expected[this._parent];
147
+ break;
148
+ case '-':
149
+ this._open_number = true;
150
+ if (this._streamNumbers) {
151
+ this.push({name: 'startNumber'});
152
+ this.push({name: 'numberChunk', value: '-'});
153
+ }
154
+ this._packNumbers && (this._accumulator = '-');
155
+ this._expect = 'numberStart';
156
+ break;
157
+ case '0':
158
+ this._open_number = true;
159
+ if (this._streamNumbers) {
160
+ this.push({name: 'startNumber'});
161
+ this.push({name: 'numberChunk', value: '0'});
162
+ }
163
+ this._packNumbers && (this._accumulator = '0');
164
+ this._expect = 'numberFraction';
165
+ break;
166
+ case '1':
167
+ case '2':
168
+ case '3':
169
+ case '4':
170
+ case '5':
171
+ case '6':
172
+ case '7':
173
+ case '8':
174
+ case '9':
175
+ this._open_number = true;
176
+ if (this._streamNumbers) {
177
+ this.push({name: 'startNumber'});
178
+ this.push({name: 'numberChunk', value: value});
179
+ }
180
+ this._packNumbers && (this._accumulator = value);
181
+ this._expect = 'numberDigit';
182
+ break;
183
+ case 'true':
184
+ case 'false':
185
+ case 'null':
186
+ if (this._buffer.length - index === value.length && !this._done) break main; // wait for more input
187
+ this.push({name: value + 'Value', value: values[value]});
188
+ this._expect = expected[this._parent];
189
+ break;
190
+ // default: // ws
191
+ }
192
+ if (noSticky) {
193
+ this._buffer = this._buffer.slice(value.length);
194
+ } else {
195
+ index += value.length;
196
+ }
197
+ break;
198
+ case 'keyVal':
199
+ case 'string':
200
+ patterns.string.lastIndex = index;
201
+ match = patterns.string.exec(this._buffer);
202
+ if (!match) {
203
+ if (index < this._buffer.length && (this._done || this._buffer.length - index >= 6))
204
+ return callback(new Error('Parser cannot parse input: escaped characters'));
205
+ if (this._done) return callback(new Error('Parser has expected a string value'));
206
+ break main; // wait for more input
207
+ }
208
+ value = match[0];
209
+ if (value === '"') {
210
+ if (this._expect === 'keyVal') {
211
+ this._streamKeys && this.push({name: 'endKey'});
212
+ if (this._packKeys) {
213
+ this.push({name: 'keyValue', value: this._accumulator});
214
+ this._accumulator = '';
215
+ }
216
+ this._expect = 'colon';
217
+ } else {
218
+ this._streamStrings && this.push({name: 'endString'});
219
+ if (this._packStrings) {
220
+ this.push({name: 'stringValue', value: this._accumulator});
221
+ this._accumulator = '';
222
+ }
223
+ this._expect = expected[this._parent];
224
+ }
225
+ } else if (value.length > 1 && value.charAt(0) === '\\') {
226
+ const t = value.length == 2 ? codes[value.charAt(1)] : fromHex(value);
227
+ if (this._expect === 'keyVal' ? this._streamKeys : this._streamStrings) {
228
+ this.push({name: 'stringChunk', value: t});
229
+ }
230
+ if (this._expect === 'keyVal' ? this._packKeys : this._packStrings) {
231
+ this._accumulator += t;
232
+ }
233
+ } else {
234
+ if (this._expect === 'keyVal' ? this._streamKeys : this._streamStrings) {
235
+ this.push({name: 'stringChunk', value: value});
236
+ }
237
+ if (this._expect === 'keyVal' ? this._packKeys : this._packStrings) {
238
+ this._accumulator += value;
239
+ }
240
+ }
241
+ if (noSticky) {
242
+ this._buffer = this._buffer.slice(value.length);
243
+ } else {
244
+ index += value.length;
245
+ }
246
+ break;
247
+ case 'key1':
248
+ case 'key':
249
+ patterns.key1.lastIndex = index;
250
+ match = patterns.key1.exec(this._buffer);
251
+ if (!match) {
252
+ if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected an object key'));
253
+ break main; // wait for more input
254
+ }
255
+ value = match[0];
256
+ if (value === '"') {
257
+ this._streamKeys && this.push({name: 'startKey'});
258
+ this._expect = 'keyVal';
259
+ } else if (value === '}') {
260
+ if (this._expect !== 'key1') return callback(new Error("Parser cannot parse input: unexpected token '}'"));
261
+ this.push({name: 'endObject'});
262
+ this._parent = this._stack.pop();
263
+ this._expect = expected[this._parent];
264
+ }
265
+ if (noSticky) {
266
+ this._buffer = this._buffer.slice(value.length);
267
+ } else {
268
+ index += value.length;
269
+ }
270
+ break;
271
+ case 'colon':
272
+ patterns.colon.lastIndex = index;
273
+ match = patterns.colon.exec(this._buffer);
274
+ if (!match) {
275
+ if (index < this._buffer.length || this._done) return callback(new Error("Parser cannot parse input: expected ':'"));
276
+ break main; // wait for more input
277
+ }
278
+ value = match[0];
279
+ value === ':' && (this._expect = 'value');
280
+ if (noSticky) {
281
+ this._buffer = this._buffer.slice(value.length);
282
+ } else {
283
+ index += value.length;
284
+ }
285
+ break;
286
+ case 'arrayStop':
287
+ case 'objectStop':
288
+ patterns.comma.lastIndex = index;
289
+ match = patterns.comma.exec(this._buffer);
290
+ if (!match) {
291
+ if (index < this._buffer.length || this._done) return callback(new Error("Parser cannot parse input: expected ','"));
292
+ break main; // wait for more input
293
+ }
294
+ if (this._open_number) {
295
+ this._streamNumbers && this.push({name: 'endNumber'});
296
+ this._open_number = false;
297
+ if (this._packNumbers) {
298
+ this.push({name: 'numberValue', value: this._accumulator});
299
+ this._accumulator = '';
300
+ }
301
+ }
302
+ value = match[0];
303
+ if (value === ',') {
304
+ this._expect = this._expect === 'arrayStop' ? 'value' : 'key';
305
+ } else if (value === '}' || value === ']') {
306
+ if (value === '}' ? this._expect === 'arrayStop' : this._expect !== 'arrayStop') {
307
+ return callback(new Error("Parser cannot parse input: expected '" + (this._expect === 'arrayStop' ? ']' : '}') + "'"));
308
+ }
309
+ this.push({name: value === '}' ? 'endObject' : 'endArray'});
310
+ this._parent = this._stack.pop();
311
+ this._expect = expected[this._parent];
312
+ }
313
+ if (noSticky) {
314
+ this._buffer = this._buffer.slice(value.length);
315
+ } else {
316
+ index += value.length;
317
+ }
318
+ break;
319
+ // number chunks
320
+ case 'numberStart': // [0-9]
321
+ patterns.numberStart.lastIndex = index;
322
+ match = patterns.numberStart.exec(this._buffer);
323
+ if (!match) {
324
+ if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a starting digit'));
325
+ break main; // wait for more input
326
+ }
327
+ value = match[0];
328
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
329
+ this._packNumbers && (this._accumulator += value);
330
+ this._expect = value === '0' ? 'numberFraction' : 'numberDigit';
331
+ if (noSticky) {
332
+ this._buffer = this._buffer.slice(value.length);
333
+ } else {
334
+ index += value.length;
335
+ }
336
+ break;
337
+ case 'numberDigit': // [0-9]*
338
+ patterns.numberDigit.lastIndex = index;
339
+ match = patterns.numberDigit.exec(this._buffer);
340
+ if (!match) {
341
+ if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a digit'));
342
+ break main; // wait for more input
343
+ }
344
+ value = match[0];
345
+ if (value) {
346
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
347
+ this._packNumbers && (this._accumulator += value);
348
+ if (noSticky) {
349
+ this._buffer = this._buffer.slice(value.length);
350
+ } else {
351
+ index += value.length;
352
+ }
353
+ } else {
354
+ if (index < this._buffer.length) {
355
+ this._expect = 'numberFraction';
356
+ break;
357
+ }
358
+ if (this._done) {
359
+ this._expect = expected[this._parent];
360
+ break;
361
+ }
362
+ break main; // wait for more input
363
+ }
364
+ break;
365
+ case 'numberFraction': // [\.eE]?
366
+ patterns.numberFraction.lastIndex = index;
367
+ match = patterns.numberFraction.exec(this._buffer);
368
+ if (!match) {
369
+ if (index < this._buffer.length || this._done) {
370
+ this._expect = expected[this._parent];
371
+ break;
372
+ }
373
+ break main; // wait for more input
374
+ }
375
+ value = match[0];
376
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
377
+ this._packNumbers && (this._accumulator += value);
378
+ this._expect = value === '.' ? 'numberFracStart' : 'numberExpSign';
379
+ if (noSticky) {
380
+ this._buffer = this._buffer.slice(value.length);
381
+ } else {
382
+ index += value.length;
383
+ }
384
+ break;
385
+ case 'numberFracStart': // [0-9]
386
+ patterns.numberFracStart.lastIndex = index;
387
+ match = patterns.numberFracStart.exec(this._buffer);
388
+ if (!match) {
389
+ if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected a fractional part of a number'));
390
+ break main; // wait for more input
391
+ }
392
+ value = match[0];
393
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
394
+ this._packNumbers && (this._accumulator += value);
395
+ this._expect = 'numberFracDigit';
396
+ if (noSticky) {
397
+ this._buffer = this._buffer.slice(value.length);
398
+ } else {
399
+ index += value.length;
400
+ }
401
+ break;
402
+ case 'numberFracDigit': // [0-9]*
403
+ patterns.numberFracDigit.lastIndex = index;
404
+ match = patterns.numberFracDigit.exec(this._buffer);
405
+ value = match[0];
406
+ if (value) {
407
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
408
+ this._packNumbers && (this._accumulator += value);
409
+ if (noSticky) {
410
+ this._buffer = this._buffer.slice(value.length);
411
+ } else {
412
+ index += value.length;
413
+ }
414
+ } else {
415
+ if (index < this._buffer.length) {
416
+ this._expect = 'numberExponent';
417
+ break;
418
+ }
419
+ if (this._done) {
420
+ this._expect = expected[this._parent];
421
+ break;
422
+ }
423
+ break main; // wait for more input
424
+ }
425
+ break;
426
+ case 'numberExponent': // [eE]?
427
+ patterns.numberExponent.lastIndex = index;
428
+ match = patterns.numberExponent.exec(this._buffer);
429
+ if (!match) {
430
+ if (index < this._buffer.length) {
431
+ this._expect = expected[this._parent];
432
+ break;
433
+ }
434
+ if (this._done) {
435
+ this._expect = 'done';
436
+ break;
437
+ }
438
+ break main; // wait for more input
439
+ }
440
+ value = match[0];
441
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
442
+ this._packNumbers && (this._accumulator += value);
443
+ this._expect = 'numberExpSign';
444
+ if (noSticky) {
445
+ this._buffer = this._buffer.slice(value.length);
446
+ } else {
447
+ index += value.length;
448
+ }
449
+ break;
450
+ case 'numberExpSign': // [-+]?
451
+ patterns.numberExpSign.lastIndex = index;
452
+ match = patterns.numberExpSign.exec(this._buffer);
453
+ if (!match) {
454
+ if (index < this._buffer.length) {
455
+ this._expect = 'numberExpStart';
456
+ break;
457
+ }
458
+ if (this._done) return callback(new Error('Parser has expected an exponent value of a number'));
459
+ break main; // wait for more input
460
+ }
461
+ value = match[0];
462
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
463
+ this._packNumbers && (this._accumulator += value);
464
+ this._expect = 'numberExpStart';
465
+ if (noSticky) {
466
+ this._buffer = this._buffer.slice(value.length);
467
+ } else {
468
+ index += value.length;
469
+ }
470
+ break;
471
+ case 'numberExpStart': // [0-9]
472
+ patterns.numberExpStart.lastIndex = index;
473
+ match = patterns.numberExpStart.exec(this._buffer);
474
+ if (!match) {
475
+ if (index < this._buffer.length || this._done) return callback(new Error('Parser cannot parse input: expected an exponent part of a number'));
476
+ break main; // wait for more input
477
+ }
478
+ value = match[0];
479
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
480
+ this._packNumbers && (this._accumulator += value);
481
+ this._expect = 'numberExpDigit';
482
+ if (noSticky) {
483
+ this._buffer = this._buffer.slice(value.length);
484
+ } else {
485
+ index += value.length;
486
+ }
487
+ break;
488
+ case 'numberExpDigit': // [0-9]*
489
+ patterns.numberExpDigit.lastIndex = index;
490
+ match = patterns.numberExpDigit.exec(this._buffer);
491
+ value = match[0];
492
+ if (value) {
493
+ this._streamNumbers && this.push({name: 'numberChunk', value: value});
494
+ this._packNumbers && (this._accumulator += value);
495
+ if (noSticky) {
496
+ this._buffer = this._buffer.slice(value.length);
497
+ } else {
498
+ index += value.length;
499
+ }
500
+ } else {
501
+ if (index < this._buffer.length || this._done) {
502
+ this._expect = expected[this._parent];
503
+ break;
504
+ }
505
+ break main; // wait for more input
506
+ }
507
+ break;
508
+ case 'done':
509
+ patterns.ws.lastIndex = index;
510
+ match = patterns.ws.exec(this._buffer);
511
+ if (!match) {
512
+ if (index < this._buffer.length) {
513
+ if (this._jsonStreaming) {
514
+ this._expect = 'value';
515
+ break;
516
+ }
517
+ return callback(new Error('Parser cannot parse input: unexpected characters'));
518
+ }
519
+ break main; // wait for more input
520
+ }
521
+ value = match[0];
522
+ if (this._open_number) {
523
+ this._streamNumbers && this.push({name: 'endNumber'});
524
+ this._open_number = false;
525
+ if (this._packNumbers) {
526
+ this.push({name: 'numberValue', value: this._accumulator});
527
+ this._accumulator = '';
528
+ }
529
+ }
530
+ if (noSticky) {
531
+ this._buffer = this._buffer.slice(value.length);
532
+ } else {
533
+ index += value.length;
534
+ }
535
+ break;
536
+ }
537
+ }
538
+ !noSticky && (this._buffer = this._buffer.slice(index));
539
+ callback(null);
540
+ }
541
+ }
542
+ Parser.parser = Parser.make;
543
+ Parser.make.Constructor = Parser;
544
+
545
+ module.exports = Parser;
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @depup/stream-json
2
+
3
+ > Dependency-bumped version of [stream-json](https://www.npmjs.com/package/stream-json)
4
+
5
+ Generated by [DepUp](https://github.com/depup/npm) -- all production
6
+ dependencies bumped to latest versions.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @depup/stream-json
12
+ ```
13
+
14
+ | Field | Value |
15
+ |-------|-------|
16
+ | Original | [stream-json](https://www.npmjs.com/package/stream-json) @ 1.9.1 |
17
+ | Processed | 2026-03-17 |
18
+ | Smoke test | passed |
19
+ | Deps updated | 1 |
20
+
21
+ ## Dependency Changes
22
+
23
+ | Dependency | From | To |
24
+ |------------|------|-----|
25
+ | stream-chain | ^2.2.5 | ^3.4.1 |
26
+
27
+ ---
28
+
29
+ Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/stream-json
30
+
31
+ License inherited from the original package.