Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bagel-module-loader

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bagel-module-loader

Flexible module loader

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Description

bagel-module-loader contains all module loading logic for bagel.

Installation

npm install --save bagel-module-loader

Usage

import createLoader from 'bagel-module-loader';

// initialize loader
const load = createLoader({resolvers: [testResolver]});

// If desired, assemble some context data which will be accessible at multiple points during the module load process
const context = {foo: 'foo'};

// load some modules!
const loaded = load('foo', __dirname, {});

Configuration Options

{
  // list of module resolvers for custom resolving
  resolvers?: Array<Resolver>,
  // list of methods to perform an action before default require() is called
  interceptors?: Array<Interceptor>,
  // list of methods to convert source code
  sourceCodeTransformers?: Array<Transformer>,
  // method to wrap the module being served
  wrapModule?: source: string => result: string,
  // method to customize cache key generation within bagel
  generateModuleCacheKey?: GenerateModuleCacheKey,
}

Request Context

A number of the hooks in the module loader provide access to a context object which can be passed in with each call to the modoule loader. The core bagel module supplies a JobHandlerRequest as context.

({ jobRequest, parentBatchRequest, jobResponseMetadata, batchResponseMetadata }: JobHandlerRequest)

Module Resolvers

type Resolver = (
  dependencyID: string,
  from: string,
  requestContext: {}
) => string | null;

Bagel's resolve method loops through the resolvers provided, falling back on default node resolution if none of the provided resolvers are able to resolve the module.

Module Interceptors

Module Interceptors Flow

type Interceptor = ({
  moduleID: string,
  requestContext: {[string]: any},
  next: string => any
}) => any;

Interceptors allow developers to tap into the module loading process. In the process of loading a module, bagel will sequentially step through the interceptors supplied before the actual 'require' function is invoked. These methods have access to a specific job's moduleID, requestContext and next. Interceptors can modify the requestContext supplied and can invoke next() to delegate to the next interceptor in the chain. Interceptors can also short-circuit the module loading process by returning early. These two flows are illustrated in the diagram above.

Source Code Transformers

type Transformer = ({path: string, source: string}) => {
  errors: Array<string>,
  transformedSource: string
};

Provide a source code transformer if you would like to transform the initial module source code.

Wrap Module

Pass in your own wrapper function if you would like to customize how a module is wrapped before it is loaded.

Generate Module Cache Key

type GenerateModuleCacheKey = ({
  moduleID: string,
  requestContext: Object,
  pathToSourceFile: string
}) => string | null;

// ie)
({moduleID}) => `${moduleID}_${pretendCacheBuster}`;

bagel-module-loader caches compiled source code. If a cache key isn't found, the source code will be loaded from disk in the course of loading the module. generateModuleCacheKey is a method to generate these cache keys.

FAQs

Package last updated on 18 Jul 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc