@capybara-hq/kernel 0.1.0-rc.68 → 0.1.0-rc.69
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/dist/admin/admin.module.js +2 -0
- package/dist/admin/health.service.d.ts +11 -4
- package/dist/admin/health.service.js +33 -17
- package/dist/background/poll.job.js +2 -2
- package/dist/background/tasks.service.d.ts +5 -5
- package/dist/background/tasks.service.js +9 -8
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/requests.service.js +1 -1
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/schema.js +9 -9
- package/dist/conversations/conversations.controller.d.ts +8 -3
- package/dist/conversations/conversations.controller.js +8 -3
- package/dist/conversations/conversations.service.d.ts +33 -15
- package/dist/conversations/conversations.service.js +60 -10
- package/dist/conversations/events.d.ts +21 -41
- package/dist/conversations/events.js +57 -31
- package/dist/conversations/permission-profiles.controller.d.ts +6 -4
- package/dist/conversations/permission-profiles.service.d.ts +6 -4
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +77 -6
- package/dist/ctx/device-provider-check.d.ts +6 -2
- package/dist/ctx/device-provider-check.js +17 -2
- package/dist/db/kernel-schema.d.ts +10 -48
- package/dist/devices/access.d.ts +3 -4
- package/dist/devices/access.js +3 -4
- package/dist/devices/conversation-devices.d.ts +1 -3
- package/dist/devices/conversation-devices.js +2 -7
- package/dist/devices/device-projects.service.js +24 -29
- package/dist/devices/device-provider-selection.d.ts +5 -6
- package/dist/devices/device-provider-selection.js +8 -9
- package/dist/devices/devices.controller.d.ts +8 -21
- package/dist/devices/devices.controller.js +9 -34
- package/dist/devices/devices.module.js +6 -4
- package/dist/devices/devices.schemas.d.ts +1 -5
- package/dist/devices/devices.schemas.js +1 -5
- package/dist/devices/devices.service.d.ts +84 -147
- package/dist/devices/devices.service.js +134 -229
- package/dist/devices/dto.d.ts +5 -32
- package/dist/devices/dto.js +5 -37
- package/dist/devices/errors.d.ts +6 -5
- package/dist/devices/errors.js +7 -8
- package/dist/devices/project-rules.d.ts +9 -11
- package/dist/devices/project-rules.js +7 -7
- package/dist/devices/working-root.d.ts +3 -6
- package/dist/devices/working-root.js +3 -8
- package/dist/dial-in/access.d.ts +23 -0
- package/dist/dial-in/access.js +64 -0
- package/dist/{local-devices/device-auth.controller.d.ts → dial-in/auth.controller.d.ts} +12 -6
- package/dist/{local-devices/device-auth.controller.js → dial-in/auth.controller.js} +30 -15
- package/dist/dial-in/auth.module.d.ts +7 -0
- package/dist/dial-in/auth.module.js +27 -0
- package/dist/{local-devices/device-auth.d.ts → dial-in/auth.service.d.ts} +42 -13
- package/dist/{local-devices/device-auth.js → dial-in/auth.service.js} +83 -32
- package/dist/dial-in/dial-in.module.d.ts +9 -0
- package/dist/dial-in/dial-in.module.js +37 -0
- package/dist/{local-devices → dial-in}/dto.d.ts +17 -2
- package/dist/{local-devices → dial-in}/dto.js +14 -2
- package/dist/dial-in/envelope.d.ts +95 -0
- package/dist/dial-in/envelope.js +89 -0
- package/dist/dial-in/gateway.d.ts +214 -0
- package/dist/{local-devices/local-device-gateway.js → dial-in/gateway.js} +337 -181
- package/dist/dial-in/handlers.d.ts +47 -0
- package/dist/dial-in/handlers.js +84 -0
- package/dist/dial-in/machines.d.ts +155 -0
- package/dist/dial-in/machines.js +383 -0
- package/dist/dial-in/relay.d.ts +135 -0
- package/dist/dial-in/relay.js +444 -0
- package/dist/dial-in/relay.module.d.ts +7 -0
- package/dist/dial-in/relay.module.js +24 -0
- package/dist/{local-devices/device-routes.d.ts → dial-in/routes.d.ts} +19 -11
- package/dist/{local-devices/device-routes.js → dial-in/routes.js} +45 -25
- package/dist/{local-devices → dial-in}/targets.d.ts +3 -18
- package/dist/{local-devices → dial-in}/targets.js +3 -48
- package/dist/errors.d.ts +8 -27
- package/dist/errors.js +8 -27
- package/dist/extensions/bootstrap.d.ts +5 -0
- package/dist/extensions/bootstrap.js +10 -1
- package/dist/extensions/extensions.module.js +6 -0
- package/dist/extensions/host.d.ts +6 -0
- package/dist/extensions/host.js +12 -2
- package/dist/extensions/permission-set.d.ts +15 -1
- package/dist/extensions/permission-set.js +11 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/http/api.module.js +4 -4
- package/dist/http/gateway-routes.d.ts +1 -1
- package/dist/http/gateway-routes.js +1 -1
- package/dist/http/meta.controller.js +1 -1
- package/dist/identity/settings.d.ts +1 -1
- package/dist/identity/settings.js +6 -8
- package/dist/infra/config/kernel-config.service.js +2 -2
- package/dist/infra/outbox/registry.d.ts +0 -14
- package/dist/infra/outbox/registry.js +0 -15
- package/dist/infra/throttler/named-throttlers.d.ts +1 -1
- package/dist/infra/throttler/named-throttlers.js +1 -1
- package/dist/kernel.module.js +0 -3
- package/dist/orgs/settings.service.js +5 -10
- package/dist/overrides.d.ts +0 -4
- package/dist/overrides.js +0 -2
- package/dist/platform/exclusions.js +5 -5
- package/dist/platform/ops/admin.ops.js +1 -1
- package/dist/platform/ops/conversations.ops.js +5 -2
- package/dist/platform/ops/devices.ops.js +11 -24
- package/dist/platform/ops/inbox.ops.js +1 -1
- package/dist/roles/gateway-role.d.ts +3 -3
- package/dist/roles/gateway-role.js +4 -4
- package/dist/roles/gateway.module.d.ts +1 -1
- package/dist/roles/gateway.module.js +5 -5
- package/dist/runtime/context/device-paths.d.ts +4 -1
- package/dist/runtime/context/permission-profile.js +5 -5
- package/dist/runtime/maintenance.d.ts +2 -5
- package/dist/runtime/maintenance.js +9 -19
- package/dist/runtime/project/knowledge.js +4 -5
- package/dist/runtime/project/knowledge.service.js +2 -1
- package/dist/sandbox/cloud-permission-snapshot.d.ts +1 -1
- package/dist/sandbox/cloud-permission-snapshot.js +1 -3
- package/dist/sandbox/device-reports.d.ts +74 -14
- package/dist/sandbox/device-reports.js +190 -33
- package/dist/sandbox/device-reports.module.d.ts +7 -0
- package/dist/sandbox/device-reports.module.js +25 -0
- package/dist/sandbox/device-sandbox.d.ts +39 -41
- package/dist/sandbox/device-sandbox.js +44 -83
- package/dist/sandbox/kernel-read.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.d.ts +11 -59
- package/dist/sandbox/legacy-retirement.js +34 -186
- package/dist/sandbox/legacy-retirement.module.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.module.js +2 -2
- package/dist/sandbox/managed-device-context.d.ts +16 -4
- package/dist/sandbox/managed-device-context.js +25 -8
- package/dist/sandbox/managed-device-context.module.js +4 -0
- package/dist/sandbox/modes.d.ts +3 -12
- package/dist/sandbox/modes.js +2 -14
- package/dist/{local-devices → sandbox}/network-grants.d.ts +2 -21
- package/dist/{local-devices → sandbox}/network-grants.js +4 -49
- package/dist/sandbox/operation-authority-types.d.ts +2 -1
- package/dist/sandbox/operation-authority.d.ts +6 -20
- package/dist/sandbox/operation-authority.js +43 -104
- package/dist/sandbox/operation-authority.module.js +1 -1
- package/dist/sandbox/operation-intents.d.ts +4 -1
- package/dist/sandbox/operation-intents.js +11 -2
- package/dist/sandbox/operation-reconcile.job.js +2 -4
- package/dist/sandbox/permission-availability.d.ts +6 -7
- package/dist/sandbox/permission-availability.js +7 -10
- package/dist/sandbox/permission-call-grants.d.ts +2 -1
- package/dist/sandbox/permission-call-grants.js +1 -7
- package/dist/sandbox/permission-call-types.d.ts +4 -4
- package/dist/sandbox/permission-ceiling-writes.d.ts +7 -23
- package/dist/sandbox/permission-ceiling-writes.js +8 -35
- package/dist/sandbox/permission-config-publication.d.ts +9 -1
- package/dist/sandbox/permission-config-publication.js +29 -21
- package/dist/sandbox/permission-context-types.d.ts +17 -10
- package/dist/sandbox/permission-facts.d.ts +98 -95
- package/dist/sandbox/permission-facts.js +160 -314
- package/dist/sandbox/permission-instance-writes.d.ts +23 -37
- package/dist/sandbox/permission-instance-writes.js +44 -121
- package/dist/sandbox/permission-runtime-reports.d.ts +38 -30
- package/dist/sandbox/permission-runtime-reports.js +105 -122
- package/dist/sandbox/providers.d.ts +26 -38
- package/dist/sandbox/providers.js +36 -18
- package/dist/sandbox/request-not-sent.d.ts +2 -13
- package/dist/sandbox/request-not-sent.js +2 -13
- package/dist/sandbox/review/permission-candidate.d.ts +13 -8
- package/dist/sandbox/review/permission-candidate.js +38 -48
- package/dist/sandbox/sandbox.module.js +7 -3
- package/dist/sandbox/sandbox.service.d.ts +49 -59
- package/dist/sandbox/sandbox.service.js +139 -259
- package/dist/schedules/fire.d.ts +15 -6
- package/dist/schedules/fire.js +27 -13
- package/dist/schedules/grant-rules.js +1 -1
- package/dist/schedules/scheduled-tasks.service.d.ts +0 -7
- package/dist/schedules/scheduled-tasks.service.js +1 -4
- package/dist/schedules/schedules.module.js +5 -3
- package/dist/skills/invoke.d.ts +1 -1
- package/dist/skills/invoke.js +2 -3
- package/dist/tools/approvals.service.d.ts +8 -5
- package/dist/tools/approvals.service.js +5 -5
- package/dist/tools/builtins/sandbox/bash.js +1 -2
- package/dist/tools/builtins/sandbox/common.d.ts +15 -9
- package/dist/tools/builtins/sandbox/common.js +37 -38
- package/dist/tools/network-asks.d.ts +19 -22
- package/dist/tools/network-asks.js +15 -21
- package/dist/tools/pipeline.d.ts +6 -7
- package/dist/tools/pipeline.js +17 -21
- package/dist/tools/tools.module.js +3 -7
- package/package.json +3 -4
- package/src/migrations/0177_dial_in_contract.sql +32 -0
- package/dist/local-devices/device-auth.module.d.ts +0 -7
- package/dist/local-devices/device-auth.module.js +0 -25
- package/dist/local-devices/device-policy.service.d.ts +0 -213
- package/dist/local-devices/device-policy.service.js +0 -605
- package/dist/local-devices/instance-bindings.d.ts +0 -82
- package/dist/local-devices/instance-bindings.js +0 -307
- package/dist/local-devices/local-device-gateway.d.ts +0 -153
- package/dist/local-devices/local-device-provider.d.ts +0 -122
- package/dist/local-devices/local-device-provider.js +0 -326
- package/dist/local-devices/local-devices.module.d.ts +0 -31
- package/dist/local-devices/local-devices.module.js +0 -100
- package/dist/local-devices/machines.d.ts +0 -211
- package/dist/local-devices/machines.js +0 -766
- package/dist/local-devices/owner-report.d.ts +0 -81
- package/dist/local-devices/owner-report.js +0 -150
- package/dist/local-devices/owner-reports.d.ts +0 -48
- package/dist/local-devices/owner-reports.js +0 -208
- package/dist/local-devices/policy-op-jobs.d.ts +0 -15
- package/dist/local-devices/policy-op-jobs.js +0 -12
- package/dist/local-devices/policy-summary.d.ts +0 -102
- package/dist/local-devices/policy-summary.js +0 -155
- package/dist/local-devices/relay.d.ts +0 -166
- package/dist/local-devices/relay.js +0 -559
- package/dist/tools/device-op-resolver.d.ts +0 -34
- package/dist/tools/device-op-resolver.js +0 -120
- /package/dist/{local-devices → dial-in}/keys.d.ts +0 -0
- /package/dist/{local-devices → dial-in}/keys.js +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.d.ts +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.js +0 -0
|
@@ -8,19 +8,21 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
10
|
import { randomUUID } from "node:crypto";
|
|
11
|
-
import {
|
|
11
|
+
import { DIAL_IN_PROTOCOL_RE, parseDialInMachineFrame, } from "@capybara-hq/plugin-sdk";
|
|
12
12
|
import { Inject, Injectable } from "@nestjs/common";
|
|
13
13
|
import { KernelConfigService } from "../infra/config/kernel-config.service.js";
|
|
14
14
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
15
15
|
import { KERNEL_BUS, KERNEL_LOGGER, REDIS } from "../infra/tokens.js";
|
|
16
|
+
import { storedResult, wireFrameOf } from "./envelope.js";
|
|
17
|
+
import { DialInHandlers } from "./handlers.js";
|
|
16
18
|
import { DEVICE_TAKEN_PREFIX, deviceLeaseKey, deviceReqKey, deviceReqTopic, deviceResKey, deviceResTopic, deviceRevokedTopic, deviceTakenKey, deviceTakeoverTopic, deviceTransportKey, } from "./keys.js";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
+
import { DialInMachines } from "./machines.js";
|
|
20
|
+
import { LIST_TTL_MARGIN_MS } from "./relay.js";
|
|
19
21
|
/** WebSocket close codes the gateway uses (the program reads 4002 and 4003). */
|
|
20
22
|
export const DEVICE_CLOSE = {
|
|
21
23
|
/** The kernel is shutting down: reconnect (another gateway replica takes over). */
|
|
22
24
|
shutdown: 1001,
|
|
23
|
-
/** A frame larger than `devices.
|
|
25
|
+
/** A frame larger than `devices.dialIn.maxFrameBytes` (sent by `ws` itself). */
|
|
24
26
|
tooBig: 1009,
|
|
25
27
|
/** The kernel failed (e.g. the database): reconnect. */
|
|
26
28
|
internal: 1011,
|
|
@@ -30,12 +32,15 @@ export const DEVICE_CLOSE = {
|
|
|
30
32
|
replaced: 4002,
|
|
31
33
|
/** The machine's credential was revoked: the program exits. */
|
|
32
34
|
revoked: 4003,
|
|
33
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* No valid `hello` first, a frame that is no valid envelope or does not fit the negotiated
|
|
37
|
+
* capacity, or the provider refused the hello (the reason carries its code).
|
|
38
|
+
*/
|
|
34
39
|
protocol: 4400,
|
|
35
40
|
/** The lease could not be claimed in time: reconnect. */
|
|
36
41
|
claimTimeout: 4409,
|
|
37
42
|
};
|
|
38
|
-
/** How long the program has to send `hello` after the upgrade. */
|
|
43
|
+
/** How long the program has to send `hello` after the upgrade, and the provider to answer it. */
|
|
39
44
|
export const HELLO_TIMEOUT_MS = 10_000;
|
|
40
45
|
/** How often a connection is pinged; two pings without a pong close it. */
|
|
41
46
|
export const PING_INTERVAL_MS = 20_000;
|
|
@@ -50,6 +55,12 @@ const TOUCH_EVERY_MS = 30_000;
|
|
|
50
55
|
* transport key lives three pings, so two misses still leave it readable).
|
|
51
56
|
*/
|
|
52
57
|
const MAX_PUBLISH_FAILURES = 3;
|
|
58
|
+
/**
|
|
59
|
+
* The most `event` frames a connection may have waiting for the provider (received, not yet
|
|
60
|
+
* handled): a machine sending faster than its provider handles them is closed (4400) instead of
|
|
61
|
+
* growing the gateway's memory.
|
|
62
|
+
*/
|
|
63
|
+
export const MAX_EVENTS_IN_FLIGHT = 32;
|
|
53
64
|
/** How often a held connection re-checks its credential (membership, org, device). */
|
|
54
65
|
const REAUTH_EVERY_MS = 60_000;
|
|
55
66
|
/**
|
|
@@ -97,6 +108,13 @@ redis.call('DEL', KEYS[1])
|
|
|
97
108
|
return 1
|
|
98
109
|
`;
|
|
99
110
|
const errMsg = (e) => (e instanceof Error ? e.message : String(e));
|
|
111
|
+
/** A WebSocket close reason is at most 123 UTF-8 bytes. */
|
|
112
|
+
const closeReason = (text) => {
|
|
113
|
+
let out = text;
|
|
114
|
+
while (Buffer.byteLength(out) > 123)
|
|
115
|
+
out = out.slice(0, -1);
|
|
116
|
+
return out;
|
|
117
|
+
};
|
|
100
118
|
/**
|
|
101
119
|
* One machine connection on this replica. Its state (the socket, the lease handle, the requests
|
|
102
120
|
* it forwarded) is the socket's own and lives exactly as long as the socket.
|
|
@@ -106,6 +124,7 @@ class DeviceConnection {
|
|
|
106
124
|
socket;
|
|
107
125
|
identity;
|
|
108
126
|
credential;
|
|
127
|
+
protocol;
|
|
109
128
|
token = randomUUID();
|
|
110
129
|
state = "hello";
|
|
111
130
|
lease;
|
|
@@ -119,19 +138,31 @@ class DeviceConnection {
|
|
|
119
138
|
chain = Promise.resolve();
|
|
120
139
|
/** Resolves once the lease is handed back (or was never held). */
|
|
121
140
|
released;
|
|
122
|
-
/** The hello's
|
|
123
|
-
|
|
124
|
-
|
|
141
|
+
/** The hello's handling, then each event's, one after another (never rejects). */
|
|
142
|
+
handled = Promise.resolve();
|
|
143
|
+
/** Aborted when the connection closes: the provider's `onHello` stops waiting. */
|
|
144
|
+
ended = new AbortController();
|
|
145
|
+
/** The provider whose `onHello` accepted this connection: it gets `onClose`. */
|
|
146
|
+
accepted;
|
|
147
|
+
/** `event` frames received and not yet handled ({@link MAX_EVENTS_IN_FLIGHT}). */
|
|
148
|
+
eventsInFlight = 0;
|
|
149
|
+
/** Why the connection closed, for the provider's `onClose` (offline unless set). */
|
|
150
|
+
closeReason = "offline";
|
|
151
|
+
constructor(gw, socket, identity, credential,
|
|
152
|
+
/** The protocol the route admitted (the serving provider's `permissions.dialIn.protocol`). */
|
|
153
|
+
protocol) {
|
|
125
154
|
this.gw = gw;
|
|
126
155
|
this.socket = socket;
|
|
127
156
|
this.identity = identity;
|
|
128
157
|
this.credential = credential;
|
|
158
|
+
this.protocol = protocol;
|
|
129
159
|
}
|
|
130
160
|
/**
|
|
131
161
|
* The id a connection fences requests and results with (sent to the machine in `ready`).
|
|
132
162
|
* Random and separate from the lease token, which stays internal to the kernel.
|
|
133
163
|
*/
|
|
134
164
|
connectionId = randomUUID();
|
|
165
|
+
/** The negotiated capacity: the smaller of the machine's and the kernel's frame limits. */
|
|
135
166
|
effectiveCap;
|
|
136
167
|
/** The program run the hello named (`programRunId`), published with the capacity. */
|
|
137
168
|
programRunId;
|
|
@@ -184,9 +215,9 @@ class DeviceConnection {
|
|
|
184
215
|
async onFrame(text) {
|
|
185
216
|
let frame;
|
|
186
217
|
try {
|
|
187
|
-
//
|
|
188
|
-
//
|
|
189
|
-
frame =
|
|
218
|
+
// the envelope only (the body is the provider's), bounded by the negotiated capacity once
|
|
219
|
+
// the hello set it, by the kernel's own limit before
|
|
220
|
+
frame = parseDialInMachineFrame(text, this.effectiveCap ?? this.gw.maxFrameBytes());
|
|
190
221
|
if (this.effectiveCap !== undefined && this.gw.maxFrameBytes() < this.effectiveCap)
|
|
191
222
|
throw new Error("Frame capacity changed; reconnect");
|
|
192
223
|
}
|
|
@@ -203,10 +234,11 @@ class DeviceConnection {
|
|
|
203
234
|
this.close(DEVICE_CLOSE.protocol, "hello sent twice");
|
|
204
235
|
return;
|
|
205
236
|
}
|
|
206
|
-
if (frame.protocol !==
|
|
207
|
-
this.close(DEVICE_CLOSE.protocol,
|
|
237
|
+
if (frame.protocol !== this.protocol) {
|
|
238
|
+
this.close(DEVICE_CLOSE.protocol, `unsupported protocol; expected ${this.protocol}`);
|
|
208
239
|
return;
|
|
209
240
|
}
|
|
241
|
+
// the machine's announced limit never raises the kernel's own
|
|
210
242
|
this.effectiveCap = Math.min(frame.transport.maxFrameBytes, this.gw.maxFrameBytes());
|
|
211
243
|
if (this.effectiveCap < 1024 || Buffer.byteLength(text) > this.effectiveCap) {
|
|
212
244
|
this.close(DEVICE_CLOSE.protocol, "Check frame limits: hello cannot fit");
|
|
@@ -215,69 +247,160 @@ class DeviceConnection {
|
|
|
215
247
|
clearTimeout(this.helloTimer);
|
|
216
248
|
this.programRunId = frame.programRunId;
|
|
217
249
|
this.state = "claiming";
|
|
218
|
-
// set before the first await:
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
try {
|
|
223
|
-
// the hello carries the authenticated owner report and runtime facts: recorded with
|
|
224
|
-
// the connection it opened (the id the machine gets in `ready`)
|
|
225
|
-
await this.gw.machines.recordHelloV3(this.identity.machineId, frame, this.connectionId);
|
|
226
|
-
}
|
|
227
|
-
catch (e) {
|
|
228
|
-
this.gw.log.warn("could not record a device hello", {
|
|
229
|
-
deviceId: this.deviceId,
|
|
230
|
-
error: errMsg(e),
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
})();
|
|
234
|
-
await this.recorded;
|
|
250
|
+
// set before the first await: an event that follows is handled after the hello
|
|
251
|
+
const hello = frame;
|
|
252
|
+
this.handled = this.welcome(hello);
|
|
253
|
+
await this.handled;
|
|
235
254
|
return;
|
|
236
255
|
}
|
|
237
|
-
if (frame.type === "
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
256
|
+
if (frame.type === "event") {
|
|
257
|
+
if (this.state === "hello") {
|
|
258
|
+
this.close(DEVICE_CLOSE.protocol, "hello expected");
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (this.eventsInFlight >= MAX_EVENTS_IN_FLIGHT) {
|
|
262
|
+
this.gw.log.warn("device sent more events than its provider handles", {
|
|
263
|
+
deviceId: this.deviceId,
|
|
264
|
+
});
|
|
265
|
+
this.close(DEVICE_CLOSE.protocol, "too many events in flight; slow down and reconnect");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.eventsInFlight++;
|
|
269
|
+
// after the hello and the earlier events, in the order they came
|
|
270
|
+
const body = frame.body;
|
|
271
|
+
this.handled = this.handled
|
|
272
|
+
.then(() => this.dispatchEvent(body))
|
|
273
|
+
.finally(() => {
|
|
274
|
+
this.eventsInFlight--;
|
|
251
275
|
});
|
|
252
|
-
await this.
|
|
276
|
+
await this.handled;
|
|
253
277
|
return;
|
|
254
278
|
}
|
|
255
|
-
if (
|
|
279
|
+
if (this.state === "hello" || !this.ready) {
|
|
256
280
|
this.close(DEVICE_CLOSE.protocol, "hello expected");
|
|
257
281
|
return;
|
|
258
282
|
}
|
|
259
|
-
// results are stored in any state after
|
|
260
|
-
// disconnected right after its
|
|
283
|
+
// results are stored in any state after ready: the program sends those it kept while
|
|
284
|
+
// disconnected right after its ready
|
|
261
285
|
await this.store(frame);
|
|
262
286
|
}
|
|
263
287
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
288
|
+
* The accepted hello: claims the lease, records the program facts, then asks the provider
|
|
289
|
+
* (`onHello`, within {@link HELLO_TIMEOUT_MS}); only an accepted hello gets `ready`. A refusal, a
|
|
290
|
+
* failure or the timeout releases the lease and closes 4400 with the provider's code.
|
|
266
291
|
*/
|
|
267
|
-
async
|
|
268
|
-
|
|
269
|
-
|
|
292
|
+
async welcome(hello) {
|
|
293
|
+
if (!(await this.claim()))
|
|
294
|
+
return;
|
|
270
295
|
try {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
296
|
+
await this.gw.machines.recordConnect(this.identity.machineId, hello.program);
|
|
297
|
+
}
|
|
298
|
+
catch (e) {
|
|
299
|
+
this.gw.log.warn("could not record a device connection", {
|
|
300
|
+
deviceId: this.deviceId,
|
|
301
|
+
error: errMsg(e),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
if (this.state !== "holding")
|
|
305
|
+
return;
|
|
306
|
+
let serving;
|
|
307
|
+
try {
|
|
308
|
+
serving = await this.gw.handlers.forDevice(this.deviceId);
|
|
309
|
+
}
|
|
310
|
+
catch (e) {
|
|
311
|
+
this.gw.log.warn("could not read a device's dial-in provider", {
|
|
312
|
+
deviceId: this.deviceId,
|
|
313
|
+
error: errMsg(e),
|
|
314
|
+
});
|
|
315
|
+
await this.close(DEVICE_CLOSE.internal, "internal error");
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
if ("unavailable" in serving || serving.protocol !== this.protocol) {
|
|
319
|
+
await this.close(DEVICE_CLOSE.protocol, "the device's provider is unavailable");
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
const verdict = await this.askProvider(serving, hello.body);
|
|
323
|
+
if (this.state !== "holding")
|
|
324
|
+
return;
|
|
325
|
+
if (!verdict.ok) {
|
|
326
|
+
this.gw.log.info("device provider refused a hello", {
|
|
327
|
+
deviceId: this.deviceId,
|
|
328
|
+
plugin: serving.plugin,
|
|
329
|
+
code: verdict.code,
|
|
330
|
+
});
|
|
331
|
+
await this.close(DEVICE_CLOSE.protocol, closeReason(`${verdict.code}: ${verdict.message}`));
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
this.accepted = serving;
|
|
335
|
+
try {
|
|
336
|
+
await this.sendReady();
|
|
337
|
+
}
|
|
338
|
+
catch (e) {
|
|
339
|
+
this.gw.log.warn("could not open a device connection", {
|
|
340
|
+
deviceId: this.deviceId,
|
|
341
|
+
error: errMsg(e),
|
|
342
|
+
});
|
|
343
|
+
await this.close(DEVICE_CLOSE.internal, "internal error");
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/** The provider's answer to the hello body; a throw or the timeout is a refusal. */
|
|
347
|
+
async askProvider(serving, body) {
|
|
348
|
+
const signal = AbortSignal.any([this.ended.signal, AbortSignal.timeout(HELLO_TIMEOUT_MS)]);
|
|
349
|
+
let timer;
|
|
350
|
+
try {
|
|
351
|
+
const answer = serving.handler.onHello(this.deviceId, body, signal);
|
|
352
|
+
const r = await Promise.race([
|
|
353
|
+
answer,
|
|
354
|
+
new Promise((_, reject) => {
|
|
355
|
+
timer = setTimeout(() => reject(new Error("the provider did not answer the hello in time")), HELLO_TIMEOUT_MS);
|
|
356
|
+
}),
|
|
357
|
+
]);
|
|
358
|
+
if (r?.ok === true)
|
|
359
|
+
return { ok: true };
|
|
360
|
+
if (r?.ok === false && typeof r.code === "string")
|
|
361
|
+
return { ok: false, code: r.code, message: String(r.message ?? "") };
|
|
362
|
+
return { ok: false, code: "provider.invalid_answer", message: "no valid answer" };
|
|
363
|
+
}
|
|
364
|
+
catch (e) {
|
|
365
|
+
this.gw.log.warn("device provider failed a hello", {
|
|
366
|
+
deviceId: this.deviceId,
|
|
367
|
+
plugin: serving.plugin,
|
|
368
|
+
error: errMsg(e),
|
|
369
|
+
});
|
|
370
|
+
return { ok: false, code: "provider.failed", message: "the provider failed the hello" };
|
|
371
|
+
}
|
|
372
|
+
finally {
|
|
373
|
+
clearTimeout(timer);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/** Hands one event body to the provider that accepted the hello (dropped otherwise). */
|
|
377
|
+
async dispatchEvent(body) {
|
|
378
|
+
if (this.state === "closed" || this.accepted === undefined)
|
|
379
|
+
return;
|
|
380
|
+
try {
|
|
381
|
+
// read again: a provider switched off (or replaced) since the hello gets no more frames
|
|
382
|
+
const serving = await this.gw.handlers.forDevice(this.deviceId);
|
|
383
|
+
if ("unavailable" in serving || serving.plugin !== this.accepted.plugin)
|
|
384
|
+
return;
|
|
385
|
+
await serving.handler.onFrame?.(this.deviceId, body);
|
|
386
|
+
}
|
|
387
|
+
catch (e) {
|
|
388
|
+
this.gw.log.warn("device provider failed a frame", {
|
|
389
|
+
deviceId: this.deviceId,
|
|
390
|
+
error: errMsg(e),
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Stores one result and announces it (the relay's wire, envelope.ts), only for a request taken
|
|
396
|
+
* for this device (ruling R20); anything else is dropped. `notSent`: the gateway's own refusal
|
|
397
|
+
* of a request it never sent (never a machine's frame).
|
|
398
|
+
*/
|
|
399
|
+
async store(res, notSent) {
|
|
400
|
+
if (this.effectiveCap === undefined)
|
|
401
|
+
return;
|
|
402
|
+
try {
|
|
403
|
+
const tagged = storedResult(res, { connectionId: this.connectionId, maxFrameBytes: this.effectiveCap }, notSent);
|
|
281
404
|
const stored = await this.gw.redis.eval(ACCEPT, 2, deviceTakenKey(res.id), deviceResKey(res.id), this.deviceId, JSON.stringify(tagged));
|
|
282
405
|
if (Number(stored) !== 1) {
|
|
283
406
|
this.gw.log.warn("dropping a device result for a request not taken for its device", {
|
|
@@ -307,6 +430,7 @@ class DeviceConnection {
|
|
|
307
430
|
await this.subscribe(deviceTakeoverTopic(env), (p) => {
|
|
308
431
|
const token = p?.token;
|
|
309
432
|
if (this.state === "holding" && token !== this.token) {
|
|
433
|
+
this.closeReason = "replaced";
|
|
310
434
|
this.close(DEVICE_CLOSE.replaced, "replaced by a newer connection");
|
|
311
435
|
}
|
|
312
436
|
});
|
|
@@ -343,36 +467,22 @@ class DeviceConnection {
|
|
|
343
467
|
lease.lost.addEventListener("abort", onLost, { once: true });
|
|
344
468
|
await this.subscribe(deviceReqTopic(env), () => this.drain());
|
|
345
469
|
await this.subscribe(deviceRevokedTopic(env), (p) => {
|
|
346
|
-
const
|
|
347
|
-
if (
|
|
470
|
+
const message = p;
|
|
471
|
+
if (message?.machineId !== this.identity.machineId)
|
|
472
|
+
return;
|
|
473
|
+
// the device signed in from another machine: replaced (the program exits as replaced)
|
|
474
|
+
if (message.reason === "replaced") {
|
|
475
|
+
this.closeReason = "replaced";
|
|
476
|
+
this.chain = this.chain.then(() => this.close(DEVICE_CLOSE.replaced, "the device signed in from another machine"));
|
|
348
477
|
return;
|
|
478
|
+
}
|
|
479
|
+
const deleted = message.reason === "deleted";
|
|
480
|
+
this.closeReason = deleted ? "deleted" : "revoked";
|
|
349
481
|
// one more drain first: a stop frame whose nudge was lost still goes out before the close
|
|
350
482
|
this.drain();
|
|
351
|
-
this.chain = this.chain.then(() => this.close(DEVICE_CLOSE.revoked, "the machine was disconnected"));
|
|
352
|
-
});
|
|
353
|
-
if (this.state !== "holding")
|
|
354
|
-
return false;
|
|
355
|
-
this.gw.log.info("device connected", {
|
|
356
|
-
deviceId: env,
|
|
357
|
-
machineId: this.identity.machineId,
|
|
358
|
-
});
|
|
359
|
-
const raw = JSON.stringify({
|
|
360
|
-
type: "ready",
|
|
361
|
-
connectionId: this.connectionId,
|
|
362
|
-
maxFrameBytes: this.effectiveCap,
|
|
483
|
+
this.chain = this.chain.then(() => this.close(DEVICE_CLOSE.revoked, deleted ? "the device was deleted" : "the machine was disconnected"));
|
|
363
484
|
});
|
|
364
|
-
|
|
365
|
-
throw new Error("Check frame limits: ready cannot fit");
|
|
366
|
-
parseDeviceFrame(raw, "kernel", this.effectiveCap);
|
|
367
|
-
// published before ready: a reader that sees the machine ready also finds its capacity
|
|
368
|
-
await this.publishCapacity();
|
|
369
|
-
if (this.state !== "holding")
|
|
370
|
-
return false;
|
|
371
|
-
this.socket.send(raw);
|
|
372
|
-
this.ready = true;
|
|
373
|
-
// what was queued while nobody held the machine
|
|
374
|
-
this.drain();
|
|
375
|
-
return true;
|
|
485
|
+
return this.state === "holding";
|
|
376
486
|
}
|
|
377
487
|
catch (e) {
|
|
378
488
|
this.gw.log.warn("could not claim a device connection", {
|
|
@@ -383,6 +493,30 @@ class DeviceConnection {
|
|
|
383
493
|
return false;
|
|
384
494
|
}
|
|
385
495
|
}
|
|
496
|
+
/** Publishes the capacity, sends `ready` and drains what was queued meanwhile. */
|
|
497
|
+
async sendReady() {
|
|
498
|
+
// the effective capacity: the smaller of the machine's announced limit and the kernel's
|
|
499
|
+
const ready = {
|
|
500
|
+
type: "ready",
|
|
501
|
+
connectionId: this.connectionId,
|
|
502
|
+
maxFrameBytes: this.effectiveCap ?? 0,
|
|
503
|
+
};
|
|
504
|
+
const raw = JSON.stringify(ready);
|
|
505
|
+
if (this.effectiveCap === undefined || Buffer.byteLength(raw) > this.effectiveCap)
|
|
506
|
+
throw new Error("Check frame limits: ready cannot fit");
|
|
507
|
+
// published before ready: a reader that sees the machine ready also finds its capacity
|
|
508
|
+
await this.publishCapacity();
|
|
509
|
+
if (this.state !== "holding")
|
|
510
|
+
return;
|
|
511
|
+
this.gw.log.info("device connected", {
|
|
512
|
+
deviceId: this.deviceId,
|
|
513
|
+
machineId: this.identity.machineId,
|
|
514
|
+
});
|
|
515
|
+
this.socket.send(raw);
|
|
516
|
+
this.ready = true;
|
|
517
|
+
// what was queued while nobody held the machine
|
|
518
|
+
this.drain();
|
|
519
|
+
}
|
|
386
520
|
/** Subscribes for this connection; a subscription that completes after the close is ended. */
|
|
387
521
|
async subscribe(topic, handler) {
|
|
388
522
|
const unsubscribe = await this.gw.bus.subscribe(topic, handler);
|
|
@@ -404,8 +538,9 @@ class DeviceConnection {
|
|
|
404
538
|
}
|
|
405
539
|
async drainOnce() {
|
|
406
540
|
const key = deviceReqKey(this.deviceId);
|
|
407
|
-
while (this.state === "holding" && this.ready) {
|
|
408
|
-
|
|
541
|
+
while (this.state === "holding" && this.ready && this.effectiveCap !== undefined) {
|
|
542
|
+
const cap = this.effectiveCap;
|
|
543
|
+
if (this.gw.maxFrameBytes() < cap) {
|
|
409
544
|
await this.close(DEVICE_CLOSE.protocol, "Frame capacity changed; reconnect");
|
|
410
545
|
return;
|
|
411
546
|
}
|
|
@@ -414,66 +549,33 @@ class DeviceConnection {
|
|
|
414
549
|
const raw = (await this.gw.redis.eval(TAKE, 1, key, DEVICE_TAKEN_PREFIX, this.deviceId, String(this.gw.graceMs()), String(LIST_TTL_MARGIN_MS)));
|
|
415
550
|
if (raw === null)
|
|
416
551
|
return;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
// a cancel carries no capacity-sized payload and must reach the machine after a
|
|
430
|
-
// reconnect too (an operation keeps running across a socket blip, as in protocol 2);
|
|
431
|
-
// so does a machine's `stop`, fenced to the machine above instead of the socket
|
|
432
|
-
const unfenced = request.type === "cancel" || (request.op === "stop" && stopFor !== undefined);
|
|
433
|
-
if (!unfenced &&
|
|
434
|
-
(connectionId !== this.connectionId || maxFrameBytes !== this.effectiveCap)) {
|
|
435
|
-
if (request.type === "req" && typeof request.id === "string")
|
|
436
|
-
await this.store({
|
|
437
|
-
type: "res",
|
|
438
|
-
id: request.id,
|
|
439
|
-
ok: false,
|
|
440
|
-
error: {
|
|
441
|
-
kind: "error",
|
|
442
|
-
message: connectionChangedMessage(String(request.op)),
|
|
443
|
-
},
|
|
444
|
-
}, true);
|
|
445
|
-
continue;
|
|
446
|
-
}
|
|
447
|
-
wire = JSON.stringify(request);
|
|
448
|
-
frame = parseDeviceFrame(wire, "kernel", this.effectiveCap);
|
|
552
|
+
// a request goes out only on the connection it was fenced to, a stop only to its machine,
|
|
553
|
+
// without the kernel-internal fields (envelope.ts)
|
|
554
|
+
const decision = wireFrameOf(raw, {
|
|
555
|
+
connectionId: this.connectionId,
|
|
556
|
+
maxFrameBytes: cap,
|
|
557
|
+
machineId: this.identity.machineId,
|
|
558
|
+
});
|
|
559
|
+
if ("drop" in decision) {
|
|
560
|
+
this.gw.log.warn(`dropping ${decision.drop}`, { deviceId: this.deviceId });
|
|
561
|
+
continue;
|
|
449
562
|
}
|
|
450
|
-
|
|
451
|
-
this.
|
|
452
|
-
deviceId: this.deviceId,
|
|
453
|
-
error: errMsg(e),
|
|
454
|
-
});
|
|
563
|
+
if ("refuse" in decision) {
|
|
564
|
+
await this.store({ id: decision.id }, decision.refuse);
|
|
455
565
|
continue;
|
|
456
566
|
}
|
|
457
|
-
const
|
|
567
|
+
const { send, text } = decision;
|
|
568
|
+
const ttlMs = this.gw.graceMs() + (send.type === "req" ? send.deadlineMs : 0) + LIST_TTL_MARGIN_MS;
|
|
458
569
|
if (this.state !== "holding" || this.socket.readyState !== this.socket.OPEN) {
|
|
459
570
|
// closed while popping: back to the head of the list for the next connection
|
|
460
571
|
await this.gw.redis.eval(PUT_BACK, 1, key, raw, String(ttlMs));
|
|
461
572
|
return;
|
|
462
573
|
}
|
|
463
|
-
if (
|
|
464
|
-
|
|
465
|
-
await this.store({
|
|
466
|
-
type: "res",
|
|
467
|
-
id: frame.id,
|
|
468
|
-
ok: false,
|
|
469
|
-
error: {
|
|
470
|
-
kind: "error",
|
|
471
|
-
message: "The request is larger than devices.local.maxFrameBytes",
|
|
472
|
-
},
|
|
473
|
-
});
|
|
574
|
+
if (send.type === "req" && Buffer.byteLength(text) > cap) {
|
|
575
|
+
await this.store({ id: send.id }, `The request is larger than the connection's frame limit of ${cap} bytes`);
|
|
474
576
|
continue;
|
|
475
577
|
}
|
|
476
|
-
this.socket.send(
|
|
578
|
+
this.socket.send(text);
|
|
477
579
|
}
|
|
478
580
|
}
|
|
479
581
|
/** The next ping (a chain of timeouts; the lease renewal is LockService's, not this timer's). */
|
|
@@ -496,7 +598,7 @@ class DeviceConnection {
|
|
|
496
598
|
this.missedPongs++;
|
|
497
599
|
this.socket.ping();
|
|
498
600
|
// a backstop for a lost nudge (the bus delivers at most once)
|
|
499
|
-
if (this.state === "holding") {
|
|
601
|
+
if (this.state === "holding" && this.ready) {
|
|
500
602
|
void this.publishCapacity().then(() => {
|
|
501
603
|
this.publishFailures = 0;
|
|
502
604
|
}, (e) => {
|
|
@@ -513,17 +615,22 @@ class DeviceConnection {
|
|
|
513
615
|
});
|
|
514
616
|
this.drain();
|
|
515
617
|
}
|
|
516
|
-
if (this.state === "holding" && Date.now() - this.lastAuth >=
|
|
618
|
+
if (this.state === "holding" && Date.now() - this.lastAuth >= this.gw.reauthEveryMs) {
|
|
517
619
|
this.lastAuth = Date.now();
|
|
518
620
|
void this.reauthenticate();
|
|
519
621
|
}
|
|
520
622
|
}
|
|
521
|
-
/**
|
|
623
|
+
/**
|
|
624
|
+
* The credential still stands (membership, org and device are read live); a deleted device
|
|
625
|
+
* closes as deleted, like its revocation does.
|
|
626
|
+
*/
|
|
522
627
|
async reauthenticate() {
|
|
523
628
|
try {
|
|
524
629
|
const id = await this.gw.machines.authenticate(this.credential);
|
|
525
630
|
if (id === null || id.machineId !== this.identity.machineId) {
|
|
526
|
-
this.
|
|
631
|
+
const deleted = await this.gw.machines.isDeviceDeleted(this.deviceId);
|
|
632
|
+
this.closeReason = deleted ? "deleted" : "revoked";
|
|
633
|
+
this.close(DEVICE_CLOSE.revoked, deleted ? "the device was deleted" : "the machine was disconnected");
|
|
527
634
|
}
|
|
528
635
|
}
|
|
529
636
|
catch (e) {
|
|
@@ -557,12 +664,16 @@ class DeviceConnection {
|
|
|
557
664
|
}
|
|
558
665
|
return this.shutdown();
|
|
559
666
|
}
|
|
560
|
-
/**
|
|
667
|
+
/**
|
|
668
|
+
* Stops timers and subscriptions, releases the lease (a lost one is abandoned) and tells the
|
|
669
|
+
* provider that accepted the hello why the connection ended (`onClose`).
|
|
670
|
+
*/
|
|
561
671
|
shutdown() {
|
|
562
672
|
if (this.released)
|
|
563
673
|
return this.released;
|
|
564
674
|
this.state = "closed";
|
|
565
675
|
this.ready = false;
|
|
676
|
+
this.ended.abort();
|
|
566
677
|
clearTimeout(this.helloTimer);
|
|
567
678
|
clearTimeout(this.pingTimer);
|
|
568
679
|
const lease = this.lease;
|
|
@@ -590,25 +701,51 @@ class DeviceConnection {
|
|
|
590
701
|
machineId: this.identity.machineId,
|
|
591
702
|
});
|
|
592
703
|
}
|
|
704
|
+
this.tellClosed();
|
|
593
705
|
})();
|
|
594
706
|
return this.released;
|
|
595
707
|
}
|
|
708
|
+
/** The provider's `onClose`, after the lease is handed back; its failure is only logged. */
|
|
709
|
+
tellClosed() {
|
|
710
|
+
const accepted = this.accepted;
|
|
711
|
+
if (accepted === undefined)
|
|
712
|
+
return;
|
|
713
|
+
this.accepted = undefined;
|
|
714
|
+
try {
|
|
715
|
+
const r = accepted.handler.onClose?.(this.deviceId, this.closeReason);
|
|
716
|
+
if (r instanceof Promise)
|
|
717
|
+
r.catch((e) => this.gw.log.warn("device provider failed onClose", {
|
|
718
|
+
deviceId: this.deviceId,
|
|
719
|
+
error: errMsg(e),
|
|
720
|
+
}));
|
|
721
|
+
}
|
|
722
|
+
catch (e) {
|
|
723
|
+
this.gw.log.warn("device provider failed onClose", {
|
|
724
|
+
deviceId: this.deviceId,
|
|
725
|
+
error: errMsg(e),
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
596
729
|
}
|
|
597
730
|
/**
|
|
598
|
-
* The gateway role's half of the
|
|
599
|
-
* connections of connected machines (`GET /device/connect`,
|
|
600
|
-
* device across replicas, under the lease
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* `capk:device-
|
|
731
|
+
* The gateway role's half of the dial-in relay (environments spec §13.4): holds the WebSocket
|
|
732
|
+
* connections of connected machines (`GET /device/connect`, routes.ts) and speaks the dial-in
|
|
733
|
+
* envelope with them. One connection per device across replicas, under the lease
|
|
734
|
+
* `capk:device-lease:<deviceId>` (the newest connection wins: it publishes
|
|
735
|
+
* `device.takeover.<deviceId>` and the holder lets go). A hello is answered by the device's
|
|
736
|
+
* provider (`DialInHandler.onHello`, {@link DialInHandlers}) once the lease is held; only an
|
|
737
|
+
* accepted hello gets `ready`. The holder drains `capk:device-req:<deviceId>` on every
|
|
738
|
+
* `device.req.<deviceId>` nudge, once after `ready` and on every ping, marking each forwarded
|
|
739
|
+
* request taken for its device (`capk:device-taken:<id>`, TTL the reconnect grace plus the
|
|
740
|
+
* request's deadline plus a margin). A `res` is accepted only for a request taken for the socket's
|
|
741
|
+
* device: it is stored under `capk:device-res:<id>` for the rest of that TTL and published on
|
|
742
|
+
* `device.res.<id>`. An `event` goes to the provider's `onFrame`; the close to its `onClose`.
|
|
607
743
|
* `device.revoked.<deviceId>` closes the connection of that machine only (ruling R16). The
|
|
608
744
|
* sockets are this process's own; everything other replicas need is in Redis.
|
|
609
745
|
*/
|
|
610
|
-
let
|
|
746
|
+
let DialInGateway = class DialInGateway {
|
|
611
747
|
machines;
|
|
748
|
+
handlers;
|
|
612
749
|
locks;
|
|
613
750
|
bus;
|
|
614
751
|
redis;
|
|
@@ -619,8 +756,11 @@ let LocalDeviceGateway = class LocalDeviceGateway {
|
|
|
619
756
|
stopping = false;
|
|
620
757
|
/** How often connections are pinged ({@link PING_INTERVAL_MS}; a test seam). */
|
|
621
758
|
pingIntervalMs = PING_INTERVAL_MS;
|
|
622
|
-
|
|
759
|
+
/** How often a held connection re-checks its credential ({@link REAUTH_EVERY_MS}; test seam). */
|
|
760
|
+
reauthEveryMs = REAUTH_EVERY_MS;
|
|
761
|
+
constructor(machines, handlers, locks, bus, redis, config, log) {
|
|
623
762
|
this.machines = machines;
|
|
763
|
+
this.handlers = handlers;
|
|
624
764
|
this.locks = locks;
|
|
625
765
|
this.bus = bus;
|
|
626
766
|
this.redis = redis;
|
|
@@ -628,43 +768,58 @@ let LocalDeviceGateway = class LocalDeviceGateway {
|
|
|
628
768
|
this.log = log;
|
|
629
769
|
}
|
|
630
770
|
graceMs() {
|
|
631
|
-
return this.config.get().devices.
|
|
771
|
+
return this.config.get().devices.dialIn.reconnectGraceSeconds * 1000;
|
|
632
772
|
}
|
|
633
773
|
leaseTtlMs() {
|
|
634
|
-
return this.config.get().devices.
|
|
774
|
+
return this.config.get().devices.dialIn.leaseTtlMs;
|
|
635
775
|
}
|
|
636
776
|
maxFrameBytes() {
|
|
637
|
-
return this.config.get().devices.
|
|
777
|
+
return this.config.get().devices.dialIn.maxFrameBytes;
|
|
638
778
|
}
|
|
639
|
-
/**
|
|
640
|
-
|
|
641
|
-
|
|
779
|
+
/**
|
|
780
|
+
* Who a `Bearer` machine credential stands for and the provider serving its device (checked
|
|
781
|
+
* before the upgrade): null for an unknown or revoked credential.
|
|
782
|
+
*/
|
|
783
|
+
async admit(credential) {
|
|
784
|
+
const identity = await this.machines.authenticate(credential);
|
|
785
|
+
if (identity === null)
|
|
786
|
+
return null;
|
|
787
|
+
const serving = await this.handlers.forDevice(identity.deviceId);
|
|
788
|
+
return "unavailable" in serving
|
|
789
|
+
? { identity, unavailable: serving.unavailable }
|
|
790
|
+
: { identity, serving };
|
|
642
791
|
}
|
|
643
792
|
/**
|
|
644
|
-
* Serves one upgraded socket of a machine until it closes
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
793
|
+
* Serves one upgraded socket of a machine until it closes, speaking `protocol` (the route
|
|
794
|
+
* admitted it). The credential and the provider are checked again here, with the socket paused,
|
|
795
|
+
* so a credential revoked or a provider switched off since the upgrade check never gets a
|
|
796
|
+
* connection.
|
|
648
797
|
*/
|
|
649
|
-
async accept(socket, credential) {
|
|
798
|
+
async accept(socket, credential, protocol) {
|
|
650
799
|
socket.pause();
|
|
651
800
|
try {
|
|
652
801
|
if (this.stopping) {
|
|
653
802
|
socket.close(DEVICE_CLOSE.shutdown, "shutting down");
|
|
654
803
|
return;
|
|
655
804
|
}
|
|
656
|
-
const
|
|
805
|
+
const admission = await this.admit(credential);
|
|
657
806
|
if (socket.readyState !== socket.OPEN)
|
|
658
807
|
return;
|
|
659
808
|
if (this.stopping) {
|
|
660
809
|
socket.close(DEVICE_CLOSE.shutdown, "shutting down");
|
|
661
810
|
return;
|
|
662
811
|
}
|
|
663
|
-
if (!
|
|
812
|
+
if (!admission) {
|
|
664
813
|
socket.close(DEVICE_CLOSE.revoked, "Reconnect with a current credential");
|
|
665
814
|
return;
|
|
666
815
|
}
|
|
667
|
-
|
|
816
|
+
if (!("serving" in admission) ||
|
|
817
|
+
admission.serving.protocol !== protocol ||
|
|
818
|
+
!DIAL_IN_PROTOCOL_RE.test(protocol)) {
|
|
819
|
+
socket.close(DEVICE_CLOSE.protocol, "the device's provider is unavailable");
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
const conn = new DeviceConnection(this, socket, admission.identity, credential, protocol);
|
|
668
823
|
this.connections.add(conn);
|
|
669
824
|
conn.start();
|
|
670
825
|
}
|
|
@@ -698,13 +853,14 @@ let LocalDeviceGateway = class LocalDeviceGateway {
|
|
|
698
853
|
await Promise.all([...this.connections].map((c) => c.close(DEVICE_CLOSE.shutdown, "shutting down")));
|
|
699
854
|
}
|
|
700
855
|
};
|
|
701
|
-
|
|
856
|
+
DialInGateway = __decorate([
|
|
702
857
|
Injectable(),
|
|
703
|
-
__param(0, Inject(
|
|
704
|
-
__param(1, Inject(
|
|
705
|
-
__param(2, Inject(
|
|
706
|
-
__param(3, Inject(
|
|
707
|
-
__param(4, Inject(
|
|
708
|
-
__param(5, Inject(
|
|
709
|
-
|
|
710
|
-
|
|
858
|
+
__param(0, Inject(DialInMachines)),
|
|
859
|
+
__param(1, Inject(DialInHandlers)),
|
|
860
|
+
__param(2, Inject(LockService)),
|
|
861
|
+
__param(3, Inject(KERNEL_BUS)),
|
|
862
|
+
__param(4, Inject(REDIS)),
|
|
863
|
+
__param(5, Inject(KernelConfigService)),
|
|
864
|
+
__param(6, Inject(KERNEL_LOGGER))
|
|
865
|
+
], DialInGateway);
|
|
866
|
+
export { DialInGateway };
|