json-stream-lite 1.0.10 → 1.0.11

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/README.md CHANGED
@@ -109,6 +109,22 @@ const response = await fetch('https://api.example.com/data.json')
109
109
  await processStream(response.body!)
110
110
  ```
111
111
 
112
+ ### Controlling Buffer Size
113
+
114
+ Limit memory usage by setting a maximum buffer size:
115
+
116
+ ```typescript
117
+ import { JsonObject } from 'json-stream-lite'
118
+
119
+ const parser = new JsonObject()
120
+ parser.maxBufferSize = 1024 * 1024 // 1 MB
121
+
122
+ // Feed data into the parser
123
+ parser.feed('...large JSON data...')
124
+ const result = parser.read()
125
+ console.log(result)
126
+ ```
127
+
112
128
  ### Key-Value Extraction
113
129
 
114
130
  Flatten nested JSON structures into dot-notation key-value pairs:
@@ -1,4 +1,4 @@
1
- import { ByteStream } from './types.js';
1
+ import type { ByteStream, JsonStreamInput } from './types.js';
2
2
  export declare class NoMoreTokensError extends Error {
3
3
  }
4
4
  export declare class EofReachedError extends Error {
@@ -28,7 +28,7 @@ export declare class ByteBuffer {
28
28
  *
29
29
  * @param input - Input items to add to the buffer
30
30
  */
31
- feed(...input: (number | number[] | Uint8Array)[]): void;
31
+ feed(...input: JsonStreamInput[]): void;
32
32
  /**
33
33
  * Checks if end of file has been reached and buffer is exhausted.
34
34
  *
@@ -1,11 +1,13 @@
1
1
  import { bytesToString } from './utils.js';
2
+ const textEncoder = new TextEncoder();
3
+ const DEFAULT_MAX_BUFFER_SIZE = 1024 * 100; // 100 KB
2
4
  export class NoMoreTokensError extends Error {
3
5
  }
4
6
  export class EofReachedError extends Error {
5
7
  }
6
8
  export class ByteBuffer {
7
9
  /** Maximum size of the buffer before compaction */
8
- maxBufferSize = 1000;
10
+ maxBufferSize = DEFAULT_MAX_BUFFER_SIZE;
9
11
  /** Whether end of file has been signaled */
10
12
  eof = false;
11
13
  /** Current position in the buffer */
@@ -34,7 +36,8 @@ export class ByteBuffer {
34
36
  this.eof = true;
35
37
  break;
36
38
  }
37
- this.feed(nextByte.value);
39
+ const value = nextByte.value;
40
+ this.feed(value);
38
41
  i++;
39
42
  }
40
43
  }
@@ -60,6 +63,13 @@ export class ByteBuffer {
60
63
  }
61
64
  continue;
62
65
  }
66
+ else if (typeof item === 'string') {
67
+ const encoded = textEncoder.encode(item);
68
+ for (const byte of encoded) {
69
+ this.buffer.push(byte);
70
+ }
71
+ continue;
72
+ }
63
73
  this.buffer.push(item);
64
74
  }
65
75
  }
package/dist/parser.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ByteBuffer } from './byte-buffer.js';
2
- import { ByteStream, JsonKeyValuePair } from './types.js';
2
+ import { ByteStream, JsonKeyValuePair, JsonStreamInput } from './types.js';
3
3
  export declare abstract class JsonEntity<T> {
4
4
  consumed: boolean;
5
5
  protected buffer: ByteBuffer;
@@ -7,7 +7,7 @@ export declare abstract class JsonEntity<T> {
7
7
  get entityType(): string;
8
8
  set maxBufferSize(size: number);
9
9
  get bufferLength(): number;
10
- feed(...input: (number | number[])[]): void;
10
+ feed(...input: JsonStreamInput[]): void;
11
11
  protected abstract parse(): T;
12
12
  protected skipWhitespace(): void;
13
13
  read(): T;
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export type ByteStream = AsyncIterable<number | number[] | Uint8Array>;
1
+ export type JsonStreamInput = string | number | number[] | Uint8Array;
2
+ export type ByteStream = AsyncIterable<JsonStreamInput>;
2
3
  export type JsonPrimitive = string | number | boolean | null;
3
4
  export type JsonKeyValuePair = [string, JsonPrimitive];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
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",