Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does module.js directly use the process bindings instead of fs? #4190

Closed
ariporad opened this issue Dec 8, 2015 · 7 comments
Closed
Labels
module Issues and PRs related to the module subsystem. question Issues that look for answers.

Comments

@ariporad
Copy link

ariporad commented Dec 8, 2015

Hi Node Core Team!

First of all, Thanks so much for making such an awesome thing!

Second, I'm currently working on developing something to make hooking require easier, it's called pirates. (I think very strongly that the node core should have an official way of hooking require, as it's a rather common thing to do, and very frequently done wrong, and maybe even worth bending the rules for 😉, but I know the module system is locked and off limits, so...).

As of right now, I'm trying to improve the tests (see danez/pirates#5). I wanted to do this with mock-fs, which overrides some methods of the fs module to be backed by a memory-based file system.

However, I discovered that module.js uses the fs binding directly to detect if file exist, despite using fs to actually read the file. I'm sure there's a good reason behind this that I'm just not smart enough to figure out, but for learning purposes, what is it? If there's no reason, would anyone object to me switching it (assuming the tests pass, of course)?

Thanks!

@mscdex mscdex added question Issues that look for answers. module Issues and PRs related to the module subsystem. labels Dec 8, 2015
@bnoordhuis
Copy link
Member

The reason is performance, see commits 98649fd...1bbf8d0 and a71ee93.

@ariporad
Copy link
Author

ariporad commented Dec 8, 2015

Ahh... I afraid of that.

This leads to the interesting situation that a non-existant module can't be mocked, but an existing one easily can. It would be nice if there was a way around this, any ideas? Currently, I have a directory of empty files for the purpose of mocking them out 😢.

@bnoordhuis
Copy link
Member

I don't think there's an easy way around that. Maybe module.Module#_compile(code, filename)?

@MauriceButler
Copy link
Contributor

@ariporad Forgive me if I am misinterpreting your intent but a combination of the functionality
here rooty and here fraudster might give you what you are looking for

i.e. overriding the Module._load and Module._resolveFilename methods to inject your pipeline.

@ariporad
Copy link
Author

@MauriceButle: Unfortunately neither of those work (rooty looks useful though), I ended up with a directory of empty js files to mock out. Thanks though!

@bnoordhuis
Copy link
Member

@ariporad Can we close this?

@ariporad
Copy link
Author

@bnoordhuis: Oh yeah, I forgot. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

4 participants