extended-buffer 7.3.0 → 7.4.0

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/README.md CHANGED
@@ -44,12 +44,37 @@ The buffer stores a contiguous region of bytes. A separate **read pointer** trac
44
44
  ### Views
45
45
 
46
46
  - `nativeBufferView` — a `Buffer` view of **all stored bytes** (from the start of stored data to the end).
47
- - If you need only unread bytes, you can derive it:
47
+ - `bufferView` a **new `ExtendedBuffer` instance** that maps to the same bytes as `nativeBufferView` (**zero-copy**).
48
+ The new instance starts with `pointer = 0`, so you can read/parse without consuming the parent buffer.
49
+
50
+ If you need only unread bytes, you can derive it:
48
51
 
49
52
  ```ts
50
53
  const unread = b.nativeBufferView.subarray(b.pointer);
51
54
  ```
52
55
 
56
+ Example: parse without touching the parent pointer:
57
+
58
+ ```ts
59
+ import { ExtendedBuffer } from 'extended-buffer';
60
+
61
+ const b = new ExtendedBuffer();
62
+ b.writeString("OK");
63
+ b.writeUInt16BE(1337);
64
+
65
+ const v = b.bufferView;
66
+ console.log(v.readString(2)); // "OK"
67
+ console.log(v.readUInt16BE()); // 1337
68
+
69
+ console.log(b.pointer); // 0 (parent is untouched)
70
+ ```
71
+
72
+ Notes:
73
+
74
+ - `bufferView` shares memory with the parent. In-place mutations (e.g. `v.nativeBufferView[0] = 0xff`) will be visible in both.
75
+ - The view usually has no spare head/tail capacity, so calling `v.write*()` will likely trigger a reallocation (copy) and then the two instances will diverge.
76
+
77
+
53
78
  ---
54
79
 
55
80
  ## Construction and options
@@ -60,6 +85,7 @@ type ExtendedBufferOptions = {
60
85
  capacityStep?: number; // how much to grow when resizing
61
86
  nativeAllocSlow?: boolean; // using Buffer.allocUnsafeSlow() when initializing ExtendedBuffer
62
87
  nativeReallocSlow?: boolean; // using Buffer.allocUnsafeSlow() for further reallocations
88
+ initNativeBuffer?: Buffer; // use an existing Buffer as the initial native buffer (no copy)
63
89
  };
64
90
  ```
65
91
 
@@ -81,6 +107,34 @@ const b = new ExtendedBuffer({
81
107
  });
82
108
  ```
83
109
 
110
+ ### Wrap an existing `Buffer` (`initNativeBuffer`)
111
+
112
+ If you already have a Node.js `Buffer` (from a socket, file, etc.) and want to parse it with `ExtendedBuffer`
113
+ **without copying**, pass it as `initNativeBuffer`.
114
+
115
+ - The buffer is **not copied** — it becomes the internal `_nativeBuffer`.
116
+ - The instance starts with `pointer = 0` and `length = initNativeBuffer.length`.
117
+
118
+ ```ts
119
+ import { ExtendedBuffer } from 'extended-buffer';
120
+
121
+ const packet = Buffer.from([0x00, 0x02, 0x4f, 0x4b]); // 2, "OK"
122
+
123
+ const b = new ExtendedBuffer({ initNativeBuffer: packet });
124
+
125
+ const len = b.readUInt16BE();
126
+ console.log(b.readString(len)); // "OK"
127
+ ```
128
+
129
+ You can also reuse an instance:
130
+
131
+ ```ts
132
+ b.initExtendedBuffer(packet);
133
+ ```
134
+
135
+ Note: when you construct from `initNativeBuffer`, the buffer is treated as already filled.
136
+ If you later call `write*()`, it will typically require a reallocation (copy) to make room.
137
+
84
138
  ---
85
139
 
86
140
  ## Writing data
@@ -357,6 +411,7 @@ The library defines these error classes:
357
411
  - `ExtendedBufferError`
358
412
  - `ExtendedBufferTypeError`
359
413
  - `ExtendedBufferRangeError`
414
+ - `ExtendedBufferUnsupportedError`
360
415
 
361
416
  Common error codes you may see:
362
417
 
@@ -365,6 +420,7 @@ Common error codes you may see:
365
420
  - `INVALID_INTEGER_SIZE_VALUE_TYPE`: size is not a safe integer
366
421
  - `INVALID_INTEGER_SIZE_VALUE_RANGE`: integer size not in `1…6`
367
422
  - `INVALID_INSTANCE_STATE`: internal invariant check failed
423
+ - `INVALID_BUFFER_TYPE`: attempt write invalid buffer type
368
424
  - `VALUE_MUST_BE_AN_INTEGER`: value not a safe integer
369
425
  - `VALUE_MUST_BE_AN_UNSIGNED_INTEGER`: value is not a safe integer or less than 0
370
426
  - `VALUE_MUST_BE_AN_BIG_INTEGER`: value is not a `bigint`
@@ -398,10 +454,10 @@ In non-Node runtimes, `global` may not exist.
398
454
  ## Reference: full public API (names)
