Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/commands/auth/users/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,5 @@ export const parse_user_ids = (
return ids
}

export const read_stdin = async (): Promise<string> => {
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'
10 changes: 7 additions & 3 deletions src/commands/deploy/secrets/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<SecretPair[]> => {
// 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 {
Expand Down Expand Up @@ -113,7 +116,8 @@ export const secrets_set: CommandModule<unknown, SecretsSetArgs> = {

// 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) {
Expand Down
38 changes: 38 additions & 0 deletions src/lib/stdin.test.ts
Original file line number Diff line number Diff line change
@@ -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<string>((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')
})
13 changes: 13 additions & 0 deletions src/lib/stdin.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
const chunks: Buffer[] = []
for await (const chunk of process.stdin) {
chunks.push(Buffer.from(chunk))
}
return Buffer.concat(chunks).toString('utf8')
}
Loading