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

Intercept absolute paths #43

Closed
dyladan opened this issue Aug 19, 2020 · 4 comments · Fixed by #45
Closed

Intercept absolute paths #43

dyladan opened this issue Aug 19, 2020 · 4 comments · Fixed by #45
Assignees
Labels
agent-nodejs Make available for APM Agents project planning.

Comments

@dyladan
Copy link
Contributor

dyladan commented Aug 19, 2020

It seems like it is impossible to use require-in-the-middle to intercept requires of absolute file paths. For instance, I would like to intercept the require of /var/runtime/util.js which is outside of my node_modules and not a core module. I tried to do hook(['/var/runtime/util'], onRequire), but when require('/var/runtime/util') is called, require-in-the-middle logs require-in-the-middle could not parse filename: /var/runtime/util.js +0ms.

It looks like require-in-the-middle is using the module-details-from-path module, which fails to parse this absolute path.

@watson
Copy link
Contributor

watson commented Aug 20, 2020

This module is meant as a way to patch files in 3rd party modules that you can't normally edit yourself. If the file you're trying to patch is not inside node_modules, I would assume that you could just go and edit the file manually. What's the use-case for wanting to patch a file outside of the node_modules folder? I'd be happy to consider this if it makes sense 😃

@dyladan
Copy link
Contributor Author

dyladan commented Aug 20, 2020

I am trying to patch the runtime internals of AWS lambda. Specifically, the /var/runtime/RAPIDClient.js file.

@dyladan
Copy link
Contributor Author

dyladan commented Aug 20, 2020

I am happy to provide a PR if you'd like. I already modified a version locally which works with the restriction that the only way to intercept files like this is by specifying their absolute path in the whitelist.

const Hook = require("require-in-the-middle");

// also works with "/var/runtime/RAPIDClient.js"
Hook(["/var/runtime/RAPIDClient"], (exports, name, basedir) => {
  console.log(name) // RAPIDClient
  console.log(basedir) // /var/runtime

  return exports;
});

@watson
Copy link
Contributor

watson commented Aug 20, 2020

Yes, please send a PR. It would be great to support this use-case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
None yet
4 participants