json-stream-lite 1.0.17 → 1.0.19
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/byte-buffer.d.ts +3 -10
- package/dist/byte-buffer.js +23 -26
- package/dist/defaults.d.ts +4 -0
- package/dist/defaults.js +4 -0
- package/dist/errors.d.ts +20 -0
- package/dist/errors.js +20 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/parser.d.ts +9 -1
- package/dist/parser.js +15 -5
- package/package.json +1 -1
package/dist/byte-buffer.d.ts
CHANGED
|
@@ -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
|
*
|
package/dist/byte-buffer.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
135
|
+
this.push(byte);
|
|
146
136
|
}
|
|
147
137
|
continue;
|
|
148
138
|
}
|
|
149
|
-
this.
|
|
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.
|
package/dist/defaults.js
ADDED
package/dist/errors.d.ts
ADDED
|
@@ -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/index.d.ts
CHANGED
package/dist/index.js
CHANGED
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
|
|
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
|
|
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
|
*/
|
|
@@ -165,11 +175,11 @@ export class JsonEntity {
|
|
|
165
175
|
throw new Error('JSON entity has already been consumed.');
|
|
166
176
|
}
|
|
167
177
|
while (!this.buffer.atEof()) {
|
|
168
|
-
await this.buffer.readStreamAsync();
|
|
169
178
|
const res = this.tryParse(() => this.read());
|
|
170
179
|
if (res !== undefined) {
|
|
171
180
|
return res;
|
|
172
181
|
}
|
|
182
|
+
await this.buffer.readStreamAsync();
|
|
173
183
|
}
|
|
174
184
|
return this.read();
|
|
175
185
|
}
|
|
@@ -479,7 +489,6 @@ export class JsonObject extends JsonEntity {
|
|
|
479
489
|
*/
|
|
480
490
|
async *membersAsync() {
|
|
481
491
|
while (!this.buffer.atEof()) {
|
|
482
|
-
await this.buffer.readStreamAsync();
|
|
483
492
|
const memberGen = this.members();
|
|
484
493
|
let currentMember = undefined;
|
|
485
494
|
while (true) {
|
|
@@ -495,6 +504,7 @@ export class JsonObject extends JsonEntity {
|
|
|
495
504
|
await currentMember.value.key.consumeAsync();
|
|
496
505
|
await currentMember.value.value.consumeAsync();
|
|
497
506
|
}
|
|
507
|
+
await this.buffer.readStreamAsync();
|
|
498
508
|
}
|
|
499
509
|
}
|
|
500
510
|
/**
|
|
@@ -570,7 +580,6 @@ export class JsonArray extends JsonEntity {
|
|
|
570
580
|
*/
|
|
571
581
|
async *itemsAsync() {
|
|
572
582
|
while (!this.buffer.atEof()) {
|
|
573
|
-
await this.buffer.readStreamAsync();
|
|
574
583
|
const itemGen = this.items();
|
|
575
584
|
let currentItem = undefined;
|
|
576
585
|
while (true) {
|
|
@@ -584,6 +593,7 @@ export class JsonArray extends JsonEntity {
|
|
|
584
593
|
yield currentItem.value;
|
|
585
594
|
await currentItem.value.consumeAsync();
|
|
586
595
|
}
|
|
596
|
+
await this.buffer.readStreamAsync();
|
|
587
597
|
}
|
|
588
598
|
}
|
|
589
599
|
/**
|
|
@@ -687,7 +697,6 @@ export class JsonKeyValueParser extends JsonEntity {
|
|
|
687
697
|
*/
|
|
688
698
|
async *parseAsync() {
|
|
689
699
|
while (!this.buffer.atEof()) {
|
|
690
|
-
await this.buffer.readStreamAsync();
|
|
691
700
|
const parserGen = this.parse();
|
|
692
701
|
let currentPair = undefined;
|
|
693
702
|
while (true) {
|
|
@@ -700,6 +709,7 @@ export class JsonKeyValueParser extends JsonEntity {
|
|
|
700
709
|
}
|
|
701
710
|
yield currentPair.value;
|
|
702
711
|
}
|
|
712
|
+
await this.buffer.readStreamAsync();
|
|
703
713
|
}
|
|
704
714
|
}
|
|
705
715
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.19",
|
|
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",
|