json-stream-lite 1.0.12 → 1.0.14

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.
@@ -37,10 +37,15 @@ export declare class ByteBuffer {
37
37
  */
38
38
  constructor(asyncIterable?: ByteStream);
39
39
  /**
40
- * Reads data from the async stream into the buffer.
40
+ * Reads data from the stream into the buffer.
41
41
  * Reads up to maxBufferSize bytes at a time.
42
42
  */
43
- readStream(): Promise<void>;
43
+ readStream(): boolean;
44
+ /**
45
+ * Reads data from the sync or async stream into the buffer.
46
+ * Reads up to maxBufferSize bytes at a time.
47
+ */
48
+ readStreamAsync(): Promise<void>;
44
49
  /**
45
50
  * Gets the current length of the buffer.
46
51
  *
@@ -1,8 +1,4 @@
1
- import { bytesToString } from './utils.js';
2
- /**
3
- * Shared TextEncoder instance for converting strings to UTF-8 bytes.
4
- */
5
- const textEncoder = new TextEncoder();
1
+ import { bytesToString, stringToBytes } from './utils.js';
6
2
  /**
7
3
  * Default maximum buffer size before compaction
8
4
  */
@@ -47,16 +43,41 @@ export class ByteBuffer {
47
43
  this.asyncIterable = asyncIterable;
48
44
  }
49
45
  /**
50
- * Reads data from the async stream into the buffer.
46
+ * Reads data from the stream into the buffer.
47
+ * Reads up to maxBufferSize bytes at a time.
48
+ */
49
+ readStream() {
50
+ if (!this.asyncIterable || !(Symbol.iterator in this.asyncIterable)) {
51
+ return false;
52
+ }
53
+ let i = 0;
54
+ const iterator = this.asyncIterable[Symbol.iterator]();
55
+ while (i < this.maxBufferSize) {
56
+ const nextByte = iterator.next();
57
+ if (nextByte.done) {
58
+ this.eof = true;
59
+ return false;
60
+ }
61
+ const value = nextByte.value;
62
+ this.feed(value);
63
+ i++;
64
+ }
65
+ return true;
66
+ }
67
+ /**
68
+ * Reads data from the sync or async stream into the buffer.
51
69
  * Reads up to maxBufferSize bytes at a time.
52
70
  */
53
- async readStream() {
54
- if (!this.asyncIterable) {
71
+ async readStreamAsync() {
72
+ this.readStream();
73
+ if (!this.asyncIterable ||
74
+ !(Symbol.asyncIterator in this.asyncIterable)) {
55
75
  return;
56
76
  }
57
77
  let i = 0;
78
+ const iterator = this.asyncIterable[Symbol.asyncIterator]();
58
79
  while (i < this.maxBufferSize) {
59
- const nextByte = await this.asyncIterable[Symbol.asyncIterator]().next();
80
+ const nextByte = await iterator.next();
60
81
  if (nextByte.done) {
61
82
  this.eof = true;
62
83
  break;
@@ -94,7 +115,7 @@ export class ByteBuffer {
94
115
  continue;
95
116
  }
96
117
  else if (typeof item === 'string') {
97
- const encoded = textEncoder.encode(item);
118
+ const encoded = stringToBytes(item);
98
119
  for (const byte of encoded) {
99
120
  this.buffer.push(byte);
100
121
  }
@@ -122,7 +143,12 @@ export class ByteBuffer {
122
143
  const index = this.bufferIndex + ahead;
123
144
  if (index >= this.buffer.length) {
124
145
  if (!this.eof) {
125
- throw new NoMoreTokensError('Buffer empty');
146
+ if (!this.readStream()) {
147
+ throw new NoMoreTokensError('No more items available');
148
+ }
149
+ else {
150
+ return this.peek(ahead);
151
+ }
126
152
  }
127
153
  return null;
128
154
  }
@@ -138,7 +164,12 @@ export class ByteBuffer {
138
164
  next() {
139
165
  if (this.bufferIndex >= this.buffer.length) {
140
166
  if (!this.eof) {
141
- throw new NoMoreTokensError('No more items available');
167
+ if (!this.readStream()) {
168
+ throw new NoMoreTokensError('No more items available');
169
+ }
170
+ else {
171
+ return this.next();
172
+ }
142
173
  }
143
174
  throw new EofReachedError('End of file reached');
144
175
  }
package/dist/parser.js CHANGED
@@ -165,7 +165,7 @@ export class JsonEntity {
165
165
  throw new Error('JSON entity has already been consumed.');
166
166
  }
167
167
  while (!this.buffer.atEof()) {
168
- await this.buffer.readStream();
168
+ await this.buffer.readStreamAsync();
169
169
  const res = this.tryParse(() => this.read());
170
170
  if (res !== undefined) {
171
171
  return res;
@@ -479,7 +479,7 @@ export class JsonObject extends JsonEntity {
479
479
  */
480
480
  async *membersAsync() {
481
481
  while (!this.buffer.atEof()) {
482
- await this.buffer.readStream();
482
+ await this.buffer.readStreamAsync();
483
483
  const memberGen = this.members();
484
484
  let currentMember = undefined;
485
485
  while (true) {
@@ -570,7 +570,7 @@ export class JsonArray extends JsonEntity {
570
570
  */
571
571
  async *itemsAsync() {
572
572
  while (!this.buffer.atEof()) {
573
- await this.buffer.readStream();
573
+ await this.buffer.readStreamAsync();
574
574
  const itemGen = this.items();
575
575
  let currentItem = undefined;
576
576
  while (true) {
@@ -687,7 +687,7 @@ export class JsonKeyValueParser extends JsonEntity {
687
687
  */
688
688
  async *parseAsync() {
689
689
  while (!this.buffer.atEof()) {
690
- await this.buffer.readStream();
690
+ await this.buffer.readStreamAsync();
691
691
  const parserGen = this.parse();
692
692
  let currentPair = undefined;
693
693
  while (true) {
package/dist/stringify.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { stringToBytes } from './utils';
1
2
  /**
2
3
  * Formats a string value for JSON output, escaping special characters.
3
4
  * Yields the formatted string in chunks for memory efficiency.
@@ -184,10 +185,6 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
184
185
  export function jsonStreamStringify(value, replacer, indent = 0, options) {
185
186
  return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
186
187
  }
187
- /**
188
- * Shared TextEncoder instance for converting strings to UTF-8 bytes.
189
- */
190
- const TEXT_ENCODER = new TextEncoder();
191
188
  /**
192
189
  * Converts a JavaScript value to JSON format, yielding Uint8Array byte chunks.
193
190
  * Provides memory-efficient streaming stringification with binary output.
@@ -209,6 +206,6 @@ const TEXT_ENCODER = new TextEncoder();
209
206
  export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options) {
210
207
  const stringGenerator = jsonStreamStringify(value, replacer, indent, options);
211
208
  for (const chunk of stringGenerator) {
212
- yield TEXT_ENCODER.encode(chunk);
209
+ yield stringToBytes(chunk);
213
210
  }
214
211
  }
package/dist/types.d.ts CHANGED
@@ -6,7 +6,7 @@ export type JsonStreamInput = string | number | number[] | Uint8Array;
6
6
  * An async iterable stream of JSON input that can be consumed incrementally.
7
7
  * Supports strings, numbers, arrays of numbers, or Uint8Arrays as stream items.
8
8
  */
9
- export type ByteStream = AsyncIterable<JsonStreamInput>;
9
+ export type ByteStream = AsyncIterable<JsonStreamInput> | Iterable<JsonStreamInput>;
10
10
  /**
11
11
  * Union type representing all JSON primitive value types.
12
12
  */
package/dist/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ const textEncoder = new TextEncoder();
2
+ const textDecoder = new TextDecoder();
1
3
  /**
2
4
  * Converts a string to a Uint8Array of UTF-8 encoded bytes.
3
5
  *
@@ -5,7 +7,7 @@
5
7
  * @returns A Uint8Array containing the UTF-8 encoded bytes
6
8
  */
7
9
  export function stringToBytes(str) {
8
- return new TextEncoder().encode(str);
10
+ return textEncoder.encode(str);
9
11
  }
10
12
  /**
11
13
  * Converts a Uint8Array of UTF-8 encoded bytes to a string.
@@ -14,7 +16,7 @@ export function stringToBytes(str) {
14
16
  * @returns The decoded string
15
17
  */
16
18
  export function bytesToString(bytes) {
17
- return new TextDecoder().decode(bytes);
19
+ return textDecoder.decode(bytes);
18
20
  }
19
21
  /**
20
22
  * Converts a Uint8Array of UTF-8 encoded bytes to a number.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
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",