json-stream-lite 1.0.12 → 1.0.13

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
  *
@@ -47,16 +47,41 @@ export class ByteBuffer {
47
47
  this.asyncIterable = asyncIterable;
48
48
  }
49
49
  /**
50
- * Reads data from the async stream into the buffer.
50
+ * Reads data from the stream into the buffer.
51
51
  * Reads up to maxBufferSize bytes at a time.
52
52
  */
53
- async readStream() {
54
- if (!this.asyncIterable) {
53
+ readStream() {
54
+ if (!this.asyncIterable || !(Symbol.iterator in this.asyncIterable)) {
55
+ return false;
56
+ }
57
+ let i = 0;
58
+ const iterator = this.asyncIterable[Symbol.iterator]();
59
+ while (i < this.maxBufferSize) {
60
+ const nextByte = iterator.next();
61
+ if (nextByte.done) {
62
+ this.eof = true;
63
+ return false;
64
+ }
65
+ const value = nextByte.value;
66
+ this.feed(value);
67
+ i++;
68
+ }
69
+ return true;
70
+ }
71
+ /**
72
+ * Reads data from the sync or async stream into the buffer.
73
+ * Reads up to maxBufferSize bytes at a time.
74
+ */
75
+ async readStreamAsync() {
76
+ this.readStream();
77
+ if (!this.asyncIterable ||
78
+ !(Symbol.asyncIterator in this.asyncIterable)) {
55
79
  return;
56
80
  }
57
81
  let i = 0;
82
+ const iterator = this.asyncIterable[Symbol.asyncIterator]();
58
83
  while (i < this.maxBufferSize) {
59
- const nextByte = await this.asyncIterable[Symbol.asyncIterator]().next();
84
+ const nextByte = await iterator.next();
60
85
  if (nextByte.done) {
61
86
  this.eof = true;
62
87
  break;
@@ -122,7 +147,12 @@ export class ByteBuffer {
122
147
  const index = this.bufferIndex + ahead;
123
148
  if (index >= this.buffer.length) {
124
149
  if (!this.eof) {
125
- throw new NoMoreTokensError('Buffer empty');
150
+ if (!this.readStream()) {
151
+ throw new NoMoreTokensError('No more items available');
152
+ }
153
+ else {
154
+ return this.peek(ahead);
155
+ }
126
156
  }
127
157
  return null;
128
158
  }
@@ -138,7 +168,12 @@ export class ByteBuffer {
138
168
  next() {
139
169
  if (this.bufferIndex >= this.buffer.length) {
140
170
  if (!this.eof) {
141
- throw new NoMoreTokensError('No more items available');
171
+ if (!this.readStream()) {
172
+ throw new NoMoreTokensError('No more items available');
173
+ }
174
+ else {
175
+ return this.next();
176
+ }
142
177
  }
143
178
  throw new EofReachedError('End of file reached');
144
179
  }
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/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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
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",