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

Override module.createRequire #9426

Closed
SimenB opened this issue Jan 19, 2020 · 5 comments
Closed

Override module.createRequire #9426

SimenB opened this issue Jan 19, 2020 · 5 comments

Comments

@SimenB
Copy link
Member

SimenB commented Jan 19, 2020

People can do

import {createRequire} from 'module';

const myRequire = createRequire(__filename);

require('./some-file');

today, which will break out of the sandbox. We need to intercept this function when module is imported and provide our own implementation. This will be especially important when we start supporting ES Modules.

Relevant places in the code:
https://github.com/facebook/jest/blob/2ece4f979deaa2504038e8cca1f6b97925c3eb26/packages/jest-runtime/src/index.ts#L860-L866
Need to check for moduleName === 'module' and call
https://github.com/facebook/jest/blob/2ece4f979deaa2504038e8cca1f6b97925c3eb26/packages/jest-runtime/src/index.ts#L970-L973
where we already have a function to create a require from a path.

I'm not sure if each createRequire call returns its own cache or not.

Docs: https://nodejs.org/api/modules.html#modules_module_createrequire_filename

We should probably inspect the real module export and see if createRequire is there and only add ours if it is. We should also check if createRequireFromPath exist and support that as well. The latter only support a full path, while the former supports URLs.


We should probably just no-op syncBuiltinESMExports at the same time

@doniyor2109
Copy link
Contributor

Hi @SimenB . Can I work on this?

@SimenB
Copy link
Member Author

SimenB commented Jan 26, 2020

Yeah, go for it!

@SimenB
Copy link
Member Author

SimenB commented Jan 26, 2020

I tested a bit in the repl now, and require.cache === modules.createRequire(somePath).cache. So they all share the same cache. This makes the implementation pretty simple, as it should only change fromPath which is what our _createRequireImplementation already take in 👍

EDIT: One complication is createRequire support URL - I updated the OP with some extra details. Shouldn't be too hard, but it's not as straightforward

@SimenB
Copy link
Member Author

SimenB commented Feb 1, 2020

Fixed by #9469

@SimenB SimenB closed this as completed Feb 1, 2020
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants