Skip to content

Commit

Permalink
Merge pull request #209 from AGrzes/master
Browse files Browse the repository at this point in the history
Fixing test failures on node 8
  • Loading branch information
tschaub authored Jun 11, 2017
2 parents e3186d6 + ea9d431 commit 86e9b1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_js:
- "5"
- "6"
- "7"
- "8"
cache:
directories:
- node_modules
Expand Down
45 changes: 42 additions & 3 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var FSError = require('./error');
var constants = require('constants');
var getPathParts = require('./filesystem').getPathParts;

/** Workaround for optimizations in node 8 */
const statValues = process.binding('fs').getStatValues ? process.binding('fs').getStatValues() : [];

/**
* Call the provided function and either return the result or call the callback
Expand Down Expand Up @@ -44,6 +46,40 @@ function maybeCallback(callback, thisArg, func) {
}
}

/**
* Call the provided function and either return the result or call the callback
* with it (depending on if a callback is provided).
* @param {function()} callback Optional callback.
* @param {Object} thisArg This argument for the following function.
* @param {function()} func Function to call.
* @return {*} Return (if callback is not provided).
*/
function maybeStatCallback(callback, thisArg, func) {
if (callback && (typeof callback === 'function' || typeof callback.oncomplete === 'function')) {
var err = null;
var val;
try {
val = func.call(thisArg);
} catch (e) {
err = e;
}
// Unpack callback from FSReqWrap
if (callback.oncomplete) {
callback = callback.oncomplete.bind(callback);
}
process.nextTick(function() {
if (val === undefined) {
callback(err);
} else {
fillStatsArray(val, statValues);
callback(err, val);
}
});
} else {
return func.call(thisArg);
}
}

function notImplemented() {
throw new Error('Method not implemented');
}
Expand Down Expand Up @@ -299,7 +335,7 @@ function fillStatsArray(stats, statValues) {
* @return {Stats|undefined} Stats or undefined (if sync).
*/
Binding.prototype.stat = function(filepath, callback) {
return maybeCallback(callback, this, function() {
return maybeStatCallback(callback, this, function() {
var item = this._system.getItem(filepath);
if (item instanceof SymbolicLink) {
item = this._system.getItem(
Expand All @@ -316,6 +352,7 @@ Binding.prototype.stat = function(filepath, callback) {
if (callback instanceof Float64Array) {
fillStatsArray(stats, callback);
} else {
fillStatsArray(stats, statValues);
return new Stats(stats);
}
});
Expand All @@ -330,7 +367,7 @@ Binding.prototype.stat = function(filepath, callback) {
* @return {Stats|undefined} Stats or undefined (if sync).
*/
Binding.prototype.fstat = function(fd, callback) {
return maybeCallback(callback, this, function() {
return maybeStatCallback(callback, this, function() {
var descriptor = this._getDescriptorById(fd);
var item = descriptor.getItem();
var stats = item.getStats();
Expand All @@ -341,6 +378,7 @@ Binding.prototype.fstat = function(fd, callback) {
if (callback instanceof Float64Array) {
fillStatsArray(stats, callback);
} else {
fillStatsArray(stats, statValues);
return new Stats(stats);
}
});
Expand Down Expand Up @@ -1041,7 +1079,7 @@ Binding.prototype.readlink = function(pathname, encoding, callback) {
* @return {Stats|undefined} Stats or undefined (if sync).
*/
Binding.prototype.lstat = function(filepath, callback) {
return maybeCallback(callback, this, function() {
return maybeStatCallback(callback, this, function() {
var item = this._system.getItem(filepath);
if (!item) {
throw new FSError('ENOENT', filepath);
Expand All @@ -1054,6 +1092,7 @@ Binding.prototype.lstat = function(filepath, callback) {
if (callback instanceof Float64Array) {
fillStatsArray(stats, callback);
} else {
fillStatsArray(stats, statValues);
return new Stats(item.getStats());
}
});
Expand Down

0 comments on commit 86e9b1b

Please sign in to comment.