399
455
 
400
456
  Properties:
401
- - `length`, `capacity`, `pointer`, `nativeBufferView`
457
+ - `length`, `capacity`, `pointer`, `nativeBufferView`, `bufferView`
402
458
 
403
459
  Core:
404
- - `initExtendedBuffer()`, `assertInstanceState()`, `clean()`
460
+ - `initExtendedBuffer(initNativeBuffer?)`, `assertInstanceState()`, `clean()`
405
461
  - `nativePointer()`, `getWritableSizeStart()`, `getWritableSizeEnd()`, `getWritableSize()`, `getReadableSize()`
406
462
  - `transaction(callback)`
407
463
  - `allocStart(size)`, `allocEnd(size)`
@@ -419,3 +475,7 @@ Numbers:
419
475
  `readInt16BE/LE`, `readUInt16BE/LE`, `readInt32BE/LE`, `readUInt32BE/LE`,
420
476
  `readBigInt64BE/LE`, `readBigUInt64BE/LE`,
421
477
  `readFloatBE/LE`, `readDoubleBE/LE`
478
+
479
+ ## License
480
+
481
+ MIT
@@ -18,7 +18,8 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
18
18
  get capacity(): number;
19
19
  get pointer(): number;
20
20
  get nativeBufferView(): Buffer;
21
- initExtendedBuffer(): this;
21
+ get bufferView(): this;
22
+ initExtendedBuffer(initNativeBuffer?: Buffer): this;
22
23
  assertInstanceState(): this;
23
24
  clean(): this;
24
25
  nativePointer(): number;
@@ -38,7 +38,7 @@ class ExtendedBuffer {
38
38
  this._capacityStep = (_b = options === null || options === void 0 ? void 0 : options.capacityStep) !== null && _b !== void 0 ? _b : DEFAULT_CAPACITY_STEP;
39
39
  utils.assertUnsignedInteger(this._capacity);
40
40
  utils.assertUnsignedInteger(this._capacityStep);
41
- this.initExtendedBuffer();
41
+ this.initExtendedBuffer(options === null || options === void 0 ? void 0 : options.initNativeBuffer);
42
42
  }
43
43
  createInstanceOptions(options) {
44
44
  return Object.assign({
@@ -64,15 +64,27 @@ class ExtendedBuffer {
64
64
  get nativeBufferView() {
65
65
  return utils.nativeBufferSubarray(this._nativeBuffer, this._pointerStart, this._pointerEnd);
66
66
  }
67
- initExtendedBuffer() {
67
+ get bufferView() {
68
+ return this.createInstance({
69
+ initNativeBuffer: this.nativeBufferView
70
+ });
71
+ }
72
+ initExtendedBuffer(initNativeBuffer) {
68
73
  this._pointer = 0;
69
- this._nativeBuffer = utils.allocNativeBuffer(this._capacity, this._nativeAllocSlow);
70
- this._pointerStart = this._pointerEnd = Math.floor(this._capacity / 2);
74
+ if (initNativeBuffer) {
75
+ this._nativeBuffer = initNativeBuffer;
76
+ this._pointerStart = 0;
77
+ this._pointerEnd = this._nativeBuffer.length;
78
+ }
79
+ else {
80
+ this._nativeBuffer = utils.allocNativeBuffer(this._capacity, this._nativeAllocSlow);
81
+ this._pointerStart = this._pointerEnd = Math.floor(this._capacity / 2);
82
+ }
71
83
  this.assertInstanceState();
72
84
  return this;
73
85
  }
74
86
  assertInstanceState() {
75
- if (!this._nativeBuffer ||
87
+ if (!buffer_1.Buffer.isBuffer(this._nativeBuffer) ||
76
88
  !Number.isSafeInteger(this._pointer) ||
77
89
  !Number.isSafeInteger(this._pointerStart) ||
78
90
  !Number.isSafeInteger(this._pointerEnd) ||
@@ -229,7 +241,10 @@ class ExtendedBuffer {
229
241
  if (value instanceof ExtendedBuffer) {
230
242
  return this.writeNativeBuffer(value.nativeBufferView, unshift);
231
243
  }
232
- return this.writeNativeBuffer(value, unshift);
244
+ if (buffer_1.Buffer.isBuffer(value)) {
245
+ return this.writeNativeBuffer(value, unshift);
246
+ }
247
+ throw new errors_1.ExtendedBufferTypeError('INVALID_BUFFER_TYPE');
233
248
  }
234
249
  writeString(string, encoding, unshift) {
235
250
  const bytes = buffer_1.Buffer.from(string, encoding);
@@ -1,6 +1,8 @@
1
+ /// <reference types="node" />
1
2
  export type ExtendedBufferOptions = {
2
3
  capacity?: number;
3
4
  capacityStep?: number;
4
5
  nativeAllocSlow?: boolean;
5
6
  nativeReallocSlow?: boolean;
7
+ initNativeBuffer?: Buffer;
6
8
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extended-buffer",
3
- "version": "7.3.0",
3
+ "version": "7.4.0",
4
4
  "description": "Node JS extended Buffer",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",