Skip to content

Commit

Permalink
nodejs: Add support for deferring the upload length (#140)
Browse files Browse the repository at this point in the history
* npm audit

* make defer length supported for nodejs

* detect file size from stream "end"

* remove 'copyBuf'
  • Loading branch information
ifedapoolarewaju authored and Acconut committed Mar 30, 2019
1 parent 686cb6c commit e6bdb48
Show file tree
Hide file tree
Showing 7 changed files with 8,503 additions and 16,233 deletions.
2 changes: 1 addition & 1 deletion lib/browser/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class StreamSource {
const hasEnoughData = end <= this._bufferOffset + len(this._buffer);
if (this._done || hasEnoughData) {
var value = this._getDataFromBuffer(start, end);
callback(null, value);
callback(null, value, value == null ? this._done : false);
return;
}
this._reader.read().then(({ value, done }) => {
Expand Down
16 changes: 10 additions & 6 deletions lib/node/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class StreamSource {
this.size = null;

stream.pause();
this._done = false;
stream.on("end", () => this._done = true);

this._buf = new Buffer(chunkSize);
this._bufPos = null;
Expand All @@ -84,24 +86,26 @@ class StreamSource {
return;
}

this._bufPos = start;
this._bufLen = 0;
if (this._done) {
callback(null, null, this._done);
return;
}

let bytesToSkip = start - this._bufPos;
let bytesToSkip = start - (this._bufPos + this._bufLen);
this._bufLen = 0;
this._bufPos = start;
let bytesToRead = end - start;
let slicingStream = new SlicingStream(bytesToSkip, bytesToRead, this);
this._stream.pipe(slicingStream);
slicingStream.size = bytesToRead;

callback(null, slicingStream);
}

close() {
//this._stream.
// not implemented
}
}


class SlicingStream extends Transform {
constructor(bytesToSkip, bytesToRead, source) {
super();
Expand Down
43 changes: 18 additions & 25 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,33 +516,26 @@ class Upload {
end = this._size;
}

// A source's slice may return a value to send or a promise.
// When we have the value we start to send it and emit progress.
// TODO: merge these two branches
if (this.options.uploadLengthDeferred) {
this._source.slice(start, end, (error, value) => {
if (error) {
this._emitError(error);
} else if (value === null) {
this._size = this._offset;
xhr.setRequestHeader("Upload-Length", this._offset);
xhr.send();
} else {
xhr.send(value);
this._emitProgress(this._offset, this._size);
}
});
} else {
this._source.slice(start, end, (error, chunk) => {
if (error) {
this._emitError(new DetailedError(`tus: could not slice file or stream (from ${start} to ${end})`, error));
return;
this._source.slice(start, end, (err, value, complete) => {
if (err) {
this._emitError(err);
return;
}

if (this.options.uploadLengthDeferred) {
if (complete) {
this._size = this._offset + (value && value.size ? value.size : 0);
xhr.setRequestHeader("Upload-Length", this._size);
}
}

xhr.send(chunk);
});
this._emitProgress(this._offset, this._size);
}
if (value === null) {
xhr.send();
} else {
xhr.send(value);
this._emitProgress(this._offset, this._size);
}
});
}
}

Expand Down
Loading

0 comments on commit e6bdb48

Please sign in to comment.