json-stream-lite 1.3.1 → 1.3.3

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/dist/parser.js CHANGED
@@ -1,6 +1,31 @@
1
1
  import { ByteBuffer } from './byte-buffer.js';
2
- import { jsonStreamStringify, jsonStreamStringifyAsync, jsonStreamStringifyBytes, jsonStreamStringifyBytesAsync, } from './stringify.js';
2
+ import { jsonStreamStringify, jsonStreamStringifyAsync, jsonStreamStringifyBytes, jsonStreamStringifyBytesAsync, jsonLinesStringify, jsonLinesStringifyAsync, jsonLinesStringifyBytes, jsonLinesStringifyBytesAsync, } from './stringify.js';
3
3
  import { bytesToNumber, bytesToString } from './utils.js';
4
+ /**
5
+ * Sets an own, enumerable property. Plain assignment would treat a member
6
+ * named `__proto__` as the prototype setter instead of creating a property.
7
+ */
8
+ function setOwnProperty(obj, key, value) {
9
+ if (key === '__proto__') {
10
+ Object.defineProperty(obj, key, {
11
+ value,
12
+ writable: true,
13
+ enumerable: true,
14
+ configurable: true,
15
+ });
16
+ }
17
+ else {
18
+ ;
19
+ obj[key] = value;
20
+ }
21
+ }
22
+ /**
23
+ * Chunk size used when JsonString.readAsync() streams a string. When more
24
+ * input is needed, at most this much of the string is parsed again.
25
+ */
26
+ const ASYNC_READ_CHUNK_SIZE = 16 * 1024;
27
+ /** Marks the end of a container's items in the async read loops below. */
28
+ const END = Symbol('end');
4
29
  /**
5
30
  * Map of character names to their byte values for JSON parsing.
6
31
  * Contains commonly used characters in JSON syntax.
@@ -267,6 +292,58 @@ export class JsonEntity {
267
292
  this.buffer.next();
268
293
  }
269
294
  }
295
+ /**
296
+ * If only whitespace and comments stand between the current position and
297
+ * `close` (a closing bracket), consumes up to and including `close`.
298
+ * Otherwise consumes nothing, so comments before a value stay available
299
+ * through its `preComments`.
300
+ *
301
+ * This lets a collection end after a trailing comma (JSONC) or a comment.
302
+ *
303
+ * @param close - The closing bracket byte
304
+ * @returns True if the closing bracket was consumed
305
+ */
306
+ consumeClosing(close) {
307
+ let offset = 0;
308
+ while (true) {
309
+ const byte = this.buffer.peek(offset);
310
+ if (isWhitespace(byte)) {
311
+ offset++;
312
+ continue;
313
+ }
314
+ if (byte === BYTE_MAP.slash) {
315
+ const next = this.buffer.peek(offset + 1);
316
+ if (next === BYTE_MAP.slash) {
317
+ // Line comment: runs to the end of the line
318
+ offset += 2;
319
+ let b = this.buffer.peek(offset);
320
+ while (b !== null && b !== BYTE_MAP.lineFeed) {
321
+ b = this.buffer.peek(++offset);
322
+ }
323
+ continue;
324
+ }
325
+ if (next === BYTE_MAP.asterisk) {
326
+ // Block comment: runs to the closing */
327
+ offset += 2;
328
+ while (!(this.buffer.peek(offset) === BYTE_MAP.asterisk &&
329
+ this.buffer.peek(offset + 1) === BYTE_MAP.slash)) {
330
+ if (this.buffer.peek(offset) === null)
331
+ return false;
332
+ offset++;
333
+ }
334
+ offset += 2;
335
+ continue;
336
+ }
337
+ }
338
+ if (byte !== close) {
339
+ return false;
340
+ }
341
+ for (let i = 0; i <= offset; i++) {
342
+ this.buffer.next();
343
+ }
344
+ return true;
345
+ }
346
+ }
270
347
  /**
271
348
  * Reads and parses the entity, consuming it in the process.
272
349
  *
@@ -320,6 +397,29 @@ export class JsonEntity {
320
397
  await this.readAsync();
321
398
  }
322
399
  }
400
+ /**
401
+ * Advances a restartable item generator (such as `items()` or
402
+ * `members()`), reading more input until the next item has started.
403
+ *
404
+ * @param generator - The generator to advance
405
+ * @param restart - Creates a fresh generator after a failed attempt; the
406
+ * buffer has been rewound to the same item boundary
407
+ * @returns The generator's next result
408
+ */
409
+ async nextAsync(generator, restart) {
410
+ while (true) {
411
+ const next = this.tryParse(() => generator.next());
412
+ if (next !== undefined) {
413
+ return next;
414
+ }
415
+ if (this.buffer.atEof()) {
416
+ // Let the synchronous parser raise the usual error
417
+ return restart().next();
418
+ }
419
+ await this.buffer.readStreamAsync();
420
+ generator = restart();
421
+ }
422
+ }
323
423
  /**
324
424
  * Attempts to parse by executing a callback, reverting buffer state on failure.
325
425
  *
@@ -449,6 +549,7 @@ export class JsonComment extends JsonEntity {
449
549
  }
450
550
  }
451
551
  export class JsonString extends JsonEntity {
552
+ midString = false;
452
553
  /**
453
554
  * Parses a JSON string from the buffer.
454
555
  *
@@ -467,7 +568,9 @@ export class JsonString extends JsonEntity {
467
568
  * @yields Uint8Array chunks containing the unescaped string bytes
468
569
  */
