fixup! feat(speech): the speech capability over Cap'n Web, behind a gate
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
This commit is contained in:
1 parent
372f6a2b96
commit
a97ba6c174
12 files changed
+245
-92
No files matched your search
+24
-9
@@ -1,10 +1,17 @@
|
||||
/**
|
||||
* @file Helpers for the servers: run a script, and time and log a job.
|
||||
* @file Helpers for the servers: run one command, and time and log a job.
|
||||
* They reach only what they're given: spawn, and a clock.
|
||||
*/
|
||||
import { spawn } from 'node:child_process';
|
||||
|
||||
/** Run cmd with input on stdin; resolve to its stdout bytes, or reject with its stderr. */
|
||||
export const runWith = (cmd, args, input) => new Promise((resolve, reject) => {
|
||||
/**
|
||||
* A command bound to one program and its arguments: run(input) feeds it stdin and
|
||||
* resolves to its stdout bytes, or rejects with its stderr.
|
||||
*
|
||||
* @param {typeof import('node:child_process').spawn} spawn
|
||||
* @param {string} cmd
|
||||
* @param {string[]} args
|
||||
*/
|
||||
export const makeCommand = (spawn, cmd, args) => (input) => new Promise((resolve, reject) => {
|
||||
const child = spawn(cmd, args, { stdio: ['pipe', 'pipe', 'pipe'] });
|
||||
const out = [], err = [];
|
||||
child.stdout.on('data', (d) => out.push(d));
|
||||
@@ -16,15 +23,23 @@ export const runWith = (cmd, args, input) => new Promise((resolve, reject) => {
|
||||
child.stdin.end(input);
|
||||
});
|
||||
|
||||
/** Time a job and log how it went; failures are logged and rethrown. */
|
||||
export const timed = async (what, job, describe) => {
|
||||
const t0 = Date.now();
|
||||
/**
|
||||
* Time a job and log how it went; failures are logged and rethrown.
|
||||
*
|
||||
* @template T
|
||||
* @param {() => number} now milliseconds
|
||||
* @param {string} what
|
||||
* @param {() => Promise<T>} job
|
||||
* @param {(result: T) => string} describe
|
||||
*/
|
||||
export const timed = async (now, what, job, describe) => {
|
||||
const t0 = now();
|
||||
try {
|
||||
const result = await job();
|
||||
console.log(`${what} in ${Date.now() - t0} ms: ${describe(result)}`);
|
||||
console.log(`${what} in ${now() - t0} ms: ${describe(result)}`);
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error(`${what} failed after ${Date.now() - t0} ms: ${err.message}`);
|
||||
console.error(`${what} failed after ${now() - t0} ms: ${err.message}`);
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user