@gsknnft/bigint-buffer 1.2.0 → 1.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.
Files changed (44) hide show
  1. package/.eslintrc +5 -0
  2. package/README.md +62 -11
  3. package/dist/node.js +10 -21
  4. package/helper/bigint.d.ts +2 -2
  5. package/okg.md +180 -0
  6. package/package.json +76 -11
  7. package/rollup.config.js +14 -11
  8. package/src/conversion/LICENSE +21 -0
  9. package/src/conversion/README.md +48 -0
  10. package/src/conversion/docs/README.md +34 -0
  11. package/src/conversion/docs/functions/base64ToBigint.md +27 -0
  12. package/src/conversion/docs/functions/bigintToBase64.md +43 -0
  13. package/src/conversion/docs/functions/bigintToBuf.md +35 -0
  14. package/src/conversion/docs/functions/bigintToHex.md +43 -0
  15. package/src/conversion/docs/functions/bigintToText.md +31 -0
  16. package/src/conversion/docs/functions/bufToBigint.md +25 -0
  17. package/src/conversion/docs/functions/bufToHex.md +37 -0
  18. package/src/conversion/docs/functions/bufToText.md +27 -0
  19. package/src/conversion/docs/functions/hexToBigint.md +29 -0
  20. package/src/conversion/docs/functions/hexToBuf.md +37 -0
  21. package/src/conversion/docs/functions/parseHex.md +45 -0
  22. package/src/conversion/docs/functions/textToBigint.md +27 -0
  23. package/src/conversion/docs/functions/textToBuf.md +33 -0
  24. package/src/conversion/docs/functions/toBigIntBE.md +27 -0
  25. package/src/conversion/docs/functions/toBigIntLE.md +27 -0
  26. package/src/conversion/docs/functions/toBufferBE.md +33 -0
  27. package/src/conversion/docs/functions/toBufferLE.md +33 -0
  28. package/src/conversion/docs/functions/validateBigIntBuffer.md +15 -0
  29. package/src/conversion/docs/type-aliases/TypedArray.md +11 -0
  30. package/src/conversion/docs/variables/isNative.md +11 -0
  31. package/src/conversion/example.cjs +9 -0
  32. package/src/conversion/example.esm.js +11 -0
  33. package/src/conversion/package.json +182 -0
  34. package/src/conversion/pnpm-lock.yaml +5571 -0
  35. package/src/conversion/tsconfig.rollup.json +9 -0
  36. package/src/conversion/typedoc.json +5 -0
  37. package/src/index.bench.ts +116 -119
  38. package/src/index.spec.ts +44 -78
  39. package/src/index.ts +40 -35
  40. package/src/types/bindings.d.t.s +4 -0
  41. package/tsconfig.json +5 -2
  42. package/tsconfig.lint.json +5 -0
  43. package/vitest.config.ts +10 -0
  44. package/dist/browser.js +0 -75
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": false,
5
+ "outDir": "./.rollup-tmp",
6
+ "composite": false
7
+ },
8
+ "include": ["src/ts/**/*"]
9
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "readme": "none",
3
+ "excludePrivate": true,
4
+ "plugin": ["typedoc-plugin-markdown"]
5
+ }
@@ -1,207 +1,204 @@
1
1
 
2
- import * as benchmark from 'benchmark';
2
+ import * as benchmark from 'benchmark'
3
3
 
4
- import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from './index';
5
-
6
- const BN = require('bn.js');
4
+ import { toBigIntBE, toBigIntLE, toBufferBE, toBufferLE } from './index.js'
7
5
 
6
+ const BN = require('bn.js')
8
7
 
9
8
  // This file contains the benchmark test suite. It includes the benchmark and
10
9
  // some lightweight boilerplate code for running benchmark.js. To
11
10
  // run the benchmarks, execute `npm run benchmark` from the package directory.
12
- const suite = new benchmark.Suite();
11
+ const suite = new benchmark.Suite()
13
12
 
14
13
  interface BenchmarkRun {
15
- name: string;
16
- hz: number;
17
- stats: benchmark.Stats;
14
+ name: string
15
+ hz: number
16
+ stats: benchmark.Stats
18
17
  }
19
18
 
