@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
package/README.md CHANGED
@@ -1,144 +1,150 @@
1
- ![CI](https://github.com/gsknknft/bigint-buffer/actions/workflows/ci.yaml/badge.svg)
2
- ![Release](https://github.com/gsknknft/bigint-buffer/actions/workflows/release.yaml/badge.svg)
3
-
4
- # @gsknnft/bigint-buffer
5
-
6
- Secure BigInt Buffer conversion with native bindings, browser fallbacks, and the `bigint-conversion` helper APIs built in. This is the actively maintained fork of the original `bigint-buffer`.
7
-
8
- **Upgrade notice:** The current 1.4.7 line ships chunked, allocation-free BE/LE converters (Buffer.read/writeBigUInt64* when available) that are fuzzed across empty, tiny, and huge buffers, alongside FixedPoint utilities and packaged native bindings. CI-verified for Node 20–24. Upgrade for the fastest conversions and consistent behaviour across environments.
9
-
10
- ---
11
-
12
- ## Migration Guide
13
-
14
- - If upgrading from legacy `bigint-buffer`, switch all imports to `@gsknnft/bigint-buffer`.
15
- - All helpers are now available as named exports (no default export).
16
- - For Electron, ensure the native binary is included in `extraResources` or `asarUnpack` so it ends up under `resources/app.asar.unpacked/node_modules/@gsknnft/bigint-buffer/build/Release/`.
17
- - If your packager relocates files, set `BIGINT_BUFFER_NATIVE_PATH` to the directory containing `build/Release/bigint_buffer.node` before launching the app.
18
- - For browser, add polyfills for `Buffer`, `path`, and `fs` as needed (see below).
19
- - ESM, CJS, and TypeScript types are all supported out of the box.
20
-
21
- ---
22
-
23
- [![NPM Version](https://img.shields.io/npm/v/@gsknnft/bigint-buffer.svg?style=flat-square)](https://www.npmjs.com/package/@gsknnft/bigint-buffer)
24
- [![Node Version](https://img.shields.io/node/v/@gsknnft/bigint-buffer.svg?style=flat-square)](https://nodejs.org)
25
-
26
- ---
27
-
28
- ## Why This Package
29
-
30
- - Native N-API binding with pure-JS fallback for browsers and constrained environments.
31
- - Conversion helpers from `bigint-conversion` in-core (no extra deps).
32
- - ESM and CJS exports plus a UMD/browser bundle.
33
- - Actively maintained; legacy `bigint-buffer` is deprecated and flagged by audits.
34
-
35
- ---
36
-
37
- ## Install
38
- ```bash
39
- npm install @gsknnft/bigint-buffer
40
- # or pnpm/yarn if preferred
41
- ```
42
-
43
- ---
44
-
45
- ## Quick Start
46
- ```ts
47
- import {
48
- toBigIntBE, toBigIntLE, toBufferBE, toBufferLE,
49
- bigintToBuf, bufToBigint, bigintToHex, hexToBigint,
50
- bigintToText, textToBigint, bigintToBase64, base64ToBigint,
51
- // New in 1.4.5
52
- toFixedPoint, fromFixedPoint, addFixedPoint, subtractFixedPoint,
53
- averageFixedPoint, compareFixedPoint, type FixedPoint,
54
- } from "@gsknnft/bigint-buffer";
55
-
56
- toBigIntBE(Buffer.from("deadbeef", "hex")); // 3735928559n
57
- toBufferLE(0xdeadbeefn, 6); // <Buffer ef be ad de 00 00>
58
- bigintToHex(123456789n); // "075bcd15"
59
- textToBigint("Hello"); // 0x48656c6c6f
60
- bigintToBase64(123456789n); // "B1vNFQ=="
61
-
62
- // FixedPoint usage
63
- const fp = toFixedPoint(123456789n, 18); // Convert bigint to FixedPoint
64
- const sum = addFixedPoint(fp, fp); // Add two FixedPoints
65
- const avg = averageFixedPoint([fp, fp]); // Average FixedPoints
66
- ```
67
-
68
- // Browser usage (with polyfills)
69
- import { toBigIntBE } from "@gsknnft/bigint-buffer";
70
- // Polyfill Buffer if needed:
71
- // import { Buffer } from "buffer";
72
-
73
- // CJS usage
74
- const { toBigIntBE } = require("@gsknnft/bigint-buffer");
75
- ```
76
-
77
- ### Performance
78
- - BE/LE conversions now stream bytes directly from buffers (64-bit chunks via Buffer.read/writeBigUInt64* when available) with no intermediate hex copies.
79
- - JS fallback matches the native binding semantics and is exercised against empty, tiny, and very large inputs in CI.
80
- - Native bindings still load automatically when present; the optimized fallback keeps browser and non-native runtimes fast.
81
- - Browser bundlers must polyfill Node built-ins: add a node polyfill plugin (e.g. `rollup-plugin-polyfill-node`) or explicit aliases for `buffer`, `path`, and `fs` so the fallback loader can resolve.
82
-
83
- ### Pushing Performance Further
84
- - For very large buffers, consider enabling the native binding (included in npm tarball) or adding SIMD/native glue in your host app if you need throughput beyond JS.
85
-
86
- ### Electron Packaging
87
- - Ensure the native binding ships with your app: add `node_modules/@gsknnft/bigint-buffer/build/Release/bigint_buffer.node` to `extraResources`/`asarUnpack` so it ends up under `resources/app.asar.unpacked/node_modules/@gsknnft/bigint-buffer/build/Release/`.
88
- - If your packager relocates files, set `BIGINT_BUFFER_NATIVE_PATH` to the directory containing `build/Release/bigint_buffer.node` before launching the app.
89
-
90
- The loader will check, in order:
91
- - `dist/build/Release/bigint_buffer.node`
92
- - `build/Release/bigint_buffer.node`
93
- - Electron asar-unpacked path
94
- - `BIGINT_BUFFER_NATIVE_PATH` (if set)
95
-
96
- ### Conversion Utilities
97
- ```ts
98
- import { conversionUtils } from "@gsknnft/bigint-buffer";
99
-
100
- const arrBuf = conversionUtils.bigintToBuf(123456789n, true); // ArrayBuffer
101
- const hex = conversionUtils.bigintToHex(123456789n, true); // '0x...' format
102
- const text = conversionUtils.bigintToText(123456789n);
103
- ```
104
-
105
- // All helpers are available as named exports:
106
- import { bigintToBuf, bufToBigint, bigintToHex, hexToBigint } from "@gsknnft/bigint-buffer";
107
- ```
108
-
109
- ---
110
-
111
- ## Runtime
112
- - Native binary: `build/Release/bigint_buffer.node` (included in npm package; loads automatically when available).
113
- - Fallback: pure JS bundle for browser and non-native installs (now improved in 1.4.5).
114
- - Check which path loaded:
115
- ```ts
116
- import { isNative } from "@gsknnft/bigint-buffer";
117
- console.log(isNative); // true when native binding is active
118
- ```
119
-
120
- ---
121
-
122
- ## Commands
123
- ```bash
124
- npm run build # bundle + declarations + type check
125
- npm test # vitest with coverage
126
- npm run test:node # mocha against built JS (after build/compile)
127
- npm run rebuild:native # rebuild the N-API binding
128
- ```
129
-
130
- ---
131
-
132
- ## API Surface (high level)
133
- - Core: `toBigIntBE/LE`, `toBufferBE/LE`, `validateBigIntBuffer`, `isNative`
134
- - Conversion: `bigintToBuf`, `bufToBigint`, `bigintToHex`, `hexToBigint`, `bigintToText`, `textToBigint`, `bigintToBase64`, `base64ToBigint`, `bufToHex`, `hexToBuf`, `textToBuf`, `bufToText`, `parseHex`
135
-
136
- All helpers are available as named exports in both ESM and CJS. TypeScript types are included.
137
-
138
- All helpers are endian-safe and validated across Node and browser builds.
139
-
140
- ---
141
-
142
- - Version: 1.4.7 (FixedPoint, native bindings out-of-the-box, improved JS fallback, robust loader, and stable exports)
143
- - Node: 20+ (tested through 24 LTS under CI)
144
- - Issues: https://github.com/gsknnft/bigint-buffer/issues
1
+ # @gsknnft/bigint-buffer
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/@gsknnft/bigint-buffer.svg?style=flat-square)](https://www.npmjs.com/package/@gsknnft/bigint-buffer)
4
+ [![Node Version](https://img.shields.io/node/v/@gsknnft/bigint-buffer.svg?style=flat-square)](https://nodejs.org)
5
+
6
+ BigInt <-> Buffer conversion with native bindings, fast JS fallback paths, and built-in conversion helpers.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install @gsknnft/bigint-buffer
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ```ts
17
+ import {
18
+ toBigIntBE,
19
+ toBigIntLE,
20
+ toBufferBE,
21
+ toBufferLE,
22
+ bigintToHex,
23
+ hexToBigint,
24
+ bigintToBase64,
25
+ base64ToBigint,
26
+ } from "@gsknnft/bigint-buffer";
27
+
28
+ const value = toBigIntBE(Buffer.from("deadbeef", "hex"));
29
+ const out = toBufferLE(value, 8);
30
+
31
+ const hex = bigintToHex(123456789n); // "075bcd15"
32
+ const roundTrip = hexToBigint(hex);
33
+
34
+ const b64 = bigintToBase64(123456789n);
35
+ const fromB64 = base64ToBigint(b64);
36
+ ```
37
+
38
+ ## FixedPoint Helpers
39
+
40
+ ```ts
41
+ import {
42
+ toFixedPoint,
43
+ fromFixedPoint,
44
+ addFixedPoint,
45
+ subtractFixedPoint,
46
+ averageFixedPoint,
47
+ compareFixedPoint,
48
+ } from "@gsknnft/bigint-buffer";
49
+
50
+ const a = toFixedPoint(12.34, 9);
51
+ const b = toFixedPoint(7.66, 9);
52
+ const sum = addFixedPoint(a, b);
53
+ const avg = averageFixedPoint([a, b]);
54
+ const cmp = compareFixedPoint(a, b);
55
+ const n = fromFixedPoint(sum, 9);
56
+ ```
57
+
58
+ ## Native Loading
59
+
60
+ The package tries native first, then falls back to JS:
61
+
62
+ 1. `node-gyp-build` prebuild lookup from installed package root
63
+ 2. `bindings` lookup with explicit `module_root` when resolvable
64
+ 3. pure JS fallback
65
+
66
+ Set `BIGINT_BUFFER_NATIVE_PATH` to override lookup root when needed.
67
+
68
+ Enable debug logging:
69
+
70
+ ```bash
71
+ BIGINT_BUFFER_DEBUG=1 node your-app.js
72
+ ```
73
+
74
+ Silence fallback warning:
75
+
76
+ ```bash
77
+ BIGINT_BUFFER_SILENT_NATIVE_FAIL=1 node your-app.js
78
+ ```
79
+
80
+ ## Electron Packaging
81
+
82
+ Include the native binary in unpacked resources:
83
+
84
+ - `node_modules/@gsknnft/bigint-buffer/build/Release/bigint_buffer.node`
85
+
86
+ Common location at runtime:
87
+
88
+ - `resources/app.asar.unpacked/node_modules/@gsknnft/bigint-buffer/build/Release/bigint_buffer.node`
89
+
90
+ If your packager relocates native files, set `BIGINT_BUFFER_NATIVE_PATH` to the directory containing `build/Release/bigint_buffer.node`.
91
+
92
+ ## Browser Notes
93
+
94
+ This package supports browser builds via fallback paths and conversion bundles.
95
+
96
+ If your bundler does not polyfill Node globals/modules automatically, add polyfills for:
97
+
98
+ - `Buffer`
99
+ - `path`
100
+ - `fs`
101
+
102
+ ## Exports
103
+
104
+ Top-level package exports:
105
+
106
+ - ESM: `dist/index.js`
107
+ - CJS: `dist/index.cjs`
108
+ - Types: `dist/index.d.ts`
109
+ - Native binary export: `./build/Release/bigint_buffer.node`
110
+ - Conversion subpath: `@gsknnft/bigint-buffer/conversion`
111
+
112
+ ## Commands
113
+
114
+ ```bash
115
+ npm run build # bundle + declarations + conversion sync + native sync
116
+ npm run compile # build + typecheck
117
+ npm test # vitest
118
+ npm run check # eslint
119
+ npm run rebuild # node-gyp rebuild + build + conversion build
120
+ npm run prebuilds # prebuildify for native binaries
121
+ ```
122
+
123
+ Internal build scripts (`scripts/*.ts`) are executed via `tsx` for stable ESM compatibility in local and CI environments.
124
+
125
+ ## Quality Snapshot
126
+
127
+ Latest local run (Node 24, Vitest v4, v8 coverage):
128
+
129
+ - Tests: `141 passed`
130
+ - Coverage: `Statements 84.15%`, `Branches 72.36%`, `Functions 88.37%`, `Lines 85.88%`
131
+ - Benchmark snapshot: see `benchmark.md`
132
+ - Publish dry-run: clean tarball (no test/bench JS artifacts)
133
+
134
+ Reproduce:
135
+
136
+ ```bash
137
+ pnpm exec vitest run --coverage --pool=forks
138
+ pnpm run benchmark
139
+ pnpm pack --dry-run
140
+ ```
141
+
142
+ ## Runtime Compatibility
143
+
144
+ - Node: `>=20`
145
+ - Package format: ESM + CJS
146
+ - Native addon: N-API (`bigint_buffer.node`)
147
+
148
+ ## License
149
+
150
+ Apache-2.0