screenreader-speech-capture 0.0.1-preview.0 → 0.0.1-preview.2

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.
Files changed (2) hide show
  1. package/dist/cli.mjs +106 -3
  2. package/package.json +8 -8
package/dist/cli.mjs CHANGED
@@ -4,7 +4,7 @@ import { TextDecoder as TextDecoder$1 } from "node:util";
4
4
  import { performance } from "node:perf_hooks";
5
5
  import { monotonicFactory } from "ulid";
6
6
  // package.json
7
- var version = "0.0.1-preview.0";
7
+ var version = "0.0.1-preview.2";
8
8
  // src/adapters/in/tcp/text-to-socket-parser.ts
9
9
  const LF = 10;
10
10
  const CR = 13;
@@ -244,8 +244,15 @@ var BridgeLog = class {
244
244
  debug: log("debug")
245
245
  };
246
246
  }
247
+ /** Writes an INFO line whatever the level is (for the startup line). */
248
+ always(prefix, message) {
249
+ this.emit(prefix, "info", message);
250
+ }
247
251
  write(prefix, level, message) {
248
252
  if (LEVEL_RANK[level] > LEVEL_RANK[this.level]) return;
253
+ this.emit(prefix, level, message);
254
+ }
255
+ emit(prefix, level, message) {
249
256
  const time = this.now();
250
257
  for (const sink of this.sinks) for (const line of formatLogLines(prefix, level, time, message, sink.colour)) sink.write(line);
251
258
  }
