screenreader-speech-capture 0.0.1-preview.4 → 0.1.0-feat-playwright-supervisor.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/dist/cli.mjs +492 -18
- package/package.json +6 -1
package/dist/cli.mjs
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import http from "node:http";
|
|
3
|
+
import os from "node:os";
|
|
3
4
|
import net from "node:net";
|
|
4
5
|
import { TextDecoder as TextDecoder$1, parseArgs } from "node:util";
|
|
5
|
-
import fs from "node:fs/promises";
|
|
6
|
+
import fs, { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
6
7
|
import path from "node:path";
|
|
7
|
-
import fs$1 from "node:fs";
|
|
8
|
-
import
|
|
8
|
+
import fs$1, { existsSync } from "node:fs";
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
import { once } from "node:events";
|
|
11
|
+
import readline from "node:readline";
|
|
9
12
|
// package.json
|
|
10
|
-
var version = "0.0.1
|
|
13
|
+
var version = "0.1.0-feat-playwright-supervisor.1";
|
|
11
14
|
// ../shared/src/api-version.ts
|
|
12
15
|
/** Path prefix of every API route. */
|
|
13
16
|
const API_PREFIX = "/api/v1";
|
|
@@ -63,6 +66,26 @@ async function listen(server, host, port) {
|
|
|
63
66
|
});
|
|
64
67
|
return server.address().port;
|
|
65
68
|
}
|
|
69
|
+
// src/adapters/in/net/reachable-urls.ts
|
|
70
|
+
/** True for a host that means "every interface" (`0.0.0.0`, `::`). */
|
|
71
|
+
const isWildcard = (host) => [
|
|
72
|
+
"0.0.0.0",
|
|
73
|
+
"::",
|
|
74
|
+
""
|
|
75
|
+
].includes(host);
|
|
76
|
+
/** `host:port` for a URL, with an IPv6 host in brackets. */
|
|
77
|
+
function hostPort(host, port) {
|
|
78
|
+
return host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* URLs other machines can use to reach a listener on `host:port`. For a wildcard host (`0.0.0.0`,
|
|
82
|
+
* `::`) these are the machine's external IPv4 addresses, then `localhost`; otherwise just `host`.
|
|
83
|
+
*/
|
|
84
|
+
function reachableUrls(scheme, host, port, interfaces = os.networkInterfaces()) {
|
|
85
|
+
if (!isWildcard(host)) return [`${scheme}://${hostPort(host, port)}`];
|
|
86
|
+
const addresses = Object.values(interfaces).flatMap((infos) => infos ?? []).filter((info) => info.family === "IPv4" && !info.internal).map((info) => info.address);
|
|
87
|
+
return [...new Set(addresses), "localhost"].map((a) => `${scheme}://${hostPort(a, port)}`);
|
|
88
|
+
}
|
|
66
89
|
// src/adapters/in/http/response.ts
|
|
67
90
|
function sendJson(res, status, body) {
|
|
68
91
|
const data = JSON.stringify(body);
|
|
@@ -176,7 +199,16 @@ function toWaitResultDto(result) {
|
|
|
176
199
|
entries: result.seen.map(toSpeechEntryDto)
|
|
177
200
|
};
|
|
178
201
|
}
|
|
179
|
-
function
|
|
202
|
+
function toServiceStatusDto(service) {
|
|
203
|
+
return {
|
|
204
|
+
state: service.state,
|
|
205
|
+
version: service.version,
|
|
206
|
+
port: service.port,
|
|
207
|
+
restarts: service.restarts,
|
|
208
|
+
exitCode: service.exitCode
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function toStatusDto({ bridge, tcp, buffer, playwright, input }) {
|
|
180
212
|
return {
|
|
181
213
|
bridge: {
|
|
182
214
|
version: bridge.version,
|
|
@@ -200,6 +232,7 @@ function toStatusDto({ bridge, tcp, buffer, input }) {
|
|
|
200
232
|
firstSeq: buffer.firstSeq,
|
|
201
233
|
lastSeq: buffer.lastSeq
|
|
202
234
|
},
|
|
235
|
+
playwright: toServiceStatusDto(playwright),
|
|
203
236
|
input: { enabled: input.enabled }
|
|
204
237
|
};
|
|
205
238
|
}
|
|
@@ -355,10 +388,6 @@ function statusRoutes(router, { status, config }) {
|
|
|
355
388
|
const CLOSE_GRACE_MS = 1e3;
|
|
356
389
|
/** Bytes an SSE stream may hold for a client that stopped reading before it is closed. */
|
|
357
390
|
const SSE_MAX_BUFFERED_BYTES = 1048576;
|
|
358
|
-
/** `host:port` for a URL, with an IPv6 host in brackets. */
|
|
359
|
-
function hostPort(host, port) {
|
|
360
|
-
return host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`;
|
|
361
|
-
}
|
|
362
391
|
/**
|
|
363
392
|
* Driving adapter for the REST + SSE API (docs/http-api.md), on plain `node:http`.
|
|
364
393
|
*/
|
|
@@ -445,7 +474,8 @@ var HttpServer = class {
|
|
|
445
474
|
...settings,
|
|
446
475
|
port
|
|
447
476
|
};
|
|
448
|
-
|
|
477
|
+
const urls = reachableUrls("http", settings.host, port).join(", ");
|
|
478
|
+
this.deps.logger.info(isWildcard(settings.host) ? `listening on ${hostPort(settings.host, port)}, reachable at ${urls}` : `listening on ${urls}`);
|
|
449
479
|
return port;
|
|
450
480
|
}
|
|
451
481
|
/**
|
|
@@ -956,6 +986,315 @@ function defaultConfigFile(platform = process.platform, env = process.env, home
|
|
|
956
986
|
const base = env.XDG_CONFIG_HOME ?? path.posix.join(home, ".config");
|
|
957
987
|
return path.posix.join(base, APP, "config.json");
|
|
958
988
|
}
|
|
989
|
+
/**
|
|
990
|
+
* Where the bridge installs Playwright versions: `%LOCALAPPDATA%\screenreader-speech-capture\
|
|
991
|
+
* playwright` on Windows, `$XDG_CACHE_HOME` (or `~/.cache`) elsewhere, for development.
|
|
992
|
+
*/
|
|
993
|
+
function defaultPlaywrightDir(platform = process.platform, env = process.env, home = os.homedir()) {
|
|
994
|
+
if (platform === "win32") {
|
|
995
|
+
const base = env.LOCALAPPDATA ?? path.win32.join(home, "AppData", "Local");
|
|
996
|
+
return path.win32.join(base, APP, "playwright");
|
|
997
|
+
}
|
|
998
|
+
const base = env.XDG_CACHE_HOME ?? path.posix.join(home, ".cache");
|
|
999
|
+
return path.posix.join(base, APP, "playwright");
|
|
1000
|
+
}
|
|
1001
|
+
// src/domain/service.ts
|
|
1002
|
+
const RESTART_POLICY = {
|
|
1003
|
+
baseDelayMs: 1e3,
|
|
1004
|
+
maxDelayMs: 3e4,
|
|
1005
|
+
stableMs: 6e4,
|
|
1006
|
+
maxCrashesInRow: 5
|
|
1007
|
+
};
|
|
1008
|
+
/**
|
|
1009
|
+
* What to do after a crash. `crashesInRow` counts the crashes before this one, `ranForMs` is how
|
|
1010
|
+
* long the service ran before it crashed.
|
|
1011
|
+
*/
|
|
1012
|
+
function nextAfterCrash(crashesInRow, ranForMs, policy = RESTART_POLICY) {
|
|
1013
|
+
const streak = (ranForMs >= policy.stableMs ? 0 : crashesInRow) + 1;
|
|
1014
|
+
if (streak >= policy.maxCrashesInRow) return {
|
|
1015
|
+
action: "fail",
|
|
1016
|
+
crashesInRow: streak
|
|
1017
|
+
};
|
|
1018
|
+
return {
|
|
1019
|
+
action: "restart",
|
|
1020
|
+
crashesInRow: streak,
|
|
1021
|
+
delayMs: Math.min(policy.baseDelayMs * 2 ** (streak - 1), policy.maxDelayMs)
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
// src/adapters/out/process/process-tree.ts
|
|
1025
|
+
/** How long the process group gets after `SIGTERM` before `SIGKILL` (not on Windows). */
|
|
1026
|
+
const TERM_GRACE_MS = 3e3;
|
|
1027
|
+
/**
|
|
1028
|
+
* Spawn options for a child whose whole tree `killTree` can end. On POSIX the child leads its own
|
|
1029
|
+
* process group. On Windows that isn't needed (`taskkill /T` walks the tree), and `detached`
|
|
1030
|
+
* there would open a new console window.
|
|
1031
|
+
*/
|
|
1032
|
+
const treeSpawnOptions = {
|
|
1033
|
+
detached: process.platform !== "win32",
|
|
1034
|
+
windowsHide: true
|
|
1035
|
+
};
|
|
1036
|
+
function hasExited(child) {
|
|
1037
|
+
return child.exitCode !== null || child.signalCode !== null;
|
|
1038
|
+
}
|
|
1039
|
+
function killGroup(pid, signal) {
|
|
1040
|
+
try {
|
|
1041
|
+
process.kill(-pid, signal);
|
|
1042
|
+
} catch {}
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Kills `child` and every process it started (docs/operations.md#shutdown). On Windows,
|
|
1046
|
+
* `taskkill /T /F` (killing a process there doesn't kill its children). Elsewhere `SIGTERM`
|
|
1047
|
+
* to the process group, then `SIGKILL` to whatever is left. Resolves once `child` has exited.
|
|
1048
|
+
* `child` must have been spawned with `treeSpawnOptions`.
|
|
1049
|
+
*/
|
|
1050
|
+
async function killTree(child) {
|
|
1051
|
+
const { pid } = child;
|
|
1052
|
+
if (pid === void 0) return;
|
|
1053
|
+
const exited = hasExited(child) ? Promise.resolve() : once(child, "exit").then(() => void 0);
|
|
1054
|
+
if (process.platform === "win32") {
|
|
1055
|
+
if (!hasExited(child)) {
|
|
1056
|
+
const taskkill = spawn("taskkill", [
|
|
1057
|
+
"/T",
|
|
1058
|
+
"/F",
|
|
1059
|
+
"/PID",
|
|
1060
|
+
String(pid)
|
|
1061
|
+
], {
|
|
1062
|
+
windowsHide: true,
|
|
1063
|
+
stdio: "ignore"
|
|
1064
|
+
});
|
|
1065
|
+
await once(taskkill, "close").catch(() => void 0);
|
|
1066
|
+
}
|
|
1067
|
+
await exited;
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
killGroup(pid, "SIGTERM");
|
|
1071
|
+
let timer;
|
|
1072
|
+
await Promise.race([exited, new Promise((resolve) => timer = setTimeout(resolve, TERM_GRACE_MS))]);
|
|
1073
|
+
clearTimeout(timer);
|
|
1074
|
+
killGroup(pid, "SIGKILL");
|
|
1075
|
+
await exited;
|
|
1076
|
+
}
|
|
1077
|
+
/** Writes each line of the child's stdout as `info` and of its stderr as `warn`. */
|
|
1078
|
+
function pipeLines(child, output) {
|
|
1079
|
+
if (child.stdout) readline.createInterface({
|
|
1080
|
+
input: child.stdout,
|
|
1081
|
+
crlfDelay: Infinity
|
|
1082
|
+
}).on("line", (line) => output.info(line));
|
|
1083
|
+
if (child.stderr) readline.createInterface({
|
|
1084
|
+
input: child.stderr,
|
|
1085
|
+
crlfDelay: Infinity
|
|
1086
|
+
}).on("line", (line) => output.warn(line));
|
|
1087
|
+
}
|
|
1088
|
+
// src/adapters/out/process/child-process-supervisor.ts
|
|
1089
|
+
const WILDCARD_PROBE = {
|
|
1090
|
+
"0.0.0.0": "127.0.0.1",
|
|
1091
|
+
"::": "::1"
|
|
1092
|
+
};
|
|
1093
|
+
/** Whether something accepts connections on `host:port`. */
|
|
1094
|
+
function portOpen(host, port) {
|
|
1095
|
+
return new Promise((resolve) => {
|
|
1096
|
+
const socket = net.connect({
|
|
1097
|
+
host: WILDCARD_PROBE[host] ?? host,
|
|
1098
|
+
port
|
|
1099
|
+
});
|
|
1100
|
+
const done = (open) => {
|
|
1101
|
+
socket.destroy();
|
|
1102
|
+
resolve(open);
|
|
1103
|
+
};
|
|
1104
|
+
socket.setTimeout(1e3, () => done(false));
|
|
1105
|
+
socket.once("connect", () => done(true));
|
|
1106
|
+
socket.once("error", () => done(false));
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1110
|
+
/**
|
|
1111
|
+
* Runs one child service (docs/operations.md#child-service-crashes): prepare, spawn, wait for
|
|
1112
|
+
* its port, restart after a crash with a growing delay, give up after too many crashes in a row,
|
|
1113
|
+
* and kill the whole process tree on stop.
|
|
1114
|
+
*/
|
|
1115
|
+
var ChildProcessSupervisor = class {
|
|
1116
|
+
options;
|
|
1117
|
+
settings;
|
|
1118
|
+
/** Settings of the current run, until it is stopped. `status()` reports these. */
|
|
1119
|
+
active = null;
|
|
1120
|
+
state = "stopped";
|
|
1121
|
+
restarts = 0;
|
|
1122
|
+
exitCode = null;
|
|
1123
|
+
crashesInRow = 0;
|
|
1124
|
+
child = null;
|
|
1125
|
+
runningSince = null;
|
|
1126
|
+
restartTimer;
|
|
1127
|
+
abort = null;
|
|
1128
|
+
/** Bumped by every launch and stop, so callbacks of an older run do nothing. */
|
|
1129
|
+
generation = 0;
|
|
1130
|
+
waiters = [];
|
|
1131
|
+
constructor(options) {
|
|
1132
|
+
this.options = options;
|
|
1133
|
+
this.settings = options.settings;
|
|
1134
|
+
}
|
|
1135
|
+
status() {
|
|
1136
|
+
const { version, port } = this.active ?? this.resolved();
|
|
1137
|
+
return {
|
|
1138
|
+
state: this.state,
|
|
1139
|
+
version,
|
|
1140
|
+
port,
|
|
1141
|
+
restarts: this.restarts,
|
|
1142
|
+
exitCode: this.exitCode
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
reconfigure(settings) {
|
|
1146
|
+
this.settings = settings;
|
|
1147
|
+
}
|
|
1148
|
+
start() {
|
|
1149
|
+
const settled = new Promise((resolve) => this.waiters.push(resolve));
|
|
1150
|
+
if (this.state === "stopped" || this.state === "failed") {
|
|
1151
|
+
this.restarts = 0;
|
|
1152
|
+
this.crashesInRow = 0;
|
|
1153
|
+
this.exitCode = null;
|
|
1154
|
+
this.launch();
|
|
1155
|
+
}
|
|
1156
|
+
return settled;
|
|
1157
|
+
}
|
|
1158
|
+
async stop() {
|
|
1159
|
+
this.generation++;
|
|
1160
|
+
clearTimeout(this.restartTimer);
|
|
1161
|
+
this.abort?.abort();
|
|
1162
|
+
this.abort = null;
|
|
1163
|
+
const child = this.child;
|
|
1164
|
+
this.child = null;
|
|
1165
|
+
this.runningSince = null;
|
|
1166
|
+
this.active = null;
|
|
1167
|
+
const wasStopped = this.state === "stopped";
|
|
1168
|
+
this.setState("stopped");
|
|
1169
|
+
if (!wasStopped) this.options.logger.info(`${this.options.name} stopped`);
|
|
1170
|
+
if (child) await killTree(child);
|
|
1171
|
+
}
|
|
1172
|
+
async restart() {
|
|
1173
|
+
await this.stop();
|
|
1174
|
+
await this.start();
|
|
1175
|
+
}
|
|
1176
|
+
resolved() {
|
|
1177
|
+
const { host, port, version } = this.settings;
|
|
1178
|
+
return {
|
|
1179
|
+
host,
|
|
1180
|
+
port,
|
|
1181
|
+
version: version ?? this.options.defaultVersion
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
setState(state) {
|
|
1185
|
+
this.state = state;
|
|
1186
|
+
if (state === "running" || state === "failed" || state === "stopped") for (const resolve of this.waiters.splice(0)) resolve();
|
|
1187
|
+
}
|
|
1188
|
+
launch() {
|
|
1189
|
+
const generation = ++this.generation;
|
|
1190
|
+
const current = () => generation === this.generation;
|
|
1191
|
+
const settings = this.resolved();
|
|
1192
|
+
this.active = settings;
|
|
1193
|
+
const abort = new AbortController();
|
|
1194
|
+
this.abort = abort;
|
|
1195
|
+
this.setState("starting");
|
|
1196
|
+
this.options.logger.info(`starting ${this.options.name} ${settings.version} on ${settings.host}:${settings.port}`);
|
|
1197
|
+
this.options.prepare(settings, abort.signal).then((command) => {
|
|
1198
|
+
if (current()) this.spawn(command, settings, generation);
|
|
1199
|
+
}, (error) => {
|
|
1200
|
+
if (!current()) return;
|
|
1201
|
+
this.options.logger.error(`${this.options.name} could not be prepared: ${error instanceof Error ? error.message : String(error)}`);
|
|
1202
|
+
this.crashed(null, "failed to start");
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
spawn(command, settings, generation) {
|
|
1206
|
+
const current = () => generation === this.generation;
|
|
1207
|
+
const child = spawn(command.command, [...command.args], {
|
|
1208
|
+
...treeSpawnOptions,
|
|
1209
|
+
stdio: [
|
|
1210
|
+
"ignore",
|
|
1211
|
+
"pipe",
|
|
1212
|
+
"pipe"
|
|
1213
|
+
]
|
|
1214
|
+
});
|
|
1215
|
+
this.child = child;
|
|
1216
|
+
pipeLines(child, this.options.output);
|
|
1217
|
+
let exited = false;
|
|
1218
|
+
const onExit = (code, signal) => {
|
|
1219
|
+
if (exited) return;
|
|
1220
|
+
exited = true;
|
|
1221
|
+
if (!current()) return;
|
|
1222
|
+
this.child = null;
|
|
1223
|
+
killTree(child);
|
|
1224
|
+
const what = code !== null ? `exited with code ${code}` : signal !== null ? `was killed (${signal})` : "failed to start";
|
|
1225
|
+
this.crashed(code, what);
|
|
1226
|
+
};
|
|
1227
|
+
child.once("exit", onExit);
|
|
1228
|
+
child.once("error", (error) => {
|
|
1229
|
+
this.options.logger.error(`${this.options.name} could not be started: ${error.message}`);
|
|
1230
|
+
if (child.pid === void 0) onExit(null, null);
|
|
1231
|
+
});
|
|
1232
|
+
this.waitForPort(settings, generation, () => exited).then((open) => {
|
|
1233
|
+
if (!current() || exited) return;
|
|
1234
|
+
if (open) {
|
|
1235
|
+
this.runningSince = Date.now();
|
|
1236
|
+
this.setState("running");
|
|
1237
|
+
this.options.logger.info(`${this.options.name} running on port ${settings.port}`);
|
|
1238
|
+
} else {
|
|
1239
|
+
this.options.logger.error(`${this.options.name} did not open port ${settings.port} in time, killing it`);
|
|
1240
|
+
killTree(child);
|
|
1241
|
+
}
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
async waitForPort(settings, generation, exited) {
|
|
1245
|
+
const deadline = Date.now() + (this.options.readyTimeoutMs ?? 6e4);
|
|
1246
|
+
while (generation === this.generation && !exited() && Date.now() < deadline) {
|
|
1247
|
+
if (await portOpen(settings.host, settings.port)) return true;
|
|
1248
|
+
await delay(this.options.probeIntervalMs ?? 200);
|
|
1249
|
+
}
|
|
1250
|
+
return false;
|
|
1251
|
+
}
|
|
1252
|
+
crashed(code, what) {
|
|
1253
|
+
const { name, logger } = this.options;
|
|
1254
|
+
const ranForMs = this.runningSince === null ? 0 : Date.now() - this.runningSince;
|
|
1255
|
+
this.runningSince = null;
|
|
1256
|
+
this.exitCode = code;
|
|
1257
|
+
const decision = nextAfterCrash(this.crashesInRow, ranForMs, this.options.policy);
|
|
1258
|
+
this.crashesInRow = decision.crashesInRow;
|
|
1259
|
+
if (decision.action === "fail") {
|
|
1260
|
+
logger.error(`${name} ${what}, ${decision.crashesInRow} crashes in a row: giving up. Restart the bridge to try again`);
|
|
1261
|
+
this.setState("failed");
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
logger.error(`${name} ${what}, restart ${this.restarts + 1} in ${formatDelay(decision.delayMs)}`);
|
|
1265
|
+
this.setState("crashed");
|
|
1266
|
+
this.restartTimer = setTimeout(() => {
|
|
1267
|
+
this.restarts++;
|
|
1268
|
+
this.launch();
|
|
1269
|
+
}, decision.delayMs);
|
|
1270
|
+
}
|
|
1271
|
+
};
|
|
1272
|
+
function formatDelay(ms) {
|
|
1273
|
+
return ms >= 1e3 ? `${ms / 1e3} s` : `${ms} ms`;
|
|
1274
|
+
}
|
|
1275
|
+
// src/adapters/out/process/run-command.ts
|
|
1276
|
+
/** `RunCommand` that writes the program's output to `output` (stdout `info`, stderr `warn`). */
|
|
1277
|
+
function runCommand(output) {
|
|
1278
|
+
return async (command, args, { cwd, shell = false, signal } = {}) => {
|
|
1279
|
+
const child = shell ? spawn([command, ...args].join(" "), {
|
|
1280
|
+
cwd,
|
|
1281
|
+
shell: true,
|
|
1282
|
+
signal,
|
|
1283
|
+
windowsHide: true
|
|
1284
|
+
}) : spawn(command, [...args], {
|
|
1285
|
+
cwd,
|
|
1286
|
+
signal,
|
|
1287
|
+
windowsHide: true
|
|
1288
|
+
});
|
|
1289
|
+
child.stdin?.end();
|
|
1290
|
+
pipeLines(child, output);
|
|
1291
|
+
const [code, exitSignal] = await once(child, "close");
|
|
1292
|
+
if (code !== 0) {
|
|
1293
|
+
const how = code === null ? `was killed (${exitSignal})` : `exited with code ${code}`;
|
|
1294
|
+
throw new Error(`${command} ${args.join(" ")} ${how}`);
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
959
1298
|
// src/domain/config.ts
|
|
960
1299
|
const LOG_LEVELS = [
|
|
961
1300
|
"error",
|
|
@@ -998,6 +1337,14 @@ const port = integer(0, 65535);
|
|
|
998
1337
|
const boolean = (value) => typeof value === "boolean" ? void 0 : "must be true or false";
|
|
999
1338
|
const nonEmptyString = (value) => typeof value === "string" && value.trim() !== "" ? void 0 : "must be a non-empty string";
|
|
1000
1339
|
const nullableString = (value) => value === null ? void 0 : nonEmptyString(value) && "must be a non-empty string or null";
|
|
1340
|
+
/**
|
|
1341
|
+
* An exact npm version (`1.56.1`, `1.57.0-beta-1`). Service versions end up on an
|
|
1342
|
+
* `npm install` command line, so nothing else may get through.
|
|
1343
|
+
*/
|
|
1344
|
+
function isExactVersion(value) {
|
|
1345
|
+
return typeof value === "string" && /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/.test(value);
|
|
1346
|
+
}
|
|
1347
|
+
const nullableVersion = (value) => value === null || isExactVersion(value) ? void 0 : "must be an exact version like 1.56.1, or null";
|
|
1001
1348
|
const oneOf = (values) => (value) => typeof value === "string" && values.includes(value) ? void 0 : `must be one of ${values.join(", ")}`;
|
|
1002
1349
|
const encoding = (value) => {
|
|
1003
1350
|
if (typeof value !== "string") return "must be a string";
|
|
@@ -1023,12 +1370,12 @@ const SCHEMA = {
|
|
|
1023
1370
|
playwright: {
|
|
1024
1371
|
enabled: boolean,
|
|
1025
1372
|
port,
|
|
1026
|
-
version:
|
|
1373
|
+
version: nullableVersion
|
|
1027
1374
|
},
|
|
1028
1375
|
mcp: {
|
|
1029
1376
|
enabled: boolean,
|
|
1030
1377
|
port,
|
|
1031
|
-
version:
|
|
1378
|
+
version: nullableVersion
|
|
1032
1379
|
},
|
|
1033
1380
|
input: { enabled: boolean },
|
|
1034
1381
|
buffer: { maxEntries: integer(1, 1e6) },
|
|
@@ -1116,6 +1463,97 @@ function overriddenFields(file, overrides) {
|
|
|
1116
1463
|
override
|
|
1117
1464
|
}));
|
|
1118
1465
|
}
|
|
1466
|
+
// src/adapters/out/playwright/playwright-installer.ts
|
|
1467
|
+
const cliOf = (dir) => path.join(dir, "node_modules", "playwright", "cli.js");
|
|
1468
|
+
/**
|
|
1469
|
+
* Makes sure `playwright@<version>` and its Chromium are installed (docs/playwright-mcp.md)
|
|
1470
|
+
* and returns the path of its `cli.js`. The package goes into `<cacheRoot>/<version>` once.
|
|
1471
|
+
* The Chromium install runs every time: Playwright skips it when the build is already there.
|
|
1472
|
+
*/
|
|
1473
|
+
async function ensurePlaywright(options) {
|
|
1474
|
+
const { version, cacheRoot, run, logger, signal } = options;
|
|
1475
|
+
if (!isExactVersion(version)) throw new Error(`Invalid Playwright version ${JSON.stringify(version)}`);
|
|
1476
|
+
const dir = path.join(cacheRoot, version);
|
|
1477
|
+
const cli = cliOf(dir);
|
|
1478
|
+
const withSignal = signal ? { signal } : {};
|
|
1479
|
+
if (!existsSync(cli)) {
|
|
1480
|
+
logger.info(`installing playwright ${version} into ${dir}`);
|
|
1481
|
+
const tmp = `${dir}.tmp-${process.pid}-${Date.now()}`;
|
|
1482
|
+
try {
|
|
1483
|
+
await mkdir(tmp, { recursive: true });
|
|
1484
|
+
await writeFile(path.join(tmp, "package.json"), "{ \"private\": true }\n");
|
|
1485
|
+
await run("npm", [
|
|
1486
|
+
"install",
|
|
1487
|
+
"--ignore-scripts",
|
|
1488
|
+
"--no-audit",
|
|
1489
|
+
"--no-fund",
|
|
1490
|
+
`playwright@${version}`
|
|
1491
|
+
], {
|
|
1492
|
+
cwd: tmp,
|
|
1493
|
+
shell: (options.platform ?? process.platform) === "win32",
|
|
1494
|
+
...withSignal
|
|
1495
|
+
});
|
|
1496
|
+
if (!existsSync(cliOf(tmp))) throw new Error(`npm did not install playwright ${version}`);
|
|
1497
|
+
try {
|
|
1498
|
+
await rename(tmp, dir);
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
if (!existsSync(cli)) throw error;
|
|
1501
|
+
}
|
|
1502
|
+
} finally {
|
|
1503
|
+
await rm(tmp, {
|
|
1504
|
+
recursive: true,
|
|
1505
|
+
force: true
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
logger.info(`checking the Chromium of playwright ${version}`);
|
|
1510
|
+
await run(process.execPath, [
|
|
1511
|
+
cli,
|
|
1512
|
+
"install",
|
|
1513
|
+
"--no-shell",
|
|
1514
|
+
"chromium"
|
|
1515
|
+
], withSignal);
|
|
1516
|
+
return cli;
|
|
1517
|
+
}
|
|
1518
|
+
// src/adapters/out/playwright/playwright-server.ts
|
|
1519
|
+
/**
|
|
1520
|
+
* Playwright version of the server when `playwright.version` is `null`. Clients need the same
|
|
1521
|
+
* version (docs/playwright-mcp.md#playwright-server).
|
|
1522
|
+
*/
|
|
1523
|
+
const DEFAULT_PLAYWRIGHT_VERSION = "1.65.0";
|
|
1524
|
+
/** Supervisor for `playwright run-server` in the configured version. */
|
|
1525
|
+
function playwrightServer(options) {
|
|
1526
|
+
const { cacheRoot, output } = options;
|
|
1527
|
+
const prepare = async ({ host, port, version }, signal) => {
|
|
1528
|
+
const cli = await ensurePlaywright({
|
|
1529
|
+
version,
|
|
1530
|
+
cacheRoot,
|
|
1531
|
+
run: runCommand(output),
|
|
1532
|
+
logger: output,
|
|
1533
|
+
signal
|
|
1534
|
+
});
|
|
1535
|
+
return {
|
|
1536
|
+
command: process.execPath,
|
|
1537
|
+
args: [
|
|
1538
|
+
cli,
|
|
1539
|
+
"run-server",
|
|
1540
|
+
"--port",
|
|
1541
|
+
String(port),
|
|
1542
|
+
"--host",
|
|
1543
|
+
host
|
|
1544
|
+
]
|
|
1545
|
+
};
|
|
1546
|
+
};
|
|
1547
|
+
return new ChildProcessSupervisor({
|
|
1548
|
+
name: "playwright",
|
|
1549
|
+
settings: options.settings,
|
|
1550
|
+
defaultVersion: DEFAULT_PLAYWRIGHT_VERSION,
|
|
1551
|
+
prepare: options.launch ?? prepare,
|
|
1552
|
+
logger: options.logger,
|
|
1553
|
+
output,
|
|
1554
|
+
...options.policy ? { policy: options.policy } : {}
|
|
1555
|
+
});
|
|
1556
|
+
}
|
|
1119
1557
|
// src/application/services/config-service.ts
|
|
1120
1558
|
const show$1 = (value) => JSON.stringify(value);
|
|
1121
1559
|
/**
|
|
@@ -1286,7 +1724,7 @@ var StatusService = class {
|
|
|
1286
1724
|
this.state = state;
|
|
1287
1725
|
}
|
|
1288
1726
|
status() {
|
|
1289
|
-
const { capture, repository, config, version, apiVersion } = this.deps;
|
|
1727
|
+
const { capture, repository, playwright, config, version, apiVersion } = this.deps;
|
|
1290
1728
|
const { listener, connections } = capture.captureStatus();
|
|
1291
1729
|
const { tcp, input } = config();
|
|
1292
1730
|
return {
|
|
@@ -1303,6 +1741,7 @@ var StatusService = class {
|
|
|
1303
1741
|
connections
|
|
1304
1742
|
},
|
|
1305
1743
|
buffer: repository.stats(),
|
|
1744
|
+
playwright: playwright.status(),
|
|
1306
1745
|
input: { enabled: input.enabled }
|
|
1307
1746
|
};
|
|
1308
1747
|
}
|
|
@@ -1474,6 +1913,12 @@ function listenFailed(section, error) {
|
|
|
1474
1913
|
return new AppError("INVALID_REQUEST", `${field}: ${error.message}`, { field });
|
|
1475
1914
|
}
|
|
1476
1915
|
const show = (value) => JSON.stringify(value);
|
|
1916
|
+
/** The Playwright server listens on the same interface as the HTTP API (`--host`). */
|
|
1917
|
+
const playwrightSettings = (config) => ({
|
|
1918
|
+
host: config.http.host,
|
|
1919
|
+
port: config.playwright.port,
|
|
1920
|
+
version: config.playwright.version
|
|
1921
|
+
});
|
|
1477
1922
|
/**
|
|
1478
1923
|
* Starts the bridge. An invalid setting rejects with a `ConfigError` before anything starts.
|
|
1479
1924
|
* Any other failure to start is written to the bridge log before the promise rejects, and so is
|
|
@@ -1515,13 +1960,22 @@ async function startBridge(options) {
|
|
|
1515
1960
|
file: savedFile,
|
|
1516
1961
|
pinned: new Map([...configFields(overrides).keys()].map((field) => [field, sourceOf(field)]))
|
|
1517
1962
|
});
|
|
1963
|
+
const playwright = playwrightServer({
|
|
1964
|
+
settings: playwrightSettings(config),
|
|
1965
|
+
cacheRoot: options.playwrightDir ?? defaultPlaywrightDir(),
|
|
1966
|
+
logger: bridgeLogger,
|
|
1967
|
+
output: log.for("playwright-server"),
|
|
1968
|
+
...options.playwrightLaunch ? { launch: options.playwrightLaunch } : {},
|
|
1969
|
+
...options.restartPolicy ? { policy: options.restartPolicy } : {}
|
|
1970
|
+
});
|
|
1518
1971
|
const tcp = new TcpListener(speech, clock, log.for("speech-capture"));
|
|
1519
1972
|
const status = new StatusService({
|
|
1520
1973
|
capture: tcp,
|
|
1521
1974
|
repository,
|
|
1975
|
+
playwright,
|
|
1522
1976
|
config: () => configService.get(),
|
|
1523
1977
|
version,
|
|
1524
|
-
apiVersion: "1.
|
|
1978
|
+
apiVersion: "1.1"
|
|
1525
1979
|
});
|
|
1526
1980
|
const http = new HttpServer({
|
|
1527
1981
|
speech,
|
|
@@ -1577,9 +2031,25 @@ async function startBridge(options) {
|
|
|
1577
2031
|
sinks = defaultSinks(next).sinks;
|
|
1578
2032
|
for (const sink of sinks) log.addSink(sink);
|
|
1579
2033
|
});
|
|
1580
|
-
|
|
1581
|
-
|
|
2034
|
+
configService.onChange(async (next, previous) => {
|
|
2035
|
+
const changed = changedSections(previous, next);
|
|
2036
|
+
const hostChanged = changed.includes("http") && next.http.host !== previous.http.host;
|
|
2037
|
+
if (!changed.includes("playwright") && !hostChanged) return;
|
|
2038
|
+
playwright.reconfigure(playwrightSettings(next));
|
|
2039
|
+
if (next.playwright.enabled) playwright.restart().catch((error) => {
|
|
2040
|
+
bridgeLogger.error(`playwright restart failed: ${String(error)}`);
|
|
2041
|
+
});
|
|
2042
|
+
else await playwright.stop();
|
|
2043
|
+
});
|
|
1582
2044
|
let stopping;
|
|
2045
|
+
const ready = () => {
|
|
2046
|
+
if (stopping !== void 0) return;
|
|
2047
|
+
status.setState("ready");
|
|
2048
|
+
bridgeLogger.info("ready");
|
|
2049
|
+
};
|
|
2050
|
+
const enabled = config.playwright.enabled ? [playwright] : [];
|
|
2051
|
+
if (enabled.length === 0) ready();
|
|
2052
|
+
else Promise.all(enabled.map((service) => service.start())).then(ready);
|
|
1583
2053
|
return {
|
|
1584
2054
|
get tcpPort() {
|
|
1585
2055
|
return tcpPort;
|
|
@@ -1600,8 +2070,12 @@ async function startBridge(options) {
|
|
|
1600
2070
|
bridgeLogger.info(reason === void 0 ? "stopping" : `stopping (${reason})`);
|
|
1601
2071
|
waits.shutdown();
|
|
1602
2072
|
const errors = [];
|
|
1603
|
-
for (const
|
|
1604
|
-
|
|
2073
|
+
for (const part of [
|
|
2074
|
+
http,
|
|
2075
|
+
tcp,
|
|
2076
|
+
playwright
|
|
2077
|
+
]) try {
|
|
2078
|
+
await part.stop();
|
|
1605
2079
|
} catch (error) {
|
|
1606
2080
|
bridgeLogger.error(`shutdown failed: ${String(error)}`);
|
|
1607
2081
|
errors.push(error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "screenreader-speech-capture",
|
|
3
|
-
"version": "0.0.1
|
|
3
|
+
"version": "0.1.0-feat-playwright-supervisor.1",
|
|
4
4
|
"description": "Capture what a screen reader says (JAWS / NVDA via a SAPI 5 socket voice) and expose it over HTTP for automated tests.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"@screenreader-speech-capture/shared": "0.0.0",
|
|
18
18
|
"tsdown": "0.23.7"
|
|
19
19
|
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/TimoSto/screenreader-speech-capture.git",
|
|
23
|
+
"directory": "packages/bridge"
|
|
24
|
+
},
|
|
20
25
|
"scripts": {
|
|
21
26
|
"build": "tsdown",
|
|
22
27
|
"dev": "tsx watch src/adapters/in/cli/cli.ts",
|