@gsknnft/bigint-buffer 1.4.7 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +150 -144
  2. package/dist/build/Release/bigint_buffer.node +0 -0
  3. package/dist/conversion/bundle.iife.js +421 -0
  4. package/dist/conversion/bundle.umd.js +423 -0
  5. package/dist/conversion/cjs/converter.d.ts +14 -0
  6. package/dist/conversion/cjs/index.d.ts +33 -0
  7. package/dist/conversion/cjs/index.js +418 -0
  8. package/dist/conversion/cjs/index.node.js +4 -0
  9. package/dist/conversion/cjs/package.json +1 -0
  10. package/dist/conversion/converter.d.ts +14 -0
  11. package/dist/conversion/esm/bundle.js +416 -0
  12. package/dist/conversion/esm/bundle.min.js +1 -0
  13. package/dist/conversion/esm/converter.d.ts +14 -0
  14. package/dist/conversion/esm/index.browser.js +416 -0
  15. package/dist/conversion/esm/index.d.ts +33 -0
  16. package/dist/conversion/esm/index.js +416 -0
  17. package/dist/conversion/esm/index.node.js +2 -0
  18. package/dist/conversion/esm/package.json +1 -0
  19. package/dist/conversion/index.d.ts +1 -2
  20. package/dist/index.cjs +1 -1
  21. package/dist/index.d.ts +4 -3
  22. package/dist/index.js +190 -250
  23. package/dist/index.umd.js +1 -1
  24. package/dist/types/conversion/index.d.ts +1 -2
  25. package/dist/types/conversion/src/index.d.ts +2 -0
  26. package/dist/types/conversion/src/ts/index.d.ts +4 -2
  27. package/dist/types/index.d.ts +4 -3
  28. package/package.json +141 -136
  29. package/scripts/postinstall.cjs +5 -3
  30. package/dist/tsconfig.tsbuildinfo +0 -1
  31. package/dist/types/bigint-buffer.test.d.ts +0 -1
  32. package/dist/types/conversion/test/bigintToBase64.test.d.ts +0 -1
  33. package/dist/types/conversion/test/bigintToBuf.test.d.ts +0 -1
  34. package/dist/types/conversion/test/bigintToHex.test.d.ts +0 -1
  35. package/dist/types/conversion/test/bigintToText.test.d.ts +0 -1
  36. package/dist/types/conversion/test/bufToBigint.test.d.ts +0 -1
  37. package/dist/types/conversion/test/fixedPoint.test.d.ts +0 -1
  38. package/dist/types/conversion/test/hexToBigint.test.d.ts +0 -1
  39. package/dist/types/conversion/test/hexToBuf.test.d.ts +0 -1
  40. package/dist/types/conversion/test/parseHex.test.d.ts +0 -1
  41. package/dist/types/conversion/test/setup.test.d.ts +0 -1
  42. package/dist/types/conversion/test/textToBuf.test.d.ts +0 -1
  43. package/dist/types/conversion/vite.config.d.ts +0 -2
  44. package/dist/types/conversion/vitest.config.d.ts +0 -2
  45. package/dist/types/index.bench.d.ts +0 -1
  46. package/dist/types/index.spec.d.ts +0 -1
