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

middleware-center

v1.2.0

Published

middleware center for JavaScript, just like koa middleware.

Downloads

9

Readme

middleware-center

NPM version travis ci Coverage Status

middleware center for JavaScript, just like koa middleware.

Installation

npm i middleware-center

Usage

Sync method

import MiddlewareCenter from "middleware-center"

const middlewareCenter = new MiddlewareCenter()

middlewareCenter.use(function validate(ctx, next) {
    console.log('before validate', ctx);
    if (ctx.name === 'zhh' && ctx.passwd === '123') {
        next()
        console.log('after validate');
    }
})

middlewareCenter.use(function request(ctx, next) {
    console.log('before request', ctx);
    next()
    console.log('after request');
})

middlewareCenter.use(function success(ctx, next) {
    console.log('on success', ctx);
})

middlewareCenter.handleRequest({name: 'zhh', passwd: '123'})

/* result
before validate { name: 'zhh', passwd: '123' }
before request { name: 'zhh', passwd: '123' }
on success { name: 'zhh', passwd: '123' }
after request
after validate
*/

Async method

import MiddlewareCenter from "middleware-center"

const middlewareCenter = new MiddlewareCenter()

var async1 = function () {
    return new Promise((resolve, reject) => {
        setTimeout(function () {
            resolve(1)
        }, 10)
    })
}

var async2 = function (ctx, next) {
    return new Promise((resolve, reject) => {
        setTimeout(function () {
            resolve(2)
        }, 20)
    })
}

var m1 = async function (ctx, next) {
    console.log('before m1')
    let result =  await async1()
    await next()
    console.log('after m1')
}

var m2 = async function (ctx, next) {
    console.log('before m2')
    let result = await async2()
    console.log('after m2')
}

middlewareCenter.use(m1)
middlewareCenter.use(m2)

middlewareCenter.handleRequest()

/* result
before m1
before m2
after m2
after m1
*/

Custom MiddlewareCenter

import MiddlewareCenter from "middleware-center"

const middlewareCenter = new CustomMiddlewareCenter()
middlewareCenter.handle('uploader')

class CustomMiddlewareCenter extends MiddlewareCenter {

    constructor() {
        super()
        this._middlewareMap = { 'uploader': [this.beforeUpload, this.startUplaod, this.finishUpload] }
        this.content = ""
    }

    handle(name) {
        let middlewares = this._middlewareMap[name]
        for (let middleware of middlewares) {
            this.use(middleware)
        }
        this.handleRequest(this)
    }

    // Middlewares

    async beforeUpload (ctx, next) {
        console.log('beforeUpload')
        ctx.content = await ctx.genContent()
        await next()
        console.log('after beforeUpload')
    }

    async startUplaod (ctx, next) {
        console.log('startUplaod')
        let result = await ctx.upload(ctx.content)
        await next()
        console.log('after startUplaod')
    }

    finishUpload (ctx, next) {
        console.log('finishUpload')
        //do something like notify listeners
        console.log('after finishUpload')
    }

    // Helpers
    genContent() {
        return new Promise((resolve, reject) => {
            setTimeout(function () {
                resolve('upload content')
            }, 3)
        })
    }

    upload(content) {
        return new Promise((resolve, reject) => {
            setTimeout(function () {
                resolve(true)
            }, 5)
        })
    }
}

/* result
beforeUpload
startUplaod
finishUpload
after finishUpload
after startUplaod
after beforeUpload
*/

Handle Error

middlewareCenter.handleRequest(ctx).catch(function(err){
  console.log(err.message)
})

API

use(middleware)

push a middleware. middleware must be function.

handleRequest(context)

handle request with context. It will trigger next chain.