Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide callback for writeStream #200

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions source/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const copy = require("./interface/copyFile.js");
const putFile = require("./interface/putFile.js");
const stats = require("./interface/stat.js");

const NOOP = () => {};

/**
* Client adapter
* @typedef {Object} ClientInterface
Expand Down Expand Up @@ -174,19 +176,21 @@ function createClient(remoteURL, opts = {}) {
* Create a writeable stream to a remote file
* @param {String} remoteFilename The file to write to
* @param {PutOptions=} options Options for the request
* @param {Function=} callback Optional callback to fire
* once the request has finished
* @memberof ClientInterface
* @returns {Writeable} A writeable stream
* @example
* const remote = client.createWriteStream("/data.zip");
* fs.createReadStream("~/myData.zip").pipe(remote);
*/
createWriteStream: function createWriteStream(remoteFilename, options) {
createWriteStream: function createWriteStream(remoteFilename, options, callback = NOOP) {
if (typeof WEB !== "undefined" && WEB === true) {
throw new Error("createWriteStream not implemented in web environment");
} else {
const createStream = require("./interface/createStream.js");
const createOptions = merge(runtimeOptions, options || {});
return createStream.createWriteStream(remoteFilename, createOptions);
return createStream.createWriteStream(remoteFilename, createOptions, callback);
}
},

Expand Down
9 changes: 8 additions & 1 deletion source/interface/createStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const responseHandlers = require("../response.js");
const { encodePath, joinURL, prepareRequestOptions, request } = require("../request.js");

const NOOP = () => {};

function createReadStream(filePath, options) {
const Stream = require("stream");
const PassThroughStream = Stream.PassThrough;
Expand All @@ -15,7 +17,7 @@ function createReadStream(filePath, options) {
return outStream;
}

function createWriteStream(filePath, options) {
function createWriteStream(filePath, options, callback = NOOP) {
const Stream = require("stream");
const PassThroughStream = Stream.PassThrough;
const writeStream = new PassThroughStream();
Expand All @@ -31,6 +33,11 @@ function createWriteStream(filePath, options) {
};
prepareRequestOptions(requestOptions, options);
request(requestOptions)
.then(response => {
// Fire callback asynchronously to avoid errors
setTimeout(callback, 0);
return response;
})
.then(responseHandlers.handleResponseCode)
.catch(err => {
writeStream.emit("error", err);
Expand Down