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.
51 lines
1.2 KiB
JavaScript
51 lines
1.2 KiB
JavaScript
"use strict";
|
|
|
|
const pEvent = require("p-event");
|
|
const debug = require("debug")("promistream:from-node-stream:writable");
|
|
|
|
const objectID = require("../object-id");
|
|
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(`[#${objectID(stream)}] Ending stream`);
|
|
|
|
let finishPromise = pEvent(stream, "finish");
|
|
stream.end();
|
|
return finishPromise;
|
|
} else {
|
|
debug(`[#${objectID(stream)}] Not ending stream because it is stdio`);
|
|
}
|
|
},
|
|
destroy: function () {
|
|
return destroyStream(stream);
|
|
}
|
|
};
|
|
};
|