bin-serde 1.6.8 → 1.6.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
@@ -4,7 +4,7 @@ import utf8Size from "utf8-buffer-size";
4
4
  const SLAB_SIZE = 8192;
5
5
  const MAX_POOLED = 4096;
6
6
 
7
- let slab: Uint8Array = new Uint8Array(SLAB_SIZE);
7
+ let slab = allocUint8Array(SLAB_SIZE);
8
8
  let slabOffset = 0;
9
9
 
10
10
  const f32 = new Float32Array(1);
@@ -13,11 +13,11 @@ const f32u8 = new Uint8Array(f32.buffer);
13
13
  function allocFromSlab(size: number): Uint8Array {
14
14
  if (size > MAX_POOLED) {
15
15
  // Too large for pool, allocate directly
16
- return new Uint8Array(size);
16
+ return allocUint8Array(size);
17
17
  }
18
18
  if (slabOffset + size > SLAB_SIZE) {
19
19
  // Slab full, allocate new one
20
- slab = new Uint8Array(SLAB_SIZE);
20
+ slab = allocUint8Array(SLAB_SIZE);
21
21
  slabOffset = 0;
22
22
  }
23
23
  const buf = slab.subarray(slabOffset, slabOffset + size);
@@ -25,6 +25,10 @@ function allocFromSlab(size: number): Uint8Array {
25
25
  return buf;
26
26
  }
27
27
 
28
+ function allocUint8Array(size: number): Uint8Array {
29
+ return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
30
+ }
31
+
28
32
  export class Writer {
29
33
  private pos = 0;
30
34
  private bytes: Uint8Array;
package/lib/index.js CHANGED
@@ -2,24 +2,27 @@ import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
3
  const SLAB_SIZE = 8192;
4
4
  const MAX_POOLED = 4096;
5
- let slab = new Uint8Array(SLAB_SIZE);
5
+ 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
9
  function allocFromSlab(size) {
10
10
  if (size > MAX_POOLED) {
11
11
  // Too large for pool, allocate directly
12
- return new Uint8Array(size);
12
+ return allocUint8Array(size);
13
13
  }
14
14
  if (slabOffset + size > SLAB_SIZE) {
15
15
  // Slab full, allocate new one
16
- slab = new Uint8Array(SLAB_SIZE);
16
+ slab = allocUint8Array(SLAB_SIZE);
17
17
  slabOffset = 0;
18
18
  }
19
19
  const buf = slab.subarray(slabOffset, slabOffset + size);
20
20
  slabOffset += size;
21
21
  return buf;
22
22
  }
23
+ function allocUint8Array(size) {
24
+ return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
25
+ }
23
26
  export class Writer {
24
27
  pos = 0;
25
28
  bytes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.8",
3
+ "version": "1.6.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",