@nxtedition/slice 1.1.7 → 1.1.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
@@ -10,6 +10,21 @@ export declare class Slice {
10
10
  byteLength: number;
11
11
  maxByteLength: number;
12
12
  static get EMPTY_BUF(): Buffer;
13
+ /**
14
+ * Fast factory for the common case of wrapping a full Buffer with no
15
+ * offset and no separate maxByteLength. Skips all the constructor's
16
+ * validation (Buffer instanceof check, byteOffset/byteLength range
17
+ * checks, integer checks, maxByteLength bounds). Use only when the
18
+ * caller already knows the input is a Buffer — e.g. wrapping freshly
19
+ * received network frames on a hot path.
20
+ *
21
+ * Note: instances produced here have a slightly different V8 hidden
22
+ * class than instances produced via `new Slice(buf)`. That introduces
23
+ * polymorphism at any downstream IC that reads slice fields. Measure
24
+ * end-to-end before adopting in code paths where consumers do heavy
25
+ * field access.
26
+ */
27
+ static fromBuffer(buffer: Buffer): Slice;
13
28
  constructor(buffer?: Buffer<ArrayBufferLike>, byteOffset?: number, byteLength?: number, maxByteLength?: number);
14
29
  reset(): void;
15
30
  get length(): number;
package/lib/index.js CHANGED
@@ -18,6 +18,30 @@ export class Slice {
18
18
  return EMPTY_BUF
19
19
  }
20
20
 
21
+ /**
22
+ * Fast factory for the common case of wrapping a full Buffer with no
23
+ * offset and no separate maxByteLength. Skips all the constructor's
24
+ * validation (Buffer instanceof check, byteOffset/byteLength range
25
+ * checks, integer checks, maxByteLength bounds). Use only when the
26
+ * caller already knows the input is a Buffer — e.g. wrapping freshly
27
+ * received network frames on a hot path.
28
+ *
29
+ * Note: instances produced here have a slightly different V8 hidden
30
+ * class than instances produced via `new Slice(buf)`. That introduces
31
+ * polymorphism at any downstream IC that reads slice fields. Measure
32
+ * end-to-end before adopting in code paths where consumers do heavy
33
+ * field access.
34
+ */
35
+ static fromBuffer(buffer ) {
36
+ const slice = Object.create(Slice.prototype)
37
+ const len = buffer.byteLength
38
+ slice.buffer = buffer
39
+ slice.byteOffset = 0
40
+ slice.byteLength = len
41
+ slice.maxByteLength = len
42
+ return slice
43
+ }
44
+
21
45
  constructor(
22
46
  buffer = Slice.EMPTY_BUF,
23
47
  byteOffset = 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/slice",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -30,5 +30,5 @@
30
30
  "tsd": "^0.33.0",
31
31
  "typescript": "^5.9.3"
32
32
  },
33
- "gitHead": "7c9c7457c885c644c7a1e70ef894d4727ce240d6"
33
+ "gitHead": "088f6e6d615d4eeee0b2e317c9011dfa57308cc9"
34
34
  }