Skip to content

Commit

Permalink
fix(main): keep compatiable
Browse files Browse the repository at this point in the history
keep compatiable
  • Loading branch information
snomiao committed Jan 19, 2025
1 parent 77570b4 commit 2bd1939
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 70 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/d3": "^7.4.3",
"bson": "^6.8.0",
"d3": "^7.9.0",
"from-node-stream": "0.0.4",
"phpdie": "^1.2.12",
"polyfill-text-decoder-stream": "^0.0.9",
"polyfill-text-encoder-stream": "^0.0.9",
Expand Down
82 changes: 12 additions & 70 deletions src/fromNodeStream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import DIE from "phpdie";
import {
fromReadable,
fromStdioDropErr,
fromStdioMergeError,
fromWritable,
} from "from-node-stream";
import { Readable, Writable } from "stream";
import { sf } from ".";
export { fromReadable, fromWritable };

/** Make TransformStream from stdio
* @deprecated import {fromStdio} from 'from-node-stream'
Expand All @@ -23,72 +28,9 @@ export function fromStdio(
fromReadable(p.stderr).pipeTo(fromWritable(stderr), {
preventClose: true,
});
return {
writable: fromWritable(p.stdin || DIE("Missing stdin")),
readable: sf(
fromReadable(p.stdout || DIE("Missing stdout")),
...(stderr === "mergeIntoStdout"
? [fromReadable(p.stderr || DIE("Missing stderr"))]
: [])
),
};
if (stderr === "mergeIntoStdout") {
return fromStdioMergeError(p);
} else {
return fromStdioDropErr(p);
}
}

/**
* @deprecated import from 'from-node-stream'
*/
export function fromReadable<T extends string | Uint8Array>(
i: Readable | NodeJS.ReadableStream
): ReadableStream<T> {
return new ReadableStream({
start: (c) => {
i.on("data", (data) => c.enqueue(data));
i.on("close", () => c.close());
i.on("error", (err) => c.error(err));
},
cancel: (reason) => (
(i as Partial<Readable> & Partial<NodeJS.ReadableStream>).destroy?.(
reason
),
undefined
),
});
}

/**
* @deprecated import from 'from-node-stream'
*/
export function fromWritable<T extends string | Uint8Array>(
i: Writable | NodeJS.WritableStream
): WritableStream<T> {
return new WritableStream({
start: (c) => (i.on("error", (err) => c.error(err)), undefined),
abort: (reason) => (
(i as Partial<Writable> & Partial<NodeJS.WritableStream>).destroy?.(
reason
),
undefined
),
write: (data: string | Uint8Array, c) => (i.write(data), undefined),
close: () => (i.end(), undefined),
});
}

// export function toReadable<T>(i: ReadableStream<T>): Readable {
// const i = new Readable()
// i.pipeTo(Readable)
// return new Readable({
// start: (c) => {
// },
// cancel: (reason) => (i.destroy(reason), undefined),
// });
// }

// export function toWritable(i: Writable) {
// return new WritableStream({
// start: (c) => (i.on("error", (err) => c.error(err)), undefined),
// abort: (reason) => (i.destroy(reason), undefined),
// write: (data, c) => (i.write(data), undefined),
// close: () => (i.end(), undefined),
// });
// }

0 comments on commit 2bd1939

Please sign in to comment.