@gsknnft/bigint-buffer 1.3.0 → 1.3.2
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 +16 -8
- package/dist/index.cjs +205 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +203 -0
- package/dist/node.js +19 -4
- package/helper/bigint.d.ts +2 -2
- package/package.json +11 -78
- package/rollup.cjs.config.js +8 -0
- package/rollup.esm.config.js +15 -0
- package/src/index.bench.ts +119 -116
- package/src/index.spec.ts +234 -43
- package/src/index.ts +149 -39
- package/tsconfig.json +2 -5
- package/.eslintrc +0 -5
- package/dist/index.bench.d.ts +0 -1
- package/dist/index.spec.d.ts +0 -1
- package/okg.md +0 -180
- package/rollup.config.js +0 -16
- package/src/conversion/LICENSE +0 -21
- package/src/conversion/README.md +0 -48
- package/src/conversion/docs/README.md +0 -34
- package/src/conversion/docs/functions/base64ToBigint.md +0 -27
- package/src/conversion/docs/functions/bigintToBase64.md +0 -43
- package/src/conversion/docs/functions/bigintToBuf.md +0 -35
- package/src/conversion/docs/functions/bigintToHex.md +0 -43
- package/src/conversion/docs/functions/bigintToText.md +0 -31
- package/src/conversion/docs/functions/bufToBigint.md +0 -25
- package/src/conversion/docs/functions/bufToHex.md +0 -37
- package/src/conversion/docs/functions/bufToText.md +0 -27
- package/src/conversion/docs/functions/hexToBigint.md +0 -29
- package/src/conversion/docs/functions/hexToBuf.md +0 -37
- package/src/conversion/docs/functions/parseHex.md +0 -45
- package/src/conversion/docs/functions/textToBigint.md +0 -27
- package/src/conversion/docs/functions/textToBuf.md +0 -33
- package/src/conversion/docs/functions/toBigIntBE.md +0 -27
- package/src/conversion/docs/functions/toBigIntLE.md +0 -27
- package/src/conversion/docs/functions/toBufferBE.md +0 -33
- package/src/conversion/docs/functions/toBufferLE.md +0 -33
- package/src/conversion/docs/functions/validateBigIntBuffer.md +0 -15
- package/src/conversion/docs/type-aliases/TypedArray.md +0 -11
- package/src/conversion/docs/variables/isNative.md +0 -11
- package/src/conversion/example.cjs +0 -9
- package/src/conversion/example.esm.js +0 -11
- package/src/conversion/package.json +0 -182
- package/src/conversion/pnpm-lock.yaml +0 -5571
- package/src/conversion/tsconfig.rollup.json +0 -9
- package/src/conversion/typedoc.json +0 -5
- package/src/types/bindings.d.t.s +0 -4
- package/tsconfig.lint.json +0 -5
- package/vitest.config.ts +0 -10
package/src/index.spec.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import 'mocha';
|
|
2
2
|
declare var process: {browser: boolean;};
|
|
3
|
-
|
|
4
|
-
import path from 'path'
|
|
3
|
+
|
|
5
4
|
import * as chai from 'chai';
|
|
6
|
-
|
|
7
|
-
const lib = require(process.browser
|
|
8
|
-
? path.resolve(__dirname, '../dist/index.js')
|
|
9
|
-
: path.resolve(__dirname, '../dist/node.js'));
|
|
5
|
+
import * as path from 'path';
|
|
10
6
|
|
|
7
|
+
const lib = process.browser ?
|
|
8
|
+
require('../../dist/index.js') :
|
|
9
|
+
require(path.join(__dirname, '../../dist/index.cjs'));
|
|
11
10
|
const toBigIntBE = lib.toBigIntBE;
|
|
12
11
|
const toBigIntLE = lib.toBigIntLE;
|
|
13
12
|
const toBufferBE = lib.toBufferBE;
|
|
@@ -172,82 +171,274 @@ describe('Try buffer conversion (big endian)', () => {
|
|
|
172
171
|
|
|
173
172
|
describe('Try bigint conversion (little endian)', () => {
|
|
174
173
|
it('0 should equal 0n', () => {
|
|
175
|
-
|
|
174
|
+
toBufferLE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
175
|
+
0, 0, 0, 0, 0, 0, 0, 0
|
|
176
|
+
]));
|
|
176
177
|
});
|
|
177
178
|
|
|
178
179
|
it('1 should equal 1n', async () => {
|
|
179
|
-
|
|
180
|
+
toBufferLE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
181
|
+
1, 0, 0, 0, 0, 0, 0, 0
|
|
182
|
+
]));
|
|
180
183
|
});
|
|
181
184
|
|
|
185
|
+
|
|
182
186
|
it('1 should equal 1n (32 byte)', async () => {
|
|
183
|
-
|
|
184
|
-
|
|
187
|
+
toBufferLE(BigInt(`1`), 32)
|
|
188
|
+
.should.deep.equal(Buffer.from(
|
|
189
|
+
'0100000000000000000000000000000000000000000000000000000000000000',
|
|
190
|
+
'hex'));
|
|
185
191
|
});
|
|
186
192
|
|
|
187
193
|
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
188
|
-
|
|
194
|
+
toBufferLE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
195
|
+
0xad, 0xde, 0, 0, 0, 0
|
|
196
|
+
]));
|
|
189
197
|
});
|
|
190
198
|
|
|
191
199
|
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
192
|
-
|
|
200
|
+
toBufferLE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
201
|
+
0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0
|
|
202
|
+
]));
|
|
193
203
|
});
|
|
194
204
|
|
|
195
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
196
|
-
|
|
197
|
-
|
|
205
|
+
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
206
|
+
async () => {
|
|
207
|
+
toBufferLE(BigInt(`0xbadc0ffee0ddf00d`), 8)
|
|
208
|
+
.should.deep.equal(
|
|
209
|
+
Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba]));
|
|
210
|
+
});
|
|
198
211
|
|
|
199
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
212
|
+
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
213
|
+
async () => {
|
|
214
|
+
toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
|
|
215
|
+
.should.deep.equal(Buffer.from([
|
|
216
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
|
|
217
|
+
0xba
|
|
218
|
+
]));
|
|
219
|
+
});
|
|
204
220
|
|
|
205
221
|
it('long value should equal long val', async () => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
222
|
+
toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
|
|
223
|
+
.should.deep.equal(Buffer.from([
|
|
224
|
+
0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
|
|
225
|
+
0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
|
|
226
|
+
0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
|
|
227
|
+
]));
|
|
211
228
|
});
|
|
212
229
|
});
|
|
213
230
|
|
|
231
|
+
|
|
214
232
|
describe('Try bigint conversion (big endian)', () => {
|
|
215
233
|
it('0 should equal 0n', () => {
|
|
216
|
-
|
|
234
|
+
toBufferBE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
235
|
+
0, 0, 0, 0, 0, 0, 0, 0
|
|
236
|
+
]));
|
|
217
237
|
});
|
|
218
238
|
|
|
219
239
|
it('1 should equal 1n', async () => {
|
|
220
|
-
|
|
240
|
+
toBufferBE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
241
|
+
0, 0, 0, 0, 0, 0, 0, 1
|
|
242
|
+
]));
|
|
221
243
|
});
|
|
222
244
|
|
|
223
245
|
it('1 should equal 1n (32 byte)', async () => {
|
|
224
|
-
|
|
225
|
-
|
|
246
|
+
toBufferBE(BigInt(`1`), 32)
|
|
247
|
+
.should.deep.equal(Buffer.from(
|
|
248
|
+
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
249
|
+
'hex'));
|
|
226
250
|
});
|
|
227
251
|
|
|
228
252
|
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
229
|
-
|
|
253
|
+
toBufferBE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
254
|
+
0, 0, 0, 0, 0xde, 0xad
|
|
255
|
+
]));
|
|
230
256
|
});
|
|
231
257
|
|
|
232
258
|
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
233
|
-
|
|
259
|
+
toBufferBE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
260
|
+
0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef
|
|
261
|
+
]));
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
265
|
+
async () => {
|
|
266
|
+
toBufferBE(BigInt(`0xbadc0ffee0ddf00d`), 8)
|
|
267
|
+
.should.deep.equal(
|
|
268
|
+
Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d]));
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
272
|
+
async () => {
|
|
273
|
+
toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
|
|
274
|
+
.should.deep.equal(Buffer.from([
|
|
275
|
+
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
|
|
276
|
+
0xef
|
|
277
|
+
]));
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('long value should equal long val', async () => {
|
|
281
|
+
toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
|
|
282
|
+
.should.deep.equal(Buffer.from([
|
|
283
|
+
0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d,
|
|
284
|
+
0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe,
|
|
285
|
+
0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
|
|
286
|
+
]));
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe('Conversion Utilities - bigintToBuf and bufToBigint', () => {
|
|
291
|
+
const bigintToBuf = lib.bigintToBuf;
|
|
292
|
+
const bufToBigint = lib.bufToBigint;
|
|
293
|
+
|
|
294
|
+
it('should convert 0 to buffer', () => {
|
|
295
|
+
const buf = bigintToBuf(BigInt(0));
|
|
296
|
+
buf.should.deep.equal(Buffer.from([0]));
|
|
234
297
|
});
|
|
235
298
|
|
|
236
|
-
it('
|
|
237
|
-
|
|
299
|
+
it('should convert small number to buffer', () => {
|
|
300
|
+
const buf = bigintToBuf(BigInt(123456789));
|
|
301
|
+
assertEquals(bufToBigint(buf), BigInt(123456789));
|
|
238
302
|
});
|
|
239
303
|
|
|
240
|
-
it('
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
304
|
+
it('should round-trip convert medium numbers', () => {
|
|
305
|
+
const original = BigInt('0xdeadbeef');
|
|
306
|
+
const buf = bigintToBuf(original);
|
|
307
|
+
const result = bufToBigint(buf);
|
|
308
|
+
assertEquals(result, original);
|
|
244
309
|
});
|
|
245
310
|
|
|
246
|
-
it('
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
311
|
+
it('should round-trip convert large numbers', () => {
|
|
312
|
+
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
313
|
+
const buf = bigintToBuf(original);
|
|
314
|
+
const result = bufToBigint(buf);
|
|
315
|
+
assertEquals(result, original);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('should throw error for negative bigint', () => {
|
|
319
|
+
chai.expect(() => bigintToBuf(BigInt(-1))).to.throw('negative bigint');
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe('Conversion Utilities - bigintToHex and hexToBigint', () => {
|
|
324
|
+
const bigintToHex = lib.bigintToHex;
|
|
325
|
+
const hexToBigint = lib.hexToBigint;
|
|
326
|
+
|
|
327
|
+
it('should convert 0 to hex', () => {
|
|
328
|
+
bigintToHex(BigInt(0)).should.equal('00');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('should convert small number to hex', () => {
|
|
332
|
+
bigintToHex(BigInt(255)).should.equal('ff');
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('should convert number to even-length hex', () => {
|
|
336
|
+
bigintToHex(BigInt(15)).should.equal('0f');
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('should convert hex string to bigint', () => {
|
|
340
|
+
assertEquals(hexToBigint('deadbeef'), BigInt('0xdeadbeef'));
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it('should handle hex string with 0x prefix', () => {
|
|
344
|
+
assertEquals(hexToBigint('0xdeadbeef'), BigInt('0xdeadbeef'));
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('should round-trip convert', () => {
|
|
348
|
+
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
349
|
+
const hex = bigintToHex(original);
|
|
350
|
+
const result = hexToBigint(hex);
|
|
351
|
+
assertEquals(result, original);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('should handle empty string', () => {
|
|
355
|
+
assertEquals(hexToBigint(''), BigInt(0));
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it('should throw error for negative bigint', () => {
|
|
359
|
+
chai.expect(() => bigintToHex(BigInt(-1))).to.throw('negative bigint');
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
describe('Conversion Utilities - bigintToText and textToBigint', () => {
|
|
364
|
+
const bigintToText = lib.bigintToText;
|
|
365
|
+
const textToBigint = lib.textToBigint;
|
|
366
|
+
|
|
367
|
+
it('should convert 0 to text', () => {
|
|
368
|
+
bigintToText(BigInt(0)).should.equal('0');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('should convert positive number to text', () => {
|
|
372
|
+
bigintToText(BigInt(123456789)).should.equal('123456789');
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('should convert large number to text', () => {
|
|
376
|
+
bigintToText(BigInt('0xdeadbeef')).should.equal('3735928559');
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it('should convert text to bigint', () => {
|
|
380
|
+
assertEquals(textToBigint('123456789'), BigInt(123456789));
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it('should round-trip convert', () => {
|
|
384
|
+
const original = BigInt('9876543210123456789');
|
|
385
|
+
const text = bigintToText(original);
|
|
386
|
+
const result = textToBigint(text);
|
|
387
|
+
assertEquals(result, original);
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it('should throw error for empty string', () => {
|
|
391
|
+
chai.expect(() => textToBigint('')).to.throw('cannot be empty');
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('should throw error for invalid decimal string', () => {
|
|
395
|
+
chai.expect(() => textToBigint('abc')).to.throw('invalid decimal');
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
describe('Conversion Utilities - bigintToBase64 and base64ToBigint', () => {
|
|
400
|
+
const bigintToBase64 = lib.bigintToBase64;
|
|
401
|
+
const base64ToBigint = lib.base64ToBigint;
|
|
402
|
+
|
|
403
|
+
it('should convert 0 to base64', () => {
|
|
404
|
+
const b64 = bigintToBase64(BigInt(0));
|
|
405
|
+
b64.should.be.a('string');
|
|
406
|
+
assertEquals(base64ToBigint(b64), BigInt(0));
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it('should convert small number to base64', () => {
|
|
410
|
+
const original = BigInt(123456789);
|
|
411
|
+
const b64 = bigintToBase64(original);
|
|
412
|
+
b64.should.equal('B1vNFQ==');
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('should convert base64 to bigint', () => {
|
|
416
|
+
assertEquals(base64ToBigint('B1vNFQ=='), BigInt(123456789));
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it('should round-trip convert medium numbers', () => {
|
|
420
|
+
const original = BigInt('0xdeadbeef');
|
|
421
|
+
const b64 = bigintToBase64(original);
|
|
422
|
+
const result = base64ToBigint(b64);
|
|
423
|
+
assertEquals(result, original);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should round-trip convert large numbers', () => {
|
|
427
|
+
const original = BigInt('0xbadc0ffee0ddf00ddeadbeef');
|
|
428
|
+
const b64 = bigintToBase64(original);
|
|
429
|
+
const result = base64ToBigint(b64);
|
|
430
|
+
assertEquals(result, original);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('should throw error for negative bigint', () => {
|
|
434
|
+
chai.expect(() => bigintToBase64(BigInt(-1))).to.throw('negative bigint');
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('should throw error for empty base64 string', () => {
|
|
438
|
+
chai.expect(() => base64ToBigint('')).to.throw('cannot be empty');
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it('should throw error for invalid base64 string', () => {
|
|
442
|
+
chai.expect(() => base64ToBigint('invalid!@#')).to.throw('invalid base64');
|
|
252
443
|
});
|
|
253
444
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
|
|
2
|
-
// etc.
|
|
3
|
-
import bindings from 'bindings'
|
|
4
|
-
const native = bindings('bigint_buffer')
|
|
5
|
-
|
|
6
2
|
interface ConverterInterface {
|
|
7
|
-
toBigInt
|
|
8
|
-
fromBigInt
|
|
3
|
+
toBigInt(buf: Buffer, bigEndian?: boolean): bigint;
|
|
4
|
+
fromBigInt(num: BigInt, buf: Buffer, bigEndian?: boolean): Buffer;
|
|
9
5
|
}
|
|
10
6
|
|
|
11
|
-
declare
|
|
12
|
-
|
|
13
|
-
let converter: ConverterInterface
|
|
7
|
+
declare var process: {browser: boolean;};
|
|
14
8
|
|
|
15
|
-
|
|
9
|
+
let converter: ConverterInterface;
|
|
10
|
+
export let isNative = false;
|
|
16
11
|
|
|
17
12
|
if (!process.browser) {
|
|
18
13
|
try {
|
|
19
|
-
converter = require('bindings')('bigint_buffer')
|
|
20
|
-
isNative = !process.browser && converter !== undefined
|
|
14
|
+
converter = require('bindings')('bigint_buffer');
|
|
15
|
+
isNative = !process.browser && converter !== undefined;
|
|
21
16
|
} catch (e) {
|
|
22
17
|
console.warn(
|
|
23
|
-
|
|
18
|
+
'bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
|
|
@@ -29,25 +24,25 @@ if (!process.browser) {
|
|
|
29
24
|
* @param buf The little-endian buffer to convert
|
|
30
25
|
* @returns A BigInt with the little-endian representation of buf.
|
|
31
26
|
*/
|
|
32
|
-
export function toBigIntLE
|
|
27
|
+
export function toBigIntLE(buf: Buffer): bigint {
|
|
33
28
|
if (process.browser || converter === undefined) {
|
|
34
|
-
const reversed = Buffer.from(buf)
|
|
35
|
-
reversed.reverse()
|
|
36
|
-
const hex = reversed.toString('hex')
|
|
29
|
+
const reversed = Buffer.from(buf);
|
|
30
|
+
reversed.reverse();
|
|
31
|
+
const hex = reversed.toString('hex');
|
|
37
32
|
if (hex.length === 0) {
|
|
38
|
-
return BigInt(0)
|
|
33
|
+
return BigInt(0);
|
|
39
34
|
}
|
|
40
|
-
return BigInt(`0x${hex}`)
|
|
35
|
+
return BigInt(`0x${hex}`);
|
|
41
36
|
}
|
|
42
|
-
return converter.toBigInt(buf, false)
|
|
37
|
+
return converter.toBigInt(buf, false);
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
export function validateBigIntBuffer
|
|
40
|
+
export function validateBigIntBuffer(): boolean {
|
|
46
41
|
try {
|
|
47
|
-
const test = toBigIntLE(Buffer.from([0x01, 0x00]))
|
|
48
|
-
return test === BigInt(1)
|
|
42
|
+
const test = toBigIntLE(Buffer.from([0x01, 0x00]));
|
|
43
|
+
return test === BigInt(1);
|
|
49
44
|
} catch {
|
|
50
|
-
return false
|
|
45
|
+
return false;
|
|
51
46
|
}
|
|
52
47
|
}
|
|
53
48
|
|
|
@@ -56,15 +51,15 @@ export function validateBigIntBuffer (): boolean {
|
|
|
56
51
|
* @param buf The big-endian buffer to convert.
|
|
57
52
|
* @returns A BigInt with the big-endian representation of buf.
|
|
58
53
|
*/
|
|
59
|
-
export function toBigIntBE
|
|
54
|
+
export function toBigIntBE(buf: Buffer): bigint {
|
|
60
55
|
if (process.browser || converter === undefined) {
|
|
61
|
-
const hex = buf.toString('hex')
|
|
56
|
+
const hex = buf.toString('hex');
|
|
62
57
|
if (hex.length === 0) {
|
|
63
|
-
return BigInt(0)
|
|
58
|
+
return BigInt(0);
|
|
64
59
|
}
|
|
65
|
-
return BigInt(`0x${hex}`)
|
|
60
|
+
return BigInt(`0x${hex}`);
|
|
66
61
|
}
|
|
67
|
-
return converter.toBigInt(buf, true)
|
|
62
|
+
return converter.toBigInt(buf, true);
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
/**
|
|
@@ -73,16 +68,16 @@ export function toBigIntBE (buf: Buffer): bigint {
|
|
|
73
68
|
* @param width The number of bytes that the resulting buffer should be.
|
|
74
69
|
* @returns A little-endian buffer representation of num.
|
|
75
70
|
*/
|
|
76
|
-
export function toBufferLE
|
|
71
|
+
export function toBufferLE(num: bigint, width: number): Buffer {
|
|
77
72
|
if (process.browser || converter === undefined) {
|
|
78
|
-
const hex = num.toString(16)
|
|
73
|
+
const hex = num.toString(16);
|
|
79
74
|
const buffer =
|
|
80
|
-
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
81
|
-
buffer.reverse()
|
|
82
|
-
return buffer
|
|
75
|
+
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
76
|
+
buffer.reverse();
|
|
77
|
+
return buffer;
|
|
83
78
|
}
|
|
84
79
|
// Allocation is done here, since it is slower using napi in C
|
|
85
|
-
return converter.fromBigInt(num, Buffer.allocUnsafe(width), false)
|
|
80
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
|
|
86
81
|
}
|
|
87
82
|
|
|
88
83
|
/**
|
|
@@ -91,10 +86,125 @@ export function toBufferLE (num: bigint, width: number): Buffer {
|
|
|
91
86
|
* @param width The number of bytes that the resulting buffer should be.
|
|
92
87
|
* @returns A big-endian buffer representation of num.
|
|
93
88
|
*/
|
|
94
|
-
export function toBufferBE
|
|
89
|
+
export function toBufferBE(num: bigint, width: number): Buffer {
|
|
95
90
|
if (process.browser || converter === undefined) {
|
|
96
|
-
const hex = num.toString(16)
|
|
97
|
-
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
91
|
+
const hex = num.toString(16);
|
|
92
|
+
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
93
|
+
}
|
|
94
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ========== Conversion Utilities ==========
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Convert a bigint to a Buffer with automatic sizing.
|
|
101
|
+
* Uses big-endian encoding and calculates the minimum buffer size needed.
|
|
102
|
+
* @param num The bigint to convert
|
|
103
|
+
* @returns A big-endian Buffer representation
|
|
104
|
+
*/
|
|
105
|
+
export function bigintToBuf(num: bigint): Buffer {
|
|
106
|
+
if (num < BigInt(0)) {
|
|
107
|
+
throw new Error('bigintToBuf: negative bigint values are not supported');
|
|
108
|
+
}
|
|
109
|
+
if (num === BigInt(0)) {
|
|
110
|
+
return Buffer.from([0]);
|
|
111
|
+
}
|
|
112
|
+
// Calculate the number of bytes needed
|
|
113
|
+
const hex = num.toString(16);
|
|
114
|
+
const width = Math.ceil(hex.length / 2);
|
|
115
|
+
return toBufferBE(num, width);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Convert a Buffer to a bigint.
|
|
120
|
+
* Assumes big-endian encoding.
|
|
121
|
+
* @param buf The buffer to convert
|
|
122
|
+
* @returns A bigint representation of the buffer
|
|
123
|
+
*/
|
|
124
|
+
export function bufToBigint(buf: Buffer): bigint {
|
|
125
|
+
return toBigIntBE(buf);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Convert a bigint to a hexadecimal string.
|
|
130
|
+
* @param num The bigint to convert
|
|
131
|
+
* @returns A hexadecimal string (without '0x' prefix)
|
|
132
|
+
*/
|
|
133
|
+
export function bigintToHex(num: bigint): string {
|
|
134
|
+
if (num < BigInt(0)) {
|
|
135
|
+
throw new Error('bigintToHex: negative bigint values are not supported');
|
|
136
|
+
}
|
|
137
|
+
const hex = num.toString(16);
|
|
138
|
+
// Ensure even length for proper byte representation
|
|
139
|
+
return hex.length % 2 === 0 ? hex : '0' + hex;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Convert a hexadecimal string to a bigint.
|
|
144
|
+
* @param hex The hexadecimal string (with or without '0x' prefix)
|
|
145
|
+
* @returns A bigint representation
|
|
146
|
+
*/
|
|
147
|
+
export function hexToBigint(hex: string): bigint {
|
|
148
|
+
// Remove '0x' prefix if present
|
|
149
|
+
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
150
|
+
if (cleanHex.length === 0) {
|
|
151
|
+
return BigInt(0);
|
|
98
152
|
}
|
|
99
|
-
return
|
|
153
|
+
return BigInt(`0x${cleanHex}`);
|
|
100
154
|
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Convert a bigint to a decimal text string.
|
|
158
|
+
* @param num The bigint to convert
|
|
159
|
+
* @returns A decimal string representation
|
|
160
|
+
*/
|
|
161
|
+
export function bigintToText(num: bigint): string {
|
|
162
|
+
return num.toString(10);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Convert a decimal text string to a bigint.
|
|
167
|
+
* @param text The decimal string to convert
|
|
168
|
+
* @returns A bigint representation
|
|
169
|
+
*/
|
|
170
|
+
export function textToBigint(text: string): bigint {
|
|
171
|
+
if (!text?.trim()) {
|
|
172
|
+
throw new Error('textToBigint: input string cannot be empty');
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
return BigInt(text);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
throw new Error(`textToBigint: invalid decimal string "${text}"`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Convert a bigint to a base64 string.
|
|
183
|
+
* @param num The bigint to convert
|
|
184
|
+
* @returns A base64 string representation
|
|
185
|
+
*/
|
|
186
|
+
export function bigintToBase64(num: bigint): string {
|
|
187
|
+
if (num < BigInt(0)) {
|
|
188
|
+
throw new Error('bigintToBase64: negative bigint values are not supported');
|
|
189
|
+
}
|
|
190
|
+
const buf = bigintToBuf(num);
|
|
191
|
+
return buf.toString('base64');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Convert a base64 string to a bigint.
|
|
196
|
+
* @param base64 The base64 string to convert
|
|
197
|
+
* @returns A bigint representation
|
|
198
|
+
*/
|
|
199
|
+
export function base64ToBigint(base64: string): bigint {
|
|
200
|
+
if (!base64?.trim()) {
|
|
201
|
+
throw new Error('base64ToBigint: input string cannot be empty');
|
|
202
|
+
}
|
|
203
|
+
// Trim whitespace and validate base64 format (allows padding)
|
|
204
|
+
const cleaned = base64.trim();
|
|
205
|
+
if (!/^[A-Za-z0-9+/]+=*$/.test(cleaned)) {
|
|
206
|
+
throw new Error('base64ToBigint: invalid base64 string format');
|
|
207
|
+
}
|
|
208
|
+
const buf = Buffer.from(cleaned, 'base64');
|
|
209
|
+
return bufToBigint(buf);
|
|
210
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -2,18 +2,15 @@
|
|
|
2
2
|
"extends": "./node_modules/gts/tsconfig-google.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
4
|
"composite": true,
|
|
5
|
-
"moduleResolution": "nodenext",
|
|
6
|
-
"module": "NodeNext",
|
|
7
5
|
"rootDir": ".",
|
|
8
6
|
"outDir": "build",
|
|
9
7
|
"target" : "esnext",
|
|
10
8
|
"lib" : [ "esnext" ],
|
|
11
|
-
"sourceMap": true
|
|
12
|
-
"rootDirs": ["src", "src/conversion"],
|
|
13
|
-
"tsBuildInfoFile": "build/main.tsbuildinfo"
|
|
9
|
+
"sourceMap": true
|
|
14
10
|
},
|
|
15
11
|
"include": [
|
|
16
12
|
"src/*.ts",
|
|
13
|
+
"src/**/*.ts",
|
|
17
14
|
"test/*.ts",
|
|
18
15
|
"test/**/*.ts"
|
|
19
16
|
],
|
package/.eslintrc
DELETED
package/dist/index.bench.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.spec.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import 'mocha';
|