bin-serde 1.7.7 → 1.7.9

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/index.ts CHANGED
@@ -288,6 +288,13 @@ export class Reader {
288
288
  return this.bytes.length - this.pos;
289
289
  }
290
290
 
291
+ reset(buf: Uint8Array) {
292
+ this.bytes = buf;
293
+ this.pos = 0;
294
+ this._view = null;
295
+ return this;
296
+ }
297
+
291
298
  private get view(): DataView {
292
299
  if (!this._view) {
293
300
  this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
package/lib/index.d.ts CHANGED
@@ -36,5 +36,6 @@ export declare class Reader {
36
36
  readStringUtf8(len?: number): string;
37
37
  readBuffer(numBytes: number): Uint8Array<ArrayBuffer>;
38
38
  remaining(): number;
39
+ reset(buf: Uint8Array): this;
39
40
  private get view();
40
41
  }
package/lib/index.js CHANGED
@@ -251,6 +251,12 @@ export class Reader {
251
251
  remaining() {
252
252
  return this.bytes.length - this.pos;
253
253
  }
254
+ reset(buf) {
255
+ this.bytes = buf;
256
+ this.pos = 0;
257
+ this._view = null;
258
+ return this;
259
+ }
254
260
  get view() {
255
261
  if (!this._view) {
256
262
  this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
@@ -1,4 +1,5 @@
1
1
  const textDecoder = new TextDecoder();
2
+ const textEncoder = new TextEncoder();
2
3
  const hasBuffer = typeof Buffer !== "undefined";
3
4
  export function unpack(buffer, start = 0, end = buffer.length) {
4
5
  const len = end - start;
@@ -38,8 +39,20 @@ export function unpack(buffer, start = 0, end = buffer.length) {
38
39
  return String.fromCharCode.apply(String, chunks);
39
40
  }
40
41
  export function pack(str, buffer, index = 0) {
42
+ const len = str.length;
43
+ if (len < 1)
44
+ return index;
45
+ // Node.js: use Buffer.write()
46
+ if (hasBuffer && Buffer.isBuffer(buffer)) {
47
+ return index + buffer.write(str, index, "utf8");
48
+ }
49
+ // Long strings: use TextEncoder
50
+ if (len > 64) {
51
+ return index + textEncoder.encodeInto(str, index === 0 ? buffer : buffer.subarray(index)).written;
52
+ }
53
+ // Short strings: use pure JS
41
54
  let c1, c2;
42
- for (let i = 0; i < str.length; i++) {
55
+ for (let i = 0; i < len; i++) {
43
56
  c1 = str.charCodeAt(i);
44
57
  if (c1 < 128) {
45
58
  buffer[index++] = c1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.7.7",
3
+ "version": "1.7.9",
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",
package/utf8-buffer.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  const textDecoder = new TextDecoder();
2
+ const textEncoder = new TextEncoder();
2
3
  const hasBuffer = typeof Buffer !== "undefined";
3
4
 
4
5
  export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
@@ -39,8 +40,22 @@ export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): stri
39
40
  }
40
41
 
41
42
  export function pack(str: string, buffer: Uint8Array, index = 0): number {
43
+ const len = str.length;
44
+ if (len < 1) return index;
45
+
46
+ // Node.js: use Buffer.write()
47
+ if (hasBuffer && Buffer.isBuffer(buffer)) {
48
+ return index + buffer.write(str, index, "utf8");
49
+ }
50
+
51
+ // Long strings: use TextEncoder
52
+ if (len > 64) {
53
+ return index + textEncoder.encodeInto(str, index === 0 ? buffer : buffer.subarray(index)).written;
54
+ }
55
+
56
+ // Short strings: use pure JS
42
57
  let c1: number, c2: number;
43
- for (let i = 0; i < str.length; i++) {
58
+ for (let i = 0; i < len; i++) {
44
59
  c1 = str.charCodeAt(i);
45
60
  if (c1 < 128) {
46
61
  buffer[index++] = c1;