Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

wellness-download

1.0.1 • Public • Published

wellness-download

A healthcheck for the wellness module, that can be used inpdependently.

The module downloads a file and checks it against an optionally supplied hash value.

Example

Stand-alone Use

'use strict';
var wellnessDownload = require('wellness-download');
 
var opts = {
    file_hash: '338ff9861dd251fd21c30f040a5b831d',
    hash_type: 'md5',
    req_opts: {
        url: 'https://close5-item-image-production.s3.amazonaws.com/images/'+
             '54dedc25f6aa03c55d0005fd_200_200.jpg'
    }
};
 
wellnessDownload.init(opts);
wellnessDownload.file_download(function(err) {
    if (err) {
        console.error('Exiting on error', err.message);
        return;
    }
    console.log('success');
});

In Clustered Server

'use strict';
var wellness = require('wellness');
var express = require('express');
var app = express();
var numCPUs = require('os').cpus().length;
var wellnessDownload = require('./index');
 
var opts = {
    healthCheckUriPath: '/healthcheck',
    expressApp: app,
    workerTimeOut: 5000,
    numWorkers: numCPUs
};
 
 
var dl_opts = {
    file_hash: '338ff9861dd251fd21c30f040a5b831c',
    hash_type: 'md5',
    req_opts: {
        url: 'https://close5-item-image-production.s3.amazonaws.com/'+
             'images/54dedc25f6aa03c55d0005fd_200_200.jpg'
    }
};
wellnessDownload.init(dl_opts);
 
function doSomethingUseful() { wellness.workerIsWorking(); }
var cluster = require('cluster');
 
if (cluster.isMaster) {
 
    for (var i = 0; i < numCPUs; i++)
        cluster.fork();
 
    wellness.clusterPostForkInit(opts);
} else {
    wellness.clusterPostForkInit(opts, function(err) {
        if (err) {
            console.error(err.message);
            return;
        }
 
        wellness.addCheck(wellnessDownload.file_download);
 
        app.listen(3000, function () {
            console.log('Example app listening on port 3000!');
            doSomethingUseful();
            setInterval(doSomethingUseful, 1000);
        });
    });
}

Test the health check

curl -v http://localhost:3000/healthcheck

API

init(opts)

Initialize the wellness-download module with an options object.

Options:

  • req_opts: Options for the request module. Any valid request option can go here. The uri or url is required.
  • logger: A winston-like logger object (for logging)
  • If a hash_type and file_hash are present, the code will compute the hash for the download and compare it to the file_hash using the hash algorithm determined by hash_type.
  • hash_type: Any valid hash type for the Node.js crypto Hash class: sha256, md5, sha1, etc. To see all hashes supported, run the following code:
var crypto = require('crypto')
console.log(crypto.getHashes())

file_download(cb)

Perform the file download and, optionally, compute the file hash. Return errors, if any, in the callback.

Install

npm i wellness-download

DownloadsWeekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • avatar