extended-buffer 7.2.1 → 7.3.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
@@ -120,6 +120,28 @@ Fixed-width helpers:
120
120
  - `writeInt16BE`, `writeInt16LE`, `writeUInt16BE`, `writeUInt16LE`
121
121
  - `writeInt32BE`, `writeInt32LE`, `writeUInt32BE`, `writeUInt32LE`
122
122
 
123
+ ### BigInt (64-bit integers)
124
+
125
+ If your runtime supports `BigInt` and Node's `Buffer.readBig*` / `Buffer.writeBig*` APIs, you can read/write 64-bit integers as `bigint` values (always **8 bytes**):
126
+
127
+ - `writeBigInt64BE`, `writeBigInt64LE` — signed 64-bit
128
+ - `writeBigUInt64BE`, `writeBigUInt64LE` — unsigned 64-bit
129
+
130
+ ```ts
131
+ import { ExtendedBuffer } from 'extended-buffer';
132
+
133
+ const b = new ExtendedBuffer();
134
+
135
+ b.writeBigUInt64BE(2n ** 63n); // 9223372036854775808n
136
+ b.writeBigInt64LE(-42n);
137
+
138
+ b.setPointer(0);
139
+ console.log(b.readBigUInt64BE()); // 9223372036854775808n
140
+ console.log(b.readBigInt64LE()); // -42n
141
+ ```
142
+
143
+ If `BigInt` is not supported, these methods throw `ExtendedBufferUnsupportedError('EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT')`.
144
+
123
145
  ### Floating point
124
146
 
125
147
  - `writeFloatBE`, `writeFloatLE` (4 bytes)
@@ -171,6 +193,24 @@ Fixed-width helpers:
171
193
  - `readInt16BE`, `readInt16LE`, `readUInt16BE`, `readUInt16LE`
172
194
  - `readInt32BE`, `readInt32LE`, `readUInt32BE`, `readUInt32LE`
173
195
 
196
+ ### BigInt (64-bit integers)
197
+
198
+ - `readBigInt64BE`, `readBigInt64LE` — signed 64-bit
199
+ - `readBigUInt64BE`, `readBigUInt64LE` — unsigned 64-bit
200
+
201
+ ```ts
202
+ const b = new ExtendedBuffer();
203
+
204
+ b.writeBigInt64BE(-1n);
205
+ b.writeBigUInt64BE(18446744073709551615n); // 2^64 - 1
206
+
207
+ b.setPointer(0);
208
+ console.log(b.readBigInt64BE()); // -1n
209
+ console.log(b.readBigUInt64BE()); // 18446744073709551615n
210
+ ```
211
+
212
+ Note: Node's `Buffer` will throw a native `RangeError` if the value doesn't fit into signed/unsigned 64-bit range.
213
+
174
214
  ### Floating point
175
215
 
176
216
  - `readFloatBE`, `readFloatLE`
@@ -202,6 +242,85 @@ If you try to set the pointer outside `[0, length]`, it throws
202
242
 
203
243
  ---
204
244
 
245
+ ## Transactions (atomic changes)
246
+
247
+ Sometimes you want to perform a **multi-step read/write** and either:
248
+
249
+ - **commit** everything if it succeeds, or
250
+ - **rollback** the buffer to the exact previous state if something fails.
251
+
252
+ `ExtendedBuffer.transaction()` wraps your code in a transaction:
253
+
254
+ ```ts
255
+ const result = b.transaction(() => {
256
+ // any reads/writes/offsets/etc.
257
+ return 123;
258
+ });
259
+ ```
260
+
261
+ Rules:
262
+
263
+ - If the callback **returns normally**, changes are kept (committed).
264
+ - If the callback **throws**, the buffer is restored (rolled back) and the error is re-thrown.
265
+ - Transactions are **re-entrant**: nested `transaction()` calls do not create extra snapshots.
266
+
267
+ What gets rolled back:
268
+
269
+ - stored payload bytes
270
+ - `pointer` (read pointer)
271
+ - internal start/end offsets and the original native `Buffer` (even if the buffer was reallocated during the callback)
272
+
273
+ ### Example: "try parse" without consuming bytes
274
+
275
+ This is useful for protocols where you might receive partial data and want to retry later.
276
+
277
+ ```ts
278
+ import { ExtendedBuffer } from 'extended-buffer';
279
+
280
+ function tryReadFrame(b: ExtendedBuffer): Buffer | null {
281
+ try {
282
+ return b.transaction(() => {
283
+ // (1) read header
284
+ const len = b.readUInt16BE();
285
+
286
+ // (2) not enough bytes yet -> rollback and let the caller wait for more data
287
+ if (!b.isReadable(len)) {
288
+ throw new Error('INCOMPLETE_FRAME');
289
+ }
290
+
291
+ // (3) success -> commit
292
+ return b.readBuffer(len, true);
293
+ });
294
+ } catch {
295
+ return null;
296
+ }
297
+ }
298
+ ```
299
+
300
+ ### Example: rollback on validation error
301
+
302
+ ```ts
303
+ b.transaction(() => {
304
+ const magic = b.readUInt32BE();
305
+ if (magic !== 0xdeadbeef) {
306
+ throw new Error('BAD_MAGIC');
307
+ }
308
+
309
+ const version = b.readUInt8();
310
+ if (version !== 1) {
311
+ throw new Error('UNSUPPORTED_VERSION');
312
+ }
313
+ });
314
+ ```
315
+
316
+ ### Performance note
317
+
318
+ `transaction()` snapshots the **current payload** (it copies the stored bytes) before running the callback.
319
+ That makes rollbacks safe, but can be expensive for very large buffers. Use it for small/medium payloads,
320
+ or when the safety/ergonomics is worth the extra copy.
321
+
322
+ ---
323
+
205
324
  ## Memory management
