extended-buffer 7.2.1 → 7.3.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/README.md +131 -0
- package/dist/ExtendedBuffer.d.ts +17 -0
- package/dist/ExtendedBuffer.js +125 -123
- package/dist/ExtendedBufferTransaction.d.ts +9 -0
- package/dist/ExtendedBufferTransaction.js +2 -0
- package/dist/errors/ExtendedBufferUnsupportedError.d.ts +3 -0
- package/dist/errors/ExtendedBufferUnsupportedError.js +7 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +3 -1
- package/dist/index.d.ts +1 -0
- package/dist/utils/assert-big-integer.d.ts +1 -0
- package/dist/utils/assert-big-integer.js +10 -0
- package/dist/utils/assert-support-big-integer.d.ts +1 -0
- package/dist/utils/assert-support-big-integer.js +16 -0
- package/dist/utils/assert-unsigned-big-integer.d.ts +1 -0
- package/dist/utils/assert-unsigned-big-integer.js +10 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +7 -1
- package/package.json +2 -2
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
|
|
@@ -238,6 +357,7 @@ The library defines these error classes:
|
|
|
238
357
|
- `ExtendedBufferError`
|
|
239
358
|
- `ExtendedBufferTypeError`
|
|
240
359
|
- `ExtendedBufferRangeError`
|
|
360
|
+
- `ExtendedBufferUnsupportedError`
|
|
241
361
|
|
|
242
362
|
Common error codes you may see:
|
|
243
363
|
|
|
@@ -246,8 +366,12 @@ Common error codes you may see:
|
|
|
246
366
|
- `INVALID_INTEGER_SIZE_VALUE_TYPE`: size is not a safe integer
|
|
247
367
|
- `INVALID_INTEGER_SIZE_VALUE_RANGE`: integer size not in `1…6`
|
|
248
368
|
- `INVALID_INSTANCE_STATE`: internal invariant check failed
|
|
369
|
+
- `INVALID_BUFFER_TYPE`: attempt write invalid buffer type
|
|
249
370
|
- `VALUE_MUST_BE_AN_INTEGER`: value not a safe integer
|
|
250
371
|
- `VALUE_MUST_BE_AN_UNSIGNED_INTEGER`: value is not a safe integer or less than 0
|
|
372
|
+
- `VALUE_MUST_BE_AN_BIG_INTEGER`: value is not a `bigint`
|
|
373
|
+
- `VALUE_MUST_BE_AN_UNSIGNED_BIG_INTEGER`: value is not a `bigint` or less than 0
|
|
374
|
+
- `EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT`: BigInt methods are not supported in the current runtime
|
|
251
375
|
- `EXCEEDING_MAXIMUM_BUFFER_SIZE`: allocation exceeds Node’s `kMaxLength` or `os.totalmem()`
|
|
252
376
|
|
|
253
377
|
---
|
|
@@ -281,6 +405,7 @@ Properties:
|
|
|
281
405
|
Core:
|
|
282
406
|
- `initExtendedBuffer()`, `assertInstanceState()`, `clean()`
|
|
283
407
|
- `nativePointer()`, `getWritableSizeStart()`, `getWritableSizeEnd()`, `getWritableSize()`, `getReadableSize()`
|
|
408
|
+
- `transaction(callback)`
|
|
284
409
|
- `allocStart(size)`, `allocEnd(size)`
|
|
285
410
|
- `writeNativeBuffer(buf, unshift?)`, `writeBuffer(bufOrEB, unshift?)`, `writeString(str, enc?, unshift?)`
|
|
286
411
|
- Pointer: `setPointer(p)`, `getPointer()`, `offset(n)`, `isReadable(size)`
|
|
@@ -289,8 +414,14 @@ Core:
|
|
|
289
414
|
Numbers:
|
|
290
415
|
- Write: `writeIntBE/LE`, `writeUIntBE/LE`, `writeInt8`, `writeUInt8`,
|
|
291
416
|
`writeInt16BE/LE`, `writeUInt16BE/LE`, `writeInt32BE/LE`, `writeUInt32BE/LE`,
|
|
417
|
+
`writeBigInt64BE/LE`, `writeBigUInt64BE/LE`,
|
|
292
418
|
`writeFloatBE/LE`, `writeDoubleBE/LE`
|
|
293
419
|
- Read: `readBuffer`, `readString`,
|
|
294
420
|
`readIntBE/LE`, `readUIntBE/LE`, `readInt8`, `readUInt8`,
|
|
295
421
|
`readInt16BE/LE`, `readUInt16BE/LE`, `readInt32BE/LE`, `readUInt32BE/LE`,
|
|
422
|
+
`readBigInt64BE/LE`, `readBigUInt64BE/LE`,
|
|
296
423
|
`readFloatBE/LE`, `readDoubleBE/LE`
|
|
424
|
+
|
|
425
|
+
## License
|
|
426
|
+
|
|
427
|
+
MIT
|
package/dist/ExtendedBuffer.d.ts
CHANGED
|
@@ -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;
|
package/dist/ExtendedBuffer.js
CHANGED
|
@@ -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) {
|
|
@@ -200,74 +229,49 @@ class ExtendedBuffer {
|
|
|
200
229
|
if (value instanceof ExtendedBuffer) {
|
|
201
230
|
return this.writeNativeBuffer(value.nativeBufferView, unshift);
|
|
202
231
|
}
|
|
203
|
-
|
|
232
|
+
if (buffer_1.Buffer.isBuffer(value)) {
|
|
233
|
+
return this.writeNativeBuffer(value, unshift);
|
|
234
|
+
}
|
|
235
|
+
throw new errors_1.ExtendedBufferTypeError('INVALID_BUFFER_TYPE');
|
|
204
236
|
}
|
|
205
237
|
writeString(string, encoding, unshift) {
|
|
206
238
|
const bytes = buffer_1.Buffer.from(string, encoding);
|
|
207
239
|
return this.writeNativeBuffer(bytes, unshift);
|
|
208
240
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (unshift) {
|
|
213
|
-
this.allocStart(size);
|
|
214
|
-
this._nativeBuffer.writeIntBE(value, this._pointerStart - size, size);
|
|
215
|
-
this._pointerStart -= size;
|
|
241
|
+
writeIntCommon(method, value, size, unsigned, unshift) {
|
|
242
|
+
if (unsigned) {
|
|
243
|
+
utils.assertUnsignedInteger(value);
|
|
216
244
|
}
|
|
217
245
|
else {
|
|
218
|
-
|
|
219
|
-
this._nativeBuffer.writeIntBE(value, this._pointerEnd, size);
|
|
220
|
-
this._pointerEnd += size;
|
|
246
|
+
utils.assertInteger(value);
|
|
221
247
|
}
|
|
222
|
-
return this;
|
|
223
|
-
}
|
|
224
|
-
writeIntLE(value, size, unshift) {
|
|
225
|
-
utils.assertInteger(value);
|
|
226
248
|
utils.assertIntegerSize(size);
|
|
227
249
|
if (unshift) {
|
|
228
250
|
this.allocStart(size);
|
|
229
|
-
this._nativeBuffer
|
|
251
|
+
this._nativeBuffer[method](value, this._pointerStart - size, size);
|
|
230
252
|
this._pointerStart -= size;
|
|
231
253
|
}
|
|
232
254
|
else {
|
|
233
255
|
this.allocEnd(size);
|
|
234
|
-
this._nativeBuffer
|
|
256
|
+
this._nativeBuffer[method](value, this._pointerEnd, size);
|
|
235
257
|
this._pointerEnd += size;
|
|
236
258
|
}
|
|
237
259
|
return this;
|
|
238
260
|
}
|
|
261
|
+
writeIntBE(value, size, unshift) {
|
|
262
|
+
return this.writeIntCommon('writeIntBE', value, size, false, unshift);
|
|
263
|
+
}
|
|
264
|
+
writeIntLE(value, size, unshift) {
|
|
265
|
+
return this.writeIntCommon('writeIntLE', value, size, false, unshift);
|
|
266
|
+
}
|
|
239
267
|
writeUIntBE(value, size, unshift) {
|
|
240
|
-
|
|
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;
|
|
268
|
+
return this.writeIntCommon('writeUIntBE', value, size, true, unshift);
|
|
253
269
|
}
|
|
254
270
|
writeUIntLE(value, size, unshift) {
|
|
255
|
-
|
|
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;
|
|
271
|
+
return this.writeIntCommon('writeUIntLE', value, size, true, unshift);
|
|
268
272
|
}
|
|
269
273
|
writeInt8(value, unshift) {
|
|
270
|
-
return this.
|
|
274
|
+
return this.writeIntLE(value, 1, unshift);
|
|
271
275
|
}
|
|
272
276
|
writeUInt8(value, unshift) {
|
|
273
277
|
return this.writeUIntLE(value, 1, unshift);
|
|
@@ -296,57 +300,63 @@ class ExtendedBuffer {
|
|
|
296
300
|
writeUInt32LE(value, unshift) {
|
|
297
301
|
return this.writeUIntLE(value, 4, unshift);
|
|
298
302
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
this._pointerStart -= 4;
|
|
303
|
+
writeBigInt64Common(method, value, unsigned, unshift) {
|
|
304
|
+
utils.assertSupportBigInteger();
|
|
305
|
+
if (unsigned) {
|
|
306
|
+
utils.assertUnsignedBigInteger(value);
|
|
304
307
|
}
|
|
305
308
|
else {
|
|
306
|
-
|
|
307
|
-
this._nativeBuffer.writeFloatBE(value, this._pointerEnd);
|
|
308
|
-
this._pointerEnd += 4;
|
|
309
|
+
utils.assertBigInteger(value);
|
|
309
310
|
}
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
writeFloatLE(value, unshift) {
|
|
311
|
+
const size = 8;
|
|
313
312
|
if (unshift) {
|
|
314
|
-
this.allocStart(
|
|
315
|
-
this._nativeBuffer
|
|
316
|
-
this._pointerStart -=
|
|
313
|
+
this.allocStart(size);
|
|
314
|
+
this._nativeBuffer[method](value, this._pointerStart - size);
|
|
315
|
+
this._pointerStart -= size;
|
|
317
316
|
}
|
|
318
317
|
else {
|
|
319
|
-
this.allocEnd(
|
|
320
|
-
this._nativeBuffer
|
|
321
|
-
this._pointerEnd +=
|
|
318
|
+
this.allocEnd(size);
|
|
319
|
+
this._nativeBuffer[method](value, this._pointerEnd);
|
|
320
|
+
this._pointerEnd += size;
|
|
322
321
|
}
|
|
323
322
|
return this;
|
|
324
323
|
}
|
|
325
|
-
|
|
324
|
+
writeBigInt64BE(value, unshift) {
|
|
325
|
+
return this.writeBigInt64Common('writeBigInt64BE', value, false, unshift);
|
|
326
|
+
}
|
|
327
|
+
writeBigInt64LE(value, unshift) {
|
|
328
|
+
return this.writeBigInt64Common('writeBigInt64LE', value, false, unshift);
|
|
329
|
+
}
|
|
330
|
+
writeBigUInt64BE(value, unshift) {
|
|
331
|
+
return this.writeBigInt64Common('writeBigUInt64BE', value, true, unshift);
|
|
332
|
+
}
|
|
333
|
+
writeBigUInt64LE(value, unshift) {
|
|
334
|
+
return this.writeBigInt64Common('writeBigUInt64LE', value, true, unshift);
|
|
335
|
+
}
|
|
336
|
+
writeFloatingPointCommon(method, value, size, unshift) {
|
|
326
337
|
if (unshift) {
|
|
327
|
-
this.allocStart(
|
|
328
|
-
this._nativeBuffer
|
|
329
|
-
this._pointerStart -=
|
|
338
|
+
this.allocStart(size);
|
|
339
|
+
this._nativeBuffer[method](value, this._pointerStart - size);
|
|
340
|
+
this._pointerStart -= size;
|
|
330
341
|
}
|
|
331
342
|
else {
|
|
332
|
-
this.allocEnd(
|
|
333
|
-
this._nativeBuffer
|
|
334
|
-
this._pointerEnd +=
|
|
343
|
+
this.allocEnd(size);
|
|
344
|
+
this._nativeBuffer[method](value, this._pointerEnd);
|
|
345
|
+
this._pointerEnd += size;
|
|
335
346
|
}
|
|
336
347
|
return this;
|
|
337
348
|
}
|
|
349
|
+
writeFloatBE(value, unshift) {
|
|
350
|
+
return this.writeFloatingPointCommon('writeFloatBE', value, 4, unshift);
|
|
351
|
+
}
|
|
352
|
+
writeFloatLE(value, unshift) {
|
|
353
|
+
return this.writeFloatingPointCommon('writeFloatLE', value, 4, unshift);
|
|
354
|
+
}
|
|
355
|
+
writeDoubleBE(value, unshift) {
|
|
356
|
+
return this.writeFloatingPointCommon('writeDoubleBE', value, 8, unshift);
|
|
357
|
+
}
|
|
338
358
|
writeDoubleLE(value, unshift) {
|
|
339
|
-
|
|
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;
|
|
359
|
+
return this.writeFloatingPointCommon('writeDoubleLE', value, 8, unshift);
|
|
350
360
|
}
|
|
351
361
|
readBuffer(size, asNative, bufferOptions) {
|
|
352
362
|
utils.assertUnsignedInteger(size);
|
|
@@ -367,44 +377,29 @@ class ExtendedBuffer {
|
|
|
367
377
|
this.offset(size);
|
|
368
378
|
return result;
|
|
369
379
|
}
|
|
370
|
-
|
|
380
|
+
readIntCommon(method, size) {
|
|
371
381
|
utils.assertIntegerSize(size);
|
|
372
382
|
if (!this.isReadable(size)) {
|
|
373
383
|
throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
|
|
374
384
|
}
|
|
375
|
-
const result = this._nativeBuffer
|
|
385
|
+
const result = this._nativeBuffer[method](this.nativePointer(), size);
|
|
376
386
|
this.offset(size);
|
|
377
387
|
return result;
|
|
378
388
|
}
|
|
389
|
+
readIntBE(size) {
|
|
390
|
+
return this.readIntCommon('readIntBE', size);
|
|
391
|
+
}
|
|
379
392
|
readIntLE(size) {
|
|
380
|
-
|
|
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;
|
|
393
|
+
return this.readIntCommon('readIntLE', size);
|
|
387
394
|
}
|
|
388
395
|
readUIntBE(size) {
|
|
389
|
-
|
|
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;
|
|
396
|
+
return this.readIntCommon('readUIntBE', size);
|
|
396
397
|
}
|
|
397
398
|
readUIntLE(size) {
|
|
398
|
-
|
|
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;
|
|
399
|
+
return this.readIntCommon('readUIntLE', size);
|
|
405
400
|
}
|
|
406
401
|
readInt8() {
|
|
407
|
-
return this.
|
|
402
|
+
return this.readIntLE(1);
|
|
408
403
|
}
|
|
409
404
|
readUInt8() {
|
|
410
405
|
return this.readUIntLE(1);
|
|
@@ -433,41 +428,48 @@ class ExtendedBuffer {
|
|
|
433
428
|
readUInt32LE() {
|
|
434
429
|
return this.readUIntLE(4);
|
|
435
430
|
}
|
|
436
|
-
|
|
437
|
-
|
|
431
|
+
readBigInt64Common(method) {
|
|
432
|
+
utils.assertSupportBigInteger();
|
|
433
|
+
const size = 8;
|
|
438
434
|
if (!this.isReadable(size)) {
|
|
439
435
|
throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
|
|
440
436
|
}
|
|
441
|
-
const result = this._nativeBuffer
|
|
437
|
+
const result = this._nativeBuffer[method](this.nativePointer());
|
|
442
438
|
this.offset(size);
|
|
443
439
|
return result;
|
|
444
440
|
}
|
|
445
|
-
|
|
446
|
-
|
|
441
|
+
readBigInt64BE() {
|
|
442
|
+
return this.readBigInt64Common('readBigInt64BE');
|
|
443
|
+
}
|
|
444
|
+
readBigInt64LE() {
|
|
445
|
+
return this.readBigInt64Common('readBigInt64LE');
|
|
446
|
+
}
|
|
447
|
+
readBigUInt64BE() {
|
|
448
|
+
return this.readBigInt64Common('readBigUInt64BE');
|
|
449
|
+
}
|
|
450
|
+
readBigUInt64LE() {
|
|
451
|
+
return this.readBigInt64Common('readBigUInt64LE');
|
|
452
|
+
}
|
|
453
|
+
readFloatingPointCommon(method, size) {
|
|
454
|
+
utils.assertUnsignedInteger(size);
|
|
447
455
|
if (!this.isReadable(size)) {
|
|
448
456
|
throw new errors_1.ExtendedBufferRangeError('SIZE_OUT_OF_RANGE');
|
|
449
457
|
}
|
|
450
|
-
const result = this._nativeBuffer
|
|
458
|
+
const result = this._nativeBuffer[method](this.nativePointer());
|
|
451
459
|
this.offset(size);
|
|
452
460
|
return result;
|
|
453
461
|
}
|
|
462
|
+
readFloatBE() {
|
|
463
|
+
return this.readFloatingPointCommon('readFloatBE', 4);
|
|
464
|
+
}
|
|
465
|
+
readFloatLE() {
|
|
466
|
+
return this.readFloatingPointCommon('readFloatLE', 4);
|
|
467
|
+
}
|
|
454
468
|
readDoubleBE() {
|
|
455
|
-
|
|
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;
|
|
469
|
+
return this.readFloatingPointCommon('readDoubleBE', 8);
|
|
462
470
|
}
|
|
463
471
|
readDoubleLE() {
|
|
464
|
-
|
|
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;
|
|
472
|
+
return this.readFloatingPointCommon('readDoubleLE', 8);
|
|
471
473
|
}
|
|
472
474
|
}
|
|
473
475
|
exports.ExtendedBuffer = ExtendedBuffer;
|
|
@@ -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;
|
package/dist/errors/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/errors/index.js
CHANGED
|
@@ -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
|
@@ -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;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/utils/index.js
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "7.3.1",
|
|
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": "^
|
|
58
|
+
"@types/node": "^12.20.55",
|
|
59
59
|
"chai": "^4.5.0",
|
|
60
60
|
"mocha": "^6.2.3",
|
|
61
61
|
"nyc": "^11.9.0",
|