@air-jam/devtools 0.0.0-stage → 1.0.0-beta.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 +21 -0
- package/README.md +112 -2
- package/dist/chunk-2O67AXCU.js +470 -0
- package/dist/chunk-2O67AXCU.js.map +1 -0
- package/dist/chunk-AO3ZDXF6.js +142 -0
- package/dist/chunk-AO3ZDXF6.js.map +1 -0
- package/dist/chunk-ART5TEYT.js +472 -0
- package/dist/chunk-ART5TEYT.js.map +1 -0
- package/dist/chunk-ASC2CK2R.js +77 -0
- package/dist/chunk-ASC2CK2R.js.map +1 -0
- package/dist/chunk-E3UZZGAV.js +50 -0
- package/dist/chunk-E3UZZGAV.js.map +1 -0
- package/dist/chunk-EJTE7UKX.js +64 -0
- package/dist/chunk-EJTE7UKX.js.map +1 -0
- package/dist/chunk-ESFKNN75.js +110 -0
- package/dist/chunk-ESFKNN75.js.map +1 -0
- package/dist/chunk-EU7A6PXW.js +1339 -0
- package/dist/chunk-EU7A6PXW.js.map +1 -0
- package/dist/chunk-FN7E6EA7.js +549 -0
- package/dist/chunk-FN7E6EA7.js.map +1 -0
- package/dist/chunk-GDFAWHGA.js +190 -0
- package/dist/chunk-GDFAWHGA.js.map +1 -0
- package/dist/chunk-HZUGBH4Y.js +347 -0
- package/dist/chunk-HZUGBH4Y.js.map +1 -0
- package/dist/chunk-ITKAOEGX.js +93 -0
- package/dist/chunk-ITKAOEGX.js.map +1 -0
- package/dist/chunk-JBCUTWRI.js +897 -0
- package/dist/chunk-JBCUTWRI.js.map +1 -0
- package/dist/chunk-JSI5S4WP.js +1174 -0
- package/dist/chunk-JSI5S4WP.js.map +1 -0
- package/dist/chunk-L4D675BG.js +97 -0
- package/dist/chunk-L4D675BG.js.map +1 -0
- package/dist/chunk-M6CJ7GQC.js +133 -0
- package/dist/chunk-M6CJ7GQC.js.map +1 -0
- package/dist/chunk-MEIMOSZC.js +749 -0
- package/dist/chunk-MEIMOSZC.js.map +1 -0
- package/dist/chunk-O3XG3FNL.js +1014 -0
- package/dist/chunk-O3XG3FNL.js.map +1 -0
- package/dist/chunk-OM6SCJ7H.js +18 -0
- package/dist/chunk-OM6SCJ7H.js.map +1 -0
- package/dist/chunk-PKD4WVX5.js +63 -0
- package/dist/chunk-PKD4WVX5.js.map +1 -0
- package/dist/chunk-PMBLXGFM.js +3384 -0
- package/dist/chunk-PMBLXGFM.js.map +1 -0
- package/dist/chunk-PMNE5GIN.js +279 -0
- package/dist/chunk-PMNE5GIN.js.map +1 -0
- package/dist/chunk-QAWI64VR.js +148 -0
- package/dist/chunk-QAWI64VR.js.map +1 -0
- package/dist/chunk-QGADSPHZ.js +449 -0
- package/dist/chunk-QGADSPHZ.js.map +1 -0
- package/dist/chunk-SPYN66UV.js +338 -0
- package/dist/chunk-SPYN66UV.js.map +1 -0
- package/dist/context.d.ts +19 -0
- package/dist/context.js +10 -0
- package/dist/context.js.map +1 -0
- package/dist/controller.d.ts +67 -0
- package/dist/controller.js +40 -0
- package/dist/controller.js.map +1 -0
- package/dist/dependencies.d.ts +44 -0
- package/dist/dependencies.js +10 -0
- package/dist/dependencies.js.map +1 -0
- package/dist/dev.d.ts +21 -0
- package/dist/dev.js +19 -0
- package/dist/dev.js.map +1 -0
- package/dist/game-builds.d.ts +200 -0
- package/dist/game-builds.js +16 -0
- package/dist/game-builds.js.map +1 -0
- package/dist/game-run-watch-CEd_SpAT.d.ts +84 -0
- package/dist/game-runs.d.ts +1452 -0
- package/dist/game-runs.js +76 -0
- package/dist/game-runs.js.map +1 -0
- package/dist/game-session-broker-CUvORfMd.d.ts +439 -0
- package/dist/game-session-broker.d.ts +4 -0
- package/dist/game-session-broker.js +40 -0
- package/dist/game-session-broker.js.map +1 -0
- package/dist/game-session.d.ts +3934 -0
- package/dist/game-session.js +195 -0
- package/dist/game-session.js.map +1 -0
- package/dist/game-settings.d.ts +16 -0
- package/dist/game-settings.js +237 -0
- package/dist/game-settings.js.map +1 -0
- package/dist/games.d.ts +28 -0
- package/dist/games.js +16 -0
- package/dist/games.js.map +1 -0
- package/dist/harness/visual.d.ts +204 -0
- package/dist/harness/visual.js +46 -0
- package/dist/harness/visual.js.map +1 -0
- package/dist/logs.d.ts +15 -0
- package/dist/logs.js +57 -0
- package/dist/logs.js.map +1 -0
- package/dist/managed-process-scope-BicYi_t9.d.ts +3 -0
- package/dist/mcp-config.d.ts +63 -0
- package/dist/mcp-config.js +191 -0
- package/dist/mcp-config.js.map +1 -0
- package/dist/platform-auth.d.ts +9 -0
- package/dist/platform-auth.js +45 -0
- package/dist/platform-auth.js.map +1 -0
- package/dist/platform-game-analytics.d.ts +51 -0
- package/dist/platform-game-analytics.js +49 -0
- package/dist/platform-game-analytics.js.map +1 -0
- package/dist/platform-game-app-id.d.ts +46 -0
- package/dist/platform-game-app-id.js +93 -0
- package/dist/platform-game-app-id.js.map +1 -0
- package/dist/platform-game-media.d.ts +347 -0
- package/dist/platform-game-media.js +168 -0
- package/dist/platform-game-media.js.map +1 -0
- package/dist/platform-games.d.ts +87 -0
- package/dist/platform-games.js +183 -0
- package/dist/platform-games.js.map +1 -0
- package/dist/quality.d.ts +16 -0
- package/dist/quality.js +62 -0
- package/dist/quality.js.map +1 -0
- package/dist/release.d.ts +1304 -0
- package/dist/release.js +34 -0
- package/dist/release.js.map +1 -0
- package/dist/rooms-check.d.ts +90 -0
- package/dist/rooms-check.js +717 -0
- package/dist/rooms-check.js.map +1 -0
- package/dist/source-tree-contract.d.ts +16 -0
- package/dist/source-tree-contract.js +13 -0
- package/dist/source-tree-contract.js.map +1 -0
- package/dist/testing.d.ts +464 -0
- package/dist/testing.js +27 -0
- package/dist/testing.js.map +1 -0
- package/dist/tooling/describe-game.d.ts +2 -0
- package/dist/tooling/describe-game.js +54 -0
- package/dist/tooling/describe-game.js.map +1 -0
- package/dist/tooling/game-session-broker.d.ts +2 -0
- package/dist/tooling/game-session-broker.js +28 -0
- package/dist/tooling/game-session-broker.js.map +1 -0
- package/dist/tooling/hold-runtime-host.d.ts +2 -0
- package/dist/tooling/hold-runtime-host.js +931 -0
- package/dist/tooling/hold-runtime-host.js.map +1 -0
- package/dist/tooling/list-visual-scenarios.d.ts +2 -0
- package/dist/tooling/list-visual-scenarios.js +25 -0
- package/dist/tooling/list-visual-scenarios.js.map +1 -0
- package/dist/tooling/managed-dev-supervisor.d.ts +2 -0
- package/dist/tooling/managed-dev-supervisor.js +51 -0
- package/dist/tooling/managed-dev-supervisor.js.map +1 -0
- package/dist/tooling/process-scope-bootstrap.mjs +24 -0
- package/dist/tooling/process-scope-guard.d.ts +2 -0
- package/dist/tooling/process-scope-guard.js +388 -0
- package/dist/tooling/process-scope-guard.js.map +1 -0
- package/dist/tooling/process-scope-windows.ps1 +148 -0
- package/dist/tooling/rooms-check-device.d.ts +2 -0
- package/dist/tooling/rooms-check-device.js +498 -0
- package/dist/tooling/rooms-check-device.js.map +1 -0
- package/dist/tooling/run-headless-room.d.ts +2 -0
- package/dist/tooling/run-headless-room.js +287 -0
- package/dist/tooling/run-headless-room.js.map +1 -0
- package/dist/tooling/run-test-profile.d.ts +2 -0
- package/dist/tooling/run-test-profile.js +181 -0
- package/dist/tooling/run-test-profile.js.map +1 -0
- package/dist/tooling/run-visual-capture.d.ts +2 -0
- package/dist/tooling/run-visual-capture.js +203 -0
- package/dist/tooling/run-visual-capture.js.map +1 -0
- package/dist/tooling/seal-game-build.d.ts +2 -0
- package/dist/tooling/seal-game-build.js +120 -0
- package/dist/tooling/seal-game-build.js.map +1 -0
- package/dist/types-CdYrgrG_.d.ts +1036 -0
- package/dist/types-DyRD-ym1.d.ts +141 -0
- package/dist/visual.d.ts +21 -0
- package/dist/visual.js +219 -0
- package/dist/visual.js.map +1 -0
- package/package.json +150 -3
- package/src/bounded-response.ts +28 -0
- package/src/built-game-runtime.ts +152 -0
- package/src/commands.ts +40 -0
- package/src/context.ts +218 -0
- package/src/controller-protocol.ts +22 -0
- package/src/controller.ts +1526 -0
- package/src/current-room-host.ts +166 -0
- package/src/decision-provider.ts +102 -0
- package/src/dependencies.ts +176 -0
- package/src/dependency-install-contract.ts +12 -0
- package/src/dev.ts +995 -0
- package/src/fs-utils.ts +93 -0
- package/src/game-action-observation.ts +106 -0
- package/src/game-build-protocol.ts +72 -0
- package/src/game-build.ts +483 -0
- package/src/game-run-owner.ts +2358 -0
- package/src/game-run-protocol.ts +448 -0
- package/src/game-run-record.ts +92 -0
- package/src/game-run-replay.ts +95 -0
- package/src/game-run-watch.ts +155 -0
- package/src/game-session-broker.ts +793 -0
- package/src/game-session-operations.ts +61 -0
- package/src/game-session-protocol.ts +180 -0
- package/src/game-session-runtime.ts +861 -0
- package/src/game-session.ts +206 -0
- package/src/game-settings.ts +295 -0
- package/src/games.ts +262 -0
- package/src/harness/visual/browser.ts +100 -0
- package/src/harness/visual/controller-observation.ts +181 -0
- package/src/harness/visual/prefab-contract.ts +29 -0
- package/src/harness/visual/prefab-runner.ts +178 -0
- package/src/harness/visual/runner.ts +549 -0
- package/src/harness/visual/runtime-controller-observations.ts +151 -0
- package/src/harness/visual/scenario-contract.ts +8 -0
- package/src/harness/visual/scenario-helpers.ts +67 -0
- package/src/harness/visual/screencast.ts +155 -0
- package/src/harness/visual/session.ts +400 -0
- package/src/harness/visual/types.ts +184 -0
- package/src/harness/visual.ts +7 -0
- package/src/headless-room-protocol.ts +112 -0
- package/src/headless-room.ts +222 -0
- package/src/helper-channel.ts +229 -0
- package/src/helper-scripts.ts +69 -0
- package/src/installed-dependencies.ts +74 -0
- package/src/logs.ts +67 -0
- package/src/managed-process-scope.ts +481 -0
- package/src/mcp-config.ts +262 -0
- package/src/node-process-environment.ts +10 -0
- package/src/platform-auth.ts +594 -0
- package/src/platform-game-analytics.ts +50 -0
- package/src/platform-game-app-id.ts +99 -0
- package/src/platform-game-media.ts +209 -0
- package/src/platform-games.ts +217 -0
- package/src/policy-artifact-contract.ts +52 -0
- package/src/policy-artifacts.ts +134 -0
- package/src/process-scope-admission.ts +50 -0
- package/src/process-scope-protocol.ts +66 -0
- package/src/process-scope.ts +327 -0
- package/src/quality.ts +82 -0
- package/src/release.ts +1683 -0
- package/src/rooms-check-fleet.ts +194 -0
- package/src/rooms-check-link.ts +138 -0
- package/src/rooms-check-protocol.ts +244 -0
- package/src/rooms-check.ts +797 -0
- package/src/runtime-owner-protocol.ts +218 -0
- package/src/source-tree-contract.ts +70 -0
- package/src/source-tree.ts +84 -0
- package/src/test-profile-process.ts +153 -0
- package/src/test-profile-protocol.ts +116 -0
- package/src/testing.ts +430 -0
- package/src/tooling/declaration-hook.ts +52 -0
- package/src/tooling/describe-game.ts +49 -0
- package/src/tooling/game-session-broker.ts +7 -0
- package/src/tooling/hold-runtime-host.ts +441 -0
- package/src/tooling/list-visual-scenarios.ts +21 -0
- package/src/tooling/load-game.ts +79 -0
- package/src/tooling/managed-dev-supervisor.ts +71 -0
- package/src/tooling/process-scope-bootstrap.mjs +24 -0
- package/src/tooling/process-scope-guard.ts +213 -0
- package/src/tooling/process-scope-windows.ps1 +148 -0
- package/src/tooling/rooms-check-device.ts +493 -0
- package/src/tooling/run-headless-room.ts +326 -0
- package/src/tooling/run-test-profile.ts +190 -0
- package/src/tooling/run-visual-capture.ts +231 -0
- package/src/tooling/runtime-owner-actions.ts +65 -0
- package/src/tooling/seal-game-build.ts +118 -0
- package/src/tooling/visual-pack.ts +11 -0
- package/src/types.ts +928 -0
- package/src/visual.ts +296 -0
- package/src/windows-process-scopes.ts +189 -0
- package/src/workspace-contract.ts +32 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
resolveDevtoolsHelperArgs,
|
|
4
|
+
resolveDevtoolsHelperScript,
|
|
5
|
+
} from "./helper-scripts.js";
|
|
6
|
+
import {
|
|
7
|
+
deviceEventSchema,
|
|
8
|
+
sharedNow,
|
|
9
|
+
type ConnectionProblem,
|
|
10
|
+
type DeviceCommand,
|
|
11
|
+
type DeviceEvent,
|
|
12
|
+
type DeviceLink,
|
|
13
|
+
} from "./rooms-check-protocol.js";
|
|
14
|
+
|
|
15
|
+
/** A scenario found the room server breaking a rule; its message says which. */
|
|
16
|
+
export class ScenarioFailure extends Error {}
|
|
17
|
+
|
|
18
|
+
export type Status = Extract<DeviceEvent, { type: "status" }>;
|
|
19
|
+
|
|
20
|
+
export const isOpen = (event: DeviceEvent): event is Status =>
|
|
21
|
+
event.type === "status" && event.status.phase === "open";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Spaces connection opens so that no minute holds more than `perMinute`,
|
|
25
|
+
* across every device process of the check, for a server that limits how
|
|
26
|
+
* often one address may connect.
|
|
27
|
+
*/
|
|
28
|
+
export class ConnectionPacer {
|
|
29
|
+
private readonly spacingMs: number;
|
|
30
|
+
private next = Number.NEGATIVE_INFINITY;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
perMinute: number,
|
|
34
|
+
private readonly now: () => number = sharedNow,
|
|
35
|
+
) {
|
|
36
|
+
this.spacingMs = Math.floor(60_000 / Math.max(perMinute - 1, 1)) + 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Reserves the next open and returns when it may happen. */
|
|
40
|
+
reserve(): number {
|
|
41
|
+
const slot = Math.max(this.now(), this.next);
|
|
42
|
+
this.next = slot + this.spacingMs;
|
|
43
|
+
return slot;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One device process the check can freeze, thaw, kill and read. */
|
|
48
|
+
export class Device {
|
|
49
|
+
readonly events: DeviceEvent[] = [];
|
|
50
|
+
private readonly waiters = new Set<() => void>();
|
|
51
|
+
private exited = false;
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
readonly name: string,
|
|
55
|
+
private readonly child: ChildProcess,
|
|
56
|
+
pacer: ConnectionPacer | null,
|
|
57
|
+
) {
|
|
58
|
+
child.on("message", (message: unknown) => {
|
|
59
|
+
const event = deviceEventSchema.parse(message);
|
|
60
|
+
if (event.type === "dial") {
|
|
61
|
+
const grant = () => {
|
|
62
|
+
if (child.connected) child.send({ type: "dial", id: event.id });
|
|
63
|
+
};
|
|
64
|
+
if (pacer) setTimeout(grant, pacer.reserve() - sharedNow());
|
|
65
|
+
else grant();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.events.push(event);
|
|
69
|
+
for (const waiter of [...this.waiters]) waiter();
|
|
70
|
+
});
|
|
71
|
+
child.on("exit", () => {
|
|
72
|
+
this.exited = true;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
send(command: DeviceCommand): void {
|
|
77
|
+
this.child.send(command);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
freeze(): void {
|
|
81
|
+
this.child.kill("SIGSTOP");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
thaw(): void {
|
|
85
|
+
this.child.kill("SIGCONT");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
kill(): void {
|
|
89
|
+
if (this.exited) return;
|
|
90
|
+
this.child.kill("SIGCONT");
|
|
91
|
+
this.child.kill("SIGKILL");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The first event since `since` that `find` accepts, or a failure naming
|
|
96
|
+
* `what`; a device that reports it failed fails whatever waits on it.
|
|
97
|
+
*/
|
|
98
|
+
until<Found>(
|
|
99
|
+
find: (event: DeviceEvent) => Found | undefined,
|
|
100
|
+
what: string,
|
|
101
|
+
deadlineMs: number,
|
|
102
|
+
since = 0,
|
|
103
|
+
): Promise<Found> {
|
|
104
|
+
const search = (): { found: Found } | { failed: string } | undefined => {
|
|
105
|
+
for (const event of this.events)
|
|
106
|
+
if (event.at >= since) {
|
|
107
|
+
if (event.type === "failed") return { failed: event.message };
|
|
108
|
+
const found = find(event);
|
|
109
|
+
if (found !== undefined) return { found };
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
};
|
|
113
|
+
return new Promise((resolve, reject) => {
|
|
114
|
+
const check = () => {
|
|
115
|
+
const outcome = search();
|
|
116
|
+
if (outcome === undefined) return;
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
this.waiters.delete(check);
|
|
119
|
+
if ("found" in outcome) resolve(outcome.found);
|
|
120
|
+
else reject(new ScenarioFailure(`${this.name}: ${outcome.failed}`));
|
|
121
|
+
};
|
|
122
|
+
const timer = setTimeout(() => {
|
|
123
|
+
this.waiters.delete(check);
|
|
124
|
+
reject(
|
|
125
|
+
new ScenarioFailure(
|
|
126
|
+
`${this.name}: ${what} did not happen within ${deadlineMs} ms.`,
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
}, deadlineMs);
|
|
130
|
+
this.waiters.add(check);
|
|
131
|
+
check();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
since<Type extends DeviceEvent["type"]>(
|
|
136
|
+
type: Type,
|
|
137
|
+
from = 0,
|
|
138
|
+
to = Number.POSITIVE_INFINITY,
|
|
139
|
+
): Array<Extract<DeviceEvent, { type: Type }>> {
|
|
140
|
+
return this.events.filter(
|
|
141
|
+
(event): event is Extract<DeviceEvent, { type: Type }> =>
|
|
142
|
+
event.type === type && event.at >= from && event.at <= to,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The devices of one scenario; every process ends with it. */
|
|
148
|
+
export class Fleet {
|
|
149
|
+
private readonly devices: Device[] = [];
|
|
150
|
+
private readonly script = resolveDevtoolsHelperScript(
|
|
151
|
+
"rooms-check-device.ts",
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
readonly link: DeviceLink;
|
|
155
|
+
readonly hostCredentialCommand: string | undefined;
|
|
156
|
+
|
|
157
|
+
constructor(
|
|
158
|
+
target: { link: DeviceLink; hostCredentialCommand?: string },
|
|
159
|
+
private readonly pacer: ConnectionPacer | null,
|
|
160
|
+
) {
|
|
161
|
+
this.link = target.link;
|
|
162
|
+
this.hostCredentialCommand = target.hostCredentialCommand;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
device(name: string): Device {
|
|
166
|
+
const child = spawn(
|
|
167
|
+
process.execPath,
|
|
168
|
+
resolveDevtoolsHelperArgs(this.script),
|
|
169
|
+
{
|
|
170
|
+
stdio: ["ignore", "ignore", "inherit", "ipc"],
|
|
171
|
+
},
|
|
172
|
+
);
|
|
173
|
+
const device = new Device(name, child, this.pacer);
|
|
174
|
+
this.devices.push(device);
|
|
175
|
+
return device;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Every connection problem the devices reported, timed from `startedAt`. */
|
|
179
|
+
problems(startedAt: number): ConnectionProblem[] {
|
|
180
|
+
return this.devices
|
|
181
|
+
.flatMap((device) =>
|
|
182
|
+
device.since("problem").map((event) => ({
|
|
183
|
+
device: device.name,
|
|
184
|
+
atMs: Math.round(event.at - startedAt),
|
|
185
|
+
problem: event.problem,
|
|
186
|
+
})),
|
|
187
|
+
)
|
|
188
|
+
.sort((left, right) => left.atMs - right.atMs);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
close(): void {
|
|
192
|
+
for (const device of this.devices) device.kill();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
roomCloseReasonForCode,
|
|
3
|
+
type RoomConnector,
|
|
4
|
+
type RoomWireData,
|
|
5
|
+
} from "@air-jam/sdk/room-protocol";
|
|
6
|
+
import { roomSocketUrls } from "@air-jam/sdk/room-session";
|
|
7
|
+
import WebSocket from "ws";
|
|
8
|
+
import type { LinkProblem } from "./rooms-check-protocol.js";
|
|
9
|
+
|
|
10
|
+
const HANDSHAKE_TIMEOUT_MS = 10_000;
|
|
11
|
+
|
|
12
|
+
interface Stallable {
|
|
13
|
+
stall(): void;
|
|
14
|
+
resume(): void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The device's network as the check controls it. A stall holds every byte
|
|
19
|
+
* both ways, as a dead cell does: nothing is read, and what the device sends
|
|
20
|
+
* waits where the device cannot see it, as in the system's socket buffer,
|
|
21
|
+
* then leaves in one burst when the network returns.
|
|
22
|
+
*/
|
|
23
|
+
export class CheckNetwork {
|
|
24
|
+
private readonly links = new Set<Stallable>();
|
|
25
|
+
private stalled = false;
|
|
26
|
+
|
|
27
|
+
attach(link: Stallable): () => void {
|
|
28
|
+
this.links.add(link);
|
|
29
|
+
if (this.stalled) link.stall();
|
|
30
|
+
return () => this.links.delete(link);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
stall(): void {
|
|
34
|
+
this.stalled = true;
|
|
35
|
+
for (const link of this.links) link.stall();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
resume(): void {
|
|
39
|
+
this.stalled = false;
|
|
40
|
+
for (const link of this.links) link.resume();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface CheckConnectorOptions {
|
|
45
|
+
/** The rooms origin under check. */
|
|
46
|
+
readonly url: string;
|
|
47
|
+
/** Sent as every connection's Origin header, as the page's origin would be. */
|
|
48
|
+
readonly origin?: string;
|
|
49
|
+
/** Resolves when this connection may open; the check paces opens across every process. */
|
|
50
|
+
readonly dial: () => Promise<void>;
|
|
51
|
+
readonly onDialing?: () => void;
|
|
52
|
+
/** A connection failed to open, or closed without this side asking. */
|
|
53
|
+
readonly onProblem: (problem: LinkProblem) => void;
|
|
54
|
+
readonly network?: CheckNetwork;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Links as a phone or a screen would, over Node's `ws` so the check can send
|
|
59
|
+
* the page's Origin and learn why a connection failed: the HTTP status of a
|
|
60
|
+
* refused upgrade, the error of one that never answered, or the close code.
|
|
61
|
+
*/
|
|
62
|
+
export const createCheckConnector = (
|
|
63
|
+
options: CheckConnectorOptions,
|
|
64
|
+
): RoomConnector => {
|
|
65
|
+
const urlFor = roomSocketUrls(options.url);
|
|
66
|
+
return (address, handlers) => {
|
|
67
|
+
let socket: WebSocket | null = null;
|
|
68
|
+
let closedHere = false;
|
|
69
|
+
let held: RoomWireData[] | null = null;
|
|
70
|
+
let detach = () => {};
|
|
71
|
+
void options.dial().then(() => {
|
|
72
|
+
if (closedHere) return;
|
|
73
|
+
options.onDialing?.();
|
|
74
|
+
const opening = new WebSocket(urlFor(address), {
|
|
75
|
+
origin: options.origin,
|
|
76
|
+
handshakeTimeout: HANDSHAKE_TIMEOUT_MS,
|
|
77
|
+
});
|
|
78
|
+
socket = opening;
|
|
79
|
+
let opened = false;
|
|
80
|
+
let refusedStatus: number | null = null;
|
|
81
|
+
let error: string | null = null;
|
|
82
|
+
opening.on("unexpected-response", (_request, response) => {
|
|
83
|
+
refusedStatus = response.statusCode ?? 0;
|
|
84
|
+
opening.terminate();
|
|
85
|
+
});
|
|
86
|
+
opening.on("error", (cause: Error & { code?: string }) => {
|
|
87
|
+
error ??= cause.code ?? cause.message;
|
|
88
|
+
});
|
|
89
|
+
opening.on("open", () => {
|
|
90
|
+
opened = true;
|
|
91
|
+
detach =
|
|
92
|
+
options.network?.attach({
|
|
93
|
+
stall: () => {
|
|
94
|
+
held ??= [];
|
|
95
|
+
opening.pause();
|
|
96
|
+
},
|
|
97
|
+
resume: () => {
|
|
98
|
+
opening.resume();
|
|
99
|
+
const burst = held ?? [];
|
|
100
|
+
held = null;
|
|
101
|
+
for (const data of burst) opening.send(data);
|
|
102
|
+
},
|
|
103
|
+
}) ?? detach;
|
|
104
|
+
handlers.open();
|
|
105
|
+
});
|
|
106
|
+
opening.on("message", (data, isBinary) => {
|
|
107
|
+
const bytes = Buffer.isBuffer(data)
|
|
108
|
+
? data
|
|
109
|
+
: Buffer.concat(Array.isArray(data) ? data : [Buffer.from(data)]);
|
|
110
|
+
handlers.message(isBinary ? new Uint8Array(bytes) : bytes.toString());
|
|
111
|
+
});
|
|
112
|
+
opening.on("close", (code) => {
|
|
113
|
+
detach();
|
|
114
|
+
if (closedHere) return;
|
|
115
|
+
options.onProblem(
|
|
116
|
+
opened
|
|
117
|
+
? { kind: "closed", code, reason: roomCloseReasonForCode(code) }
|
|
118
|
+
: refusedStatus !== null
|
|
119
|
+
? { kind: "refused", status: refusedStatus }
|
|
120
|
+
: { kind: "failed", error: error ?? `closed with ${code}` },
|
|
121
|
+
);
|
|
122
|
+
handlers.close(code);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
return {
|
|
126
|
+
send: (data) => {
|
|
127
|
+
if (held) held.push(data);
|
|
128
|
+
else if (socket?.readyState === WebSocket.OPEN) socket.send(data);
|
|
129
|
+
},
|
|
130
|
+
buffered: () => (socket?.bufferedAmount ?? 0) > 0,
|
|
131
|
+
close: () => {
|
|
132
|
+
closedHere = true;
|
|
133
|
+
if (socket?.readyState === WebSocket.OPEN) socket.close(1000);
|
|
134
|
+
else socket?.terminate();
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The messages between `airjam rooms check` and its device processes. Each
|
|
5
|
+
* edge-case device (one host or one phone) runs in its own process, so the
|
|
6
|
+
* check can freeze it as a locked phone is frozen, kill it as a closed tab
|
|
7
|
+
* is, and start a replacement; load runs whole rooms in a few workers.
|
|
8
|
+
*/
|
|
9
|
+
const keySchema = z.string().min(16).max(128);
|
|
10
|
+
const codeSchema = z.string().min(4).max(8);
|
|
11
|
+
|
|
12
|
+
/** How every device reaches the room server. */
|
|
13
|
+
const linkSchema = z.object({
|
|
14
|
+
url: z.string(),
|
|
15
|
+
/** The Origin header each connection sends. */
|
|
16
|
+
origin: z.string().optional(),
|
|
17
|
+
});
|
|
18
|
+
export type DeviceLink = z.infer<typeof linkSchema>;
|
|
19
|
+
|
|
20
|
+
/** A shell command whose trimmed output is one fresh host credential. */
|
|
21
|
+
const credentialCommandSchema = z.string().min(1).optional();
|
|
22
|
+
|
|
23
|
+
export const deviceCommandSchema = z.discriminatedUnion("type", [
|
|
24
|
+
z.object({
|
|
25
|
+
type: z.literal("host"),
|
|
26
|
+
link: linkSchema,
|
|
27
|
+
key: keySchema,
|
|
28
|
+
code: codeSchema.optional(),
|
|
29
|
+
/** A host resuming its instance asks for no credential. */
|
|
30
|
+
instance: z.string().optional(),
|
|
31
|
+
credentialCommand: credentialCommandSchema,
|
|
32
|
+
seats: z.number().int().min(1).max(16),
|
|
33
|
+
statesPerSecond: z.number().nonnegative(),
|
|
34
|
+
/** False sends no client pings, as a hidden tab's throttled timers do. */
|
|
35
|
+
heartbeat: z.boolean(),
|
|
36
|
+
}),
|
|
37
|
+
z.object({
|
|
38
|
+
type: z.literal("phone"),
|
|
39
|
+
link: linkSchema,
|
|
40
|
+
key: keySchema,
|
|
41
|
+
code: codeSchema,
|
|
42
|
+
instance: z.string().optional(),
|
|
43
|
+
inputsPerSecond: z.number().nonnegative(),
|
|
44
|
+
}),
|
|
45
|
+
/** An idle phone taps once. */
|
|
46
|
+
z.object({ type: z.literal("press") }),
|
|
47
|
+
/** The device's network stalls, holding every byte both ways, or comes back. */
|
|
48
|
+
z.object({ type: z.literal("network"), stalled: z.boolean() }),
|
|
49
|
+
/** The connection the device asked to open with `dial` may open now. */
|
|
50
|
+
z.object({ type: z.literal("dial"), id: z.number().int() }),
|
|
51
|
+
z.object({
|
|
52
|
+
type: z.literal("load"),
|
|
53
|
+
link: linkSchema,
|
|
54
|
+
credentialCommand: credentialCommandSchema,
|
|
55
|
+
rooms: z.number().int().positive(),
|
|
56
|
+
phonesPerRoom: z.number().int().min(1).max(16),
|
|
57
|
+
inputsPerSecond: z.number().positive(),
|
|
58
|
+
statesPerSecond: z.number().nonnegative(),
|
|
59
|
+
durationMs: z.number().int().positive(),
|
|
60
|
+
}),
|
|
61
|
+
]);
|
|
62
|
+
export type DeviceCommand = z.infer<typeof deviceCommandSchema>;
|
|
63
|
+
|
|
64
|
+
const statusSchema = z.object({
|
|
65
|
+
phase: z.enum(["connecting", "waiting", "open", "reconnecting", "closed"]),
|
|
66
|
+
reason: z.string().optional(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export const latencySummarySchema = z.object({
|
|
70
|
+
count: z.number(),
|
|
71
|
+
medianMs: z.number().nullable(),
|
|
72
|
+
p99Ms: z.number().nullable(),
|
|
73
|
+
maximumMs: z.number().nullable(),
|
|
74
|
+
});
|
|
75
|
+
export type LatencySummary = z.infer<typeof latencySummarySchema>;
|
|
76
|
+
|
|
77
|
+
const trafficSchema = z.object({
|
|
78
|
+
/** Messages and payload bytes the devices sent to the room server. */
|
|
79
|
+
messagesIn: z.number(),
|
|
80
|
+
bytesIn: z.number(),
|
|
81
|
+
/** Messages and payload bytes the room server sent to the devices. */
|
|
82
|
+
messagesOut: z.number(),
|
|
83
|
+
bytesOut: z.number(),
|
|
84
|
+
});
|
|
85
|
+
export type RoomTraffic = z.infer<typeof trafficSchema>;
|
|
86
|
+
|
|
87
|
+
export const loadResultSchema = z.object({
|
|
88
|
+
roomsOpened: z.number(),
|
|
89
|
+
phonesJoined: z.number(),
|
|
90
|
+
roomOpenMs: z.array(z.number()),
|
|
91
|
+
phoneJoinMs: z.array(z.number()),
|
|
92
|
+
/** Tap to host, per press, measured within the worker on one clock. */
|
|
93
|
+
inputLatencyMs: z.array(z.number()),
|
|
94
|
+
pressesSent: z.number(),
|
|
95
|
+
pressesLost: z.number(),
|
|
96
|
+
stateLatencyMs: z.array(z.number()),
|
|
97
|
+
/** Joined phones whose copy did not end equal to what their host last published. */
|
|
98
|
+
phonesWithoutState: z.number(),
|
|
99
|
+
traffic: trafficSchema,
|
|
100
|
+
cpuMs: z.number(),
|
|
101
|
+
});
|
|
102
|
+
export type LoadResult = z.infer<typeof loadResultSchema>;
|
|
103
|
+
|
|
104
|
+
export const linkProblemSchema = z.discriminatedUnion("kind", [
|
|
105
|
+
/** The server answered the upgrade with this HTTP status instead of a socket. */
|
|
106
|
+
z.object({ kind: z.literal("refused"), status: z.number() }),
|
|
107
|
+
/** No answer: the network or TLS error, or the handshake timing out. */
|
|
108
|
+
z.object({ kind: z.literal("failed"), error: z.string() }),
|
|
109
|
+
/** An open connection closed without the device asking, with the room's reason when the code has one. */
|
|
110
|
+
z.object({
|
|
111
|
+
kind: z.literal("closed"),
|
|
112
|
+
code: z.number(),
|
|
113
|
+
reason: z.string().nullable(),
|
|
114
|
+
}),
|
|
115
|
+
]);
|
|
116
|
+
export type LinkProblem = z.infer<typeof linkProblemSchema>;
|
|
117
|
+
|
|
118
|
+
export const describeLinkProblem = (problem: LinkProblem): string => {
|
|
119
|
+
switch (problem.kind) {
|
|
120
|
+
case "refused":
|
|
121
|
+
return `refused with HTTP ${problem.status}`;
|
|
122
|
+
case "failed":
|
|
123
|
+
return `failed to open: ${problem.error}`;
|
|
124
|
+
case "closed":
|
|
125
|
+
return `closed with ${problem.code}${problem.reason ? ` (${problem.reason})` : ""}`;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/** Everything a device reports, each stamped with the shared wall clock. */
|
|
130
|
+
export const deviceEventSchema = z.discriminatedUnion("type", [
|
|
131
|
+
z.object({
|
|
132
|
+
type: z.literal("status"),
|
|
133
|
+
at: z.number(),
|
|
134
|
+
status: statusSchema,
|
|
135
|
+
code: z.string().optional(),
|
|
136
|
+
instance: z.string().optional(),
|
|
137
|
+
seat: z.string().optional(),
|
|
138
|
+
hostGraceMs: z.number().optional(),
|
|
139
|
+
}),
|
|
140
|
+
z.object({
|
|
141
|
+
type: z.literal("seats"),
|
|
142
|
+
at: z.number(),
|
|
143
|
+
seats: z.array(z.object({ id: z.string(), connected: z.boolean() })),
|
|
144
|
+
}),
|
|
145
|
+
z.object({ type: z.literal("host"), at: z.number(), present: z.boolean() }),
|
|
146
|
+
/** A phone pressed: its button's press count went up to `presses`. */
|
|
147
|
+
z.object({ type: z.literal("pressed"), at: z.number(), presses: z.number() }),
|
|
148
|
+
/** A host saw a seat's press count reach `presses`. */
|
|
149
|
+
z.object({
|
|
150
|
+
type: z.literal("received"),
|
|
151
|
+
at: z.number(),
|
|
152
|
+
seat: z.string(),
|
|
153
|
+
presses: z.number(),
|
|
154
|
+
}),
|
|
155
|
+
z.object({ type: z.literal("published"), at: z.number(), tick: z.number() }),
|
|
156
|
+
z.object({ type: z.literal("state"), at: z.number(), tick: z.number() }),
|
|
157
|
+
z.object({
|
|
158
|
+
type: z.literal("load"),
|
|
159
|
+
at: z.number(),
|
|
160
|
+
result: loadResultSchema,
|
|
161
|
+
}),
|
|
162
|
+
z.object({ type: z.literal("failed"), at: z.number(), message: z.string() }),
|
|
163
|
+
/** Asks for a grant to open a connection; the check paces opens across devices. */
|
|
164
|
+
z.object({ type: z.literal("dial"), at: z.number(), id: z.number().int() }),
|
|
165
|
+
/** A connection began opening. */
|
|
166
|
+
z.object({ type: z.literal("dialing"), at: z.number() }),
|
|
167
|
+
z.object({
|
|
168
|
+
type: z.literal("problem"),
|
|
169
|
+
at: z.number(),
|
|
170
|
+
problem: linkProblemSchema,
|
|
171
|
+
}),
|
|
172
|
+
]);
|
|
173
|
+
export type DeviceEvent = z.infer<typeof deviceEventSchema>;
|
|
174
|
+
|
|
175
|
+
export const ROOMS_CHECK_SCENARIOS = [
|
|
176
|
+
"join",
|
|
177
|
+
"phone-brief-freeze",
|
|
178
|
+
"phone-long-freeze",
|
|
179
|
+
"network-switch",
|
|
180
|
+
"network-stall",
|
|
181
|
+
"duplicate-join",
|
|
182
|
+
"host-reload",
|
|
183
|
+
"host-gone",
|
|
184
|
+
"background-host",
|
|
185
|
+
"crowd",
|
|
186
|
+
"load",
|
|
187
|
+
] as const;
|
|
188
|
+
export type RoomsCheckScenario = (typeof ROOMS_CHECK_SCENARIOS)[number];
|
|
189
|
+
|
|
190
|
+
/** Every scenario but load, which is heavy enough to be asked for by name. */
|
|
191
|
+
export const DEFAULT_ROOMS_CHECK_SCENARIOS = ROOMS_CHECK_SCENARIOS.filter(
|
|
192
|
+
(scenario) => scenario !== "load",
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
export interface RoomsCheckScenarioResult {
|
|
196
|
+
readonly scenario: RoomsCheckScenario;
|
|
197
|
+
readonly passed: boolean;
|
|
198
|
+
/** Why it failed, in one sentence. */
|
|
199
|
+
readonly failure?: string;
|
|
200
|
+
readonly durationMs: number;
|
|
201
|
+
/** What it measured, in milliseconds unless the name says otherwise. */
|
|
202
|
+
readonly measurements: Readonly<Record<string, unknown>>;
|
|
203
|
+
/** Every connection that failed to open or closed without its device asking, expected or not. */
|
|
204
|
+
readonly connectionProblems: readonly ConnectionProblem[];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface ConnectionProblem {
|
|
208
|
+
readonly device: string;
|
|
209
|
+
/** Since the scenario began. */
|
|
210
|
+
readonly atMs: number;
|
|
211
|
+
readonly problem: LinkProblem;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface RoomsCheckReport {
|
|
215
|
+
readonly url: string;
|
|
216
|
+
readonly passed: boolean;
|
|
217
|
+
readonly scenarios: readonly RoomsCheckScenarioResult[];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The wall clock every device process shares, with sub-millisecond precision. */
|
|
221
|
+
export const sharedNow = (): number =>
|
|
222
|
+
performance.timeOrigin + performance.now();
|
|
223
|
+
|
|
224
|
+
export const summarizeLatency = (
|
|
225
|
+
samples: readonly number[],
|
|
226
|
+
): LatencySummary => {
|
|
227
|
+
if (samples.length === 0)
|
|
228
|
+
return { count: 0, medianMs: null, p99Ms: null, maximumMs: null };
|
|
229
|
+
const sorted = [...samples].sort((left, right) => left - right);
|
|
230
|
+
const at = (fraction: number) =>
|
|
231
|
+
round(
|
|
232
|
+
sorted[
|
|
233
|
+
Math.min(sorted.length - 1, Math.floor(fraction * sorted.length))
|
|
234
|
+
] ?? 0,
|
|
235
|
+
);
|
|
236
|
+
return {
|
|
237
|
+
count: sorted.length,
|
|
238
|
+
medianMs: at(0.5),
|
|
239
|
+
p99Ms: at(0.99),
|
|
240
|
+
maximumMs: round(sorted.at(-1) ?? 0),
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const round = (value: number) => Math.round(value * 100) / 100;
|