20
19
  // Tests the performance of a no-op.
21
- suite.add('no-op', () => {});
20
+ suite.add('no-op', () => {})
22
21
 
23
22
  // Test small strings (unaligned)
24
- const smallHex = 'deadbeef';
25
- const smallString = `0x${smallHex}`;
26
- const smallBuf: Buffer = Buffer.from(smallHex, 'hex');
23
+ const smallHex = 'deadbeef'
24
+ const smallString = `0x${smallHex}`
25
+ const smallBuf: Buffer = Buffer.from(smallHex, 'hex')
27
26
  suite.add('bigint from hex string (small)', () => {
28
- return BigInt(smallString);
29
- });
27
+ return BigInt(smallString)
28
+ })
30
29
  suite.add('bigint from hex string from buffer (small)', () => {
31
- return BigInt(`0x${smallBuf.toString('hex')}`);
32
- });
30
+ return BigInt(`0x${smallBuf.toString('hex')}`)
31
+ })
33
32
  suite.add('BN from hex string from buffer (small)', () => {
34
- return new BN(smallBuf.toString('hex'), 16);
35
- });
33
+ return new BN(smallBuf.toString('hex'), 16)
34
+ })
36
35
  suite.add('LE bigint-buffer ToBigInt (small)', () => {
37
- return toBigIntLE(smallBuf);
38
- });
36
+ return toBigIntLE(smallBuf)
37
+ })
39
38
  suite.add('BE bigint-buffer ToBigInt (small)', () => {
40
- return toBigIntBE(smallBuf);
41
- });
39
+ return toBigIntBE(smallBuf)
40
+ })
42
41
 
43
42
  // Test mid strings (aligned)
44
- const midHex = 'badc0ffee0ddf00d';
45
- const midString = `0x${midHex}`;
46
- const midBuf: Buffer = Buffer.from(midHex, 'hex');
43
+ const midHex = 'badc0ffee0ddf00d'
44
+ const midString = `0x${midHex}`
45
+ const midBuf: Buffer = Buffer.from(midHex, 'hex')
47
46
  suite.add('bigint from hex string (mid, aligned)', () => {
48
- return BigInt(midString);
49
- });
47
+ return BigInt(midString)
48
+ })
50
49
  suite.add('bigint from hex string from buffer (mid, aligned)', () => {
51
- return BigInt(`0x${midBuf.toString('hex')}`);
52
- });
50
+ return BigInt(`0x${midBuf.toString('hex')}`)
51
+ })
53
52
  suite.add('BN from hex string from buffer (mid, aligned)', () => {
54
- return new BN(midBuf.toString('hex'), 16);
55
- });
53
+ return new BN(midBuf.toString('hex'), 16)
54
+ })
56
55
  suite.add('LE bigint-buffer ToBigInt (mid, aligned)', () => {
57
- return toBigIntLE(midBuf);
58
- });
56
+ return toBigIntLE(midBuf)
57
+ })
59
58
  suite.add('BE bigint-buffer ToBigInt (mid, aligned)', () => {
60
- return toBigIntBE(midBuf);
61
- });
59
+ return toBigIntBE(midBuf)
60
+ })
62
61
 
63
62
  // Test huge strings
64
63
  const hugeHex =
65
- 'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d';
66
- const hugeString = `0x${hugeHex}`;
67
- const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex');
64
+ 'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d'
65
+ const hugeString = `0x${hugeHex}`
66
+ const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex')
68
67
  suite.add('bigint from hex string (huge)', () => {
69
- return BigInt(hugeString);
70
- });
68
+ return BigInt(hugeString)
69
+ })
71
70
  suite.add('bigint from hex string from buffer (huge)', () => {
72
- return BigInt(`0x${hugeBuf.toString('hex')}`);
73
- });
71
+ return BigInt(`0x${hugeBuf.toString('hex')}`)
72
+ })
74
73
  suite.add('BN from hex string from buffer (huge)', () => {
75
- return new BN(hugeBuf.toString('hex'), 16);
76
- });
74
+ return new BN(hugeBuf.toString('hex'), 16)
75
+ })
77
76
  suite.add('LE bigint-buffer ToBigInt (huge)', () => {
78
- return toBigIntLE(hugeBuf);
79
- });
77
+ return toBigIntLE(hugeBuf)
78
+ })
80
79
  suite.add('BE bigint-buffer ToBigInt (huge)', () => {
81
- return toBigIntBE(hugeBuf);
82
- });
80
+ return toBigIntBE(hugeBuf)
81
+ })
83
82
 
84
83
  const bigIntToBufferWithStringBE = (int: bigint, width: number): Buffer => {
85
- const hex = int.toString(16);
86
- return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
87
- };
84
+ const hex = int.toString(16)
85
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
86
+ }
88
87
 
89
88
  const bigIntToBufferWithStringLE = (int: bigint, width: number): Buffer => {
90
- const hex = int.toString(16);
89
+ const hex = int.toString(16)
91
90
  const buffer =
92
- Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
93
- buffer.reverse();
94
- return buffer;
95
- };
91
+ Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
92
+ buffer.reverse()
93
+ return buffer
94
+ }
96
95
 
97
96
  // Test small toBuffer
98
- const smallValue = 12345678n;
97
+ const smallValue = 12345678n
99
98
  suite.add('LE bigint to hex string to buffer (small)', () => {
100
- return bigIntToBufferWithStringLE(smallValue, 8);
101
- });
99
+ return bigIntToBufferWithStringLE(smallValue, 8)
100
+ })
102
101
 
103
102
  suite.add('BE bigint to hex string to buffer (small)', () => {
104
- return bigIntToBufferWithStringBE(smallValue, 8);
105
- });
103
+ return bigIntToBufferWithStringBE(smallValue, 8)
104
+ })
106
105
 
107
- const bnSmallValue = new BN('12345678', 10);
106
+ const bnSmallValue = new BN('12345678', 10)
108
107
  suite.add('BN to buffer (small)', () => {
109
- return bnSmallValue.toBuffer(8);
110
- });
108
+ return bnSmallValue.toBuffer(8)
109
+ })
111
110
 
112
111
  suite.add('LE bigint-buffer to buffer (small)', () => {
113
- return toBufferLE(smallValue, 8);
114
- });
112
+ return toBufferLE(smallValue, 8)
113
+ })
115
114
 
116
115
  suite.add('BE bigint-buffer to buffer (small)', () => {
117
- return toBufferBE(smallValue, 8);
118
- });
119
-
116
+ return toBufferBE(smallValue, 8)
117
+ })
120
118
 
121
119
  // Test large toBuffer
122
120
  const largeValue =
123
- 0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn;
121
+ 0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn
124
122
  suite.add('LE bigint to hex string to buffer (large)', () => {
125
- return bigIntToBufferWithStringLE(largeValue, 24);
126
- });
123
+ return bigIntToBufferWithStringLE(largeValue, 24)
124
+ })
127
125
 
128
126
  suite.add('BE bigint to hex string to buffer (large)', () => {
129
- return bigIntToBufferWithStringBE(largeValue, 24);
130
- });
127
+ return bigIntToBufferWithStringBE(largeValue, 24)
128
+ })
131
129
 
132
130
  const bnLargeValue = new BN(
133
- 'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
134
- 16);
131
+ 'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
132
+ 16)
135
133
  suite.add('BN to buffer (large)', () => {
136
- return bnLargeValue.toBuffer(24);
137
- });
134
+ return bnLargeValue.toBuffer(24)
135
+ })
138
136
 
139
137
  suite.add('LE bigint-buffer to buffer (large)', () => {
140
- return toBufferLE(largeValue, 24);
141
- });
138
+ return toBufferLE(largeValue, 24)
139
+ })
142
140
 
143
141
  suite.add('BE bigint-buffer to buffer (large)', () => {
144
- return toBufferBE(largeValue, 24);
145
- });
142
+ return toBufferBE(largeValue, 24)
143
+ })
146
144
 
147
145
  suite.add('LE bigint to hex string to buffer (large)', () => {
148
- return bigIntToBufferWithStringLE(largeValue, 8);
149
- });
146
+ return bigIntToBufferWithStringLE(largeValue, 8)
147
+ })
150
148
 
