5.0.0 • Published 3 months ago

@contrast/require-hook v5.0.0

Weekly downloads
3,561
License
MIT
Repository
-
Last release
3 months ago

@contrast/require-hook

Pipeline Status

Intercept calls to require in order to modify or replace exports.

Usage

Class: RequireHook

Instantiation

const RequireHook = require('./lib');
const requireHook = new RequireHook();

The RequireHook constructor accepts a pino logger as an argument.

.resolve(descriptor, ...handlers)

Options:

  • descriptor: This can be a string or an object describing the module you want to intercept. If a string is used, or if the version field of the descriptor isn't set, all versions of the described module will be matched. Descriptors can have a name, version, and file property.

  • handlers: The remaning arguments are the handlers which will be invoked when the described module is require'd. Each handler is passed the exported module and metadata including the module's root directory and its name and version as seen in its package.json file. If a handler returns a truthy value, then that value will replace the return value of require.

Note: Registered handlers run once per unique instance of an export matching a descriptor.

.install()

This will monkey-patch Module.prototype.require so that exports can be intercepted. The monkey-patching will only happen once regardless of how many times this is invoked.

.uninstall()

This will reset Module.prototype.require to its value before being monkey-patched by the instance.

Examples

Use case: For express versions greater than or equal to 4, intercept the export of the package's lib/view.js file (relative to the package's base directory) and apply a tag to the exported function.

const RequireHook = require('./lib');
const requireHook = new RequireHook();
requireHook.resolve(
  {
    name: 'express',
    version: '>=4',
    file: 'lib/view.js',
  },
  (xport, metadata) => {
    // Read from the package.json:
    // - metadata.name
    // - metadata.version
    // Absolute path to file:
    // - metadata.packageDir

    // xport === function View() { /*...*/ }
    xport['I was intercepted'] = true;
  },
);

Use case: Intercept all versions of body-parser and replace the exported functions.

const RequireHook = require('./lib');
const requireHook = new RequireHook();
requireHook.resolve({ name: 'body-parser' }, (xport, metadata) => {
  // Read from the package.json:
  // - metadata.name
  // - metadata.version
  // Absolute path to file:
  // - metadata.packageDir

  // xport === function bodyParser() { /*...*/ }
  return function bodyParserReplacement() {
    /*...*/
  };
});
5.0.0

3 months ago

4.1.4

7 months ago

4.1.3

7 months ago

4.1.0

7 months ago

4.0.0

8 months ago

4.1.2

7 months ago

4.2.0

7 months ago

4.1.1

7 months ago

3.2.3

10 months ago

3.2.2

1 year ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.11

2 years ago

2.0.7

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.8

2 years ago

2.0.6

2 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1-beta.3

4 years ago

1.0.1-beta.2

4 years ago

1.0.1-beta.1

4 years ago

1.0.1-beta.0

4 years ago

1.0.0

4 years ago