@@ -0,0 +1,416 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBigIntValue = exports.toHexString = exports.ZERO_FIXED_POINT = exports.FIXED_POINT_PATTERN = exports.FIXED_POINT_DECIMALS = void 0;
4
+ exports.toBigIntLE = toBigIntLE;
5
+ exports.validateBigIntBuffer = validateBigIntBuffer;
6
+ exports.toBigIntBE = toBigIntBE;
7
+ exports.toBufferLE = toBufferLE;
8
+ exports.toBufferBE = toBufferBE;
9
+ exports.parseHex = parseHex;
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.bufToText = bufToText;
17
+ exports.textToBuf = textToBuf;
18
+ exports.bufToHex = bufToHex;
19
+ exports.hexToBuf = hexToBuf;
20
+ exports.bigintToBase64 = bigintToBase64;
21
+ exports.base64ToBigint = base64ToBigint;
22
+ exports.toFixedPoint = toFixedPoint;
23
+ exports.fromFixedPoint = fromFixedPoint;
24
+ exports.addFixedPoint = addFixedPoint;
25
+ exports.subtractFixedPoint = subtractFixedPoint;
26
+ exports.averageFixedPoint = averageFixedPoint;
27
+ exports.compareFixedPoint = compareFixedPoint;
28
+ exports.fixedPointToBigInt = fixedPointToBigInt;
29
+ const converter_1 = require("./converter");
30
+ let converter;
31
+ const hasRead64LE = typeof Buffer.prototype.readBigUInt64LE === "function";
32
+ const hasRead64BE = typeof Buffer.prototype.readBigUInt64BE === "function";
33
+ const hasWrite64LE = typeof Buffer.prototype.writeBigUInt64LE === "function";
34
+ const hasWrite64BE = typeof Buffer.prototype.writeBigUInt64BE === "function";
35
+ const read64LE = hasRead64LE
36
+ ? (buf, offset) => buf.readBigUInt64LE(offset)
37
+ : (buf, offset) => {
38
+ let out = 0n;
39
+ for (let i = 7; i >= 0; i--) {
40
+ out = (out << 8n) + BigInt(buf[offset + i]);
41
+ }
42
+ return out;
43
+ };
44
+ const read64BE = hasRead64BE
45
+ ? (buf, offset) => buf.readBigUInt64BE(offset)
46
+ : (buf, offset) => {
47
+ let out = 0n;
48
+ for (let i = 0; i < 8; i++) {
49
+ out = (out << 8n) + BigInt(buf[offset + i]);
50
+ }
51
+ return out;
52
+ };
53
+ const write64LE = hasWrite64LE
54
+ ? (buf, offset, value) => {
55
+ buf.writeBigUInt64LE(value, offset);
56
+ }
57
+ : (buf, offset, value) => {
58
+ let temp = value;
59
+ for (let i = 0; i < 8; i++) {
60
+ buf[offset + i] = Number(temp & 0xffn);
61
+ temp >>= 8n;
62
+ }
63
+ };
64
+ const write64BE = hasWrite64BE
65
+ ? (buf, offset, value) => {
66
+ buf.writeBigUInt64BE(value, offset);
67
+ }
68
+ : (buf, offset, value) => {
69
+ let temp = value;
70
+ for (let i = 7; i >= 0; i--) {
71
+ buf[offset + i] = Number(temp & 0xffn);
72
+ temp >>= 8n;
73
+ }
74
+ };
75
+ const bufferToBigIntLE = (buf) => {
76
+ let result = 0n;
77
+ let multiplier = 1n;
78
+ const len = buf.length;
79
+ const remainder = len & 7;
80
+ for (let i = 0; i < remainder; i++) {
81
+ result += BigInt(buf[i]) * multiplier;
82
+ multiplier <<= 8n;
83
+ }
84
+ for (let offset = remainder; offset < len; offset += 8) {
85
+ const chunk = read64LE(buf, offset);
86
+ result += chunk * multiplier;
87
+ multiplier <<= 64n;
88
+ }
89
+ return result;
90
+ };
91
+ const bufferToBigIntBE = (buf) => {
92
+ const len = buf.length;
93
+ if (len === 0)
94
+ return 0n;
95
+ let result = 0n;
96
+ const remainder = len & 7;
97
+ let offset = 0;
98
+ if (remainder !== 0) {
99
+ for (; offset < remainder; offset++) {
100
+ result = (result << 8n) + BigInt(buf[offset]);
101
+ }
102
+ }
103
+ for (; offset < len; offset += 8) {
104
+ const chunk = read64BE(buf, offset);
105
+ result = (result << 64n) + chunk;
106
+ }
107
+ return result;
108
+ };
109
+ const writeBigIntToBufferLE = (num, target) => {
110
+ const width = target.length;
111
+ let remaining = num;
112
+ let offset = 0;
113
+ const limit = width - (width % 8);
114
+ for (; offset < limit; offset += 8) {
115
+ write64LE(target, offset, remaining & 0xffffffffffffffffn);
116
+ remaining >>= 64n;
117
+ }
118
+ for (; offset < width; offset++) {
119
+ target[offset] = Number(remaining & 0xffn);
120
+ remaining >>= 8n;
121
+ }
122
+ return target;
123
+ };
124
+ const writeBigIntToBufferBE = (num, target) => {
125
+ const width = target.length;
126
+ let remaining = num;
127
+ let offset = width;
128
+ const limit = width & ~7;
129
+ while (offset > limit) {
130
+ offset--;
131
+ target[offset] = Number(remaining & 0xffn);
132
+ remaining >>= 8n;
133
+ }
134
+ for (; offset > 0; offset -= 8) {
135
+ const chunk = remaining & 0xffffffffffffffffn;
136
+ write64BE(target, offset - 8, chunk);
137
+ remaining >>= 64n;
138
+ }
139
+ return target;
140
+ };
141
+ const toBufferInput = (input) => {
142
+ if (Buffer.isBuffer(input))
143
+ return input;
144
+ if (input instanceof Uint8Array) {
145
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
146
+ }
147
+ return Buffer.from(input);
148
+ };
149
+ const assertWidth = (width, fnName) => {
150
+ if (!Number.isInteger(width) || width < 0) {
151
+ throw new RangeError(`${fnName} width must be a non-negative integer`);
152
+ }
153
+ };
154
+ if (!converter_1.IS_BROWSER) {
155
+ converter = (0, converter_1.loadNative)();
156
+ }
157
+ if (converter === undefined) {
158
+ converter = {
159
+ toBigInt: (buf, bigEndian = true) => bigEndian ? bufferToBigIntBE(buf) : bufferToBigIntLE(buf),
160
+ fromBigInt: (num, buf, bigEndian = true) => bigEndian
161
+ ? writeBigIntToBufferBE(num, buf)
162
+ : writeBigIntToBufferLE(num, buf),
163
+ };
164
+ }
165
+ function toBigIntLE(buf) {
166
+ if (converter === undefined)
167
+ return 0n;
168
+ return converter.toBigInt(toBufferInput(buf), false);
169
+ }
170
+ function validateBigIntBuffer() {
171
+ try {
172
+ const test = toBigIntLE(Buffer.from([0x01, 0x00]));
173
+ return test === BigInt(1);
174
+ }
175
+ catch {
176
+ return false;
177
+ }
178
+ }
179
+ function toBigIntBE(buf) {
180
+ if (converter === undefined)
181
+ return 0n;
182
+ return converter.toBigInt(toBufferInput(buf), true);
183
+ }
184
+ function toBufferLE(num, width) {
185
+ assertWidth(width, "toBufferLE");
186
+ if (converter === undefined)
187
+ return Buffer.alloc(0);
188
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
189
+ }
190
+ function toBufferBE(num, width) {
191
+ assertWidth(width, "toBufferBE");
192
+ if (converter === undefined)
193
+ return Buffer.alloc(0);
194
+ return converter.fromBigInt(num, Buffer.allocUnsafe(width), true);
195
+ }
196
+ function parseHex(a, prefix0x = false, byteLength) {
197
+ const hexMatch = a.match(/^(0x)?([\da-fA-F]+)$/);
198
+ if (hexMatch == null) {
199
+ throw new RangeError("input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'");
200
+ }
201
+ let hex = hexMatch[2];
202
+ if (byteLength !== undefined) {
203
+ if (byteLength < hex.length / 2) {
204
+ throw new RangeError(`expected byte length ${byteLength} < input hex byte length ${Math.ceil(hex.length / 2)}`);
205
+ }
206
+ hex = hex.padStart(byteLength * 2, "0");
207
+ }
208
+ return prefix0x ? "0x" + hex : hex;
209
+ }
210
+ function bigintToBuf(a, returnArrayBuffer = false) {
211
+ if (a < 0) {
212
+ throw RangeError("a should be a non-negative integer. Negative values are not supported");
213
+ }
214
+ return hexToBuf(bigintToHex(a), returnArrayBuffer);
215
+ }
216
+ function bufToBigint(buf) {
217
+ let bits = 8n;
218
+ if (ArrayBuffer.isView(buf)) {
219
+ bits = BigInt(buf.BYTES_PER_ELEMENT * 8);
220
+ }
221
+ else {
222
+ buf = new Uint8Array(buf);
223
+ }
224
+ let ret = 0n;
225
+ for (const i of buf.values()) {
226
+ const bi = BigInt(i);
227
+ ret = (ret << bits) + bi;
228
+ }
229
+ return ret;
230
+ }
231
+ function bigintToHex(a, prefix0x = false, byteLength) {
232
+ if (a < 0) {
233
+ throw RangeError("a should be a non-negative integer. Negative values are not supported");
234
+ }
235
+ return parseHex(a.toString(16), prefix0x, byteLength);
236
+ }
237
+ function hexToBigint(hexStr) {
238
+ return BigInt(parseHex(hexStr, true));
239
+ }
240
+ function bigintToText(a) {
241
+ if (a < 0) {
242
+ throw RangeError("a should be a non-negative integer. Negative values are not supported");
243
+ }
244
+ return bufToText(hexToBuf(a.toString(16)));
245
+ }
246
+ function textToBigint(text) {
247
+ return hexToBigint(bufToHex(textToBuf(text)));
248
+ }
249
+ function toBuffer(input) {
250
+ if (Buffer.isBuffer(input)) {
251
+ return input;
252
+ }
253
+ if (ArrayBuffer.isView(input)) {
254
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
255
+ }
256
+ if (input instanceof ArrayBuffer) {
257
+ return Buffer.from(new Uint8Array(input));
258
+ }
259
+ throw new TypeError("Unsupported input type for Buffer.from");
260
+ }
261
+ function bufToText(buf) {
262
+ const input = toBuffer(buf);
263
+ if (converter_1.IS_BROWSER) {
264
+ return new TextDecoder().decode(new Uint8Array(input));
265
+ }
266
+ else {
267
+ return Buffer.from(input).toString();
268
+ }
269
+ }
270
+ function textToBuf(str, returnArrayBuffer = false) {
271
+ if (!converter_1.IS_BROWSER && !returnArrayBuffer) {
272
+ return Buffer.from(new TextEncoder().encode(str).buffer);
273
+ }
274
+ return new TextEncoder().encode(str).buffer;
275
+ }
276
+ function bufToHex(buf, prefix0x = false, byteLength) {
277
+ if (converter_1.IS_BROWSER) {
278
+ let s = "";
279
+ const h = "0123456789abcdef";
280
+ if (ArrayBuffer.isView(buf)) {
281
+ buf = new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
282
+ }
283
+ else {
284
+ buf = new Uint8Array(buf);
285
+ }
286
+ buf.forEach((v) => {
287
+ s += h[v >> 4] + h[v & 15];
288
+ });
289
+ return parseHex(s, prefix0x, byteLength);
290
+ }
291
+ else {
292
+ const input = toBuffer(buf);
293
+ if (ArrayBuffer.isView(input)) {
294
+ buf = new Uint8Array(input.buffer.slice(input.byteOffset, input.byteOffset + input.byteLength));
295
+ }
296
+ return parseHex(Buffer.from(toBuffer(buf)).toString("hex"), prefix0x, byteLength);
297
+ }
298
+ }
299
+ function hexToBuf(hexStr, returnArrayBuffer = false) {
300
+ let hex = parseHex(hexStr);
301
+ hex = parseHex(hexStr, false, Math.ceil(hex.length / 2));
302
+ if (converter_1.IS_BROWSER) {
303
+ return Uint8Array.from(hex.match(/[\da-fA-F]{2}/g).map((h) => {
304
+ return Number("0x" + h);
305
+ })).buffer;
306
+ }
307
+ else {
308
+ const b = Buffer.from(hex, "hex");
309
+ return returnArrayBuffer
310
+ ? b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength)
311
+ : b;
312
+ }
313
+ }
314
+ function bigintToBase64(a, urlsafe = false, padding = true) {
315
+ if (a < 0n) {
316
+ throw new RangeError("negative bigint");
317
+ }
318
+ const buf = bigintToBuf(a);
319
+ let base64 = Buffer.isBuffer(buf)
320
+ ? buf.toString("base64")
321
+ : Buffer.from(buf).toString("base64");
322
+ if (urlsafe) {
323
+ base64 = base64.replace(/\+/g, "-").replace(/\//g, "_");
324
+ }
325
+ if (!padding) {
326
+ base64 = base64.replace(/=+$/, "");
327
+ }
328
+ return base64;
329
+ }
330
+ function base64ToBigint(a) {
331
+ if (!a || a.trim() === "") {
332
+ return 0n;
333
+ }
334
+ const cleaned = a.trim();
335
+ if (!/^[A-Za-z0-9+/=_-]*$/.test(cleaned)) {
336
+ throw new RangeError("invalid base64");
337
+ }
338
+ let base64 = cleaned.replace(/-/g, "+").replace(/_/g, "/");
339
+ while (base64.length % 4 !== 0) {
340
+ base64 += "=";
341
+ }
342
+ const buf = Buffer.from(base64, "base64");
343
+ return bufToBigint(buf);
344
+ }
345
+ exports.FIXED_POINT_DECIMALS = 9;
346
+ exports.FIXED_POINT_PATTERN = /^-?0x[0-9a-f]+$/i;
347
+ exports.ZERO_FIXED_POINT = "0x0";
348
+ const normalizeHex = (value) => value.startsWith("0x") || value.startsWith("0X") ? value : `0x${value}`;
349
+ const toHexString = (value) => {
350
+ if (value === 0n) {
351
+ return exports.ZERO_FIXED_POINT;
352
+ }
353
+ const isNegative = value < 0n;
354
+ const absValue = isNegative ? -value : value;
355
+ const hexValue = bigintToHex(absValue);
356
+ return `${isNegative ? "-" : ""}0x${hexValue}`;
357
+ };
358
+ exports.toHexString = toHexString;
359
+ const toBigIntValue = (value) => {
360
+ if (!value) {
361
+ return 0n;
362
+ }
363
+ const trimmed = value.trim();
364
+ if (trimmed.length === 0) {
365
+ return 0n;
366
+ }
367
+ const isNegative = trimmed.startsWith("-");
368
+ const body = isNegative ? trimmed.slice(1) : trimmed;
369
+ const normalized = normalizeHex(body);
370
+ const bigValue = hexToBigint(normalized);
371
+ return isNegative ? -bigValue : bigValue;
372
+ };
373
+ exports.toBigIntValue = toBigIntValue;
374
+ function toFixedPoint(value, decimals = exports.FIXED_POINT_DECIMALS) {
375
+ if (!Number.isFinite(value)) {
376
+ return exports.ZERO_FIXED_POINT;
377
+ }
378
+ const scale = 10n ** BigInt(decimals);
379
+ const scaled = BigInt(Math.round(value * Number(scale)));
380
+ return (0, exports.toHexString)(scaled);
381
+ }
382
+ function fromFixedPoint(value, decimals = 9) {
383
+ if (!value)
384
+ return 0;
385
+ const trimmed = value.trim();
386
+ if (trimmed.length === 0)
387
+ return 0;
388
+ const isNegative = trimmed.startsWith('-');
389
+ const body = isNegative ? trimmed.slice(1) : trimmed;
390
+ const bigValue = isNegative ? -BigInt(body) : BigInt(body);
391
+ const scale = 10 ** decimals;
392
+ return Number(bigValue) / scale;
393
+ }
394
+ function addFixedPoint(a, b) {
395
+ return (0, exports.toHexString)((0, exports.toBigIntValue)(a) + (0, exports.toBigIntValue)(b));
396
+ }
397
+ function subtractFixedPoint(a, b) {
398
+ return (0, exports.toHexString)((0, exports.toBigIntValue)(a) - (0, exports.toBigIntValue)(b));
399
+ }
400
+ function averageFixedPoint(values) {
401
+ if (values.length === 0) {
402
+ return exports.ZERO_FIXED_POINT;
403
+ }
404
+ const sum = values.reduce((acc, value) => acc + (0, exports.toBigIntValue)(value), 0n);
405
+ return (0, exports.toHexString)(sum / BigInt(values.length));
406
+ }
407
+ function compareFixedPoint(a, b) {
408
+ const diff = (0, exports.toBigIntValue)(a) - (0, exports.toBigIntValue)(b);
409
+ if (diff === 0n) {
410
+ return 0;
411
+ }
412
+ return diff > 0n ? 1 : -1;
413
+ }
414
+ function fixedPointToBigInt(value) {
415
+ return (0, exports.toBigIntValue)(value);
416
+ }
@@ -0,0 +1,2 @@
1
+ export * from './index.js';
2
+ export { default } from './index.js';
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -1,2 +1 @@
1
- export * from "./src/ts/index";
2
- export * from "./src/ts/converter";
1
+ export * from "./src/index";
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var $=Object.create;var m=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,U=Object.prototype.hasOwnProperty;var O=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of j(e))!U.call(t,o)&&o!==n&&m(t,o,{get:()=>e[o],enumerable:!(r=k(e,o))||r.enumerable});return t};var v=(t,e,n)=>(n=t!=null?$(A(t)):{},O(e||!t||!t.__esModule?m(n,"default",{value:t,enumerable:!0}):n,t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let E=!1,l,B;const I=typeof globalThis<"u"&&typeof globalThis.document<"u";let s,h;typeof process<"u"&&process.versions?.node&&!I&&(s=require("path"),h=require("fs"));const T=()=>{if(s)try{const t=require.resolve("@gsknnft/bigint-buffer/package.json");return s.dirname(t)}catch{return}},W=()=>{if(!s)return[];const t=new Set,e=o=>{if(!o)return;const i=s.resolve(o);t.has(i)||t.add(i)},n=T(),r=typeof process<"u"&&process.resourcesPath;return e(process.env?.BIGINT_BUFFER_NATIVE_PATH),e(n),e(n?s.join(n,"dist"):void 0),e(r?s.join(r,"app.asar.unpacked","node_modules","@gsknnft","bigint-buffer"):void 0),e(s.resolve(__dirname,"..")),e(s.resolve(__dirname,"../..")),e(s.resolve(__dirname,"../../..")),e(s.resolve(__dirname,"../../../..")),e(s.resolve(__dirname,"../../../../..")),Array.from(t)},q=()=>{if(!(!s||!h))for(const t of W()){const e=s.join(t,"build","Release","bigint_buffer.node");if(h.existsSync(e))return t}},D=t=>{if(typeof t=="function")return t;if(t&&typeof t=="object"){const e=t.default;if(typeof e=="function")return e;if(e&&typeof e=="object"&&typeof e.default=="function")return e.default}throw new TypeError("bindings is not a function")},H=()=>{if(s)try{const t=require("node-gyp-build"),e=T();return e?t(e):void 0}catch(t){B=t;return}};function _(){const t=H();if(t)return t;try{const e=require("bindings"),n=D(e),r=q();return n(r?{bindings:"bigint_buffer",module_root:r}:"bigint_buffer")}catch(e){B=e;return}}I||(l=_(),E=l!==void 0,!E&&B!==void 0&&process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL!=="1"&&console.warn("bigint-buffer: Failed to load native bindings; using pure JS fallback. Run npm run rebuild to restore native.",B));l===void 0&&(l={toBigInt:(t,e=!0)=>{const n=Buffer.from(t);e||n.reverse();const r=n.toString("hex");return r.length===0?0n:BigInt(`0x${r}`)},fromBigInt:(t,e,n=!0)=>{const r=t.toString(16),o=e.length,i=r.padStart(o*2,"0").slice(0,o*2),c=Buffer.from(i,"hex");return n||c.reverse(),c.copy(e),e}});I||_();function y(t,e=!1,n){const r=t.match(/^(0x)?([\da-fA-F]+)$/);if(r==null)throw new RangeError("input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'");let o=r[2];return e?"0x"+o:o}function V(t,e=!1,n){if(t<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return y(t.toString(16),e)}function G(t){return BigInt(y(t,!0))}const x=9,M=/^-?0x[0-9a-f]+$/i,b="0x0",X=t=>t.startsWith("0x")||t.startsWith("0X")?t:`0x${t}`,p=t=>{if(t===0n)return b;const e=t<0n,n=e?-t:t,r=V(n);return`${e?"-":""}0x${r}`},u=t=>{if(!t)return 0n;const e=t.trim();if(e.length===0)return 0n;const n=e.startsWith("-"),r=n?e.slice(1):e,o=X(r),i=G(o);return n?-i:i};function C(t,e=x){if(!Number.isFinite(t))return b;const n=10n**BigInt(e),r=BigInt(Math.round(t*Number(n)));return p(r)}function z(t,e=9){if(!t)return 0;const n=t.trim();if(n.length===0)return 0;const r=n.startsWith("-"),o=r?n.slice(1):n,i=r?-BigInt(o):BigInt(o),c=10**e;return Number(i)/c}function Z(t,e){return p(u(t)+u(e))}function J(t,e){return p(u(t)-u(e))}function K(t){if(t.length===0)return b;const e=t.reduce((n,r)=>n+u(r),0n);return p(e/BigInt(t.length))}function Q(t,e){const n=u(t)-u(e);return n===0n?0:n>0n?1:-1}function Y(t){return u(t)}const tt=typeof Buffer.prototype.readBigUInt64LE=="function",et=typeof Buffer.prototype.readBigUInt64BE=="function",nt=typeof Buffer.prototype.writeBigUInt64LE=="function",rt=typeof Buffer.prototype.writeBigUInt64BE=="function",ot=tt?(t,e)=>t.readBigUInt64LE(e):(t,e)=>{let n=0n;for(let r=7;r>=0;r--)n=(n<<8n)+BigInt(t[e+r]);return n},it=et?(t,e)=>t.readBigUInt64BE(e):(t,e)=>{let n=0n;for(let r=0;r<8;r++)n=(n<<8n)+BigInt(t[e+r]);return n},ft=nt?(t,e,n)=>{t.writeBigUInt64LE(n,e)}:(t,e,n)=>{let r=n;for(let o=0;o<8;o++)t[e+o]=Number(r&0xffn),r>>=8n},st=rt?(t,e,n)=>{t.writeBigUInt64BE(n,e)}:(t,e,n)=>{let r=n;for(let o=7;o>=0;o--)t[e+o]=Number(r&0xffn),r>>=8n},ut=t=>{let e=0n,n=1n;const r=t.length,o=r&7;for(let i=0;i<o;i++)e+=BigInt(t[i])*n,n<<=8n;for(let i=o;i<r;i+=8){const c=ot(t,i);e+=c*n,n<<=64n}return e},ct=t=>{const e=t.length;if(e===0)return 0n;let n=0n;const r=e&7;let o=0;if(r!==0)for(;o<r;o++)n=(n<<8n)+BigInt(t[o]);for(;o<e;o+=8){const i=it(t,o);n=(n<<64n)+i}return n},at=(t,e)=>{const n=e.length;let r=t,o=0;const i=n-n%8;for(;o<i;o+=8)ft(e,o,r&0xffffffffffffffffn),r>>=64n;for(;o<n;o++)e[o]=Number(r&0xffn),r>>=8n;return e},dt=(t,e)=>{const n=e.length;let r=t,o=n;const i=n&-8;for(;o>i;)o--,e[o]=Number(r&0xffn),r>>=8n;for(;o>0;o-=8){const c=r&0xffffffffffffffffn;st(e,o-8,c),r>>=64n}return e},gt=t=>{if(t===0n)return 1;let e=t,n=0;for(;e>0n;)n++,e>>=8n;return n},lt={toBigInt:(t,e=!0)=>e?ct(t):ut(t),fromBigInt:(t,e,n=!0)=>n?dt(t,e):at(t,e)},w=typeof globalThis<"u"&&typeof globalThis.document<"u";let f,a;w||(f=require("path"),a=require("fs"));const Bt=()=>{if(!f)return[];const t=S(),e=typeof process<"u"&&process.resourcesPath;return[process.env?.BIGINT_BUFFER_NATIVE_PATH,t,t?f.join(t,"dist"):void 0,e?f.join(e,"app.asar.unpacked","node_modules","@gsknnft","bigint-buffer"):void 0,f.resolve(__dirname,".."),f.resolve(__dirname,"."),f.resolve(__dirname,"../.."),f.resolve(__dirname,"../../.."),f.resolve(__dirname,"../../../..")].filter(n=>!!n)},bt=()=>{if(!(!f||!a))for(const t of Bt()){const e=f.join(t,"build","Release","bigint_buffer.node");if(a.existsSync(e))return t;const n=f.join(t,"dist","build","Release","bigint_buffer.node");if(a.existsSync(n))return f.join(t,"dist")}},pt=()=>{if(f)try{const t=require("node-gyp-build"),e=S();return e?t(e):void 0}catch(t){d=t;return}},ht=t=>{if(typeof t=="function")return t;if(t&&typeof t=="object"){const e=t.default;if(typeof e=="function")return e;if(e&&typeof e=="object"&&typeof e.default=="function")return e.default}throw new TypeError("bindings is not a function")};let g=lt,d;exports.isNative=!1;const It=()=>{d=void 0;const t=pt();if(t)return t;try{const e=require("bindings"),n=ht(e),r=bt();return n(r?{bindings:"bigint_buffer",module_root:r}:"bigint_buffer")}catch(e){d=d??e;return}};if(!w){const t=It();t!==void 0?(g=t,exports.isNative=!0):d!==void 0&&process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL!=="1"&&console.warn("bigint-buffer: Failed to load native bindings; using pure JS fallback. Run npm run rebuild to restore native.",d)}function N(t){return g.toBigInt(t,!1)}function mt(){try{return N(Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function R(t){return g.toBigInt(t,!0)}function vt(t,e){if(e<0)throw new RangeError("toBufferLE width must be non-negative");const n=e===0?Buffer.alloc(0):Buffer.allocUnsafe(e);return g.fromBigInt(t,n,!1)}function P(t,e){if(e<0)throw new RangeError("toBufferBE width must be non-negative");const n=e===0?Buffer.alloc(0):Buffer.allocUnsafe(e);return g.fromBigInt(t,n,!0)}function F(t){if(t<BigInt(0))throw new Error("bigintToBuf: negative bigint values are not supported");const e=gt(t);return P(t,e)}function L(t){return R(t)}function Et(t){if(t<BigInt(0))throw new Error("bigintToHex: negative bigint values are not supported");const e=t.toString(16);return e.length%2===0?e:"0"+e}function Tt(t){const e=t.startsWith("0x")?t.slice(2):t;return e.length===0?BigInt(0):BigInt(`0x${e}`)}function _t(t){return t.toString(10)}function yt(t){if(!t?.trim())throw new Error("textToBigint: input string cannot be empty");try{return BigInt(t)}catch(e){throw new Error(`textToBigint: invalid decimal string "${t}" ${e instanceof Error?e.message:String(e)}`)}}function xt(t){if(t<BigInt(0))throw new Error("bigintToBase64: negative bigint values are not supported");return F(t).toString("base64")}function wt(t){if(!t?.trim())throw new Error("base64ToBigint: input string cannot be empty");const e=t.trim();if(!/^[A-Za-z0-9+/]+=*$/.test(e))throw new Error("base64ToBigint: invalid base64 string format");const n=Buffer.from(e,"base64");return L(n)}function S(){if(!f)return;try{const e=require.resolve("@gsknnft/bigint-buffer/package.json");return f.dirname(e)}catch{}if(!a)return;let t=__dirname;for(;;){const e=f.join(t,"package.json");if(a.existsSync(e))return t;const n=f.dirname(t);if(n===t)break;t=n}}exports.FIXED_POINT_DECIMALS=x;exports.FIXED_POINT_PATTERN=M;exports.ZERO_FIXED_POINT=b;exports.addFixedPoint=Z;exports.averageFixedPoint=K;exports.base64ToBigint=wt;exports.bigintToBase64=xt;exports.bigintToBuf=F;exports.bigintToHex=Et;exports.bigintToText=_t;exports.bufToBigint=L;exports.compareFixedPoint=Q;exports.fixedPointToBigInt=Y;exports.fromFixedPoint=z;exports.hexToBigint=Tt;exports.subtractFixedPoint=J;exports.textToBigint=yt;exports.toBigIntBE=R;exports.toBigIntLE=N;exports.toBigIntValue=u;exports.toBufferBE=P;exports.toBufferLE=vt;exports.toFixedPoint=C;exports.validateBigIntBuffer=mt;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let g;const l=typeof globalThis<"u"&&typeof globalThis.document<"u";let f,c;typeof process<"u"&&process.versions?.node&&!l&&(f=require("path"),c=require("fs"));const b=()=>{if(!f||!c)return;let t=__dirname;for(;;){const o=f.join(t,"node_modules","@gsknnft","bigint-buffer","package.json");if(c.existsSync(o))return f.dirname(o);const s=f.dirname(t);if(s===t)break;t=s}const n=typeof require=="function"&&typeof require.resolve=="function",e=o=>{if(n)try{return require.resolve(o)}catch{return}},r=e("@gsknnft/bigint-buffer")??e("@gsknnft/bigint-buffer/dist/index.cjs")??e("@gsknnft/bigint-buffer/dist/index.js");let i=r?f.dirname(r):__dirname;for(;;){const o=f.join(i,"package.json");if(c.existsSync(o))return i;const s=f.dirname(i);if(s===i)break;i=s}},w=()=>{if(!f)return[];const t=new Set,n=o=>{if(!o)return;const s=f.resolve(o);t.has(s)||t.add(s)},e=b(),r=typeof process<"u"&&process.resourcesPath;n(process.env?.BIGINT_BUFFER_NATIVE_PATH),n(e),n(e?f.join(e,"dist"):void 0),n(r?f.join(r,"app.asar.unpacked","node_modules","@gsknnft","bigint-buffer"):void 0),n(f.resolve(__dirname,"..")),n(f.resolve(__dirname,"../..")),n(f.resolve(__dirname,"../../..")),n(f.resolve(__dirname,"../../../..")),n(f.resolve(__dirname,"../../../../.."));const i=Array.from(t);return process.env.BIGINT_BUFFER_DEBUG&&console.log("bigint-buffer: candidateRoots:",i),i},F=()=>{if(!(!f||!c)){for(const t of w()){const n=f.join(t,"build","Release","bigint_buffer.node");if(process.env.BIGINT_BUFFER_DEBUG&&console.log("bigint-buffer: checking for native at",n),c.existsSync(n))return process.env.BIGINT_BUFFER_DEBUG&&console.log("bigint-buffer: found native at",n),t;const e=f.join(t,"dist","build","Release","bigint_buffer.node");if(process.env.BIGINT_BUFFER_DEBUG&&console.log("bigint-buffer: checking for native at",e),c.existsSync(e))return process.env.BIGINT_BUFFER_DEBUG&&console.log("bigint-buffer: found native at",e),f.join(t,"dist")}process.env.BIGINT_BUFFER_DEBUG&&console.warn("bigint-buffer: native binary not found in any candidate root")}},N=t=>{if(typeof t=="function")return t;if(t&&typeof t=="object"){const n=t.default;if(typeof n=="function")return n;if(n&&typeof n=="object"&&typeof n.default=="function")return n.default}throw new TypeError("bindings is not a function")},R=()=>{if(f)try{const t=require("node-gyp-build"),n=b();return n?t(n):void 0}catch(t){g=t;return}};function I(){g=void 0;const t=R();if(t)return t;try{const n=require("bindings"),e=N(n),r=b(),i=F()??r;return e(i?{bindings:"bigint_buffer",module_root:i}:"bigint_buffer")}catch(n){g=n,process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL!=="1"&&console.warn("bigint-buffer: Failed to load native bindings; using pure JS fallback. Run npm run rebuild to restore native.",g)}}Buffer.prototype.readBigUInt64LE;Buffer.prototype.readBigUInt64BE;Buffer.prototype.writeBigUInt64LE;Buffer.prototype.writeBigUInt64BE;l||I();function p(t,n=!1,e){const r=t.match(/^(0x)?([\da-fA-F]+)$/);if(r==null)throw new RangeError("input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'");let i=r[2];return n?"0x"+i:i}function U(t,n=!1,e){if(t<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return p(t.toString(16),n)}function P(t){return BigInt(p(t,!0))}const E=9,L=/^-?0x[0-9a-f]+$/i,B="0x0",k=t=>t.startsWith("0x")||t.startsWith("0X")?t:`0x${t}`,d=t=>{if(t===0n)return B;const n=t<0n,e=n?-t:t,r=U(e);return`${n?"-":""}0x${r}`},u=t=>{if(!t)return 0n;const n=t.trim();if(n.length===0)return 0n;const e=n.startsWith("-"),r=e?n.slice(1):n,i=k(r),o=P(i);return e?-o:o};function S(t,n=E){if(!Number.isFinite(t))return B;const e=10n**BigInt(n),r=BigInt(Math.round(t*Number(e)));return d(r)}function D(t,n=9){if(!t)return 0;const e=t.trim();if(e.length===0)return 0;const r=e.startsWith("-"),i=r?e.slice(1):e,o=r?-BigInt(i):BigInt(i),s=10**n;return Number(o)/s}function G(t,n){return d(u(t)+u(n))}function j(t,n){return d(u(t)-u(n))}function A(t){if(t.length===0)return B;const n=t.reduce((e,r)=>e+u(r),0n);return d(n/BigInt(t.length))}function $(t,n){const e=u(t)-u(n);return e===0n?0:e>0n?1:-1}function O(t){return u(t)}const W=typeof Buffer.prototype.readBigUInt64LE=="function",H=typeof Buffer.prototype.readBigUInt64BE=="function",V=typeof Buffer.prototype.writeBigUInt64LE=="function",q=typeof Buffer.prototype.writeBigUInt64BE=="function",X=W?(t,n)=>t.readBigUInt64LE(n):(t,n)=>{let e=0n;for(let r=7;r>=0;r--)e=(e<<8n)+BigInt(t[n+r]);return e},C=H?(t,n)=>t.readBigUInt64BE(n):(t,n)=>{let e=0n;for(let r=0;r<8;r++)e=(e<<8n)+BigInt(t[n+r]);return e},M=V?(t,n,e)=>{t.writeBigUInt64LE(e,n)}:(t,n,e)=>{let r=e;for(let i=0;i<8;i++)t[n+i]=Number(r&0xffn),r>>=8n},z=q?(t,n,e)=>{t.writeBigUInt64BE(e,n)}:(t,n,e)=>{let r=e;for(let i=7;i>=0;i--)t[n+i]=Number(r&0xffn),r>>=8n},Z=t=>{let n=0n,e=1n;const r=t.length,i=r&7;for(let o=0;o<i;o++)n+=BigInt(t[o])*e,e<<=8n;for(let o=i;o<r;o+=8){const s=X(t,o);n+=s*e,e<<=64n}return n},J=t=>{const n=t.length;if(n===0)return 0n;let e=0n;const r=n&7;let i=0;if(r!==0)for(;i<r;i++)e=(e<<8n)+BigInt(t[i]);for(;i<n;i+=8){const o=C(t,i);e=(e<<64n)+o}return e},K=(t,n)=>{const e=n.length;let r=t,i=0;const o=e-e%8;for(;i<o;i+=8)M(n,i,r&0xffffffffffffffffn),r>>=64n;for(;i<e;i++)n[i]=Number(r&0xffn),r>>=8n;return n},Q=(t,n)=>{const e=n.length;let r=t,i=e;const o=e&-8;for(;i>o;)i--,n[i]=Number(r&0xffn),r>>=8n;for(;i>0;i-=8){const s=r&0xffffffffffffffffn;z(n,i-8,s),r>>=64n}return n},Y=t=>{if(t===0n)return 1;let n=t,e=0;for(;n>0n;)e++,n>>=8n;return e},m=(t,n)=>{if(!Number.isInteger(t)||t<0)throw new RangeError(`${n} width must be a non-negative integer`)},h=t=>Buffer.isBuffer(t)?t:t instanceof Uint8Array?Buffer.from(t.buffer,t.byteOffset,t.byteLength):Buffer.from(t),tt={toBigInt:(t,n=!0)=>n?J(t):Z(t),fromBigInt:(t,n,e=!0)=>e?Q(t,n):K(t,n)};let a=tt;exports.isNative=!1;if(!l){const t=I();t!==void 0&&(a=t,exports.isNative=!0)}function T(t){return a.toBigInt(h(t),!1)}function nt(){try{return T(Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function y(t){return a.toBigInt(h(t),!0)}function et(t,n){m(n,"toBufferLE");const e=n===0?Buffer.alloc(0):Buffer.allocUnsafe(n);return a.fromBigInt(t,e,!1)}function v(t,n){m(n,"toBufferBE");const e=n===0?Buffer.alloc(0):Buffer.allocUnsafe(n);return a.fromBigInt(t,e,!0)}function _(t){if(t<BigInt(0))throw new Error("bigintToBuf: negative bigint values are not supported");const n=Y(t);return v(t,n)}function x(t){return y(t)}function rt(t){if(t<BigInt(0))throw new Error("bigintToHex: negative bigint values are not supported");const n=t.toString(16);return n.length%2===0?n:"0"+n}function it(t){const n=t.startsWith("0x")?t.slice(2):t;return n.length===0?BigInt(0):BigInt(`0x${n}`)}function ot(t){return t.toString(10)}function ft(t){if(!t?.trim())throw new Error("textToBigint: input string cannot be empty");try{return BigInt(t)}catch(n){throw new Error(`textToBigint: invalid decimal string "${t}" ${n instanceof Error?n.message:String(n)}`)}}function st(t){if(t<BigInt(0))throw new Error("bigintToBase64: negative bigint values are not supported");return _(t).toString("base64")}function ut(t){if(!t?.trim())throw new Error("base64ToBigint: input string cannot be empty");const n=t.trim();if(!/^[A-Za-z0-9+/=_-]+$/.test(n))throw new Error("base64ToBigint: invalid base64 string format");let e=n.replace(/-/g,"+").replace(/_/g,"/");for(;e.length%4!==0;)e+="=";const r=Buffer.from(e,"base64");return x(r)}exports.FIXED_POINT_DECIMALS=E;exports.FIXED_POINT_PATTERN=L;exports.ZERO_FIXED_POINT=B;exports.addFixedPoint=G;exports.averageFixedPoint=A;exports.base64ToBigint=ut;exports.bigintToBase64=st;exports.bigintToBuf=_;exports.bigintToHex=rt;exports.bigintToText=ot;exports.bufToBigint=x;exports.compareFixedPoint=$;exports.fixedPointToBigInt=O;exports.fromFixedPoint=D;exports.hexToBigint=it;exports.subtractFixedPoint=j;exports.textToBigint=ft;exports.toBigIntBE=y;exports.toBigIntLE=T;exports.toBigIntValue=u;exports.toBufferBE=v;exports.toBufferLE=et;exports.toFixedPoint=S;exports.validateBigIntBuffer=nt;
package/dist/index.d.ts CHANGED
@@ -1,18 +1,19 @@
1
1
  export { FIXED_POINT_DECIMALS, FIXED_POINT_PATTERN, ZERO_FIXED_POINT, toFixedPoint, fromFixedPoint, addFixedPoint, subtractFixedPoint, averageFixedPoint, compareFixedPoint, fixedPointToBigInt, toBigIntValue, } from "./conversion/src/ts/index";
2
+ type ByteInput = Buffer | Uint8Array | ArrayBuffer;
2
3
  export declare let isNative: boolean;
3
4
  /**
4
5
  * Convert a little-endian buffer into a BigInt.
5
6
  * @param buf The little-endian buffer to convert
6
7
  * @returns A BigInt with the little-endian representation of buf.
7
8
  */
8
- export declare function toBigIntLE(buf: Buffer): bigint;
9
+ export declare function toBigIntLE(buf: ByteInput): bigint;
9
10
  export declare function validateBigIntBuffer(): boolean;
10
11
  /**
11
12
  * Convert a big-endian buffer into a BigInt
12
13
  * @param buf The big-endian buffer to convert.
13
14
  * @returns A BigInt with the big-endian representation of buf.
14
15
  */
15
- export declare function toBigIntBE(buf: Buffer): bigint;
16
+ export declare function toBigIntBE(buf: ByteInput): bigint;
16
17
  /**
17
18
  * Convert a BigInt to a little-endian buffer.
18
19
  * @param num The BigInt to convert.
@@ -40,7 +41,7 @@ export declare function bigintToBuf(num: bigint): Buffer;
40
41
  * @param buf The buffer to convert
41
42
  * @returns A bigint representation of the buffer
42
43
  */
43
- export declare function bufToBigint(buf: Buffer): bigint;
44
+ export declare function bufToBigint(buf: ByteInput): bigint;
44
45
  /**
45
46
  * Convert a bigint to a hexadecimal string.
46
47
  * @param num The bigint to convert