Socket
Socket
Sign inDemoInstall

@newrelic/ritm

Package Overview
Dependencies
13
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @newrelic/ritm

Module to hook into the Node.js require function


Version published
Weekly downloads
182K
decreased by-15.93%
Maintainers
1
Install size
293 kB
Created
Weekly downloads
 

Changelog

Source

v7.2.0

  • Improve performance (possibly significantly) when using the Hook without a modules argument, e.g. new Hook(null, onrequire), by caching exports in more cases. Contributed by @nwalters512. (https://github.com/elastic/require-in-the-middle/issues/75)

Readme

Source

@newrelic/ritm

Hook into the Node.js require function. This allows you to modify modules on-the-fly as they are being required.

Installation

npm install @newrelic/ritm --save

Usage

const path = require('path')
const { Hook } = require('@newrelic/ritm')

// Hook into the express and mongodb module
new Hook(['express', 'mongodb'], function (exports, name, basedir) {
  const version = require(path.join(basedir, 'package.json')).version

  console.log('loading %s@%s', name, version)

  // expose the module version as a property on its exports object
  exports._version = version

  // whatever you return will be returned by `require`
  return exports
})

API

The @newrelic/ritm module exposes a single function:

hook = new Hook([modules][, options], onrequire)

When called a hook object is returned.

Arguments:

  • modules <string[]> An optional array of module names to limit which modules trigger a call of the onrequire callback. If specified, this must be the first argument. Both regular modules (e.g. react-dom) and sub-modules (e.g. react-dom/server) can be specified in the array.
  • options <Object> An optional object containing fields that change when the onrequire callback is called. If specified, this must be the second argument.
    • options.internals <boolean> Specifies whether onrequire should be called when module-internal files are loaded; defaults to false.
  • onrequire <Function> The function to call when a module is required.

The onrequire callback will be called the first time a module is required. The function is called with three arguments:

  • exports <Object> The value of the module.exports property that would normally be exposed by the required module.
  • name <string> The name of the module being required. If options.internals was set to true, the path of module-internal files that are loaded (relative to basedir) will be appended to the module name, separated by path.sep.
  • basedir <string> The directory where the module is located, or undefined for core modules.

Return the value you want the module to expose (normally the exports argument).

hook.unhook()

Removes the onrequire callback so that it will not be triggerd by subsequent calls to require().

License

MIT

Keywords

FAQs

Last updated on 21 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc