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

umid

v1.0.11

Published

A generic and lightweight express like middleware with zero dependencies

Downloads

14

Readme

Universal Middleware

Fast, lightweight middleware framework.

Features

  • Lightweight - less than 370 bytes minified
  • Browser and Node - Use in browser and node
  • Async Await and Promise support - Support both async await and promise functions
  • No Dependency - No Bloating. No external dependencies
  • Express.js style middlware - Express.js like design

Install

$ npm install umid

$ yarn add umid

Usage

Basic Example

const Middleware = require("umid");

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

let m = new Middleware();

// Plain middleware
m.use((context, next) => {
	console.log("M1 context", context);
	context.exec = "M1 executed";
	next();
})

// Async await middleware
.use(async (context, next) => {
	await sleep(2000);
	console.log("M2 context", context);
	context.exec = "M2 executed in 2 seconds";
	next();
});

// Async await middleware
let m3 = async (context, next) => {
	await sleep(500);
	await sleep(500);
	console.log("M3 context", context);
	context.exec = "M3 executed in 1 second";
	next();
};

// settimeout middleware
let m4 = (context, next) => {
	setTimeout(() => {
		console.log("M4 context", context);
		context.exec = "M4 executed in 2 second";
		next();
	}, 2000);
};

// Promise middleware
let m5 = async (context, next) => {
	Promise.resolve().then(() => {
		console.log("M5 context", context);
		context.exec = "M5 executed";
		next();
	});
};

// Error middleware
let errorMiddleware = async (context, next) => {
	throw "got error!!";
	next();
};

m.use(m3, [m4, m5]);

// m.use(errorMiddleware)

(async () => {
	let context = {};
	let result = await m.process(context);
	console.log(result);
})();

or

let context = {};
m.run((err, context) => {
	if (err) console.log("error!", err, context);
	else console.log("Complete!", context);
}, context);

Error Middleware example

const Middleware = require("umid");

let m = new Middleware();

// Pass string in next to create an error
m.use((context, next) => {
	    next("Try again");
    })

	// Throw an error

	.use((context, next) => {
		throw new Error("Try again");
	})

	// Throw error string

	.use((context, next) => {
		throw "Try again";
	})

	// throw error in next function

	.use((context, next) => {
		next(new Error("Try again"));
	})

	.use((context, next) => {
		console.log("I never get executed :(");
	})

	(async () => {
		let context = {};
		try {
			let result = await m.process(context);
			console.log(result);	
		} catch (error) {
			console.error(error);
		}
		
	})();

API

Middleware()

Returns an instance of a middleware

use((context, next))

Attach middleware(s).

These are the different signatures for use function

use(...middlewares)

use(middleware)

use([middleware1, middleware2])

use(middleware1, middleware2)

use(middleware1, [middleware2, middleware3], middlware4)

middleware

This is the signature for defining middleware.

(context, next)

context

The context that passes to the next middleware. This can be updated and passed on to next middleware.

next

Type: Function

Most importantly, a middleware must either call next() or terminate the response with next('reason').

run(initialContext)

Type: async Function. Returns updated context

initialContext

This is for assigning the middlewares with intial context object to pass.

process((err, context), initialContext)

(err, context)

Type: Function

Its signature is (err, context), where err is the String or Error thrown by the middleware.

initialContext

This is for assigning the middlewares with intial context object to pass.

Middleware Errors

If an error arises within a middleware, the loop will be exited. This means that no other middleware will execute.

There are three ways to "throw" an error from within a middleware function.

  1. Pass any string to next('Err')

This will exit the loop with your error string as the error message.

new Middleware().use((context, next) => {
  next('Try again');
});
  1. Pass an Error to next()

This is similar to the above option.

new Middleware().use((context, next) => {
  let err = new Error('Try again');
  next(err);
});
  1. Terminate with throw
new Middleware().use((context, next) => {
  let err = new Error('Try again');
  throw err;
});

License

MIT © Kethan Surana