You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1 KiB
JavaScript

"use strict";
const debug = require("debug")("promistream:from-node-stream:writable");
const isStdioStream = require("../is-stdio-stream");
module.exports = function writeToStream(stream, value) {
if (!isStdioStream(stream)) {
let canWriteMore = stream.write(value);
if (canWriteMore) {
debug("Stream can accept more data");
return;
} else {
debug("Stream is backed up, waiting for drain event...");
// TODO: Use p-event instead?
return new Promise((resolve, _reject) => {
stream.once("drain", () => {
debug("Drain event received");
resolve();
});
});
}
} else {
// NOTE: According to the `stream-to-pull-stream` code, stdout/stderr behave differently from normal streams, and the `drain` event doesn't work correctly there. So instead, we use the flush callback to know when to write the next bit of data.
return new Promise((resolve, _reject) => {
stream.write(value, (_error) => {
// NOTE: We ignore any errors here, and wait for them to be thrown in the `error` event, to simplify the logic.
resolve();
});
});
}
};