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.

46 lines
1.0 KiB
JavaScript

"use strict";
const debug = require("debug")("promistream:from-node-stream:writable");
const attachHandlers = require("./attach-handlers");
const writeToStream = require("./write-to-stream");
const isStdioStream = require("../is-stdio-stream");
const destroyStream = require("../destroy-stream");
const assertErrorType = require("../assert-error-type");
module.exports = function wireUpWritableInterface(stream, { onEnd, onError } = {}) {
attachHandlers({
stream: stream,
onClose: () => {
if (onEnd != null) {
onEnd();
}
},
onError: (error) => {
assertErrorType(error);
if (onError != null) {
onError(error);
}
}
});
return {
write: function (value) {
return writeToStream(stream, value);
},
end: function () {
// stdout/stderr cannot be ended like other streams
if (!isStdioStream(stream)) {
debug("Ending stream");
stream.end();
} else {
debug("Not ending stream because it is stdio");
}
},
destroy: function () {
return destroyStream(stream);
}
};
};