-
Notifications
You must be signed in to change notification settings - Fork 3
✨ feat(node): add Stdio context API for host process stdio #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taras
wants to merge
7
commits into
main
Choose a base branch
from
feat/node-stdio-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a70638
✨ feat(node): add Stdio context API for host process stdio
taras 4bd78e0
🧪 test(node): verify default Stdio.stdin reads from process.stdin
taras f70f329
🧪 test(node): tighten Stdio tests to assert end-to-end behavior
taras 82b657c
✨ feat(node): return Stream directly from Stdio.stdin()
taras f60e1ed
📝 docs(node): document the Stdio context API in the README
taras a652dda
📝 docs(node): fix Stdio JSDoc example comment to match pass-through b…
taras 8fb5b68
🧪 test(node): pin multi-consumer contract for Stdio.stdin()
taras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from "./stream.ts"; | ||
| export * from "./events.ts"; | ||
| export * from "./stdio.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { describe, it } from "@effectionx/vitest"; | ||
| import { createSignal, each, scoped, type Stream } from "effection"; | ||
| import { expect } from "expect"; | ||
|
|
||
| import { Stdio, stderr, stdin, stdout } from "./stdio.ts"; | ||
|
|
||
| describe("Stdio middleware", () => { | ||
| it("captures stdout bytes via middleware", function* () { | ||
| const captured: Uint8Array[] = []; | ||
|
|
||
| yield* Stdio.around({ | ||
| *stdout(args, next) { | ||
| captured.push(args[0]); | ||
| return yield* next(...args); | ||
| }, | ||
| }); | ||
|
|
||
| const bytes = new TextEncoder().encode("hello\n"); | ||
| yield* stdout(bytes); | ||
|
|
||
| expect(captured.length).toBe(1); | ||
| expect(new TextDecoder().decode(captured[0])).toBe("hello\n"); | ||
| }); | ||
|
|
||
| it("captures stderr bytes via middleware", function* () { | ||
| const captured: Uint8Array[] = []; | ||
|
|
||
| yield* Stdio.around({ | ||
| *stderr(args, next) { | ||
| captured.push(args[0]); | ||
| return yield* next(...args); | ||
| }, | ||
| }); | ||
|
|
||
| const bytes = new TextEncoder().encode("oops\n"); | ||
| yield* stderr(bytes); | ||
|
|
||
| expect(captured.length).toBe(1); | ||
| expect(new TextDecoder().decode(captured[0])).toBe("oops\n"); | ||
| }); | ||
|
|
||
| it("can substitute stdin with a synthetic stream", function* () { | ||
| const signal = createSignal<Uint8Array, void>(); | ||
| const synthetic: Stream<Uint8Array, void> = signal; | ||
|
|
||
| yield* Stdio.around({ | ||
| *stdin(_args, _next) { | ||
| return synthetic; | ||
| }, | ||
| }); | ||
|
|
||
| const stream = yield* stdin(); | ||
| const subscription = yield* stream; | ||
|
|
||
| const encoder = new TextEncoder(); | ||
| signal.send(encoder.encode("one")); | ||
| signal.send(encoder.encode("two")); | ||
| signal.close(); | ||
|
|
||
| const chunks: string[] = []; | ||
| const decoder = new TextDecoder(); | ||
| let result = yield* subscription.next(); | ||
| while (!result.done) { | ||
| chunks.push(decoder.decode(result.value)); | ||
| result = yield* subscription.next(); | ||
| } | ||
|
|
||
| expect(chunks).toEqual(["one", "two"]); | ||
| }); | ||
|
|
||
| it("middleware is scoped and does not leak", function* () { | ||
| const outerCalls: string[] = []; | ||
|
|
||
| yield* Stdio.around({ | ||
| *stdout(args, next) { | ||
| outerCalls.push("outer"); | ||
| return yield* next(...args); | ||
| }, | ||
| }); | ||
|
|
||
| const bytes = new TextEncoder().encode("hi\n"); | ||
| yield* stdout(bytes); | ||
| expect(outerCalls).toEqual(["outer"]); | ||
|
|
||
| yield* scoped(function* () { | ||
| const innerCalls: string[] = []; | ||
|
|
||
| yield* Stdio.around({ | ||
| *stdout(args, next) { | ||
| innerCalls.push("inner"); | ||
| return yield* next(...args); | ||
| }, | ||
| }); | ||
|
|
||
| yield* stdout(bytes); | ||
| expect(outerCalls).toEqual(["outer", "outer"]); | ||
| expect(innerCalls).toEqual(["inner"]); | ||
| }); | ||
|
|
||
| outerCalls.length = 0; | ||
| yield* stdout(bytes); | ||
| expect(outerCalls).toEqual(["outer"]); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Stdio defaults", () => { | ||
| it("reads from process.stdin by default (subscription acquires without error)", function* () { | ||
| yield* scoped(function* () { | ||
| // Default handler wraps process.stdin via fromReadable. We just | ||
| // verify that acquiring a subscription and then letting the scope | ||
| // tear it down does not throw; we can't easily assert on real | ||
| // host stdin bytes in a unit test. | ||
| const stream = yield* stdin(); | ||
| const _sub = yield* stream; | ||
| expect(true).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import process from "node:process"; | ||
| import { type Api, createApi } from "@effectionx/context-api"; | ||
| import type { Operation, Stream } from "effection"; | ||
| import { fromReadable } from "./stream.ts"; | ||
|
|
||
| /** | ||
| * Middleware-capable shape for host-process stdio. | ||
| * | ||
| * `stdin` yields a readable byte stream sourced from the host's standard | ||
| * input. `stdout` and `stderr` take bytes and write them to the host's | ||
| * corresponding output streams. | ||
| */ | ||
| export interface StdioApi { | ||
| stdin(): Operation<Stream<Uint8Array, void>>; | ||
| stdout(bytes: Uint8Array): Operation<void>; | ||
| stderr(bytes: Uint8Array): Operation<void>; | ||
| } | ||
|
|
||
| /** | ||
| * Context API used to observe or customize the host process's stdio. | ||
| * | ||
| * By default, `stdin` reads from `process.stdin`, and `stdout` / `stderr` | ||
| * write to `process.stdout` / `process.stderr`. Middleware can wrap this API | ||
| * via `Stdio.around(...)` to capture, transform, or redirect bytes — useful | ||
| * for tests that assert what was written to stdout, or harnesses that feed | ||
| * synthesized stdin. | ||
| * | ||
| * This is distinct from `@effectionx/process`'s `Stdio`, which governs child | ||
| * process stdio. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { main } from "effection"; | ||
| * import { Stdio, stdout } from "@effectionx/node"; | ||
| * | ||
| * await main(function* () { | ||
| * const captured: Uint8Array[] = []; | ||
| * | ||
| * yield* Stdio.around({ | ||
| * *stdout(args, next) { | ||
| * captured.push(args[0]); | ||
| * return yield* next(...args); | ||
| * }, | ||
| * }); | ||
| * | ||
| * yield* stdout(new TextEncoder().encode("hello\n")); | ||
| * // bytes flow into `captured` instead of the terminal | ||
| * }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| * ``` | ||
| */ | ||
| export const Stdio: Api<StdioApi> = createApi<StdioApi>( | ||
| "@effectionx/node/stdio", | ||
| { | ||
| *stdin() { | ||
| return fromReadable(process.stdin); | ||
| }, | ||
| *stdout(bytes) { | ||
| process.stdout.write(bytes); | ||
| }, | ||
| *stderr(bytes) { | ||
| process.stderr.write(bytes); | ||
| }, | ||
|
taras marked this conversation as resolved.
|
||
| }, | ||
| ); | ||
|
|
||
| export const { stdin, stdout, stderr } = Stdio.operations; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,9 @@ | |
| "references": [ | ||
| { | ||
| "path": "../vitest" | ||
| }, | ||
| { | ||
| "path": "../context-api" | ||
| } | ||
| ] | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.