469
570
  *streamBytes(chunkSize = 1024) {
470
- if (this.buffer.peek() === BYTE_MAP.quotes) {
571
+ // streamBytesAsync restarts this generator mid-string once a chunk has
572
+ // been yielded; from then on a quote is the closing one.
573
+ if (!this.midString && this.buffer.peek() === BYTE_MAP.quotes) {
471
574
  this.buffer.next(); // consume quotes
472
575
  }
473
576
  const bytes = [];
@@ -503,6 +606,10 @@ export class JsonString extends JsonEntity {
503
606
  bytes.push(BYTE_MAP.tab); // tab
504
607
  this.buffer.next(); // consume escaped character
505
608
  break;
609
+ case BYTE_MAP.slash:
610
+ bytes.push(BYTE_MAP.slash); // solidus
611
+ this.buffer.next(); // consume escaped character
612
+ break;
506
613
  case BYTE_MAP.u:
507
614
  // Unicode escape sequence \uXXXX
508
615
  this.buffer.next(); // consume 'u'
@@ -520,8 +627,18 @@ export class JsonString extends JsonEntity {
520
627
  bytes.push(charCode);
521
628
  }
522
629
  else {
630
+ // A high surrogate followed by a \u low
631
+ // surrogate escape is one character outside
632
+ // the BMP (e.g. "\ud83d\ude00"); decoding
633
+ // the halves separately gives two U+FFFD.
634
+ const low = this.peekLowSurrogateEscape(charCode);
635
+ let char = String.fromCharCode(charCode);
636
+ if (low !== undefined) {
637
+ for (let i = 0; i < 6; i++)
638
+ this.buffer.next();
639
+ char = String.fromCharCode(charCode, low);
640
+ }
523
641
  // UTF-8 encode the character
524
- const char = String.fromCharCode(charCode);
525
642
  const encoded = new TextEncoder().encode(char);
526
643
  bytes.push(...encoded);
527
644
  }
@@ -542,14 +659,42 @@ export class JsonString extends JsonEntity {
542
659
  yield result;
543
660
  }
544
661
  }
545
- if (this.buffer.peek() === BYTE_MAP.quotes) {
546
- this.buffer.next(); // consume quotes
662
+ if (this.buffer.peek() !== BYTE_MAP.quotes) {
663
+ throw new Error('Unterminated string in JSON input.');
547
664
  }
665
+ this.buffer.next(); // consume quotes
666
+ // The whole string (including the closing quote) has been read, so
667
+ // containers must not try to consume it a second time.
668
+ this.consumed = true;
548
669
  if (bytes.length > 0) {
549
670
  const result = new Uint8Array(bytes);
550
671
  yield result;
551
672
  }
552
673
  }
674
+ /**
675
+ * If `charCode` is a high surrogate and the next six bytes are a `\uXXXX`
676
+ * escape for a low surrogate, returns that low surrogate without
677
+ * consuming anything.
678
+ */
679
+ peekLowSurrogateEscape(charCode) {
680
+ if (charCode < 0xd800 || charCode > 0xdbff)
681
+ return undefined;
682
+ if (this.buffer.peek() !== BYTE_MAP.backslash ||
683
+ this.buffer.peek(1) !== BYTE_MAP.u) {
684
+ return undefined;
685
+ }
686
+ let hex = '';
687
+ for (let i = 2; i < 6; i++) {
688
+ const byte = this.buffer.peek(i);
689
+ if (byte === null)
690
+ return undefined;
691
+ hex += String.fromCharCode(byte);
692
+ }
693
+ if (!/^[0-9a-fA-F]{4}$/.test(hex))
694
+ return undefined;
695
+ const low = parseInt(hex, 16);
696
+ return low >= 0xdc00 && low <= 0xdfff ? low : undefined;
697
+ }
553
698
  /**
554
699
  * Async generator that yields the string content as raw byte chunks from a stream.
555
700
  * Handles escape sequences by converting them to their actual byte values.
@@ -570,10 +715,50 @@ export class JsonString extends JsonEntity {
570
715
  if (next.done) {
571
716
  return;
572
717
  }
718
+ this.midString = true;
573
719
  yield next.value;
574
720
  }
575
721
  }
576
722
  }
723
+ /**
724
+ * Asynchronously reads the string from a stream.
725
+ *
726
+ * Reads through {@link streamBytesAsync}, which resumes mid-string when
727
+ * more input is needed, so a long string split across many chunks is
728
+ * not re-parsed from its opening quote on every refill.
729
+ *
730
+ * @returns A promise that resolves to the parsed string value
731
+ */
732
+ async readAsync() {
733
+ if (this.consumed) {
734
+ throw new Error('JSON entity has already been consumed.');
735
+ }
736
+ // Fast path: the whole string is already buffered
737
+ const whole = this.tryParse(() => this.read());
738
+ if (whole !== undefined) {
739
+ return whole;
740
+ }
741
+ for await (const _ of this.preCommentsAsync) {
742
+ }
743
+ const chunks = [];
744
+ let length = 0;
745
+ for await (const chunk of this.streamBytesAsync(ASYNC_READ_CHUNK_SIZE)) {
746
+ chunks.push(chunk);
747
+ length += chunk.length;
748
+ }
749
+ if (!this.consumed) {
750
+ // The input ended before the string started: let the synchronous
751
+ // parser raise the usual error.
752
+ return this.read();
753
+ }
754
+ const bytes = new Uint8Array(length);
755
+ let offset = 0;
756
+ for (const chunk of chunks) {
757
+ bytes.set(chunk, offset);
758
+ offset += chunk.length;
759
+ }
760
+ return bytesToString(bytes);
761
+ }
577
762
  /**
578
763
  * Generator that yields the string content as chunks of strings.
579
764
  *
@@ -581,9 +766,17 @@ export class JsonString extends JsonEntity {
581
766
  * @yields Chunks of the string content
582
767
  */
583
768
  *stream(chunkSize = 1024) {
769
+ // A streaming decoder buffers incomplete multi-byte sequences between
770
+ // chunks, so characters split across chunk boundaries decode correctly.
771
+ const decoder = new TextDecoder();
584
772
  for (const chunk of this.streamBytes(chunkSize)) {
585
- yield bytesToString(chunk);
773
+ const text = decoder.decode(chunk, { stream: true });
774
+ if (text)
775
+ yield text;
586
776
  }
777
+ const rest = decoder.decode();
778
+ if (rest)
779
+ yield rest;
587
780
  }
588
781
  /**
589
782
  * Async generator that yields the string content as chunks of strings.
@@ -591,9 +784,15 @@ export class JsonString extends JsonEntity {
591
784
  * @yields Chunks of the string content
592
785
  */
593
786
  async *streamAsync(chunkSize = 1024) {
787
+ const decoder = new TextDecoder();
594
788
  for await (const chunk of this.streamBytesAsync(chunkSize)) {
595
- yield bytesToString(chunk);
789
+ const text = decoder.decode(chunk, { stream: true });
790
+ if (text)
791
+ yield text;
596
792
  }
793
+ const rest = decoder.decode();
794
+ if (rest)
795
+ yield rest;
597
796
  }
598
797
  /**
599
798
  * Returns an iterator for string chunks.
@@ -842,6 +1041,14 @@ export class JsonObject extends JsonEntity {
842
1041
  this.buffer.next(); // consume ,
843
1042
  this.skipWhitespace();
844
1043
  }
1044
+ // A trailing comma (JSONC) or a comment before the closing brace
1045
+ if (this.consumeClosing(BYTE_MAP.rightBrace)) {
1046
+ this.consumed = true;
1047
+ return;
1048
+ }
1049
+ if (this.buffer.peek() === null) {
1050
+ throw new Error('Unterminated object in JSON input.');
1051
+ }
845
1052
  const key = new JsonString(this.buffer);
846
1053
  const value = new JsonValue(this.buffer, key);
847
1054
  yield { key, value };
@@ -882,6 +1089,55 @@ export class JsonObject extends JsonEntity {
882
1089
  await this.buffer.readStreamAsync();
883
1090
  }
884
1091
  }
1092
+ /**
1093
+ * Asynchronously reads the whole object from a stream.
1094
+ *
1095
+ * Reads member by member through {@link membersAsync}, so when more
1096
+ * input is needed only the current member is parsed again, not the
1097
+ * whole object.
1098
+ *
1099
+ * @returns A promise that resolves to the parsed object
1100
+ */
1101
+ async readAsync() {
1102
+ if (this.consumed) {
1103
+ throw new Error('JSON entity has already been consumed.');
1104
+ }
1105
+ for await (const _ of this.preCommentsAsync) {
1106
+ }
1107
+ const obj = {};
1108
+ let members = this.members();
1109
+ while (true) {
1110
+ // Fast path: read a whole member synchronously if it is buffered
1111
+ const member = this.tryParse(() => {
1112
+ const next = members.next();
1113
+ if (next.done)
1114
+ return END;
1115
+ return {
1116
+ key: next.value.key.read(),
1117
+ value: next.value.value.readValue(),
1118
+ };
1119
+ });
1120
+ if (member === END)
1121
+ break;
1122
+ if (member !== undefined) {
1123
+ setOwnProperty(obj, member.key, member.value);
1124
+ continue;
1125
+ }
1126
+ // Not buffered yet: restart at this member and read it
1127
+ // incrementally, so only the unfinished part waits for input.
1128
+ members = this.members();
1129
+ const next = await this.nextAsync(members, () => {
1130
+ members = this.members();
1131
+ return members;
1132
+ });
1133
+ if (next.done)
1134
+ break;
1135
+ const keyString = await next.value.key.readAsync();
1136
+ setOwnProperty(obj, keyString, await next.value.value.readValueAsync());
1137
+ }
1138
+ this.consumed = true;
1139
+ return obj;
1140
+ }
885
1141
  /**
886
1142
  * Returns an iterator for object members.
887
1143
  * Enables use of for...of loops on JsonObject.
@@ -905,7 +1161,7 @@ export class JsonObject extends JsonEntity {
905
1161
  const obj = {};
906
1162
  for (const { key, value } of this.members()) {
907
1163
  const keyString = key.read();
908
- obj[keyString] = value.readValue();
1164
+ setOwnProperty(obj, keyString, value.readValue());
909
1165
  }
910
1166
  return obj;
911
1167
  }
@@ -939,6 +1195,11 @@ export class JsonArray extends JsonEntity {
939
1195
  this.buffer.next(); // consume ,
940
1196
  this.skipWhitespace();
941
1197
  }
1198
+ // A trailing comma (JSONC) or a comment before the closing bracket
1199
+ if (this.consumeClosing(BYTE_MAP.rightSquare)) {
1200
+ this.consumed = true;
1201
+ return;
1202
+ }
942
1203
  const valueObject = new JsonValue(this.buffer);
943
1204
  yield valueObject;
944
1205
  valueObject.consume();
@@ -971,6 +1232,48 @@ export class JsonArray extends JsonEntity {
971
1232
  await this.buffer.readStreamAsync();
972
1233
  }
973
1234
  }
1235
+ /**
1236
+ * Asynchronously reads the whole array from a stream.
1237
+ *
1238
+ * Reads item by item through {@link itemsAsync}, so when more input is
1239
+ * needed only the current item is parsed again, not the whole array.
1240
+ *
1241
+ * @returns A promise that resolves to the parsed array
1242
+ */
1243
+ async readAsync() {
1244
+ if (this.consumed) {
1245
+ throw new Error('JSON entity has already been consumed.');
1246
+ }
1247
+ for await (const _ of this.preCommentsAsync) {
1248
+ }
1249
+ const values = [];
1250
+ let items = this.items();
1251
+ while (true) {
1252
+ // Fast path: read a whole item synchronously if it is buffered
1253
+ const item = this.tryParse(() => {
1254
+ const next = items.next();
1255
+ return next.done ? END : { value: next.value.readValue() };
1256
+ });
1257
+ if (item === END)
1258
+ break;
1259
+ if (item !== undefined) {
1260
+ values.push(item.value);
1261
+ continue;
1262
+ }
1263
+ // Not buffered yet: restart at this item and read it
1264
+ // incrementally, so only the unfinished part waits for input.
1265
+ items = this.items();
1266
+ const next = await this.nextAsync(items, () => {
1267
+ items = this.items();
1268
+ return items;
1269
+ });
1270
+ if (next.done)
1271
+ break;
1272
+ values.push((await next.value.readValueAsync()));
1273
+ }
1274
+ this.consumed = true;
1275
+ return values;
1276
+ }
974
1277
  /**
975
1278
  * Returns an iterator for array items.
976
1279
  * Enables use of for...of loops on JsonArray.
@@ -998,6 +1301,204 @@ export class JsonArray extends JsonEntity {
998
1301
  return values;
999
1302
  }
1000
1303
  }
1304
+ /**
1305
+ * Represents a JSON Lines (JSONL / NDJSON) document: a sequence of JSON values
1306
+ * separated by newlines. Provides streaming access to each line's value.
1307
+ *
1308
+ * @typeParam T - The expected type of each line's value
1309
+ */
1310
+ export class JsonLines extends JsonEntity {
1311
+ valuesRead = 0;
1312
+ /**
1313
+ * When false (default), blank lines are silently ignored and several
1314
+ * values may share a line. When true, the input must follow the JSON Lines
1315
+ * format exactly: one value per line, no blank lines, and nothing after the
1316
+ * final line terminator. Violations throw an Error.
1317
+ */
1318
+ strict = false;
1319
+ /**
1320
+ * Generator that yields one JsonValue per line. Blank lines are skipped
1321
+ * unless `strict` is set.
1322
+ *
1323
+ * @yields Each value entity in the document
1324
+ */
1325
+ *items() {
1326
+ while (true) {
1327
+ let newlines = 0;
1328
+ while (isWhitespace(this.buffer.peek())) {
1329
+ if (this.buffer.next() === BYTE_MAP.lineFeed)
1330
+ newlines++;
1331
+ }
1332
+ const atEnd = this.buffer.peek() === null;
1333
+ if (this.strict) {
1334
+ // Exactly one terminator between values, none before the
1335
+ // first, and at most one after the last.
1336
+ const expected = this.valuesRead === 0 ? 0 : 1;
1337
+ if (newlines > expected) {
1338
+ throw new Error(`Blank line in JSON Lines input after value ${this.valuesRead}.`);
1339
+ }
1340
+ if (!atEnd && newlines < expected) {
1341
+ throw new Error(`Multiple values on one line in JSON Lines input after value ${this.valuesRead}.`);
1342
+ }
1343
+ }
1344
+ if (atEnd) {
1345
+ this.consumed = true;
1346
+ return;
1347
+ }
1348
+ const valueObject = new JsonValue(this.buffer);
1349
+ yield valueObject;
1350
+ valueObject.consume();
1351
+ this.valuesRead++;
1352
+ }
1353
+ }
1354
+ /**
1355
+ * Async generator that yields one JsonValue per line from a stream.
1356
+ *
1357
+ * @yields Each value entity in the document
1358
+ */
1359
+ async *itemsAsync() {
1360
+ while (!this.buffer.atEof()) {
1361
+ const itemGen = this.items();
1362
+ while (true) {
1363
+ const current = this.tryParse(() => itemGen.next());
1364
+ if (current === undefined) {
1365
+ break; // Need more data
1366
+ }
1367
+ if (current.done) {
1368
+ return; // No more lines
1369
+ }
1370
+ yield current.value;
1371
+ await current.value.consumeAsync();
1372
+ }
1373
+ await this.buffer.readStreamAsync();
1374
+ }
1375
+ }
1376
+ /**
1377
+ * Enables use of for...of loops on JsonLines.
1378
+ */
1379
+ [Symbol.iterator]() {
1380
+ return this.items();
1381
+ }
1382
+ /**
1383
+ * Enables use of for await...of loops on JsonLines.
1384
+ */
1385
+ [Symbol.asyncIterator]() {
1386
+ return this.itemsAsync();
1387
+ }
1388
+ /**
1389
+ * Parses the entire document into an array of values.
1390
+ *
1391
+ * @returns The parsed values, one per line
1392
+ */
1393
+ parse() {
1394
+ const values = [];
1395
+ for (const value of this) {
1396
+ values.push(value.readValue());
1397
+ }
1398
+ return values;
1399
+ }
1400
+ /**
1401
+ * Serializes values as JSON Lines (JSONL / NDJSON) using json-stream-lite.
1402
+ * See `jsonLinesStringify` for more details.
1403
+ * @param indent - Ignored: JSON Lines output is never indented
1404
+ * @returns A generator yielding JSON Lines string chunks
1405
+ */
1406
+ static stringify(values, replacer, indent = 0, options) {
1407
+ return jsonLinesStringify(values, replacer, options);
1408
+ }
1409
+ /**
1410
+ * Serializes values as JSON Lines as Uint8Array byte chunks using json-stream-lite.
1411
+ * See `jsonLinesStringifyBytes` for more details.
1412
+ * @param indent - Ignored: JSON Lines output is never indented
1413
+ * @returns A generator yielding JSON Lines byte chunks
1414
+ */
1415
+ static stringifyBytes(values, replacer, indent = 0, options) {
1416
+ return jsonLinesStringifyBytes(values, replacer, options);
1417
+ }
1418
+ /**
1419
+ * Asynchronously serializes values as JSON Lines using json-stream-lite.
1420
+ * See `jsonLinesStringifyAsync` for more details.
1421
+ * @param indent - Ignored: JSON Lines output is never indented
1422
+ * @returns An async generator yielding JSON Lines string chunks
1423
+ * */
1424
+ static stringifyAsync(values, replacer, indent = 0, options) {
1425
+ return jsonLinesStringifyAsync(values, replacer, options);
1426
+ }
1427
+ /**
1428
+ * Asynchronously serializes values as JSON Lines as Uint8Array byte chunks using json-stream-lite.
1429
+ * See `jsonLinesStringifyBytesAsync` for more details.
1430
+ * @param indent - Ignored: JSON Lines output is never indented
1431
+ * @returns An async generator yielding JSON Lines byte chunks
1432
+ * */
1433
+ static stringifyBytesAsync(values, replacer, indent = 0, options) {
1434
+ return jsonLinesStringifyBytesAsync(values, replacer, options);
1435
+ }
1436
+ /**
1437
+ * Asynchronously reads the entire document into an array of values.
1438
+ *
1439
+ * Reads line by line: a value that is fully buffered is read
1440
+ * synchronously, one that is not is read incrementally, so a refill
1441
+ * re-parses at most the unfinished value and the buffer stays near
1442
+ * `maxBufferSize`. Retrying the whole document instead would re-run the
1443
+ * strict-mode checks with a stale line count.
1444
+ *
1445
+ * @returns The parsed values, one per line
1446
+ */
1447
+ async readAsync() {
1448
+ if (this.consumed) {
1449
+ throw new Error('JSON entity has already been consumed.');
1450
+ }
1451
+ // Unlike JsonArray, leading whitespace is left to items(): skipping
1452
+ // it here would hide a blank first line from strict mode.
1453
+ const values = [];
1454
+ let items = this.items();
1455
+ while (true) {
1456
+ // Fast path: read a whole value synchronously if it is buffered
1457
+ const item = this.tryParse(() => {
1458
+ const next = items.next();
1459
+ return next.done ? END : { value: next.value.readValue() };
1460
+ });
1461
+ if (item === END)
1462
+ break;
1463
+ if (item !== undefined) {
1464
+ values.push(item.value);
1465
+ continue;
1466
+ }
1467
+ // Not buffered yet: restart at this value and read it
1468
+ // incrementally, so only the unfinished part waits for input.
1469
+ items = this.items();
1470
+ const next = await this.nextAsync(items, () => {
1471
+ items = this.items();
1472
+ return items;
1473
+ });
1474
+ if (next.done)
1475
+ break;
1476
+ values.push((await next.value.readValueAsync()));
1477
+ }
1478
+ this.consumed = true;
1479
+ return values;
1480
+ }
1481
+ /**
1482
+ * Reads the entire document into an array of values.
1483
+ *
1484
+ * @returns The parsed values, one per line
1485
+ */
1486
+ read() {
1487
+ if (this.strict && this.valuesRead === 0) {
1488
+ // The base read() skips leading whitespace, which would hide a
1489
+ // blank first line from items().
1490
+ let ahead = 0;
1491
+ let byte = this.buffer.peek(ahead);
1492
+ while (isWhitespace(byte)) {
1493
+ if (byte === BYTE_MAP.lineFeed) {
1494
+ throw new Error('Blank line in JSON Lines input after value 0.');
1495
+ }
1496
+ byte = this.buffer.peek(++ahead);
1497
+ }
1498
+ }
1499
+ return super.read();
1500
+ }
1501
+ }
1001
1502
  /**
1002
1503
  * Parser that flattens nested JSON structures into key-value pairs.
1003
1504
  * Useful for extracting specific values from complex JSON without parsing the entire structure.
@@ -56,3 +56,29 @@ export declare function jsonStreamStringify(value: unknown, replacer?: any, inde
56
56
  export declare function jsonStreamStringifyBytes(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
57
57
  export declare function jsonStreamStringifyAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<string>;
58
58
  export declare function jsonStreamStringifyBytesAsync(value: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): AsyncGenerator<Uint8Array>;
59
+ /**
60
+ * Serializes values as JSON Lines (JSONL / NDJSON): one compact JSON value per
61
+ * line, each terminated by a newline. Values with no JSON representation
62
+ * (undefined, functions) are written as `null`. The `jsonc` option is ignored.
63
+ *
64
+ * @param values - The values to serialize, one per line
65
+ * @param replacer - Optional replacer function or array
66
+ * @param options - Additional stringification options
67
+ * @returns Generator yielding string chunks
68
+ */
69
+ export declare function jsonLinesStringify(values: Iterable<unknown>, replacer?: any, options?: JsonStreamStringifyOptions): Generator<string>;
70
+ /**
71
+ * Serializes values as JSON Lines, yielding Uint8Array chunks.
72
+ * See {@link jsonLinesStringify}.
73
+ */
74
+ export declare function jsonLinesStringifyBytes(values: Iterable<unknown>, replacer?: any, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
75
+ /**
76
+ * Asynchronously serializes values (sync or async iterable) as JSON Lines.
77
+ * See {@link jsonLinesStringify}.
78
+ */
79
+ export declare function jsonLinesStringifyAsync(values: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, options?: JsonStreamStringifyOptions): AsyncGenerator<string>;
80
+ /**
81
+ * Asynchronously serializes values as JSON Lines, yielding Uint8Array chunks.
82
+ * See {@link jsonLinesStringify}.
83
+ */
84
+ export declare function jsonLinesStringifyBytesAsync(values: AsyncIterable<unknown> | Iterable<unknown>, replacer?: any, options?: JsonStreamStringifyOptions): AsyncGenerator<Uint8Array>;