@gsknnft/bigint-buffer 1.3.2 → 1.4.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/src/index.ts DELETED
@@ -1,210 +0,0 @@
1
-
2
- interface ConverterInterface {
3
- toBigInt(buf: Buffer, bigEndian?: boolean): bigint;
4
- fromBigInt(num: BigInt, buf: Buffer, bigEndian?: boolean): Buffer;
5
- }
6
-
7
- declare var process: {browser: boolean;};
8
-
9
- let converter: ConverterInterface;
10
- export let isNative = false;
11
-
12
- if (!process.browser) {
13
- try {
14
- converter = require('bindings')('bigint_buffer');
15
- isNative = !process.browser && converter !== undefined;
16
- } catch (e) {
17
- console.warn(
18
- 'bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)');
19
- }
20
- }
21
-
22
- /**
23
- * Convert a little-endian buffer into a BigInt.
24
- * @param buf The little-endian buffer to convert
25
- * @returns A BigInt with the little-endian representation of buf.
26
- */
27
- export function toBigIntLE(buf: Buffer): bigint {
28
- if (process.browser || converter === undefined) {
29
- const reversed = Buffer.from(buf);
30
- reversed.reverse();
31
- const hex = reversed.toString('hex');
32
- if (hex.length === 0) {
33
- return BigInt(0);
34
- }
35
- return BigInt(`0x${hex}`);
36
- }
37
- return converter.toBigInt(buf, false);
38
- }
39
-
40
- export function validateBigIntBuffer(): boolean {
41
- try {
42
- const test = toBigIntLE(Buffer.from([0x01, 0x00]));
43
- return test === BigInt(1);
44
- } catch {
45
- return false;
46
- }
47
- }
48
-
49
- /**
50
- * Convert a big-endian buffer into a BigInt
51
- * @param buf The big-endian buffer to convert.
52
- * @returns A BigInt with the big-endian representation of buf.
53
- */
54
- export function toBigIntBE(buf: Buffer): bigint {
55
- if (process.browser || converter === undefined) {
56
- const hex = buf.toString('hex');
57
- if (hex.length === 0) {
58
- return BigInt(0);
59
- }
60
- return BigInt(`0x${hex}`);
61
- }
62
- return converter.toBigInt(buf, true);
63
- }
64
-
65
- /**
66
- * Convert a BigInt to a little-endian buffer.
67
- * @param num The BigInt to convert.
68
- * @param width The number of bytes that the resulting buffer should be.
69
- * @returns A little-endian buffer representation of num.
70
- */
71
- export function toBufferLE(num: bigint, width: number): Buffer {
72
- if (process.browser || converter === undefined) {
73
- const hex = num.toString(16);
74
- const buffer =
75
- Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
76
- buffer.reverse();
77
- return buffer;
78
- }
79
- // Allocation is done here, since it is slower using napi in C
80
- return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
81
- }
82
-
83
- /**
84
- * Convert a BigInt to a big-endian buffer.
85
- * @param num The BigInt to convert.
86
- * @param width The number of bytes that the resulting buffer should be.
87
- * @returns A big-endian buffer representation of num.
88
- */
89
- export function toBufferBE(num: bigint, width: number): Buffer {
90
- 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');
93
- }
94
- return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
95
- }
96
-
97
- // ========== Conversion Utilities ==========
98
-
99
- /**
100
- * Convert a bigint to a Buffer with automatic sizing.
101
- * Uses big-endian encoding and calculates the minimum buffer size needed.
102
- * @param num The bigint to convert
103
- * @returns A big-endian Buffer representation
104
- */
105
- export function bigintToBuf(num: bigint): Buffer {
106
- if (num < BigInt(0)) {
107
- throw new Error('bigintToBuf: negative bigint values are not supported');
108
- }
109
- if (num === BigInt(0)) {
110
- return Buffer.from([0]);
111
- }
112
- // Calculate the number of bytes needed
113
- const hex = num.toString(16);
114
- const width = Math.ceil(hex.length / 2);
115
- return toBufferBE(num, width);
116
- }
117
-
118
- /**
119
- * Convert a Buffer to a bigint.
120
- * Assumes big-endian encoding.
121
- * @param buf The buffer to convert
122
- * @returns A bigint representation of the buffer
123
- */
124
- export function bufToBigint(buf: Buffer): bigint {
125
- return toBigIntBE(buf);
126
- }
127
-
128
- /**
129
- * Convert a bigint to a hexadecimal string.
130
- * @param num The bigint to convert
131
- * @returns A hexadecimal string (without '0x' prefix)
132
- */
133
- export function bigintToHex(num: bigint): string {
134
- if (num < BigInt(0)) {
135
- throw new Error('bigintToHex: negative bigint values are not supported');
136
- }
137
- const hex = num.toString(16);
138
- // Ensure even length for proper byte representation
139
- return hex.length % 2 === 0 ? hex : '0' + hex;
140
- }
141
-
142
- /**
143
- * Convert a hexadecimal string to a bigint.
144
- * @param hex The hexadecimal string (with or without '0x' prefix)
145
- * @returns A bigint representation
146
- */
147
- export function hexToBigint(hex: string): bigint {
148
- // Remove '0x' prefix if present
149
- const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
150
- if (cleanHex.length === 0) {
151
- return BigInt(0);
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);
210
- }
package/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "extends": "./node_modules/gts/tsconfig-google.json",
3
- "compilerOptions": {
4
- "composite": true,
5
- "rootDir": ".",
6
- "outDir": "build",
7
- "target" : "esnext",
8
- "lib" : [ "esnext" ],
9
- "sourceMap": true
10
- },
11
- "include": [
12
- "src/*.ts",
13
- "src/**/*.ts",
14
- "test/*.ts",
15
- "test/**/*.ts"
16
- ],
17
- "exclude": [
18
- "node_modules"
19
- ]
20
- }