from-node-stream
Advanced tools
| import type { Readable } from "stream"; | ||
| /** | ||
| * Converts a Node.js Readable stream to a Web API ReadableStream | ||
| * @template T - The type of data being read (string or Uint8Array) | ||
| * @param i - The Node.js readable stream to convert | ||
| * @returns A Web API ReadableStream that wraps the Node.js stream | ||
| */ | ||
| export declare function fromReadable<T extends string | Uint8Array>(i: Readable | NodeJS.ReadableStream): ReadableStream<T>; |
| import type { Writable } from "stream"; | ||
| /** | ||
| * Converts a Node.js Writable stream to a Web API WritableStream | ||
| * @template T - The type of data being written (string or Uint8Array) | ||
| * @param i - The Node.js writable stream to convert | ||
| * @returns A Web API WritableStream that wraps the Node.js stream | ||
| */ | ||
| export declare function fromWritable<T extends string | Uint8Array>(i: Writable | NodeJS.WritableStream): WritableStream<T>; |
+45
-8
| import { Readable, Writable } from "stream"; | ||
| import { fromReadable } from "./fromReadable"; | ||
| import { fromWritable } from "./fromWritable"; | ||
| export declare function fromStdioDropErr( | ||
| /** | ||
| * Creates a TransformStream from a process's stdio, dropping stderr output | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @returns A TransformStream that connects stdin to stdout, ignoring stderr | ||
| */ | ||
| export declare function fromStdioDropErr<IN extends string | Uint8Array, OUT extends string | Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -10,5 +17,11 @@ p: { | ||
| stderr?: Readable | null; | ||
| }): TransformStream<string | Uint8Array, string | Uint8Array>; | ||
| /** Make TransformStream from stdio*/ | ||
| export declare function fromStdioMergeError( | ||
| }): TransformStream<IN, OUT>; | ||
| /** | ||
| * Creates a TransformStream from a process's stdio, merging stdout and stderr | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @returns A TransformStream that connects stdin to a merged stdout+stderr stream | ||
| */ | ||
| export declare function fromStdioMergeError<IN extends string | Uint8Array, OUT extends string | Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -19,7 +32,13 @@ p: { | ||
| stderr?: Readable | null; | ||
| }): TransformStream<string | Uint8Array, string | Uint8Array>; | ||
| }): TransformStream<IN, OUT>; | ||
| /** | ||
| * Make TransformStream from stdio | ||
| * Creates a TransformStream from a process's stdio, forwarding stderr to a specified stream | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @param options - Configuration object | ||
| * @param options.stderr - The writable stream to forward stderr to | ||
| * @returns A TransformStream that connects stdin to stdout, forwarding stderr separately | ||
| */ | ||
| export declare function fromStdio( | ||
| export declare function fromStdioAndForwardError<IN extends string | Uint8Array, OUT extends string | Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -30,6 +49,24 @@ p: { | ||
| stderr?: Readable | null; | ||
| }, { stderr }: { | ||
| stderr: Writable; | ||
| }): TransformStream<IN, OUT>; | ||
| /** | ||
| * Creates a TransformStream from a process's stdio with configurable stderr handling | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @param options - Configuration object for stderr handling | ||
| * @param options.stderr - Writable stream to forward stderr to, or null to drop stderr, or undefined to merge with stdout | ||
| * @returns A TransformStream that connects stdin to stdout with the specified stderr behavior | ||
| */ | ||
| export declare function fromStdio<IN extends string | Uint8Array, OUT extends string | Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
| p: { | ||
| stdin?: Writable | null; | ||
| stdout?: Readable | null; | ||
| stderr?: Readable | null; | ||
| }, { stderr, }?: { | ||
| /** specify stderr to forward, or set to null to drop. */ | ||
| stderr?: Writable | null; | ||
| }): TransformStream<string | Uint8Array, string | Uint8Array>; | ||
| }): TransformStream<IN, OUT>; | ||
| export { fromReadable, fromWritable }; |
+16
-5
@@ -42,12 +42,22 @@ // index.ts | ||
| } | ||
| function fromStdioAndForwardError(p, { stderr }) { | ||
| const stdin = fromWritable(p.stdin); | ||
| const stdout = fromReadable(p.stdout); | ||
| if (p.stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr)); | ||
| return { | ||
| writable: stdin, | ||
| readable: stdout | ||
| }; | ||
| } | ||
| function fromStdio(p, { | ||
| stderr | ||
| } = {}) { | ||
| if (p.stderr?.pipe && stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr), { | ||
| preventClose: true | ||
| }); | ||
| if (stderr === undefined) { | ||
| return fromStdioMergeError(p); | ||
| } else if (stderr === null) { | ||
| return fromStdioDropErr(p); | ||
| } else { | ||
| if (p.stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr)); | ||
| return fromStdioDropErr(p); | ||
@@ -60,2 +70,3 @@ } | ||
| fromStdioDropErr, | ||
| fromStdioAndForwardError, | ||
| fromStdio, | ||
@@ -65,2 +76,2 @@ fromReadable | ||
| //# debugId=14BF211E607D71C964756E2164756E21 | ||
| //# debugId=C247144402D037BB64756E2164756E21 |
@@ -5,9 +5,9 @@ { | ||
| "sourcesContent": [ | ||
| "import { mergeStream } from \"sflow\"; // TODO: tree shake sflow\nimport { Readable, Writable } from \"stream\";\nimport { fromReadable } from \"./fromReadable\";\nimport { fromWritable } from \"./fromWritable\";\n\nexport function fromStdioDropErr(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n }\n): TransformStream<string | Uint8Array, string | Uint8Array> {\n return {\n writable: fromWritable(p.stdin!),\n readable: fromReadable(p.stdout!),\n };\n}\n\n/** Make TransformStream from stdio*/\nexport function fromStdioMergeError(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n }\n): TransformStream<string | Uint8Array, string | Uint8Array> {\n const stdin = fromWritable(p.stdin!);\n const stdout = fromReadable(p.stdout!);\n const stderr = fromReadable(p.stderr!);\n return {\n writable: stdin,\n readable: mergeStream(stdout, stderr),\n };\n}\n/**\n * Make TransformStream from stdio\n */\nexport function fromStdio(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n },\n {\n stderr,\n }: {\n /** specify stderr to forward, or set to null to drop. */\n stderr?: Writable | null;\n } = {}\n): TransformStream<string | Uint8Array, string | Uint8Array> {\n if (p.stderr?.pipe && stderr?.pipe)\n fromReadable(p.stderr).pipeTo(fromWritable(stderr), {\n preventClose: true,\n });\n if (stderr === undefined) {\n return fromStdioMergeError(p);\n } else {\n return fromStdioDropErr(p);\n }\n}\nexport { fromReadable, fromWritable };\n", | ||
| "import type { Readable } from \"stream\";\n\nexport function fromReadable<T extends string | Uint8Array>(\n i: Readable | NodeJS.ReadableStream\n): ReadableStream<T> {\n return new ReadableStream({\n start: (c) => {\n i.on(\"data\", (data) => c.enqueue(data));\n i.on(\"close\", () => c.close());\n i.on(\"error\", (err) => c.error(err));\n },\n cancel: (reason) => (\n (i as Partial<Readable> & Partial<NodeJS.ReadableStream>).destroy?.(\n reason\n ),\n undefined\n ),\n });\n}\n", | ||
| "import type { Writable } from \"stream\";\n\nexport function fromWritable<T extends string | Uint8Array>(\n i: Writable | NodeJS.WritableStream\n): WritableStream<T> {\n return new WritableStream({\n start: (c) => (i.on(\"error\", (err) => c.error(err)), undefined),\n abort: (reason) => (\n (i as Partial<Writable> & Partial<NodeJS.WritableStream>).destroy?.(\n reason\n ),\n undefined\n ),\n write: (data: string | Uint8Array, c) => (i.write(data), undefined),\n close: () => (i.end(), undefined),\n });\n}\n" | ||
| "import { mergeStream } from \"sflow\"; // TODO: ensure tree shake sflow\nimport { Readable, Writable } from \"stream\";\nimport { fromReadable } from \"./fromReadable\";\nimport { fromWritable } from \"./fromWritable\";\n\n/**\n * Creates a TransformStream from a process's stdio, dropping stderr output\n * @template IN - Input data type (string or Uint8Array)\n * @template OUT - Output data type (string or Uint8Array)\n * @param p - A process object with stdin, stdout, and stderr streams\n * @returns A TransformStream that connects stdin to stdout, ignoring stderr\n */\nexport function fromStdioDropErr<IN extends string|Uint8Array, OUT extends string|Uint8Array>(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n }\n): TransformStream<IN, OUT> {\n return {\n writable: fromWritable<IN>(p.stdin!),\n readable: fromReadable<OUT>(p.stdout!),\n };\n}\n\n/**\n * Creates a TransformStream from a process's stdio, merging stdout and stderr\n * @template IN - Input data type (string or Uint8Array)\n * @template OUT - Output data type (string or Uint8Array)\n * @param p - A process object with stdin, stdout, and stderr streams\n * @returns A TransformStream that connects stdin to a merged stdout+stderr stream\n */\nexport function fromStdioMergeError<IN extends string|Uint8Array, OUT extends string|Uint8Array>(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n }\n): TransformStream<IN, OUT> {\n const stdin = fromWritable<IN>(p.stdin!);\n const stdout = fromReadable<OUT>(p.stdout!);\n const stderr = fromReadable<OUT>(p.stderr!);\n return {\n writable: stdin,\n readable: mergeStream(stdout, stderr),\n };\n}\n\n/**\n * Creates a TransformStream from a process's stdio, forwarding stderr to a specified stream\n * @template IN - Input data type (string or Uint8Array)\n * @template OUT - Output data type (string or Uint8Array)\n * @param p - A process object with stdin, stdout, and stderr streams\n * @param options - Configuration object\n * @param options.stderr - The writable stream to forward stderr to\n * @returns A TransformStream that connects stdin to stdout, forwarding stderr separately\n */\nexport function fromStdioAndForwardError<IN extends string|Uint8Array, OUT extends string|Uint8Array>(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n },\n { stderr }: {\n stderr: Writable\n }\n): TransformStream<IN, OUT> {\n const stdin = fromWritable<IN>(p.stdin!);\n const stdout = fromReadable<OUT>(p.stdout!);\n if (p.stderr?.pipe)\n fromReadable(p.stderr).pipeTo(fromWritable(stderr));\n return {\n writable: stdin, readable: stdout,\n };\n}\n\n/**\n * Creates a TransformStream from a process's stdio with configurable stderr handling\n * @template IN - Input data type (string or Uint8Array)\n * @template OUT - Output data type (string or Uint8Array)\n * @param p - A process object with stdin, stdout, and stderr streams\n * @param options - Configuration object for stderr handling\n * @param options.stderr - Writable stream to forward stderr to, or null to drop stderr, or undefined to merge with stdout\n * @returns A TransformStream that connects stdin to stdout with the specified stderr behavior\n */\nexport function fromStdio<IN extends string|Uint8Array, OUT extends string|Uint8Array>(\n /** a process, which has stdin, stdout, stderr */\n p: {\n stdin?: Writable | null;\n stdout?: Readable | null;\n stderr?: Readable | null;\n },\n {\n stderr,\n }: {\n /** specify stderr to forward, or set to null to drop. */\n stderr?: Writable | null;\n } = {}\n): TransformStream<IN, OUT> {\n if (stderr === undefined) {\n return fromStdioMergeError(p);\n\n } else if (stderr === null) {\n return fromStdioDropErr(p);\n } else {\n // forward stderr if stderr is specified\n if (p.stderr?.pipe)\n fromReadable(p.stderr).pipeTo(fromWritable(stderr));\n return fromStdioDropErr(p);\n }\n\n}\n\nexport { fromReadable, fromWritable };\n", | ||
| "import type { Readable } from \"stream\";\n\n/**\n * Converts a Node.js Readable stream to a Web API ReadableStream\n * @template T - The type of data being read (string or Uint8Array)\n * @param i - The Node.js readable stream to convert\n * @returns A Web API ReadableStream that wraps the Node.js stream\n */\nexport function fromReadable<T extends string | Uint8Array>(\n i: Readable | NodeJS.ReadableStream\n): ReadableStream<T> {\n return new ReadableStream({\n start: (c) => {\n i.on(\"data\", (data) => c.enqueue(data));\n i.on(\"close\", () => c.close());\n i.on(\"error\", (err) => c.error(err));\n },\n cancel: (reason) => (\n (i as Partial<Readable> & Partial<NodeJS.ReadableStream>).destroy?.(\n reason\n ),\n undefined\n ),\n });\n}\n", | ||
| "import type { Writable } from \"stream\";\n\n/**\n * Converts a Node.js Writable stream to a Web API WritableStream\n * @template T - The type of data being written (string or Uint8Array)\n * @param i - The Node.js writable stream to convert\n * @returns A Web API WritableStream that wraps the Node.js stream\n */\nexport function fromWritable<T extends string | Uint8Array>(\n i: Writable | NodeJS.WritableStream\n): WritableStream<T> {\n return new WritableStream({\n start: (c) => (i.on(\"error\", (err) => c.error(err)), undefined),\n abort: (reason) => (\n (i as Partial<Writable> & Partial<NodeJS.WritableStream>).destroy?.(\n reason\n ),\n undefined\n ),\n write: (data: string | Uint8Array, c) => (i.write(data), undefined),\n close: () => (i.end(), undefined),\n });\n}\n" | ||
| ], | ||
| "mappings": ";AAAA;;;ACEO,SAAS,YAA2C,CACzD,GACmB;AAAA,EACnB,OAAO,IAAI,eAAe;AAAA,IACxB,OAAO,CAAC,MAAM;AAAA,MACZ,EAAE,GAAG,QAAQ,CAAC,SAAS,EAAE,QAAQ,IAAI,CAAC;AAAA,MACtC,EAAE,GAAG,SAAS,MAAM,EAAE,MAAM,CAAC;AAAA,MAC7B,EAAE,GAAG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC;AAAA;AAAA,IAErC,QAAQ,CAAC,YACN,EAAyD,UACxD,MACF,GACA;AAAA,EAEJ,CAAC;AAAA;;;ACfI,SAAS,YAA2C,CACzD,GACmB;AAAA,EACnB,OAAO,IAAI,eAAe;AAAA,IACxB,OAAO,CAAC,OAAO,EAAE,GAAG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,GAAG;AAAA,IACrD,OAAO,CAAC,YACL,EAAyD,UACxD,MACF,GACA;AAAA,IAEF,OAAO,CAAC,MAA2B,OAAO,EAAE,MAAM,IAAI,GAAG;AAAA,IACzD,OAAO,OAAO,EAAE,IAAI,GAAG;AAAA,EACzB,CAAC;AAAA;;;AFVI,SAAS,gBAAgB,CAE9B,GAK2D;AAAA,EAC3D,OAAO;AAAA,IACL,UAAU,aAAa,EAAE,KAAM;AAAA,IAC/B,UAAU,aAAa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
| "names": [] | ||
| } |
+6
-0
| import type { Readable } from "stream"; | ||
| /** | ||
| * Converts a Node.js Readable stream to a Web API ReadableStream | ||
| * @template T - The type of data being read (string or Uint8Array) | ||
| * @param i - The Node.js readable stream to convert | ||
| * @returns A Web API ReadableStream that wraps the Node.js stream | ||
| */ | ||
| export function fromReadable<T extends string | Uint8Array>( | ||
@@ -4,0 +10,0 @@ i: Readable | NodeJS.ReadableStream |
+6
-0
| import type { Writable } from "stream"; | ||
| /** | ||
| * Converts a Node.js Writable stream to a Web API WritableStream | ||
| * @template T - The type of data being written (string or Uint8Array) | ||
| * @param i - The Node.js writable stream to convert | ||
| * @returns A Web API WritableStream that wraps the Node.js stream | ||
| */ | ||
| export function fromWritable<T extends string | Uint8Array>( | ||
@@ -4,0 +10,0 @@ i: Writable | NodeJS.WritableStream |
+71
-18
@@ -1,2 +0,2 @@ | ||
| import { mergeStream } from "sflow"; // TODO: tree shake sflow | ||
| import { mergeStream } from "sflow"; // TODO: ensure tree shake sflow | ||
| import { Readable, Writable } from "stream"; | ||
@@ -6,3 +6,10 @@ import { fromReadable } from "./fromReadable"; | ||
| export function fromStdioDropErr( | ||
| /** | ||
| * Creates a TransformStream from a process's stdio, dropping stderr output | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @returns A TransformStream that connects stdin to stdout, ignoring stderr | ||
| */ | ||
| export function fromStdioDropErr<IN extends string|Uint8Array, OUT extends string|Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -14,11 +21,17 @@ p: { | ||
| } | ||
| ): TransformStream<string | Uint8Array, string | Uint8Array> { | ||
| ): TransformStream<IN, OUT> { | ||
| return { | ||
| writable: fromWritable(p.stdin!), | ||
| readable: fromReadable(p.stdout!), | ||
| writable: fromWritable<IN>(p.stdin!), | ||
| readable: fromReadable<OUT>(p.stdout!), | ||
| }; | ||
| } | ||
| /** Make TransformStream from stdio*/ | ||
| export function fromStdioMergeError( | ||
| /** | ||
| * Creates a TransformStream from a process's stdio, merging stdout and stderr | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @returns A TransformStream that connects stdin to a merged stdout+stderr stream | ||
| */ | ||
| export function fromStdioMergeError<IN extends string|Uint8Array, OUT extends string|Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -30,6 +43,6 @@ p: { | ||
| } | ||
| ): TransformStream<string | Uint8Array, string | Uint8Array> { | ||
| const stdin = fromWritable(p.stdin!); | ||
| const stdout = fromReadable(p.stdout!); | ||
| const stderr = fromReadable(p.stderr!); | ||
| ): TransformStream<IN, OUT> { | ||
| const stdin = fromWritable<IN>(p.stdin!); | ||
| const stdout = fromReadable<OUT>(p.stdout!); | ||
| const stderr = fromReadable<OUT>(p.stderr!); | ||
| return { | ||
@@ -40,6 +53,13 @@ writable: stdin, | ||
| } | ||
| /** | ||
| * Make TransformStream from stdio | ||
| * Creates a TransformStream from a process's stdio, forwarding stderr to a specified stream | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @param options - Configuration object | ||
| * @param options.stderr - The writable stream to forward stderr to | ||
| * @returns A TransformStream that connects stdin to stdout, forwarding stderr separately | ||
| */ | ||
| export function fromStdio( | ||
| export function fromStdioAndForwardError<IN extends string|Uint8Array, OUT extends string|Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
@@ -51,2 +71,31 @@ p: { | ||
| }, | ||
| { stderr }: { | ||
| stderr: Writable | ||
| } | ||
| ): TransformStream<IN, OUT> { | ||
| const stdin = fromWritable<IN>(p.stdin!); | ||
| const stdout = fromReadable<OUT>(p.stdout!); | ||
| if (p.stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr)); | ||
| return { | ||
| writable: stdin, readable: stdout, | ||
| }; | ||
| } | ||
| /** | ||
| * Creates a TransformStream from a process's stdio with configurable stderr handling | ||
| * @template IN - Input data type (string or Uint8Array) | ||
| * @template OUT - Output data type (string or Uint8Array) | ||
| * @param p - A process object with stdin, stdout, and stderr streams | ||
| * @param options - Configuration object for stderr handling | ||
| * @param options.stderr - Writable stream to forward stderr to, or null to drop stderr, or undefined to merge with stdout | ||
| * @returns A TransformStream that connects stdin to stdout with the specified stderr behavior | ||
| */ | ||
| export function fromStdio<IN extends string|Uint8Array, OUT extends string|Uint8Array>( | ||
| /** a process, which has stdin, stdout, stderr */ | ||
| p: { | ||
| stdin?: Writable | null; | ||
| stdout?: Readable | null; | ||
| stderr?: Readable | null; | ||
| }, | ||
| { | ||
@@ -58,13 +107,17 @@ stderr, | ||
| } = {} | ||
| ): TransformStream<string | Uint8Array, string | Uint8Array> { | ||
| if (p.stderr?.pipe && stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr), { | ||
| preventClose: true, | ||
| }); | ||
| ): TransformStream<IN, OUT> { | ||
| if (stderr === undefined) { | ||
| return fromStdioMergeError(p); | ||
| } else if (stderr === null) { | ||
| return fromStdioDropErr(p); | ||
| } else { | ||
| // forward stderr if stderr is specified | ||
| if (p.stderr?.pipe) | ||
| fromReadable(p.stderr).pipeTo(fromWritable(stderr)); | ||
| return fromStdioDropErr(p); | ||
| } | ||
| } | ||
| export { fromReadable, fromWritable }; |
+1
-1
| { | ||
| "name": "from-node-stream", | ||
| "version": "0.0.11", | ||
| "version": "0.1.0", | ||
| "description": "convert nodejs-stream into webstream", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
+16
-16
@@ -5,2 +5,18 @@ # from-node-stream | ||
| ### Stdio Passthrough Example | ||
| Create a script that pipes process stdin through a bash process and then to stdout: | ||
| ```ts | ||
| import { exec } from "child_process"; | ||
| import { fromStdio } from "from-node-stream"; | ||
| import { fromReadable } from "from-node-stream/fromReadable"; | ||
| import { fromWritable } from "from-node-stream/fromWritable"; | ||
| // Execute everything from stdin in bash and then output to stdout | ||
| await fromReadable(process.stdin) | ||
| .pipeThrough(fromStdio(exec("bash"))) | ||
| .pipeTo(fromWritable(process.stdout)); | ||
| ``` | ||
| ### Basic: Read and Write from Node Streams | ||
@@ -93,18 +109,2 @@ | ||
| ### Stdio Passthrough Example | ||
| Create a script that pipes process stdin through a bash process and then to stdout: | ||
| ```ts | ||
| import { exec } from "child_process"; | ||
| import { fromStdio } from "from-node-stream"; | ||
| import { fromReadable } from "from-node-stream/fromReadable"; | ||
| import { fromWritable } from "from-node-stream/fromWritable"; | ||
| // Execute everything from stdin in bash and then output to stdout | ||
| await fromReadable(process.stdin) | ||
| .pipeThrough(fromStdio(exec("bash"))) | ||
| .pipeTo(fromWritable(process.stdout)); | ||
| ``` | ||
| ## Development | ||
@@ -111,0 +111,0 @@ To install dependencies: |
26097
53.77%346
53.1%