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

@mellkam/middleware

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mellkam/middleware

Universal and lightweight middleware utility

  • 0.2.2
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

@mellkam/middleware

This library provides a simple implementation of a middleware pattern that can be used to wrap or extend functions with additional functionality without changing their original implementation.

Installation

npm install @mellkam/middleware

Usage

import { type Middleware, wrap } from "@mellkam/middleware";

const timer: Middleware = (args, next) => {
  console.time("timer");
  const result = next(...args);
  console.timeEnd("timer");
  return result;
};

console.log(wrap(fibonacci).use(timer).run(40));

// timer: 1.518s
// 102334155

Execution order (LIFO)

Middlewares are executed in LIFO order ("Last In, First Out").

Everytime you push a new middleware to the stack, it is added as a new onion layer on top of all existing ones.

import { wrap } from "@mellkam/middleware";

const log = wrap(console.log)
  .use((args, next) => {
    console.log("Middleware 1: Before");
    next(...args);
    console.log("Middleware 1: After");
  })
  .use((args, next) => {
    console.log("Middleware 2: Before");
    next(...args);
    console.log("Middleware 2: After");
  }).run;

log("Actual func call");

Logs:

Middleware 2: Before
Middleware 1: Before
Actual func call
Middleware 1: After
Middleware 2: After

Before/After concept

const mw: Middleware = (args, next) => {
  //
  // BEFORE
  //
  next(...args);
  //
  // AFTER
  //
};

Check out more examples here

Keywords

FAQs

Package last updated on 29 Apr 2023

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