npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gasket/plugin-middleware

v7.0.1

Published

Handles common server engine setups for routing and executing lifecycles.

Downloads

50

Readme

@gasket/plugin-middleware

The @gasket/plugin-middleware plugin provides an easy way to apply middleware to Express or Fastify.

Installation

To install the plugin, run:

npm i @gasket/plugin-middleware

Then, update your gasket.js configuration file to include the plugin:

// gasket.js

+ import pluginMiddleware from '@gasket/plugin-middleware';

export default makeGasket({
  plugins: [
+   pluginMiddleware
  ]
});

Configuration

You can configure the middleware settings under either the express or fastify sections in your gasket.js file, depending on which framework you are using.

Configuration Options

  • compression (default: true): Enable or disable response compression. Set to false if compression is handled elsewhere.
  • excludedRoutesRegex: (deprecated) Use middlewareInclusionRegex instead.
  • middlewareInclusionRegex: A regular expression to filter request URLs and determine when Gasket middleware should run. You can use this to exclude routes like static resource paths.
  • trustProxy: Enable the "trust proxy" option. Refer to the Fastify trust proxy documentation or the Express trust proxy documentation for more details.
  • routes: A path or glob pattern pointing to files that export route-defining functions. These functions receive the app object (Fastify or Express) to attach handlers and middleware.

Example Configuration for Express

// gasket.js

export default makeGasket({
  plugins: {
    pluginMiddleware,
    pluginExpress
  },
  express: {
    compression: false,
    routes: 'api/*.js',
    middlewareInclusionRegex: /^(?!\/_next\/)/,
    trustProxy: true
  }
});

Example Configuration for Fastify

// gasket.js

export default makeGasket({
  plugins: {
    pluginMiddleware,
    pluginFastify
  },
  fastify: {
    compression: false,
    routes: 'api/*.js',
    middlewareInclusionRegex: /^(?!\/_next\/)/,
    trustProxy: true
  }
});

Middleware paths

You can define middleware paths in your gasket.js file using the middleware property. This property is an array of objects that map plugins to specific route or path patterns, allowing you to control which middleware is triggered for specific requests.

// gasket.js

export default makeGasket({
  ...
  middleware: [
    {
      plugin: 'gasket-plugin-example', // Name of the Gasket plugin
      paths: ['/api']
    },
    {
      plugin: '@some/gasket-plugin-example',
      paths: [/\/default/]
    },
    {
      plugin: '@another/gasket-plugin-example',
      paths: ['/proxy', /\/home/]
    }
  ]
  ...
});

Lifecycles

middleware

The middleware lifecycle is executed when the Fastify or Express server is created. It applies all returned functions as middleware.

export default {
  name: 'sample-plugin',
  hooks: {
    /**
    * Add Fastify middleware
    *
    * @param {Gasket} gasket The Gasket API
    * @param {Fastify} app - Fastify app instance
    * @returns {function|function[]} middleware(s)
    */
    middleware: function (gasket, app) {
      return require('x-xss-protection')();
    }
  }
}

Logging

This plugin attaches a logger object to the request object. This object has a metadata method that allows you to attach details to any log entry related to the request. For example, you can add the user ID to each log entry. When logging within the context of a request, use the req.logger object instead of the global gasket.logger so that contextual information is included in the log entry. Here is an example of how to attach metadata to req.logger object and how to use it:

function someMiddleware(req, res, next) {
  req.logger.metadata({ userId: req.user.id });
  next();
}

function someOtherMiddleware(req, res, next) {
  req.logger.info('Some log message');
  next();
}

You can also return an array if you need to inject multiple middleware functions.