@gsknnft/bigint-buffer 1.3.2 → 1.4.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 (106) hide show
  1. package/README.md +123 -126
  2. package/build/Release/bigint_buffer.exp +0 -0
  3. package/build/Release/bigint_buffer.iobj +0 -0
  4. package/build/Release/bigint_buffer.ipdb +0 -0
  5. package/build/Release/bigint_buffer.lib +0 -0
  6. package/build/Release/bigint_buffer.node +0 -0
  7. package/build/Release/bigint_buffer.pdb +0 -0
  8. package/build/Release/obj/bigint_buffer/bigint_buffer.node.recipe +11 -0
  9. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.command.1.tlog +0 -0
  10. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.read.1.tlog +0 -0
  11. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/CL.write.1.tlog +0 -0
  12. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/Cl.items.tlog +2 -0
  13. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/bigint_buffer.lastbuildstate +2 -0
  14. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.command.1.tlog +0 -0
  15. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.read.1.tlog +0 -0
  16. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.secondary.1.tlog +5 -0
  17. package/build/Release/obj/bigint_buffer/bigint_buffer.tlog/link.write.1.tlog +0 -0
  18. package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
  19. package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
  20. package/build/bigint_buffer.vcxproj +148 -0
  21. package/build/bigint_buffer.vcxproj.filters +67 -0
  22. package/build/binding.sln +19 -0
  23. package/build/config.gypi +522 -0
  24. package/dist/index.cjs +1 -205
  25. package/dist/index.js +347 -202
  26. package/dist/index.umd.js +1 -0
  27. package/dist/tsconfig.tsbuildinfo +1 -0
  28. package/dist/types/bigint-buffer.test.d.ts +1 -0
  29. package/dist/types/conversion/index.d.ts +1 -0
  30. package/dist/types/conversion/src/ts/index.d.ts +168 -0
  31. package/dist/types/conversion/test/bigintToBase64.test.d.ts +1 -0
  32. package/dist/types/conversion/test/bigintToBuf.test.d.ts +1 -0
  33. package/dist/types/conversion/test/bigintToHex.test.d.ts +1 -0
  34. package/dist/types/conversion/test/bigintToText.test.d.ts +1 -0
  35. package/dist/types/conversion/test/bufToBigint.test.d.ts +1 -0
  36. package/dist/types/conversion/test/hexToBigint.test.d.ts +1 -0
  37. package/dist/types/conversion/test/hexToBuf.test.d.ts +1 -0
  38. package/dist/types/conversion/test/parseHex.test.d.ts +1 -0
  39. package/dist/types/conversion/test/setup.test.d.ts +1 -0
  40. package/dist/types/conversion/test/textToBuf.test.d.ts +1 -0
  41. package/dist/types/conversion/vite.config.d.ts +2 -0
  42. package/dist/types/conversion/vitest.config.d.ts +2 -0
  43. package/dist/types/dist/dist/conversion/conversion/index.d.ts +1 -0
  44. package/dist/{index.d.ts → types/dist/dist/index.d.ts} +2 -0
  45. package/dist/types/index.bench.d.ts +1 -0
  46. package/dist/types/index.d.ts +80 -0
  47. package/dist/types/index.spec.d.ts +1 -0
  48. package/package.json +83 -49
  49. package/src/bigint-buffer.test.ts +11 -0
  50. package/src/conversion/.github/workflows/build-and-test.yml +116 -0
  51. package/src/conversion/CODE_OF_CONDUCT.md +134 -0
  52. package/src/conversion/LICENSE +21 -0
  53. package/src/conversion/README.md +48 -0
  54. package/src/conversion/docs/README.md +34 -0
  55. package/src/conversion/docs/functions/base64ToBigint.md +27 -0
  56. package/src/conversion/docs/functions/bigintToBase64.md +43 -0
  57. package/src/conversion/docs/functions/bigintToBuf.md +35 -0
  58. package/src/conversion/docs/functions/bigintToHex.md +43 -0
  59. package/src/conversion/docs/functions/bigintToText.md +31 -0
  60. package/src/conversion/docs/functions/bufToBigint.md +25 -0
  61. package/src/conversion/docs/functions/bufToHex.md +37 -0
  62. package/src/conversion/docs/functions/bufToText.md +27 -0
  63. package/src/conversion/docs/functions/hexToBigint.md +29 -0
  64. package/src/conversion/docs/functions/hexToBuf.md +37 -0
  65. package/src/conversion/docs/functions/parseHex.md +45 -0
  66. package/src/conversion/docs/functions/textToBigint.md +27 -0
  67. package/src/conversion/docs/functions/textToBuf.md +33 -0
  68. package/src/conversion/docs/functions/toBigIntBE.md +27 -0
  69. package/src/conversion/docs/functions/toBigIntLE.md +27 -0
  70. package/src/conversion/docs/functions/toBufferBE.md +33 -0
  71. package/src/conversion/docs/functions/toBufferLE.md +33 -0
  72. package/src/conversion/docs/functions/validateBigIntBuffer.md +15 -0
  73. package/src/conversion/docs/type-aliases/TypedArray.md +11 -0
  74. package/src/conversion/docs/variables/isNative.md +11 -0
  75. package/src/conversion/example.cjs +9 -0
  76. package/src/conversion/example.esm.js +11 -0
  77. package/src/conversion/index.ts +1 -0
  78. package/src/conversion/package.json +163 -0
  79. package/src/conversion/src/docs/index.md +47 -0
  80. package/src/conversion/src/ts/index.ts +514 -0
  81. package/src/conversion/test/bigintToBase64.test.ts +37 -0
  82. package/src/conversion/test/bigintToBuf.test.ts +43 -0
  83. package/src/conversion/test/bigintToHex.test.ts +52 -0
  84. package/src/conversion/test/bigintToText.test.ts +30 -0
  85. package/src/conversion/test/bufToBigint.test.ts +20 -0
  86. package/src/conversion/test/hexToBigint.test.ts +22 -0
  87. package/src/conversion/test/hexToBuf.test.ts +39 -0
  88. package/src/conversion/test/parseHex.test.ts +35 -0
  89. package/src/conversion/test/setup.test.ts +9 -0
  90. package/src/conversion/test/textToBuf.test.ts +26 -0
  91. package/src/conversion/tsconfig.json +57 -0
  92. package/src/conversion/tsconfig.rollup.json +9 -0
  93. package/src/conversion/typedoc.json +5 -0
  94. package/src/conversion/types/bindings.d.t.s +4 -0
  95. package/src/conversion/vite.config.ts +10 -0
  96. package/src/conversion/vitest.config.ts +15 -0
  97. package/src/index.bench.ts +5 -6
  98. package/src/index.spec.ts +232 -358
  99. package/src/index.ts +33 -28
  100. package/.travis.yml +0 -51
  101. package/PR_TEMPLATE.md +0 -53
  102. package/dist/node.js +0 -94
  103. package/karma.conf.js +0 -62
  104. package/rollup.cjs.config.js +0 -8
  105. package/rollup.esm.config.js +0 -15
  106. package/tsconfig.json +0 -20
