Skip to content

Commit

Permalink
SFTPStream: fix node v6.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Jul 8, 2019
1 parent 6ecc420 commit 0de75c7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
83 changes: 81 additions & 2 deletions lib/node-fs-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ exports.ERR_INTERNAL_ASSERTION = class ERR_INTERNAL_ASSERTION extends Error {
}
};

var MAX_32BIT_INT = Math.pow(2, 32);
var MAX_32BIT_BIGINT = (function() {
try {
return new Function('return 2n ** 32n')();
} catch (ex) {}
})();
exports.ERR_OUT_OF_RANGE = class ERR_OUT_OF_RANGE extends RangeError {
constructor(str, range, input, replaceDefaultBoolean) {
super();
Expand All @@ -65,11 +71,11 @@ exports.ERR_OUT_OF_RANGE = class ERR_OUT_OF_RANGE extends RangeError {
? str
: `The value of "${str}" is out of range.`);
var received;
if (Number.isInteger(input) && Math.abs(input) > 2 ** 32) {
if (Number.isInteger(input) && Math.abs(input) > MAX_32BIT_INT) {
received = addNumericalSeparator(String(input));
} else if (typeof input === 'bigint') {
received = String(input);
if (input > 2n ** 32n || input < -(2n ** 32n))
if (input > MAX_32BIT_BIGINT || input < -MAX_32BIT_BIGINT)
received = addNumericalSeparator(received);
received += 'n';
} else {
Expand Down Expand Up @@ -116,3 +122,76 @@ exports.validateNumber = function validateNumber(value, name) {
if (typeof value !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
};


// =============================================================================
// Following code is only needed to support node v6.x ....

// Undocumented cb() API, needed for core, not for public API
exports.destroyImpl = function destroy(err, cb) {
const readableDestroyed = this._readableState &&
this._readableState.destroyed;
const writableDestroyed = this._writableState &&
this._writableState.destroyed;

if (readableDestroyed || writableDestroyed) {
if (cb) {
cb(err);
} else if (err) {
if (!this._writableState) {
process.nextTick(emitErrorNT, this, err);
} else if (!this._writableState.errorEmitted) {
this._writableState.errorEmitted = true;
process.nextTick(emitErrorNT, this, err);
}
}

return this;
}

// We set destroyed to true before firing error callbacks in order
// to make it re-entrance safe in case destroy() is called within callbacks

if (this._readableState) {
this._readableState.destroyed = true;
}

// If this is a duplex stream mark the writable part as destroyed as well
if (this._writableState) {
this._writableState.destroyed = true;
}

this._destroy(err || null, (err) => {
if (!cb && err) {
if (!this._writableState) {
process.nextTick(emitErrorAndCloseNT, this, err);
} else if (!this._writableState.errorEmitted) {
this._writableState.errorEmitted = true;
process.nextTick(emitErrorAndCloseNT, this, err);
} else {
process.nextTick(emitCloseNT, this);
}
} else if (cb) {
process.nextTick(emitCloseNT, this);
cb(err);
} else {
process.nextTick(emitCloseNT, this);
}
});

return this;
};

function emitErrorAndCloseNT(self, err) {
emitErrorNT(self, err);
emitCloseNT(self);
}

function emitCloseNT(self) {
if (self._writableState && !self._writableState.emitClose)
return;
if (self._readableState && !self._readableState.emitClose)
return;
self.emit('close');
}
// =============================================================================
15 changes: 15 additions & 0 deletions lib/sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,7 @@ SFTPStream.Stats = Stats;
// ReadStream/WriteStream-related
var {
validateNumber,
destroyImpl,
ERR_OUT_OF_RANGE,
ERR_INVALID_ARG_TYPE
} = require('./node-fs-compat');
Expand Down Expand Up @@ -2872,6 +2873,9 @@ ReadStream.prototype._read = function(n) {
pool.used = roundUpToMultipleOf8(pool.used + toRead);
};

if (typeof ReadableStream.prototype.destroy !== 'function')
ReadStream.prototype.destroy = destroyImpl;

ReadStream.prototype._destroy = function(err, cb) {
if (this._opening && !Buffer.isBuffer(this.handle)) {
this.once('open', closeStream.bind(null, this, cb, err));
Expand Down Expand Up @@ -2945,6 +2949,14 @@ function WriteStream(sftp, path, options) {
if (options.encoding)
this.setDefaultEncoding(options.encoding);

// Node v6.x only
this.on('finish', function() {
if (this._writableState.finalCalled)
return;
if (this.autoClose)
this.destroy();
});

if (!Buffer.isBuffer(this.handle))
this.open();
}
Expand Down Expand Up @@ -3077,6 +3089,9 @@ WriteStream.prototype._writev = function(data, cb) {
}
};

if (typeof WritableStream.prototype.destroy !== 'function')
WriteStream.prototype.destroy = ReadStream.prototype.destroy;

WriteStream.prototype._destroy = ReadStream.prototype._destroy;
WriteStream.prototype.close = function(cb) {
if (cb) {
Expand Down

0 comments on commit 0de75c7

Please sign in to comment.