@@ -266,6 +273,13 @@ var AppError = class extends Error {
266
273
  function invalidField(field, message) {
267
274
  return new AppError("INVALID_REQUEST", `${field}: ${message}`, { field });
268
275
  }
276
+ // src/domain/config.ts
277
+ const LOG_LEVELS = [
278
+ "error",
279
+ "warn",
280
+ "info",
281
+ "debug"
282
+ ];
269
283
  const DEFAULT_CONFIG = {
270
284
  tcp: {
271
285
  host: "127.0.0.1",
@@ -295,6 +309,79 @@ const DEFAULT_CONFIG = {
295
309
  file: null
296
310
  }
297
311
  };
312
+ const integer = (min, max) => (value) => typeof value === "number" && Number.isInteger(value) && value >= min && value <= max ? void 0 : `must be an integer from ${min} to ${max}`;
313
+ const port = integer(0, 65535);
314
+ const boolean = (value) => typeof value === "boolean" ? void 0 : "must be true or false";
315
+ const nonEmptyString = (value) => typeof value === "string" && value.trim() !== "" ? void 0 : "must be a non-empty string";
316
+ const nullableString = (value) => value === null ? void 0 : nonEmptyString(value) && "must be a non-empty string or null";
317
+ const oneOf = (values) => (value) => typeof value === "string" && values.includes(value) ? void 0 : `must be one of ${values.join(", ")}`;
318
+ const encoding = (value) => {
319
+ if (typeof value !== "string") return "must be a string";
320
+ try {
321
+ new TextDecoder(value);
322
+ return;
323
+ } catch {
324
+ return `unknown encoding "${value}"`;
325
+ }
326
+ };
327
+ const SCHEMA = {
328
+ tcp: {
329
+ host: nonEmptyString,
330
+ port,
331
+ format: oneOf(["text-to-socket"]),
332
+ encoding,
333
+ maxLineBytes: integer(1024, 16777216)
334
+ },
335
+ http: {
336
+ host: nonEmptyString,
337
+ port
338
+ },
339
+ playwright: {
340
+ enabled: boolean,
341
+ port,
342
+ version: nullableString
343
+ },
344
+ mcp: {
345
+ enabled: boolean,
346
+ port,
347
+ version: nullableString
348
+ },
349
+ input: { enabled: boolean },
350
+ buffer: { maxEntries: integer(1, 1e6) },
351
+ log: {
352
+ level: oneOf(LOG_LEVELS),
353
+ file: nullableString
354
+ }
355
+ };
356
+ function isPlainObject(value) {
357
+ return typeof value === "object" && value !== null && !Array.isArray(value);
358
+ }
359
+ /**
360
+ * Applies a partial configuration to `base` and validates the result.
361
+ * Throws `INVALID_REQUEST` naming the first invalid or unknown field (`details.field`).
362
+ */
363
+ function mergeConfig(base, patch) {
364
+ if (patch === void 0 || patch === null) return base;
365
+ if (!isPlainObject(patch)) throw invalidField("config", "must be an object");
366
+ const result = {};
367
+ for (const section of Object.keys(base)) result[section] = { ...base[section] };
368
+ for (const [section, values] of Object.entries(patch)) {
369
+ const sectionSchema = SCHEMA[section];
370
+ if (sectionSchema === void 0) throw invalidField(section, "unknown setting");
371
+ if (values === void 0) continue;
372
+ if (!isPlainObject(values)) throw invalidField(section, "must be an object");
373
+ for (const [key, value] of Object.entries(values)) {
374
+ const field = `${section}.${key}`;
375
+ const check = sectionSchema[key];
376
+ if (check === void 0) throw invalidField(field, "unknown setting");
377
+ if (value === void 0) continue;
378
+ const problem = check(value);
379
+ if (problem !== void 0) throw invalidField(field, problem);
380
+ result[section][key] = value;
381
+ }
382
+ }
383
+ return result;
384
+ }
298
385
  // src/adapters/out/clock/system-clock.ts
299
386
  var SystemClock = class {
300
387
  now() {
@@ -525,7 +612,7 @@ async function startBridge(options) {
525
612
  const { config, version } = options;
526
613
  const log = options.log ?? new BridgeLog(config.log.level, [consoleSink()]);
527
614
  const bridgeLogger = log.for("bridge");
528
- bridgeLogger.info(`starting, version ${version}`);
615
+ log.always("bridge", `starting, version ${version}, log level ${config.log.level}` + (config.log.level === "debug" ? "" : " (set SR_SPEECH_LOG_LEVEL=debug to see the speech)"));
529
616
  const clock = options.clock ?? new SystemClock();
530
617
  const ids = new UlidGenerator();
531
618
  const repository = new RingBufferRepository(config.buffer.maxEntries);
@@ -559,7 +646,23 @@ async function startBridge(options) {
559
646
  };
560
647
  }
561
648
  // src/cli.ts
562
- const config = DEFAULT_CONFIG;
649
+ function configFromEnv(env) {
650
+ const port = env.SR_SPEECH_TCP_PORT;
651
+ return mergeConfig(DEFAULT_CONFIG, {
652
+ tcp: {
653
+ port: port === void 0 ? void 0 : Number(port),
654
+ encoding: env.SR_SPEECH_TCP_ENCODING
655
+ },
656
+ log: { level: env.SR_SPEECH_LOG_LEVEL }
657
+ });
658
+ }
659
+ let config;
660
+ try {
661
+ config = configFromEnv(process.env);
662
+ } catch (error) {
663
+ process.stderr.write(`screenreader-speech-capture: ${error.message}\n`);
664
+ process.exit(2);
665
+ }
563
666
  const log = new BridgeLog(config.log.level, [consoleSink()]);
564
667
  try {
565
668
  const bridge = await startBridge({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screenreader-speech-capture",
3
- "version": "0.0.1-preview.0",
3
+ "version": "0.0.1-preview.2",
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",
@@ -13,16 +13,16 @@
13
13
  "files": [
14
14
  "dist"
15
15
  ],
16
- "scripts": {
17
- "build": "tsdown",
18
- "dev": "tsx watch src/cli.ts",
19
- "typecheck": "tsc --noEmit"
20
- },
21
16
  "dependencies": {
22
17
  "ulid": "^3.0.2"
23
18
  },
24
19
  "devDependencies": {
25
- "@screenreader-speech-capture/shared": "workspace:*",
20
+ "@screenreader-speech-capture/shared": "0.0.0",
26
21
  "tsdown": "^0.23.7"
22
+ },
23
+ "scripts": {
24
+ "build": "tsdown",
25
+ "dev": "tsx watch src/cli.ts",
26
+ "typecheck": "tsc --noEmit"
27
27
  }
28
- }
28
+ }