151
149
  suite.add('BE bigint to hex string to buffer (large)', () => {
152
- return bigIntToBufferWithStringBE(largeValue, 8);
153
- });
150
+ return bigIntToBufferWithStringBE(largeValue, 8)
151
+ })
154
152
 
155
153
  suite.add('LE bigint-buffer to buffer (large, truncated)', () => {
156
- return toBufferLE(largeValue, 8);
157
- });
154
+ return toBufferLE(largeValue, 8)
155
+ })
158
156
 
159
157
  suite.add('BE bigint-buffer to buffer (large, truncated)', () => {
160
- return toBufferBE(largeValue, 8);
161
- });
162
-
163
- const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
164
- const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex');
165
- const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
166
- const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex');
167
- const n1 = 0x0123456789ABCDEF0123456789ABCDEFn;
168
- const n2 = 0x0123456789ABCDEF0123456789ABCDEFn;
158
+ return toBufferBE(largeValue, 8)
159
+ })
160
+
161
+ const b1 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex')
162
+ const b2 = Buffer.from('0123456789ABCDEF0123456789ABCDEF', 'hex')
163
+ const bn1 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex')
164
+ const bn2 = new BN('0123456789ABCDEF0123456789ABCDEF', 'hex')
165
+ const n1 = 0x0123456789ABCDEF0123456789ABCDEFn
166
+ const n2 = 0x0123456789ABCDEF0123456789ABCDEFn
169
167
  suite.add('Buffer equality comparison', () => {
170
- return b1.compare(b2) === 0;
171
- });
168
+ return b1.compare(b2) === 0
169
+ })
172
170
 
173
171
  suite.add('BN equality comparison', () => {
174
- return bn1.eq(bn2);
175
- });
172
+ return bn1.eq(bn2)
173
+ })
176
174
 
177
175
  suite.add('bigint equality comparison', () => {
178
- return n1 === n2;
179
- });
176
+ return n1 === n2
177
+ })
180
178
 
181
179
  suite.add('BN multiply', () => {
182
- return bn1.mul(bn2);
183
- });
180
+ return bn1.mul(bn2)
181
+ })
184
182
 
185
183
  suite.add('bigint multiply', () => {
186
- return n1 * n2;
187
- });
188
-
189
- //#endregion
184
+ return n1 * n2
185
+ })
190
186
 
187
+ // #endregion
191
188
 
192
189
  // Reporter for each benchmark
193
190
  suite.on('cycle', (event: benchmark.Event) => {
194
- const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun;
195
- const stats = benchmarkRun.stats as benchmark.Stats;
196
- const meanInNanos = (stats.mean * 1000000000).toFixed(2);
197
- const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3);
198
- const runs = stats.sample.length;
199
- const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0);
200
- const err = stats.rme.toFixed(2);
191
+ const benchmarkRun: BenchmarkRun = event.target as BenchmarkRun
192
+ const stats = benchmarkRun.stats
193
+ const meanInNanos = (stats.mean * 1000000000).toFixed(2)
194
+ const stdDevInNanos = (stats.deviation * 1000000000).toFixed(3)
195
+ const runs = stats.sample.length
196
+ const ops = benchmarkRun.hz.toFixed(benchmarkRun.hz < 100 ? 2 : 0)
197
+ const err = stats.rme.toFixed(2)
201
198
 
202
199
  console.log(`${benchmarkRun.name}: ${ops}±${err}% ops/s ${meanInNanos}±${
203
- stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`);
204
- });
200
+ stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`)
201
+ })
205
202
 
206
203
  // Runs the test suite
207
- suite.run();
204
+ suite.run()
package/src/index.spec.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import 'mocha';
2
2
  declare var process: {browser: boolean;};
3
-
3
+ import { expect } from 'chai';
4
+ import path from 'path'
4
5
  import * as chai from 'chai';
5
- import * as path from 'path';
6
+ // import lib from '../dist/index.js';
7
+ const lib = require(process.browser
8
+ ? path.resolve(__dirname, '../dist/index.js')
9
+ : path.resolve(__dirname, '../dist/node.js'));
6
10
 
7
- const lib = process.browser ? require('../../dist/browser') :
8
- require(path.join(__dirname, '../dist/node'));
9
11
  const toBigIntBE = lib.toBigIntBE;
10
12
  const toBigIntLE = lib.toBigIntLE;
11
13
  const toBufferBE = lib.toBufferBE;
@@ -170,118 +172,82 @@ describe('Try buffer conversion (big endian)', () => {
170
172
 
171
173
  describe('Try bigint conversion (little endian)', () => {
172
174
  it('0 should equal 0n', () => {
173
- toBufferLE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
174
- 0, 0, 0, 0, 0, 0, 0, 0
175
- ]));
175
+ expect(toBufferLE(BigInt('0'), 8)).to.deep.equal(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]));
176
176
  });
177
177
 
178
178
  it('1 should equal 1n', async () => {
179
- toBufferLE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
180
- 1, 0, 0, 0, 0, 0, 0, 0
181
- ]));
179
+ expect(toBufferLE(BigInt('1'), 8)).to.deep.equal(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0]));
182
180
  });
183
181
 
184
-
185
182
  it('1 should equal 1n (32 byte)', async () => {
186
- toBufferLE(BigInt(`1`), 32)
187
- .should.deep.equal(Buffer.from(
188
- '0100000000000000000000000000000000000000000000000000000000000000',
189
- 'hex'));
183
+ expect(toBufferLE(BigInt('1'), 32)).to.deep.equal(Buffer.from(
184
+ '0100000000000000000000000000000000000000000000000000000000000000', 'hex'));
190
185
  });
191
186
 
192
187
  it('0xdead should equal 0xdeadn (6 byte)', async () => {
193
- toBufferLE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
194
- 0xad, 0xde, 0, 0, 0, 0
195
- ]));
188
+ expect(toBufferLE(BigInt('0xdead'), 6)).to.deep.equal(Buffer.from([0xad, 0xde, 0, 0, 0, 0]));
196
189
  });
197
190
 
198
191
  it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
199
- toBufferLE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
200
- 0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0
201
- ]));
192
+ expect(toBufferLE(BigInt('0xdeadbeef'), 9)).to.deep.equal(Buffer.from([0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0]));
202
193
  });
203
194
 
204
- it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
205
- async () => {
206
- toBufferLE(BigInt(`0xbadc0ffee0ddf00d`), 8)
207
- .should.deep.equal(
208
- Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba]));
209
- });
195
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)', async () => {
196
+ expect(toBufferLE(BigInt('0xbadc0ffee0ddf00d'), 8)).to.deep.equal(Buffer.from([0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba]));
197
+ });
210
198
 
211
- it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
212
- async () => {
213
- toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
214
- .should.deep.equal(Buffer.from([
215
- 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc,
216
- 0xba
217
- ]));
218
- });
199
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn', async () => {
200
+ expect(toBufferLE(BigInt('0xbadc0ffee0ddf00ddeadbeef'), 12)).to.deep.equal(Buffer.from([
201
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
202
+ ]));
203
+ });
219
204
 
220
205
  it('long value should equal long val', async () => {
221
- toBufferLE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
222
- .should.deep.equal(Buffer.from([
223
- 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
224
- 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
225
- 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
226
- ]));
206
+ expect(toBufferLE(BigInt('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef'), 24)).to.deep.equal(Buffer.from([
207
+ 0xef, 0xbe, 0xad, 0xde, 0x0d, 0xf0, 0xdd, 0xe0,
208
+ 0xfe, 0x0f, 0xdc, 0xba, 0xef, 0xbe, 0xad, 0xde,
209
+ 0x0d, 0xf0, 0xdd, 0xe0, 0xfe, 0x0f, 0xdc, 0xba
210
+ ]));
227
211
  });
228
212
  });
229
213
 
230
-
231
214
  describe('Try bigint conversion (big endian)', () => {
232
215
  it('0 should equal 0n', () => {
233
- toBufferBE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
234
- 0, 0, 0, 0, 0, 0, 0, 0
235
- ]));
216
+ expect(toBufferBE(BigInt('0'), 8)).to.deep.equal(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]));
236
217
  });
237
218
 
238
219
  it('1 should equal 1n', async () => {
239
- toBufferBE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
240
- 0, 0, 0, 0, 0, 0, 0, 1
241
- ]));
220
+ expect(toBufferBE(BigInt('1'), 8)).to.deep.equal(Buffer.from([0, 0, 0, 0, 0, 0, 0, 1]));
242
221
  });
243
222
 
244
223
  it('1 should equal 1n (32 byte)', async () => {
245
- toBufferBE(BigInt(`1`), 32)
246
- .should.deep.equal(Buffer.from(
247
- '0000000000000000000000000000000000000000000000000000000000000001',
248
- 'hex'));
224
+ expect(toBufferBE(BigInt('1'), 32)).to.deep.equal(Buffer.from(
225
+ '0000000000000000000000000000000000000000000000000000000000000001', 'hex'));
249
226
  });
250
227
 
251
228
  it('0xdead should equal 0xdeadn (6 byte)', async () => {
252
- toBufferBE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
253
- 0, 0, 0, 0, 0xde, 0xad
254
- ]));
229
+ expect(toBufferBE(BigInt('0xdead'), 6)).to.deep.equal(Buffer.from([0, 0, 0, 0, 0xde, 0xad]));
255
230
  });
256
231
 
257
232
  it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
258
- toBufferBE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
259
- 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef
260
- ]));
233
+ expect(toBufferBE(BigInt('0xdeadbeef'), 9)).to.deep.equal(Buffer.from([0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef]));
261
234
  });
262
235
 
263
- it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
264
- async () => {
265
- toBufferBE(BigInt(`0xbadc0ffee0ddf00d`), 8)
266
- .should.deep.equal(
267
- Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d]));
268
- });
236
+ it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)', async () => {
237
+ expect(toBufferBE(BigInt('0xbadc0ffee0ddf00d'), 8)).to.deep.equal(Buffer.from([0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d]));
238
+ });
269
239
 
270
- it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
271
- async () => {
272
- toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeef`), 12)
273
- .should.deep.equal(Buffer.from([
274
- 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe,
275
- 0xef
276
- ]));
277
- });
240
+ it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn', async () => {
241
+ expect(toBufferBE(BigInt('0xbadc0ffee0ddf00ddeadbeef'), 12)).to.deep.equal(Buffer.from([
242
+ 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
243
+ ]));
244
+ });
278
245
 
279
246
  it('long value should equal long val', async () => {
280
- toBufferBE(BigInt(`0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef`), 24)
281
- .should.deep.equal(Buffer.from([
282
- 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d,
283
- 0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe,
284
- 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
285
- ]));
247
+ expect(toBufferBE(BigInt('0xbadc0ffee0ddf00ddeadbeefbadc0ffee0ddf00ddeadbeef'), 24)).to.deep.equal(Buffer.from([
248
+ 0xba, 0xdc, 0x0f, 0xfe, 0xe0, 0xdd, 0xf0, 0x0d,
249
+ 0xde, 0xad, 0xbe, 0xef, 0xba, 0xdc, 0x0f, 0xfe,
250
+ 0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
251
+ ]));
286
252
  });
287
253
  });
package/src/index.ts CHANGED
@@ -1,21 +1,26 @@
1
1
 
2
+ // etc.
3
+ import bindings from 'bindings'
4
+ const native = bindings('bigint_buffer')
5
+
2
6
  interface ConverterInterface {
3
- toBigInt(buf: Buffer, bigEndian?: boolean): bigint;
4
- fromBigInt(num: BigInt, buf: Buffer, bigEndian?: boolean): Buffer;
7
+ toBigInt: (buf: Buffer, bigEndian?: boolean) => bigint
8
+ fromBigInt: (num: bigint, buf: Buffer, bigEndian?: boolean) => Buffer
5
9
  }
6
10
 
7
- declare var process: {browser: boolean;};
11
+ declare let process: { browser: boolean }
12
+
13
+ let converter: ConverterInterface
8
14
 
9
- let converter: ConverterInterface;
10
- export let isNative = false;
15
+ export let isNative = false
11
16
 
12
17
  if (!process.browser) {
13
18
  try {
14
- converter = require('bindings')('bigint_buffer');
15
- isNative = !process.browser && converter !== undefined;
19
+ converter = require('bindings')('bigint_buffer')
20
+ isNative = !process.browser && converter !== undefined
16
21
  } catch (e) {
17
22
  console.warn(
18
- 'bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
23
+ 'bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)')
19
24
  }
20
25
  }
