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.
36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const pEvent = require("p-event");
|
|
const debug = require("debug")("promistream:from-node-stream:writable");
|
|
|
|
const objectID = require("../object-id");
|
|
const isStdioStream = require("../is-stdio-stream");
|
|
|
|
module.exports = function writeToStream(stream, value) {
|
|
if (!isStdioStream(stream)) {
|
|
let canWriteMore = stream.write(value);
|
|
|
|
if (canWriteMore) {
|
|
debug(`[#${objectID(stream)}] Stream can accept more data`);
|
|
return;
|
|
} else {
|
|
debug(`[#${objectID(stream)}] Stream is backed up, waiting for drain event...`);
|
|
|
|
return Promise.try(() => {
|
|
return pEvent(stream, "drain");
|
|
}).then(() => {
|
|
debug(`[#${objectID(stream)}] Drain event received`);
|
|
});
|
|
}
|
|
} 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();
|
|
});
|
|
});
|
|
}
|
|
};
|