extended-buffer 7.0.2 → 7.1.1

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017 Sality
3
+ Copyright (c) 2025 mvcbox
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![npm version](https://badge.fury.io/js/extended-buffer.svg?flush_cache=v7_0_2)](https://badge.fury.io/js/extended-buffer)
1
+ [![npm version](https://badge.fury.io/js/extended-buffer.svg)](https://badge.fury.io/js/extended-buffer)
2
2
 
3
3
  # ExtendedBuffer
4
4
 
@@ -56,20 +56,29 @@ const unread = b.nativeBufferView.subarray(b.pointer);
56
56
 
57
57
  ```ts
58
58
  type ExtendedBufferOptions = {
59
- capacity?: number; // initial native buffer size (bytes)
60
- capacityStep?: number; // how much to grow when resizing
59
+ capacity?: number; // initial native buffer size (bytes)
60
+ capacityStep?: number; // how much to grow when resizing
61
+ nativeAllocSlow?: boolean; // using Buffer.allocUnsafeSlow() when initializing ExtendedBuffer
62
+ nativeReallocSlow?: boolean; // using Buffer.allocUnsafeSlow() for further reallocations
61
63
  };
62
64
  ```
63
65
 
64
- Defaults (from source):
66
+ Default values:
65
67
 
66
68
  - `capacity`: `16 * 1024` bytes (16 KiB)
67
69
  - `capacityStep`: same as `capacity`
70
+ - `nativeAllocSlow`: `false`
71
+ - `nativeReallocSlow`: `false`
68
72
 
69
73
  Example:
70
74
 
71
75
  ```ts
72
- const b = new ExtendedBuffer({ capacity: 64 * 1024, capacityStep: 16 * 1024 });
76
+ const b = new ExtendedBuffer({
77
+ capacity: 1024 * 1024,
78
+ capacityStep: 1024 * 1024,
79
+ nativeAllocSlow: true,
80
+ nativeReallocSlow: true
81
+ });
73
82
  ```
74
83
 
75
84
  ---
@@ -137,7 +146,7 @@ if (b.isReadable(4)) {
137
146
  // Copy out as a native Buffer
138
147
  const chunk: Buffer = b.readBuffer(10, true);
139
148
 
140
- // Copy out as a new ExtendedBuffer (same capacity/capacityStep by default)
149
+ // Copy out as a new ExtendedBuffer (same capacity/capacityStep/nativeAllocSlow/nativeReallocSlow by default)
141
150
  const eb: ExtendedBuffer = b.readBuffer(10);
142
151
  ```
143
152
 
@@ -232,11 +241,14 @@ The library defines these error classes:
232
241
 
233
242
  Common error codes you may see:
234
243
 
235
- - `SIZE_OUT_OF_RANGE` (reading more bytes than available)
236
- - `POINTER_OUT_OF_RANGE` (setting pointer outside `0…length`)
237
- - `INVALID_INTEGER_SIZE_VALUE_RANGE` (integer size not in `1…6`)
238
- - `EXCEEDING_MAXIMUM_BUFFER_SIZE` (allocation exceeds Node’s `kMaxLength` or `os.totalmem()`)
239
- - `INVALID_INSTANCE_STATE` (internal invariant check failed)
244
+ - `SIZE_OUT_OF_RANGE`: reading more bytes than available
245
+ - `POINTER_OUT_OF_RANGE`: setting pointer outside `0…length`
246
+ - `INVALID_INTEGER_SIZE_VALUE_TYPE`: size is not a safe integer
247
+ - `INVALID_INTEGER_SIZE_VALUE_RANGE`: integer size not in `1…6`
248
+ - `INVALID_INSTANCE_STATE`: internal invariant check failed
249
+ - `VALUE_MUST_BE_AN_INTEGER`: value not a safe integer
250
+ - `VALUE_MUST_BE_AN_UNSIGNED_INTEGER`: value is not a safe integer or less than 0
251
+ - `EXCEEDING_MAXIMUM_BUFFER_SIZE`: allocation exceeds Node’s `kMaxLength` or `os.totalmem()`
240
252
 
241
253
  ---
242
254
 
@@ -7,6 +7,8 @@ export declare class ExtendedBuffer {
7
7
  protected _nativeBuffer: Buffer;
8
8
  protected readonly _capacity: number;
9
9
  protected readonly _capacityStep: number;
10
+ protected readonly _nativeAllocSlow?: boolean;
11
+ protected readonly _nativeReallocSlow?: boolean;
10
12
  constructor(options?: ExtendedBufferOptions);
11
13
  protected createInstance(options?: ExtendedBufferOptions): this;
12
14
  get length(): number;
@@ -1,14 +1,39 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.ExtendedBuffer = void 0;
4
27
  const buffer_1 = require("buffer");
5
- const utils = require("./utils");
28
+ const utils = __importStar(require("./utils"));
6
29
  const errors_1 = require("./errors");
7
30
  const defaultCapacity = 16 * 1024;
8
31
  const defaultCapacityStep = defaultCapacity;
9
32
  class ExtendedBuffer {
10
33
  constructor(options) {
11
34
  var _a, _b;
35
+ this._nativeAllocSlow = options === null || options === void 0 ? void 0 : options.nativeAllocSlow;
36
+ this._nativeReallocSlow = options === null || options === void 0 ? void 0 : options.nativeReallocSlow;
12
37
  this._capacity = (_a = options === null || options === void 0 ? void 0 : options.capacity) !== null && _a !== void 0 ? _a : defaultCapacity;
13
38
  this._capacityStep = (_b = options === null || options === void 0 ? void 0 : options.capacityStep) !== null && _b !== void 0 ? _b : defaultCapacityStep;
14
39
  utils.assertUnsignedInteger(this._capacity);
@@ -33,7 +58,7 @@ class ExtendedBuffer {
33
58
  }
34
59
  initExtendedBuffer() {
35
60
  this._pointer = 0;
36
- this._nativeBuffer = utils.allocNativeBuffer(this._capacity);
61
+ this._nativeBuffer = utils.allocNativeBuffer(this._capacity, this._nativeAllocSlow);
37
62
  this._pointerStart = this._pointerEnd = Math.floor(this._capacity / 2);
38
63
  this.assertInstanceState();
39
64
  return this;
@@ -79,7 +104,7 @@ class ExtendedBuffer {
79
104
  }
80
105
  if (this.getWritableSize() < size) {
81
106
  const newSize = this._nativeBuffer.length + (size - this.getWritableSize()) + this._capacityStep;
82
- this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize);
107
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize, this._nativeReallocSlow);
83
108
  }
84
109
  const offset = Math.floor((this.getWritableSize() - size) / 2) + size - this._pointerStart;
85
110
  this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
@@ -95,7 +120,7 @@ class ExtendedBuffer {
95
120
  }
96
121
  if (this.getWritableSize() < size) {
97
122
  const newSize = this._nativeBuffer.length + (size - this.getWritableSize()) + this._capacityStep;
98
- this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize);
123
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newSize, this._nativeReallocSlow);
99
124
  }
100
125
  const offset = this._nativeBuffer.length - Math.floor((this.getWritableSize() - size) / 2) - size - this._pointerEnd;
101
126
  this._nativeBuffer.copy(this._nativeBuffer, this._pointerStart + offset, this._pointerStart, this._pointerEnd);
@@ -127,7 +152,7 @@ class ExtendedBuffer {
127
152
  const reduceSize = freeSize - this._capacityStep;
128
153
  const newNativeSize = this._nativeBuffer.length - reduceSize;
129
154
  this.allocEnd(reduceSize);
130
- this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newNativeSize);
155
+ this._nativeBuffer = utils.reallocNativeBuffer(this._nativeBuffer, newNativeSize, this._nativeReallocSlow);
131
156
  }
132
157
  return this;
133
158
  }
@@ -175,7 +200,6 @@ class ExtendedBuffer {
175
200
  }
176
201
  writeIntBE(value, size, unshift) {
177
202
  utils.assertInteger(value);
178
- utils.assertUnsignedInteger(size);
179
203
  utils.assertIntegerSize(size);
180
204
  if (unshift) {
181
205
  this.allocStart(size);
@@ -191,7 +215,6 @@ class ExtendedBuffer {
191
215
  }
192
216
  writeIntLE(value, size, unshift) {
193
217
  utils.assertInteger(value);
194
- utils.assertUnsignedInteger(size);
195
218
  utils.assertIntegerSize(size);
196
219
  if (unshift) {
197
220
  this.allocStart(size);
@@ -207,7 +230,6 @@ class ExtendedBuffer {
207
230
  }
208
231
  writeUIntBE(value, size, unshift) {
209
232
  utils.assertUnsignedInteger(value);
210
- utils.assertUnsignedInteger(size);
211
233
  utils.assertIntegerSize(size);
212
234
  if (unshift) {
213
235
  this.allocStart(size);
@@ -223,7 +245,6 @@ class ExtendedBuffer {
223
245
  }
224
246
  writeUIntLE(value, size, unshift) {
225
247
  utils.assertUnsignedInteger(value);
226
- utils.assertUnsignedInteger(size);
227
248
  utils.assertIntegerSize(size);
228
249
  if (unshift) {
229
250
  this.allocStart(size);
@@ -332,7 +353,9 @@ class ExtendedBuffer {
332
353
  else {
333
354
  bufferOptions = Object.assign({
334
355
  capacity: this._capacity,
335
- capacityStep: this._capacityStep
356
+ capacityStep: this._capacityStep,
357
+ nativeAllocSlow: this._nativeAllocSlow,
358
+ nativeReallocSlow: this._nativeReallocSlow
336
359
  }, bufferOptions !== null && bufferOptions !== void 0 ? bufferOptions : {});
337
360
  result = this.createInstance(bufferOptions).writeNativeBuffer(buffer);
338
361
  }
@@ -349,7 +372,6 @@ class ExtendedBuffer {
349
372
  return result;
350
373
  }
351
374
  readIntBE(size) {
352
- utils.assertUnsignedInteger(size);
353
375
  utils.assertIntegerSize(size);
354
376
  if (!this.isReadable(size)) {
355
377
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
@@ -359,7 +381,6 @@ class ExtendedBuffer {
359
381
  return result;
360
382
  }
361
383
  readIntLE(size) {
362
- utils.assertUnsignedInteger(size);
363
384
  utils.assertIntegerSize(size);
364
385
  if (!this.isReadable(size)) {
365
386
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
@@ -369,7 +390,6 @@ class ExtendedBuffer {
369
390
  return result;
370
391
  }
371
392
  readUIntBE(size) {
372
- utils.assertUnsignedInteger(size);
373
393
  utils.assertIntegerSize(size);
374
394
  if (!this.isReadable(size)) {
375
395
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
@@ -379,7 +399,6 @@ class ExtendedBuffer {
379
399
  return result;
380
400
  }
381
401
  readUIntLE(size) {
382
- utils.assertUnsignedInteger(size);
383
402
  utils.assertIntegerSize(size);
384
403
  if (!this.isReadable(size)) {
385
404
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
@@ -1,4 +1,6 @@
1
1
  export type ExtendedBufferOptions = {
2
2
  capacity?: number;
3
3
  capacityStep?: number;
4
+ nativeAllocSlow?: boolean;
5
+ nativeReallocSlow?: boolean;
4
6
  };
@@ -1,2 +1,3 @@
1
1
  export declare class ExtendedBufferError extends Error {
2
+ constructor(message?: string);
2
3
  }
@@ -2,5 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExtendedBufferError = void 0;
4
4
  class ExtendedBufferError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = new.target.name;
8
+ if (Error.captureStackTrace) {
9
+ Error.captureStackTrace(this, new.target);
10
+ }
11
+ }
5
12
  }
6
13
  exports.ExtendedBufferError = ExtendedBufferError;
@@ -1,2 +1,2 @@
1
1
  /// <reference types="node" />
2
- export declare function allocNativeBuffer(size: number): Buffer;
2
+ export declare function allocNativeBuffer(size: number, allocSlow?: boolean): Buffer;
@@ -1,17 +1,40 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
26
  exports.allocNativeBuffer = void 0;
4
- const os = require("os");
27
+ const os = __importStar(require("os"));
5
28
  const buffer_1 = require("buffer");
6
29
  const errors_1 = require("../errors");
7
30
  const assert_unsigned_integer_1 = require("./assert-unsigned-integer");
8
31
  const maxBufferSize = Math.min(buffer_1.kMaxLength, os.totalmem());
9
32
  (0, assert_unsigned_integer_1.assertUnsignedInteger)(maxBufferSize);
10
- function allocNativeBuffer(size) {
33
+ function allocNativeBuffer(size, allocSlow) {
11
34
  (0, assert_unsigned_integer_1.assertUnsignedInteger)(size);
12
35
  if (size > maxBufferSize) {
13
36
  throw new errors_1.ExtendedBufferRangeError('EXCEEDING_MAXIMUM_BUFFER_SIZE');
14
37
  }
15
- return buffer_1.Buffer.allocUnsafe(size);
38
+ return allocSlow ? buffer_1.Buffer.allocUnsafeSlow(size) : buffer_1.Buffer.allocUnsafe(size);
16
39
  }
17
40
  exports.allocNativeBuffer = allocNativeBuffer;
@@ -1,2 +1,2 @@
1
1
  /// <reference types="node" />
2
- export declare function reallocNativeBuffer(buffer: Buffer, newSize: number): Buffer;
2
+ export declare function reallocNativeBuffer(buffer: Buffer, newSize: number, allocSlow?: boolean): Buffer;
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.reallocNativeBuffer = void 0;
4
4
  const alloc_native_buffer_1 = require("./alloc-native-buffer");
5
5
  const assert_unsigned_integer_1 = require("./assert-unsigned-integer");
6
- function reallocNativeBuffer(buffer, newSize) {
6
+ function reallocNativeBuffer(buffer, newSize, allocSlow) {
7
7
  (0, assert_unsigned_integer_1.assertUnsignedInteger)(newSize);
8
8
  if (buffer.length === newSize) {
9
9
  return buffer;
10
10
  }
11
- const newBuffer = (0, alloc_native_buffer_1.allocNativeBuffer)(newSize);
11
+ const newBuffer = (0, alloc_native_buffer_1.allocNativeBuffer)(newSize, allocSlow);
12
12
  buffer.copy(newBuffer, 0, 0, Math.min(buffer.length, newBuffer.length));
13
13
  return newBuffer;
14
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extended-buffer",
3
- "version": "7.0.2",
3
+ "version": "7.1.1",
4
4
  "description": "Node JS extended Buffer",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -9,6 +9,7 @@
9
9
  ".": {
10
10
  "types": "./dist/index.d.ts",
11
11
  "require": "./dist/index.js",
12
+ "import": "./dist/index.js",
12
13
  "default": "./dist/index.js"
13
14
  }
14
15
  },