package/dist/index.cjs CHANGED
@@ -1,205 +1 @@
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
- }
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("buffer"),c=require("path"),U=require("fs");let B=!1,f,l;const s=typeof globalThis<"u"&&typeof globalThis.document<"u",x=[c.resolve(__dirname,".."),c.resolve(__dirname,"../../.."),c.resolve(__dirname,"../../../../")],N=()=>{for(const e of x){const t=c.join(e,"build","Release","bigint_buffer.node");if(U.existsSync(t))return e}return x[0]};function L(){try{const e=require("bindings"),t=N();return e({bindings:"bigint_buffer",module_root:t})}catch(e){l=e;return}}s||(f=L(),B=f!==void 0,!B&&l!==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.",l));f===void 0&&(f={toBigInt:(e,t=!0)=>{const n=o.Buffer.from(e);t||n.reverse();const r=n.toString("hex");return r.length===0?0n:BigInt(`0x${r}`)},fromBigInt:(e,t,n=!0)=>{const r=e.toString(16),i=t.length,g=r.padStart(i*2,"0").slice(0,i*2),h=o.Buffer.from(g,"hex");return n||h.reverse(),h.copy(t),t}});function v(e){if(s||f===void 0){const t=o.Buffer.from(e);t.reverse();const n=t.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return f.toBigInt(e,!1)}function O(){try{return v(o.Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function H(e){if(s||f===void 0){const t=e.toString("hex");return t.length===0?BigInt(0):BigInt(`0x${t}`)}return f.toBigInt(e,!0)}function M(e,t){if(s||f===void 0){const n=e.toString(16),r=o.Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex");return r.reverse(),r}return f.fromBigInt(e,o.Buffer.allocUnsafe(t),!1)}function F(e,t){if(s||f===void 0){const n=e.toString(16);return o.Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex")}return f.fromBigInt(e,o.Buffer.allocUnsafe(t),!0)}function a(e,t=!1,n){const r=e.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];if(n!==void 0){if(n<i.length/2)throw new RangeError(`expected byte length ${n} < input hex byte length ${Math.ceil(i.length/2)}`);i=i.padStart(n*2,"0")}return t?"0x"+i:i}function T(e,t=!1){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return b(I(e),t)}function p(e){let t=8n;ArrayBuffer.isView(e)?t=BigInt(e.BYTES_PER_ELEMENT*8):e=new Uint8Array(e);let n=0n;for(const r of e.values()){const i=BigInt(r);n=(n<<t)+i}return n}function I(e,t=!1,n){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return a(e.toString(16),t,n)}function w(e){return BigInt(a(e,!0))}function q(e){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return m(b(e.toString(16)))}function V(e){return w(E(y(e)))}function d(e){if(o.Buffer.isBuffer(e))return e;if(ArrayBuffer.isView(e))return o.Buffer.from(e.buffer,e.byteOffset,e.byteLength);if(e instanceof ArrayBuffer)return o.Buffer.from(new Uint8Array(e));throw new TypeError("Unsupported input type for Buffer.from")}function m(e){const t=d(e);return s?new TextDecoder().decode(new Uint8Array(t)):o.Buffer.from(t).toString()}function y(e,t=!1){return!s&&!t?o.Buffer.from(new TextEncoder().encode(e).buffer):new TextEncoder().encode(e).buffer}function E(e,t=!1,n){if(s){let r="";const i="0123456789abcdef";return ArrayBuffer.isView(e)?e=new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)):e=new Uint8Array(e),e.forEach(g=>{r+=i[g>>4]+i[g&15]}),a(r,t,n)}else{const r=d(e);return ArrayBuffer.isView(r)&&(e=new Uint8Array(r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength))),a(o.Buffer.from(d(e)).toString("hex"),t,n)}}function b(e,t=!1){let n=a(e);if(n=a(e,!1,Math.ceil(n.length/2)),s)return Uint8Array.from(n.match(/[\da-fA-F]{2}/g).map(r=>+("0x"+r))).buffer;{const r=o.Buffer.from(n,"hex");return t?r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength):r}}function j(e,t=!1,n=!0){if(e<0n)throw new RangeError("negative bigint");const r=T(e);let i=o.Buffer.isBuffer(r)?r.toString("base64"):o.Buffer.from(r).toString("base64");return t&&(i=i.replace(/\+/g,"-").replace(/\//g,"_")),n||(i=i.replace(/=+$/,"")),i}function z(e){if(!e||e.trim()==="")return 0n;const t=e.trim();if(!/^[A-Za-z0-9+/=_-]*$/.test(t))throw new RangeError("invalid base64");let n=t.replace(/-/g,"+").replace(/_/g,"/");for(;n.length%4!==0;)n+="=";const r=o.Buffer.from(n,"base64");return p(r)}const P=Object.freeze(Object.defineProperty({__proto__:null,base64ToBigint:z,bigintToBase64:j,bigintToBuf:T,bigintToHex:I,bigintToText:q,bufToBigint:p,bufToHex:E,bufToText:m,hexToBigint:w,hexToBuf:b,get isNative(){return B},parseHex:a,textToBigint:V,textToBuf:y,toBigIntBE:H,toBigIntLE:v,toBufferBE:F,toBufferLE:M,validateBigIntBuffer:O},Symbol.toStringTag,{value:"Module"}));let u;exports.isNative=!1;if(typeof window>"u")try{u=require("bindings")("bigint_buffer"),exports.isNative=u!==void 0}catch(e){console.warn("bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)",e)}function S(e){if(typeof window<"u"||u===void 0){const t=Buffer.from(e);t.reverse();const n=t.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return u.toBigInt(e,!1)}function J(){try{return S(Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function $(e){if(typeof window<"u"||u===void 0){const t=e.toString("hex");return t.length===0?BigInt(0):BigInt(`0x${t}`)}return u.toBigInt(e,!0)}function W(e,t){if(typeof window<"u"||u===void 0){const n=e.toString(16),r=Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex");return r.reverse(),r}return u.fromBigInt(e,Buffer.allocUnsafe(t),!1)}function _(e,t){if(typeof window<"u"||u===void 0){const n=e.toString(16);return Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex")}return u.fromBigInt(e,Buffer.allocUnsafe(t),!0)}function A(e){if(e<BigInt(0))throw new Error("bigintToBuf: negative bigint values are not supported");if(e===BigInt(0))return Buffer.from([0]);const t=e.toString(16),n=Math.ceil(t.length/2);return _(e,n)}function R(e){return $(e)}function Z(e){if(e<BigInt(0))throw new Error("bigintToHex: negative bigint values are not supported");const t=e.toString(16);return t.length%2===0?t:"0"+t}function k(e){const t=e.startsWith("0x")?e.slice(2):e;return t.length===0?BigInt(0):BigInt(`0x${t}`)}function D(e){return e.toString(10)}function G(e){if(!e?.trim())throw new Error("textToBigint: input string cannot be empty");try{return BigInt(e)}catch(t){throw new Error(`textToBigint: invalid decimal string "${e}" ${t instanceof Error?t.message:String(t)}`)}}function Y(e){if(e<BigInt(0))throw new Error("bigintToBase64: negative bigint values are not supported");return A(e).toString("base64")}function C(e){if(!e?.trim())throw new Error("base64ToBigint: input string cannot be empty");const t=e.trim();if(!/^[A-Za-z0-9+/]+=*$/.test(t))throw new Error("base64ToBigint: invalid base64 string format");const n=Buffer.from(t,"base64");return R(n)}exports.base64ToBigint=C;exports.bigintToBase64=Y;exports.bigintToBuf=A;exports.bigintToHex=Z;exports.bigintToText=D;exports.bufToBigint=R;exports.conversionUtils=P;exports.hexToBigint=k;exports.textToBigint=G;exports.toBigIntBE=$;exports.toBigIntLE=S;exports.toBufferBE=_;exports.toBufferLE=W;exports.validateBigIntBuffer=J;