From 1bf13d39157d64de824ff60e4fc68850c1f8e575 Mon Sep 17 00:00:00 2001 From: Brendan Ashworth Date: Sun, 21 Jun 2015 15:06:30 -0700 Subject: [PATCH 1/2] fix fs module process.binding hackery Fix the horrible atrocity introduced in 08471b246bec73848b53e9ad4972f5fe02031805. Previously it would evaluate the code directly through process.binding(), and it now creates a new Object with the prototype being the real fs module, exported by core. Ref: https://github.com/nodejs/io.js/pull/2026 --- fs.js | 12 +----------- graceful-fs.js | 2 -- test/module-evil-hackery.js | 10 ++++++++++ 3 files changed, 11 insertions(+), 13 deletions(-) create mode 100644 test/module-evil-hackery.js diff --git a/fs.js b/fs.js index 64ad980..7e4db24 100644 --- a/fs.js +++ b/fs.js @@ -1,11 +1 @@ -// eeeeeevvvvviiiiiiillllll -// more evil than monkey-patching the native builtin? -// Not sure. - -var mod = require("module") -var pre = '(function (exports, require, module, __filename, __dirname) { ' -var post = '});' -var src = pre + process.binding('natives').fs + post -var vm = require('vm') -var fn = vm.runInThisContext(src) -fn(exports, require, module, __filename, __dirname) +module.exports = Object.create(require('fs')) diff --git a/graceful-fs.js b/graceful-fs.js index fb206b8..9f0e899 100644 --- a/graceful-fs.js +++ b/graceful-fs.js @@ -1,5 +1,3 @@ -// Monkey-patching the fs module. -// It's ugly, but there is simply no other way to do this. var fs = module.exports = require('./fs.js') var assert = require('assert') diff --git a/test/module-evil-hackery.js b/test/module-evil-hackery.js new file mode 100644 index 0000000..ce63666 --- /dev/null +++ b/test/module-evil-hackery.js @@ -0,0 +1,10 @@ +var test = require('tap').test +var realfs = require('fs') +var fs = require('../') + +test('module assignment should not leak', function (t) { + t.plan(1) + + fs.abc = 3 + t.equal(realfs.abc, undefined) +}) From 810e80d9071a306a5379e72e3b7c0545437ffa0c Mon Sep 17 00:00:00 2001 From: Eran Tiktin Date: Sat, 6 Jun 2015 09:03:22 +0300 Subject: [PATCH 2/2] Add test case --- test/stats.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/stats.js diff --git a/test/stats.js b/test/stats.js new file mode 100644 index 0000000..4519012 --- /dev/null +++ b/test/stats.js @@ -0,0 +1,12 @@ +var test = require('tap').test +var fs = require('fs') +var gfs = require('../graceful-fs.js') + +test('graceful fs uses same stats constructor as fs', function (t) { + t.equal(gfs.Stats, fs.Stats, 'should reference the same constructor') + t.ok(fs.statSync(__filename) instanceof fs.Stats, + 'should be instance of fs.Stats') + t.ok(gfs.statSync(__filename) instanceof fs.Stats, + 'should be instance of fs.Stats') + t.end() +})