json-stream-lite 1.3.0 → 1.3.2

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.
@@ -20,8 +20,14 @@ export declare class ByteBuffer {
20
20
  protected outputOffset: number;
21
21
  /** Buffer holding input items */
22
22
  protected buffer: number[];
23
+ /** Whether the start of the input has been checked for a byte order mark */
24
+ private bomChecked;
23
25
  /** Optional async iterable input source */
24
26
  protected asyncIterable?: ByteStream;
27
+ /** Cached sync iterator */
28
+ private syncIterator?;
29
+ /** Cached async iterator */
30
+ private asyncIterator?;
25
31
  /**
26
32
  * Creates a new ByteBuffer instance.
27
33
  *
@@ -62,6 +68,14 @@ export declare class ByteBuffer {
62
68
  * @returns True if no more input is available
63
69
  */
64
70
  atEof(): boolean;
71
+ /**
72
+ * Drops a UTF-8 byte order mark (EF BB BF) from the very start of the
73
+ * input. RFC 8259 section 8.1 lets parsers ignore one.
74
+ *
75
+ * @throws NoMoreTokensError if the input might start with a byte order mark
76
+ * but too few bytes have arrived to tell
77
+ */
78
+ private skipByteOrderMark;
65
79
  /**
66
80
  * Peeks at an item in the buffer without consuming it.
67
81
  *
@@ -42,8 +42,14 @@ export class ByteBuffer {
42
42
  outputOffset = 0;
43
43
  /** Buffer holding input items */
44
44
  buffer = [];
45
+ /** Whether the start of the input has been checked for a byte order mark */
46
+ bomChecked = false;
45
47
  /** Optional async iterable input source */
46
48
  asyncIterable;
49
+ /** Cached sync iterator */
50
+ syncIterator;
51
+ /** Cached async iterator */
52
+ asyncIterator;
47
53
  /**
48
54
  * Creates a new ByteBuffer instance.
49
55
  *
@@ -67,10 +73,12 @@ export class ByteBuffer {
67
73
  if (!this.asyncIterable || !(Symbol.iterator in this.asyncIterable)) {
68
74
  return false;
69
75
  }
70
- const iterator = this.asyncIterable[Symbol.iterator]();
76
+ if (!this.syncIterator) {
77
+ this.syncIterator = this.asyncIterable[Symbol.iterator]();
78
+ }
71
79
  let processed = false;
72
80
  while (this.length < this.maxBufferSize || !processed) {
73
- const next = iterator.next();
81
+ const next = this.syncIterator.next();
74
82
  processed = true;
75
83
  if (next.done) {
76
84
  this.eof = true;
@@ -91,11 +99,13 @@ export class ByteBuffer {
91
99
  !(Symbol.asyncIterator in this.asyncIterable)) {
92
100
  return;
93
101
  }
94
- const iterator = this.asyncIterable[Symbol.asyncIterator]();
102
+ if (!this.asyncIterator) {
103
+ this.asyncIterator = this.asyncIterable[Symbol.asyncIterator]();
104
+ }
95
105
  let processed = false;
96
106
  while (this.length < this.maxBufferSize || !processed) {
97
107
  processed = true;
98
- const next = await iterator.next();
108
+ const next = await this.asyncIterator.next();
99
109
  if (next.done) {
100
110
  this.eof = true;
101
111
  return;
@@ -163,6 +173,35 @@ export class ByteBuffer {
163
173
  atEof() {
164
174
  return this.eof && this.atEnd();
165
175
  }
176
+ /**
177
+ * Drops a UTF-8 byte order mark (EF BB BF) from the very start of the
178
+ * input. RFC 8259 section 8.1 lets parsers ignore one.
179
+ *
180
+ * @throws NoMoreTokensError if the input might start with a byte order mark
181
+ * but too few bytes have arrived to tell
182
+ */
183
+ skipByteOrderMark() {
184
+ const BOM = [0xef, 0xbb, 0xbf];
185
+ while (this.buffer.length < BOM.length && !this.eof) {
186
+ if (!this.readStream())
187
+ break;
188
+ }
189
+ for (let i = 0; i < BOM.length; i++) {
190
+ if (i >= this.buffer.length) {
191
+ if (this.eof)
192
+ break;
193
+ throw new NoMoreTokensError('No more items available');
194
+ }
195
+ if (this.buffer[i] !== BOM[i]) {
196
+ this.bomChecked = true;
197
+ return;
198
+ }
199
+ }
200
+ if (this.buffer.length >= BOM.length) {
201
+ this.buffer.splice(0, BOM.length);
202
+ }
203
+ this.bomChecked = true;
204
+ }
166
205
  /**
167
206
  * Peeks at an item in the buffer without consuming it.
168
207
  *
@@ -171,6 +210,8 @@ export class ByteBuffer {
171
210
  * @throws NoMoreTokensError if more input is needed and EOF not signaled
172
211
  */
173
212
  peek(ahead = 0) {
213
+ if (!this.bomChecked)
214
+ this.skipByteOrderMark();
174
215
  const index = this.bufferIndex + ahead;
175
216
  if (index >= this.buffer.length) {
176
217
  if (!this.eof) {
@@ -193,8 +234,10 @@ export class ByteBuffer {
193
234
  * @throws EofReachedError if at EOF and no more items available
194
235
  */
195
236
  next() {
237
+ if (!this.bomChecked)
238
+ this.skipByteOrderMark();
196
239
  if (this.bufferIndex >= this.buffer.length) {
197
- if (this.readStream()) {
240
+ if (!this.eof && this.readStream()) {
198
241
  return this.next();
199
242
  }
200
243
  if (!this.eof) {
package/dist/parser.d.ts CHANGED
@@ -180,6 +180,7 @@ export declare class JsonComment extends JsonEntity<string> {
180
180
  get postComments(): Generator<JsonComment>;
181
181
  }
182
182
  export declare class JsonString<T extends string = string> extends JsonEntity<T> {
183
+ private midString;
183
184
  /**
184
185
  * Parses a JSON string from the buffer.
185
186
  *
package/dist/parser.js CHANGED
@@ -449,6 +449,7 @@ export class JsonComment extends JsonEntity {
449
449
  }
450
450
  }
451
451
  export class JsonString extends JsonEntity {
452
+ midString = false;
452
453
  /**
453
454
  * Parses a JSON string from the buffer.
454
455
  *
@@ -467,7 +468,9 @@ export class JsonString extends JsonEntity {
467
468
  * @yields Uint8Array chunks containing the unescaped string bytes
468
469
  */
469
470
  *streamBytes(chunkSize = 1024) {
470
- if (this.buffer.peek() === BYTE_MAP.quotes) {
471
+ // streamBytesAsync restarts this generator mid-string once a chunk has
472
+ // been yielded; from then on a quote is the closing one.
473
+ if (!this.midString && this.buffer.peek() === BYTE_MAP.quotes) {
471
474
  this.buffer.next(); // consume quotes
472
475
  }
473
476
  const bytes = [];
@@ -542,9 +545,13 @@ export class JsonString extends JsonEntity {
542
545
  yield result;
543
546
  }
544
547
  }
545
- if (this.buffer.peek() === BYTE_MAP.quotes) {
546
- this.buffer.next(); // consume quotes
548
+ if (this.buffer.peek() !== BYTE_MAP.quotes) {
549
+ throw new Error('Unterminated string in JSON input.');
547
550
  }
551
+ this.buffer.next(); // consume quotes
552
+ // The whole string (including the closing quote) has been read, so
553
+ // containers must not try to consume it a second time.
554
+ this.consumed = true;
548
555
  if (bytes.length > 0) {
549
556
  const result = new Uint8Array(bytes);
550
557
  yield result;
@@ -570,6 +577,7 @@ export class JsonString extends JsonEntity {
570
577
  if (next.done) {
571
578
  return;
572
579
  }
580
+ this.midString = true;
573
581
  yield next.value;
574
582
  }
575
583
  }
@@ -581,9 +589,17 @@ export class JsonString extends JsonEntity {
581
589
  * @yields Chunks of the string content
582
590
  */
583
591
  *stream(chunkSize = 1024) {
592
+ // A streaming decoder buffers incomplete multi-byte sequences between
593
+ // chunks, so characters split across chunk boundaries decode correctly.
594
+ const decoder = new TextDecoder();
584
595
  for (const chunk of this.streamBytes(chunkSize)) {
585
- yield bytesToString(chunk);
596
+ const text = decoder.decode(chunk, { stream: true });
597
+ if (text)
598
+ yield text;
586
599
  }
600
+ const rest = decoder.decode();
601
+ if (rest)
602
+ yield rest;
587
603
  }
588
604
  /**
589
605
  * Async generator that yields the string content as chunks of strings.
@@ -591,9 +607,15 @@ export class JsonString extends JsonEntity {
591
607
  * @yields Chunks of the string content
592
608
  */
593
609
  async *streamAsync(chunkSize = 1024) {
610
+ const decoder = new TextDecoder();
594
611
  for await (const chunk of this.streamBytesAsync(chunkSize)) {
595
- yield bytesToString(chunk);
612
+ const text = decoder.decode(chunk, { stream: true });
613
+ if (text)
614
+ yield text;
596
615
  }
616
+ const rest = decoder.decode();
617
+ if (rest)
618
+ yield rest;
597
619
  }
598
620
  /**
599
621
  * Returns an iterator for string chunks.
@@ -842,6 +864,9 @@ export class JsonObject extends JsonEntity {
842
864
  this.buffer.next(); // consume ,
843
865
  this.skipWhitespace();
844
866
  }
867
+ if (this.buffer.peek() === null) {
868
+ throw new Error('Unterminated object in JSON input.');
869
+ }
845
870
  const key = new JsonString(this.buffer);
846
871
  const value = new JsonValue(this.buffer, key);
847
872
  yield { key, value };
package/dist/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const textEncoder = new TextEncoder();
2
- const textDecoder = new TextDecoder();
2
+ // ignoreBOM keeps a leading U+FEFF in decoded text instead of dropping it.
3
+ const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
3
4
  /**
4
5
  * Converts a string to a Uint8Array of UTF-8 encoded bytes.
5
6
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "A lightweight, memory-efficient, zero-dependency streaming JSON parser and stringifier for JavaScript and TypeScript. It works in both Node.js and browser environments.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -17,9 +17,13 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "default": "./dist/index.js"
19
19
  },
20
+ "./*.js": {
21
+ "types": "./dist/*.d.ts",
22
+ "default": "./dist/*.js"
23
+ },
20
24
  "./*": {
21
25
  "types": "./dist/*.d.ts",
22
- "default": "./dist/*"
26
+ "default": "./dist/*.js"
23
27
  }
24
28
  },
25
29
  "keywords": [
@@ -46,12 +50,12 @@
46
50
  "author": "Jacob Shirley",
47
51
  "license": "MIT",
48
52
  "devDependencies": {
53
+ "@vitest/browser": "^5.0.0",
54
+ "@vitest/browser-playwright": "^5.0.0",
55
+ "@vitest/coverage-v8": "^5.0.0",
56
+ "playwright": "^1.60.0",
49
57
  "typescript": "^6.0.2",
50
- "vitest": "^4.0.14",
51
- "@vitest/browser": "^4.0.14",
52
- "@vitest/browser-playwright": "^4.0.14",
53
- "@vitest/coverage-v8": "^4.0.14",
54
- "playwright": "^1.57.0"
58
+ "vitest": "^5.0.0"
55
59
  },
56
60
  "files": [
57
61
  "dist",