@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.
@@ -0,0 +1,485 @@
1
+ 'use strict';
2
+
3
+ const {Writable} = require('stream');
4
+ const {StringDecoder} = require('string_decoder');
5
+
6
+ const patterns = {
7
+ value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
8
+ string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
9
+ key1: /^(?:[\"\}]|\s{1,256})/,
10
+ colon: /^(?:\:|\s{1,256})/,
11
+ comma: /^(?:[\,\]\}]|\s{1,256})/,
12
+ ws: /^\s{1,256}/,
13
+ numberStart: /^\d/,
14
+ numberDigit: /^\d{0,256}/,
15
+ numberFraction: /^[\.eE]/,
16
+ numberExponent: /^[eE]/,
17
+ numberExpSign: /^[-+]/
18
+ };
19
+ const MAX_PATTERN_SIZE = 16;
20
+
21
+ let noSticky = true;
22
+ try {
23
+ new RegExp('.', 'y');
24
+ noSticky = false;
25
+ } catch (e) {
26
+ // suppress
27
+ }
28
+
29
+ !noSticky &&
30
+ Object.keys(patterns).forEach(key => {
31
+ let src = patterns[key].source.slice(1); // lop off ^
32
+ if (src.slice(0, 3) === '(?:' && src.slice(-1) === ')') {
33
+ src = src.slice(3, -1);
34
+ }
35
+ patterns[key] = new RegExp(src, 'y');
36
+ });
37
+
38
+ patterns.numberFracStart = patterns.numberExpStart = patterns.numberStart;
39
+ patterns.numberFracDigit = patterns.numberExpDigit = patterns.numberDigit;
40
+
41
+ const eol = /[\u000A\u2028\u2029]|\u000D\u000A|\u000D/g;
42
+
43
+ const expected = {object: 'objectStop', array: 'arrayStop', '': 'done'};
44
+
45
+ class Verifier extends Writable {
46
+ static make(options) {
47
+ return new Verifier(options);
48
+ }
49
+
50
+ constructor(options) {
51
+ super(Object.assign({}, options, {objectMode: false}));
52
+
53
+ if (options) {
54
+ this._jsonStreaming = options.jsonStreaming;
55
+ }
56
+
57
+ this._buffer = '';
58
+ this._done = false;
59
+ this._expect = this._jsonStreaming ? 'done' : 'value';
60
+ this._stack = [];
61
+ this._parent = '';
62
+
63
+ this._line = this._pos = 1;
64
+ this._offset = 0;
65
+ }
66
+
67
+ _write(chunk, encoding, callback) {
68
+ if (typeof chunk == 'string') {
69
+ this._write = this._writeString;
70
+ } else {
71
+ this._stringDecoder = new StringDecoder();
72
+ this._write = this._writeBuffer;
73
+ }
74
+ this._write(chunk, encoding, callback);
75
+ }
76
+
77
+ _writeBuffer(chunk, _, callback) {
78
+ this._buffer += this._stringDecoder.write(chunk);
79
+ this._processBuffer(callback);
80
+ }
81
+
82
+ _writeString(chunk, _, callback) {
83
+ this._buffer += chunk.toString();
84
+ this._processBuffer(callback);
85
+ }
86
+
87
+ _final(callback) {
88
+ if (this._stringDecoder) {
89
+ this._buffer += this._stringDecoder.end();
90
+ }
91
+ this._done = true;
92
+ this._processBuffer(callback);
93
+ }
94
+
95
+ _makeError(msg) {
96
+ const error = new Error('ERROR at ' + this._offset + ' (' + this._line + ', ' + this._pos + '): ' + msg);
97
+ error.line = this._line;
98
+ error.pos = this._pos;
99
+ error.offset = this._offset;
100
+ return error;
101
+ }
102
+
103
+ _updatePos(value) {
104
+ let len = value.length;
105
+ this._offset += len;
106
+ value.replace(eol, (match, offset) => {
107
+ len = value.length - match.length - offset;
108
+ ++this._line;
109
+ this._pos = 1;
110
+ return '';
111
+ });
112
+ this._pos += len;
113
+ }
114
+
115
+ _processBuffer(callback) {
116
+ let match,
117
+ value,
118
+ index = 0;
119
+ main: for (;;) {
120
+ switch (this._expect) {
121
+ case 'value1':
122
+ case 'value':
123
+ patterns.value1.lastIndex = index;
124
+ match = patterns.value1.exec(this._buffer);
125
+ if (!match) {
126
+ if (this._done || index + MAX_PATTERN_SIZE < this._buffer.length) {
127
+ if (index < this._buffer.length) return callback(this._makeError('Verifier cannot parse input: expected a value'));
128
+ return callback(this._makeError('Verifier has expected a value'));
129
+ }
130
+ break main; // wait for more input
131
+ }
132
+ value = match[0];
133
+ switch (value) {
134
+ case '"':
135
+ this._expect = 'string';
136
+ break;
137
+ case '{':
138
+ this._stack.push(this._parent);
139
+ this._parent = 'object';
140
+ this._expect = 'key1';
141
+ break;
142
+ case '[':
143
+ this._stack.push(this._parent);
144
+ this._parent = 'array';
145
+ this._expect = 'value1';
146
+ break;
147
+ case ']':
148
+ if (this._expect !== 'value1') return callback(this._makeError("Verifier cannot parse input: unexpected token ']'"));
149
+ this._parent = this._stack.pop();
150
+ this._expect = expected[this._parent];
151
+ break;
152
+ case '-':
153
+ this._expect = 'numberStart';
154
+ break;
155
+ case '0':
156
+ this._expect = 'numberFraction';
157
+ break;
158
+ case '1':
159
+ case '2':
160
+ case '3':
161
+ case '4':
162
+ case '5':
163
+ case '6':
164
+ case '7':
165
+ case '8':
166
+ case '9':
167
+ this._expect = 'numberDigit';
168
+ break;
169
+ case 'true':
170
+ case 'false':
171
+ case 'null':
172
+ if (this._buffer.length - index === value.length && !this._done) break main; // wait for more input
173
+ this._expect = expected[this._parent];
174
+ break;
175
+ // default: // ws
176
+ }
177
+ this._updatePos(value);
178
+ if (noSticky) {
179
+ this._buffer = this._buffer.slice(value.length);
180
+ } else {
181
+ index += value.length;
182
+ }
183
+ break;
184
+ case 'keyVal':
185
+ case 'string':
186
+ patterns.string.lastIndex = index;
187
+ match = patterns.string.exec(this._buffer);
188
+ if (!match) {
189
+ if (index < this._buffer.length && (this._done || this._buffer.length - index >= 6))
190
+ return callback(this._makeError('Verifier cannot parse input: escaped characters'));
191
+ if (this._done) return callback(this._makeError('Verifier has expected a string value'));
192
+ break main; // wait for more input
193
+ }
194
+ value = match[0];
195
+ if (value === '"') {
196
+ if (this._expect === 'keyVal') {
197
+ this._expect = 'colon';
198
+ } else {
199
+ this._expect = expected[this._parent];
200
+ }
201
+ }
202
+ this._updatePos(value);
203
+ if (noSticky) {
204
+ this._buffer = this._buffer.slice(value.length);
205
+ } else {
206
+ index += value.length;
207
+ }
208
+ break;
209
+ case 'key1':
210
+ case 'key':
211
+ patterns.key1.lastIndex = index;
212
+ match = patterns.key1.exec(this._buffer);
213
+ if (!match) {
214
+ if (index < this._buffer.length || this._done) return callback(this._makeError('Verifier cannot parse input: expected an object key'));
215
+ break main; // wait for more input
216
+ }
217
+ value = match[0];
218
+ if (value === '"') {
219
+ this._expect = 'keyVal';
220
+ } else if (value === '}') {
221
+ if (this._expect !== 'key1') return callback(this._makeError("Verifier cannot parse input: unexpected token '}'"));
222
+ this._parent = this._stack.pop();
223
+ this._expect = expected[this._parent];
224
+ }
225
+ this._updatePos(value);
226
+ if (noSticky) {
227
+ this._buffer = this._buffer.slice(value.length);
228
+ } else {
229
+ index += value.length;
230
+ }
231
+ break;
232
+ case 'colon':
233
+ patterns.colon.lastIndex = index;
234
+ match = patterns.colon.exec(this._buffer);
235
+ if (!match) {
236
+ if (index < this._buffer.length || this._done) return callback(this._makeError("Verifier cannot parse input: expected ':'"));
237
+ break main; // wait for more input
238
+ }
239
+ value = match[0];
240
+ value === ':' && (this._expect = 'value');
241
+ this._updatePos(value);
242
+ if (noSticky) {
243
+ this._buffer = this._buffer.slice(value.length);
244
+ } else {
245
+ index += value.length;
246
+ }
247
+ break;
248
+ case 'arrayStop':
249
+ case 'objectStop':
250
+ patterns.comma.lastIndex = index;
251
+ match = patterns.comma.exec(this._buffer);
252
+ if (!match) {
253
+ if (index < this._buffer.length || this._done) return callback(this._makeError("Verifier cannot parse input: expected ','"));
254
+ break main; // wait for more input
255
+ }
256
+ value = match[0];
257
+ if (value === ',') {
258
+ this._expect = this._expect === 'arrayStop' ? 'value' : 'key';
259
+ } else if (value === '}' || value === ']') {
260
+ if (value === '}' ? this._expect === 'arrayStop' : this._expect !== 'arrayStop') {
261
+ return callback(this._makeError("Verifier cannot parse input: expected '" + (this._expect === 'arrayStop' ? ']' : '}') + "'"));
262
+ }
263
+ this._parent = this._stack.pop();
264
+ this._expect = expected[this._parent];
265
+ }
266
+ this._updatePos(value);
267
+ if (noSticky) {
268
+ this._buffer = this._buffer.slice(value.length);
269
+ } else {
270
+ index += value.length;
271
+ }
272
+ break;
273
+ // number chunks
274
+ case 'numberStart': // [0-9]
275
+ patterns.numberStart.lastIndex = index;
276
+ match = patterns.numberStart.exec(this._buffer);
277
+ if (!match) {
278
+ if (index < this._buffer.length || this._done) return callback(this._makeError('Verifier cannot parse input: expected a starting digit'));
279
+ break main; // wait for more input
280
+ }
281
+ value = match[0];
282
+ this._expect = value === '0' ? 'numberFraction' : 'numberDigit';
283
+ this._updatePos(value);
284
+ if (noSticky) {
285
+ this._buffer = this._buffer.slice(value.length);
286
+ } else {
287
+ index += value.length;
288
+ }
289
+ break;
290
+ case 'numberDigit': // [0-9]*
291
+ patterns.numberDigit.lastIndex = index;
292
+ match = patterns.numberDigit.exec(this._buffer);
293
+ if (!match) {
294
+ if (index < this._buffer.length || this._done) return callback(this._makeError('Verifier cannot parse input: expected a digit'));
295
+ break main; // wait for more input
296
+ }
297
+ value = match[0];
298
+ if (value) {
299
+ this._updatePos(value);
300
+ if (noSticky) {
301
+ this._buffer = this._buffer.slice(value.length);
302
+ } else {
303
+ index += value.length;
304
+ }
305
+ } else {
306
+ if (index < this._buffer.length) {
307
+ this._expect = 'numberFraction';
308
+ break;
309
+ }
310
+ if (this._done) {
311
+ this._expect = expected[this._parent];
312
+ break;
313
+ }
314
+ break main; // wait for more input
315
+ }
316
+ break;
317
+ case 'numberFraction': // [\.eE]?
318
+ patterns.numberFraction.lastIndex = index;
319
+ match = patterns.numberFraction.exec(this._buffer);
320
+ if (!match) {
321
+ if (index < this._buffer.length || this._done) {
322
+ this._expect = expected[this._parent];
323
+ break;
324
+ }
325
+ break main; // wait for more input
326
+ }
327
+ value = match[0];
328
+ this._expect = value === '.' ? 'numberFracStart' : 'numberExpSign';
329
+ this._updatePos(value);
330
+ if (noSticky) {
331
+ this._buffer = this._buffer.slice(value.length);
332
+ } else {
333
+ index += value.length;
334
+ }
335
+ break;
336
+ case 'numberFracStart': // [0-9]
337
+ patterns.numberFracStart.lastIndex = index;
338
+ match = patterns.numberFracStart.exec(this._buffer);
339
+ if (!match) {
340
+ if (index < this._buffer.length || this._done)
341
+ return callback(this._makeError('Verifier cannot parse input: expected a fractional part of a number'));
342
+ break main; // wait for more input
343
+ }
344
+ value = match[0];
345
+ this._expect = 'numberFracDigit';
346
+ this._updatePos(value);
347
+ if (noSticky) {
348
+ this._buffer = this._buffer.slice(value.length);
349
+ } else {
350
+ index += value.length;
351
+ }
352
+ break;
353
+ case 'numberFracDigit': // [0-9]*
354
+ patterns.numberFracDigit.lastIndex = index;
355
+ match = patterns.numberFracDigit.exec(this._buffer);
356
+ value = match[0];
357
+ if (value) {
358
+ this._updatePos(value);
359
+ if (noSticky) {
360
+ this._buffer = this._buffer.slice(value.length);
361
+ } else {
362
+ index += value.length;
363
+ }
364
+ } else {
365
+ if (index < this._buffer.length) {
366
+ this._expect = 'numberExponent';
367
+ break;
368
+ }
369
+ if (this._done) {
370
+ this._expect = expected[this._parent];
371
+ break;
372
+ }
373
+ break main; // wait for more input
374
+ }
375
+ break;
376
+ case 'numberExponent': // [eE]?
377
+ patterns.numberExponent.lastIndex = index;
378
+ match = patterns.numberExponent.exec(this._buffer);
379
+ if (!match) {
380
+ if (index < this._buffer.length) {
381
+ this._expect = expected[this._parent];
382
+ break;
383
+ }
384
+ if (this._done) {
385
+ this._expect = 'done';
386
+ break;
387
+ }
388
+ break main; // wait for more input
389
+ }
390
+ value = match[0];
391
+ this._expect = 'numberExpSign';
392
+ this._updatePos(value);
393
+ if (noSticky) {
394
+ this._buffer = this._buffer.slice(value.length);
395
+ } else {
396
+ index += value.length;
397
+ }
398
+ break;
399
+ case 'numberExpSign': // [-+]?
400
+ patterns.numberExpSign.lastIndex = index;
401
+ match = patterns.numberExpSign.exec(this._buffer);
402
+ if (!match) {
403
+ if (index < this._buffer.length) {
404
+ this._expect = 'numberExpStart';
405
+ break;
406
+ }
407
+ if (this._done) return callback(this._makeError('Verifier has expected an exponent value of a number'));
408
+ break main; // wait for more input
409
+ }
410
+ value = match[0];
411
+ this._expect = 'numberExpStart';
412
+ this._updatePos(value);
413
+ if (noSticky) {
414
+ this._buffer = this._buffer.slice(value.length);
415
+ } else {
416
+ index += value.length;
417
+ }
418
+ break;
419
+ case 'numberExpStart': // [0-9]
420
+ patterns.numberExpStart.lastIndex = index;
421
+ match = patterns.numberExpStart.exec(this._buffer);
422
+ if (!match) {
423
+ if (index < this._buffer.length || this._done)
424
+ return callback(this._makeError('Verifier cannot parse input: expected an exponent part of a number'));
425
+ break main; // wait for more input
426
+ }
427
+ value = match[0];
428
+ this._expect = 'numberExpDigit';
429
+ this._updatePos(value);
430
+ if (noSticky) {
431
+ this._buffer = this._buffer.slice(value.length);
432
+ } else {
433
+ index += value.length;
434
+ }
435
+ break;
436
+ case 'numberExpDigit': // [0-9]*
437
+ patterns.numberExpDigit.lastIndex = index;
438
+ match = patterns.numberExpDigit.exec(this._buffer);
439
+ value = match[0];
440
+ if (value) {
441
+ this._updatePos(value);
442
+ if (noSticky) {
443
+ this._buffer = this._buffer.slice(value.length);
444
+ } else {
445
+ index += value.length;
446
+ }
447
+ } else {
448
+ if (index < this._buffer.length || this._done) {
449
+ this._expect = expected[this._parent];
450
+ break;
451
+ }
452
+ break main; // wait for more input
453
+ }
454
+ break;
455
+ case 'done':
456
+ patterns.ws.lastIndex = index;
457
+ match = patterns.ws.exec(this._buffer);
458
+ if (!match) {
459
+ if (index < this._buffer.length) {
460
+ if (this._jsonStreaming) {
461
+ this._expect = 'value';
462
+ break;
463
+ }
464
+ return callback(this._makeError('Verifier cannot parse input: unexpected characters'));
465
+ }
466
+ break main; // wait for more input
467
+ }
468
+ value = match[0];
469
+ this._updatePos(value);
470
+ if (noSticky) {
471
+ this._buffer = this._buffer.slice(value.length);
472
+ } else {
473
+ index += value.length;
474
+ }
475
+ break;
476
+ }
477
+ }
478
+ !noSticky && (this._buffer = this._buffer.slice(index));
479
+ callback(null);
480
+ }
481
+ }
482
+ Verifier.verifier = Verifier.make;
483
+ Verifier.make.Constructor = Verifier;
484
+
485
+ module.exports = Verifier;
package/utils/emit.js ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const emit = stream => stream.on('data', item => stream.emit(item.name, item.value));
4
+
5
+ module.exports = emit;
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ const {chain} = require('stream-chain');
4
+
5
+ const Parser = require('../Parser');
6
+
7
+ const withParser = (fn, options) =>
8
+ chain([new Parser(options), fn(options)], Object.assign({}, options, {writableObjectMode: false, readableObjectMode: true}));
9
+
10
+ module.exports = withParser;