json-stream-lite 1.0.13 → 1.0.14

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,8 +1,4 @@
1
- import { bytesToString } from './utils.js';
2
- /**
3
- * Shared TextEncoder instance for converting strings to UTF-8 bytes.
4
- */
5
- const textEncoder = new TextEncoder();
1
+ import { bytesToString, stringToBytes } from './utils.js';
6
2
  /**
7
3
  * Default maximum buffer size before compaction
8
4
  */
@@ -119,7 +115,7 @@ export class ByteBuffer {
119
115
  continue;
120
116
  }
121
117
  else if (typeof item === 'string') {
122
- const encoded = textEncoder.encode(item);
118
+ const encoded = stringToBytes(item);
123
119
  for (const byte of encoded) {
124
120
  this.buffer.push(byte);
125
121
  }
package/dist/stringify.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { stringToBytes } from './utils';
1
2
  /**
2
3
  * Formats a string value for JSON output, escaping special characters.
3
4
  * Yields the formatted string in chunks for memory efficiency.
@@ -184,10 +185,6 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
184
185
  export function jsonStreamStringify(value, replacer, indent = 0, options) {
185
186
  return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
186
187
  }
187
- /**
188
- * Shared TextEncoder instance for converting strings to UTF-8 bytes.
189
- */
190
- const TEXT_ENCODER = new TextEncoder();
191
188
  /**
192
189
  * Converts a JavaScript value to JSON format, yielding Uint8Array byte chunks.
193
190
  * Provides memory-efficient streaming stringification with binary output.
@@ -209,6 +206,6 @@ const TEXT_ENCODER = new TextEncoder();
209
206
  export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options) {
210
207
  const stringGenerator = jsonStreamStringify(value, replacer, indent, options);
211
208
  for (const chunk of stringGenerator) {
212
- yield TEXT_ENCODER.encode(chunk);
209
+ yield stringToBytes(chunk);
213
210
  }
214
211
  }
package/dist/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ const textEncoder = new TextEncoder();
2
+ const textDecoder = new TextDecoder();
1
3
  /**
2
4
  * Converts a string to a Uint8Array of UTF-8 encoded bytes.
3
5
  *
@@ -5,7 +7,7 @@
5
7
  * @returns A Uint8Array containing the UTF-8 encoded bytes
6
8
  */
7
9
  export function stringToBytes(str) {
8
- return new TextEncoder().encode(str);
10
+ return textEncoder.encode(str);
9
11
  }
10
12
  /**
11
13
  * Converts a Uint8Array of UTF-8 encoded bytes to a string.
@@ -14,7 +16,7 @@ export function stringToBytes(str) {
14
16
  * @returns The decoded string
15
17
  */
16
18
  export function bytesToString(bytes) {
17
- return new TextDecoder().decode(bytes);
19
+ return textDecoder.decode(bytes);
18
20
  }
19
21
  /**
20
22
  * Converts a Uint8Array of UTF-8 encoded bytes to a number.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
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",