diff --git a/src/commands/auth/users/ids.ts b/src/commands/auth/users/ids.ts index f5cfd86..1848e6a 100644 --- a/src/commands/auth/users/ids.ts +++ b/src/commands/auth/users/ids.ts @@ -47,10 +47,5 @@ export const parse_user_ids = ( return ids } -export const read_stdin = async (): Promise => { - const chunks: Buffer[] = [] - for await (const chunk of process.stdin) { - chunks.push(Buffer.from(chunk)) - } - return Buffer.concat(chunks).toString('utf8') -} +// Lives in lib/stdin.ts, shared with `deploy secrets set -f -`. +export { read_stdin } from '../../../lib/stdin' diff --git a/src/commands/deploy/secrets/set.ts b/src/commands/deploy/secrets/set.ts index 166ad88..5b1a042 100644 --- a/src/commands/deploy/secrets/set.ts +++ b/src/commands/deploy/secrets/set.ts @@ -3,6 +3,7 @@ import { resolve } from 'node:path' import { CommandModule } from 'yargs' import { requireApi } from '../../../api/context' import { json_option, print_json } from '../../../lib/listing' +import { read_stdin } from '../../../lib/stdin' import { log } from '../../../log' import { resolve_app_id } from '../resolve_app_id' import { managed_warning } from './managed_names' @@ -30,8 +31,10 @@ const DEFAULT_ENV_FILE = '.env' // runs; the short alias is invisible to it. const STDIN = '-' -const read_env_file = (raw_path: string): SecretPair[] => { - if (raw_path === STDIN) return parse_env(readFileSync(0, 'utf8'), 'stdin') +const read_env_file = async (raw_path: string): Promise => { + // Asynchronously: `readFileSync(0)` throws EAGAIN on a pipe that has not + // been written yet (lib/stdin.ts). + if (raw_path === STDIN) return parse_env(await read_stdin(), 'stdin') const path = raw_path || DEFAULT_ENV_FILE let content: string try { @@ -113,7 +116,8 @@ export const secrets_set: CommandModule = { // Validate EVERYTHING before writing ANY, so one malformed line or pair // aborts the whole command with no partial writes. - const from_file = env_file === undefined ? [] : read_env_file(env_file) + const from_file = + env_file === undefined ? [] : await read_env_file(env_file) const parsed = combine(from_file, parse_pairs(pairs)) if (env_file !== undefined) { diff --git a/src/lib/stdin.test.ts b/src/lib/stdin.test.ts new file mode 100644 index 0000000..f260084 --- /dev/null +++ b/src/lib/stdin.test.ts @@ -0,0 +1,38 @@ +import test from 'ava' +import { spawn } from 'node:child_process' +import path from 'node:path' +import { fileURLToPath } from 'node:url' + +// `faable auth webhooks create --json | … | faable deploy secrets set -f -` +// failed with EAGAIN: the CLI touches `process.stdin` early (the TTY check), +// which makes fd 0 non-blocking, and `readFileSync(0)` on a pipe that has not +// been written yet throws instead of waiting. The writer here is late on +// purpose, and the reader touches stdin first, like the CLI does. +const here = path.dirname(fileURLToPath(import.meta.url)) + +const readWithLateWriter = (script: string) => + new Promise((resolve, reject) => { + const child = spawn(process.execPath, ['--import', 'tsx', '-e', script], { + cwd: path.resolve(here, '..', '..'), + stdio: ['pipe', 'pipe', 'inherit'] + }) + let out = '' + child.stdout.on('data', d => (out += d)) + child.on('error', reject) + child.on('close', () => resolve(out.trim())) + setTimeout(() => child.stdin.end('A=1\n'), 500) + }) + +test('read_stdin waits for a late writer on a non-blocking pipe', async t => { + const out = await readWithLateWriter( + "process.stdin.isTTY; import('./src/lib/stdin.ts').then(m => m.read_stdin()).then(s => console.log(JSON.stringify(s)))" + ) + t.is(out, JSON.stringify('A=1\n')) +}) + +test('the synchronous read it replaces fails in the same setup (the bug)', async t => { + const out = await readWithLateWriter( + "process.stdin.isTTY; try { console.log(require('fs').readFileSync(0, 'utf8')) } catch (e) { console.log(e.code) }" + ) + t.is(out, 'EAGAIN') +}) diff --git a/src/lib/stdin.ts b/src/lib/stdin.ts new file mode 100644 index 0000000..5f868ab --- /dev/null +++ b/src/lib/stdin.ts @@ -0,0 +1,13 @@ +// All of stdin, read asynchronously. +// +// Never `readFileSync(0)`: when stdin is a pipe from another process that has +// not written yet (`faable auth webhooks create --json | … | faable deploy +// secrets set -f -`), the descriptor is non-blocking and the synchronous read +// throws EAGAIN instead of waiting. +export const read_stdin = async (): Promise => { + const chunks: Buffer[] = [] + for await (const chunk of process.stdin) { + chunks.push(Buffer.from(chunk)) + } + return Buffer.concat(chunks).toString('utf8') +}