@tetherto/mdk-gateway 0.0.0 → 0.12.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/LICENSE ADDED
@@ -0,0 +1,202 @@
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
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Tether
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,306 @@
1
+ # Gateway
2
+
3
+ ## Overview
4
+
5
+ The Gateway is a router for [apps](#extend-the-gateway): each app is a plugin that defines its own routes and business logic, runs
6
+ in its own [`@tetherto/mdk-app-runtime`](../app-runtime/README.md) process, and opens its own connection to Kernel. The Gateway reads
7
+ each app's route table (`mdk-plugin.json`) and forwards every request to the app's runtime over HRPC; it executes no app code.
8
+
9
+ The Gateway, `@tetherto/mdk-gateway`, delivers an HTTP interface on top of the
10
+ [Kernel](../kernel/README.md): each app builds its own [`@tetherto/mdk-client`](../client/README.md) from its context to reach
11
+ the Kernel, which routes a query to whichever [Worker](../../workers/README.md) owns the target device.
12
+
13
+ The Gateway gates routes a manifest marks `"auth": true` through an `authVerifier` you supply, and identifying the caller stays
14
+ your identity layer's job, as the [security model](#security-model) sets out.
15
+
16
+ > [!NOTE]
17
+ > The Gateway's responsibilities stop at forwarding the request to the right controller and returning whatever that controller produces;
18
+ > it never combines results from more than one controller itself. The route (the URL your app calls) is handled entirely by the plugin
19
+ > that declared it. That plugin's "controller" must build the whole answer
20
+ > and hand it back as the HTTP response. If a route needs to combine telemetry from many
21
+ > devices, such as total hashrate across every miner on site, that happens inside that one controller, which must
22
+ > query every relevant Worker and aggregate the results.
23
+
24
+ ## HTTP API overview
25
+
26
+ The Gateway declares no application routes of its own. Every REST route it serves comes from one of three places, all wired in
27
+ [`http.node.wrk.js`](workers/http.node.wrk.js):
28
+
29
+ | Source | How it arrives |
30
+ |--------|----------------|
31
+ | Apps | Every app the stack declares in `spec.apps[]`, or that you pass as `apps` — and nothing else. Mounted under `/apps/<name>` unless the app sets `mount` |
32
+ | [`additionalRoutes`](#raw-fastify-routes) | Raw Fastify route objects you pass to `startGateway()` |
33
+ | Static files | `GET /*` from `staticRootPath`, only when `common.json` sets it |
34
+
35
+ Which paths that adds up to is a property of the manifests, not of the Gateway. The
36
+ plugin route reference lists every route the plugins MDK ships serve, generated from their
37
+ `mdk-plugin.json` files so it cannot drift.
38
+
39
+ > [!IMPORTANT]
40
+ > A Gateway loads **only** what it is told to load. A stack serves just the apps it declares, and any other path gets `404`.
41
+
42
+ ### Request forwarding
43
+
44
+ Each route is a Fastify route, so its `schema` is enforced at the Gateway. The Gateway reads the runtime's key from the app's `keyFile`
45
+ on every request and forwards `{ params, query, body, headers }` by route `id`. A unary result comes back as JSON, so a controller
46
+ returns plain data (a `Buffer` would arrive as its JSON form); a `stream: true` route pipes the runtime's status,
47
+ headers and body to the response. The `authorization`, `proxy-authorization` and `cookie` headers are not forwarded: the caller's
48
+ identity reaches the app as `req._info`, and app code never sees credentials it could replay. A missing or invalid key file answers
49
+ `503` `ERR_APP_RUNTIME_UNAVAILABLE: <name>` (a runtime removes its key file when it stops), an unreachable runtime `502`. A unary
50
+ call, and the wait for a stream's response head, is bounded by `spec.gateway.config.appTimeoutMs` in `mdk.yaml` (default `60000`);
51
+ past it the Gateway answers `504` `ERR_APP_RUNTIME_TIMEOUT`. A client that disconnects first aborts its call: while the runtime connection is
52
+ still opening the request is never sent, and a stream's channel is closed so the controller's response emits `close`. A `cache`
53
+ route is cached under the app name, route id and the named request fields (plus a hash of the caller's identity on an `auth: true`
54
+ route), and is not aborted, since waiting requests share it.
55
+
56
+ ## Live data
57
+
58
+ The Gateway has no push channel — clients poll its HTTP routes for updates.
59
+
60
+ > A UI reads a route with `useQuery` and a `refetchInterval` — the [mvp-site](../../../examples/mvp-site/README.md) dashboard
61
+ > polls its `site` plugin's routes this way. Pick the interval per route: fast for live readings, slow for inventory.
62
+
63
+ ## Configuration
64
+
65
+ Config files are written to `opts.root/config/facs/` by `startGateway()`. Example files ship in `backend/core/gateway/config/facs/*.example`.
66
+ Edit the generated files to persist your changes across restarts.
67
+
68
+ | File | Controls |
69
+ |------|---------|
70
+ | `httpd.config.json` | Fastify HTTP server options |
71
+ | `store.config.json` | Hyperbee store path; the store holds identity seeds only |
72
+ | `net.config.json` | IP assignment (DHCP facility) |
73
+ | `logging.config.json` | Log level, format |
74
+
75
+ HTTP hardening is read from `startGateway()` options first, then from `common.json`, then from the defaults below. A
76
+ custom `authVerifier` is a function, so only `startGateway()` (or a programmatic worker `ctx`) can pass one; the name of a
77
+ built-in verifier (`"loopback"`) can also come from `common.json`.
78
+
79
+ | Option | Default | Effect |
80
+ |--------|---------|--------|
81
+ | `authVerifier` | none | `async (request, route) => identity \| false`, or `"loopback"`. Runs for every `auth: true` route; see [security model](#security-model) |
82
+ | `allowedHosts` | `[]` | Extra `Host` values served, as `name` (any port) or `name:port`. `localhost`, `127.0.0.1` and `[::1]` on the bound port, plus a specific bind address, are always allowed. `["*"]` turns the check off |
83
+ | `trustProxy` | `false` | Fastify `trustProxy`. Set it only behind a proxy you control, or client IPs and `X-Forwarded-Host` can be spoofed |
84
+ | `bodyLimit` | `1048576` | Largest request body in bytes; a larger one gets `413` |
85
+ | `rateLimit` | `{ max: 300, timeWindow: "1 minute" }` | [`@fastify/rate-limit`](https://github.com/fastify/fastify-rate-limit) options, keyed by client IP; `false` disables |
86
+ | `helmet` | Helmet defaults without `upgrade-insecure-requests` | [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) options; `false` disables |
87
+ | `staticRootPath` | unset | Directory served at `GET /*` (with `index.html` for unknown paths). Static serving is off unless you set it |
88
+
89
+ ```js
90
+ await startGateway({
91
+ kernel,
92
+ authVerifier: async (request, route) => {
93
+ const token = request.headers.authorization?.replace('Bearer ', '')
94
+ return token ? await myIdentityLayer.verify(token) : false // e.g. { user: { userId }, perms: ['miner:w'] }
95
+ },
96
+ allowedHosts: ['mdk.example.com', '10.0.0.4:3000']
97
+ })
98
+ ```
99
+
100
+ ## Kernel connection
101
+
102
+ > [!NOTE]
103
+ > `startGateway()`, used throughout this section, is exported by [`@tetherto/mdk-core`](../mdk/README.md), not by this
104
+ > `@tetherto/mdk-gateway` package.
105
+
106
+ The Gateway opens no Kernel connection: each app runtime dials Kernel over HRPC (`@hyperswarm/rpc`) with its own keypair, using the
107
+ Kernel's listener public key. `startGateway()` still resolves that key
108
+ **before any boot side effects**, in this order:
109
+
110
+ 1. `kernelKey`: hex string or Buffer. Pass `kernelKey: false` to run without a Kernel key (useful when testing without a live Kernel).
111
+ 2. `kernel`: an in-process `KernelManager` handle; the key comes from `kernel.getPublicKey()`.
112
+ 3. Key file: `keyFile` (default: `DEFAULT_KEY_FILE`, i.e. `~/.mdk/.kernel-key`), which `getKernel()` publishes on start.
113
+ 4. If none resolves, `startGateway()` throws `ERR_KERNEL_KEY_FILE_NOT_FOUND`.
114
+
115
+ **Zero-config (same host, default)**: Start the Kernel with `getKernel()`, then `startGateway()` with no endpoint options: the
116
+ Gateway picks the key up from the key file automatically.
117
+
118
+ **Cross-host**: Obtain the Kernel listener key with `kernel.getPublicKey().toString('hex')` on the host running Kernel, then pass
119
+ it on the Gateway host:
120
+
121
+ ```js
122
+ await startGateway({ kernelKey: '<kernel-listener-pubkey-hex>' })
123
+ ```
124
+
125
+ For testnets, pass `bootstrap` to set the DHT bootstrap nodes of the Gateway's client to app runtimes (`ctx.kernelBootstrap`).
126
+
127
+ Kernel's allowlist (`auth.whitelist`) is fail-closed: empty admits nobody, so [each app runtime's Kernel public key must be admitted
128
+ before the connection is accepted](../kernel/README.md#caller-admission-and-roles). A runtime writes that key to its `kernelKeyFile`;
129
+ `mdk run` points it into the trusted-keys dir, where the Kernel admits it. It is derived from a seed persisted in the runtime's store,
130
+ so it survives restarts. `startGateway({ kernel })` from `@tetherto/mdk-core` also admits the Gateway's own DHT key (`status.rpcClientKey`)
131
+ on an in-process Kernel and publishes it to the trusted-keys dir for same-machine Kernels and Workers.
132
+
133
+ ## Security model
134
+
135
+ - **Routes marked `"auth": true` fail closed**: before body parsing, validation, the cache or the forward to the app runtime, the Gateway
136
+ calls the configured `authVerifier(request, route)`. `route` is `{ id, method, path, app, permissions }` from the manifest. A falsy return,
137
+ or no verifier at all, answers `401 ERR_AUTH_UNAUTHORIZED`; a verifier may throw its own `4xx` `ERR_*` error (for example
138
+ `403 ERR_AUTH_PERM_DENIED`), and any other throw is a `401`. A truthy return is the caller's identity: it is forwarded to the runtime and
139
+ reaches the controller as `req._info`. The Gateway ships no identity layer, so checking permissions against the identity stays with the
140
+ verifier or the controller ([auth and permissions](../../../docs/guides/gateway/plugins.md#auth-and-permissions))
141
+ - **Built-in `"loopback"` verifier** for single-machine stacks (`mdk run`, the examples): a caller whose TCP peer is
142
+ `127.0.0.0/8` or `::1` gets `{ user: { userId: 'local' } }`; anyone else gets `401`. It reads the socket, never
143
+ `X-Forwarded-For`, so do not use it behind a reverse proxy on the same host, where every proxied request is loopback.
144
+ A browser request also needs a loopback `Origin` (`localhost`, `127.0.0.1`, `[::1]` on any port) or, while the Host
145
+ allowlist is on, the Gateway's own origin, and must not carry `Sec-Fetch-Site: cross-site`, so a page on another site open
146
+ in the operator's browser cannot drive `auth: true` routes with a blind cross-site `POST`. With `allowedHosts: ["*"]` the
147
+ `Host` header is unchecked, so a same-origin `Origin` proves nothing (a DNS-rebound page is "same origin" with itself) and
148
+ only loopback Origins, or none, are accepted
149
+ - **Routes without `"auth": true` are open**: any caller that passes the Host check reaches them, with `req._info` set to `{}`
150
+ - **Host allowlist**: a request whose `Host` header is not in `allowedHosts` (loopback on the bound port by default) gets
151
+ `403 ERR_GATEWAY_HOST_NOT_ALLOWED`, which blocks DNS-rebinding pages from reading a gateway on `localhost`. Add the names
152
+ your clients use when you bind to a LAN address or put the Gateway behind a proxy
153
+ - **Limits and headers**: rate limiting (`429 ERR_RATE_LIMITED`, counted before the auth check so failed attempts are throttled), a
154
+ configurable `bodyLimit`, and Helmet security headers are on by default. `trustProxy` is off. There is no `GET /echo` route
155
+ - **Input validation**: path, query and header `parameters` and the `application/json` `requestBody` schema are validated before the request
156
+ is forwarded
157
+ - **Per-caller cache on `auth: true` routes**: a `cache` route behind auth adds a SHA-256 of the verified identity (`req._info`) to
158
+ its cache key, so two callers never share a cached response or an in-flight call. Open routes keep one shared entry per key
159
+ - **Cache refresh**: `?overwriteCache=true` bypasses a cached response only for a caller the `authVerifier` returned with `cache:overwrite`
160
+ in its `perms` array
161
+ - **App code runs outside the Gateway**: the Gateway reads only each app's route table; the controllers run in the app's own runtime
162
+ process, and a manifest `handler` must resolve inside its plugin directory
163
+ - **App runtime security**: a runtime admits only the public keys in its `allowedKeys`/`allowedKeyFiles`. The Gateway presents one
164
+ key to every runtime, written to `gatewayKeyFile` (and `status.appClientKey`) and derived from a seed at
165
+ `<root>/identity/seedRuntimeClient` (`0600`), so it survives restarts. The key file is replaced atomically with mode `0644`
166
+ in a directory created `0700`, since runtimes refuse an allowed-key file that is group/other-writable or owned by another user
167
+ - **Static files**: static serving goes through `@fastify/static`, which `@tetherto/svc-facs-httpd@2.0.1` pins to `^9`. That range carries
168
+ GHSA-8pvw-jcv7-9cmj and GHSA-83w8-p2f5-377r. Add the override in [Known dependency advisory](#known-dependency-advisory) to your
169
+ app, or leave `staticRootPath` unset in production and serve the UI from a separate web server or proxy
170
+ - **Kernel connection security**: the HRPC connection is an encrypted Noise channel. Kernel's HRPC firewall is fail-closed: each app
171
+ runtime's Kernel public key (its `kernelKeyFile`, stable across restarts) must be admitted (`auth.whitelist`, `kernel.allowKey()`, or
172
+ the trusted-keys dir). Workers that app clients call directly must allow the same key (`allowedKeys`).
173
+ See [Kernel Transport](../kernel/README.md#transports) and the `auth-whitelist` example
174
+ for the key exchange pattern
175
+ - Once connected, Kernel trusts all messages from an app runtime implicitly, apart from the device-family write permissions it requires in the
176
+ `authPerms` array accompanying each write action
177
+ - Human and AI callers reach the same routes on the same terms, since the Gateway distinguishes neither
178
+
179
+ ## Known dependency advisory
180
+
181
+ A fresh install of `@tetherto/mdk-gateway` (and of `@tetherto/mdk-core` and `@tetherto/mdk-mcp`, which depend on it) resolves
182
+ `@fastify/static@9.x` through `@tetherto/svc-facs-httpd@2.0.1`. `npm audit` then reports one high advisory
183
+ (GHSA-8pvw-jcv7-9cmj, GHSA-83w8-p2f5-377r) on `@fastify/static`, and moderate ones on the packages that pull it in. The
184
+ vulnerable code runs only when `staticRootPath` is set; static serving is off by default.
185
+
186
+ The fix needs a `svc-facs-httpd` release on `@fastify/static ^10.1.2`. Until then, add an override to your app's **root**
187
+ `package.json`. npm applies `overrides` only from the project being installed, so this package cannot set it for you:
188
+
189
+ ```json
190
+ {
191
+ "overrides": {
192
+ "@fastify/static": "^10.1.2"
193
+ }
194
+ }
195
+ ```
196
+
197
+ Then reinstall and confirm:
198
+
199
+ ```bash
200
+ npm install
201
+ npm ls @fastify/static # @fastify/static@10.x under @tetherto/svc-facs-httpd
202
+ npm audit --omit=dev # no @fastify/static advisory
203
+ ```
204
+
205
+ `svc-facs-httpd` uses `@fastify/static` only through `register(...)` with `root`, `prefix` and `constraints`, plus `reply.sendFile`
206
+ for the 404 page, and these work unchanged on 10.x. Remove the override once a `svc-facs-httpd` release requires `^10`.
207
+
208
+ ## Extend the Gateway
209
+
210
+ ### Apps (recommended)
211
+
212
+ Pass each app's directory and the file its runtime writes its key to via `apps` to mount its routes at startup:
213
+
214
+ ```js
215
+ await startGateway({
216
+ kernel,
217
+ gatewayKeyFile: './run/gateway.key',
218
+ apps: [
219
+ { name: 'my-metrics', appDir: path.join(__dirname, 'apps/my-metrics'), keyFile: './run/my-metrics.key' }
220
+ ]
221
+ })
222
+ ```
223
+
224
+ Run each app with [`createAppRuntime()`](../app-runtime/README.md) and `allowedKeyFiles: ['./run/gateway.key']`. Controllers receive
225
+ `(req)`, and each builds its own `@tetherto/mdk-client` from the context config it reads via `require('@tetherto/mdk-gateway/plugin')`.
226
+ The plugins MDK ships run the same way, only when a stack declares them; nothing auto-registers.
227
+
228
+ The [plugin authoring guide](../../../docs/guides/gateway/plugins.md) and the plugin reference cover the full
229
+ manifest schema, controller contract, and plugin context, and [what the loader throws](#errors) is documented here.
230
+
231
+ ### Raw Fastify routes
232
+
233
+ For one-off handlers that do not need the plugin manifest format, pass `additionalRoutes` directly:
234
+
235
+ ```js
236
+ await startGateway({
237
+ kernel,
238
+ additionalRoutes: [
239
+ {
240
+ method: 'GET',
241
+ url: '/custom/endpoint',
242
+ handler: async (req, reply) => { return { ok: true } }
243
+ }
244
+ ]
245
+ })
246
+ ```
247
+
248
+ These are registered as plain Fastify routes: no app runtime and no manifest validation. Unlike an app controller, the handler
249
+ receives the Fastify `reply`, so this is the way to control status codes.
250
+
251
+ ## Errors
252
+
253
+ [`loadPlugin()`](../app-runtime/lib/plugin-loader.js) validates every manifest at startup (the Gateway reads route tables only; an app
254
+ runtime also loads the handlers) and throws on the first problem. The `apps` option errors are in the
255
+ [`startGateway()` reference](../mdk/README.md):
256
+
257
+ | Code | Fires when | Fix |
258
+ | --- | --- | --- |
259
+ | `ERR_PLUGIN_MANIFEST_MISSING` | No `mdk-plugin.json` in the plugin directory | Add the manifest, or fix the app's `appDir` |
260
+ | `ERR_PLUGIN_MANIFEST_INVALID` | The manifest fails to parse as JSON, parses to a non-object value (e.g. a bare string or number), or is missing `name`, `version`, `routes`, or a route is missing `id`/`handler`/a valid `method`/`path`, has a `path` that does not start with `/`, or has a non-boolean `auth`. A JSON array manifest passes the object check and fails on the missing `name` field instead — same code, different message. | Fix the field the error message names |
261
+ | `ERR_PLUGIN_ROUTE_DUPLICATE_ID` | Two routes in the same manifest share an `id` | Rename one of the duplicate route ids |
262
+ | `ERR_PLUGIN_ROUTE_DUPLICATE_PATH` | Two routes in the same manifest share a method and path (`/x/{id}` and `/x/:id` are the same path) | Remove or move one of them |
263
+ | `ERR_PLUGIN_HANDLER_NOT_FOUND` | A route's `handler` file path doesn't exist | Fix the `handler` path for that route |
264
+ | `ERR_PLUGIN_HANDLER_LOAD_FAILED` | The `handler` file exists but cannot load: a dependency is not installed, or it throws when required. The message carries the cause | Install the app's dependencies, or fix the error the message names |
265
+ | `ERR_PLUGIN_HANDLER_NOT_FUNCTION` | A route's resolved handler module (or named export) isn't a function | Export a function from the file |
266
+ | `ERR_PLUGIN_HANDLER_OUTSIDE_PLUGIN` | A route's `handler` is absolute or resolves outside the plugin directory | Move the handler into the plugin and use a relative path |
267
+
268
+ Booting the Gateway also throws `ERR_GATEWAY_AUTH_VERIFIER_INVALID`, `ERR_GATEWAY_ALLOWED_HOSTS_INVALID` or `ERR_GATEWAY_BODY_LIMIT_INVALID`
269
+ when the matching [option](#configuration) has the wrong type.
270
+
271
+ ## Directory layout
272
+
273
+ ```text
274
+ gateway/
275
+ ├── workers/
276
+ │ ├── http.node.wrk.js # WrkServerHttp — Fastify worker, mounts app routes
277
+ │ └── lib/
278
+ │ ├── plugin-loader.js # Re-exports @tetherto/mdk-app-runtime's manifest loader
279
+ │ ├── plugin-adapter.js # Converts app routes to Fastify handlers that forward to the runtime, applies caching
280
+ │ ├── plugin-gateway.js # Builds a plugin's context ({ config }), for the MCP server
281
+ │ ├── security.js # authVerifier hook, Host allowlist, hardening defaults
282
+ │ ├── constants.js
283
+ │ ├── utils.js
284
+ │ └── server/lib/ # cachedRoute.js and send200.js, used by the adapter
285
+ ├── config/
286
+ │ └── facs/ # Example config files (*.json.example)
287
+ ├── db/ # Local store directory (identity seeds)
288
+ └── tests/
289
+ ├── unit/lib/ # One suite per lib module
290
+ └── integration/
291
+ ├── api.test.js # HTTP route tests
292
+ └── app-forwarding.test.js # The Gateway against real app runtimes
293
+ ```
294
+
295
+ ## Next steps
296
+
297
+ - [Run the Gateway](../../../docs/guides/gateway/run.md)
298
+ - [Secure the site](../../../docs/guides/security/index.md): admit this Gateway to Kernel and lock down the fleet
299
+ - [Add routes with the plugin system](../../../docs/guides/gateway/plugins.md)
300
+ - Browse the [supported plugins reference](../../../docs/reference/supported-plugins.md)
301
+ - See a complete [worked example](../../../examples/mvp-site/README.md)
302
+ - Browse the [`startGateway()` options](../mdk/README.md)
303
+ - Reach MDK over MCP — run the standalone [`@tetherto/mdk-mcp`](../mcp/README.md) process (`mdk run mcp`), which derives
304
+ tools from this Gateway's own plugins without the Gateway hosting any MCP code itself
305
+ - Skip the HTTP surface and plugin system entirely by [connecting to Kernel directly](../client/README.md) with
306
+ `@tetherto/mdk-client` — viable for a background service that only dispatches commands, though most applications build on the Gateway
@@ -0,0 +1,15 @@
1
+ {
2
+ "dir_log": "logs",
3
+ "debug": 0,
4
+ "trustProxy": false,
5
+ "allowedHosts": [],
6
+ "bodyLimit": 1048576,
7
+ "rateLimit": {
8
+ "max": 300,
9
+ "timeWindow": "1 minute"
10
+ },
11
+ "ttl": 300,
12
+ "site": "SITE_NAME",
13
+ "rpcTimeout": 15000,
14
+ "appTimeoutMs": 60000
15
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "h0": {
3
+ }
4
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "r0": {
3
+ "allow": []
4
+ }
5
+ }
File without changes
package/package.json CHANGED
@@ -1 +1,69 @@
1
- {"name":"@tetherto/mdk-gateway","version":"0.0.0"}
1
+ {
2
+ "name": "@tetherto/mdk-gateway",
3
+ "version": "0.12.1",
4
+ "description": "MDK Gateway",
5
+ "keywords": [
6
+ "tether"
7
+ ],
8
+ "author": "Tether",
9
+ "license": "Apache-2.0",
10
+ "engines": {
11
+ "node": ">=24"
12
+ },
13
+ "exports": {
14
+ "./plugin": "./plugin.js",
15
+ "./workers/lib/plugin-loader": "./workers/lib/plugin-loader.js",
16
+ "./workers/lib/plugin-adapter": "./workers/lib/plugin-adapter.js",
17
+ "./workers/lib/plugin-gateway": "./workers/lib/plugin-gateway.js",
18
+ "./workers/http.node.wrk": "./workers/http.node.wrk.js",
19
+ "./package.json": "./package.json"
20
+ },
21
+ "scripts": {
22
+ "lint": "standard",
23
+ "lint:fix": "standard --fix",
24
+ "test": "npm run lint && npm run test:unit && npm run test:integration",
25
+ "test:unit": "NODE_ENV=test brittle tests/unit/**/*.test.js",
26
+ "test:integration": "NODE_ENV=test npx brittle tests/integration/*.test.js",
27
+ "test:api": "NODE_ENV=test npx brittle tests/integration/api.test.js",
28
+ "test:watch": "brittle tests/unit/**/*.js --watch",
29
+ "test:coverage": "NODE_ENV=test brittle --coverage 'tests/**/*.test.js'",
30
+ "dev": "node worker.js --wtype wrk-node-http --env development --port 3000",
31
+ "start": "node worker.js --wtype wrk-node-http --env production --port 3000"
32
+ },
33
+ "dependencies": {
34
+ "@bitfinex/bfx-facs-lru": "^1.0.0",
35
+ "@bitfinex/bfx-svc-boot-js": "1.2.0",
36
+ "@fastify/helmet": "13.1.1",
37
+ "@fastify/rate-limit": "11.2.0",
38
+ "@tetherto/mdk-app-runtime": "^0.12.1",
39
+ "@tetherto/svc-facs-httpd": "^2.0.0",
40
+ "@tetherto/tether-wrk-base": "^2.0.0",
41
+ "async": "3.2.6",
42
+ "debug": "4.4.1",
43
+ "pino": "10.3.1",
44
+ "pino-pretty": "13.1.2"
45
+ },
46
+ "devDependencies": {
47
+ "@tetherto/mdk-core": "^0.12.1",
48
+ "@tetherto/tether-svc-test-helper": "^1.0.0",
49
+ "brittle": "3.18.0",
50
+ "http-server": "14.1.1",
51
+ "hyperdht": "6.34.1",
52
+ "standard": "17.1.0"
53
+ },
54
+ "repository": {
55
+ "type": "git",
56
+ "url": "git+https://github.com/tetherto/mdk.git",
57
+ "directory": "backend/core/gateway"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ },
62
+ "files": [
63
+ "worker.js",
64
+ "plugin.js",
65
+ "workers/",
66
+ "config/**/*.example",
67
+ "README.md"
68
+ ]
69
+ }
package/plugin.js ADDED
@@ -0,0 +1,16 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * The per-plugin context, required by gateway plugin files as
5
+ * `require('@tetherto/mdk-gateway/plugin')` and resolving to that plugin's
6
+ * frozen context: config, logger and onReady — clients are plugin-owned [per the full surface](./workers/lib/plugin-gateway.js).
7
+ *
8
+ * Inside a plugin the request never reaches [`./workers/lib/plugin-loader.js`](./workers/lib/plugin-loader.js),
9
+ * the module context an app runtime loads each plugin through, intercepts it and hands
10
+ * back the context the plugin was registered with.
11
+ * This file exists to make the specifier resolvable and to fail with a pointer
12
+ * at the plugin loader when a plugin module is required outside a runtime — a
13
+ * test requiring a controller directly, say — instead of leaving the plugin
14
+ * with `undefined` where its context should be.
15
+ */
16
+ throw new Error('ERR_NO_PLUGIN_CONTEXT: @tetherto/mdk-gateway/plugin is only available inside a gateway plugin; load the plugin through an app runtime (createAppRuntime) or loadPlugin(dir, context) from @tetherto/mdk-gateway/workers/lib/plugin-loader')
package/worker.js ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict'
2
+
3
+ // used to spawn a `bfx-svc-js` service. Contains the worker CLI.
4
+ const worker = require('@bitfinex/bfx-svc-boot-js')
5
+ module.exports = worker