Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

enfspatch

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enfspatch - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

8

CHANGELOG.md

@@ -0,1 +1,9 @@

1.0.0 / 2017-02-13
==================
* dependencies update
* rename async improvement
* rename tests
* general code improvements
* major version
0.1.0 / 2016-04-11

@@ -2,0 +10,0 @@ ==================

0

index.js

@@ -0,0 +0,0 @@ /**

321

lib/enfsPatch.js

@@ -9,3 +9,3 @@ /**

* @createdAt Created at 18-02-2016.
* @version 0.1.0
* @version 1.0.0
*/

@@ -19,8 +19,10 @@

const enfsaddins = require("enfsaddins");
const platform = process.env.ENFS_FS_PLATFORM || process.platform;
let Queue = [], cwd = null;
function noop(){}
process.cwd = (function(cwdFn) {
return function() {
process.cwd = ((function (cwdFn) {
return function () {
if (!cwd) {

@@ -30,4 +32,4 @@ cwd = cwdFn.call(process);

return cwd;
}
})(process.cwd);
};
})(process.cwd));

@@ -37,99 +39,157 @@ try {

} catch (er) {
//Intentionally left blank
}
process.chdir = (function(chdir) {
return function(dir) {
process.chdir = ((function (chdir) {
return function (dir) {
cwd = null;
chdir.call(process, dir);
};
})(process.chdir));
function addQueue(elem) {
Queue.push(elem);
}
function processQueue() {
let elem;
elem = Queue.shift();
if (elem) {
elem[0].apply(null, elem[1]);
}
})(process.chdir);
}
// Always patch fs.close/closeSync, because we want to
// retry() whenever a close happens *anywhere* in the program.
enFs.close = (function(close) {
return function(fd, callback) {
return close.call(enFs, fd, function(err) {
if (!err) {
processQueue();
}
if (typeof callback === "function") {
callback.apply(this, arguments);
}
enFs.close = ((function (close) {
return function (fd, callback) {
callback = callback || noop;
return close.call(enFs, fd, function () {
//retry even if close fails
processQueue();
callback.apply(this, arguments);
});
}
})(enFs.close);
};
})(enFs.close));
enFs.closeSync = (function(closeSync) {
return function(fd) {
enFs.closeSync = ((function (closeSync) {
return function () {
try {
return closeSync.apply(enFs, arguments);
} finally {
//retry even if close fails
processQueue();
}
}
})(enFs.closeSync);
};
})(enFs.closeSync));
module.exports = patch(require("./fs"));
function patch(fs) {
// Everything that references the open() function needs to be in here
polyfills(fs);
enfsaddins(fs);
fs.mockEnfs = patch;
//This time is used in rename async and can be changed if needed (default: 1000ms)
fs.renamingTime = 1000;
// on Windows, A/V software can lock the directory, causing this
// to fail with an EACCES or EPERM if the directory contains newly
// created files. Try again on failure, for up to 1 second.
if (process.platform === "win32") {
fs.rename = (function(rename) {
return function(from, to, callback) {
let stop = Date.now()+1000;
if (platform === "win32") {
/*fs.rename = ((function(rename) {
return function(from, to, callback) {
let stop = Date.now()+1000;
function fsR(from, to, callback) {
return rename(from, to, function(err) {
if (err && (err.code === "EACCESS" || err.code === "EPERM") && (Date.now() < stop)) {
addQueue([fsR, [from, to, callback]]);
} else {
if (typeof callback === "function") {
callback(this, arguments);
}
processQueue();
function fsR(from, to, callback) {
return rename(from, to, function(err) {
if (err && (err.code === "EACCES" || err.code === "EPERM") && (Date.now() < stop)) {
addQueue([fsR, [from, to, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
processQueue();
}
});
}
return fsR(from, to, callback);
};
})(fs.rename));*/
fs.rename = ((function (rename) {
return function (from, to, callback) {
callback = callback || noop;
let stopTime = Date.now() + fs.renamingTime;
let callAgainIn = 0;
function cb(err) {
if (err && (err.code === "EACCES" || err.code === "EPERM") && (Date.now() < stopTime)) {
if (callAgainIn < 100) {
callAgainIn += 10;
}
});
setTimeout(function () {
fs.stat(to, function (statErr) {
if (statErr && statErr.code === "ENOENT") {
rename(from, to, cb);
} else {
callback(err);
}
});
}, callAgainIn);
} else {
callback(err);
}
}
return fsR(from, to, callback);
}
})(fs.rename);
rename(from, to, cb);
};
})(fs.rename));
fs.renameSync = (function(rename) {
return function(from, to) {
let stop = Date.now() + 1000;
while (true) {
fs.renameSync = ((function (rename) {
return function (from, to) {
let stopTime = Date.now() + 1000;
function running() {
try {
return rename(from, to);
} catch (err) {
if ((err.code === "EACCESS" || err.code === "EPERM") && (Date.now() < stop)) {
continue;
if (!((err.code === "EACCESS" || err.code === "EPERM") && (Date.now() < stopTime))) {
throw err;
} else {
try {
fs.statSync(to);
} catch (errStat) {
if (errStat.code === "ENOENT") {
running();
} else {
throw err;
}
}
}
throw err;
}
}
}
})(fs.renameSync);
running();
};
})(fs.renameSync));
}
// if read() returns EAGAIN, then just try it again.
fs.read = (function(read) {
return function(fd, buffer, offset, length, position, callback) {
fs.read = ((function (read) {
return function (fd, buffer, offset, length, position, callback) {
callback = callback || noop;
let counter = 10;
function fsR(fd, buffer, offset, length, position, callback) {
return read(fd, buffer, offset, position, function(err) {
return read(fd, buffer, offset, position, function (err) {
if (err && err.code === "EAGAIN" && --counter > 0) {
addQueue([fsR, [fs, buffer, offset, length, position, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
//if (typeof callback === "function") {
callback.apply(this, arguments);
//}
processQueue();

@@ -141,9 +201,10 @@ }

return fsR(fd, buffer, offset, length, position, callback);
}
})(fs.read);
};
})(fs.read));
fs.readSync = (function(readSync) {
return function(fd, buffer, offset, length, position) {
fs.readSync = ((function (readSync) {
return function (fd, buffer, offset, length, position) {
let counter = 10;
while (true) {
function running() {
try {

@@ -153,3 +214,3 @@ return readSync(fd, buffer, offset, length, position);

if (err.code === "EAGAIN" && --counter > 0) {
continue;
return running();
}

@@ -159,7 +220,10 @@ throw err;

}
}
})(fs.readSync);
fs.readFile = (function(readFile) {
return function(path, options, callback) {
running();
};
})(fs.readSync));
fs.readFile = ((function (readFile) {
return function (path, options, callback) {
callback = callback || noop;
if (typeof options === "function") {

@@ -171,9 +235,9 @@ callback = options;

function fsRF(path, options, callback) {
return readFile(path, options, function(err) {
return readFile(path, options, function (err) {
if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
addQueue([fsRF, [path, options, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
//if (typeof callback === "function") {
callback.apply(this, arguments);
//}
processQueue();

@@ -185,7 +249,8 @@ }

return fsRF(path, options, callback);
}
})(fs.readFile);
};
})(fs.readFile));
fs.writeFile = (function(writeFile) {
return function(path, data, options, callback) {
fs.writeFile = ((function (writeFile) {
return function (path, data, options, callback) {
callback = callback || noop;
if (typeof options === "function") {

@@ -197,9 +262,9 @@ callback = options;

function fsWF(path, data, options, callback) {
return writeFile(path, data, options, function(err) {
return writeFile(path, data, options, function (err) {
if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
addQueue([fsWF, [path, data, options, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
//if (typeof callback === "function") {
callback.apply(this, arguments);
//}
processQueue();

@@ -211,7 +276,8 @@ }

return fsWF(path, data, options, callback);
}
})(fs.writeFile);
};
})(fs.writeFile));
fs.appendFile = (function(appendFile) {
return function(path, data, options, callback) {
fs.appendFile = ((function (appendFile) {
return function (path, data, options, callback) {
callback = callback || noop;
if (typeof options === "function") {

@@ -223,9 +289,9 @@ callback = options;

function fsAF(path, data, options, callback) {
return appendFile(path, data, options, function(err) {
return appendFile(path, data, options, function (err) {
if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
addQueue([fsAF, [path, data, options, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
//if (typeof callback === "function") {
callback.apply(this, arguments);
//}
processQueue();

@@ -237,19 +303,19 @@ }

return fsAF(path, data, options, callback);
}
})(fs.appendFile);
};
})(fs.appendFile));
fs.readdir = (function(readdir) {
return function(path, callback) {
fs.readdir = ((function (readdir) {
return function (path, callback) {
callback = callback || noop;
function fsRd(path, callback) {
return readdir(path, function(err, files) {
if (files && files.sort) {
files = files.sort();
}
return readdir(path, function (err, files) {
if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
addQueue([fsRd, [path, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
if (files && files.sort) {
//noinspection JSUnusedAssignment
files = files.sort();
}
callback.apply(this, arguments);
processQueue();

@@ -261,8 +327,10 @@ }

return fsRd(path, callback);
}
})(fs.readdir);
};
})(fs.readdir));
fs.readDir = fs.readdir;
fs.open = (function(open) {
return function(path, flags, mode, callback) {
fs.open = ((function (open) {
return function (path, flags, mode, callback) {
callback = callback || noop;
if (typeof mode === "function") {

@@ -274,9 +342,9 @@ callback = mode;

function fsO(path, flags, mode, callback) {
return open(path, flags, mode, function(err, fd) {
return open(path, flags, mode, function (err) {
if (err && (err.code === "EMFILE" || err.code === "ENFILE")) {
addQueue([fsO, [path, flags, mode, callback]]);
} else {
if (typeof callback === "function") {
callback.apply(this, arguments);
}
//if (typeof callback === "function") {
callback.apply(this, arguments);
//}
processQueue();

@@ -288,11 +356,11 @@ }

return fsO(path, flags, mode, callback);
}
})(fs.open);
};
})(fs.open));
var oldReadStream = fs.ReadStream;
let OldReadStream = fs.ReadStream;
function ReadStream(path, options) {
function ReadStream() {
if (this instanceof ReadStream) {
return oldReadStream.apply(this, arguments);
return OldReadStream.apply(this, arguments);
}

@@ -302,4 +370,4 @@ return ReadStream.apply(Object.create(ReadStream.prototype), arguments);

ReadStream.prototype = Object.create(oldReadStream.prototype);
ReadStream.prototype.open = function() {
ReadStream.prototype = Object.create(OldReadStream.prototype);
ReadStream.prototype.open = function () {
fs.open(this.path, this.flags, this.mode, (err, fd) => {

@@ -313,3 +381,3 @@ if (err) {

this.fd = fd;
this.emit("open", fd);
this.emit("open", this.fd);
this.read();

@@ -327,7 +395,7 @@ }

var oldWriteStream = fs.WriteStream;
let OldWriteStream = fs.WriteStream;
function WriteStream(path, options) {
function WriteStream() {
if (this instanceof WriteStream) {
return oldWriteStream.apply(this, arguments);
return OldWriteStream.apply(this, arguments);
}

@@ -337,4 +405,4 @@ return WriteStream.apply(Object.create(WriteStream.prototype), arguments);

WriteStream.prototype = Object.create(oldWriteStream.prototype);
WriteStream.prototype.open = function() {
WriteStream.prototype = Object.create(OldWriteStream.prototype);
WriteStream.prototype.open = function () {
fs.open(this.path, this.flags, this.mode, (err, fd) => {

@@ -346,3 +414,3 @@ if (err) {

this.fd = fd;
this.emit("open", fd);
this.emit("open", this.fd);
}

@@ -359,18 +427,7 @@ });

return fs;
}
function addQueue(elem) {
Queue.push(elem);
}
function processQueue() {
var elem;
elem = Queue.shift();
if (elem) {
elem[0].apply(null, elem[1]);
}
}
module.exports = patch(require("./fs"));
module.exports.enfsPatching = patch;

@@ -0,0 +0,0 @@ /**

@@ -9,3 +9,3 @@ /**

* @createdAt Created at 18-02-2016.
* @version 0.0.2
* @version 1.0.0
*/

@@ -17,3 +17,3 @@

const nodeConstants = require('constants');
const nodeConstants = require("constants");

@@ -24,58 +24,12 @@ function noop() {

module.exports = function patch(fs) {
// (re-)implement some things that are known busted or missing.
// lutimes implementation, or no-op
if (!fs.lutimes) {
patchLutimes(fs);
}
// https://github.com/isaacs/node-graceful-fs/issues/4
// Chown should not fail on einval or eperm if non-root.
// It should not fail on enosys ever, as this just indicates
// that a fs doesn't support the intended operation.
fs.chown = chownFix(fs, fs.chown);
fs.fchown = chownFix(fs, fs.fchown);
fs.lchown = chownFix(fs, fs.lchown);
fs.chmod = chownFix(fs, fs.chmod);
fs.fchmod = chownFix(fs, fs.fchmod);
fs.lchmod = chownFix(fs, fs.lchmod);
fs.chownSync = chownFixSync(fs, fs.chownSync);
fs.fchownSync = chownFixSync(fs, fs.fchownSync);
fs.lchownSync = chownFixSync(fs, fs.lchownSync);
fs.chmodSync = chownFix(fs, fs.chmodSync);
fs.fchmodSync = chownFix(fs, fs.fchmodSync);
fs.lchmodSync = chownFix(fs, fs.lchmodSync);
// if lchmod/lchown do not exist, then make them no-ops
if (!fs.lchmod) {
fs.lchmod = function(path, mode, cb) {
process.nextTick(cb);
};
fs.lchmodSync = noop;
}
if (!fs.lchown) {
fs.lchown = function(path, uid, gid, cb) {
process.nextTick(cb);
};
fs.lchownSync = noop;
}
};
function patchLutimes(fs) {
if (nodeConstants.hasOwnProperty("O_SYMLINK")) {
fs.lutimes = function(path, at, mt, cb) {
fs.open(path, nodeConstants.O_SYMLINK, function(er, fd) {
fs.lutimes = function (path, at, mt, cb) {
fs.open(path, nodeConstants.O_SYMLINK, function (er, fd) {
cb = cb || noop;
if (er) {
return cb(er)
return cb(er);
}
fs.futimes(fd, at, mt, function(er) {
fs.close(fd, function(er2) {
fs.futimes(fd, at, mt, function (er) {
fs.close(fd, function (er2) {
return cb(er || er2);

@@ -87,7 +41,7 @@ });

fs.lutimesSync = function(path, at, mt) {
var fd, ret, threw;
fd = fs.openSync(path, nodeConstants.O_SYMLINK);
fs.lutimesSync = function (path, at, mt) {
let ret;
let fd = fs.openSync(path, nodeConstants.O_SYMLINK);
threw = true;
let threw = true;
try {

@@ -101,2 +55,4 @@ ret = fs.futimesSync(fd, at, mt);

} catch (er) {
//this is intentional, we don't care if close throws an error because fd pointer
//may not exist at all
}

@@ -108,5 +64,5 @@ } else {

return ret;
}
};
} else {
fs.lutimes = function(_a, _b, _c, cb) {
fs.lutimes = function (_a, _b, _c, cb) {
process.nextTick(cb);

@@ -118,2 +74,33 @@ };

// ENOSYS means that the fs doesn't support the op. Just ignore
// that, because it doesn't matter.
//
// if there's no getuid, or if getuid() is something other
// than 0, and the error is EINVAL or EPERM, then just ignore
// it.
//
// This specific case is a silent failure in cp, install, tar,
// and most other unix tools that manage permissions.
//
// When running as root, or if other types of errors are
// encountered, then it's strict.
function processUidErr(err) {
return (!process.getuid || process.getuid() !== 0) && (err.code === "EINVAL" || err.code === "EPERM");
}
function chownErOk(err) {
if(!err || err && err.code === "ENOSYS") {
return true;
}
return processUidErr(err);
/*if (!process.getuid || process.getuid() !== 0) {
if (err.code === "EINVAL" || err.code === "EPERM") {
return true;
}
}
return false;*/
}
function chownFix(fs, orig) {

@@ -123,4 +110,4 @@ if (!orig) {

}
return function(target, uid, gid, cb) {
return orig.call(fs, target, uid, gid, function(err, res) {
return function (target, uid, gid, cb) {
return orig.call(fs, target, uid, gid, function (err, res) {
if (chownErOk(err)) {

@@ -131,3 +118,3 @@ err = null;

});
}
};
}

@@ -139,3 +126,3 @@

}
return function(target, uid, gid) {
return function (target, uid, gid) {
try {

@@ -148,33 +135,96 @@ return orig.call(fs, target, uid, gid);

}
};
}
function statFix(fs, orig) {
if (!orig) {
return orig;
}
// Older versions of Node erroneously returned signed integers for
// uid + gid.
return function (target, callback) {
callback = callback || function(){};
return orig.call(fs, target, function (er, stats) {
if(stats) {
if (stats.uid < 0) {
stats.uid += 0x100000000;
}
if (stats.gid < 0) {
stats.gid += 0x100000000;
}
}
callback.apply(this, arguments);
});
};
}
// ENOSYS means that the fs doesn't support the op. Just ignore
// that, because it doesn't matter.
//
// if there's no getuid, or if getuid() is something other
// than 0, and the error is EINVAL or EPERM, then just ignore
// it.
//
// This specific case is a silent failure in cp, install, tar,
// and most other unix tools that manage permissions.
//
// When running as root, or if other types of errors are
// encountered, then it's strict.
function chownErOk(err) {
if (!err) {
return true;
function statFixSync(fs, orig) {
if (!orig) {
return orig;
}
// Older versions of Node erroneously returned signed integers for
// uid + gid.
return function (target) {
let stats = orig.call(fs, target);
if (stats.uid < 0) {
stats.uid += 0x100000000;
}
if (stats.gid < 0) {
stats.gid += 0x100000000;
}
return stats;
};
}
if (err.code === "ENOSYS") {
return true;
module.exports = function patch(fs) {
// (re-)implement some things that are known busted or missing.
// lutimes implementation, or no-op
if (!fs.lutimes) {
patchLutimes(fs);
}
var nonroot = !process.getuid || process.getuid() !== 0;
if (nonroot) {
if (err.code === "EINVAL" || err.code === "EPERM") {
return true;
}
// https://github.com/isaacs/node-graceful-fs/issues/4
// Chown should not fail on einval or eperm if non-root.
// It should not fail on enosys ever, as this just indicates
// that a fs doesn't support the intended operation.
fs.chown = chownFix(fs, fs.chown);
fs.fchown = chownFix(fs, fs.fchown);
fs.lchown = chownFix(fs, fs.lchown);
fs.chmod = chownFix(fs, fs.chmod);
fs.fchmod = chownFix(fs, fs.fchmod);
fs.lchmod = chownFix(fs, fs.lchmod);
fs.chownSync = chownFixSync(fs, fs.chownSync);
fs.fchownSync = chownFixSync(fs, fs.fchownSync);
fs.lchownSync = chownFixSync(fs, fs.lchownSync);
fs.chmodSync = chownFix(fs, fs.chmodSync);
fs.fchmodSync = chownFix(fs, fs.fchmodSync);
fs.lchmodSync = chownFix(fs, fs.lchmodSync);
fs.stat = statFix(fs, fs.stat);
fs.fstat = statFix(fs, fs.fstat);
fs.lstat = statFix(fs, fs.lstat);
fs.statSync = statFixSync(fs, fs.statSync);
fs.fstatSync = statFixSync(fs, fs.fstatSync);
fs.lstatSync = statFixSync(fs, fs.lstatSync);
// if lchmod/lchown do not exist, then make them no-ops
if (!fs.lchmod) {
fs.lchmod = function (path, mode, cb) {
process.nextTick(cb);
};
fs.lchmodSync = noop;
}
return false;
}
if (!fs.lchown) {
fs.lchown = function (path, uid, gid, cb) {
process.nextTick(cb);
};
fs.lchownSync = noop;
}
};
{
"name": "enfspatch",
"version": "0.1.0",
"version": "1.0.0",
"description": "Patches for node fs module",

@@ -30,11 +30,11 @@ "license": "CC-BY-4.0",

"dependencies": {
"clone": "1.0.2",
"enfsaddins": "0.1"
"clone": "^2.1.1",
"enfsaddins": "^1.0.0"
},
"devDependencies": {
"eslint": "2.7.0",
"eslint-plugin-mocha": "2.1.0",
"mocha": "2.4.5",
"should": "8.3.0",
"rimraf": "2.5.2"
"eslint": "^3.18.0",
"eslint-plugin-mocha": "^4.9.0",
"mocha": "^3.2.0",
"rimraf": "^2.6.1",
"should": "^11.2.1"
},

@@ -41,0 +41,0 @@ "keywords": [

@@ -0,0 +0,0 @@ [![Build Status](https://travis-ci.org/n3okill/enfspatch.svg)](https://travis-ci.org/n3okill/enfspatch)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc