@gsknnft/bigint-buffer 1.3.0 → 1.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 +16 -8
- package/dist/browser.js +75 -0
- package/dist/index.cjs +90 -0
- package/dist/node.js +21 -10
- package/helper/bigint.d.ts +2 -2
- package/package.json +100 -163
- package/rollup.cjs.config.js +8 -0
- package/rollup.esm.config.js +13 -0
- package/src/index.bench.ts +119 -116
- package/src/index.spec.ts +78 -44
- package/src/index.ts +35 -40
- package/tsconfig.json +2 -5
- package/.eslintrc +0 -5
- 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.bench.ts
CHANGED
|
@@ -1,204 +1,207 @@
|
|
|
1
1
|
|
|
2
|
-
import * as benchmark from 'benchmark'
|
|
2
|
+
import * as benchmark from 'benchmark';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from './index';
|
|
5
|
+
|
|
6
|
+
const BN = require('bn.js');
|
|
5
7
|
|
|
6
|
-
const BN = require('bn.js')
|
|
7
8
|
|
|
8
9
|
// This file contains the benchmark test suite. It includes the benchmark and
|
|
9
10
|
// some lightweight boilerplate code for running benchmark.js. To
|
|
10
11
|
// run the benchmarks, execute `npm run benchmark` from the package directory.
|
|
11
|
-
const suite = new benchmark.Suite()
|
|
12
|
+
const suite = new benchmark.Suite();
|
|
12
13
|
|
|
13
14
|
interface BenchmarkRun {
|
|
14
|
-
name: string
|
|
15
|
-
hz: number
|
|
16
|
-
stats: benchmark.Stats
|
|
15
|
+
name: string;
|
|
16
|
+
hz: number;
|
|
17
|
+
stats: benchmark.Stats;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
// Tests the performance of a no-op.
|
|
20
|
-
suite.add('no-op', () => {})
|
|
21
|
+
suite.add('no-op', () => {});
|
|
21
22
|
|
|
22
23
|
// Test small strings (unaligned)
|
|
23
|
-
const smallHex = 'deadbeef'
|
|
24
|
-
const smallString = `0x${smallHex}
|
|
25
|
-
const smallBuf: Buffer = Buffer.from(smallHex, 'hex')
|
|
24
|
+
const smallHex = 'deadbeef';
|
|
25
|
+
const smallString = `0x${smallHex}`;
|
|
26
|
+
const smallBuf: Buffer = Buffer.from(smallHex, 'hex');
|
|
26
27
|
suite.add('bigint from hex string (small)', () => {
|
|
27
|
-
return BigInt(smallString)
|
|
28
|
-
})
|
|
28
|
+
return BigInt(smallString);
|
|
29
|
+
});
|
|
29
30
|
suite.add('bigint from hex string from buffer (small)', () => {
|
|
30
|
-
return BigInt(`0x${smallBuf.toString('hex')}`)
|
|
31
|
-
})
|
|
31
|
+
return BigInt(`0x${smallBuf.toString('hex')}`);
|
|
32
|
+
});
|
|
32
33
|
suite.add('BN from hex string from buffer (small)', () => {
|
|
33
|
-
return new BN(smallBuf.toString('hex'), 16)
|
|
34
|
-
})
|
|
34
|
+
return new BN(smallBuf.toString('hex'), 16);
|
|
35
|
+
});
|
|
35
36
|
suite.add('LE bigint-buffer ToBigInt (small)', () => {
|
|
36
|
-
return toBigIntLE(smallBuf)
|
|
37
|
-
})
|
|
37
|
+
return toBigIntLE(smallBuf);
|
|
38
|
+
});
|
|
38
39
|
suite.add('BE bigint-buffer ToBigInt (small)', () => {
|
|
39
|
-
return toBigIntBE(smallBuf)
|
|
40
|
-
})
|
|
40
|
+
return toBigIntBE(smallBuf);
|
|
41
|
+
});
|
|
41
42
|
|
|
42
43
|
// Test mid strings (aligned)
|
|
43
|
-
const midHex = 'badc0ffee0ddf00d'
|
|
44
|
-
const midString = `0x${midHex}
|
|
45
|
-
const midBuf: Buffer = Buffer.from(midHex, 'hex')
|
|
44
|
+
const midHex = 'badc0ffee0ddf00d';
|
|
45
|
+
const midString = `0x${midHex}`;
|
|
46
|
+
const midBuf: Buffer = Buffer.from(midHex, 'hex');
|
|
46
47
|
suite.add('bigint from hex string (mid, aligned)', () => {
|
|
47
|
-
return BigInt(midString)
|
|
48
|
-
})
|
|
48
|
+
return BigInt(midString);
|
|
49
|
+
});
|
|
49
50
|
suite.add('bigint from hex string from buffer (mid, aligned)', () => {
|
|
50
|
-
return BigInt(`0x${midBuf.toString('hex')}`)
|
|
51
|
-
})
|
|
51
|
+
return BigInt(`0x${midBuf.toString('hex')}`);
|
|
52
|
+
});
|
|
52
53
|
suite.add('BN from hex string from buffer (mid, aligned)', () => {
|
|
53
|
-
return new BN(midBuf.toString('hex'), 16)
|
|
54
|
-
})
|
|
54
|
+
return new BN(midBuf.toString('hex'), 16);
|
|
55
|
+
});
|
|
55
56
|
suite.add('LE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
56
|
-
return toBigIntLE(midBuf)
|
|
57
|
-
})
|
|
57
|
+
return toBigIntLE(midBuf);
|
|
58
|
+
});
|
|
58
59
|
suite.add('BE bigint-buffer ToBigInt (mid, aligned)', () => {
|
|
59
|
-
return toBigIntBE(midBuf)
|
|
60
|
-
})
|
|
60
|
+
return toBigIntBE(midBuf);
|
|
61
|
+
});
|
|
61
62
|
|
|
62
63
|
// Test huge strings
|
|
63
64
|
const hugeHex =
|
|
64
|
-
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d'
|
|
65
|
-
const hugeString = `0x${hugeHex}
|
|
66
|
-
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex')
|
|
65
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d';
|
|
66
|
+
const hugeString = `0x${hugeHex}`;
|
|
67
|
+
const hugeBuf: Buffer = Buffer.from(hugeHex, 'hex');
|
|
67
68
|
suite.add('bigint from hex string (huge)', () => {
|
|
68
|
-
return BigInt(hugeString)
|
|
69
|
-
})
|
|
69
|
+
return BigInt(hugeString);
|
|
70
|
+
});
|
|
70
71
|
suite.add('bigint from hex string from buffer (huge)', () => {
|
|
71
|
-
return BigInt(`0x${hugeBuf.toString('hex')}`)
|
|
72
|
-
})
|
|
72
|
+
return BigInt(`0x${hugeBuf.toString('hex')}`);
|
|
73
|
+
});
|
|
73
74
|
suite.add('BN from hex string from buffer (huge)', () => {
|
|
74
|
-
return new BN(hugeBuf.toString('hex'), 16)
|
|
75
|
-
})
|
|
75
|
+
return new BN(hugeBuf.toString('hex'), 16);
|
|
76
|
+
});
|
|
76
77
|
suite.add('LE bigint-buffer ToBigInt (huge)', () => {
|
|
77
|
-
return toBigIntLE(hugeBuf)
|
|
78
|
-
})
|
|
78
|
+
return toBigIntLE(hugeBuf);
|
|
79
|
+
});
|
|
79
80
|
suite.add('BE bigint-buffer ToBigInt (huge)', () => {
|
|
80
|
-
return toBigIntBE(hugeBuf)
|
|
81
|
-
})
|
|
81
|
+
return toBigIntBE(hugeBuf);
|
|
82
|
+
});
|
|
82
83
|
|
|
83
84
|
const bigIntToBufferWithStringBE = (int: bigint, width: number): Buffer => {
|
|
84
|
-
const hex = int.toString(16)
|
|
85
|
-
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
86
|
-
}
|
|
85
|
+
const hex = int.toString(16);
|
|
86
|
+
return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
87
|
+
};
|
|
87
88
|
|
|
88
89
|
const bigIntToBufferWithStringLE = (int: bigint, width: number): Buffer => {
|
|
89
|
-
const hex = int.toString(16)
|
|
90
|
+
const hex = int.toString(16);
|
|
90
91
|
const buffer =
|
|
91
|
-
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
|
|
92
|
-
buffer.reverse()
|
|
93
|
-
return buffer
|
|
94
|
-
}
|
|
92
|
+
Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
|
|
93
|
+
buffer.reverse();
|
|
94
|
+
return buffer;
|
|
95
|
+
};
|
|
95
96
|
|
|
96
97
|
// Test small toBuffer
|
|
97
|
-
const smallValue = 12345678n
|
|
98
|
+
const smallValue = 12345678n;
|
|
98
99
|
suite.add('LE bigint to hex string to buffer (small)', () => {
|
|
99
|
-
return bigIntToBufferWithStringLE(smallValue, 8)
|
|
100
|
-
})
|
|
100
|
+
return bigIntToBufferWithStringLE(smallValue, 8);
|
|
101
|
+
});
|
|
101
102
|
|
|
102
103
|
suite.add('BE bigint to hex string to buffer (small)', () => {
|
|
103
|
-
return bigIntToBufferWithStringBE(smallValue, 8)
|
|
104
|
-
})
|
|
104
|
+
return bigIntToBufferWithStringBE(smallValue, 8);
|
|
105
|
+
});
|
|
105
106
|
|
|
106
|
-
const bnSmallValue = new BN('12345678', 10)
|
|
107
|
+
const bnSmallValue = new BN('12345678', 10);
|
|
107
108
|
suite.add('BN to buffer (small)', () => {
|
|
108
|
-
return bnSmallValue.toBuffer(8)
|
|
109
|
-
})
|
|
109
|
+
return bnSmallValue.toBuffer(8);
|
|
110
|
+
});
|
|
110
111
|
|
|
111
112
|
suite.add('LE bigint-buffer to buffer (small)', () => {
|
|
112
|
-
return toBufferLE(smallValue, 8)
|
|
113
|
-
})
|
|
113
|
+
return toBufferLE(smallValue, 8);
|
|
114
|
+
});
|
|
114
115
|
|
|
115
116
|
suite.add('BE bigint-buffer to buffer (small)', () => {
|
|
116
|
-
return toBufferBE(smallValue, 8)
|
|
117
|
-
})
|
|
117
|
+
return toBufferBE(smallValue, 8);
|
|
118
|
+
});
|
|
119
|
+
|
|
118
120
|
|
|
119
121
|
// Test large toBuffer
|
|
120
122
|
const largeValue =
|
|
121
|
-
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn
|
|
123
|
+
0xbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dn;
|
|
122
124
|
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
123
|
-
return bigIntToBufferWithStringLE(largeValue, 24)
|
|
124
|
-
})
|
|
125
|
+
return bigIntToBufferWithStringLE(largeValue, 24);
|
|
126
|
+
});
|
|
125
127
|
|
|
126
128
|
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
127
|
-
return bigIntToBufferWithStringBE(largeValue, 24)
|
|
128
|
-
})
|
|
129
|
+
return bigIntToBufferWithStringBE(largeValue, 24);
|
|
130
|
+
});
|
|
129
131
|
|
|
130
132
|
const bnLargeValue = new BN(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
+
'badc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00dbadc0ffee0ddf00d',
|
|
134
|
+
16);
|
|
133
135
|
suite.add('BN to buffer (large)', () => {
|
|
134
|
-
return bnLargeValue.toBuffer(24)
|
|
135
|
-
})
|
|
136
|
+
return bnLargeValue.toBuffer(24);
|
|
137
|
+
});
|
|
136
138
|
|
|
137
139
|
suite.add('LE bigint-buffer to buffer (large)', () => {
|
|
138
|
-
return toBufferLE(largeValue, 24)
|
|
139
|
-
})
|
|
140
|
+
return toBufferLE(largeValue, 24);
|
|
141
|
+
});
|
|
140
142
|
|
|
141
143
|
suite.add('BE bigint-buffer to buffer (large)', () => {
|
|
142
|
-
return toBufferBE(largeValue, 24)
|
|
143
|
-
})
|
|
144
|
+
return toBufferBE(largeValue, 24);
|
|
145
|
+
});
|
|
144
146
|
|
|
145
147
|
suite.add('LE bigint to hex string to buffer (large)', () => {
|
|
146
|
-
return bigIntToBufferWithStringLE(largeValue, 8)
|
|
147
|
-
})
|
|
148
|
+
return bigIntToBufferWithStringLE(largeValue, 8);
|
|
149
|
+
});
|
|
148
150
|
|
|
149
151
|
suite.add('BE bigint to hex string to buffer (large)', () => {
|
|
150
|
-
return bigIntToBufferWithStringBE(largeValue, 8)
|
|
151
|
-
})
|
|
152
|
+
return bigIntToBufferWithStringBE(largeValue, 8);
|
|
153
|
+
});
|
|
152
154
|
|
|
153
155
|
suite.add('LE bigint-buffer to buffer (large, truncated)', () => {
|
|
154
|
-
return toBufferLE(largeValue, 8)
|
|
155
|
-
})
|
|
156
|
+
return toBufferLE(largeValue, 8);
|
|
157
|
+
});
|
|
156
158
|
|
|
157
159
|
suite.add('BE bigint-buffer to buffer (large, truncated)', () => {
|
|
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
|
|
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;
|
|
167
169
|
suite.add('Buffer equality comparison', () => {
|
|
168
|
-
return b1.compare(b2) === 0
|
|
169
|
-
})
|
|
170
|
+
return b1.compare(b2) === 0;
|
|
171
|
+
});
|
|
170
172
|
|
|
171
173
|
suite.add('BN equality comparison', () => {
|
|
172
|
-
return bn1.eq(bn2)
|
|
173
|
-
})
|
|
174
|
+
return bn1.eq(bn2);
|
|
175
|
+
});
|
|
174
176
|
|
|
175
177
|
suite.add('bigint equality comparison', () => {
|
|
176
|
-
return n1 === n2
|
|
177
|
-
})
|
|
178
|
+
return n1 === n2;
|
|
179
|
+
});
|
|
178
180
|
|
|
179
181
|
suite.add('BN multiply', () => {
|
|
180
|
-
return bn1.mul(bn2)
|
|
181
|
-
})
|
|
182
|
+
return bn1.mul(bn2);
|
|
183
|
+
});
|
|
182
184
|
|
|
183
185
|
suite.add('bigint multiply', () => {
|
|
184
|
-
return n1 * n2
|
|
185
|
-
})
|
|
186
|
+
return n1 * n2;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
186
190
|
|
|
187
|
-
// #endregion
|
|
188
191
|
|
|
189
192
|
// Reporter for each benchmark
|
|
190
193
|
suite.on('cycle', (event: benchmark.Event) => {
|
|
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)
|
|
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);
|
|
198
201
|
|
|
199
202
|
console.log(`${benchmarkRun.name}: ${ops}±${err}% ops/s ${meanInNanos}±${
|
|
200
|
-
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`)
|
|
201
|
-
})
|
|
203
|
+
stdDevInNanos} ns/op (${runs} run${runs === 0 ? '' : 's'})`);
|
|
204
|
+
});
|
|
202
205
|
|
|
203
206
|
// Runs the test suite
|
|
204
|
-
suite.run()
|
|
207
|
+
suite.run();
|
package/src/index.spec.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
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 ? require('../../dist/browser') :
|
|
8
|
+
require(path.join(__dirname, '../dist/node'));
|
|
11
9
|
const toBigIntBE = lib.toBigIntBE;
|
|
12
10
|
const toBigIntLE = lib.toBigIntLE;
|
|
13
11
|
const toBufferBE = lib.toBufferBE;
|
|
@@ -172,82 +170,118 @@ describe('Try buffer conversion (big endian)', () => {
|
|
|
172
170
|
|
|
173
171
|
describe('Try bigint conversion (little endian)', () => {
|
|
174
172
|
it('0 should equal 0n', () => {
|
|
175
|
-
|
|
173
|
+
toBufferLE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
174
|
+
0, 0, 0, 0, 0, 0, 0, 0
|
|
175
|
+
]));
|
|
176
176
|
});
|
|
177
177
|
|
|
178
178
|
it('1 should equal 1n', async () => {
|
|
179
|
-
|
|
179
|
+
toBufferLE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
180
|
+
1, 0, 0, 0, 0, 0, 0, 0
|
|
181
|
+
]));
|
|
180
182
|
});
|
|
181
183
|
|
|
184
|
+
|
|
182
185
|
it('1 should equal 1n (32 byte)', async () => {
|
|
183
|
-
|
|
184
|
-
|
|
186
|
+
toBufferLE(BigInt(`1`), 32)
|
|
187
|
+
.should.deep.equal(Buffer.from(
|
|
188
|
+
'0100000000000000000000000000000000000000000000000000000000000000',
|
|
189
|
+
'hex'));
|
|
185
190
|
});
|
|
186
191
|
|
|
187
192
|
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
188
|
-
|
|
193
|
+
toBufferLE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
194
|
+
0xad, 0xde, 0, 0, 0, 0
|
|
195
|
+
]));
|
|
189
196
|
});
|
|
190
197
|
|
|
191
198
|
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
192
|
-
|
|
199
|
+
toBufferLE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
200
|
+
0xef, 0xbe, 0xad, 0xde, 0, 0, 0, 0, 0
|
|
201
|
+
]));
|
|
193
202
|
});
|
|
194
203
|
|
|
195
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
196
|
-
|
|
197
|
-
|
|
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
|
+
});
|
|
198
210
|
|
|
199
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
});
|
|
204
219
|
|
|
205
220
|
it('long value should equal long val', async () => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
+
]));
|
|
211
227
|
});
|
|
212
228
|
});
|
|
213
229
|
|
|
230
|
+
|
|
214
231
|
describe('Try bigint conversion (big endian)', () => {
|
|
215
232
|
it('0 should equal 0n', () => {
|
|
216
|
-
|
|
233
|
+
toBufferBE(BigInt(`0`), 8).should.deep.equal(Buffer.from([
|
|
234
|
+
0, 0, 0, 0, 0, 0, 0, 0
|
|
235
|
+
]));
|
|
217
236
|
});
|
|
218
237
|
|
|
219
238
|
it('1 should equal 1n', async () => {
|
|
220
|
-
|
|
239
|
+
toBufferBE(BigInt(`1`), 8).should.deep.equal(Buffer.from([
|
|
240
|
+
0, 0, 0, 0, 0, 0, 0, 1
|
|
241
|
+
]));
|
|
221
242
|
});
|
|
222
243
|
|
|
223
244
|
it('1 should equal 1n (32 byte)', async () => {
|
|
224
|
-
|
|
225
|
-
|
|
245
|
+
toBufferBE(BigInt(`1`), 32)
|
|
246
|
+
.should.deep.equal(Buffer.from(
|
|
247
|
+
'0000000000000000000000000000000000000000000000000000000000000001',
|
|
248
|
+
'hex'));
|
|
226
249
|
});
|
|
227
250
|
|
|
228
251
|
it('0xdead should equal 0xdeadn (6 byte)', async () => {
|
|
229
|
-
|
|
252
|
+
toBufferBE(BigInt(`0xdead`), 6).should.deep.equal(Buffer.from([
|
|
253
|
+
0, 0, 0, 0, 0xde, 0xad
|
|
254
|
+
]));
|
|
230
255
|
});
|
|
231
256
|
|
|
232
257
|
it('0xdeadbeef should equal 0xdeadbeef0000000000n (9 byte)', async () => {
|
|
233
|
-
|
|
258
|
+
toBufferBE(BigInt(`0xdeadbeef`), 9).should.deep.equal(Buffer.from([
|
|
259
|
+
0, 0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef
|
|
260
|
+
]));
|
|
234
261
|
});
|
|
235
262
|
|
|
236
|
-
it('0xbadc0ffee0ddf00d should equal 0xbadc0ffee0ddf00dn (8 byte)',
|
|
237
|
-
|
|
238
|
-
|
|
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
|
+
});
|
|
239
269
|
|
|
240
|
-
it('0xbadc0ffee0ddf00ddeadbeef should equal 0xbadc0ffee0ddf00ddeadbeefn',
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
+
});
|
|
245
278
|
|
|
246
279
|
it('long value should equal long val', async () => {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
+
]));
|
|
252
286
|
});
|
|
253
287
|
});
|
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,10 @@ 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');
|
|
98
93
|
}
|
|
99
|
-
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true)
|
|
100
|
-
}
|
|
94
|
+
return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
|
|
95
|
+
}
|
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
|
],
|