206
325
 
207
326
  ### Discard already-read data
@@ -248,6 +367,9 @@ Common error codes you may see:
248
367
  - `INVALID_INSTANCE_STATE`: internal invariant check failed
249
368
  - `VALUE_MUST_BE_AN_INTEGER`: value not a safe integer
250
369
  - `VALUE_MUST_BE_AN_UNSIGNED_INTEGER`: value is not a safe integer or less than 0
370
+ - `VALUE_MUST_BE_AN_BIG_INTEGER`: value is not a `bigint`
371
+ - `VALUE_MUST_BE_AN_UNSIGNED_BIG_INTEGER`: value is not a `bigint` or less than 0
372
+ - `EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT`: BigInt methods are not supported in the current runtime
251
373
  - `EXCEEDING_MAXIMUM_BUFFER_SIZE`: allocation exceeds Node’s `kMaxLength` or `os.totalmem()`
252
374
 
253
375
  ---
@@ -281,6 +403,7 @@ Properties:
281
403
  Core:
282
404
  - `initExtendedBuffer()`, `assertInstanceState()`, `clean()`
283
405
  - `nativePointer()`, `getWritableSizeStart()`, `getWritableSizeEnd()`, `getWritableSize()`, `getReadableSize()`
406
+ - `transaction(callback)`
284
407
  - `allocStart(size)`, `allocEnd(size)`
285
408
  - `writeNativeBuffer(buf, unshift?)`, `writeBuffer(bufOrEB, unshift?)`, `writeString(str, enc?, unshift?)`
286
409
  - Pointer: `setPointer(p)`, `getPointer()`, `offset(n)`, `isReadable(size)`
@@ -289,8 +412,10 @@ Core:
289
412
  Numbers:
290
413
  - Write: `writeIntBE/LE`, `writeUIntBE/LE`, `writeInt8`, `writeUInt8`,
291
414
  `writeInt16BE/LE`, `writeUInt16BE/LE`, `writeInt32BE/LE`, `writeUInt32BE/LE`,
415
+ `writeBigInt64BE/LE`, `writeBigUInt64BE/LE`,
292
416
  `writeFloatBE/LE`, `writeDoubleBE/LE`
293
417
  - Read: `readBuffer`, `readString`,
294
418
  `readIntBE/LE`, `readUIntBE/LE`, `readInt8`, `readUInt8`,
295
419
  `readInt16BE/LE`, `readUInt16BE/LE`, `readInt32BE/LE`, `readUInt32BE/LE`,
