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

node-ware

v1.0.9

Published

Middleware for nodejs, likes decorator.

Downloads

5

Readme

node-ware

NPM Version

Middleware for nodejs, likes decorator.

Middleware functions for node-ware are functions that have access to the input object and the output object. When the run() is invoked, the middlewares will get executed in the order of their use calls or their order in the array, and return a Promise contains the final input and output if resolved.

Installation

npm i node-ware

Usage

const Ware = require('node-ware');
let ware = new Ware();

function addA(input, output) {
    input.a = 'a';
    return [input, output]
}

function addB(input, output) {
    output.b = 'b';
    return [input, output]
}

ware.use(addA);
ware.use(addB);
ware.run().then(([input, output]) => {
    console.log(input, output);
}).catch(err => {
    console.log(err);
});

A complete example

A ware for request, which makes it easier and cleaner to add some functions like logger, timer and storer during the request.

const co = require('co');
const fs = require('fs');
const log = require('pino')();
const Ware = require('./index');
const request = require('request');

let wrequest = new Ware();

let proxy = function (input) {
    input.proxy = "http://8.8.8.8:8080";
    return input
};

let time = function () {
    return {time: new Date()}
};

let rp = function (input) {
    return new Promise(function (resolve, reject) {
        request(input, function (err, res) {
            if (err) {
                reject(err)
            } else {
                setTimeout(() => {
                    resolve([, res])
                }, 1000)
            }
        })
    })
};

let timeEnd = function () {
    return {timeEnd: new Date()}
};

let logger = function (input) {
    log.info(`${input.description} start at ${input.time.toISOString()} end at ${input.timeEnd.toISOString()} takes ${input.timeEnd - input.time} ms`);
};

let storer = function (input, output) {
    fs.writeFile(input.description + '.txt', output.body, (err) => {
        if (err) throw err;
        console.log('The file ' + input.description + '.txt has been saved!');
    });
};

wrequest
    .use(time)
    .use(rp)
    .use(timeEnd)
    .use(() => ([, {message: "OK"}]))
    .use(logger)
    .use(storer);

co(function* () {

    console.log(wrequest.hold());

    let [input1, res1] = yield wrequest
        .pre(() => ({description: 'req1'}))
        .run({url: "http://example.com/"});

    console.log(wrequest.hold()); // Anonymous functions were removed after running

    let [, res2] = yield wrequest
        .pre(() => ({description: 'req2'}))
        .run({url: "http://example.com/"});

    console.log(input1, res1.statusCode, res1.message, res2.statusCode, res2.message);

}).catch(err => {
    console.log(err);
});

console's output as follows

[ [Function: time],
  [Function: rp],
  [Function: timeEnd],
  [Function],
  [Function: logger],
  [Function: storer] ]

{"pid":89464,"hostname":"Cooper-X","level":30,"time":1506002581239,"msg":"req1 start at 2017-09-21T14:02:59.244Z end at 2017-09-21T14:03:01.235Z takes 1991 ms","v":1}

[ [Function: time],
  [Function: rp],
  [Function: timeEnd],
  [Function: logger],
  [Function: storer] ]

The file req1.txt has been saved!

{"pid":89464,"hostname":"Cooper-X","level":30,"time":1506002583225,"msg":"req2 start at 2017-09-21T14:03:01.244Z end at 2017-09-21T14:03:03.225Z takes 1981 ms","v":1}

{ url: 'http://example.com/',
  description: 'req1',
  time: 2017-09-21T14:02:59.244Z,
  timeEnd: 2017-09-21T14:03:01.235Z } 200 'OK' 200 undefined

The file req2.txt has been saved!

Tests

no scripts

Contributing

...