json-stream-lite 1.0.17 → 1.0.18

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.
@@ -1,14 +1,4 @@
1
1
  import type { ByteStream, JsonStreamInput } from './types.js';
2
- /**
3
- * Error thrown when the buffer is empty and more input is needed.
4
- */
5
- export declare class NoMoreTokensError extends Error {
6
- }
7
- /**
8
- * Error thrown when the end of file has been reached and no more items are available.
9
- */
10
- export declare class EofReachedError extends Error {
11
- }
12
2
  /**
13
3
  * A buffer for managing byte-level input with support for async streams.
14
4
  * Provides lookahead, consumption tracking, and buffer compaction capabilities.
@@ -22,6 +12,8 @@ export declare class ByteBuffer {
22
12
  bufferIndex: number;
23
13
  /** Whether the buffer is locked against compaction */
24
14
  locked: boolean;
15
+ /** Whether to allow exceeding the buffer size temporarily */
16
+ allowBufferToBeExceeded: boolean;
25
17
  /** Current position in the input stream */
26
18
  protected inputOffset: number;
27
19
  /** Number of outputs generated */
@@ -58,6 +50,7 @@ export declare class ByteBuffer {
58
50
  * @param input - Input items to add to the buffer
59
51
  */
60
52
  feed(...input: JsonStreamInput[]): void;
53
+ push(byte: number): void;
61
54
  /**
62
55
  * Checks if end of file has been reached and buffer is exhausted.
63
56
  *
@@ -1,18 +1,6 @@
1
+ import { DEFAULT_MAX_BUFFER_SIZE } from './defaults.js';
2
+ import { BufferSizeExceededError, EofReachedError, NoMoreTokensError, } from './errors.js';
1
3
  import { bytesToString, stringToBytes } from './utils.js';
2
- /**
3
- * Default maximum buffer size before compaction
4
- */
5
- const DEFAULT_MAX_BUFFER_SIZE = 1024 * 100; // 100 KB
6
- /**
7
- * Error thrown when the buffer is empty and more input is needed.
8
- */
9
- export class NoMoreTokensError extends Error {
10
- }
11
- /**
12
- * Error thrown when the end of file has been reached and no more items are available.
13
- */
14
- export class EofReachedError extends Error {
15
- }
16
4
  /**
17
5
  * Converts a ReadableStream into an AsyncIterable.
18
6
  *
@@ -46,6 +34,8 @@ export class ByteBuffer {
46
34
  bufferIndex = 0;
47
35
  /** Whether the buffer is locked against compaction */
48
36
  locked = false;
37
+ /** Whether to allow exceeding the buffer size temporarily */
38
+ allowBufferToBeExceeded = true;
49
39
  /** Current position in the input stream */
50
40
  inputOffset = 0;
51
41
  /** Number of outputs generated */
@@ -75,19 +65,19 @@ export class ByteBuffer {
75
65
  if (!this.asyncIterable || !(Symbol.iterator in this.asyncIterable)) {
76
66
  return false;
77
67
  }
78
- let i = 0;
79
68
  const iterator = this.asyncIterable[Symbol.iterator]();
80
- while (i < this.maxBufferSize) {
69
+ let processed = false;
70
+ while (this.length < this.maxBufferSize || !processed) {
81
71
  const next = iterator.next();
72
+ processed = true;
82
73
  if (next.done) {
83
74
  this.eof = true;
84
75
  break;
85
76
  }
86
77
  const value = next.value;
87
78
  this.feed(value);
88
- i++;
89
79
  }
90
- return i > 0;
80
+ return processed;
91
81
  }
92
82
  /**
93
83
  * Reads data from the sync or async stream into the buffer.
@@ -99,17 +89,17 @@ export class ByteBuffer {
99
89
  !(Symbol.asyncIterator in this.asyncIterable)) {
100
90
  return;
101
91
  }
102
- let i = 0;
103
92
  const iterator = this.asyncIterable[Symbol.asyncIterator]();
104
- while (i < this.maxBufferSize) {
93
+ let processed = false;
94
+ while (this.length < this.maxBufferSize || !processed) {
95
+ processed = true;
105
96
  const next = await iterator.next();
106
97
  if (next.done) {
107
98
  this.eof = true;
108
- break;
99
+ return;
109
100
  }
110
101
  const value = next.value;
111
102
  this.feed(value);
112
- i++;
113
103
  }
114
104
  }
115
105
  /**
@@ -129,25 +119,32 @@ export class ByteBuffer {
129
119
  for (const item of input) {
130
120
  if (Array.isArray(item)) {
131
121
  for (const subItem of item) {
132
- this.buffer.push(subItem);
122
+ this.push(subItem);
133
123
  }
134
124
  continue;
135
125
  }
136
126
  else if (item instanceof Uint8Array) {
137
127
  for (const subItem of item) {
138
- this.buffer.push(subItem);
128
+ this.push(subItem);
139
129
  }
140
130
  continue;
141
131
  }
142
132
  else if (typeof item === 'string') {
143
133
  const encoded = stringToBytes(item);
144
134
  for (const byte of encoded) {
145
- this.buffer.push(byte);
135
+ this.push(byte);
146
136
  }
147
137
  continue;
148
138
  }
149
- this.buffer.push(item);
139
+ this.push(item);
140
+ }
141
+ }
142
+ push(byte) {
143
+ if (!this.allowBufferToBeExceeded &&
144
+ this.buffer.length >= this.maxBufferSize) {
145
+ throw new BufferSizeExceededError('Buffer size exceeded maximum limit');
150
146
  }
147
+ this.buffer.push(byte);
151
148
  }
152
149
  /**
153
150
  * Checks if end of file has been reached and buffer is exhausted.
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Default maximum buffer size before compaction
3
+ */
4
+ export declare const DEFAULT_MAX_BUFFER_SIZE: number;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Default maximum buffer size before compaction
3
+ */
4
+ export const DEFAULT_MAX_BUFFER_SIZE = 1024 * 100; // 100 KB
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Base error class for JSON Stream Lite errors.
3
+ */
4
+ export declare class JsonStreamLiteError extends Error {
5
+ }
6
+ /**
7
+ * Error thrown when the buffer is empty and more input is needed.
8
+ */
9
+ export declare class NoMoreTokensError extends JsonStreamLiteError {
10
+ }
11
+ /**
12
+ * Error thrown when the end of file has been reached and no more items are available.
13
+ */
14
+ export declare class EofReachedError extends JsonStreamLiteError {
15
+ }
16
+ /**
17
+ * Error thrown when the buffer size limit is exceeded.
18
+ */
19
+ export declare class BufferSizeExceededError extends JsonStreamLiteError {
20
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Base error class for JSON Stream Lite errors.
3
+ */
4
+ export class JsonStreamLiteError extends Error {
5
+ }
6
+ /**
7
+ * Error thrown when the buffer is empty and more input is needed.
8
+ */
9
+ export class NoMoreTokensError extends JsonStreamLiteError {
10
+ }
11
+ /**
12
+ * Error thrown when the end of file has been reached and no more items are available.
13
+ */
14
+ export class EofReachedError extends JsonStreamLiteError {
15
+ }
16
+ /**
17
+ * Error thrown when the buffer size limit is exceeded.
18
+ */
19
+ export class BufferSizeExceededError extends JsonStreamLiteError {
20
+ }
package/dist/parser.d.ts CHANGED
@@ -22,7 +22,15 @@ export declare abstract class JsonEntity<T> {
22
22
  */
23
23
  get entityType(): string;
24
24
  /**
25
- * Sets the maximum buffer size before compaction occurs.
25
+ * Sets whether to allow exceeding the buffer size temporarily. Default is true.
26
+ *
27
+ * @param value - True to allow exceeding the buffer size, false to enforce the limit
28
+ */
29
+ set allowBufferToBeExceeded(value: boolean);
30
+ /**
31
+ * Sets the maximum buffer size before compaction occurs. Defaults to 100 KB.
32
+ * NOTE: The buffer size may be exceeded temporarily during reads. For example, if a large string is read that exceeds the max size.
33
+ * If this is not desired, set `allowBufferToBeExceeded` to false (default is true).
26
34
  *
27
35
  * @param size - The maximum buffer size in bytes
28
36
  */
package/dist/parser.js CHANGED
@@ -105,7 +105,17 @@ export class JsonEntity {
105
105
  return this.constructor.name;
106
106
  }
107
107
  /**
108
- * Sets the maximum buffer size before compaction occurs.
108
+ * Sets whether to allow exceeding the buffer size temporarily. Default is true.
109
+ *
110
+ * @param value - True to allow exceeding the buffer size, false to enforce the limit
111
+ */
112
+ set allowBufferToBeExceeded(value) {
113
+ this.buffer.allowBufferToBeExceeded = value;
114
+ }
115
+ /**
116
+ * Sets the maximum buffer size before compaction occurs. Defaults to 100 KB.
117
+ * NOTE: The buffer size may be exceeded temporarily during reads. For example, if a large string is read that exceeds the max size.
118
+ * If this is not desired, set `allowBufferToBeExceeded` to false (default is true).
109
119
  *
110
120
  * @param size - The maximum buffer size in bytes
111
121
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
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",