Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/bombshell-dev/playground.
Something went wrong. Try again.
895 B · 37 lines
TypeScript
at focus-stack
1234567891011121314151617181920212223242526272829303132333435363738import { createChannel, each, type Operation, race, resource, spawn, type Stream, until,} from "effection";import { stdin } from "node:process";
// Bridge Node's process.stdin (raw bytes) into an Effection Stream.export function useStdin(): Operation<Stream<Uint8Array, void>> { return resource(function* (provide) { const channel = createChannel<Uint8Array, void>();
const iterator = stdin[Symbol.asyncIterator]();
yield* spawn(function* () { let next = yield* until(iterator.next()); while (!next.done) { yield* channel.send(next.value); next = yield* until(iterator.next()); } yield* channel.close(); });
yield* race([provide(channel), drain(channel)]); });}
function* drain<T, TClose>(stream: Stream<T, TClose>): Operation<void> { for (const _ of yield* each(stream)) { yield* each.next(); }}