Skip to content

Commit 08471b2

Browse files
committed
Eval is less evil than monkey-patching a global
Closes isaacs#27 Due to the way that streams and the various FS methods interact with one another, NOT monkey-patching doesn't work. However, since the 'fs' module is effectively a process-level global, making monkey-patchy changes to it can introduce some really weird behavior. Even if such behavior is not actively harmful, it can end up being applied multiple times, and wrapping 'fs.open()' over and over again is a bit of a bummer when it comes to performance. Mixing 'eval()' and a private/internal Node API kind of gives me the heeby jeebies. If anyone can come up with a less evil way to do this, I'll be happy to accept a patch.
1 parent baa5f09 commit 08471b2

File tree

5 files changed

+9
-6
lines changed

5 files changed

+9
-6
lines changed

fs.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// eeeeeevvvvviiiiiiillllll
2+
// more evil than monkey-patching the native builtin?
3+
// Not sure.
4+
5+
eval(process.binding('natives').fs)

graceful-fs.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// Monkey-patching the fs module.
22
// It's ugly, but there is simply no other way to do this.
3-
var fs = module.exports = require('fs')
3+
var fs = module.exports = require('./fs.js')
44

55
var assert = require('assert')
66

77
// fix up some busted stuff, mostly on windows and old nodes
88
require('./polyfills.js')
99

10-
// The EMFILE enqueuing stuff
11-
1210
var util = require('util')
1311

1412
function noop () {}

polyfills.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var fs = require('fs')
1+
var fs = require('./fs.js')
22
var constants = require('constants')
33

44
var origCwd = process.cwd

test/open.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var test = require('tap').test
22
var fs = require('../graceful-fs.js')
33

44
test('graceful fs is monkeypatched fs', function (t) {
5-
t.equal(fs, require('fs'))
5+
t.equal(fs, require('../fs.js'))
66
t.end()
77
})
88

test/readdir-sort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var test = require("tap").test
2-
var fs = require("fs")
2+
var fs = require("../fs.js")
33

44
var readdir = fs.readdir
55
fs.readdir = function(path, cb) {

0 commit comments

Comments
 (0)