b4n1-boost 0.3.11 → 0.3.12
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/bin/linux-arm64/libb4n1_boost.so +0 -0
- package/bin/linux-x64/libb4n1_boost.so +0 -0
- package/bin/macos-arm64/libb4n1_boost.dylib +0 -0
- package/bin/macos-x64/libb4n1_boost.dylib +0 -0
- package/bin/windows-x64/b4n1_boost.dll +0 -0
- package/core.js +137 -0
- package/core.test.js +93 -0
- package/index.js +66 -80
- package/lib-loader.js +95 -0
- package/package.json +17 -5
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/core.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// B4N1-Boost shared core for Node.js — Business Source License 1.1 (BSL-1.1).
|
|
2
|
+
// Thin FFI wrapper. ALL logic (LRU eviction, TTL, bloom hashing, circuit
|
|
3
|
+
// thresholds) lives in the native core and is shared with Python, .NET and
|
|
4
|
+
// Java through the same C-ABI — a fix in the core fixes every SDK at once.
|
|
5
|
+
const loader = require('./lib-loader');
|
|
6
|
+
|
|
7
|
+
function native(name) {
|
|
8
|
+
if (!loader.available) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
'b4n1-boost core requires the native library. ' +
|
|
11
|
+
`Reason: ${loader.loadError || 'unknown'}. ` +
|
|
12
|
+
'Compression/JSON have a pure-JS fallback; cache/bloom/circuit do not.'
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
return loader.fn[name];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Copies `len` bytes from a native pointer before the core frees them. */
|
|
19
|
+
function readNative(ptr, len) {
|
|
20
|
+
// koffi hands back pointers as BigInt on 64-bit; view() needs a Number.
|
|
21
|
+
// koffi 3.x returns an ArrayBuffer.
|
|
22
|
+
return Buffer.from(new Uint8Array(loader.koffi.view(Number(ptr), len)));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Response cache (LRU + TTL) backed by the native core.
|
|
27
|
+
* @example
|
|
28
|
+
* const cache = new ResponseCache(1024);
|
|
29
|
+
* cache.set('/api/users', body, 200, 60); // ttlSecs 0 = never expires
|
|
30
|
+
* const hit = cache.get('/api/users'); // null on a miss
|
|
31
|
+
*/
|
|
32
|
+
class ResponseCache {
|
|
33
|
+
constructor(capacity = 1024) {
|
|
34
|
+
this._new = native('cache_new');
|
|
35
|
+
this._get = native('cache_get');
|
|
36
|
+
this._set = native('cache_set');
|
|
37
|
+
this._invalidate = native('cache_invalidate');
|
|
38
|
+
this._clear = native('cache_clear');
|
|
39
|
+
this._free = native('cache_free');
|
|
40
|
+
this._freeBuf = native('free_buffer');
|
|
41
|
+
this._handle = this._new(capacity);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @returns {{body: Buffer, status: number}|null} */
|
|
45
|
+
get(key) {
|
|
46
|
+
// koffi needs an out-param *array* to write a pointer-to-pointer.
|
|
47
|
+
const outBody = [null];
|
|
48
|
+
const outLen = [0];
|
|
49
|
+
const outStatus = [0];
|
|
50
|
+
const hit = this._get(this._handle, key, outBody, outLen, outStatus);
|
|
51
|
+
if (!hit) return null;
|
|
52
|
+
const ptr = outBody[0];
|
|
53
|
+
const len = outLen[0];
|
|
54
|
+
// Copy before releasing: the core owns that buffer.
|
|
55
|
+
const body = readNative(ptr, len);
|
|
56
|
+
this._freeBuf(Number(ptr), len);
|
|
57
|
+
return { body, status: outStatus[0] };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
set(key, body, status = 200, ttlSecs = 300) {
|
|
61
|
+
const buf = Buffer.isBuffer(body) ? body : Buffer.from(String(body));
|
|
62
|
+
this._set(this._handle, key, buf, buf.length, status, ttlSecs);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
invalidate(key) {
|
|
66
|
+
return !!this._invalidate(this._handle, key);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
clear() {
|
|
70
|
+
this._clear(this._handle);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
close() {
|
|
74
|
+
if (this._handle) {
|
|
75
|
+
this._free(this._handle);
|
|
76
|
+
this._handle = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Bloom filter for rate limiting / dedup. checkAndSet is true on duplicate. */
|
|
82
|
+
class BloomFilter {
|
|
83
|
+
constructor(expectedItems = 10000, fpRate = 0.01) {
|
|
84
|
+
this._new = native('bloom_new');
|
|
85
|
+
this._check = native('bloom_check_and_set');
|
|
86
|
+
this._free = native('bloom_free');
|
|
87
|
+
// The core reads the double from its u64 bit pattern.
|
|
88
|
+
const bits = Buffer.alloc(8);
|
|
89
|
+
bits.writeDoubleLE(fpRate, 0);
|
|
90
|
+
this._handle = this._new(expectedItems, bits.readBigUInt64LE(0));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
checkAndSet(item) {
|
|
94
|
+
return !!this._check(this._handle, item);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
close() {
|
|
98
|
+
if (this._handle) {
|
|
99
|
+
this._free(this._handle);
|
|
100
|
+
this._handle = 0;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Circuit breaker: allows() is false while the circuit is open. */
|
|
106
|
+
class CircuitBreaker {
|
|
107
|
+
constructor(threshold = 5, successThreshold = 1, timeoutSecs = 30) {
|
|
108
|
+
this._new = native('circuit_new');
|
|
109
|
+
this._allows = native('circuit_allows');
|
|
110
|
+
this._ok = native('circuit_record_success');
|
|
111
|
+
this._fail = native('circuit_record_failure');
|
|
112
|
+
this._free = native('circuit_free');
|
|
113
|
+
this._handle = this._new(threshold, successThreshold, timeoutSecs);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** @returns {boolean} false means "do not call the upstream". */
|
|
117
|
+
allows() {
|
|
118
|
+
return !!this._allows(this._handle);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
recordSuccess() {
|
|
122
|
+
this._ok(this._handle);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
recordFailure() {
|
|
126
|
+
this._fail(this._handle);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
close() {
|
|
130
|
+
if (this._handle) {
|
|
131
|
+
this._free(this._handle);
|
|
132
|
+
this._handle = 0;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = { ResponseCache, BloomFilter, CircuitBreaker };
|
package/core.test.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Contract tests for the shared core through the Node SDK.
|
|
2
|
+
// Same Rust primitives as Python/.NET/Java (core_abi) — if these pass here,
|
|
3
|
+
// the core is reachable from every language; only the shim differs.
|
|
4
|
+
// Requires the native library: set B4N1_BOOST_LIB_PATH (or ship bin/<plat>).
|
|
5
|
+
const assert = require('assert');
|
|
6
|
+
const loader = require('./lib-loader');
|
|
7
|
+
|
|
8
|
+
if (!loader.available) {
|
|
9
|
+
console.error('[SKIP] native library unavailable:', loader.loadError);
|
|
10
|
+
console.error(' set B4N1_BOOST_LIB_PATH to libb4n1_boost.so/.dylib/.dll');
|
|
11
|
+
process.exit(2); // 2 = skip, distinct from failure
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const { ResponseCache, BloomFilter, CircuitBreaker } = require('./core.js');
|
|
15
|
+
let passed = 0;
|
|
16
|
+
function test(name, fn) {
|
|
17
|
+
try {
|
|
18
|
+
fn();
|
|
19
|
+
console.log(` ✓ ${name}`);
|
|
20
|
+
passed++;
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error(` ✗ ${name}: ${e.message}`);
|
|
23
|
+
process.exitCode = 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
test('cache: miss, set, hit, invalidate', () => {
|
|
28
|
+
const c = new ResponseCache(8);
|
|
29
|
+
assert.strictEqual(c.get('a'), null, 'empty cache must miss');
|
|
30
|
+
c.set('a', Buffer.from('{"v":1}'), 200, 60);
|
|
31
|
+
const hit = c.get('a');
|
|
32
|
+
assert.strictEqual(hit.body.toString(), '{"v":1}');
|
|
33
|
+
assert.strictEqual(hit.status, 200);
|
|
34
|
+
assert.strictEqual(c.invalidate('a'), true);
|
|
35
|
+
assert.strictEqual(c.get('a'), null, 'invalidated key must miss again');
|
|
36
|
+
c.close();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('cache: ttl 0 never expires', () => {
|
|
40
|
+
const c = new ResponseCache(4);
|
|
41
|
+
c.set('k', Buffer.from('x'), 204, 0);
|
|
42
|
+
assert.ok(c.get('k'), 'ttl 0 must mean never expires');
|
|
43
|
+
c.close();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('bloom: detects duplicates', () => {
|
|
47
|
+
const b = new BloomFilter(1000, 0.01);
|
|
48
|
+
assert.strictEqual(b.checkAndSet('u1'), false, 'first sight is new');
|
|
49
|
+
assert.strictEqual(b.checkAndSet('u1'), true, 'second sight is a duplicate');
|
|
50
|
+
b.close();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('circuit: opens and recovers', () => {
|
|
54
|
+
const cb = new CircuitBreaker(2, 1, 3600);
|
|
55
|
+
assert.strictEqual(cb.allows(), true);
|
|
56
|
+
cb.recordFailure();
|
|
57
|
+
assert.strictEqual(cb.allows(), true, 'one failure stays closed');
|
|
58
|
+
cb.recordFailure();
|
|
59
|
+
assert.strictEqual(cb.allows(), false, 'threshold reached -> open');
|
|
60
|
+
cb.close();
|
|
61
|
+
|
|
62
|
+
const cb2 = new CircuitBreaker(2, 1, 0);
|
|
63
|
+
cb2.recordFailure();
|
|
64
|
+
cb2.recordFailure();
|
|
65
|
+
assert.strictEqual(cb2.allows(), true, 'expired open admits a probe');
|
|
66
|
+
cb2.recordSuccess();
|
|
67
|
+
assert.strictEqual(cb2.allows(), true, 'success closes it');
|
|
68
|
+
cb2.close();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// zstd has NO pure-JS fallback: a passing round-trip proves the native Rust
|
|
72
|
+
// core is doing the work, not a shim. That distinction is the whole point —
|
|
73
|
+
// a stale or unexported binary still "works" while silently using JS.
|
|
74
|
+
test('native core is really bound (not a JS fallback)', () => {
|
|
75
|
+
const B = require('./index.js');
|
|
76
|
+
assert.ok(B.isAvailable(), 'isAvailable() must be true: the binary is not bound');
|
|
77
|
+
assert.ok(B.libraryPath().length > 0, 'libraryPath() must report the loaded binary');
|
|
78
|
+
|
|
79
|
+
const data = 'x'.repeat(4000);
|
|
80
|
+
const packed = B.compressZstd(Buffer.from(data));
|
|
81
|
+
assert.ok(packed.length < 100, `zstd must actually compress, got ${packed.length}`);
|
|
82
|
+
const back = B.decompressZstd(packed);
|
|
83
|
+
assert.strictEqual(back.toString(), data, 'zstd round-trip must be lossless');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('gzip and json go through the native core', () => {
|
|
87
|
+
const B = require('./index.js');
|
|
88
|
+
assert.ok(B.compressGzip(Buffer.from('x'.repeat(4000))).length < 100);
|
|
89
|
+
assert.strictEqual(B.isJsonValid('{"a":1}'), true);
|
|
90
|
+
assert.strictEqual(B.isJsonValid('{a}'), false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log(`[CORE] ${passed}/6 passed`);
|
package/index.js
CHANGED
|
@@ -2,111 +2,97 @@
|
|
|
2
2
|
// Thin FFI wrapper over the proprietary B4N1 native core.
|
|
3
3
|
// The native binary is licensed separately under the B4N1 EULA (EULA.txt).
|
|
4
4
|
// Copyright (c) 2026 B4N1. All rights reserved.
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
//
|
|
6
|
+
// FFI via koffi, NOT ffi-napi: ffi-napi needs node-gyp and fails to build on
|
|
7
|
+
// any machine without a toolchain, so `require('ffi-napi')` threw and the
|
|
8
|
+
// whole SDK silently degraded to a pure-JS fallback — isAvailable() was false
|
|
9
|
+
// in production even though the package "worked". koffi ships prebuilt
|
|
10
|
+
// binaries per platform, so `npm install` never compiles anything.
|
|
11
|
+
const loader = require('./lib-loader');
|
|
7
12
|
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
throw new Error("Invalid library filename in B4N1_BOOST_LIB_PATH");
|
|
15
|
-
}
|
|
16
|
-
return resolved;
|
|
13
|
+
/** Native fn or a loud error. No silent fallback on a missing binding. */
|
|
14
|
+
function native(name) {
|
|
15
|
+
if (!loader.available) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
'b4n1-boost native core unavailable: ' + (loader.loadError || 'unknown') +
|
|
18
|
+
' (looked for ' + loader.libPath + ')');
|
|
17
19
|
}
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
21
|
-
}
|
|
22
|
-
function resolveBundledPath() {
|
|
23
|
-
const plat = process.platform === 'win32' ? 'windows-x64'
|
|
24
|
-
: process.platform === 'darwin' ? (process.arch === 'arm64' ? 'macos-arm64' : 'macos-x64')
|
|
25
|
-
: (process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64');
|
|
26
|
-
const lib = process.platform === 'win32' ? 'b4n1_boost.dll'
|
|
27
|
-
: process.platform === 'darwin' ? 'libb4n1_boost.dylib' : 'libb4n1_boost.so';
|
|
28
|
-
const p = path.join(__dirname, 'bin', plat, lib);
|
|
29
|
-
return fs.existsSync(p) ? p : null;
|
|
20
|
+
const fn = loader.fn[name];
|
|
21
|
+
if (!fn) throw new Error('b4n1-boost: missing native symbol ' + name);
|
|
22
|
+
return fn;
|
|
30
23
|
}
|
|
31
|
-
const libPath = resolveLibPath();
|
|
32
|
-
|
|
33
24
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
uint8Ptr = ref.refType(ref.types.uint8);
|
|
45
|
-
uint8PtrPtr = ref.refType(uint8Ptr);
|
|
46
|
-
usizePtr = ref.refType(ref.types.size_t);
|
|
47
|
-
|
|
48
|
-
if (fs.existsSync(libPath)) {
|
|
49
|
-
lib = ffi.Library(libPath, {
|
|
50
|
-
b4n1_boost_compress_gzip: ['int', [uint8Ptr, 'size_t', uint8PtrPtr, usizePtr, 'uint32']],
|
|
51
|
-
b4n1_boost_compress_zstd: ['int', [uint8Ptr, 'size_t', uint8PtrPtr, usizePtr, 'int32']],
|
|
52
|
-
b4n1_boost_decompress_zstd: ['int', [uint8Ptr, 'size_t', uint8PtrPtr, usizePtr]],
|
|
53
|
-
b4n1_boost_free_buffer: ['void', [uint8Ptr, 'size_t']],
|
|
54
|
-
b4n1_boost_minify_html: ['string', ['string']],
|
|
55
|
-
b4n1_boost_json_validate: ['int', ['string']],
|
|
56
|
-
b4n1_boost_free_string: ['void', ['string']]
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
} catch (e) {
|
|
60
|
-
// ffi-napi optional dependency
|
|
25
|
+
/**
|
|
26
|
+
* Copies a native buffer out, then releases it with the core's own free.
|
|
27
|
+
* The core owns the allocation; leaking it would be a real memory leak.
|
|
28
|
+
*/
|
|
29
|
+
function takeNative(outPtrBuf, outLenBuf) {
|
|
30
|
+
const ptr = outPtrBuf[0];
|
|
31
|
+
const len = outLenBuf[0];
|
|
32
|
+
const copy = Buffer.from(new Uint8Array(loader.koffi.view(Number(ptr), len)));
|
|
33
|
+
native('free_buffer')(Number(ptr), len);
|
|
34
|
+
return copy;
|
|
61
35
|
}
|
|
62
36
|
|
|
63
37
|
class B4N1BoostNode {
|
|
38
|
+
/** True when the native core is genuinely bound (not a fallback). */
|
|
64
39
|
static isAvailable() {
|
|
65
|
-
return
|
|
40
|
+
return loader.available;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Absolute path of the binary actually loaded — for diagnostics. */
|
|
44
|
+
static libraryPath() {
|
|
45
|
+
return loader.libPath;
|
|
66
46
|
}
|
|
67
47
|
|
|
68
48
|
static compressGzip(buffer, level = 4) {
|
|
69
|
-
if (!
|
|
49
|
+
if (!loader.available) {
|
|
70
50
|
const zlib = require('zlib');
|
|
71
51
|
return zlib.gzipSync(buffer, { level });
|
|
72
52
|
}
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
const res =
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const outLen = outLenBuf.deref();
|
|
80
|
-
const resultBuf = Buffer.from(ref.reinterpret(outPtr, outLen, 0));
|
|
81
|
-
lib.b4n1_boost_free_buffer(outPtr, outLen);
|
|
82
|
-
return resultBuf;
|
|
53
|
+
const outPtr = [null];
|
|
54
|
+
const outLen = [0];
|
|
55
|
+
const res = native('compress_gzip')(
|
|
56
|
+
buffer, buffer.length, outPtr, outLen, level);
|
|
57
|
+
if (res !== 0) throw new Error('Gzip compression failed');
|
|
58
|
+
return takeNative(outPtr, outLen);
|
|
83
59
|
}
|
|
84
60
|
|
|
85
61
|
static compressZstd(buffer, level = 3) {
|
|
86
|
-
if (!
|
|
87
|
-
throw new Error(
|
|
62
|
+
if (!loader.available) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
'b4n1-boost native library not available (no pure-JS zstd fallback)');
|
|
88
65
|
}
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
const res =
|
|
92
|
-
|
|
66
|
+
const outPtr = [null];
|
|
67
|
+
const outLen = [0];
|
|
68
|
+
const res = native('compress_zstd')(
|
|
69
|
+
buffer, buffer.length, outPtr, outLen, level);
|
|
70
|
+
if (res !== 0) throw new Error('Zstd compression failed');
|
|
71
|
+
return takeNative(outPtr, outLen);
|
|
72
|
+
}
|
|
93
73
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
74
|
+
static decompressZstd(buffer) {
|
|
75
|
+
if (!loader.available) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
'b4n1-boost native library not available (no pure-JS zstd fallback)');
|
|
78
|
+
}
|
|
79
|
+
const outPtr = [null];
|
|
80
|
+
const outLen = [0];
|
|
81
|
+
const res = native('decompress_zstd')(
|
|
82
|
+
buffer, buffer.length, outPtr, outLen);
|
|
83
|
+
if (res !== 0) throw new Error('Zstd decompression failed');
|
|
84
|
+
return takeNative(outPtr, outLen);
|
|
99
85
|
}
|
|
100
86
|
|
|
101
87
|
static minifyHtml(htmlString) {
|
|
102
|
-
if (!
|
|
88
|
+
if (!loader.available) {
|
|
103
89
|
return htmlString.replace(/\s+/g, ' ').trim();
|
|
104
90
|
}
|
|
105
|
-
return
|
|
91
|
+
return native('minify_html')(htmlString);
|
|
106
92
|
}
|
|
107
93
|
|
|
108
94
|
static isJsonValid(jsonString) {
|
|
109
|
-
if (!
|
|
95
|
+
if (!loader.available) {
|
|
110
96
|
try {
|
|
111
97
|
JSON.parse(jsonString);
|
|
112
98
|
return true;
|
|
@@ -114,8 +100,8 @@ class B4N1BoostNode {
|
|
|
114
100
|
return false;
|
|
115
101
|
}
|
|
116
102
|
}
|
|
117
|
-
return
|
|
103
|
+
return native('json_validate')(jsonString) === 1;
|
|
118
104
|
}
|
|
119
105
|
}
|
|
120
106
|
|
|
121
|
-
module.exports = B4N1BoostNode;
|
|
107
|
+
module.exports = B4N1BoostNode;
|
package/lib-loader.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// B4N1-Boost shared native library loader (Node.js).
|
|
2
|
+
// Uses koffi: prebuilt binaries per platform, so `npm install` never needs a
|
|
3
|
+
// compiler (ffi-napi requires node-gyp and fails on most machines/CI).
|
|
4
|
+
// The native binary is licensed separately under the EULA (EULA.txt).
|
|
5
|
+
//
|
|
6
|
+
// koffi 3.x declares functions with a single signature string:
|
|
7
|
+
// lib.func('uint64_t b4n1_cache_new(int32_t)')
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
|
|
11
|
+
function bundledPath() {
|
|
12
|
+
const plat = process.platform === 'win32' ? 'windows-x64'
|
|
13
|
+
: process.platform === 'darwin' ? (process.arch === 'arm64' ? 'macos-arm64' : 'macos-x64')
|
|
14
|
+
: (process.arch === 'arm64' ? 'linux-arm64' : 'linux-x64');
|
|
15
|
+
const name = process.platform === 'win32' ? 'b4n1_boost.dll'
|
|
16
|
+
: process.platform === 'darwin' ? 'libb4n1_boost.dylib' : 'libb4n1_boost.so';
|
|
17
|
+
const p = path.join(__dirname, 'bin', plat, name);
|
|
18
|
+
return fs.existsSync(p) ? p : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function resolveLibPath() {
|
|
22
|
+
const envPath = process.env.B4N1_BOOST_LIB_PATH;
|
|
23
|
+
if (envPath) {
|
|
24
|
+
const resolved = path.resolve(envPath);
|
|
25
|
+
const ok = ['libb4n1_boost.so', 'libb4n1_boost.dylib', 'b4n1_boost.dll'];
|
|
26
|
+
if (!ok.some(n => resolved.endsWith(n))) {
|
|
27
|
+
// Fail loud: silently disabling the native core on a typo is worse.
|
|
28
|
+
throw new Error(`Invalid library filename in B4N1_BOOST_LIB_PATH: ${resolved}`);
|
|
29
|
+
}
|
|
30
|
+
return resolved;
|
|
31
|
+
}
|
|
32
|
+
return bundledPath() || path.join(__dirname, '../../target/release/libb4n1_boost.so');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const libPath = resolveLibPath();
|
|
36
|
+
|
|
37
|
+
// Firmas copiadas de src/c_abi/mod.rs y src/core_abi.rs. No estimarlas:
|
|
38
|
+
// un parametro de mas o de menos hace que koffi escriba en una direccion
|
|
39
|
+
// equivocada en silencio.
|
|
40
|
+
const SIGNATURES = {
|
|
41
|
+
compress_gzip: 'int b4n1_boost_compress_gzip(void*, size_t, _Out_ void**, _Out_ size_t*, uint32_t)',
|
|
42
|
+
compress_zstd: 'int b4n1_boost_compress_zstd(void*, size_t, _Out_ void**, _Out_ size_t*, int32_t)',
|
|
43
|
+
decompress_zstd: 'int b4n1_boost_decompress_zstd(void*, size_t, _Out_ void**, _Out_ size_t*)',
|
|
44
|
+
free_buffer: 'void b4n1_boost_free_buffer(void*, size_t)',
|
|
45
|
+
free_string: 'void b4n1_boost_free_string(char*)',
|
|
46
|
+
minify_html: 'char* b4n1_boost_minify_html(char*)',
|
|
47
|
+
json_validate: 'int b4n1_boost_json_validate(char*)',
|
|
48
|
+
|
|
49
|
+
// Shared core — identical ABI for every language SDK.
|
|
50
|
+
cache_new: 'uint64_t b4n1_cache_new(int32_t)',
|
|
51
|
+
cache_get: 'int b4n1_cache_get(uint64_t, char*, _Out_ void**, _Out_ size_t*, _Out_ uint16_t*)',
|
|
52
|
+
cache_set: 'int b4n1_cache_set(uint64_t, char*, void*, size_t, uint16_t, uint64_t)',
|
|
53
|
+
cache_invalidate: 'int b4n1_cache_invalidate(uint64_t, char*)',
|
|
54
|
+
cache_clear: 'void b4n1_cache_clear(uint64_t)',
|
|
55
|
+
cache_free: 'void b4n1_cache_free(uint64_t)',
|
|
56
|
+
bloom_new: 'uint64_t b4n1_bloom_new(uint64_t, uint64_t)',
|
|
57
|
+
bloom_check_and_set: 'int b4n1_bloom_check_and_set(uint64_t, char*)',
|
|
58
|
+
bloom_free: 'void b4n1_bloom_free(uint64_t)',
|
|
59
|
+
circuit_new: 'uint64_t b4n1_circuit_new(int32_t, int32_t, uint64_t)',
|
|
60
|
+
circuit_allows: 'int b4n1_circuit_allows(uint64_t)',
|
|
61
|
+
circuit_record_success: 'void b4n1_circuit_record_success(uint64_t)',
|
|
62
|
+
circuit_record_failure: 'void b4n1_circuit_record_failure(uint64_t)',
|
|
63
|
+
circuit_free: 'void b4n1_circuit_free(uint64_t)',
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
let handle = null;
|
|
67
|
+
let koffi = null;
|
|
68
|
+
let loadError = null;
|
|
69
|
+
const fn = {};
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
koffi = require('koffi');
|
|
73
|
+
handle = koffi.load(libPath);
|
|
74
|
+
// Collect EVERY missing symbol instead of aborting at the first one: a
|
|
75
|
+
// stale binary (shipped before the core ABI existed) otherwise reports
|
|
76
|
+
// only "cannot find b4n1_cache_new" and hides the real problem.
|
|
77
|
+
const missing = [];
|
|
78
|
+
for (const [name, sig] of Object.entries(SIGNATURES)) {
|
|
79
|
+
try {
|
|
80
|
+
fn[name] = handle.func(sig);
|
|
81
|
+
} catch {
|
|
82
|
+
missing.push(name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (missing.length) {
|
|
86
|
+
loadError = `binary at ${libPath} is missing ${missing.length} symbol(s): `
|
|
87
|
+
+ missing.join(', ') + ' — it does not match this SDK version';
|
|
88
|
+
handle = null;
|
|
89
|
+
}
|
|
90
|
+
} catch (e) {
|
|
91
|
+
handle = null;
|
|
92
|
+
loadError = e.message;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { libPath, handle, fn, available: handle !== null, loadError, koffi };
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "b4n1-boost",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.12",
|
|
4
4
|
"description": "Native accelerator for Node.js & Express: SIMD compression, fast JSON, background tasks without Redis",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test.js"
|
|
7
|
+
"test": "node test.js && node core.test.js",
|
|
8
|
+
"test:core": "node core.test.js"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [
|
|
10
11
|
"gzip",
|
|
@@ -18,7 +19,7 @@
|
|
|
18
19
|
"native"
|
|
19
20
|
],
|
|
20
21
|
"author": "B4N1 Team <dev@b4n1.com>",
|
|
21
|
-
"license": "
|
|
22
|
+
"license": "BUSL-1.1",
|
|
22
23
|
"files": [
|
|
23
24
|
"index.js",
|
|
24
25
|
"express.js",
|
|
@@ -26,6 +27,17 @@
|
|
|
26
27
|
"LICENSE",
|
|
27
28
|
"EULA.txt",
|
|
28
29
|
"README.md",
|
|
29
|
-
"background.js"
|
|
30
|
-
|
|
30
|
+
"background.js",
|
|
31
|
+
"core.js",
|
|
32
|
+
"lib-loader.js",
|
|
33
|
+
"core.test.js"
|
|
34
|
+
],
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"ffi-napi": {
|
|
37
|
+
"optional": true
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"optionalDependencies": {
|
|
41
|
+
"koffi": "^3.3.0"
|
|
42
|
+
}
|
|
31
43
|
}
|