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

express-delayed-response

v0.4.1

Published

automatically provides polling responses for long running operations

Downloads

2,929

Readme

express-delayed-response

npm version Build Status Coverage Status

Break long running processes into multiple requests with a status end point.

delay

Use delay middleware to automatically provide polling and status functionality to following handlers.

const { delay } = require('express-delayed-response').init();

app.get('/path', delay(), potentiallySlowHandler);

delay.timeout

By default delay will respond within 5 seconds, either with the handler response or a 202 Accepted response. This can be modified with the timeout option.

// provide the timeout in milliseconds (wait 30 seconds)
app.get('/path', delay({ timeout: 30000 }), potentiallySlowHandler);

status

Use status middleware to query operation status and get cached responses.

If the operation is still processing then it will return a 202 Accepted response; otherwise, it will return the cached response from the initial operation.

const { status } = require('express-delayed-response').init();

app.get('/status/:id', status());

status.resolveID

By default, status uses the id path parameter to resolve the id of the status object to query against. To override this behavior provide a resolveID method to the options.

app.get('/status', status({
    resolveID: req => req.headers['X-STATUS-ID']
}));

Redis Support

By default an in-memory LRU cache with 5000 entries is used to store the responses. If needed, a Redis client can be used as the cache client in place of the LRU cache.

const redis = require('redis');
const { delay, status } = require('express-delayed-response').init({ cacheClient: redis.createClient() });

The values are stored in a hash with the key express-delayed-response to customize this key supply the cacheKey option to init

const { delay, status } = require('express-delayed-response').init({ 
    cacheClient: redis.createClient(), 
    cacheKey: 'delay-key',
});

Set LRU options

If working in a limited memory environment, it may be necessary to control the LRU cache. Use createCacheClient to supply options to the lru-cache instance used.

const delayedResponse = require('express-delayed-response');

const { delay, status } = delayedResponse.init({ 
    cacheClient: delayedResponse.createCacheClient({ 
        max: 500,
    }),
});

The cache client uses lru-cache and the options are passed directly to the LRU instace created.

res.progress()

While the client is waiting for the server to resolve it is possible to provide status indications to the client via the status end point.

delay adds the progress method to response that accepts a string or json argument that is added to the status response progress field.

app.use(delay(), (req, res, next) => {
    const items = req.body.items;
    const state = { completed: 0, of: items.length };
    res.progress(state)
    const requests = items.map((item) => (
        processItem(item).then((result) => {
            state.completed++;
            res.progress(state);
            return result;
        })
    ));
    Promise.all(requests).then((data) => {
        res.json({ data });
    });
});

Then on a status query:

{
    "id": "Rasdclajs-",
    "progress": {
        "completed": 10,
        "of": 200
    }
}