bin-serde 1.6.6 → 1.6.8

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/lib/index.d.ts CHANGED
@@ -14,7 +14,7 @@ export declare class Writer {
14
14
  writeStringUtf8(val: string, len?: number): this;
15
15
  writeBuffer(buf: Uint8Array): this;
16
16
  concat(other: Writer): this;
17
- toBuffer(): Uint8Array;
17
+ toBuffer(): Uint8Array<ArrayBufferLike>;
18
18
  get size(): number;
19
19
  private ensureSize;
20
20
  private get view();
@@ -33,7 +33,7 @@ export declare class Reader {
33
33
  readBits(numBits: number): boolean[];
34
34
  readStringAscii(len: number): string;
35
35
  readStringUtf8(len?: number): string;
36
- readBuffer(numBytes: number): Uint8Array;
36
+ readBuffer(numBytes: number): Uint8Array<ArrayBuffer>;
37
37
  remaining(): number;
38
38
  private get view();
39
39
  }
@@ -1,37 +1,17 @@
1
+ const textDecoder = new TextDecoder();
2
+ const hasBuffer = typeof Buffer !== "undefined";
1
3
  export function unpack(buffer, start = 0, end = buffer.length) {
2
- const len = end - start;
3
- if (len < 1)
4
+ if (end - start < 1)
4
5
  return "";
5
- const chunks = [];
6
- const chunk = [];
7
- let i = 0;
8
- while (start < end) {
9
- let t = buffer[start++];
10
- if (t < 128) {
11
- chunk[i++] = t;
12
- }
13
- else if (t > 191 && t < 224) {
14
- chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
- }
16
- else if (t > 239 && t < 245) {
17
- t =
18
- (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
19
- 0x10000;
20
- chunk[i++] = 0xd800 + (t >> 10);
21
- chunk[i++] = 0xdc00 + (t & 1023);
22
- }
23
- else {
24
- chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
25
- }
26
- if (i > 8191) {
27
- chunks.push(String.fromCharCode.apply(String, chunk));
28
- i = 0;
29
- }
6
+ // Fast path: use Buffer.toString() in Node.js (avoids subarray overhead)
7
+ if (hasBuffer && Buffer.isBuffer(buffer)) {
8
+ return buffer.toString("utf8", start, end);
30
9
  }
31
- if (i > 0) {
32
- chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
10
+ // Standard path: TextDecoder
11
+ if (start === 0 && end === buffer.length) {
12
+ return textDecoder.decode(buffer);
33
13
  }
34
- return chunks.join("");
14
+ return textDecoder.decode(buffer.subarray(start, end));
35
15
  }
36
16
  export function pack(str, buffer, index = 0) {
37
17
  let c1, c2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.6",
3
+ "version": "1.6.8",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -30,6 +30,7 @@
30
30
  "utf8-buffer-size": "^0.0.4"
31
31
  },
32
32
  "devDependencies": {
33
- "typescript": "^4.5.5"
33
+ "@types/node": "^25.0.1",
34
+ "typescript": "^5.9.3"
34
35
  }
35
36
  }
package/utf8-buffer.ts CHANGED
@@ -1,36 +1,19 @@
1
- export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
2
- const len = end - start;
3
- if (len < 1) return "";
1
+ const textDecoder = new TextDecoder();
2
+ const hasBuffer = typeof Buffer !== "undefined";
4
3
 
5
- const chunks: string[] = [];
6
- const chunk: number[] = [];
7
- let i = 0;
4
+ export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
5
+ if (end - start < 1) return "";
8
6
 
9
- while (start < end) {
10
- let t = buffer[start++];
11
- if (t < 128) {
12
- chunk[i++] = t;
13
- } else if (t > 191 && t < 224) {
14
- chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
- } else if (t > 239 && t < 245) {
16
- t =
17
- (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
18
- 0x10000;
19
- chunk[i++] = 0xd800 + (t >> 10);
20
- chunk[i++] = 0xdc00 + (t & 1023);
21
- } else {
22
- chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
23
- }
24
- if (i > 8191) {
25
- chunks.push(String.fromCharCode.apply(String, chunk));
26
- i = 0;
27
- }
7
+ // Fast path: use Buffer.toString() in Node.js (avoids subarray overhead)
8
+ if (hasBuffer && Buffer.isBuffer(buffer)) {
9
+ return buffer.toString("utf8", start, end);
28
10
  }
29
11
 
30
- if (i > 0) {
31
- chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
12
+ // Standard path: TextDecoder
13
+ if (start === 0 && end === buffer.length) {
14
+ return textDecoder.decode(buffer);
32
15
  }
33
- return chunks.join("");
16
+ return textDecoder.decode(buffer.subarray(start, end));
34
17
  }
35
18
 
36
19
  export function pack(str: string, buffer: Uint8Array, index = 0): number {