@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.
Files changed (50) hide show
  1. package/README.md +16 -8
  2. package/dist/index.cjs +205 -0
  3. package/dist/index.d.ts +50 -0
  4. package/dist/index.js +203 -0
  5. package/dist/node.js +19 -4
  6. package/helper/bigint.d.ts +2 -2
  7. package/package.json +11 -78
  8. package/rollup.cjs.config.js +8 -0
  9. package/rollup.esm.config.js +15 -0
  10. package/src/index.bench.ts +119 -116
  11. package/src/index.spec.ts +234 -43
  12. package/src/index.ts +149 -39
  13. package/tsconfig.json +2 -5
  14. package/.eslintrc +0 -5
  15. package/dist/index.bench.d.ts +0 -1
  16. package/dist/index.spec.d.ts +0 -1
  17. package/okg.md +0 -180
  18. package/rollup.config.js +0 -16
  19. package/src/conversion/LICENSE +0 -21
  20. package/src/conversion/README.md +0 -48
  21. package/src/conversion/docs/README.md +0 -34
  22. package/src/conversion/docs/functions/base64ToBigint.md +0 -27
  23. package/src/conversion/docs/functions/bigintToBase64.md +0 -43
  24. package/src/conversion/docs/functions/bigintToBuf.md +0 -35
  25. package/src/conversion/docs/functions/bigintToHex.md +0 -43
  26. package/src/conversion/docs/functions/bigintToText.md +0 -31
  27. package/src/conversion/docs/functions/bufToBigint.md +0 -25
  28. package/src/conversion/docs/functions/bufToHex.md +0 -37
  29. package/src/conversion/docs/functions/bufToText.md +0 -27
  30. package/src/conversion/docs/functions/hexToBigint.md +0 -29
  31. package/src/conversion/docs/functions/hexToBuf.md +0 -37
  32. package/src/conversion/docs/functions/parseHex.md +0 -45
  33. package/src/conversion/docs/functions/textToBigint.md +0 -27
  34. package/src/conversion/docs/functions/textToBuf.md +0 -33
  35. package/src/conversion/docs/functions/toBigIntBE.md +0 -27
  36. package/src/conversion/docs/functions/toBigIntLE.md +0 -27
  37. package/src/conversion/docs/functions/toBufferBE.md +0 -33
  38. package/src/conversion/docs/functions/toBufferLE.md +0 -33
  39. package/src/conversion/docs/functions/validateBigIntBuffer.md +0 -15
  40. package/src/conversion/docs/type-aliases/TypedArray.md +0 -11
  41. package/src/conversion/docs/variables/isNative.md +0 -11
  42. package/src/conversion/example.cjs +0 -9
  43. package/src/conversion/example.esm.js +0 -11
  44. package/src/conversion/package.json +0 -182
  45. package/src/conversion/pnpm-lock.yaml +0 -5571
  46. package/src/conversion/tsconfig.rollup.json +0 -9
  47. package/src/conversion/typedoc.json +0 -5
  48. package/src/types/bindings.d.t.s +0 -4
  49. package/tsconfig.lint.json +0 -5
  50. package/vitest.config.ts +0 -10
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  As of October 2025, `bigint-buffer@1.1.5` is **compromised and flagged by multiple audit tools** due to unresolved vulnerabilities in its native bindings and transitive dependencies. No upstream patch has been published.
12
12
 
13
- This fork — `@gsknnft/bigint-buffer@1.3.0` — is a **sovereign override**:
13
+ This fork — `@gsknnft/bigint-buffer@1.3.2` — is a **sovereign override**:
14
14
  - ✅ Rebuilt with modern TypeScript and Rollup
15
15
  - ✅ Native bindings patched and rebuilt via `node-gyp`
16
16
  - ✅ Browser fallback formalized via `"browser"` field
@@ -22,7 +22,7 @@ If you're using `bigint-buffer` in a secure or reproducible system, **migrate to
22
22
  ```json
23
23
  "pnpm": {
24
24
  "overrides": {
25
- "bigint-buffer": "@gsknnft/bigint-buffer@1.3.0"
25
+ "bigint-buffer": "@gsknnft/bigint-buffer@1.3.2"
26
26
  }
27
27
  }
28
28
  ```
@@ -85,6 +85,12 @@ const base64 = bigintToBase64(123456789n); // → "B1vNFQ=="
85
85
 
86
86
  ---
87
87
 
88
+ The original introduced utilities for converting TC39 BigInts to and from buffers. This fork builds on that foundation with modern tooling, patched bindings, and reproducible builds.
89
+
90
+ This utility is necessary because BigInts, as proposed, do not support direct conversion between Buffers (or UInt8Arrays), but rather require conversion from buffers to hexadecimal strings then to BigInts, which is suboptimal. This utility includes N-API bindings, so under node, conversion is performed without generating a hexadecimal string. In the browser, normal string conversion is used.
91
+
92
+ ---
93
+
88
94
  # Why use BigInts?
89
95
 
90
96
  BigInts are currently a stage 3 proposal, supported in Node 10 and V8 v6.7. BigInts are primitive arbitrary precision integers, overcoming the limitations of the number type in javascript, which only supports up to 53 bits of precision.
@@ -140,7 +146,7 @@ bigint from hex string from buffer (huge): 1230607±1.02% ops/s 812.61±40.013 n
140
146
 
141
147
  bigint-buffer introduces four functions for conversion between buffers and bigints. A small example follows:
142
148
  ```typescript
143
- import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from 'bigint-buffer';
149
+ import {toBigIntBE, toBigIntLE, toBufferBE, toBufferLE} from '@gsknnft/bigint-buffer';
144
150
 
145
151
  // Get a big endian buffer of the given width
146
152
  toBufferBE(0xdeadbeefn, 8);
@@ -176,8 +182,6 @@ BE bigint to hex string to buffer (large): 1714292±1.35% ops/s 583.33±37.995 n
176
182
  BE bigint-buffer to buffer (large, truncated): 5977218±4.68% ops/s 167.30±37.284 ns/op (87 runs)
177
183
  ```
178
184
 
179
- You can run the benchmarks by running `npm run benchmark`.
180
-
181
185
  # Typescript Support
182
186
 
183
187
  bigint-buffer supplies typescript bindings, but BigInts are still not supported in typescript, though
@@ -188,13 +192,17 @@ a pull request has been opened, so support should be coming soon. If you are usi
188
192
 
189
193
  Add bigint-buffer to your project with:
190
194
 
191
- > `npm install bigint-buffer`
195
+ > `pnpm add @gsknnft/bigint-buffer`
196
+
197
+ or
198
+
199
+ > `npm install @gsknnft/bigint-buffer`
192
200
 
193
201
  # Documentation
194
202
 
195
- Basic API documentation can be found [here](https://no2chem.github.io/bigint-buffer/). Note that v1.0.0 changes
203
+ Basic API documentation can be found [here](https://gsknnft.github.io/bigint-buffer/). Note that v1.0.0 changes
196
204
  the name of the original functions to meet style guidelines.
197
205
 
198
206
  # Benchmarks
199
207
 
200
- Benchmarks can be run by executing `npm run benchmark` from the package directory.
208
+ Benchmarks can be run by executing `pnpm run benchmark` from the package directory.
package/dist/index.cjs ADDED
@@ -0,0 +1,205 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.isNative = void 0;
5
+ exports.toBigIntLE = toBigIntLE;
6
+ exports.validateBigIntBuffer = validateBigIntBuffer;
7
+ exports.toBigIntBE = toBigIntBE;
8
+ exports.toBufferLE = toBufferLE;
9
+ exports.toBufferBE = toBufferBE;
10
+ exports.bigintToBuf = bigintToBuf;
11
+ exports.bufToBigint = bufToBigint;
12
+ exports.bigintToHex = bigintToHex;
13
+ exports.hexToBigint = hexToBigint;
14
+ exports.bigintToText = bigintToText;
15
+ exports.textToBigint = textToBigint;
16
+ exports.bigintToBase64 = bigintToBase64;
17
+ exports.base64ToBigint = base64ToBigint;
18
+ let converter;
19
+ exports.isNative = false;
20
+ if (!process.browser) {
21
+ try {
22
+ converter = require('bindings')('bigint_buffer');
23
+ exports.isNative = !process.browser && converter !== undefined;
24
+ }
25
+ catch (e) {
26
+ console.warn('bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
27
+ }
28
+ }
29
+ /**
30
+ * Convert a little-endian buffer into a BigInt.
31
+ * @param buf The little-endian buffer to convert
32
+ * @returns A BigInt with the little-endian representation of buf.
33
+ */
34
+ function toBigIntLE(buf) {
35
+ if (process.browser || converter === undefined) {
36
+ const reversed = Buffer.from(buf);
37
+ reversed.reverse();
38
+ const hex = reversed.toString('hex');
39
+ if (hex.length === 0) {
40
+ return BigInt(0);
41
+ }
42
+ return BigInt(`0x${hex}`);
43
+ }
44
+ return converter.toBigInt(buf, false);
45
+ }
46
+ function validateBigIntBuffer() {
47
+ try {
48
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
49
+ return test === BigInt(1);
50
+ }
51
+ catch {
52
+ return false;
53
+ }
54
+ }
55
+ /**
56
+ * Convert a big-endian buffer into a BigInt
57
+ * @param buf The big-endian buffer to convert.
58
+ * @returns A BigInt with the big-endian representation of buf.
59
+ */
60
+ function toBigIntBE(buf) {
61
+ if (process.browser || converter === undefined) {
62
+ const hex = buf.toString('hex');
63
+ if (hex.length === 0) {
64
+ return BigInt(0);
65
+ }
66
+ return BigInt(`0x${hex}`);
67
+ }
68
+ return converter.toBigInt(buf, true);
69
+ }
70
+ /**
71
+ * Convert a BigInt to a little-endian buffer.
72
+ * @param num The BigInt to convert.
73
+ * @param width The number of bytes that the resulting buffer should be.
74
+ * @returns A little-endian buffer representation of num.
75
+ */
76
+ function toBufferLE(num, width) {
77
+ if (process.browser || converter === undefined) {
78
+ const hex = num.toString(16);
79
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
80
+ buffer.reverse();
81
+ return buffer;
82
+ }
83
+ // Allocation is done here, since it is slower using napi in C
84
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
85
+ }
86
+ /**
87
+ * Convert a BigInt to a big-endian buffer.
88
+ * @param num The BigInt to convert.
89
+ * @param width The number of bytes that the resulting buffer should be.
90
+ * @returns A big-endian buffer representation of num.
91
+ */
92
+ function toBufferBE(num, width) {
93
+ if (process.browser || converter === undefined) {
94
+ const hex = num.toString(16);
95
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
96
+ }
97
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
98
+ }
99
+ // ========== Conversion Utilities ==========
100
+ /**
101
+ * Convert a bigint to a Buffer with automatic sizing.
102
+ * Uses big-endian encoding and calculates the minimum buffer size needed.
103
+ * @param num The bigint to convert
104
+ * @returns A big-endian Buffer representation
105
+ */
106
+ function bigintToBuf(num) {
107
+ if (num < BigInt(0)) {
108
+ throw new Error('bigintToBuf: negative bigint values are not supported');
109
+ }
110
+ if (num === BigInt(0)) {
111
+ return Buffer.from([0]);
112
+ }
113
+ // Calculate the number of bytes needed
114
+ const hex = num.toString(16);
115
+ const width = Math.ceil(hex.length / 2);
116
+ return toBufferBE(num, width);
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
+ function bufToBigint(buf) {
125
+ return toBigIntBE(buf);
126
+ }
127
+ /**
128
+ * Convert a bigint to a hexadecimal string.
129
+ * @param num The bigint to convert
130
+ * @returns A hexadecimal string (without '0x' prefix)
131
+ */
132
+ function bigintToHex(num) {
133
+ if (num < BigInt(0)) {
134
+ throw new Error('bigintToHex: negative bigint values are not supported');
135
+ }
136
+ const hex = num.toString(16);
137
+ // Ensure even length for proper byte representation
138
+ return hex.length % 2 === 0 ? hex : '0' + hex;
139
+ }
140
+ /**
141
+ * Convert a hexadecimal string to a bigint.
142
+ * @param hex The hexadecimal string (with or without '0x' prefix)
143
+ * @returns A bigint representation
144
+ */
145
+ function hexToBigint(hex) {
146
+ // Remove '0x' prefix if present
147
+ const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
148
+ if (cleanHex.length === 0) {
149
+ return BigInt(0);
150
+ }
151
+ return BigInt(`0x${cleanHex}`);
152
+ }
153
+ /**
154
+ * Convert a bigint to a decimal text string.
155
+ * @param num The bigint to convert
156
+ * @returns A decimal string representation
157
+ */
158
+ function bigintToText(num) {
159
+ return num.toString(10);
160
+ }
161
+ /**
162
+ * Convert a decimal text string to a bigint.
163
+ * @param text The decimal string to convert
164
+ * @returns A bigint representation
165
+ */
166
+ function textToBigint(text) {
167
+ if (!text?.trim()) {
168
+ throw new Error('textToBigint: input string cannot be empty');
169
+ }
170
+ try {
171
+ return BigInt(text);
172
+ }
173
+ catch (e) {
174
+ throw new Error(`textToBigint: invalid decimal string "${text}"`);
175
+ }
176
+ }
177
+ /**
178
+ * Convert a bigint to a base64 string.
179
+ * @param num The bigint to convert
180
+ * @returns A base64 string representation
181
+ */
182
+ function bigintToBase64(num) {
183
+ if (num < BigInt(0)) {
184
+ throw new Error('bigintToBase64: negative bigint values are not supported');
185
+ }
186
+ const buf = bigintToBuf(num);
187
+ return buf.toString('base64');
188
+ }
189
+ /**
190
+ * Convert a base64 string to a bigint.
191
+ * @param base64 The base64 string to convert
192
+ * @returns A bigint representation
193
+ */
194
+ function base64ToBigint(base64) {
195
+ if (!base64?.trim()) {
196
+ throw new Error('base64ToBigint: input string cannot be empty');
197
+ }
198
+ // Trim whitespace and validate base64 format (allows padding)
199
+ const cleaned = base64.trim();
200
+ if (!/^[A-Za-z0-9+/]+=*$/.test(cleaned)) {
201
+ throw new Error('base64ToBigint: invalid base64 string format');
202
+ }
203
+ const buf = Buffer.from(cleaned, 'base64');
204
+ return bufToBigint(buf);
205
+ }
package/dist/index.d.ts CHANGED
@@ -26,3 +26,53 @@ export declare function toBufferLE(num: bigint, width: number): Buffer;
26
26
  * @returns A big-endian buffer representation of num.
27
27
  */
28
28
  export declare function toBufferBE(num: bigint, width: number): Buffer;
29
+ /**
30
+ * Convert a bigint to a Buffer with automatic sizing.
31
+ * Uses big-endian encoding and calculates the minimum buffer size needed.
32
+ * @param num The bigint to convert
33
+ * @returns A big-endian Buffer representation
34
+ */
35
+ export declare function bigintToBuf(num: bigint): Buffer;
36
+ /**
37
+ * Convert a Buffer to a bigint.
38
+ * Assumes big-endian encoding.
39
+ * @param buf The buffer to convert
40
+ * @returns A bigint representation of the buffer
41
+ */
42
+ export declare function bufToBigint(buf: Buffer): bigint;
43
+ /**
44
+ * Convert a bigint to a hexadecimal string.
45
+ * @param num The bigint to convert
46
+ * @returns A hexadecimal string (without '0x' prefix)
47
+ */
48
+ export declare function bigintToHex(num: bigint): string;
49
+ /**
50
+ * Convert a hexadecimal string to a bigint.
51
+ * @param hex The hexadecimal string (with or without '0x' prefix)
52
+ * @returns A bigint representation
53
+ */
54
+ export declare function hexToBigint(hex: string): bigint;
55
+ /**
56
+ * Convert a bigint to a decimal text string.
57
+ * @param num The bigint to convert
58
+ * @returns A decimal string representation
59
+ */
60
+ export declare function bigintToText(num: bigint): string;
61
+ /**
62
+ * Convert a decimal text string to a bigint.
63
+ * @param text The decimal string to convert
64
+ * @returns A bigint representation
65
+ */
66
+ export declare function textToBigint(text: string): bigint;
67
+ /**
68
+ * Convert a bigint to a base64 string.
69
+ * @param num The bigint to convert
70
+ * @returns A base64 string representation
71
+ */
72
+ export declare function bigintToBase64(num: bigint): string;
73
+ /**
74
+ * Convert a base64 string to a bigint.
75
+ * @param base64 The base64 string to convert
76
+ * @returns A bigint representation
77
+ */
78
+ export declare function base64ToBigint(base64: string): bigint;
package/dist/index.js ADDED
@@ -0,0 +1,203 @@
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.isNative = void 0;
3
+ exports.toBigIntLE = toBigIntLE;
4
+ exports.validateBigIntBuffer = validateBigIntBuffer;
5
+ exports.toBigIntBE = toBigIntBE;
6
+ exports.toBufferLE = toBufferLE;
7
+ exports.toBufferBE = toBufferBE;
8
+ exports.bigintToBuf = bigintToBuf;
9
+ exports.bufToBigint = bufToBigint;
10
+ exports.bigintToHex = bigintToHex;
11
+ exports.hexToBigint = hexToBigint;
12
+ exports.bigintToText = bigintToText;
13
+ exports.textToBigint = textToBigint;
14
+ exports.bigintToBase64 = bigintToBase64;
15
+ exports.base64ToBigint = base64ToBigint;
16
+ let converter;
17
+ exports.isNative = false;
18
+ {
19
+ try {
20
+ converter = require('bindings')('bigint_buffer');
21
+ exports.isNative = !false && converter !== undefined;
22
+ }
23
+ catch (e) {
24
+ console.warn('bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
25
+ }
26
+ }
27
+ /**
28
+ * Convert a little-endian buffer into a BigInt.
29
+ * @param buf The little-endian buffer to convert
30
+ * @returns A BigInt with the little-endian representation of buf.
31
+ */
32
+ function toBigIntLE(buf) {
33
+ if (converter === undefined) {
34
+ const reversed = Buffer.from(buf);
35
+ reversed.reverse();
36
+ const hex = reversed.toString('hex');
37
+ if (hex.length === 0) {
38
+ return BigInt(0);
39
+ }
40
+ return BigInt(`0x${hex}`);
41
+ }
42
+ return converter.toBigInt(buf, false);
43
+ }
44
+ function validateBigIntBuffer() {
45
+ try {
46
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
47
+ return test === BigInt(1);
48
+ }
49
+ catch {
50
+ return false;
51
+ }
52
+ }
53
+ /**
54
+ * Convert a big-endian buffer into a BigInt
55
+ * @param buf The big-endian buffer to convert.
56
+ * @returns A BigInt with the big-endian representation of buf.
57
+ */
58
+ function toBigIntBE(buf) {
59
+ if (converter === undefined) {
60
+ const hex = buf.toString('hex');
61
+ if (hex.length === 0) {
62
+ return BigInt(0);
63
+ }
64
+ return BigInt(`0x${hex}`);
65
+ }
66
+ return converter.toBigInt(buf, true);
67
+ }
68
+ /**
69
+ * Convert a BigInt to a little-endian buffer.
70
+ * @param num The BigInt to convert.
71
+ * @param width The number of bytes that the resulting buffer should be.
72
+ * @returns A little-endian buffer representation of num.
73
+ */
74
+ function toBufferLE(num, width) {
75
+ if (converter === undefined) {
76
+ const hex = num.toString(16);
77
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
78
+ buffer.reverse();
79
+ return buffer;
80
+ }
81
+ // Allocation is done here, since it is slower using napi in C
82
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
83
+ }
84
+ /**
85
+ * Convert a BigInt to a big-endian buffer.
86
+ * @param num The BigInt to convert.
87
+ * @param width The number of bytes that the resulting buffer should be.
88
+ * @returns A big-endian buffer representation of num.
89
+ */
90
+ function toBufferBE(num, width) {
91
+ if (converter === undefined) {
92
+ const hex = num.toString(16);
93
+ return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
94
+ }
95
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
96
+ }
97
+ // ========== Conversion Utilities ==========
98
+ /**
99
+ * Convert a bigint to a Buffer with automatic sizing.
100
+ * Uses big-endian encoding and calculates the minimum buffer size needed.
101
+ * @param num The bigint to convert
102
+ * @returns A big-endian Buffer representation
103
+ */
104
+ function bigintToBuf(num) {
105
+ if (num < BigInt(0)) {
106
+ throw new Error('bigintToBuf: negative bigint values are not supported');
107
+ }
108
+ if (num === BigInt(0)) {
109
+ return Buffer.from([0]);
110
+ }
111
+ // Calculate the number of bytes needed
112
+ const hex = num.toString(16);
113
+ const width = Math.ceil(hex.length / 2);
114
+ return toBufferBE(num, width);
115
+ }
116
+ /**
117
+ * Convert a Buffer to a bigint.
118
+ * Assumes big-endian encoding.
119
+ * @param buf The buffer to convert
120
+ * @returns A bigint representation of the buffer
121
+ */
122
+ function bufToBigint(buf) {
123
+ return toBigIntBE(buf);
124
+ }
125
+ /**
126
+ * Convert a bigint to a hexadecimal string.
127
+ * @param num The bigint to convert
128
+ * @returns A hexadecimal string (without '0x' prefix)
129
+ */
130
+ function bigintToHex(num) {
131
+ if (num < BigInt(0)) {
132
+ throw new Error('bigintToHex: negative bigint values are not supported');
133
+ }
134
+ const hex = num.toString(16);
135
+ // Ensure even length for proper byte representation
136
+ return hex.length % 2 === 0 ? hex : '0' + hex;
137
+ }
138
+ /**
139
+ * Convert a hexadecimal string to a bigint.
140
+ * @param hex The hexadecimal string (with or without '0x' prefix)
141
+ * @returns A bigint representation
142
+ */
143
+ function hexToBigint(hex) {
144
+ // Remove '0x' prefix if present
145
+ const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
146
+ if (cleanHex.length === 0) {
147
+ return BigInt(0);
148
+ }
149
+ return BigInt(`0x${cleanHex}`);
150
+ }
151
+ /**
152
+ * Convert a bigint to a decimal text string.
153
+ * @param num The bigint to convert
154
+ * @returns A decimal string representation
155
+ */
156
+ function bigintToText(num) {
157
+ return num.toString(10);
158
+ }
159
+ /**
160
+ * Convert a decimal text string to a bigint.
161
+ * @param text The decimal string to convert
162
+ * @returns A bigint representation
163
+ */
164
+ function textToBigint(text) {
165
+ if (!text?.trim()) {
166
+ throw new Error('textToBigint: input string cannot be empty');
167
+ }
168
+ try {
169
+ return BigInt(text);
170
+ }
171
+ catch (e) {
172
+ throw new Error(`textToBigint: invalid decimal string "${text}"`);
173
+ }
174
+ }
175
+ /**
176
+ * Convert a bigint to a base64 string.
177
+ * @param num The bigint to convert
178
+ * @returns A base64 string representation
179
+ */
180
+ function bigintToBase64(num) {
181
+ if (num < BigInt(0)) {
182
+ throw new Error('bigintToBase64: negative bigint values are not supported');
183
+ }
184
+ const buf = bigintToBuf(num);
185
+ return buf.toString('base64');
186
+ }
187
+ /**
188
+ * Convert a base64 string to a bigint.
189
+ * @param base64 The base64 string to convert
190
+ * @returns A bigint representation
191
+ */
192
+ function base64ToBigint(base64) {
193
+ if (!base64?.trim()) {
194
+ throw new Error('base64ToBigint: input string cannot be empty');
195
+ }
196
+ // Trim whitespace and validate base64 format (allows padding)
197
+ const cleaned = base64.trim();
198
+ if (!/^[A-Za-z0-9+/]+=*$/.test(cleaned)) {
199
+ throw new Error('base64ToBigint: invalid base64 string format');
200
+ }
201
+ const buf = Buffer.from(cleaned, 'base64');
202
+ return bufToBigint(buf);
203
+ }
package/dist/node.js CHANGED
@@ -11,14 +11,24 @@ exports.toBufferBE = toBufferBE;
11
11
  // etc.
12
12
  const bindings_1 = __importDefault(require("bindings"));
13
13
  (0, bindings_1.default)('bigint_buffer');
14
+ let converter;
14
15
  exports.isNative = false;
16
+ {
17
+ try {
18
+ converter = require('bindings')('bigint_buffer');
19
+ exports.isNative = !false && converter !== undefined;
20
+ }
21
+ catch (e) {
22
+ console.warn('bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
23
+ }
24
+ }
15
25
  /**
16
26
  * Convert a little-endian buffer into a BigInt.
17
27
  * @param buf The little-endian buffer to convert
18
28
  * @returns A BigInt with the little-endian representation of buf.
19
29
  */
20
30
  function toBigIntLE(buf) {
21
- {
31
+ if (converter === undefined) {
22
32
  const reversed = Buffer.from(buf);
23
33
  reversed.reverse();
24
34
  const hex = reversed.toString('hex');
@@ -27,6 +37,7 @@ function toBigIntLE(buf) {
27
37
  }
28
38
  return BigInt(`0x${hex}`);
29
39
  }
40
+ return converter.toBigInt(buf, false);
30
41
  }
31
42
  function validateBigIntBuffer() {
32
43
  try {
@@ -43,13 +54,14 @@ function validateBigIntBuffer() {
43
54
  * @returns A BigInt with the big-endian representation of buf.
44
55
  */
45
56
  function toBigIntBE(buf) {
46
- {
57
+ if (converter === undefined) {
47
58
  const hex = buf.toString('hex');
48
59
  if (hex.length === 0) {
49
60
  return BigInt(0);
50
61
  }
51
62
  return BigInt(`0x${hex}`);
52
63
  }
64
+ return converter.toBigInt(buf, true);
53
65
  }
54
66
  /**
55
67
  * Convert a BigInt to a little-endian buffer.
@@ -58,12 +70,14 @@ function toBigIntBE(buf) {
58
70
  * @returns A little-endian buffer representation of num.
59
71
  */
60
72
  function toBufferLE(num, width) {
61
- {
73
+ if (converter === undefined) {
62
74
  const hex = num.toString(16);
63
75
  const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
64
76
  buffer.reverse();
65
77
  return buffer;
66
78
  }
79
+ // Allocation is done here, since it is slower using napi in C
80
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
67
81
  }
68
82
  /**
69
83
  * Convert a BigInt to a big-endian buffer.
@@ -72,8 +86,9 @@ function toBufferLE(num, width) {
72
86
  * @returns A big-endian buffer representation of num.
73
87
  */
74
88
  function toBufferBE(num, width) {
75
- {
89
+ if (converter === undefined) {
76
90
  const hex = num.toString(16);
77
91
  return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
78
92
  }
93
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
79
94
  }
@@ -1,3 +1,3 @@
1
1
  // not used by code, only for documentation generation (since typedoc can't handle BigInt yet)
2
- type BigIntDoc = number
3
- declare const BigIntDoc: typeof Number
2
+ type BigInt = number
3
+ declare const BigInt: typeof Number