bin-serde 1.7.4 → 1.7.6

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
@@ -9,6 +9,8 @@ let slabOffset = 0;
9
9
 
10
10
  const f32 = new Float32Array(1);
11
11
  const f32u8 = new Uint8Array(f32.buffer);
12
+ const copyBuffer =
13
+ typeof Buffer !== "undefined" ? (buf: Uint8Array) => Buffer.from(buf) : (buf: Uint8Array) => buf.slice();
12
14
 
13
15
  function allocFromSlab(size: number): Uint8Array {
14
16
  if (size > MAX_POOLED) {
@@ -154,9 +156,7 @@ export class Writer {
154
156
  }
155
157
 
156
158
  toBuffer() {
157
- return typeof Buffer !== "undefined"
158
- ? Buffer.from(this.bytes.buffer, this.bytes.byteOffset, this.pos)
159
- : this.bytes.slice(0, this.pos);
159
+ return copyBuffer(this.bytes.subarray(0, this.pos));
160
160
  }
161
161
 
162
162
  reset() {
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<ArrayBuffer> | Buffer<ArrayBufferLike>;
17
+ toBuffer(): Uint8Array<ArrayBuffer> | Buffer<ArrayBuffer>;
18
18
  reset(): this;
19
19
  get size(): number;
20
20
  private ensureSize;
package/lib/index.js CHANGED
@@ -6,6 +6,7 @@ let slab = allocUint8Array(SLAB_SIZE);
6
6
  let slabOffset = 0;
7
7
  const f32 = new Float32Array(1);
8
8
  const f32u8 = new Uint8Array(f32.buffer);
9
+ const copyBuffer = typeof Buffer !== "undefined" ? (buf) => Buffer.from(buf) : (buf) => buf.slice();
9
10
  function allocFromSlab(size) {
10
11
  if (size > MAX_POOLED) {
11
12
  // Too large for pool, allocate directly
@@ -136,9 +137,7 @@ export class Writer {
136
137
  return this;
137
138
  }
138
139
  toBuffer() {
139
- return typeof Buffer !== "undefined"
140
- ? Buffer.from(this.bytes.buffer, this.bytes.byteOffset, this.pos)
141
- : this.bytes.slice(0, this.pos);
140
+ return copyBuffer(this.bytes.subarray(0, this.pos));
142
141
  }
143
142
  reset() {
144
143
  this.pos = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
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",