@qvac/inference 0.20.0 → 0.21.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.
- package/NOTICE +107 -33
- package/README.md +8 -5
- package/dist/api/assess-model-fit.d.ts +9 -4
- package/dist/api/assess-model-fit.js +9 -4
- package/dist/api/bci-transcribe.d.ts +4 -1
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/rpc-server.d.ts +19 -0
- package/dist/api/rpc-server.js +44 -0
- package/dist/api/text-to-speech.d.ts +9 -0
- package/dist/api/text-to-speech.js +9 -0
- package/dist/api/transcribe.d.ts +11 -7
- package/dist/api/transcribe.js +47 -15
- package/dist/api/video-request.d.ts +5 -5
- package/dist/dispatch.js +39 -7
- package/dist/errors/index.d.ts +7 -0
- package/dist/errors/index.js +15 -0
- package/dist/handlers/assess-model-fit.d.ts +18 -6
- package/dist/handlers/assess-model-fit.js +215 -35
- package/dist/handlers/completion-orchestrate.js +17 -0
- package/dist/handlers/get-loaded-model-info.js +4 -1
- package/dist/handlers/get-model-info.js +29 -1
- package/dist/handlers/load-model/coreml-sidecars.d.ts +25 -0
- package/dist/handlers/load-model/coreml-sidecars.js +274 -0
- package/dist/handlers/load-model/hyperdrive.js +2 -2
- package/dist/handlers/load-model/registry-download-utils.d.ts +1 -0
- package/dist/handlers/load-model/registry-download-utils.js +2 -1
- package/dist/handlers/load-model/registry.js +59 -0
- package/dist/handlers/rpc-server.d.ts +23 -0
- package/dist/handlers/rpc-server.js +141 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/models/registry/models.d.ts +7731 -5672
- package/dist/models/registry/models.js +4652 -2526
- package/dist/models/registry/resource-profiles.js +481 -94
- package/dist/plugins/builtin/audiogen-ggml/plugin.d.ts +8 -6
- package/dist/plugins/builtin/audiogen-ggml/plugin.js +2 -1
- package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.d.ts +73 -1
- package/dist/plugins/builtin/bci-whispercpp-transcription/plugin.js +37 -19
- package/dist/plugins/builtin/ggml-vla/plugin.d.ts +1 -1
- package/dist/plugins/builtin/llamacpp-completion/ops/batch-completion-stream.js +9 -5
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stats.d.ts +1 -1
- package/dist/plugins/builtin/llamacpp-completion/ops/completion-stream.js +16 -7
- package/dist/plugins/builtin/llamacpp-completion/plugin.d.ts +80 -8
- package/dist/plugins/builtin/llamacpp-completion/plugin.js +9 -3
- package/dist/plugins/builtin/llamacpp-completion/transform.js +19 -0
- package/dist/plugins/builtin/llamacpp-embedding/plugin.d.ts +5 -3
- package/dist/plugins/builtin/llamacpp-embedding/plugin.js +2 -1
- package/dist/plugins/builtin/parakeet-transcription/plugin.d.ts +121 -0
- package/dist/plugins/builtin/parakeet-transcription/plugin.js +56 -27
- package/dist/plugins/builtin/sdcpp-generation/plugin.d.ts +8 -2
- package/dist/plugins/builtin/sdcpp-generation/plugin.js +7 -2
- package/dist/plugins/builtin/tts-ggml/ops/parler-options.d.ts +2 -0
- package/dist/plugins/builtin/tts-ggml/ops/parler-options.js +5 -1
- package/dist/plugins/builtin/tts-ggml/ops/text-to-speech.js +27 -10
- package/dist/plugins/builtin/tts-ggml/plugin.d.ts +181 -0
- package/dist/plugins/builtin/tts-ggml/plugin.js +84 -2
- package/dist/plugins/builtin/whispercpp-transcription/plugin.d.ts +136 -0
- package/dist/plugins/builtin/whispercpp-transcription/plugin.js +18 -7
- package/dist/plugins/ops/bci-transcribe.d.ts +6 -2
- package/dist/plugins/ops/bci-transcribe.js +18 -13
- package/dist/plugins/ops/load-model.js +2 -2
- package/dist/plugins/ops/transcribe.d.ts +4 -2
- package/dist/plugins/ops/transcribe.js +39 -2
- package/dist/plugins/ops/translate-stats.d.ts +2 -1
- package/dist/plugins/ops/translate-stats.js +64 -7
- package/dist/plugins/ops/translate.js +20 -4
- package/dist/profiling/operation-metrics.js +56 -0
- package/dist/registry.js +4 -0
- package/dist/resources/model-fit/assess.d.ts +46 -2
- package/dist/resources/model-fit/assess.js +67 -3
- package/dist/resources/model-fit/estimators/llm.js +2 -2
- package/dist/resources/model-fit/estimators/whisper.js +3 -4
- package/dist/resources/model-fit/fit-stub/fetch-fit-stub.d.ts +102 -0
- package/dist/resources/model-fit/fit-stub/fetch-fit-stub.js +212 -0
- package/dist/resources/model-fit/fit-stub/project-fit-from-load.d.ts +27 -0
- package/dist/resources/model-fit/fit-stub/project-fit-from-load.js +31 -0
- package/dist/resources/model-fit/fit-stub/resolve-load-from-stubs.d.ts +38 -0
- package/dist/resources/model-fit/fit-stub/resolve-load-from-stubs.js +138 -0
- package/dist/resources/model-fit/floor.js +2 -2
- package/dist/resources/model-fit/native-probe/advisory-fit.d.ts +9 -6
- package/dist/resources/model-fit/native-probe/advisory-fit.js +129 -80
- package/dist/resources/model-fit/native-probe/child-engine-fit.d.ts +2 -0
- package/dist/resources/model-fit/native-probe/child-engine-fit.js +41 -0
- package/dist/resources/model-fit/native-probe/classify-fit.d.ts +6 -0
- package/dist/resources/model-fit/native-probe/classify-fit.js +135 -0
- package/dist/resources/model-fit/native-probe/create-fit-request.d.ts +28 -0
- package/dist/resources/model-fit/native-probe/create-fit-request.js +51 -0
- package/dist/resources/model-fit/native-probe/engine-fit.d.ts +59 -0
- package/dist/resources/model-fit/native-probe/engine-fit.js +13 -0
- package/dist/resources/model-fit/native-probe/engines/asr.d.ts +10 -0
- package/dist/resources/model-fit/native-probe/engines/asr.js +103 -0
- package/dist/resources/model-fit/native-probe/engines/audiogen.d.ts +8 -0
- package/dist/resources/model-fit/native-probe/engines/audiogen.js +35 -0
- package/dist/resources/model-fit/native-probe/engines/diffusion.d.ts +7 -0
- package/dist/resources/model-fit/native-probe/engines/diffusion.js +55 -0
- package/dist/resources/model-fit/native-probe/engines/gpu.d.ts +10 -0
- package/dist/resources/model-fit/native-probe/engines/gpu.js +21 -0
- package/dist/resources/model-fit/native-probe/engines/llama.d.ts +13 -0
- package/dist/resources/model-fit/native-probe/engines/llama.js +145 -0
- package/dist/resources/model-fit/native-probe/engines/tts.d.ts +8 -0
- package/dist/resources/model-fit/native-probe/engines/tts.js +106 -0
- package/dist/resources/model-fit/native-probe/fit-outcome.d.ts +16 -0
- package/dist/resources/model-fit/native-probe/fit-outcome.js +1 -0
- package/dist/resources/model-fit/native-probe/fit-process.d.ts +27 -0
- package/dist/resources/model-fit/native-probe/fit-process.js +55 -0
- package/dist/resources/model-fit/native-probe/fit-runner.d.ts +1 -0
- package/dist/resources/model-fit/native-probe/fit-runner.js +58 -0
- package/dist/resources/model-fit/native-probe/run-fit.d.ts +24 -0
- package/dist/resources/model-fit/native-probe/run-fit.js +20 -0
- package/dist/resources/model-fit/native-probe/run-in-process-fit.d.ts +14 -34
- package/dist/resources/model-fit/native-probe/run-in-process-fit.js +55 -139
- package/dist/resources/model-fit/native-probe/run-isolated-fit.d.ts +5 -12
- package/dist/resources/model-fit/native-probe/run-isolated-fit.js +13 -8
- package/dist/resources/model-fit/platform.d.ts +9 -0
- package/dist/resources/model-fit/platform.js +28 -0
- package/dist/rpc/device-map.d.ts +13 -0
- package/dist/rpc/device-map.js +22 -0
- package/dist/rpc/discovery.d.ts +12 -0
- package/dist/rpc/discovery.js +225 -0
- package/dist/rpc/ggml-provider.d.ts +4 -0
- package/dist/rpc/ggml-provider.js +25 -0
- package/dist/rpc/index.d.ts +2 -0
- package/dist/rpc/index.js +1 -0
- package/dist/rpc/instance.d.ts +6 -0
- package/dist/rpc/instance.js +21 -0
- package/dist/rpc/inventory.d.ts +5 -0
- package/dist/rpc/inventory.js +144 -0
- package/dist/rpc/manager.d.ts +14 -0
- package/dist/rpc/manager.js +174 -0
- package/dist/rpc/network.d.ts +11 -0
- package/dist/rpc/network.js +85 -0
- package/dist/rpc/provider.d.ts +6 -0
- package/dist/rpc/provider.js +24 -0
- package/dist/runtime/lifecycle.js +31 -13
- package/dist/runtime/request-context.d.ts +1 -1
- package/dist/runtime/state.js +5 -0
- package/dist/schemas/assess-model-fit.d.ts +284 -77
- package/dist/schemas/assess-model-fit.js +93 -55
- package/dist/schemas/audio-gen.d.ts +6 -6
- package/dist/schemas/audio-gen.js +2 -2
- package/dist/schemas/batch-completion-stream.d.ts +13 -5
- package/dist/schemas/batch-completion-stream.js +5 -3
- package/dist/schemas/bci-config.js +1 -1
- package/dist/schemas/bci.d.ts +86 -1
- package/dist/schemas/bci.js +21 -2
- package/dist/schemas/cancel.d.ts +10 -0
- package/dist/schemas/cancel.js +2 -0
- package/dist/schemas/common.d.ts +765 -56
- package/dist/schemas/common.js +7 -0
- package/dist/schemas/completion-event.d.ts +6 -6
- package/dist/schemas/completion-stream.d.ts +27 -8
- package/dist/schemas/completion-stream.js +41 -4
- package/dist/schemas/config.d.ts +236 -30
- package/dist/schemas/config.js +10 -0
- package/dist/schemas/embed.d.ts +2 -2
- package/dist/schemas/error.js +13 -0
- package/dist/schemas/errors.d.ts +1 -0
- package/dist/schemas/errors.js +5 -0
- package/dist/schemas/index.d.ts +1 -0
- package/dist/schemas/index.js +1 -0
- package/dist/schemas/llamacpp-config.d.ts +105 -14
- package/dist/schemas/llamacpp-config.js +113 -3
- package/dist/schemas/load-model.d.ts +3102 -627
- package/dist/schemas/load-model.js +2 -2
- package/dist/schemas/mcp-adapter.d.ts +4 -0
- package/dist/schemas/model-config-schemas.d.ts +205 -5
- package/dist/schemas/model-info.d.ts +56 -0
- package/dist/schemas/model-info.js +5 -1
- package/dist/schemas/plugin.d.ts +8 -2
- package/dist/schemas/profiling.d.ts +4 -4
- package/dist/schemas/reload-config.d.ts +30 -0
- package/dist/schemas/rpc-server.d.ts +101 -0
- package/dist/schemas/rpc-server.js +87 -0
- package/dist/schemas/sdcpp-config.d.ts +7 -3
- package/dist/schemas/sdcpp-config.js +37 -2
- package/dist/schemas/system-resources.d.ts +4 -4
- package/dist/schemas/text-to-speech.d.ts +529 -1
- package/dist/schemas/text-to-speech.js +122 -12
- package/dist/schemas/tools.d.ts +2 -0
- package/dist/schemas/tools.js +8 -0
- package/dist/schemas/transcription-config.d.ts +5 -0
- package/dist/schemas/transcription-config.js +20 -2
- package/dist/schemas/transcription.d.ts +164 -0
- package/dist/schemas/transcription.js +71 -4
- package/dist/schemas/vla.d.ts +1 -1
- package/dist/surface.d.ts +4 -1
- package/dist/surface.js +3 -1
- package/dist/utils/addon-responses.d.ts +5 -1
- package/dist/utils/archive.js +5 -1
- package/dist/utils/asr-diagnostics.d.ts +12 -0
- package/dist/utils/asr-diagnostics.js +42 -0
- package/dist/utils/asr-events.d.ts +1 -0
- package/dist/utils/asr-events.js +3 -2
- package/dist/utils/cache/paths.js +3 -2
- package/dist/utils/mcp-adapter.js +7 -1
- package/dist/utils/multi-gpu-mobile.d.ts +1 -1
- package/dist/utils/multi-gpu-mobile.js +8 -6
- package/dist/utils/path-security.d.ts +5 -0
- package/dist/utils/path-security.js +14 -0
- package/dist/utils/tool-helpers.d.ts +4 -0
- package/dist/utils/tool-helpers.js +2 -0
- package/dist/utils/tools/defer.d.ts +93 -0
- package/dist/utils/tools/defer.js +290 -0
- package/dist/utils/tools/index.d.ts +1 -0
- package/dist/utils/tools/index.js +1 -0
- package/dist/utils/transcribe-metadata.d.ts +21 -7
- package/dist/utils/transcribe-metadata.js +24 -8
- package/dist/utils/tts-stats.d.ts +6 -0
- package/dist/utils/tts-stats.js +14 -1
- package/package.json +44 -30
- package/dist/resources/model-fit/native-probe/create-llama-fit-request.d.ts +0 -26
- package/dist/resources/model-fit/native-probe/create-llama-fit-request.js +0 -171
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import Hyperswarm from 'hyperswarm';
|
|
54
|
+
import { Buffer } from 'bare-buffer';
|
|
55
|
+
import { createDisposableScope } from '../runtime/disposable-scope.js';
|
|
56
|
+
import { registerSwarm, unregisterSwarm } from '../runtime/runtime-lifecycle.js';
|
|
57
|
+
import { getEngineLogger } from '../logging/index.js';
|
|
58
|
+
import { delay, privateEndpoint, rpcTopic, throwIfAborted } from './network.js';
|
|
59
|
+
import { queryRpcInventory } from './inventory.js';
|
|
60
|
+
const FRAME_LIMIT = 1024;
|
|
61
|
+
const LEASE_MS = 3000;
|
|
62
|
+
const MAX_CANDIDATES = 64;
|
|
63
|
+
export function createDiscoverySwarm() {
|
|
64
|
+
const swarm = new Hyperswarm({ maxPeers: 32 });
|
|
65
|
+
swarm.on('error', (error) => getEngineLogger().warn('RPC discovery swarm:', error));
|
|
66
|
+
registerSwarm(swarm, { label: 'rpc-discovery', createdAt: Date.now() });
|
|
67
|
+
return swarm;
|
|
68
|
+
}
|
|
69
|
+
export async function destroyDiscoverySwarm(swarm) {
|
|
70
|
+
await swarm.destroy();
|
|
71
|
+
unregisterSwarm(swarm);
|
|
72
|
+
}
|
|
73
|
+
export function parseAnnouncement(line) {
|
|
74
|
+
if (line.length > FRAME_LIMIT)
|
|
75
|
+
return;
|
|
76
|
+
try {
|
|
77
|
+
const value = JSON.parse(line);
|
|
78
|
+
if (!value || typeof value !== 'object')
|
|
79
|
+
return;
|
|
80
|
+
const frame = value;
|
|
81
|
+
if (frame['version'] !== 1 || typeof frame['url'] !== 'string')
|
|
82
|
+
return;
|
|
83
|
+
return privateEndpoint(frame['url']) ? frame['url'] : undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export async function advertiseRpcServer(topic, url, createSwarm = createDiscoverySwarm) {
|
|
90
|
+
if (!privateEndpoint(url)) {
|
|
91
|
+
throw new Error('RPC advertising requires a concrete private IPv4 endpoint');
|
|
92
|
+
}
|
|
93
|
+
const swarm = createSwarm();
|
|
94
|
+
let withdrawn = false;
|
|
95
|
+
const connections = new Set();
|
|
96
|
+
const frame = JSON.stringify({ version: 1, url }) + '\n';
|
|
97
|
+
const announce = () => {
|
|
98
|
+
if (swarm.suspended)
|
|
99
|
+
return;
|
|
100
|
+
for (const connection of connections) {
|
|
101
|
+
if (!connection.destroyed && !connection.write(frame))
|
|
102
|
+
connection.destroy();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
swarm.on('connection', (connection) => {
|
|
106
|
+
if (withdrawn || swarm.suspended) {
|
|
107
|
+
connection.destroy();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
connection.on('error', () => { });
|
|
111
|
+
connection.once('close', () => connections.delete(connection));
|
|
112
|
+
// This protocol is one-way; clients never send requests to advertisers.
|
|
113
|
+
connection.on('data', () => connection.destroy());
|
|
114
|
+
connections.add(connection);
|
|
115
|
+
if (!connection.write(frame))
|
|
116
|
+
connection.destroy();
|
|
117
|
+
});
|
|
118
|
+
const timer = setInterval(announce, 1000);
|
|
119
|
+
async function withdraw() {
|
|
120
|
+
withdrawn = true;
|
|
121
|
+
clearInterval(timer);
|
|
122
|
+
for (const connection of connections)
|
|
123
|
+
connection.destroy();
|
|
124
|
+
await destroyDiscoverySwarm(swarm);
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
swarm.join(rpcTopic(topic), { server: true, client: false });
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
await withdraw();
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
return withdraw;
|
|
134
|
+
}
|
|
135
|
+
export async function discoverRpcEndpoints(topic, timeoutMs, ctx, deps = {}) {
|
|
136
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
137
|
+
try {
|
|
138
|
+
const scope = __addDisposableResource(env_1, createDisposableScope(), true);
|
|
139
|
+
const swarm = (deps.createSwarm ?? createDiscoverySwarm)();
|
|
140
|
+
scope.defer(() => destroyDiscoverySwarm(swarm));
|
|
141
|
+
const candidates = new Map();
|
|
142
|
+
const inventories = new Map();
|
|
143
|
+
const probes = new Map();
|
|
144
|
+
const deadline = Date.now() + timeoutMs;
|
|
145
|
+
const connections = new Set();
|
|
146
|
+
scope.defer(() => {
|
|
147
|
+
for (const connection of connections)
|
|
148
|
+
connection.destroy();
|
|
149
|
+
});
|
|
150
|
+
swarm.on('connection', (connection) => {
|
|
151
|
+
if (connections.size >= MAX_CANDIDATES) {
|
|
152
|
+
connection.destroy();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
connections.add(connection);
|
|
156
|
+
let pending = Buffer.alloc(0);
|
|
157
|
+
let frames = 0;
|
|
158
|
+
connection.on('error', () => { });
|
|
159
|
+
connection.once('close', () => {
|
|
160
|
+
candidates.delete(connection);
|
|
161
|
+
connections.delete(connection);
|
|
162
|
+
});
|
|
163
|
+
connection.on('data', (chunk) => {
|
|
164
|
+
if (Date.now() >= deadline || ctx.signal.aborted || swarm.suspended)
|
|
165
|
+
return;
|
|
166
|
+
if (pending.length + chunk.length > FRAME_LIMIT) {
|
|
167
|
+
connection.destroy();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
pending = Buffer.concat([pending, chunk]);
|
|
171
|
+
let newline;
|
|
172
|
+
while ((newline = pending.indexOf('\n')) !== -1) {
|
|
173
|
+
// Bound work from a peer that ignores the one-second announcement interval.
|
|
174
|
+
if (++frames > Math.ceil(timeoutMs / 1000) + 4) {
|
|
175
|
+
connection.destroy();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
const url = parseAnnouncement(pending.subarray(0, newline).toString());
|
|
179
|
+
pending = Buffer.from(pending.subarray(newline + 1));
|
|
180
|
+
if (!url) {
|
|
181
|
+
connection.destroy();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const previous = candidates.get(connection);
|
|
185
|
+
if (previous && previous.url !== url) {
|
|
186
|
+
connection.destroy();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
candidates.set(connection, { url, seen: Date.now() });
|
|
190
|
+
if (probes.has(url) || probes.size >= MAX_CANDIDATES)
|
|
191
|
+
continue;
|
|
192
|
+
const endpoint = privateEndpoint(url);
|
|
193
|
+
const probe = (deps.probe ?? queryRpcInventory)(endpoint.host, endpoint.port, Math.max(1, Math.min(500, deadline - Date.now())), ctx.signal, scope)
|
|
194
|
+
.then((inventory) => {
|
|
195
|
+
if (inventory?.devices.length)
|
|
196
|
+
inventories.set(url, inventory);
|
|
197
|
+
})
|
|
198
|
+
.catch(() => { });
|
|
199
|
+
probes.set(url, probe);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
swarm.join(rpcTopic(topic), { server: false, client: true });
|
|
204
|
+
await delay(timeoutMs, ctx.signal);
|
|
205
|
+
throwIfAborted(ctx.signal);
|
|
206
|
+
// No cache survives the lookup. Closed peers and expired leases are excluded.
|
|
207
|
+
if (swarm.suspended)
|
|
208
|
+
return [];
|
|
209
|
+
const now = Date.now();
|
|
210
|
+
return [
|
|
211
|
+
...new Set([...candidates.values()]
|
|
212
|
+
.filter(({ url, seen }) => now - seen < LEASE_MS && inventories.has(url))
|
|
213
|
+
.map(({ url }) => url))
|
|
214
|
+
].map((url) => ({ url, ...inventories.get(url) }));
|
|
215
|
+
}
|
|
216
|
+
catch (e_1) {
|
|
217
|
+
env_1.error = e_1;
|
|
218
|
+
env_1.hasError = true;
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
const result_1 = __disposeResources(env_1);
|
|
222
|
+
if (result_1)
|
|
223
|
+
await result_1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { startRpcServer } from '@qvac/ggml-rpc-server';
|
|
2
|
+
/** Optional native adapter. Import this subpath only in workers assembled for RPC serving. */
|
|
3
|
+
export const ggmlRpcServerProvider = {
|
|
4
|
+
async start(options) {
|
|
5
|
+
const handle = await startRpcServer({
|
|
6
|
+
...(options.host !== undefined && { host: options.host }),
|
|
7
|
+
...(options.port !== undefined && { port: options.port }),
|
|
8
|
+
...(options.device !== undefined && { device: options.device }),
|
|
9
|
+
...(options.cache !== undefined && { cache: options.cache }),
|
|
10
|
+
...(options.threads !== undefined && { threads: options.threads }),
|
|
11
|
+
...(options.expectRdma !== undefined && { expectRdma: options.expectRdma }),
|
|
12
|
+
...(options.allowNonLoopbackHost !== undefined && {
|
|
13
|
+
allowNonLoopbackHost: options.allowNonLoopbackHost
|
|
14
|
+
})
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
host: handle.host,
|
|
18
|
+
port: handle.port,
|
|
19
|
+
url: handle.url,
|
|
20
|
+
rdmaCapable: handle.rdmaCapable,
|
|
21
|
+
stop: () => handle.stop()
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
export default ggmlRpcServerProvider;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { registerRpcServerProvider } from './provider.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const rpcServers: {
|
|
2
|
+
start: (options: import("../index.js").StartRpcServerOptions, ctx: import("../runtime/index.js").RequestContext) => Promise<import("../index.js").RpcServerInfo>;
|
|
3
|
+
stop: (serverId: string) => Promise<void>;
|
|
4
|
+
close: () => Promise<void>;
|
|
5
|
+
readonly size: number;
|
|
6
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createRpcServerManager } from './manager.js';
|
|
2
|
+
import { advertiseRpcServer } from './discovery.js';
|
|
3
|
+
import { waitForReady } from './network.js';
|
|
4
|
+
import { getRpcServerProvider } from './provider.js';
|
|
5
|
+
export const rpcServers = createRpcServerManager({
|
|
6
|
+
async start(options) {
|
|
7
|
+
return getRpcServerProvider().start({
|
|
8
|
+
...(options.host !== undefined && { host: options.host }),
|
|
9
|
+
...(options.port !== undefined && { port: options.port }),
|
|
10
|
+
...(options.device !== undefined && { device: options.device }),
|
|
11
|
+
...(options.cache !== undefined && { cache: options.cache }),
|
|
12
|
+
...(options.threads !== undefined && { threads: options.threads }),
|
|
13
|
+
...(options.expectRdma !== undefined && { expectRdma: options.expectRdma }),
|
|
14
|
+
...(options.allowNonLoopbackHost !== undefined && {
|
|
15
|
+
allowNonLoopbackHost: options.allowNonLoopbackHost
|
|
16
|
+
})
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
ready: (server, ctx) => waitForReady(server.host, server.port, ctx.signal),
|
|
20
|
+
advertise: advertiseRpcServer
|
|
21
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AbortSignal } from 'bare-abort-controller';
|
|
2
|
+
import type { DisposableScope } from '../runtime/disposable-scope.js';
|
|
3
|
+
import type { RpcServerCandidate } from '../schemas/rpc-server.js';
|
|
4
|
+
export type RpcInventory = Pick<RpcServerCandidate, 'devices' | 'rdmaAvailable'>;
|
|
5
|
+
export declare function queryRpcInventory(host: string, port: number, timeoutMs: number, signal: AbortSignal, scope?: DisposableScope): Promise<RpcInventory | undefined>;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import net from 'bare-net';
|
|
2
|
+
import { Buffer } from 'bare-buffer';
|
|
3
|
+
import { InferenceCancelledError } from '../errors/index.js';
|
|
4
|
+
import { getEngineLogger } from '../logging/index.js';
|
|
5
|
+
import { throwIfAborted } from './network.js';
|
|
6
|
+
// Fabric RPC_PROTO_*_VERSION:
|
|
7
|
+
// https://github.com/tetherto/qvac-fabric-llm.cpp/blob/v10549.5.0/ggml/include/ggml-rpc.h
|
|
8
|
+
// Command IDs and HELLO layouts, rpc_cmd and rpc_msg_hello_*:
|
|
9
|
+
// https://github.com/tetherto/qvac-fabric-llm.cpp/blob/v10549.5.0/ggml/src/ggml-rpc/ggml-rpc.cpp
|
|
10
|
+
// QVAC RPC 108.0/109.0 share these inventory messages. Patch versions are compatible.
|
|
11
|
+
const SUPPORTED_VERSIONS = [
|
|
12
|
+
{ major: 108, minor: 0 },
|
|
13
|
+
{ major: 109, minor: 0 }
|
|
14
|
+
];
|
|
15
|
+
const SUPPORTED_VERSION_LABEL = SUPPORTED_VERSIONS.map(({ major, minor }) => `${major}.${minor}.x`).join(', ');
|
|
16
|
+
const HELLO = 14;
|
|
17
|
+
const HELLO_REQUEST_SIZE = 24;
|
|
18
|
+
const HELLO_RESPONSE_SIZE = 28;
|
|
19
|
+
const DEVICE_COUNT = 15;
|
|
20
|
+
const GET_DEVICE_MEMORY = 11;
|
|
21
|
+
const MAX_DEVICES = 128;
|
|
22
|
+
export function queryRpcInventory(host, port, timeoutMs, signal, scope) {
|
|
23
|
+
throwIfAborted(signal);
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const socket = new net.Socket();
|
|
26
|
+
const devices = [];
|
|
27
|
+
let pending = Buffer.alloc(0);
|
|
28
|
+
let command = HELLO;
|
|
29
|
+
let responseSize = HELLO_RESPONSE_SIZE;
|
|
30
|
+
let count = 0;
|
|
31
|
+
let rdmaAvailable = false;
|
|
32
|
+
let finished = false;
|
|
33
|
+
const timer = setTimeout(() => finish(), timeoutMs);
|
|
34
|
+
const abort = () => finish(undefined, new InferenceCancelledError('rpc'));
|
|
35
|
+
function finish(result, error) {
|
|
36
|
+
if (finished)
|
|
37
|
+
return;
|
|
38
|
+
finished = true;
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
signal.removeEventListener('abort', abort);
|
|
41
|
+
socket.destroy();
|
|
42
|
+
if (error)
|
|
43
|
+
reject(error);
|
|
44
|
+
else
|
|
45
|
+
resolve(result);
|
|
46
|
+
}
|
|
47
|
+
function rejectHello(reason, chunk = Buffer.alloc(0)) {
|
|
48
|
+
// Read only the length and version prefix, even for an oversized chunk.
|
|
49
|
+
const prefix = Buffer.alloc(Math.min(11, pending.length + chunk.length));
|
|
50
|
+
const copied = pending.copy(prefix, 0, 0, prefix.length);
|
|
51
|
+
chunk.copy(prefix, copied, 0, prefix.length - copied);
|
|
52
|
+
const size = prefix.length >= 8 ? prefix.readBigUInt64LE(0) : undefined;
|
|
53
|
+
const version = size !== undefined && size >= 3n && prefix.length >= 11
|
|
54
|
+
? `${prefix[8]}.${prefix[9]}.${prefix[10]}`
|
|
55
|
+
: 'unavailable';
|
|
56
|
+
getEngineLogger().warn('RPC inventory HELLO rejected', {
|
|
57
|
+
url: `${host}:${port}`,
|
|
58
|
+
reason,
|
|
59
|
+
receivedVersion: version,
|
|
60
|
+
supportedVersions: SUPPORTED_VERSION_LABEL,
|
|
61
|
+
receivedHelloBytes: size?.toString() ?? 'unavailable',
|
|
62
|
+
expectedHelloBytes: HELLO_RESPONSE_SIZE
|
|
63
|
+
});
|
|
64
|
+
finish();
|
|
65
|
+
}
|
|
66
|
+
function send(cmd, payload, size) {
|
|
67
|
+
command = cmd;
|
|
68
|
+
responseSize = size;
|
|
69
|
+
const frame = Buffer.alloc(9 + payload.length);
|
|
70
|
+
frame[0] = cmd;
|
|
71
|
+
frame.writeBigUInt64LE(BigInt(payload.length), 1);
|
|
72
|
+
payload.copy(frame, 9);
|
|
73
|
+
socket.write(frame);
|
|
74
|
+
}
|
|
75
|
+
function memory() {
|
|
76
|
+
const payload = Buffer.alloc(4);
|
|
77
|
+
payload.writeUInt32LE(devices.length);
|
|
78
|
+
send(GET_DEVICE_MEMORY, payload, 16);
|
|
79
|
+
}
|
|
80
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
81
|
+
scope?.defer(() => finish());
|
|
82
|
+
socket.on('error', () => finish());
|
|
83
|
+
socket.on('close', () => finish());
|
|
84
|
+
// Zero capabilities keep this probe on TCP.
|
|
85
|
+
socket.on('connect', () => send(HELLO, Buffer.alloc(HELLO_REQUEST_SIZE), HELLO_RESPONSE_SIZE));
|
|
86
|
+
socket.on('data', (chunk) => {
|
|
87
|
+
if (finished)
|
|
88
|
+
return;
|
|
89
|
+
if (!Buffer.isBuffer(chunk))
|
|
90
|
+
return finish();
|
|
91
|
+
// One bounded response per request; reject excess bytes before allocating.
|
|
92
|
+
if (pending.length + chunk.length > 8 + responseSize) {
|
|
93
|
+
if (command === HELLO)
|
|
94
|
+
return rejectHello('response exceeds frame limit', chunk);
|
|
95
|
+
return finish();
|
|
96
|
+
}
|
|
97
|
+
pending = Buffer.concat([pending, chunk]);
|
|
98
|
+
if (pending.length < 8)
|
|
99
|
+
return;
|
|
100
|
+
if (pending.readBigUInt64LE(0) !== BigInt(responseSize)) {
|
|
101
|
+
if (command === HELLO)
|
|
102
|
+
return rejectHello('unexpected response size');
|
|
103
|
+
return finish();
|
|
104
|
+
}
|
|
105
|
+
if (pending.length < 8 + responseSize)
|
|
106
|
+
return;
|
|
107
|
+
if (command === HELLO &&
|
|
108
|
+
!SUPPORTED_VERSIONS.some(({ major, minor }) => pending[8] === major && pending[9] === minor)) {
|
|
109
|
+
return rejectHello('unsupported protocol version');
|
|
110
|
+
}
|
|
111
|
+
const payload = Buffer.from(pending.subarray(8));
|
|
112
|
+
pending = Buffer.alloc(0);
|
|
113
|
+
if (command === HELLO) {
|
|
114
|
+
// QPN follows the four version/padding bytes. The probe itself stays on TCP.
|
|
115
|
+
rdmaAvailable = payload.readUInt32LE(4) !== 0;
|
|
116
|
+
send(DEVICE_COUNT, Buffer.alloc(0), 4);
|
|
117
|
+
}
|
|
118
|
+
else if (command === DEVICE_COUNT) {
|
|
119
|
+
count = payload.readUInt32LE(0);
|
|
120
|
+
if (count > MAX_DEVICES)
|
|
121
|
+
return finish();
|
|
122
|
+
if (count === 0)
|
|
123
|
+
return finish({ devices, rdmaAvailable });
|
|
124
|
+
memory();
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const freeMemory = Number(payload.readBigUInt64LE(0));
|
|
128
|
+
const totalMemory = Number(payload.readBigUInt64LE(8));
|
|
129
|
+
if (!Number.isSafeInteger(freeMemory) || !Number.isSafeInteger(totalMemory))
|
|
130
|
+
return finish();
|
|
131
|
+
devices.push({ index: devices.length, freeMemory, totalMemory });
|
|
132
|
+
if (devices.length === count)
|
|
133
|
+
return finish({ devices, rdmaAvailable });
|
|
134
|
+
memory();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
try {
|
|
138
|
+
socket.connect(port, host);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
finish();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RequestContext } from '../runtime/request-context.js';
|
|
2
|
+
import type { StartRpcServerOptions, RpcServerInfo, RpcServerHandle } from '../schemas/rpc-server.js';
|
|
3
|
+
export type ManagedRpcServer = RpcServerHandle;
|
|
4
|
+
export interface RpcServerDependencies {
|
|
5
|
+
start(options: StartRpcServerOptions): Promise<ManagedRpcServer>;
|
|
6
|
+
ready(server: ManagedRpcServer, ctx: RequestContext): Promise<void>;
|
|
7
|
+
advertise(topic: string, url: string): Promise<() => Promise<void>>;
|
|
8
|
+
}
|
|
9
|
+
export declare function createRpcServerManager(deps: RpcServerDependencies): {
|
|
10
|
+
start: (options: StartRpcServerOptions, ctx: RequestContext) => Promise<RpcServerInfo>;
|
|
11
|
+
stop: (serverId: string) => Promise<void>;
|
|
12
|
+
close: () => Promise<void>;
|
|
13
|
+
readonly size: number;
|
|
14
|
+
};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { generateRandomRequestId } from '../runtime/request-id.js';
|
|
54
|
+
import { createDisposableScope } from '../runtime/disposable-scope.js';
|
|
55
|
+
import { privateEndpoint, throwIfAborted } from './network.js';
|
|
56
|
+
export function createRpcServerManager(deps) {
|
|
57
|
+
const servers = new Map();
|
|
58
|
+
const starts = new Set();
|
|
59
|
+
let closing;
|
|
60
|
+
async function stop(serverId) {
|
|
61
|
+
const entry = servers.get(serverId);
|
|
62
|
+
if (!entry)
|
|
63
|
+
throw new Error(`Unknown RPC server: ${serverId}`);
|
|
64
|
+
if (entry.stopping)
|
|
65
|
+
return entry.stopping;
|
|
66
|
+
entry.stopping = (async () => {
|
|
67
|
+
const errors = [];
|
|
68
|
+
if (entry.withdraw) {
|
|
69
|
+
try {
|
|
70
|
+
await entry.withdraw();
|
|
71
|
+
delete entry.withdraw;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
errors.push(error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
await entry.handle.stop();
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
errors.push(error);
|
|
82
|
+
}
|
|
83
|
+
if (errors.length === 1)
|
|
84
|
+
throw errors[0];
|
|
85
|
+
if (errors.length > 1) {
|
|
86
|
+
throw new AggregateError(errors, `Failed to stop RPC server ${serverId}: ${errors.map(String).join('; ')}`, { cause: errors[0] });
|
|
87
|
+
}
|
|
88
|
+
servers.delete(serverId);
|
|
89
|
+
})();
|
|
90
|
+
try {
|
|
91
|
+
await entry.stopping;
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
delete entry.stopping;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function startOwned(options, ctx) {
|
|
98
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
99
|
+
try {
|
|
100
|
+
throwIfAborted(ctx.signal);
|
|
101
|
+
if (options.discoveryTopic &&
|
|
102
|
+
(!options.allowNonLoopbackHost || !privateEndpoint(`${options.host}:1`))) {
|
|
103
|
+
throw new Error('RPC advertising requires a concrete private IPv4 host and allowNonLoopbackHost: true');
|
|
104
|
+
}
|
|
105
|
+
const scope = __addDisposableResource(env_1, createDisposableScope(), true);
|
|
106
|
+
const handle = await deps.start(options);
|
|
107
|
+
const serverId = generateRandomRequestId();
|
|
108
|
+
const entry = { handle, active: false };
|
|
109
|
+
servers.set(serverId, entry);
|
|
110
|
+
let transferred = false;
|
|
111
|
+
scope.defer(async () => {
|
|
112
|
+
if (!transferred)
|
|
113
|
+
await stop(serverId);
|
|
114
|
+
});
|
|
115
|
+
throwIfAborted(ctx.signal);
|
|
116
|
+
await deps.ready(handle, ctx);
|
|
117
|
+
throwIfAborted(ctx.signal);
|
|
118
|
+
if (closing)
|
|
119
|
+
throw new Error('RPC server manager is closing');
|
|
120
|
+
if (options.discoveryTopic) {
|
|
121
|
+
entry.withdraw = await deps.advertise(options.discoveryTopic, handle.url);
|
|
122
|
+
}
|
|
123
|
+
throwIfAborted(ctx.signal);
|
|
124
|
+
if (closing)
|
|
125
|
+
throw new Error('RPC server manager is closing');
|
|
126
|
+
entry.active = true;
|
|
127
|
+
transferred = true;
|
|
128
|
+
return { serverId, url: handle.url, rdmaCapable: handle.rdmaCapable };
|
|
129
|
+
}
|
|
130
|
+
catch (e_1) {
|
|
131
|
+
env_1.error = e_1;
|
|
132
|
+
env_1.hasError = true;
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
const result_1 = __disposeResources(env_1);
|
|
136
|
+
if (result_1)
|
|
137
|
+
await result_1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function start(options, ctx) {
|
|
141
|
+
if (closing)
|
|
142
|
+
return Promise.reject(new Error('RPC server manager is closing'));
|
|
143
|
+
const task = startOwned(options, ctx);
|
|
144
|
+
starts.add(task);
|
|
145
|
+
void task.then(() => starts.delete(task), () => starts.delete(task));
|
|
146
|
+
return task;
|
|
147
|
+
}
|
|
148
|
+
function close() {
|
|
149
|
+
if (closing)
|
|
150
|
+
return closing;
|
|
151
|
+
closing = (async () => {
|
|
152
|
+
// Start every known stop before waiting for starts that may be rolling back.
|
|
153
|
+
const initialIds = new Set([...servers].filter(([, entry]) => entry.active).map(([id]) => id));
|
|
154
|
+
const stopping = Promise.allSettled([...initialIds].map(stop));
|
|
155
|
+
await Promise.allSettled([...starts]);
|
|
156
|
+
const lateStops = Promise.allSettled([...servers.keys()].filter((id) => !initialIds.has(id)).map(stop));
|
|
157
|
+
const results = [...(await stopping), ...(await lateStops)];
|
|
158
|
+
const errors = results.flatMap((r) => (r.status === 'rejected' ? [r.reason] : []));
|
|
159
|
+
if (errors.length)
|
|
160
|
+
throw new AggregateError(errors, 'Failed to stop owned RPC servers');
|
|
161
|
+
})();
|
|
162
|
+
return closing.finally(() => {
|
|
163
|
+
closing = undefined;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
start,
|
|
168
|
+
stop,
|
|
169
|
+
close,
|
|
170
|
+
get size() {
|
|
171
|
+
return servers.size;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DisposableScope } from '../runtime/disposable-scope.js';
|
|
2
|
+
import type { AbortSignal } from 'bare-abort-controller';
|
|
3
|
+
export declare function rpcTopic(topic: string): import("bare-buffer");
|
|
4
|
+
export declare function privateEndpoint(url: string): {
|
|
5
|
+
host: string;
|
|
6
|
+
port: number;
|
|
7
|
+
} | undefined;
|
|
8
|
+
export declare function throwIfAborted(signal: AbortSignal): void;
|
|
9
|
+
export declare function delay(ms: number, signal: AbortSignal): Promise<void>;
|
|
10
|
+
export declare function probeTcp(host: string, port: number, timeoutMs: number, signal: AbortSignal, scope?: DisposableScope): Promise<boolean>;
|
|
11
|
+
export declare function waitForReady(host: string, port: number, signal: AbortSignal): Promise<void>;
|