express-delayed-response
automatically provides polling responses for long running operations
Last updated 2 years ago by corycook .
SEE LICENSE IN LICENSE · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install express-delayed-response 
SYNC missed versions from official npm registry.

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
    }
}

Current Tags

  • 0.4.1                                ...           latest (2 years ago)

8 Versions

  • 0.4.1                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (11)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |