@gsknnft/bigint-buffer 1.3.1 → 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 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.2.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.2.0"
25
+ "bigint-buffer": "@gsknnft/bigint-buffer@1.3.2"
26
26
  }
27
27
  }
28
28
  ```
package/dist/index.cjs CHANGED
@@ -7,6 +7,14 @@ exports.validateBigIntBuffer = validateBigIntBuffer;
7
7
  exports.toBigIntBE = toBigIntBE;
8
8
  exports.toBufferLE = toBufferLE;
9
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;
10
18
  let converter;
11
19
  exports.isNative = false;
12
20
  if (!process.browser) {
@@ -88,3 +96,110 @@ function toBufferBE(num, width) {
88
96
  }
89
97
  return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
90
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
@@ -1,5 +1,6 @@
1
- 'use strict';
2
-
1
+ var __importDefault = (undefined && undefined.__importDefault) || function (mod) {
2
+ return (mod && mod.__esModule) ? mod : { "default": mod };
3
+ };
3
4
  Object.defineProperty(exports, "__esModule", { value: true });
4
5
  exports.isNative = void 0;
5
6
  exports.toBigIntLE = toBigIntLE;
@@ -7,6 +8,9 @@ exports.validateBigIntBuffer = validateBigIntBuffer;
7
8
  exports.toBigIntBE = toBigIntBE;
8
9
  exports.toBufferLE = toBufferLE;
9
10
  exports.toBufferBE = toBufferBE;
11
+ // etc.
12
+ const bindings_1 = __importDefault(require("bindings"));
13
+ (0, bindings_1.default)('bigint_buffer');
10
14
  let converter;
11
15
  exports.isNative = false;
12
16
  {
package/package.json CHANGED
@@ -1,100 +1,96 @@
1
- {
2
- "name": "@gsknnft/bigint-buffer",
3
- "version": "1.3.1",
4
- "description": "bigint to buffer conversion with native support",
5
- "main": "dist/index.js",
6
- "type": "module",
7
- "browser": {
8
- "./dist/node.js": "./dist/browser.js"
9
- },
10
- "types": "dist/index.d.ts",
11
- "scripts": {
12
- "test": "npm run test:node && npm run test:browser",
13
- "coverage": "istanbul cover ./test/index.js",
14
- "coveralls": "npm run coverage && coveralls <coverage/lcov.info",
15
- "lint": "gts check",
16
- "install": "npm run rebuild || echo \"Couldn't build bindings. Non-native version used.\"",
17
- "prepare": "npm run compile",
18
- "prepublishOnly": "rm -rf build/Release; rm build/Makefile; rm build/gyp-*; rm build/*.mk; rm build/*.Makefile; rm *.tgz",
19
- "test:browser": "karma start karma.conf.js",
20
- "test:node": "mocha -r ts-node/register src/**/*.spec.ts --timeout 40000",
21
- "benchmark": "node -r ts-node/register src/index.bench.ts",
22
- "typedoc": "typedoc --out docs $(pwd)/src $(pwd)/helper --target es6 --mode file --tsconfig ./tsconfig.json --excludePrivate --excludeProtected --excludeNotExported --exclude '**/*+(spec|bench).ts'",
23
- "rebuild": "node-gyp rebuild",
24
- "check": "gts check",
25
- "clean": "gts clean",
26
- "compile": "tsc -p . && rollup -c rollup.cjs.config.js && rollup -c rollup.esm.config.js && cpy build/src/*.d.ts dist",
27
- "compileOG": "mkdirp dist && tsc -p . && rollup -c > dist/node.js && cross-env BROWSER=true rollup -c > dist/browser.js && cpx \"build/src/*.d.ts\" dist",
28
- "fix": "gts fix",
29
- "pretest": "npm run compile",
30
- "posttest": "npm run check"
31
- },
32
- "repository": {
33
- "type": "git",
34
- "url": "https://github.com/gsknnft/bigint-buffer.git"
35
- },
36
- "bugs": {
37
- "url": "https://github.com/gsknnft/bigint-buffer/issues"
38
- },
39
- "homepage": "https://github.com/gsknnft/bigintbuffer",
40
- "keywords": [
41
- "bigint",
42
- "bignum",
43
- "tc39-bigint",
44
- "napi"
45
- ],
46
- "license": "Apache-2.0",
47
- "dependencies": {
48
- "bindings": "^1.5.0"
49
- },
50
- "devDependencies": {
51
- "@types/benchmark": "^2.1.5",
52
- "@types/bn.js": "^5.2.0",
53
- "@types/chai": "^5.2.3",
54
- "@types/mocha": "^10.0.10",
55
- "@types/node": "^24.9.1",
56
- "benchmark": "^2.1.4",
57
- "bn.js": "^5.2.2",
58
- "chai": "^4.1.2",
59
- "coveralls": "^3.1.1",
60
- "cpy-cli": "^6.0.0",
61
- "cross-env": "^6.0.3",
62
- "gts": "^0.8.0",
63
- "istanbul": "^0.4.1",
64
- "karma": "^3.0.0",
65
- "karma-chrome-launcher": "^2.2.0",
66
- "karma-env-preprocessor": "^0.1.1",
67
- "karma-mocha": "^1.3.0",
68
- "karma-mocha-reporter": "^2.2.5",
69
- "karma-webpack": "^3.0.0",
70
- "microtime": "^3.0.0",
71
- "mkdirp": "^0.5.1",
72
- "mocha": "^5.2.0",
73
- "pre-commit": "^1.2.2",
74
- "rollup": "^4.52.5",
75
- "rollup-plugin-replace": "^2.2.0",
76
- "ts-loader": "^9.5.4",
77
- "ts-node": "^10.9.2",
78
- "typedoc": "^0.28.14",
79
- "typescript": "5.9.3",
80
- "webpack": "5.102.1",
81
- "webpack-cli": "6.0.1"
82
- },
83
- "exports": {
84
- ".": {
85
- "import": "./dist/index.js",
86
- "require": "./dist/index.cjs",
87
- "types": "./dist/index.d.ts"
88
- }
89
- },
90
- "engines": {
91
- "node": ">=24.0.0"
92
- },
93
- "publishConfig": {
94
- "access": "public"
95
- },
96
- "contributors": [
97
- "Gordon Skinner <gsknnft@gmail.com> (https://github.com/gsknnft)",
98
- "Michael Wei <mwei@vmware.com> (https://github.com/no2chem)"
99
- ]
100
- }
1
+ {
2
+ "name": "@gsknnft/bigint-buffer",
3
+ "version": "1.3.2",
4
+ "description": "bigint to buffer conversion with native support",
5
+ "main": "dist/index.js",
6
+ "browser": {
7
+ "./dist/node.js": "./dist/browser.js"
8
+ },
9
+ "types": "dist/index.d.ts",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/gsknnft/bigint-buffer.git"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/gsknnft/bigint-buffer/issues"
16
+ },
17
+ "homepage": "https://github.com/gsknnft/bigintbuffer",
18
+ "keywords": [
19
+ "bigint",
20
+ "bignum",
21
+ "tc39-bigint",
22
+ "napi"
23
+ ],
24
+ "license": "Apache-2.0",
25
+ "dependencies": {
26
+ "bindings": "^1.5.0"
27
+ },
28
+ "devDependencies": {
29
+ "@types/benchmark": "^2.1.5",
30
+ "@types/bn.js": "^5.2.0",
31
+ "@types/chai": "^5.2.3",
32
+ "@types/mocha": "^10.0.10",
33
+ "@types/node": "^24.9.1",
34
+ "benchmark": "^2.1.4",
35
+ "bn.js": "^5.2.2",
36
+ "chai": "^4.1.2",
37
+ "coveralls": "^3.1.1",
38
+ "cpy-cli": "^6.0.0",
39
+ "cross-env": "^6.0.3",
40
+ "gts": "^0.8.0",
41
+ "istanbul": "^0.4.1",
42
+ "karma": "^3.0.0",
43
+ "karma-chrome-launcher": "^2.2.0",
44
+ "karma-env-preprocessor": "^0.1.1",
45
+ "karma-mocha": "^1.3.0",
46
+ "karma-mocha-reporter": "^2.2.5",
47
+ "karma-webpack": "^3.0.0",
48
+ "microtime": "^3.0.0",
49
+ "mkdirp": "^0.5.1",
50
+ "mocha": "^5.2.0",
51
+ "pre-commit": "^1.2.2",
52
+ "rollup": "^4.52.5",
53
+ "@rollup/plugin-replace": "6.0.3",
54
+ "ts-loader": "^9.5.4",
55
+ "ts-node": "^10.9.2",
56
+ "typedoc": "^0.28.14",
57
+ "typescript": "5.9.3",
58
+ "webpack": "5.102.1",
59
+ "webpack-cli": "6.0.1"
60
+ },
61
+ "exports": {
62
+ ".": {
63
+ "import": "./dist/index.js",
64
+ "require": "./dist/index.cjs",
65
+ "types": "./dist/index.d.ts"
66
+ }
67
+ },
68
+ "engines": {
69
+ "node": ">=24.0.0"
70
+ },
71
+ "publishConfig": {
72
+ "access": "public"
73
+ },
74
+ "contributors": [
75
+ "Gordon Skinner <gsknnft@gmail.com> (https://github.com/gsknnft)",
76
+ "Michael Wei <mwei@vmware.com> (https://github.com/no2chem)"
77
+ ],
78
+ "scripts": {
79
+ "test": "npm run test:node && npm run test:browser",
80
+ "coverage": "istanbul cover ./test/index.js",
81
+ "coveralls": "npm run coverage && coveralls <coverage/lcov.info",
82
+ "lint": "gts check",
83
+ "install": "npm run rebuild || echo \"Couldn't build bindings. Non-native version used.\"",
84
+ "test:browser": "karma start karma.conf.js",
85
+ "test:node": "mocha build/src/**/*.spec.js --timeout 40000",
86
+ "benchmark": "node -r ts-node/register src/index.bench.ts",
87
+ "typedoc": "typedoc --out docs $(pwd)/src $(pwd)/helper --target es6 --mode file --tsconfig ./tsconfig.json --excludePrivate --excludeProtected --excludeNotExported --exclude '**/*+(spec|bench).ts'",
88
+ "rebuild": "node-gyp rebuild",
89
+ "check": "gts check",
90
+ "clean": "gts clean",
91
+ "compile": "tsc -p . && rollup -c rollup.cjs.config.js && rollup -c rollup.esm.config.js && cp build/src/index.d.ts dist/index.d.ts",
92
+ "fix": "gts fix",
93
+ "pretest": "npm run compile",
94
+ "posttest": "npm run check"
95
+ }
96
+ }
@@ -1,13 +1,15 @@
1
- import replace from 'rollup-plugin-replace';
1
+ import replace from '@rollup/plugin-replace';
2
2
 
3
3
  export default {
4
4
  input: 'build/src/index.js',
5
5
  output: {
6
- format: 'cjs'
6
+ file: 'dist/index.js',
7
+ format: 'esm'
7
8
  },
9
+ external: ['bindings'],
8
10
  plugins: [
9
11
  replace({
10
- 'process.browser': process.env.BROWSER === "true"
12
+ 'process.browser': 'false'
11
13
  })
12
14
  ]
13
15
  };
package/src/index.spec.ts CHANGED
@@ -4,8 +4,9 @@ declare var process: {browser: boolean;};
4
4
  import * as chai from 'chai';
5
5
  import * as path from 'path';
6
6
 
7
- const lib = process.browser ? require('../../dist/browser') :
8
- require(path.join(__dirname, '../dist/node'));
7
+ const lib = process.browser ?
8
+ require('../../dist/index.js') :
9
+ require(path.join(__dirname, '../../dist/index.cjs'));
9
10
  const toBigIntBE = lib.toBigIntBE;
10
11
  const toBigIntLE = lib.toBigIntLE;
11
12
  const toBufferBE = lib.toBufferBE;
@@ -284,4 +285,160 @@ describe('Try bigint conversion (big endian)', () => {
284
285
  0xe0, 0xdd, 0xf0, 0x0d, 0xde, 0xad, 0xbe, 0xef
285
286
  ]));
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]));
297
+ });
298
+
299
+ it('should convert small number to buffer', () => {
300
+ const buf = bigintToBuf(BigInt(123456789));
301
+ assertEquals(bufToBigint(buf), BigInt(123456789));
302
+ });
303
+
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);
309
+ });
310
+
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');
443
+ });
287
444
  });
package/src/index.ts CHANGED
@@ -92,4 +92,119 @@ export function toBufferBE(num: bigint, width: number): Buffer {
92
92
  return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
93
93
  }
94
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);
152
+ }
153
+ return BigInt(`0x${cleanHex}`);
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);
95
210
  }
package/dist/browser.js DELETED
@@ -1,75 +0,0 @@
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.isNative = false;
11
- /**
12
- * Convert a little-endian buffer into a BigInt.
13
- * @param buf The little-endian buffer to convert
14
- * @returns A BigInt with the little-endian representation of buf.
15
- */
16
- function toBigIntLE(buf) {
17
- {
18
- const reversed = Buffer.from(buf);
19
- reversed.reverse();
20
- const hex = reversed.toString('hex');
21
- if (hex.length === 0) {
22
- return BigInt(0);
23
- }
24
- return BigInt(`0x${hex}`);
25
- }
26
- }
27
- function validateBigIntBuffer() {
28
- try {
29
- const test = toBigIntLE(Buffer.from([0x01, 0x00]));
30
- return test === BigInt(1);
31
- }
32
- catch {
33
- return false;
34
- }
35
- }
36
- /**
37
- * Convert a big-endian buffer into a BigInt
38
- * @param buf The big-endian buffer to convert.
39
- * @returns A BigInt with the big-endian representation of buf.
40
- */
41
- function toBigIntBE(buf) {
42
- {
43
- const hex = buf.toString('hex');
44
- if (hex.length === 0) {
45
- return BigInt(0);
46
- }
47
- return BigInt(`0x${hex}`);
48
- }
49
- }
50
- /**
51
- * Convert a BigInt to a little-endian buffer.
52
- * @param num The BigInt to convert.
53
- * @param width The number of bytes that the resulting buffer should be.
54
- * @returns A little-endian buffer representation of num.
55
- */
56
- function toBufferLE(num, width) {
57
- {
58
- const hex = num.toString(16);
59
- const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
60
- buffer.reverse();
61
- return buffer;
62
- }
63
- }
64
- /**
65
- * Convert a BigInt to a big-endian buffer.
66
- * @param num The BigInt to convert.
67
- * @param width The number of bytes that the resulting buffer should be.
68
- * @returns A big-endian buffer representation of num.
69
- */
70
- function toBufferBE(num, width) {
71
- {
72
- const hex = num.toString(16);
73
- return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
74
- }
75
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- import 'mocha';