screenreader-speech-capture 0.0.1-preview.2 → 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 +106 -17
- 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"),
|
|
@@ -246,17 +294,34 @@ var BridgeLog = class {
|
|
|
246
294
|
}
|
|
247
295
|
/** Writes an INFO line whatever the level is (for the startup line). */
|
|
248
296
|
always(prefix, message) {
|
|
249
|
-
this.emit(prefix, "info", message);
|
|
297
|
+
this.emit(prefix, "info", message, "log");
|
|
250
298
|
}
|
|
251
|
-
write(prefix, level, message) {
|
|
299
|
+
write(prefix, level, message, channel) {
|
|
252
300
|
if (LEVEL_RANK[level] > LEVEL_RANK[this.level]) return;
|
|
253
|
-
this.emit(prefix, level, message);
|
|
301
|
+
this.emit(prefix, level, message, channel);
|
|
254
302
|
}
|
|
255
|
-
emit(prefix, level, message) {
|
|
303
|
+
emit(prefix, level, message, channel) {
|
|
256
304
|
const time = this.now();
|
|
257
|
-
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
|
+
}
|
|
258
309
|
}
|
|
259
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
|
+
}
|
|
260
325
|
// src/domain/errors.ts
|
|
261
326
|
/** An expected error with a stable code, for example a bad request or a timeout. */
|
|
262
327
|
var AppError = class extends Error {
|
|
@@ -306,7 +371,8 @@ const DEFAULT_CONFIG = {
|
|
|
306
371
|
buffer: { maxEntries: 1e4 },
|
|
307
372
|
log: {
|
|
308
373
|
level: "info",
|
|
309
|
-
file: null
|
|
374
|
+
file: null,
|
|
375
|
+
consoleSpeech: false
|
|
310
376
|
}
|
|
311
377
|
};
|
|
312
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}`;
|
|
@@ -350,7 +416,8 @@ const SCHEMA = {
|
|
|
350
416
|
buffer: { maxEntries: integer(1, 1e6) },
|
|
351
417
|
log: {
|
|
352
418
|
level: oneOf(LOG_LEVELS),
|
|
353
|
-
file: nullableString
|
|
419
|
+
file: nullableString,
|
|
420
|
+
consoleSpeech: boolean
|
|
354
421
|
}
|
|
355
422
|
};
|
|
356
423
|
function isPlainObject(value) {
|
|
@@ -540,7 +607,7 @@ var RecordSpeechService = class {
|
|
|
540
607
|
this.deps.logger.info(`connection ${connectionId}: voice=${voice}`);
|
|
541
608
|
}
|
|
542
609
|
record(connectionId, text) {
|
|
543
|
-
const { repository, events, clock, ids,
|
|
610
|
+
const { repository, events, clock, ids, transcript } = this.deps;
|
|
544
611
|
const entry = repository.add({
|
|
545
612
|
id: ids.next(),
|
|
546
613
|
kind: "speech",
|
|
@@ -551,7 +618,7 @@ var RecordSpeechService = class {
|
|
|
551
618
|
voice: this.open.get(connectionId)?.voice ?? null,
|
|
552
619
|
connectionId
|
|
553
620
|
});
|
|
554
|
-
|
|
621
|
+
transcript.debug(`#${entry.seq} (connection ${connectionId}) ${text}`);
|
|
555
622
|
events.publish({
|
|
556
623
|
type: "entry",
|
|
557
624
|
entry
|
|
@@ -608,11 +675,19 @@ var SpeechService = class {
|
|
|
608
675
|
}
|
|
609
676
|
};
|
|
610
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
|
+
}
|
|
611
686
|
async function startBridge(options) {
|
|
612
687
|
const { config, version } = options;
|
|
613
688
|
const log = options.log ?? new BridgeLog(config.log.level, [consoleSink()]);
|
|
614
689
|
const bridgeLogger = log.for("bridge");
|
|
615
|
-
log.always("bridge",
|
|
690
|
+
log.always("bridge", startupMessage(config, version));
|
|
616
691
|
const clock = options.clock ?? new SystemClock();
|
|
617
692
|
const ids = new UlidGenerator();
|
|
618
693
|
const repository = new RingBufferRepository(config.buffer.maxEntries);
|
|
@@ -622,7 +697,8 @@ async function startBridge(options) {
|
|
|
622
697
|
events,
|
|
623
698
|
clock,
|
|
624
699
|
ids,
|
|
625
|
-
logger: log.for("speech-capture")
|
|
700
|
+
logger: log.for("speech-capture"),
|
|
701
|
+
transcript: log.for("speech-capture", { channel: "speech" })
|
|
626
702
|
});
|
|
627
703
|
const speech = new SpeechService({
|
|
628
704
|
repository,
|
|
@@ -646,6 +722,15 @@ async function startBridge(options) {
|
|
|
646
722
|
};
|
|
647
723
|
}
|
|
648
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
|
+
}
|
|
649
734
|
function configFromEnv(env) {
|
|
650
735
|
const port = env.SR_SPEECH_TCP_PORT;
|
|
651
736
|
return mergeConfig(DEFAULT_CONFIG, {
|
|
@@ -653,7 +738,11 @@ function configFromEnv(env) {
|
|
|
653
738
|
port: port === void 0 ? void 0 : Number(port),
|
|
654
739
|
encoding: env.SR_SPEECH_TCP_ENCODING
|
|
655
740
|
},
|
|
656
|
-
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
|
+
}
|
|
657
746
|
});
|
|
658
747
|
}
|
|
659
748
|
let config;
|
|
@@ -663,7 +752,7 @@ try {
|
|
|
663
752
|
process.stderr.write(`screenreader-speech-capture: ${error.message}\n`);
|
|
664
753
|
process.exit(2);
|
|
665
754
|
}
|
|
666
|
-
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)]]);
|
|
667
756
|
try {
|
|
668
757
|
const bridge = await startBridge({
|
|
669
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",
|