from-node-stream
Version:
convert nodejs-stream into webstream
142 lines (141 loc) • 5.07 kB
JavaScript
//#region ts/fromReadable.ts
/**
* 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
*/
function fromReadable(i) {
if (i instanceof ReadableStream) return i;
const stream = i;
return new ReadableStream({
start: (c) => {
stream.on("data", (data) => c.enqueue(data));
stream.on("close", () => c.close());
stream.on("error", (err) => c.error(err));
},
cancel: (reason) => (stream.destroy?.(reason), void 0)
});
}
//#endregion
//#region ts/fromWritable.ts
/**
* 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
*/
function fromWritable(i) {
if (i instanceof WritableStream) return i;
const stream = i;
return new WritableStream({
start: (c) => (stream.on("error", (err) => c.error(err)), void 0),
abort: (reason) => (stream.destroy?.(reason), void 0),
write: (data, c) => (stream.write(data), void 0),
close: () => (stream.end(), void 0)
});
}
//#endregion
//#region ts/fromDuplex.ts
/**
* Converts a Node.js Duplex stream to a Web API TransformStream
* @template IN - The type of data being written (string or Uint8Array)
* @template OUT - The type of data being read (string or Uint8Array)
* @param duplex - The Node.js duplex stream to convert
* @returns A Web API TransformStream that wraps the Node.js duplex stream
*/
function fromDuplex(duplex) {
if (duplex instanceof TransformStream) return duplex;
return {
readable: fromReadable(duplex),
writable: fromWritable(duplex)
};
}
//#endregion
//#region ts/index.ts
function mergeStream(...streams) {
return new ReadableStream({ start(controller) {
let activeStreams = streams.length;
streams.forEach(async (stream) => {
try {
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
controller.enqueue(value);
}
reader.releaseLock();
} catch (error) {
controller.error(error);
return;
} finally {
activeStreams--;
if (activeStreams === 0) controller.close();
}
});
} });
}
/**
* 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
*/
function fromStdioDropErr(p) {
return {
writable: fromWritable(p.stdin),
readable: fromReadable(p.stdout)
};
}
/**
* 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
*/
function fromStdioMergeError(p) {
return {
writable: fromWritable(p.stdin),
readable: mergeStream(fromReadable(p.stdout), fromReadable(p.stderr))
};
}
/**
* 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
*/
function fromStdioAndForwardError(p, { stderr }) {
const stdin = fromWritable(p.stdin);
const stdout = fromReadable(p.stdout);
if (p.stderr) 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
*/
function fromStdio(p, { stderr } = {}) {
if (stderr === void 0) return fromStdioMergeError(p);
else if (stderr === null) return fromStdioDropErr(p);
else {
if (p.stderr) fromReadable(p.stderr).pipeTo(fromWritable(stderr));
return fromStdioDropErr(p);
}
}
//#endregion
export { fromDuplex, fromReadable, fromStdio, fromStdioAndForwardError, fromStdioDropErr, fromStdioMergeError, fromWritable };
//# sourceMappingURL=index.mjs.map