screenreader-speech-capture 0.0.1-preview.1 → 0.0.1-preview.3
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 +110 -14
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import net from "node:net";
|
|
3
3
|
import { TextDecoder as TextDecoder$1 } from "node:util";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import os from "node:os";
|
|
4
7
|
import { performance } from "node:perf_hooks";
|
|
5
8
|
import { monotonicFactory } from "ulid";
|
|
6
9
|
// package.json
|
|
7
|
-
var version = "0.0.1-preview.
|
|
10
|
+
var version = "0.0.1-preview.3";
|
|
8
11
|
// src/adapters/in/tcp/text-to-socket-parser.ts
|
|
9
12
|
const LF = 10;
|
|
10
13
|
const CR = 13;
|
|
@@ -160,6 +163,48 @@ var TcpListener = class {
|
|
|
160
163
|
});
|
|
161
164
|
}
|
|
162
165
|
};
|
|
166
|
+
// src/adapters/out/log/file-sink.ts
|
|
167
|
+
/**
|
|
168
|
+
* Bridge log file (docs/operations.md#logging): no colours, includes the speech channel.
|
|
169
|
+
* Writes synchronously so nothing is lost on a crash. Log volume is small.
|
|
170
|
+
*/
|
|
171
|
+
function fileSink(file, options = {}) {
|
|
172
|
+
const { maxBytes = 10485760, keep = 5 } = options;
|
|
173
|
+
const onError = options.onError ?? ((error) => process.stderr.write(`screenreader-speech-capture: log file disabled: ${error.message}\n`));
|
|
174
|
+
let size = 0;
|
|
175
|
+
let failed = false;
|
|
176
|
+
try {
|
|
177
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
178
|
+
size = fs.existsSync(file) ? fs.statSync(file).size : 0;
|
|
179
|
+
} catch (error) {
|
|
180
|
+
failed = true;
|
|
181
|
+
onError(error);
|
|
182
|
+
}
|
|
183
|
+
const rotate = () => {
|
|
184
|
+
fs.rmSync(`${file}.${keep}`, { force: true });
|
|
185
|
+
for (let i = keep - 1; i >= 1; i--) if (fs.existsSync(`${file}.${i}`)) fs.renameSync(`${file}.${i}`, `${file}.${i + 1}`);
|
|
186
|
+
if (keep >= 1) fs.renameSync(file, `${file}.1`);
|
|
187
|
+
else fs.rmSync(file, { force: true });
|
|
188
|
+
size = 0;
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
colour: false,
|
|
192
|
+
speech: true,
|
|
193
|
+
write(line) {
|
|
194
|
+
if (failed) return;
|
|
195
|
+
const data = `${line}\n`;
|
|
196
|
+
const bytes = Buffer.byteLength(data);
|
|
197
|
+
try {
|
|
198
|
+
if (size > 0 && size + bytes > maxBytes) rotate();
|
|
199
|
+
fs.appendFileSync(file, data);
|
|
200
|
+
size += bytes;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
failed = true;
|
|
203
|
+
onError(error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
163
208
|
// src/adapters/out/log/log.ts
|
|
164
209
|
const ESC = "\x1B[";
|
|
165
210
|
const RESET = `${ESC}0m`;
|
|
@@ -209,9 +254,11 @@ function wantsColour(isTTY, env) {
|
|
|
209
254
|
return isTTY === true;
|
|
210
255
|
}
|
|
211
256
|
/** Console sink on stdout. */
|
|
212
|
-
function consoleSink(
|
|
257
|
+
function consoleSink(options = {}) {
|
|
258
|
+
const { speech = false, stream = process.stdout, env = process.env } = options;
|
|
213
259
|
return {
|
|
214
260
|
colour: wantsColour(stream.isTTY, env),
|
|
261
|
+
speech,
|
|
215
262
|
write: (line) => void stream.write(`${line}\n`)
|
|
216
263
|
};
|
|
217
264
|
}
|
|
@@ -235,8 +282,9 @@ var BridgeLog = class {
|
|
|
235
282
|
const index = this.sinks.indexOf(sink);
|
|
236
283
|
if (index !== -1) this.sinks.splice(index, 1);
|
|
237
284
|
}
|
|
238
|
-
for(prefix) {
|
|
239
|
-
const
|
|
285
|
+
for(prefix, options = {}) {
|
|
286
|
+
const channel = options.channel ?? "log";
|
|
287
|
+
const log = (level) => (message) => this.write(prefix, level, message, channel);
|
|
240
288
|
return {
|
|
241
289
|
error: log("error"),
|
|
242
290
|
warn: log("warn"),
|
|
@@ -244,12 +292,36 @@ var BridgeLog = class {
|
|
|
244
292
|
debug: log("debug")
|
|
245
293
|
};
|
|
246
294
|
}
|
|
247
|
-
|
|
295
|
+
/** Writes an INFO line whatever the level is (for the startup line). */
|
|
296
|
+
always(prefix, message) {
|
|
297
|
+
this.emit(prefix, "info", message, "log");
|
|
298
|
+
}
|
|
299
|
+
write(prefix, level, message, channel) {
|
|
248
300
|
if (LEVEL_RANK[level] > LEVEL_RANK[this.level]) return;
|
|
301
|
+
this.emit(prefix, level, message, channel);
|
|
302
|
+
}
|
|
303
|
+
emit(prefix, level, message, channel) {
|
|
249
304
|
const time = this.now();
|
|
250
|
-
for (const sink of this.sinks)
|
|
305
|
+
for (const sink of this.sinks) {
|
|
306
|
+
if (channel === "speech" && !sink.speech) continue;
|
|
307
|
+
for (const line of formatLogLines(prefix, level, time, message, sink.colour)) sink.write(line);
|
|
308
|
+
}
|
|
251
309
|
}
|
|
252
310
|
};
|
|
311
|
+
// src/adapters/out/log/paths.ts
|
|
312
|
+
const APP = "screenreader-speech-capture";
|
|
313
|
+
/**
|
|
314
|
+
* Default bridge log file: `%LOCALAPPDATA%\screenreader-speech-capture\logs\bridge.log` on
|
|
315
|
+
* Windows, `$XDG_STATE_HOME` (or `~/.local/state`) elsewhere, for development.
|
|
316
|
+
*/
|
|
317
|
+
function defaultLogFile(platform = process.platform, env = process.env, home = os.homedir()) {
|
|
318
|
+
if (platform === "win32") {
|
|
319
|
+
const base = env.LOCALAPPDATA ?? path.win32.join(home, "AppData", "Local");
|
|
320
|
+
return path.win32.join(base, APP, "logs", "bridge.log");
|
|
321
|
+
}
|
|
322
|
+
const base = env.XDG_STATE_HOME ?? path.posix.join(home, ".local", "state");
|
|
323
|
+
return path.posix.join(base, APP, "logs", "bridge.log");
|
|
324
|
+
}
|
|
253
325
|
// src/domain/errors.ts
|
|
254
326
|
/** An expected error with a stable code, for example a bad request or a timeout. */
|
|
255
327
|
var AppError = class extends Error {
|
|
@@ -299,7 +371,8 @@ const DEFAULT_CONFIG = {
|
|
|
299
371
|
buffer: { maxEntries: 1e4 },
|
|
300
372
|
log: {
|
|
301
373
|
level: "info",
|
|
302
|
-
file: null
|
|
374
|
+
file: null,
|
|
375
|
+
consoleSpeech: false
|
|
303
376
|
}
|
|
304
377
|
};
|
|
305
378
|
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}`;
|
|
@@ -343,7 +416,8 @@ const SCHEMA = {
|
|
|
343
416
|
buffer: { maxEntries: integer(1, 1e6) },
|
|
344
417
|
log: {
|
|
345
418
|
level: oneOf(LOG_LEVELS),
|
|
346
|
-
file: nullableString
|
|
419
|
+
file: nullableString,
|
|
420
|
+
consoleSpeech: boolean
|
|
347
421
|
}
|
|
348
422
|
};
|
|
349
423
|
function isPlainObject(value) {
|
|
@@ -533,7 +607,7 @@ var RecordSpeechService = class {
|
|
|
533
607
|
this.deps.logger.info(`connection ${connectionId}: voice=${voice}`);
|
|
534
608
|
}
|
|
535
609
|
record(connectionId, text) {
|
|
536
|
-
const { repository, events, clock, ids,
|
|
610
|
+
const { repository, events, clock, ids, transcript } = this.deps;
|
|
537
611
|
const entry = repository.add({
|
|
538
612
|
id: ids.next(),
|
|
539
613
|
kind: "speech",
|
|
@@ -544,7 +618,7 @@ var RecordSpeechService = class {
|
|
|
544
618
|
voice: this.open.get(connectionId)?.voice ?? null,
|
|
545
619
|
connectionId
|
|
546
620
|
});
|
|
547
|
-
|
|
621
|
+
transcript.debug(`#${entry.seq} (connection ${connectionId}) ${text}`);
|
|
548
622
|
events.publish({
|
|
549
623
|
type: "entry",
|
|
550
624
|
entry
|
|
@@ -601,11 +675,19 @@ var SpeechService = class {
|
|
|
601
675
|
}
|
|
602
676
|
};
|
|
603
677
|
// src/main.ts
|
|
678
|
+
function startupMessage(config, version) {
|
|
679
|
+
const { level, file, consoleSpeech } = config.log;
|
|
680
|
+
let message = `starting, version ${version}, log level ${level}`;
|
|
681
|
+
if (file !== null) message += `, log file ${file}`;
|
|
682
|
+
if (level !== "debug") return `${message} (speech text is logged at debug)`;
|
|
683
|
+
if (consoleSpeech) return `${message} (speech text also on the console)`;
|
|
684
|
+
return file === null ? `${message} (speech text is not shown: no log file)` : `${message} (speech text goes to the log file only, not to the console)`;
|
|
685
|
+
}
|
|
604
686
|
async function startBridge(options) {
|
|
605
687
|
const { config, version } = options;
|
|
606
688
|
const log = options.log ?? new BridgeLog(config.log.level, [consoleSink()]);
|
|
607
689
|
const bridgeLogger = log.for("bridge");
|
|
608
|
-
|
|
690
|
+
log.always("bridge", startupMessage(config, version));
|
|
609
691
|
const clock = options.clock ?? new SystemClock();
|
|
610
692
|
const ids = new UlidGenerator();
|
|
611
693
|
const repository = new RingBufferRepository(config.buffer.maxEntries);
|
|
@@ -615,7 +697,8 @@ async function startBridge(options) {
|
|
|
615
697
|
events,
|
|
616
698
|
clock,
|
|
617
699
|
ids,
|
|
618
|
-
logger: log.for("speech-capture")
|
|
700
|
+
logger: log.for("speech-capture"),
|
|
701
|
+
transcript: log.for("speech-capture", { channel: "speech" })
|
|
619
702
|
});
|
|
620
703
|
const speech = new SpeechService({
|
|
621
704
|
repository,
|
|
@@ -639,6 +722,15 @@ async function startBridge(options) {
|
|
|
639
722
|
};
|
|
640
723
|
}
|
|
641
724
|
// src/cli.ts
|
|
725
|
+
function flag(value) {
|
|
726
|
+
if (value === void 0 || value === "") return void 0;
|
|
727
|
+
return ![
|
|
728
|
+
"0",
|
|
729
|
+
"false",
|
|
730
|
+
"no",
|
|
731
|
+
"off"
|
|
732
|
+
].includes(value.toLowerCase());
|
|
733
|
+
}
|
|
642
734
|
function configFromEnv(env) {
|
|
643
735
|
const port = env.SR_SPEECH_TCP_PORT;
|
|
644
736
|
return mergeConfig(DEFAULT_CONFIG, {
|
|
@@ -646,7 +738,11 @@ function configFromEnv(env) {
|
|
|
646
738
|
port: port === void 0 ? void 0 : Number(port),
|
|
647
739
|
encoding: env.SR_SPEECH_TCP_ENCODING
|
|
648
740
|
},
|
|
649
|
-
log: {
|
|
741
|
+
log: {
|
|
742
|
+
level: env.SR_SPEECH_LOG_LEVEL,
|
|
743
|
+
file: env.SR_SPEECH_BRIDGE_LOG ?? defaultLogFile(),
|
|
744
|
+
consoleSpeech: flag(env.SR_SPEECH_CONSOLE_SPEECH)
|
|
745
|
+
}
|
|
650
746
|
});
|
|
651
747
|
}
|
|
652
748
|
let config;
|
|
@@ -656,7 +752,7 @@ try {
|
|
|
656
752
|
process.stderr.write(`screenreader-speech-capture: ${error.message}\n`);
|
|
657
753
|
process.exit(2);
|
|
658
754
|
}
|
|
659
|
-
const log = new BridgeLog(config.log.level, [consoleSink()]);
|
|
755
|
+
const log = new BridgeLog(config.log.level, [consoleSink({ speech: config.log.consoleSpeech }), ...config.log.file === null ? [] : [fileSink(config.log.file)]]);
|
|
660
756
|
try {
|
|
661
757
|
const bridge = await startBridge({
|
|
662
758
|
config,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "screenreader-speech-capture",
|
|
3
|
-
"version": "0.0.1-preview.
|
|
3
|
+
"version": "0.0.1-preview.3",
|
|
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",
|