extended-buffer 7.1.1 → 7.2.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.
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type { ExtendedBufferOptions } from './ExtendedBufferOptions';
3
- export declare class ExtendedBuffer {
3
+ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = ExtendedBufferOptions> {
4
4
  protected _pointer: number;
5
5
  protected _pointerEnd: number;
6
6
  protected _pointerStart: number;
@@ -10,7 +10,8 @@ export declare class ExtendedBuffer {
10
10
  protected readonly _nativeAllocSlow?: boolean;
11
11
  protected readonly _nativeReallocSlow?: boolean;
12
12
  constructor(options?: ExtendedBufferOptions);
13
- protected createInstance(options?: ExtendedBufferOptions): this;
13
+ protected createInstanceOptions(options?: EBO): ExtendedBufferOptions;
14
+ protected createInstance(options?: EBO): this;
14
15
  get length(): number;
15
16
  get capacity(): number;
16
17
  get pointer(): number;
@@ -33,7 +34,7 @@ export declare class ExtendedBuffer {
33
34
  getPointer(): number;
34
35
  offset(offset: number): this;
35
36
  isReadable(size: number): boolean;
36
- writeBuffer(value: Buffer | ExtendedBuffer, unshift?: boolean): this;
37
+ writeBuffer(value: Buffer | ExtendedBuffer<ExtendedBufferOptions>, unshift?: boolean): this;
37
38
  writeString(string: string, encoding?: BufferEncoding, unshift?: boolean): this;
38
39
  writeIntBE(value: number, size: number, unshift?: boolean): this;
39
40
  writeIntLE(value: number, size: number, unshift?: boolean): this;
@@ -55,7 +56,7 @@ export declare class ExtendedBuffer {
55
56
  writeDoubleLE(value: number, unshift?: boolean): this;
56
57
  readBuffer(size: number): this;
57
58
  readBuffer(size: number, asNative: true): Buffer;
58
- readBuffer(size: number, asNative: false, bufferOptions?: ExtendedBufferOptions): this;
59
+ readBuffer(size: number, asNative: false, bufferOptions?: EBO): this;
59
60
  readString(size: number, encoding?: BufferEncoding): string;
60
61
  readIntBE(size: number): number;
61
62
  readIntLE(size: number): number;
@@ -27,22 +27,30 @@ exports.ExtendedBuffer = void 0;
27
27
  const buffer_1 = require("buffer");
28
28
  const utils = __importStar(require("./utils"));
29
29
  const errors_1 = require("./errors");
30
- const defaultCapacity = 16 * 1024;
31
- const defaultCapacityStep = defaultCapacity;
30
+ const DEFAULT_CAPACITY = 16 * 1024;
31
+ const DEFAULT_CAPACITY_STEP = DEFAULT_CAPACITY;
32
32
  class ExtendedBuffer {
33
33
  constructor(options) {
34
34
  var _a, _b;
35
35
  this._nativeAllocSlow = options === null || options === void 0 ? void 0 : options.nativeAllocSlow;
36
36
  this._nativeReallocSlow = options === null || options === void 0 ? void 0 : options.nativeReallocSlow;
37
- this._capacity = (_a = options === null || options === void 0 ? void 0 : options.capacity) !== null && _a !== void 0 ? _a : defaultCapacity;
38
- this._capacityStep = (_b = options === null || options === void 0 ? void 0 : options.capacityStep) !== null && _b !== void 0 ? _b : defaultCapacityStep;
37
+ this._capacity = (_a = options === null || options === void 0 ? void 0 : options.capacity) !== null && _a !== void 0 ? _a : DEFAULT_CAPACITY;
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
41
  this.initExtendedBuffer();
42
42
  }
43
+ createInstanceOptions(options) {
44
+ return Object.assign({
45
+ capacity: this._capacity,
46
+ capacityStep: this._capacityStep,
47
+ nativeAllocSlow: this._nativeAllocSlow,
48
+ nativeReallocSlow: this._nativeReallocSlow
49
+ }, options !== null && options !== void 0 ? options : {});
50
+ }
43
51
  createInstance(options) {
44
52
  const ThisClass = this.constructor;
45
- return new ThisClass(options);
53
+ return new ThisClass(this.createInstanceOptions(options));
46
54
  }
47
55
  get length() {
48
56
  return this._pointerEnd - this._pointerStart;
@@ -189,10 +197,10 @@ class ExtendedBuffer {
189
197
  return this.getReadableSize() >= size;
190
198
  }
191
199
  writeBuffer(value, unshift) {
192
- if (value instanceof buffer_1.Buffer) {
193
- return this.writeNativeBuffer(value, unshift);
200
+ if (value instanceof ExtendedBuffer) {
201
+ return this.writeNativeBuffer(value.nativeBufferView, unshift);
194
202
  }
195
- return this.writeNativeBuffer(value.nativeBufferView, unshift);
203
+ return this.writeNativeBuffer(value, unshift);
196
204
  }
197
205
  writeString(string, encoding, unshift) {
198
206
  const bytes = buffer_1.Buffer.from(string, encoding);
@@ -345,20 +353,8 @@ class ExtendedBuffer {
345
353
  if (!this.isReadable(size)) {
346
354
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
347
355
  }
348
- let result;
349
- const buffer = utils.nativeBufferSubarray(this._nativeBuffer, this.nativePointer(), this.nativePointer() + size);
350
- if (asNative) {
351
- result = buffer_1.Buffer.from(buffer);
352
- }
353
- else {
354
- bufferOptions = Object.assign({
355
- capacity: this._capacity,
356
- capacityStep: this._capacityStep,
357
- nativeAllocSlow: this._nativeAllocSlow,
358
- nativeReallocSlow: this._nativeReallocSlow
359
- }, bufferOptions !== null && bufferOptions !== void 0 ? bufferOptions : {});
360
- result = this.createInstance(bufferOptions).writeNativeBuffer(buffer);
361
- }
356
+ const nativeBuffer = utils.nativeBufferSubarray(this._nativeBuffer, this.nativePointer(), this.nativePointer() + size);
357
+ const result = asNative ? buffer_1.Buffer.from(nativeBuffer) : this.createInstance(bufferOptions).writeNativeBuffer(nativeBuffer);
362
358
  this.offset(size);
363
359
  return result;
364
360
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extended-buffer",
3
- "version": "7.1.1",
3
+ "version": "7.2.0",
4
4
  "description": "Node JS extended Buffer",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",