@aztec/bb.js 3.0.0-nightly.20251124 → 3.0.0-nightly.20251125
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/build/amd64-linux/bb +0 -0
- package/build/amd64-linux/nodejs_module.node +0 -0
- package/build/amd64-macos/bb +0 -0
- package/build/amd64-macos/nodejs_module.node +0 -0
- package/build/arm64-linux/bb +0 -0
- package/build/arm64-linux/nodejs_module.node +0 -0
- package/build/arm64-macos/bb +0 -0
- package/build/arm64-macos/nodejs_module.node +0 -0
- package/dest/browser/barretenberg/index.js +7 -6
- package/dest/browser/barretenberg_wasm/fetch_code/browser/barretenberg-threads.js +1 -1
- package/dest/browser/barretenberg_wasm/fetch_code/browser/barretenberg.js +1 -1
- package/dest/browser/bb_backends/index.d.ts +1 -1
- package/dest/browser/bb_backends/index.d.ts.map +1 -1
- package/dest/browser/bb_backends/index.js +2 -2
- package/dest/browser/bin/index.d.ts +3 -0
- package/dest/browser/bin/index.d.ts.map +1 -0
- package/dest/browser/bin/index.js +11 -0
- package/dest/node/barretenberg/index.js +7 -6
- package/dest/node/barretenberg/poseidon.bench.test.js +55 -13
- package/dest/node/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
- package/dest/node/bb_backends/index.d.ts +1 -1
- package/dest/node/bb_backends/index.d.ts.map +1 -1
- package/dest/node/bb_backends/index.js +2 -2
- package/dest/node/bb_backends/node/index.d.ts.map +1 -1
- package/dest/node/bb_backends/node/index.js +6 -8
- package/dest/node/bb_backends/node/native_shm.d.ts +1 -2
- package/dest/node/bb_backends/node/native_shm.d.ts.map +1 -1
- package/dest/node/bb_backends/node/native_shm.js +9 -20
- package/dest/node/bb_backends/node/native_shm_async.d.ts +51 -0
- package/dest/node/bb_backends/node/native_shm_async.d.ts.map +1 -0
- package/dest/node/bb_backends/node/native_shm_async.js +234 -0
- package/dest/node/bin/index.d.ts +3 -0
- package/dest/node/bin/index.d.ts.map +1 -0
- package/dest/node/bin/index.js +11 -0
- package/dest/node-cjs/barretenberg/index.js +7 -6
- package/dest/node-cjs/barretenberg/poseidon.bench.test.js +55 -13
- package/dest/node-cjs/barretenberg_wasm/barretenberg-threads.wasm.gz +0 -0
- package/dest/node-cjs/bb_backends/index.d.ts +1 -1
- package/dest/node-cjs/bb_backends/index.d.ts.map +1 -1
- package/dest/node-cjs/bb_backends/index.js +2 -2
- package/dest/node-cjs/bb_backends/node/index.d.ts.map +1 -1
- package/dest/node-cjs/bb_backends/node/index.js +6 -8
- package/dest/node-cjs/bb_backends/node/native_shm.d.ts +1 -2
- package/dest/node-cjs/bb_backends/node/native_shm.d.ts.map +1 -1
- package/dest/node-cjs/bb_backends/node/native_shm.js +9 -20
- package/dest/node-cjs/bb_backends/node/native_shm_async.d.ts +51 -0
- package/dest/node-cjs/bb_backends/node/native_shm_async.d.ts.map +1 -0
- package/dest/node-cjs/bb_backends/node/native_shm_async.js +238 -0
- package/dest/node-cjs/bin/index.d.ts +3 -0
- package/dest/node-cjs/bin/index.d.ts.map +1 -0
- package/dest/node-cjs/bin/index.js +13 -0
- package/package.json +5 -3
- package/src/barretenberg/index.ts +5 -5
- package/src/barretenberg/poseidon.bench.test.ts +62 -12
- package/src/bb_backends/index.ts +1 -1
- package/src/bb_backends/node/index.ts +5 -17
- package/src/bb_backends/node/native_shm.ts +8 -21
- package/src/bb_backends/node/native_shm_async.ts +266 -0
- package/src/bin/index.ts +14 -0
- package/src/cbind/generated/api_types.ts +4271 -0
- package/src/cbind/generated/async.ts +717 -0
- package/src/cbind/generated/curve_constants.ts +53 -0
- package/src/cbind/generated/sync.ts +663 -0
- package/dest/browser/bb_backends/sync_to_async_adapter.d.ts +0 -15
- package/dest/browser/bb_backends/sync_to_async_adapter.d.ts.map +0 -1
- package/dest/browser/bb_backends/sync_to_async_adapter.js +0 -20
- package/dest/node/bb_backends/sync_to_async_adapter.d.ts +0 -15
- package/dest/node/bb_backends/sync_to_async_adapter.d.ts.map +0 -1
- package/dest/node/bb_backends/sync_to_async_adapter.js +0 -20
- package/dest/node-cjs/bb_backends/sync_to_async_adapter.d.ts +0 -15
- package/dest/node-cjs/bb_backends/sync_to_async_adapter.d.ts.map +0 -1
- package/dest/node-cjs/bb_backends/sync_to_async_adapter.js +0 -24
- package/src/bb_backends/sync_to_async_adapter.ts +0 -21
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BarretenbergNativeSocketAsyncBackend } from './native_socket.js';
|
|
2
2
|
import { BarretenbergWasmSyncBackend, BarretenbergWasmAsyncBackend } from '../wasm.js';
|
|
3
3
|
import { BarretenbergNativeShmSyncBackend } from './native_shm.js';
|
|
4
|
-
import {
|
|
4
|
+
import { BarretenbergNativeShmAsyncBackend } from './native_shm_async.js';
|
|
5
5
|
import { findBbBinary, findNapiBinary } from './platform.js';
|
|
6
6
|
import { Barretenberg, BarretenbergSync } from '../../barretenberg/index.js';
|
|
7
7
|
import { BackendOptions, BackendType } from '../index.js';
|
|
@@ -37,17 +37,10 @@ export async function createAsyncBackend(
|
|
|
37
37
|
}
|
|
38
38
|
const napiPath = findNapiBinary();
|
|
39
39
|
if (!napiPath) {
|
|
40
|
-
throw new Error('Native
|
|
40
|
+
throw new Error('Native async backend requires napi client stub.');
|
|
41
41
|
}
|
|
42
|
-
logger(`Using native shared memory backend
|
|
43
|
-
|
|
44
|
-
const syncBackend = await BarretenbergNativeShmSyncBackend.new(
|
|
45
|
-
bbPath,
|
|
46
|
-
options.threads,
|
|
47
|
-
options.maxClients,
|
|
48
|
-
options.logger,
|
|
49
|
-
);
|
|
50
|
-
const asyncBackend = new SyncToAsyncAdapter(syncBackend);
|
|
42
|
+
logger(`Using native shared memory async backend: ${bbPath}`);
|
|
43
|
+
const asyncBackend = await BarretenbergNativeShmAsyncBackend.new(bbPath, options.threads, options.logger);
|
|
51
44
|
return new Barretenberg(asyncBackend, options);
|
|
52
45
|
}
|
|
53
46
|
|
|
@@ -94,12 +87,7 @@ export async function createSyncBackend(
|
|
|
94
87
|
throw new Error('Native sync backend requires napi client stub.');
|
|
95
88
|
}
|
|
96
89
|
logger(`Using native shared memory backend: ${bbPath}`);
|
|
97
|
-
const shm = await BarretenbergNativeShmSyncBackend.new(
|
|
98
|
-
bbPath,
|
|
99
|
-
options.threads,
|
|
100
|
-
options.maxClients,
|
|
101
|
-
options.logger,
|
|
102
|
-
);
|
|
90
|
+
const shm = await BarretenbergNativeShmSyncBackend.new(bbPath, options.threads, options.logger);
|
|
103
91
|
return new BarretenbergSync(shm);
|
|
104
92
|
}
|
|
105
93
|
|
|
@@ -3,7 +3,6 @@ import { spawn, ChildProcess } from 'child_process';
|
|
|
3
3
|
import { openSync, closeSync } from 'fs';
|
|
4
4
|
import { IMsgpackBackendSync } from '../interface.js';
|
|
5
5
|
import { findNapiBinary, findPackageRoot } from './platform.js';
|
|
6
|
-
import readline from 'readline';
|
|
7
6
|
|
|
8
7
|
// Import the NAPI module
|
|
9
8
|
// The addon is built to the nodejs_module directory
|
|
@@ -20,6 +19,8 @@ try {
|
|
|
20
19
|
addon = null;
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
let instanceCounter = 0;
|
|
23
|
+
|
|
23
24
|
/**
|
|
24
25
|
* Synchronous shared memory backend that communicates with bb binary via shared memory.
|
|
25
26
|
* Uses NAPI module to interface with shared memory IPC.
|
|
@@ -46,12 +47,10 @@ export class BarretenbergNativeShmSyncBackend implements IMsgpackBackendSync {
|
|
|
46
47
|
* Create and initialize a shared memory backend.
|
|
47
48
|
* @param bbBinaryPath Path to bb binary
|
|
48
49
|
* @param threads Optional number of threads
|
|
49
|
-
* @param maxClients Optional maximum concurrent clients (default: 1)
|
|
50
50
|
*/
|
|
51
51
|
static async new(
|
|
52
52
|
bbBinaryPath: string,
|
|
53
53
|
threads?: number,
|
|
54
|
-
maxClients?: number,
|
|
55
54
|
logger?: (msg: string) => void,
|
|
56
55
|
): Promise<BarretenbergNativeShmSyncBackend> {
|
|
57
56
|
if (!addon || !addon.MsgpackClient) {
|
|
@@ -59,14 +58,11 @@ export class BarretenbergNativeShmSyncBackend implements IMsgpackBackendSync {
|
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
// Create a unique shared memory name
|
|
62
|
-
const shmName = `bb-${process.pid}`;
|
|
63
|
-
|
|
64
|
-
// Default maxClients to 1 if not specified
|
|
65
|
-
const clientCount = maxClients ?? 1;
|
|
61
|
+
const shmName = `bb-sync-${process.pid}-${instanceCounter++}`;
|
|
66
62
|
|
|
67
63
|
// If threads not set use 1 thread. We're not expected to do long lived work on sync backends.
|
|
68
64
|
const hwc = threads ? threads.toString() : '1';
|
|
69
|
-
const env = { ...process.env, HARDWARE_CONCURRENCY:
|
|
65
|
+
const env = { ...process.env, HARDWARE_CONCURRENCY: hwc };
|
|
70
66
|
|
|
71
67
|
// Set up file logging if logger is provided.
|
|
72
68
|
// Direct file redirection bypasses Node event loop - logs are written even if process hangs.
|
|
@@ -78,17 +74,8 @@ export class BarretenbergNativeShmSyncBackend implements IMsgpackBackendSync {
|
|
|
78
74
|
logger(`BB process logs redirected to: ${logPath}`);
|
|
79
75
|
}
|
|
80
76
|
|
|
81
|
-
// Spawn bb process with shared memory mode
|
|
82
|
-
const args = [
|
|
83
|
-
'msgpack',
|
|
84
|
-
'run',
|
|
85
|
-
'--input',
|
|
86
|
-
`${shmName}.shm`,
|
|
87
|
-
'--max-clients',
|
|
88
|
-
clientCount.toString(),
|
|
89
|
-
'--request-ring-size',
|
|
90
|
-
`${1024 * 1024 * 2}`,
|
|
91
|
-
];
|
|
77
|
+
// Spawn bb process with shared memory mode (SPSC-only, no max-clients needed)
|
|
78
|
+
const args = ['msgpack', 'run', '--input', `${shmName}.shm`, '--request-ring-size', `${1024 * 1024 * 4}`];
|
|
92
79
|
const bbProcess = spawn(bbBinaryPath, args, {
|
|
93
80
|
stdio: ['ignore', logFd ?? 'ignore', logFd ?? 'ignore'],
|
|
94
81
|
env,
|
|
@@ -137,8 +124,8 @@ export class BarretenbergNativeShmSyncBackend implements IMsgpackBackendSync {
|
|
|
137
124
|
}
|
|
138
125
|
|
|
139
126
|
try {
|
|
140
|
-
// Create NAPI client
|
|
141
|
-
client = new addon.MsgpackClient(shmName
|
|
127
|
+
// Create NAPI client (SPSC-only, no max_clients needed)
|
|
128
|
+
client = new addon.MsgpackClient(shmName);
|
|
142
129
|
break; // Success!
|
|
143
130
|
} catch (err: any) {
|
|
144
131
|
// Connection failed, will retry
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { spawn, ChildProcess } from 'child_process';
|
|
3
|
+
import { openSync, closeSync } from 'fs';
|
|
4
|
+
import { IMsgpackBackendAsync } from '../interface.js';
|
|
5
|
+
import { findNapiBinary, findPackageRoot } from './platform.js';
|
|
6
|
+
|
|
7
|
+
// Import the NAPI module
|
|
8
|
+
// The addon is built to the nodejs_module directory
|
|
9
|
+
const addonPath = findNapiBinary();
|
|
10
|
+
// Try loading, but don't throw if it doesn't exist (will be caught in constructor)
|
|
11
|
+
let addon: any = null;
|
|
12
|
+
try {
|
|
13
|
+
if (addonPath) {
|
|
14
|
+
const require = createRequire(findPackageRoot()!);
|
|
15
|
+
addon = require(addonPath);
|
|
16
|
+
}
|
|
17
|
+
} catch (err) {
|
|
18
|
+
// Addon not built yet or not available
|
|
19
|
+
addon = null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let instanceCounter = 0;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Asynchronous shared memory backend that communicates with bb binary via shared memory.
|
|
26
|
+
* Uses NAPI module with background thread polling for async operations.
|
|
27
|
+
* Supports request pipelining - multiple requests can be in flight simultaneously.
|
|
28
|
+
*
|
|
29
|
+
* Architecture (matches socket backend pattern):
|
|
30
|
+
* - bb acts as the SERVER, TypeScript is the CLIENT
|
|
31
|
+
* - bb creates the shared memory region
|
|
32
|
+
* - TypeScript connects via NAPI wrapper (MsgpackClientAsync)
|
|
33
|
+
* - TypeScript manages promise queue (single-threaded, no mutex needed)
|
|
34
|
+
* - C++ background thread polls for responses, calls JavaScript callback
|
|
35
|
+
* - JavaScript callback pops queue and resolves promises in FIFO order
|
|
36
|
+
*/
|
|
37
|
+
export class BarretenbergNativeShmAsyncBackend implements IMsgpackBackendAsync {
|
|
38
|
+
private process: ChildProcess;
|
|
39
|
+
private client: any; // NAPI MsgpackClientAsync instance
|
|
40
|
+
private logFd?: number; // File descriptor for logs
|
|
41
|
+
|
|
42
|
+
// Queue of pending callbacks for pipelined requests
|
|
43
|
+
// Responses come back in FIFO order, so we match them with queued callbacks
|
|
44
|
+
private pendingCallbacks: Array<{
|
|
45
|
+
resolve: (data: Uint8Array) => void;
|
|
46
|
+
reject: (error: Error) => void;
|
|
47
|
+
}> = [];
|
|
48
|
+
|
|
49
|
+
private constructor(process: ChildProcess, client: any, logFd?: number) {
|
|
50
|
+
this.process = process;
|
|
51
|
+
this.client = client;
|
|
52
|
+
this.logFd = logFd;
|
|
53
|
+
|
|
54
|
+
// Register our response handler with the C++ client
|
|
55
|
+
// This callback will be invoked from the background thread via ThreadSafeFunction
|
|
56
|
+
this.client.setResponseCallback((responseBuffer: Buffer) => {
|
|
57
|
+
this.handleResponse(responseBuffer);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Handle response from C++ background thread
|
|
63
|
+
* Dequeues the next pending callback and resolves it (FIFO order)
|
|
64
|
+
*/
|
|
65
|
+
private handleResponse(responseBuffer: Buffer): void {
|
|
66
|
+
// Response is complete - dequeue the next pending callback (FIFO)
|
|
67
|
+
const callback = this.pendingCallbacks.shift();
|
|
68
|
+
if (callback) {
|
|
69
|
+
callback.resolve(new Uint8Array(responseBuffer));
|
|
70
|
+
} else {
|
|
71
|
+
// This shouldn't happen - response without a pending request
|
|
72
|
+
console.warn('Received response but no pending callback');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// If no more pending callbacks, release ref to allow process to exit
|
|
76
|
+
if (this.pendingCallbacks.length === 0) {
|
|
77
|
+
this.client.release();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Create and initialize an async shared memory backend.
|
|
83
|
+
* @param bbBinaryPath Path to bb binary
|
|
84
|
+
* @param threads Optional number of threads (defaults to min(32, num_cpus))
|
|
85
|
+
* @param logger Optional logger function for bb output
|
|
86
|
+
*/
|
|
87
|
+
static async new(
|
|
88
|
+
bbBinaryPath: string,
|
|
89
|
+
threads?: number,
|
|
90
|
+
logger?: (msg: string) => void,
|
|
91
|
+
): Promise<BarretenbergNativeShmAsyncBackend> {
|
|
92
|
+
if (!addon || !addon.MsgpackClientAsync) {
|
|
93
|
+
throw new Error('Shared memory async NAPI not available.');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Create a unique shared memory name
|
|
97
|
+
const shmName = `bb-async-${process.pid}-${instanceCounter++}`;
|
|
98
|
+
|
|
99
|
+
// If threads not set use num cpu cores, max 32 (same as socket backend)
|
|
100
|
+
const hwc = threads ? threads.toString() : '1';
|
|
101
|
+
const env = { ...process.env, HARDWARE_CONCURRENCY: hwc };
|
|
102
|
+
|
|
103
|
+
// Set up file logging if logger is provided
|
|
104
|
+
// Direct file redirection bypasses Node event loop - logs are written even if process hangs
|
|
105
|
+
let logFd: number | undefined;
|
|
106
|
+
let logPath: string | undefined;
|
|
107
|
+
if (logger) {
|
|
108
|
+
logPath = `/tmp/${shmName}.log`;
|
|
109
|
+
logFd = openSync(logPath, 'w');
|
|
110
|
+
logger(`BB process logs redirected to: ${logPath}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Spawn bb process with shared memory mode
|
|
114
|
+
// Use larger ring buffers for async mode to support pipelining
|
|
115
|
+
const args = [
|
|
116
|
+
'msgpack',
|
|
117
|
+
'run',
|
|
118
|
+
'--input',
|
|
119
|
+
`${shmName}.shm`,
|
|
120
|
+
'--request-ring-size',
|
|
121
|
+
`${1024 * 1024 * 4}`,
|
|
122
|
+
'--response-ring-size',
|
|
123
|
+
`${1024 * 1024 * 4}`,
|
|
124
|
+
];
|
|
125
|
+
const bbProcess = spawn(bbBinaryPath, args, {
|
|
126
|
+
stdio: ['ignore', logFd ?? 'ignore', logFd ?? 'ignore'],
|
|
127
|
+
env,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Disconnect from event loop so process can exit without waiting for bb
|
|
131
|
+
// The bb process has parent death monitoring (prctl on Linux, kqueue on macOS)
|
|
132
|
+
// so it will automatically exit when Node.js exits
|
|
133
|
+
bbProcess.unref();
|
|
134
|
+
|
|
135
|
+
// Track if process has exited
|
|
136
|
+
let processExited = false;
|
|
137
|
+
let exitError: Error | null = null;
|
|
138
|
+
|
|
139
|
+
bbProcess.on('error', err => {
|
|
140
|
+
processExited = true;
|
|
141
|
+
exitError = new Error(`Native backend process error: ${err.message}`);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
bbProcess.on('exit', (code, signal) => {
|
|
145
|
+
processExited = true;
|
|
146
|
+
if (code !== null && code !== 0) {
|
|
147
|
+
exitError = new Error(`Native backend process exited with code ${code}`);
|
|
148
|
+
} else if (signal && signal !== 'SIGTERM') {
|
|
149
|
+
exitError = new Error(`Native backend process killed with signal ${signal}`);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Wait for bb to create shared memory
|
|
154
|
+
// Retry connection every 100ms for up to 5 seconds (longer than sync for thread startup)
|
|
155
|
+
const retryInterval = 100; // ms
|
|
156
|
+
const timeout = 5000; // ms
|
|
157
|
+
const maxAttempts = Math.floor(timeout / retryInterval);
|
|
158
|
+
let client: any = null;
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
162
|
+
// Check if bb process has exited before attempting connection
|
|
163
|
+
if (processExited) {
|
|
164
|
+
throw exitError || new Error('Native backend process exited unexpectedly during startup');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Wait before attempting connection (except first attempt)
|
|
168
|
+
if (attempt > 0) {
|
|
169
|
+
await new Promise(resolve => setTimeout(resolve, retryInterval));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
// Create NAPI async client
|
|
174
|
+
client = new addon.MsgpackClientAsync(shmName);
|
|
175
|
+
break; // Success!
|
|
176
|
+
} catch (err: any) {
|
|
177
|
+
// Connection failed, will retry
|
|
178
|
+
if (attempt === maxAttempts - 1) {
|
|
179
|
+
// Last attempt failed - check one more time if process exited
|
|
180
|
+
if (processExited && exitError) {
|
|
181
|
+
throw exitError;
|
|
182
|
+
}
|
|
183
|
+
throw new Error(`Failed to connect to shared memory after ${timeout}ms: ${err.message}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!client) {
|
|
189
|
+
throw new Error('Failed to create client connection');
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return new BarretenbergNativeShmAsyncBackend(bbProcess, client, logFd);
|
|
193
|
+
} finally {
|
|
194
|
+
// If we failed to connect, ensure the process is killed and log file closed
|
|
195
|
+
if (!client) {
|
|
196
|
+
bbProcess.kill('SIGKILL');
|
|
197
|
+
if (logFd !== undefined) {
|
|
198
|
+
try {
|
|
199
|
+
closeSync(logFd);
|
|
200
|
+
} catch (e) {
|
|
201
|
+
// Ignore errors during cleanup
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Send a msgpack request asynchronously.
|
|
210
|
+
* Supports pipelining - can be called multiple times before awaiting responses.
|
|
211
|
+
* Use Promise.all() to send multiple requests concurrently.
|
|
212
|
+
*
|
|
213
|
+
* Example:
|
|
214
|
+
* const results = await Promise.all([
|
|
215
|
+
* backend.call(buf1),
|
|
216
|
+
* backend.call(buf2),
|
|
217
|
+
* backend.call(buf3)
|
|
218
|
+
* ]);
|
|
219
|
+
*
|
|
220
|
+
* @param inputBuffer The msgpack-encoded request
|
|
221
|
+
* @returns Promise resolving to msgpack-encoded response
|
|
222
|
+
*/
|
|
223
|
+
async call(inputBuffer: Uint8Array): Promise<Uint8Array> {
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
// If this is the first pending callback, acquire ref to keep event loop alive
|
|
226
|
+
if (this.pendingCallbacks.length === 0) {
|
|
227
|
+
this.client.acquire();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Enqueue this promise's callbacks (FIFO order)
|
|
231
|
+
this.pendingCallbacks.push({ resolve, reject });
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
// Send request to shared memory (synchronous write)
|
|
235
|
+
// C++ call() no longer returns a promise - we manage them here
|
|
236
|
+
this.client.call(Buffer.from(inputBuffer));
|
|
237
|
+
} catch (err: any) {
|
|
238
|
+
// Send failed - dequeue the callback we just added and reject
|
|
239
|
+
this.pendingCallbacks.pop();
|
|
240
|
+
|
|
241
|
+
// If queue is now empty, release ref to allow exit
|
|
242
|
+
if (this.pendingCallbacks.length === 0) {
|
|
243
|
+
this.client.release();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
reject(new Error(`Shared memory async call failed: ${err.message}`));
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async destroy(): Promise<void> {
|
|
252
|
+
// Kill the bb process
|
|
253
|
+
// Background thread and callbacks will be cleaned up by OS on process exit
|
|
254
|
+
this.process.kill('SIGTERM');
|
|
255
|
+
this.process.removeAllListeners();
|
|
256
|
+
|
|
257
|
+
// Close log file if open
|
|
258
|
+
if (this.logFd !== undefined) {
|
|
259
|
+
try {
|
|
260
|
+
closeSync(this.logFd);
|
|
261
|
+
} catch (e) {
|
|
262
|
+
// Ignore errors during cleanup
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
package/src/bin/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { findBbBinary } from '../bb_backends/node/platform.js';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
|
|
5
|
+
const bin = findBbBinary();
|
|
6
|
+
|
|
7
|
+
if (!bin) {
|
|
8
|
+
console.error('Could not find bb binary. Please ensure it is built and accessible.');
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const result = spawnSync(bin, process.argv.slice(2), { stdio: 'inherit' });
|
|
13
|
+
|
|
14
|
+
process.exit(result.status ?? 1);
|