@qvac/ggml-rpc-server 0.0.0 → 0.1.1

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/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ ## [0.1.1] - 2026-09-30
4
+
5
+ ### Changed
6
+
7
+ - `@qvac/fabric` dependency bumped `^0.18.1` -> `^0.19.0`, and the mobile
8
+ `@qvac/fabric-android-arm64` and `@qvac/fabric-ios` pins `0.18.1` ->
9
+ `0.19.0`. This carries `qvac-fabric` `10549.4.0` -> `10549.5.0`, whose RPC
10
+ change matters here: idle RDMA and tensor-dispatch threads now sleep instead
11
+ of spinning, so an idle server no longer keeps cores busy. A caret on a `0.x`
12
+ version locks the minor, so `^0.18.1` would not have resolved `0.19.0` on
13
+ its own. No API change.
14
+
15
+ ## 0.1.0
16
+
17
+ - Add the managed `ggml-rpc-server` API as an in-process Bare addon backed by
18
+ the npm `@qvac/fabric` package.
19
+ - Report through `rdmaCapable` whether the installed Fabric RPC backend was
20
+ built with RDMA, and reject `expectRdma: true` when it was not.
21
+ - Report native start and stop failures as typed errors extending
22
+ `RpcServerNativeError` (`RpcServerDeviceError`, `RpcServerCacheError`,
23
+ `RpcServerStartError`, `RpcServerBackendError`, `RpcServerStopError`).
24
+ - Build prebuild artifacts for macOS arm64/x64, Linux arm64/x64, Windows x64, Android arm64, and iOS arm64.
package/LICENSE ADDED
@@ -0,0 +1,179 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2026 Tether Data, S.A. de C.V.
package/NOTICE ADDED
@@ -0,0 +1,6 @@
1
+ @qvac/ggml-rpc-server
2
+ Copyright (c) Tether
3
+
4
+ This product includes software developed by third parties under their
5
+ respective licenses. @qvac/ggml-rpc-server itself is licensed under the Apache
6
+ License, Version 2.0.
package/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # @qvac/ggml-rpc-server
2
+
3
+ Managed in-process GGML RPC server for Bare applications.
4
+
5
+ The package loads the RPC backend supplied by `@qvac/fabric` and starts it
6
+ through a native addon. It does not bundle or launch llama.cpp's
7
+ `ggml-rpc-server` CLI executable.
8
+
9
+ Prebuild artifacts are produced for macOS arm64/x64, Linux arm64/x64, Windows
10
+ x64, Android arm64, iOS arm64, and the iOS simulator on arm64/x64. Desktop
11
+ prebuild jobs smoke-test the in-process lifecycle path; Android/iOS jobs
12
+ cross-build the same addon for their targets.
13
+
14
+ ```js
15
+ const { startRpcServer } = require('@qvac/ggml-rpc-server')
16
+
17
+ const server = await startRpcServer({ device: 'Vulkan0' })
18
+
19
+ try {
20
+ console.log(server.url)
21
+ console.log(server.rdmaCapable)
22
+ } finally {
23
+ await server.stop()
24
+ }
25
+ ```
26
+
27
+ The listener accepts IPv4 addresses; `localhost` is normalized to `127.0.0.1`.
28
+ The default host is `127.0.0.1`, but loopback is not private to the host
29
+ application: other local processes (including apps with local TCP access on
30
+ Android and iOS) can connect. The underlying RPC listener has no authentication
31
+ and serves one client at a time, so another local client can occupy the server
32
+ and block the intended client. Start it only when needed, stop it after use,
33
+ and do not treat loopback binding as an access-control boundary. Non-loopback
34
+ hosts are rejected unless `allowNonLoopbackHost: true` is passed; use them only
35
+ on a trusted/private network with external access controls.
36
+
37
+ With `cache: true`, the server stores tensor data sent by clients in a local
38
+ cache directory, so any client that can connect can write to that directory.
39
+ Combined with a non-loopback host, that is unauthenticated disk writes from the
40
+ network; enable the cache only for clients you trust.
41
+
42
+ Like a listening `net.Server`, a running server keeps the process alive until
43
+ `stop()` resolves, so a standalone worker can start it and wait for clients.
44
+
45
+ Native server output is written to the host application's platform log.
46
+
47
+ ## Errors
48
+
49
+ Errors are classes that can be matched by `name` or `instanceof`. Invalid
50
+ options throw `RpcServerInvalidHostError`, `RpcServerNonLoopbackHostError`,
51
+ `RangeError` (port) or `TypeError` (threads); a failed port lookup throws
52
+ `RpcServerPortAllocationError`; and `expectRdma: true` on a backend without RDMA
53
+ throws `RpcServerRdmaUnavailableError`. Failures reported by the native server
54
+ extend `RpcServerNativeError`, which carries the native error as `cause` and a
55
+ `code` equal to its `name`:
56
+
57
+ | Error | When |
58
+ |---|---|
59
+ | `RpcServerDeviceError` | No requested device exists, or no device is available |
60
+ | `RpcServerCacheError` | The cache directory cannot be resolved or created |
61
+ | `RpcServerStartError` | The server cannot be created or bound, or the RPC backend is missing |
62
+ | `RpcServerBackendError` | The Fabric backends directory is invalid or cannot be inspected |
63
+ | `RpcServerStopError` | The server does not stop cleanly |
64
+
65
+ Anything else from the native addon, such as an out-of-memory failure, is
66
+ rethrown unchanged.
67
+
68
+ ## Linux requirements
69
+
70
+ The Linux RPC backend in `@qvac/fabric` links `libibverbs.so.1`, so Linux
71
+ hosts must provide it (`libibverbs1` on Debian/Ubuntu) even when connections
72
+ use TCP. Without it, `startRpcServer()` rejects with an error whose `code` is
73
+ `RpcServerStartError`.
74
+ RDMA also requires the provider package for the host's hardware.
75
+
76
+ ## RDMA-capable builds
77
+
78
+ On Linux (not Android), `rdmaCapable` reports whether the installed
79
+ `@qvac/fabric` RPC backend was built with RDMA. Such a backend negotiates RDMA
80
+ with each client that also supports it and falls back to TCP otherwise, so
81
+ `rdmaCapable: true` does not guarantee that a given connection uses RDMA. Other
82
+ platforms always report `false`.
83
+
84
+ To fail closed when RDMA is required, pass `expectRdma: true`. Startup rejects
85
+ with `RpcServerRdmaUnavailableError` when the installed backend lacks RDMA:
86
+
87
+ ```js
88
+ const server = await startRpcServer({
89
+ device: 'Vulkan0',
90
+ host: '10.10.10.2',
91
+ expectRdma: true,
92
+ allowNonLoopbackHost: true
93
+ })
94
+
95
+ console.log(server.rdmaCapable)
96
+ ```
97
+
98
+ Without `expectRdma`, the server starts either way and reports the backend's
99
+ capability in `rdmaCapable`.
package/binding.js ADDED
@@ -0,0 +1,6 @@
1
+ // Pre-load @qvac/fabric so its shared .bare module (the llama.cpp + ggml
2
+ // runtime) is registered with the bare runtime before our addon resolves its
3
+ // DT_NEEDED dependency qvac__fabric@0.bare.
4
+ require("@qvac/fabric");
5
+
6
+ module.exports = require.addon();
package/index.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ export declare const DEFAULT_RPC_SERVER_HOST: string;
2
+ export declare class RpcServerPortAllocationError extends Error {
3
+ constructor(cause?: unknown);
4
+ }
5
+ export declare class RpcServerNonLoopbackHostError extends Error {
6
+ constructor(host: string);
7
+ }
8
+ export declare class RpcServerInvalidHostError extends Error {
9
+ constructor(host: string);
10
+ }
11
+ export declare class RpcServerRdmaUnavailableError extends Error {
12
+ constructor();
13
+ }
14
+ /**
15
+ * A failure reported by the native server. The addon raises plain errors with a
16
+ * `code`; these classes let callers branch on `name` or `instanceof` as they do
17
+ * for the errors above. `code` equals `name`, and `cause` is the native error.
18
+ */
19
+ export declare abstract class RpcServerNativeError extends Error {
20
+ readonly code: string;
21
+ constructor(name: string, message: string, cause: unknown);
22
+ }
23
+ /** No requested device exists, or no device is available. */
24
+ export declare class RpcServerDeviceError extends RpcServerNativeError {
25
+ constructor(message: string, cause: unknown);
26
+ }
27
+ /** The RPC cache directory could not be resolved or created. */
28
+ export declare class RpcServerCacheError extends RpcServerNativeError {
29
+ constructor(message: string, cause: unknown);
30
+ }
31
+ /** The server could not be created or bound, or the RPC backend is missing. */
32
+ export declare class RpcServerStartError extends RpcServerNativeError {
33
+ constructor(message: string, cause: unknown);
34
+ }
35
+ /** The Fabric backends directory is invalid or could not be inspected. */
36
+ export declare class RpcServerBackendError extends RpcServerNativeError {
37
+ constructor(message: string, cause: unknown);
38
+ }
39
+ /** The server did not stop cleanly. */
40
+ export declare class RpcServerStopError extends RpcServerNativeError {
41
+ constructor(message: string, cause: unknown);
42
+ }
43
+ export interface StartRpcServerOptions {
44
+ readonly device?: string | readonly string[];
45
+ readonly host?: string;
46
+ readonly port?: number;
47
+ readonly cache?: boolean;
48
+ readonly threads?: number;
49
+ readonly expectRdma?: boolean;
50
+ readonly allowNonLoopbackHost?: boolean;
51
+ }
52
+ export interface RpcServer {
53
+ readonly host: string;
54
+ readonly port: number;
55
+ readonly url: string;
56
+ readonly device?: string;
57
+ /**
58
+ * Whether the loaded Fabric RPC backend was built with RDMA. Such a backend
59
+ * negotiates RDMA with each RDMA-capable client and falls back to TCP otherwise.
60
+ */
61
+ readonly rdmaCapable: boolean;
62
+ stop(): Promise<void>;
63
+ }
64
+ export interface AllocateFreePortOptions {
65
+ readonly allowNonLoopbackHost?: boolean;
66
+ }
67
+ export declare function allocateFreePort(host?: string, options?: AllocateFreePortOptions): Promise<number>;
68
+ export declare function startRpcServer(options?: StartRpcServerOptions): Promise<RpcServer>;
package/index.js ADDED
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RpcServerStopError =
4
+ exports.RpcServerBackendError =
5
+ exports.RpcServerStartError =
6
+ exports.RpcServerCacheError =
7
+ exports.RpcServerDeviceError =
8
+ exports.RpcServerNativeError =
9
+ exports.RpcServerRdmaUnavailableError =
10
+ exports.RpcServerInvalidHostError =
11
+ exports.RpcServerNonLoopbackHostError =
12
+ exports.RpcServerPortAllocationError =
13
+ exports.DEFAULT_RPC_SERVER_HOST =
14
+ void 0;
15
+ exports.allocateFreePort = allocateFreePort;
16
+ exports.startRpcServer = startRpcServer;
17
+ /* eslint-disable @typescript-eslint/no-require-imports -- Bare modules and native bindings expose CommonJS export shapes. */
18
+ const net = require("bare-net");
19
+ const path = require("bare-path");
20
+ const fabricBackends = require("@qvac/fabric/backends");
21
+ const binding = require("./binding");
22
+ /* eslint-enable @typescript-eslint/no-require-imports */
23
+ exports.DEFAULT_RPC_SERVER_HOST = "127.0.0.1";
24
+ const TRUSTED_LAN_WARNING_CODE = "QVAC_GGML_RPC_SERVER_TRUSTED_LAN";
25
+ // Keep native handles alive until an explicit stop finishes. Otherwise their
26
+ // finalizer can synchronously stop and join a live server during garbage collection.
27
+ const activeServerHandles = new Set();
28
+ // Fabric never unloads its RPC module, so its RDMA build cannot change within a
29
+ // process. Cache the native check, which reads the module from disk.
30
+ const rdmaSupportByBackendsDir = new Map();
31
+ class RpcServerPortAllocationError extends Error {
32
+ constructor(cause) {
33
+ super("Failed to allocate a free port for ggml-rpc-server", { cause });
34
+ this.name = "RpcServerPortAllocationError";
35
+ }
36
+ }
37
+ exports.RpcServerPortAllocationError = RpcServerPortAllocationError;
38
+ class RpcServerNonLoopbackHostError extends Error {
39
+ constructor(host) {
40
+ super(
41
+ `${host} is not a loopback host; pass allowNonLoopbackHost: true to bind it on a trusted network`,
42
+ );
43
+ this.name = "RpcServerNonLoopbackHostError";
44
+ }
45
+ }
46
+ exports.RpcServerNonLoopbackHostError = RpcServerNonLoopbackHostError;
47
+ class RpcServerInvalidHostError extends Error {
48
+ constructor(host) {
49
+ super(`ggml-rpc-server requires an IPv4 address or localhost: ${host}`);
50
+ this.name = "RpcServerInvalidHostError";
51
+ }
52
+ }
53
+ exports.RpcServerInvalidHostError = RpcServerInvalidHostError;
54
+ class RpcServerRdmaUnavailableError extends Error {
55
+ constructor() {
56
+ super("RDMA is not available in the installed @qvac/fabric RPC backend");
57
+ this.name = "RpcServerRdmaUnavailableError";
58
+ }
59
+ }
60
+ exports.RpcServerRdmaUnavailableError = RpcServerRdmaUnavailableError;
61
+ /**
62
+ * A failure reported by the native server. The addon raises plain errors with a
63
+ * `code`; these classes let callers branch on `name` or `instanceof` as they do
64
+ * for the errors above. `code` equals `name`, and `cause` is the native error.
65
+ */
66
+ class RpcServerNativeError extends Error {
67
+ code;
68
+ // Names are literals, not new.target.name, so they survive minification.
69
+ constructor(name, message, cause) {
70
+ super(message, { cause });
71
+ this.name = name;
72
+ this.code = name;
73
+ }
74
+ }
75
+ exports.RpcServerNativeError = RpcServerNativeError;
76
+ /** No requested device exists, or no device is available. */
77
+ class RpcServerDeviceError extends RpcServerNativeError {
78
+ constructor(message, cause) {
79
+ super("RpcServerDeviceError", message, cause);
80
+ }
81
+ }
82
+ exports.RpcServerDeviceError = RpcServerDeviceError;
83
+ /** The RPC cache directory could not be resolved or created. */
84
+ class RpcServerCacheError extends RpcServerNativeError {
85
+ constructor(message, cause) {
86
+ super("RpcServerCacheError", message, cause);
87
+ }
88
+ }
89
+ exports.RpcServerCacheError = RpcServerCacheError;
90
+ /** The server could not be created or bound, or the RPC backend is missing. */
91
+ class RpcServerStartError extends RpcServerNativeError {
92
+ constructor(message, cause) {
93
+ super("RpcServerStartError", message, cause);
94
+ }
95
+ }
96
+ exports.RpcServerStartError = RpcServerStartError;
97
+ /** The Fabric backends directory is invalid or could not be inspected. */
98
+ class RpcServerBackendError extends RpcServerNativeError {
99
+ constructor(message, cause) {
100
+ super("RpcServerBackendError", message, cause);
101
+ }
102
+ }
103
+ exports.RpcServerBackendError = RpcServerBackendError;
104
+ /** The server did not stop cleanly. */
105
+ class RpcServerStopError extends RpcServerNativeError {
106
+ constructor(message, cause) {
107
+ super("RpcServerStopError", message, cause);
108
+ }
109
+ }
110
+ exports.RpcServerStopError = RpcServerStopError;
111
+ const nativeErrorClasses = new Map([
112
+ ["RpcServerDeviceError", RpcServerDeviceError],
113
+ ["RpcServerCacheError", RpcServerCacheError],
114
+ ["RpcServerStartError", RpcServerStartError],
115
+ ["RpcServerBackendError", RpcServerBackendError],
116
+ ["RpcServerStopError", RpcServerStopError],
117
+ ]);
118
+ function toTypedError(error) {
119
+ const code = error?.code;
120
+ const ErrorClass =
121
+ typeof code === "string" ? nativeErrorClasses.get(code) : undefined;
122
+ return ErrorClass === undefined
123
+ ? error
124
+ : new ErrorClass(error.message, error);
125
+ }
126
+ async function callNative(call) {
127
+ try {
128
+ return await call();
129
+ } catch (error) {
130
+ throw toTypedError(error);
131
+ }
132
+ }
133
+ function isLoopbackHost(host) {
134
+ const parts = host.split(".");
135
+ return (
136
+ parts.length === 4 &&
137
+ parts[0] === "127" &&
138
+ parts.every((part) => /^\d{1,3}$/.test(part) && Number(part) <= 255)
139
+ );
140
+ }
141
+ function isIpv4Host(host) {
142
+ const parts = host.split(".");
143
+ return (
144
+ parts.length === 4 &&
145
+ parts.every((part) => /^\d{1,3}$/.test(part) && Number(part) <= 255)
146
+ );
147
+ }
148
+ function normalizeHost(host) {
149
+ return host === "localhost" ? exports.DEFAULT_RPC_SERVER_HOST : host;
150
+ }
151
+ function assertSupportedHost(host) {
152
+ if (!isIpv4Host(host)) {
153
+ throw new RpcServerInvalidHostError(host);
154
+ }
155
+ }
156
+ function assertLoopbackHost(host, allowNonLoopbackHost = false) {
157
+ if (!allowNonLoopbackHost && !isLoopbackHost(host)) {
158
+ throw new RpcServerNonLoopbackHostError(host);
159
+ }
160
+ }
161
+ function warnForTrustedLanHost(host, allowNonLoopbackHost = false) {
162
+ if (!allowNonLoopbackHost || isLoopbackHost(host)) return;
163
+ console.warn(
164
+ `[${TRUSTED_LAN_WARNING_CODE}] ggml-rpc-server is binding to non-loopback host ${host}. ` +
165
+ "The ggml RPC transport has no authentication or encryption; use this only on a trusted private network with external access controls.",
166
+ );
167
+ }
168
+ function normalizeDevice(device) {
169
+ if (device === undefined) return undefined;
170
+ // The native side splits on ',' or '/' and matches names exactly, so trim
171
+ // each name: 'Vulkan0, CPU' would otherwise look up ' CPU'.
172
+ const names = typeof device === "string" ? device.split(/[,/]/) : device;
173
+ const trimmed = names.map((name) => name.trim());
174
+ // A blank-only value must not collapse to '', which means "default devices".
175
+ // Pass it through untouched so the native side rejects it as unknown.
176
+ if (
177
+ trimmed.every((name) => name === "") &&
178
+ names.some((name) => name !== "")
179
+ ) {
180
+ return names.join(",");
181
+ }
182
+ return trimmed.join(",");
183
+ }
184
+ function validatePort(port) {
185
+ if (!Number.isSafeInteger(port) || port <= 0 || port > 65535) {
186
+ throw new RangeError("port must be an integer between 1 and 65535");
187
+ }
188
+ }
189
+ function validateThreads(threads) {
190
+ if (
191
+ threads !== undefined &&
192
+ (!Number.isSafeInteger(threads) || threads <= 0)
193
+ ) {
194
+ throw new TypeError("threads must be a positive integer");
195
+ }
196
+ }
197
+ function rpcBackendSupportsRdma(backendsDir) {
198
+ let supported = rdmaSupportByBackendsDir.get(backendsDir);
199
+ if (supported === undefined) {
200
+ try {
201
+ supported = binding.rpcBackendSupportsRdma({ backendsDir });
202
+ } catch (error) {
203
+ throw toTypedError(error);
204
+ }
205
+ rdmaSupportByBackendsDir.set(backendsDir, supported);
206
+ }
207
+ return supported;
208
+ }
209
+ function allocateFreePort(
210
+ host = exports.DEFAULT_RPC_SERVER_HOST,
211
+ options = {},
212
+ ) {
213
+ const bindHost = normalizeHost(host);
214
+ assertSupportedHost(bindHost);
215
+ assertLoopbackHost(bindHost, options.allowNonLoopbackHost);
216
+ return new Promise((resolve, reject) => {
217
+ const server = net.createServer();
218
+ server.once("error", (error) =>
219
+ reject(new RpcServerPortAllocationError(error)),
220
+ );
221
+ server.listen(0, bindHost, () => {
222
+ const address = server.address();
223
+ if (address === null || typeof address === "string") {
224
+ server.close(() => reject(new RpcServerPortAllocationError()));
225
+ return;
226
+ }
227
+ server.close(() => resolve(address.port));
228
+ });
229
+ });
230
+ }
231
+ async function startRpcServer(options = {}) {
232
+ const host = normalizeHost(options.host ?? exports.DEFAULT_RPC_SERVER_HOST);
233
+ assertSupportedHost(host);
234
+ assertLoopbackHost(host, options.allowNonLoopbackHost);
235
+ warnForTrustedLanHost(host, options.allowNonLoopbackHost);
236
+ validateThreads(options.threads);
237
+ // Packed mobile bundles do not retain a resolvable node_modules tree. Their
238
+ // packagers stage Fabric's backends beside this addon instead.
239
+ const backendsDir =
240
+ fabricBackends.resolveBackendsDir() ?? path.join(__dirname, "prebuilds");
241
+ const rdmaCapable = rpcBackendSupportsRdma(backendsDir);
242
+ if (options.expectRdma === true && !rdmaCapable) {
243
+ throw new RpcServerRdmaUnavailableError();
244
+ }
245
+ const port =
246
+ options.port ??
247
+ (await allocateFreePort(host, {
248
+ allowNonLoopbackHost: options.allowNonLoopbackHost,
249
+ }));
250
+ validatePort(port);
251
+ const device = normalizeDevice(options.device);
252
+ const handle = await callNative(() =>
253
+ binding.startServer({
254
+ endpoint: `${host}:${port}`,
255
+ device,
256
+ cache: options.cache ?? false,
257
+ threads: options.threads,
258
+ backendsDir,
259
+ }),
260
+ );
261
+ activeServerHandles.add(handle);
262
+ let stopPromise;
263
+ return {
264
+ host,
265
+ port,
266
+ url: `${host}:${port}`,
267
+ device,
268
+ rdmaCapable,
269
+ stop: () => {
270
+ stopPromise ??= callNative(() => binding.stopServer(handle)).then(
271
+ () => {
272
+ activeServerHandles.delete(handle);
273
+ },
274
+ (error) => {
275
+ // Keep the handle pinned so the caller can retry a failed stop.
276
+ stopPromise = undefined;
277
+ throw error;
278
+ },
279
+ );
280
+ return stopPromise;
281
+ },
282
+ };
283
+ }
package/package.json CHANGED
@@ -1 +1,75 @@
1
- {"name":"@qvac/ggml-rpc-server","version":"0.0.0"}
1
+ {
2
+ "name": "@qvac/ggml-rpc-server",
3
+ "version": "0.1.1",
4
+ "description": "Managed ggml RPC server for QVAC LLM inference",
5
+ "addon": true,
6
+ "scripts": {
7
+ "build": "npm run build:ts && npm run build:native",
8
+ "build:native": "bare-make generate && bare-make build && bare-make install",
9
+ "build:ts": "tsc -p tsconfig.build.json",
10
+ "check:generated": "node scripts/check-generated.mjs",
11
+ "typecheck": "tsc --noEmit -p tsconfig.json",
12
+ "lint": "npm run lint:js && npm run lint:ts && npm run test:types",
13
+ "lint:js": "prettier --check \"**/*.{js,cjs,mjs}\" && lunte",
14
+ "lint:ts": "eslint \"src/**/*.ts\" --max-warnings=0",
15
+ "lint:ts:fix": "eslint \"src/**/*.ts\" --fix",
16
+ "lint-cpp": "clang-tidy -p build addon/binding.cpp",
17
+ "format": "prettier --write \"**/*.{js,cjs,mjs}\"",
18
+ "test": "npm run build:ts && npm run test:node",
19
+ "test:node": "node --test test/node/*.test.cjs",
20
+ "test:unit": "npm run test:node",
21
+ "test:types": "npm run typecheck && npm run check:generated",
22
+ "test:dts": "npm run test:types"
23
+ },
24
+ "files": [
25
+ "index.js",
26
+ "index.d.ts",
27
+ "binding.js",
28
+ "prebuilds",
29
+ "CHANGELOG.md",
30
+ "LICENSE",
31
+ "NOTICE",
32
+ "README.md"
33
+ ],
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/tetherto/qvac.git",
37
+ "directory": "packages/ggml-rpc-server"
38
+ },
39
+ "author": "Tether",
40
+ "license": "Apache-2.0",
41
+ "bugs": "https://github.com/tetherto/qvac/issues",
42
+ "homepage": "https://github.com/tetherto/qvac/tree/main/packages/ggml-rpc-server#readme",
43
+ "devDependencies": {
44
+ "@qvac/fabric-android-arm64": "0.19.0",
45
+ "@qvac/fabric-ios": "0.19.0",
46
+ "@types/node": "^24.2.1",
47
+ "cmake-bare": "^1.7.5",
48
+ "cmake-npm": "^1.1.0",
49
+ "cmake-vcpkg": "^1.1.0",
50
+ "eslint": "^9.39.4",
51
+ "eslint-import-resolver-typescript": "^4.4.5",
52
+ "eslint-plugin-import": "^2.32.0",
53
+ "lunte": "^1.8.4",
54
+ "prettier": "^3.9.6",
55
+ "prettier-config-holepunch": "^2.0.0",
56
+ "typescript": "^5.9.2",
57
+ "typescript-eslint": "^8.63.0"
58
+ },
59
+ "dependencies": {
60
+ "@qvac/fabric": "^0.19.0",
61
+ "bare-net": "^2.3.2",
62
+ "bare-path": "^3.0.0"
63
+ },
64
+ "engines": {
65
+ "bare": ">=1.24.0"
66
+ },
67
+ "exports": {
68
+ "./package": "./package.json",
69
+ ".": {
70
+ "types": "./index.d.ts",
71
+ "default": "./index.js"
72
+ }
73
+ },
74
+ "types": "index.d.ts"
75
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "main_library": {
3
+ "compatibility_versions": [
4
+ {
5
+ "version": "0"
6
+ }
7
+ ],
8
+ "current_versions": [
9
+ {
10
+ "version": "0.1.1"
11
+ }
12
+ ],
13
+ "exported_symbols": [
14
+ {
15
+ "text": {
16
+ "global": [
17
+ "_bare_register_module_v0",
18
+ "_bare_get_module_name_v0"
19
+ ]
20
+ }
21
+ }
22
+ ],
23
+ "flags": [
24
+ {
25
+ "attributes": [
26
+ "not_app_extension_safe"
27
+ ]
28
+ }
29
+ ],
30
+ "install_names": [
31
+ {
32
+ "name": "qvac__ggml-rpc-server@0.bare"
33
+ }
34
+ ],
35
+ "target_info": [
36
+ {
37
+ "min_deployment": "13",
38
+ "target": "arm64-macos"
39
+ }
40
+ ]
41
+ },
42
+ "tapi_tbd_version": 5
43
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "main_library": {
3
+ "compatibility_versions": [
4
+ {
5
+ "version": "0"
6
+ }
7
+ ],
8
+ "current_versions": [
9
+ {
10
+ "version": "0.1.1"
11
+ }
12
+ ],
13
+ "exported_symbols": [
14
+ {
15
+ "text": {
16
+ "global": [
17
+ "_bare_register_module_v0",
18
+ "_bare_get_module_name_v0"
19
+ ]
20
+ }
21
+ }
22
+ ],
23
+ "flags": [
24
+ {
25
+ "attributes": [
26
+ "not_app_extension_safe"
27
+ ]
28
+ }
29
+ ],
30
+ "install_names": [
31
+ {
32
+ "name": "qvac__ggml-rpc-server@0.bare"
33
+ }
34
+ ],
35
+ "target_info": [
36
+ {
37
+ "min_deployment": "13",
38
+ "target": "x86_64-macos"
39
+ }
40
+ ]
41
+ },
42
+ "tapi_tbd_version": 5
43
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "main_library": {
3
+ "compatibility_versions": [
4
+ {
5
+ "version": "0"
6
+ }
7
+ ],
8
+ "current_versions": [
9
+ {
10
+ "version": "0.1.1"
11
+ }
12
+ ],
13
+ "exported_symbols": [
14
+ {
15
+ "text": {
16
+ "global": [
17
+ "_bare_register_module_v0",
18
+ "_bare_get_module_name_v0"
19
+ ]
20
+ }
21
+ }
22
+ ],
23
+ "flags": [
24
+ {
25
+ "attributes": [
26
+ "not_app_extension_safe"
27
+ ]
28
+ }
29
+ ],
30
+ "install_names": [
31
+ {
32
+ "name": "qvac__ggml-rpc-server@0.bare"
33
+ }
34
+ ],
35
+ "target_info": [
36
+ {
37
+ "min_deployment": "14",
38
+ "target": "arm64-ios"
39
+ }
40
+ ]
41
+ },
42
+ "tapi_tbd_version": 5
43
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "main_library": {
3
+ "compatibility_versions": [
4
+ {
5
+ "version": "0"
6
+ }
7
+ ],
8
+ "current_versions": [
9
+ {
10
+ "version": "0.1.1"
11
+ }
12
+ ],
13
+ "exported_symbols": [
14
+ {
15
+ "text": {
16
+ "global": [
17
+ "_bare_register_module_v0",
18
+ "_bare_get_module_name_v0"
19
+ ]
20
+ }
21
+ }
22
+ ],
23
+ "flags": [
24
+ {
25
+ "attributes": [
26
+ "not_app_extension_safe"
27
+ ]
28
+ }
29
+ ],
30
+ "install_names": [
31
+ {
32
+ "name": "qvac__ggml-rpc-server@0.bare"
33
+ }
34
+ ],
35
+ "target_info": [
36
+ {
37
+ "min_deployment": "14",
38
+ "target": "arm64-ios-simulator"
39
+ }
40
+ ]
41
+ },
42
+ "tapi_tbd_version": 5
43
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "main_library": {
3
+ "compatibility_versions": [
4
+ {
5
+ "version": "0"
6
+ }
7
+ ],
8
+ "current_versions": [
9
+ {
10
+ "version": "0.1.1"
11
+ }
12
+ ],
13
+ "exported_symbols": [
14
+ {
15
+ "text": {
16
+ "global": [
17
+ "_bare_register_module_v0",
18
+ "_bare_get_module_name_v0"
19
+ ]
20
+ }
21
+ }
22
+ ],
23
+ "flags": [
24
+ {
25
+ "attributes": [
26
+ "not_app_extension_safe"
27
+ ]
28
+ }
29
+ ],
30
+ "install_names": [
31
+ {
32
+ "name": "qvac__ggml-rpc-server@0.bare"
33
+ }
34
+ ],
35
+ "target_info": [
36
+ {
37
+ "min_deployment": "14",
38
+ "target": "x86_64-ios-simulator"
39
+ }
40
+ ]
41
+ },
42
+ "tapi_tbd_version": 5
43
+ }