420
+ `readBigInt64BE/LE`, `readBigUInt64BE/LE`,
296
421
  `readFloatBE/LE`, `readDoubleBE/LE`
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import type { ExtendedBufferOptions } from './ExtendedBufferOptions';
3
+ import type { ExtendedBufferTransaction } from './ExtendedBufferTransaction';
3
4
  export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = ExtendedBufferOptions> {
4
5
  protected _pointer: number;
5
6
  protected _pointerEnd: number;
@@ -9,6 +10,7 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
9
10
  protected readonly _capacityStep: number;
10
11
  protected readonly _nativeAllocSlow?: boolean;
11
12
  protected readonly _nativeReallocSlow?: boolean;
13
+ protected _transaction?: ExtendedBufferTransaction;
12
14
  constructor(options?: ExtendedBufferOptions);
13
15
  protected createInstanceOptions(options?: ExtendedBufferOptions): ExtendedBufferOptions;
14
16
  protected createInstance(options?: EBO): this;
@@ -23,6 +25,7 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
23
25
  getWritableSizeStart(): number;
24
26
  getWritableSizeEnd(): number;
25
27
  getWritableSize(): number;
28
+ transaction<T>(callback: () => T): T;
26
29
  allocStart(size: number): this;
27
30
  allocEnd(size: number): this;
28
31
  getReadableSize(): number;
@@ -36,6 +39,7 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
36
39
  isReadable(size: number): boolean;
37
40
  writeBuffer(value: Buffer | ExtendedBuffer<ExtendedBufferOptions>, unshift?: boolean): this;
38
41
  writeString(string: string, encoding?: BufferEncoding, unshift?: boolean): this;
42
+ protected writeIntCommon(method: 'writeIntBE' | 'writeIntLE' | 'writeUIntBE' | 'writeUIntLE', value: number, size: number, unsigned: boolean, unshift?: boolean): this;
39
43
  writeIntBE(value: number, size: number, unshift?: boolean): this;
40
44
  writeIntLE(value: number, size: number, unshift?: boolean): this;
41
45
  writeUIntBE(value: number, size: number, unshift?: boolean): this;
@@ -50,6 +54,12 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
50
54
  writeInt32LE(value: number, unshift?: boolean): this;
51
55
  writeUInt32BE(value: number, unshift?: boolean): this;
52
56
  writeUInt32LE(value: number, unshift?: boolean): this;
57
+ protected writeBigInt64Common(method: 'writeBigInt64BE' | 'writeBigInt64LE' | 'writeBigUInt64BE' | 'writeBigUInt64LE', value: bigint, unsigned: boolean, unshift?: boolean): this;
58
+ writeBigInt64BE(value: bigint, unshift?: boolean): this;
59
+ writeBigInt64LE(value: bigint, unshift?: boolean): this;
60
+ writeBigUInt64BE(value: bigint, unshift?: boolean): this;
61
+ writeBigUInt64LE(value: bigint, unshift?: boolean): this;
62
+ protected writeFloatingPointCommon(method: 'writeFloatBE' | 'writeFloatLE' | 'writeDoubleBE' | 'writeDoubleLE', value: number, size: 4 | 8, unshift?: boolean): this;
53
63
  writeFloatBE(value: number, unshift?: boolean): this;
54
64
  writeFloatLE(value: number, unshift?: boolean): this;
55
65
  writeDoubleBE(value: number, unshift?: boolean): this;
@@ -58,6 +68,7 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
58
68
  readBuffer(size: number, asNative: true): Buffer;
59
69
  readBuffer(size: number, asNative: false, bufferOptions?: EBO): this;
60
70
  readString(size: number, encoding?: BufferEncoding): string;
71
+ protected readIntCommon(method: 'readIntBE' | 'readIntLE' | 'readUIntBE' | 'readUIntLE', size: number): number;
61
72
  readIntBE(size: number): number;
62
73
  readIntLE(size: number): number;
63
74
  readUIntBE(size: number): number;
@@ -72,6 +83,12 @@ export declare class ExtendedBuffer<EBO extends ExtendedBufferOptions = Extended
72
83
  readInt32LE(): number;
73
84
  readUInt32BE(): number;
74
85
  readUInt32LE(): number;
86
+ protected readBigInt64Common(method: 'readBigInt64BE' | 'readBigInt64LE' | 'readBigUInt64BE' | 'readBigUInt64LE'): bigint;
87
+ readBigInt64BE(): bigint;
88
+ readBigInt64LE(): bigint;
89
+ readBigUInt64BE(): bigint;
90
+ readBigUInt64LE(): bigint;
91
+ protected readFloatingPointCommon(method: 'readFloatBE' | 'readFloatLE' | 'readDoubleBE' | 'readDoubleLE', size: 4 | 8): number;
75
92
  readFloatBE(): number;
76
93
  readFloatLE(): number;
77
94
  readDoubleBE(): number;
@@ -105,6 +105,35 @@ class ExtendedBuffer {
105
105
  getWritableSize() {
106
106
  return this.getWritableSizeStart() + this.getWritableSizeEnd();
107
107
  }
108
+ transaction(callback) {
109
+ if (this._transaction) {
110
+ return callback();
111
+ }
112
+ this.assertInstanceState();
113
+ this._transaction = {
114
+ pointer: this._pointer,
115
+ pointerEnd: this._pointerEnd,
116
+ pointerStart: this._pointerStart,
117
+ nativeBuffer: this._nativeBuffer,
118
+ nativeBufferLength: this._nativeBuffer.length,
119
+ nativePayload: buffer_1.Buffer.from(utils.nativeBufferSubarray(this._nativeBuffer, this._pointerStart, this._pointerEnd))
120
+ };
121
+ try {
122
+ return callback();
123
+ }
124
+ catch (e) {
125
+ this._transaction.nativePayload.copy(this._transaction.nativeBuffer, this._transaction.pointerStart);
126
+ this._nativeBuffer = this._transaction.nativeBuffer;
127
+ this._pointer = this._transaction.pointer;
128
+ this._pointerEnd = this._transaction.pointerEnd;
129
+ this._pointerStart = this._transaction.pointerStart;
130
+ this.assertInstanceState();
131
+ throw e;
132
+ }
133
+ finally {
134
+ this._transaction = undefined;
135
+ }
136
+ }
108
137
  allocStart(size) {
109
138
  utils.assertUnsignedInteger(size);
110
139
  if (this.getWritableSizeStart() >= size) {
@@ -206,68 +235,40 @@ class ExtendedBuffer {
206
235
  const bytes = buffer_1.Buffer.from(string, encoding);
207
236
  return this.writeNativeBuffer(bytes, unshift);
208
237
  }
209
- writeIntBE(value, size, unshift) {
210
- utils.assertInteger(value);
211
- utils.assertIntegerSize(size);
212
- if (unshift) {
213
- this.allocStart(size);
214
- this._nativeBuffer.writeIntBE(value, this._pointerStart - size, size);
215
- this._pointerStart -= size;
238
+ writeIntCommon(method, value, size, unsigned, unshift) {
239
+ if (unsigned) {
240
+ utils.assertUnsignedInteger(value);
216
241
  }
217
242
  else {
218
- this.allocEnd(size);
219
- this._nativeBuffer.writeIntBE(value, this._pointerEnd, size);
220
- this._pointerEnd += size;
243
+ utils.assertInteger(value);
221
244
  }
222
- return this;
223
- }
224
- writeIntLE(value, size, unshift) {
225
- utils.assertInteger(value);
226
245
  utils.assertIntegerSize(size);
227
246
  if (unshift) {
228
247
  this.allocStart(size);
229
- this._nativeBuffer.writeIntLE(value, this._pointerStart - size, size);
248
+ this._nativeBuffer[method](value, this._pointerStart - size, size);
230
249
  this._pointerStart -= size;
231
250
  }
232
251
  else {
233
252
  this.allocEnd(size);
234
- this._nativeBuffer.writeIntLE(value, this._pointerEnd, size);
253
+ this._nativeBuffer[method](value, this._pointerEnd, size);
235
254
  this._pointerEnd += size;
236
255
  }
237
256
  return this;
238
257
  }
258
+ writeIntBE(value, size, unshift) {
259
+ return this.writeIntCommon('writeIntBE', value, size, false, unshift);
260
+ }
261
+ writeIntLE(value, size, unshift) {
262
+ return this.writeIntCommon('writeIntLE', value, size, false, unshift);
263
+ }
239
264
  writeUIntBE(value, size, unshift) {
240
- utils.assertUnsignedInteger(value);
241
- utils.assertIntegerSize(size);
242
- if (unshift) {
243
- this.allocStart(size);
244
- this._nativeBuffer.writeUIntBE(value, this._pointerStart - size, size);
245
- this._pointerStart -= size;
246
- }
247
- else {
248
- this.allocEnd(size);
249
- this._nativeBuffer.writeUIntBE(value, this._pointerEnd, size);
250
- this._pointerEnd += size;
251
- }
252
- return this;
265
+ return this.writeIntCommon('writeUIntBE', value, size, true, unshift);
253
266
  }
254
267
  writeUIntLE(value, size, unshift) {
255
- utils.assertUnsignedInteger(value);
256
- utils.assertIntegerSize(size);
257
- if (unshift) {
258
- this.allocStart(size);
259
- this._nativeBuffer.writeUIntLE(value, this._pointerStart - size, size);
260
- this._pointerStart -= size;
261
- }
262
- else {
263
- this.allocEnd(size);
264
- this._nativeBuffer.writeUIntLE(value, this._pointerEnd, size);
265
- this._pointerEnd += size;
266
- }
267
- return this;
268
+ return this.writeIntCommon('writeUIntLE', value, size, true, unshift);
268
269
  }
269
270
  writeInt8(value, unshift) {
270
- return this.writeIntBE(value, 1, unshift);
271
+ return this.writeIntLE(value, 1, unshift);
271
272
  }
272
273
  writeUInt8(value, unshift) {
273
274
  return this.writeUIntLE(value, 1, unshift);
@@ -296,57 +297,63 @@ class ExtendedBuffer {
296
297
  writeUInt32LE(value, unshift) {
297
298
  return this.writeUIntLE(value, 4, unshift);
298
299
  }
299
- writeFloatBE(value, unshift) {
300
- if (unshift) {
301
- this.allocStart(4);
302
- this._nativeBuffer.writeFloatBE(value, this._pointerStart - 4);
303
- this._pointerStart -= 4;
300
+ writeBigInt64Common(method, value, unsigned, unshift) {
301
+ utils.assertSupportBigInteger();
302
+ if (unsigned) {
303
+ utils.assertUnsignedBigInteger(value);
304
304
  }
305
305
  else {
306
- this.allocEnd(4);
307
- this._nativeBuffer.writeFloatBE(value, this._pointerEnd);
308
- this._pointerEnd += 4;
306
+ utils.assertBigInteger(value);
309
307
  }
310
- return this;
311
- }
312
- writeFloatLE(value, unshift) {
308
+ const size = 8;
313
309
  if (unshift) {
314
- this.allocStart(4);
315
- this._nativeBuffer.writeFloatLE(value, this._pointerStart - 4);
316
- this._pointerStart -= 4;
310
+ this.allocStart(size);
311
+ this._nativeBuffer[method](value, this._pointerStart - size);
312
+ this._pointerStart -= size;
317
313
  }
318
314
  else {
319
- this.allocEnd(4);
320
- this._nativeBuffer.writeFloatLE(value, this._pointerEnd);
321
- this._pointerEnd += 4;
315
+ this.allocEnd(size);
316
+ this._nativeBuffer[method](value, this._pointerEnd);
317
+ this._pointerEnd += size;
322
318
  }
323
319
  return this;
324
320
  }
325
- writeDoubleBE(value, unshift) {
321
+ writeBigInt64BE(value, unshift) {
322
+ return this.writeBigInt64Common('writeBigInt64BE', value, false, unshift);
323
+ }
324
+ writeBigInt64LE(value, unshift) {
325
+ return this.writeBigInt64Common('writeBigInt64LE', value, false, unshift);
326
+ }
327
+ writeBigUInt64BE(value, unshift) {
328
+ return this.writeBigInt64Common('writeBigUInt64BE', value, true, unshift);
329
+ }
330
+ writeBigUInt64LE(value, unshift) {
331
+ return this.writeBigInt64Common('writeBigUInt64LE', value, true, unshift);
332
+ }
333
+ writeFloatingPointCommon(method, value, size, unshift) {
326
334
  if (unshift) {
327
- this.allocStart(8);
328
- this._nativeBuffer.writeDoubleBE(value, this._pointerStart - 8);
329
- this._pointerStart -= 8;
335
+ this.allocStart(size);
336
+ this._nativeBuffer[method](value, this._pointerStart - size);
337
+ this._pointerStart -= size;
330
338
  }
331
339
  else {
332
- this.allocEnd(8);
333
- this._nativeBuffer.writeDoubleBE(value, this._pointerEnd);
334
- this._pointerEnd += 8;
340
+ this.allocEnd(size);
341
+ this._nativeBuffer[method](value, this._pointerEnd);
342
+ this._pointerEnd += size;
335
343
  }
336
344
  return this;
337
345
  }
346
+ writeFloatBE(value, unshift) {
347
+ return this.writeFloatingPointCommon('writeFloatBE', value, 4, unshift);
348
+ }
349
+ writeFloatLE(value, unshift) {
350
+ return this.writeFloatingPointCommon('writeFloatLE', value, 4, unshift);
351
+ }
352
+ writeDoubleBE(value, unshift) {
353
+ return this.writeFloatingPointCommon('writeDoubleBE', value, 8, unshift);
354
+ }
338
355
  writeDoubleLE(value, unshift) {
339
- if (unshift) {
340
- this.allocStart(8);
341
- this._nativeBuffer.writeDoubleLE(value, this._pointerStart - 8);
342
- this._pointerStart -= 8;
343
- }
344
- else {
345
- this.allocEnd(8);
346
- this._nativeBuffer.writeDoubleLE(value, this._pointerEnd);
347
- this._pointerEnd += 8;
348
- }
349
- return this;
356
+ return this.writeFloatingPointCommon('writeDoubleLE', value, 8, unshift);
350
357
  }
351
358
  readBuffer(size, asNative, bufferOptions) {
352
359
  utils.assertUnsignedInteger(size);
@@ -367,44 +374,29 @@ class ExtendedBuffer {
367
374
  this.offset(size);
368
375
  return result;
369
376
  }
370
- readIntBE(size) {
377
+ readIntCommon(method, size) {
371
378
  utils.assertIntegerSize(size);
372
379
  if (!this.isReadable(size)) {
373
380
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
374
381
  }
375
- const result = this._nativeBuffer.readIntBE(this.nativePointer(), size);
382
+ const result = this._nativeBuffer[method](this.nativePointer(), size);
376
383
  this.offset(size);
377
384
  return result;
378
385
  }
386
+ readIntBE(size) {
387
+ return this.readIntCommon('readIntBE', size);
388
+ }
379
389
  readIntLE(size) {
380
- utils.assertIntegerSize(size);
381
- if (!this.isReadable(size)) {
382
- throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
383
- }
384
- const result = this._nativeBuffer.readIntLE(this.nativePointer(), size);
385
- this.offset(size);
386
- return result;
390
+ return this.readIntCommon('readIntLE', size);
387
391
  }
388
392
  readUIntBE(size) {
389
- utils.assertIntegerSize(size);
390
- if (!this.isReadable(size)) {
391
- throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
392
- }
393
- const result = this._nativeBuffer.readUIntBE(this.nativePointer(), size);
394
- this.offset(size);
395
- return result;
393
+ return this.readIntCommon('readUIntBE', size);
396
394
  }
397
395
  readUIntLE(size) {
398
- utils.assertIntegerSize(size);
399
- if (!this.isReadable(size)) {
400
- throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
401
- }
402
- const result = this._nativeBuffer.readUIntLE(this.nativePointer(), size);
403
- this.offset(size);
404
- return result;
396
+ return this.readIntCommon('readUIntLE', size);
405
397
  }
406
398
  readInt8() {
407
- return this.readIntBE(1);
399
+ return this.readIntLE(1);
408
400
  }
409
401
  readUInt8() {
410
402
  return this.readUIntLE(1);
@@ -433,41 +425,48 @@ class ExtendedBuffer {
433
425
  readUInt32LE() {
434
426
  return this.readUIntLE(4);
435
427
  }
436
- readFloatBE() {
437
- const size = 4;
428
+ readBigInt64Common(method) {
429
+ utils.assertSupportBigInteger();
430
+ const size = 8;
438
431
  if (!this.isReadable(size)) {
439
432
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
440
433
  }
441
- const result = this._nativeBuffer.readFloatBE(this.nativePointer());
434
+ const result = this._nativeBuffer[method](this.nativePointer());
442
435
  this.offset(size);
443
436
  return result;
444
437
  }
445
- readFloatLE() {
446
- const size = 4;
438
+ readBigInt64BE() {
439
+ return this.readBigInt64Common('readBigInt64BE');
440
+ }
441
+ readBigInt64LE() {
442
+ return this.readBigInt64Common('readBigInt64LE');
443
+ }
444
+ readBigUInt64BE() {
445
+ return this.readBigInt64Common('readBigUInt64BE');
446
+ }
447
+ readBigUInt64LE() {
448
+ return this.readBigInt64Common('readBigUInt64LE');
449
+ }
450
+ readFloatingPointCommon(method, size) {
451
+ utils.assertUnsignedInteger(size);
447
452
  if (!this.isReadable(size)) {
448
453
  throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
449
454
  }
450
- const result = this._nativeBuffer.readFloatLE(this.nativePointer());
455
+ const result = this._nativeBuffer[method](this.nativePointer());
451
456
  this.offset(size);
452
457
  return result;
453
458
  }
459
+ readFloatBE() {
460
+ return this.readFloatingPointCommon('readFloatBE', 4);
461
+ }
462
+ readFloatLE() {
463
+ return this.readFloatingPointCommon('readFloatLE', 4);
464
+ }
454
465
  readDoubleBE() {
455
- const size = 8;
456
- if (!this.isReadable(size)) {
457
- throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
458
- }
459
- const result = this._nativeBuffer.readDoubleBE(this.nativePointer());
460
- this.offset(size);
461
- return result;
466
+ return this.readFloatingPointCommon('readDoubleBE', 8);
462
467
  }
463
468
  readDoubleLE() {
464
- const size = 8;
465
- if (!this.isReadable(size)) {
466
- throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
467
- }
468
- const result = this._nativeBuffer.readDoubleLE(this.nativePointer());
469
- this.offset(size);
470
- return result;
469
+ return this.readFloatingPointCommon('readDoubleLE', 8);
471
470
  }
472
471
  }
473
472
  exports.ExtendedBuffer = ExtendedBuffer;
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ export type ExtendedBufferTransaction = {
3
+ pointer: number;
4
+ pointerEnd: number;
5
+ pointerStart: number;
6
+ nativeBuffer: Buffer;
7
+ nativePayload: Buffer;
8
+ nativeBufferLength: number;
9
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { ExtendedBufferError } from './ExtendedBufferError';
2
+ export declare class ExtendedBufferUnsupportedError extends ExtendedBufferError {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExtendedBufferUnsupportedError = void 0;
4
+ const ExtendedBufferError_1 = require("./ExtendedBufferError");
5
+ class ExtendedBufferUnsupportedError extends ExtendedBufferError_1.ExtendedBufferError {
6
+ }
7
+ exports.ExtendedBufferUnsupportedError = ExtendedBufferUnsupportedError;
@@ -1,3 +1,4 @@
1
1
  export { ExtendedBufferError } from './ExtendedBufferError';
2
2
  export { ExtendedBufferTypeError } from './ExtendedBufferTypeError';
3
3
  export { ExtendedBufferRangeError } from './ExtendedBufferRangeError';
4
+ export { ExtendedBufferUnsupportedError } from './ExtendedBufferUnsupportedError';
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExtendedBufferRangeError = exports.ExtendedBufferTypeError = exports.ExtendedBufferError = void 0;
3
+ exports.ExtendedBufferUnsupportedError = exports.ExtendedBufferRangeError = exports.ExtendedBufferTypeError = exports.ExtendedBufferError = void 0;
4
4
  var ExtendedBufferError_1 = require("./ExtendedBufferError");
5
5
  Object.defineProperty(exports, "ExtendedBufferError", { enumerable: true, get: function () { return ExtendedBufferError_1.ExtendedBufferError; } });
6
6
  var ExtendedBufferTypeError_1 = require("./ExtendedBufferTypeError");
7
7
  Object.defineProperty(exports, "ExtendedBufferTypeError", { enumerable: true, get: function () { return ExtendedBufferTypeError_1.ExtendedBufferTypeError; } });
8
8
  var ExtendedBufferRangeError_1 = require("./ExtendedBufferRangeError");
9
9
  Object.defineProperty(exports, "ExtendedBufferRangeError", { enumerable: true, get: function () { return ExtendedBufferRangeError_1.ExtendedBufferRangeError; } });
10
+ var ExtendedBufferUnsupportedError_1 = require("./ExtendedBufferUnsupportedError");
11
+ Object.defineProperty(exports, "ExtendedBufferUnsupportedError", { enumerable: true, get: function () { return ExtendedBufferUnsupportedError_1.ExtendedBufferUnsupportedError; } });
package/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export * from './utils';
2
2
  export * from './errors';
3
3
  export { ExtendedBuffer } from './ExtendedBuffer';
4
4
  export type { ExtendedBufferOptions } from './ExtendedBufferOptions';
5
+ export type { ExtendedBufferTransaction } from './ExtendedBufferTransaction';
@@ -0,0 +1 @@
1
+ export declare function assertBigInteger(value: bigint): void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertBigInteger = void 0;
4
+ const errors_1 = require("../errors");
5
+ function assertBigInteger(value) {
6
+ if (typeof value !== 'bigint') {
7
+ throw new errors_1.ExtendedBufferTypeError('VALUE_MUST_BE_AN_BIG_INTEGER');
8
+ }
9
+ }
10
+ exports.assertBigInteger = assertBigInteger;
@@ -0,0 +1 @@
1
+ export declare function assertSupportBigInteger(): void;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertSupportBigInteger = void 0;
4
+ const buffer_1 = require("buffer");
5
+ const errors_1 = require("../errors");
6
+ function assertSupportBigInteger() {
7
+ let isSupported = false;
8
+ try {
9
+ isSupported = typeof BigInt(0) === 'bigint';
10
+ }
11
+ catch (e) { }
12
+ if (!isSupported || typeof buffer_1.Buffer.prototype.readBigUInt64LE !== 'function') {
13
+ throw new errors_1.ExtendedBufferUnsupportedError('EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT');
14
+ }
15
+ }
16
+ exports.assertSupportBigInteger = assertSupportBigInteger;
@@ -0,0 +1 @@
1
+ export declare function assertUnsignedBigInteger(value: bigint): void;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertUnsignedBigInteger = void 0;
4
+ const errors_1 = require("../errors");
5
+ function assertUnsignedBigInteger(value) {
6
+ if (typeof value !== 'bigint' || value < 0) {
7
+ throw new errors_1.ExtendedBufferTypeError('VALUE_MUST_BE_AN_UNSIGNED_BIG_INTEGER');
8
+ }
9
+ }
10
+ exports.assertUnsignedBigInteger = assertUnsignedBigInteger;
@@ -1,6 +1,9 @@
1
1
  export { assertInteger } from './assert-integer';
2
+ export { assertBigInteger } from './assert-big-integer';
2
3
  export { assertIntegerSize } from './assert-integer-size';
3
4
  export { allocNativeBuffer } from './alloc-native-buffer';
4
5
  export { reallocNativeBuffer } from './realloc-native-buffer';
5
6
  export { nativeBufferSubarray } from './native-buffer-subarray';
6
7
  export { assertUnsignedInteger } from './assert-unsigned-integer';
8
+ export { assertSupportBigInteger } from './assert-support-big-integer';
9
+ export { assertUnsignedBigInteger } from './assert-unsigned-big-integer';
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertUnsignedInteger = exports.nativeBufferSubarray = exports.reallocNativeBuffer = exports.allocNativeBuffer = exports.assertIntegerSize = exports.assertInteger = void 0;
3
+ exports.assertUnsignedBigInteger = exports.assertSupportBigInteger = exports.assertUnsignedInteger = exports.nativeBufferSubarray = exports.reallocNativeBuffer = exports.allocNativeBuffer = exports.assertIntegerSize = exports.assertBigInteger = exports.assertInteger = void 0;
4
4
  var assert_integer_1 = require("./assert-integer");
5
5
  Object.defineProperty(exports, "assertInteger", { enumerable: true, get: function () { return assert_integer_1.assertInteger; } });
6
+ var assert_big_integer_1 = require("./assert-big-integer");
7
+ Object.defineProperty(exports, "assertBigInteger", { enumerable: true, get: function () { return assert_big_integer_1.assertBigInteger; } });
6
8
  var assert_integer_size_1 = require("./assert-integer-size");
7
9
  Object.defineProperty(exports, "assertIntegerSize", { enumerable: true, get: function () { return assert_integer_size_1.assertIntegerSize; } });
8
10
  var alloc_native_buffer_1 = require("./alloc-native-buffer");
@@ -13,3 +15,7 @@ var native_buffer_subarray_1 = require("./native-buffer-subarray");
13
15
  Object.defineProperty(exports, "nativeBufferSubarray", { enumerable: true, get: function () { return native_buffer_subarray_1.nativeBufferSubarray; } });
14
16
  var assert_unsigned_integer_1 = require("./assert-unsigned-integer");
15
17
  Object.defineProperty(exports, "assertUnsignedInteger", { enumerable: true, get: function () { return assert_unsigned_integer_1.assertUnsignedInteger; } });
18
+ var assert_support_big_integer_1 = require("./assert-support-big-integer");
19
+ Object.defineProperty(exports, "assertSupportBigInteger", { enumerable: true, get: function () { return assert_support_big_integer_1.assertSupportBigInteger; } });
20
+ var assert_unsigned_big_integer_1 = require("./assert-unsigned-big-integer");
21
+ Object.defineProperty(exports, "assertUnsignedBigInteger", { enumerable: true, get: function () { return assert_unsigned_big_integer_1.assertUnsignedBigInteger; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extended-buffer",
3
- "version": "7.2.1",
3
+ "version": "7.3.0",
4
4
  "description": "Node JS extended Buffer",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -55,7 +55,7 @@
55
55
  "README.md"
56
56
  ],
57
57
  "devDependencies": {
58
- "@types/node": "^6.14.13",
58
+ "@types/node": "^12.20.55",
59
59
  "chai": "^4.5.0",
60
60
  "mocha": "^6.2.3",
61
61
  "nyc": "^11.9.0",