json-stream-lite 1.0.15 → 1.0.17
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.js +34 -9
- package/dist/parser.d.ts +18 -20
- package/dist/stringify.js +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/byte-buffer.js
CHANGED
|
@@ -13,6 +13,26 @@ export class NoMoreTokensError extends Error {
|
|
|
13
13
|
*/
|
|
14
14
|
export class EofReachedError extends Error {
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Converts a ReadableStream into an AsyncIterable.
|
|
18
|
+
*
|
|
19
|
+
* @param stream - The ReadableStream to convert
|
|
20
|
+
* @returns An AsyncIterable that yields items from the stream
|
|
21
|
+
*/
|
|
22
|
+
function readableStreamToAsyncIterable(stream) {
|
|
23
|
+
const reader = stream.getReader();
|
|
24
|
+
return {
|
|
25
|
+
async *[Symbol.asyncIterator]() {
|
|
26
|
+
while (true) {
|
|
27
|
+
const { done, value } = await reader.read();
|
|
28
|
+
if (done) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
yield value;
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
16
36
|
/**
|
|
17
37
|
* A buffer for managing byte-level input with support for async streams.
|
|
18
38
|
* Provides lookahead, consumption tracking, and buffer compaction capabilities.
|
|
@@ -40,7 +60,12 @@ export class ByteBuffer {
|
|
|
40
60
|
* @param asyncIterable - Optional async iterable source for streaming input
|
|
41
61
|
*/
|
|
42
62
|
constructor(asyncIterable) {
|
|
43
|
-
this.asyncIterable =
|
|
63
|
+
this.asyncIterable =
|
|
64
|
+
asyncIterable instanceof ReadableStream
|
|
65
|
+
? readableStreamToAsyncIterable(asyncIterable)
|
|
66
|
+
: typeof asyncIterable === 'string'
|
|
67
|
+
? [asyncIterable]
|
|
68
|
+
: asyncIterable;
|
|
44
69
|
}
|
|
45
70
|
/**
|
|
46
71
|
* Reads data from the stream into the buffer.
|
|
@@ -53,16 +78,16 @@ export class ByteBuffer {
|
|
|
53
78
|
let i = 0;
|
|
54
79
|
const iterator = this.asyncIterable[Symbol.iterator]();
|
|
55
80
|
while (i < this.maxBufferSize) {
|
|
56
|
-
const
|
|
57
|
-
if (
|
|
81
|
+
const next = iterator.next();
|
|
82
|
+
if (next.done) {
|
|
58
83
|
this.eof = true;
|
|
59
|
-
|
|
84
|
+
break;
|
|
60
85
|
}
|
|
61
|
-
const value =
|
|
86
|
+
const value = next.value;
|
|
62
87
|
this.feed(value);
|
|
63
88
|
i++;
|
|
64
89
|
}
|
|
65
|
-
return
|
|
90
|
+
return i > 0;
|
|
66
91
|
}
|
|
67
92
|
/**
|
|
68
93
|
* Reads data from the sync or async stream into the buffer.
|
|
@@ -77,12 +102,12 @@ export class ByteBuffer {
|
|
|
77
102
|
let i = 0;
|
|
78
103
|
const iterator = this.asyncIterable[Symbol.asyncIterator]();
|
|
79
104
|
while (i < this.maxBufferSize) {
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
105
|
+
const next = await iterator.next();
|
|
106
|
+
if (next.done) {
|
|
82
107
|
this.eof = true;
|
|
83
108
|
break;
|
|
84
109
|
}
|
|
85
|
-
const value =
|
|
110
|
+
const value = next.value;
|
|
86
111
|
this.feed(value);
|
|
87
112
|
i++;
|
|
88
113
|
}
|
package/dist/parser.d.ts
CHANGED
|
@@ -142,7 +142,7 @@ export type JsonPrimitiveType = JsonString | JsonNumber | JsonBoolean | JsonNull
|
|
|
142
142
|
*
|
|
143
143
|
* @typeParam T - The expected type of the value
|
|
144
144
|
*/
|
|
145
|
-
export type JsonValueType<T =
|
|
145
|
+
export type JsonValueType<T = any> = T extends string ? JsonString<Extract<T, string>> : T extends number ? JsonNumber<Extract<T, number>> : T extends boolean ? JsonBoolean<Extract<T, boolean>> : T extends null ? JsonNull : T extends (infer K)[] ? JsonArray<K> : T extends object ? JsonObject<Extract<T, object>> : JsonString | JsonNumber | JsonBoolean | JsonNull | JsonArray | JsonObject;
|
|
146
146
|
/**
|
|
147
147
|
* Represents any JSON value (primitive, object, or array).
|
|
148
148
|
* Provides lazy evaluation and type detection for JSON values.
|
|
@@ -150,7 +150,7 @@ export type JsonValueType<T = unknown> = JsonPrimitiveType | JsonObject<T> | Jso
|
|
|
150
150
|
* @typeParam T - The expected type of the value
|
|
151
151
|
* @typeParam K - The key type for object properties (defaults to string)
|
|
152
152
|
*/
|
|
153
|
-
export declare class JsonValue<T
|
|
153
|
+
export declare class JsonValue<T = any, K extends string = string> extends JsonEntity<JsonValueType<T>> {
|
|
154
154
|
private key?;
|
|
155
155
|
private value?;
|
|
156
156
|
/**
|
|
@@ -200,49 +200,47 @@ export declare class JsonValue<T extends unknown = unknown, K extends string = s
|
|
|
200
200
|
*/
|
|
201
201
|
consumeAsync(): Promise<void>;
|
|
202
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Type representing the members of a JSON object as key-value pairs.
|
|
205
|
+
* Each member consists of a key of type K and a value of type T[K].
|
|
206
|
+
*/
|
|
207
|
+
export type JsonObjectMember<T extends object> = {
|
|
208
|
+
[K in keyof T]: {
|
|
209
|
+
key: JsonString<Extract<K, string>>;
|
|
210
|
+
value: JsonValue<T[K]>;
|
|
211
|
+
};
|
|
212
|
+
}[keyof T];
|
|
203
213
|
/**
|
|
204
214
|
* Represents a JSON object.
|
|
205
215
|
* Provides streaming access to object members (key-value pairs).
|
|
206
216
|
*
|
|
207
217
|
* @typeParam T - The expected type of the object
|
|
208
218
|
*/
|
|
209
|
-
export declare class JsonObject<T =
|
|
219
|
+
export declare class JsonObject<T extends object = any> extends JsonEntity<T> {
|
|
210
220
|
/**
|
|
211
221
|
* Generator that yields object members as key-value pairs.
|
|
212
222
|
* Allows for streaming/incremental processing of large objects.
|
|
213
223
|
*
|
|
214
224
|
* @yields Object containing the key and value entities for each member
|
|
215
225
|
*/
|
|
216
|
-
members(): Generator<
|
|
217
|
-
key: JsonString<Extract<keyof T, string>>;
|
|
218
|
-
value: JsonValue<T>;
|
|
219
|
-
}>;
|
|
226
|
+
members(): Generator<JsonObjectMember<T>>;
|
|
220
227
|
/**
|
|
221
228
|
* Async generator that yields object members from a stream.
|
|
222
229
|
* Allows for asynchronous streaming/incremental processing.
|
|
223
230
|
*
|
|
224
231
|
* @yields Object containing the key and value entities for each member
|
|
225
232
|
*/
|
|
226
|
-
membersAsync(): AsyncGenerator<
|
|
227
|
-
key: JsonString<Extract<keyof T, string>>;
|
|
228
|
-
value: JsonValue<T>;
|
|
229
|
-
}>;
|
|
233
|
+
membersAsync(): AsyncGenerator<JsonObjectMember<T>>;
|
|
230
234
|
/**
|
|
231
235
|
* Returns an iterator for object members.
|
|
232
236
|
* Enables use of for...of loops on JsonObject.
|
|
233
237
|
*/
|
|
234
|
-
[Symbol.iterator](): Generator<
|
|
235
|
-
key: JsonString<Extract<keyof T, string>>;
|
|
236
|
-
value: JsonValue<T>;
|
|
237
|
-
}, any, any>;
|
|
238
|
+
[Symbol.iterator](): Generator<JsonObjectMember<T>, any, any>;
|
|
238
239
|
/**
|
|
239
240
|
* Returns an async iterator for object members.
|
|
240
241
|
* Enables use of for await...of loops on JsonObject.
|
|
241
242
|
*/
|
|
242
|
-
[Symbol.asyncIterator](): AsyncGenerator<
|
|
243
|
-
key: JsonString<Extract<keyof T, string>>;
|
|
244
|
-
value: JsonValue<T>;
|
|
245
|
-
}, any, any>;
|
|
243
|
+
[Symbol.asyncIterator](): AsyncGenerator<JsonObjectMember<T>, any, any>;
|
|
246
244
|
/**
|
|
247
245
|
* Parses the entire object into a JavaScript object.
|
|
248
246
|
*
|
|
@@ -256,7 +254,7 @@ export declare class JsonObject<T = unknown> extends JsonEntity<T> {
|
|
|
256
254
|
*
|
|
257
255
|
* @typeParam T - The expected type of array elements
|
|
258
256
|
*/
|
|
259
|
-
export declare class JsonArray<T =
|
|
257
|
+
export declare class JsonArray<T = any> extends JsonEntity<T[]> {
|
|
260
258
|
/**
|
|
261
259
|
* Generator that yields array items.
|
|
262
260
|
* Allows for streaming/incremental processing of large arrays.
|
package/dist/stringify.js
CHANGED
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> | Iterable<JsonStreamInput>;
|
|
9
|
+
export type ByteStream = AsyncIterable<JsonStreamInput> | Iterable<JsonStreamInput> | ReadableStream<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.
|
|
3
|
+
"version": "1.0.17",
|
|
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",
|