21
26
 
@@ -24,25 +29,25 @@ if (!process.browser) {
24
29
  * @param buf The little-endian buffer to convert
25
30
  * @returns A BigInt with the little-endian representation of buf.
26
31
  */
27
- export function toBigIntLE(buf: Buffer): bigint {
32
+ export function toBigIntLE (buf: Buffer): bigint {
28
33
  if (process.browser || converter === undefined) {
29
- const reversed = Buffer.from(buf);
30
- reversed.reverse();
31
- const hex = reversed.toString('hex');
34
+ const reversed = Buffer.from(buf)
35
+ reversed.reverse()
36
+ const hex = reversed.toString('hex')
32
37
  if (hex.length === 0) {
33
- return BigInt(0);
38
+ return BigInt(0)
34
39
  }
35
- return BigInt(`0x${hex}`);
40
+ return BigInt(`0x${hex}`)
36
41
  }
37
- return converter.toBigInt(buf, false);
42
+ return converter.toBigInt(buf, false)
38
43
  }
39
44
 
40
- export function validateBigIntBuffer(): boolean {
45
+ export function validateBigIntBuffer (): boolean {
41
46
  try {
42
- const test = toBigIntLE(Buffer.from([0x01, 0x00]));
43
- return test === BigInt(1);
47
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]))
48
+ return test === BigInt(1)
44
49
  } catch {
45
- return false;
50
+ return false
46
51
  }
47
52
  }
48
53
 
@@ -51,15 +56,15 @@ export function validateBigIntBuffer(): boolean {
51
56
  * @param buf The big-endian buffer to convert.
52
57
  * @returns A BigInt with the big-endian representation of buf.
53
58
  */
54
- export function toBigIntBE(buf: Buffer): bigint {
59
+ export function toBigIntBE (buf: Buffer): bigint {
55
60
  if (process.browser || converter === undefined) {
56
- const hex = buf.toString('hex');
61
+ const hex = buf.toString('hex')
57
62
  if (hex.length === 0) {
58
- return BigInt(0);
63
+ return BigInt(0)
59
64
  }
60
- return BigInt(`0x${hex}`);
65
+ return BigInt(`0x${hex}`)
61
66
  }
62
- return converter.toBigInt(buf, true);
67
+ return converter.toBigInt(buf, true)
63
68
  }
64
69
 
65
70
  /**
@@ -68,16 +73,16 @@ export function toBigIntBE(buf: Buffer): bigint {
68
73
  * @param width The number of bytes that the resulting buffer should be.
69
74
  * @returns A little-endian buffer representation of num.
70
75
  */
71
- export function toBufferLE(num: bigint, width: number): Buffer {
76
+ export function toBufferLE (num: bigint, width: number): Buffer {
72
77
  if (process.browser || converter === undefined) {
73
- const hex = num.toString(16);
78
+ const hex = num.toString(16)
74
79
  const buffer =
75
- Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
76
- buffer.reverse();
77
- return buffer;
80
+ Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
81
+ buffer.reverse()
82
+ return buffer
78
83
  }
79
84
  // Allocation is done here, since it is slower using napi in C
80
- return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
85
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false)
81
86
  }
82
87
 
83
88
  /**
@@ -86,10 +91,10 @@ export function toBufferLE(num: bigint, width: number): Buffer {
86
91
  * @param width The number of bytes that the resulting buffer should be.
87
92
  * @returns A big-endian buffer representation of num.
88
93
  */
89
- export function toBufferBE(num: bigint, width: number): Buffer {
94
+ export function toBufferBE (num: bigint, width: number): Buffer {
90
95
  if (process.browser || converter === undefined) {
91
- const hex = num.toString(16);
92
- return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
96
+ const hex = num.toString(16)
97
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
93
98
  }
94
- return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
95
- }
99
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true)
100
+ }
@@ -0,0 +1,4 @@
1
+ declare module 'bindings' {
2
+ function bindings(name: string): any;
3
+ export = bindings;
4
+ }