Comparing version 0.0.0 to 1.0.0
50
index.js
@@ -0,5 +1,7 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var Promise = require('bluebird'); | ||
config = { | ||
var config = { | ||
expect: 'promise', | ||
@@ -12,3 +14,3 @@ path: '', | ||
function Warm() { | ||
var cfg = this.cfg = config; | ||
var cfg = this.cfg = _.clone(config); | ||
this.payloads = _.chain(arguments) | ||
@@ -20,2 +22,5 @@ .values() | ||
_.extend(cfg, item); | ||
if(item.path && item.path[item.path.length-1] !== '/') { | ||
cfg.path += '/'; | ||
} | ||
return false; | ||
@@ -27,2 +32,3 @@ } | ||
.value(); | ||
} | ||
@@ -33,37 +39,33 @@ | ||
var cfg = this.cfg, path = cfg.path, | ||
_module = null, _warmPayload = null; | ||
var cfg = this.cfg, _module = null; | ||
if(path && path[path.length-1] !== '/') { | ||
path += '/'; | ||
function wrap(err) { | ||
return function () { | ||
return err ? Promise.reject(err) : Promise.resolve(); | ||
}; | ||
} | ||
if(typeof item === 'string') { | ||
_module = require(path + item); | ||
_module = require(this.cfg.path + item); | ||
} else { | ||
_module = item; | ||
} | ||
warmPayload = _module && _module[cfg.init]; | ||
var warmPayload = _module && _module[cfg.init]; | ||
if (warmPayload instanceof Function) { | ||
if(cfg.expect === 'promise') { | ||
return warmPayload; | ||
} else { | ||
return function() { | ||
return new Promise(function(resolve, reject) { | ||
warmPayload(function (err, res) { | ||
return err ? reject(err) : resolve(res); | ||
}); | ||
}); | ||
}; | ||
} | ||
} else if(warmPayload instanceof Promise || warmPayload) { | ||
throw new Error('.'+cfg.init+'() should be function (with callback or promise)'); | ||
} else { | ||
if(cfg.strict) { | ||
throw new Error('.'+cfg.init+'() not found in one of modules'); | ||
} | ||
return function() { | ||
Promise.resolve(); | ||
return new Promise(function(resolve, reject) { | ||
warmPayload(function (err, res) { | ||
return err ? reject(err) : resolve(res); | ||
}); | ||
}); | ||
}; | ||
} | ||
if(cfg.strict) { | ||
return wrap(new Error('.'+cfg.init+'() should be function (containing callback or promise)')); | ||
} | ||
return wrap(); | ||
}; | ||
@@ -82,3 +84,3 @@ | ||
} else { | ||
Promise.reject(err); | ||
throw err; | ||
} | ||
@@ -85,0 +87,0 @@ }); |
{ | ||
"name": "warm", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "warm up node modules before using", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"coverage": "istanbul cover _mocha --", | ||
"pretest": "jshint index.js", | ||
"test": "istanbul cover _mocha --" | ||
}, | ||
@@ -27,6 +29,15 @@ "repository": { | ||
"dependencies": { | ||
"async": "^1.4.2", | ||
"bluebird": "^2.10.2", | ||
"lodash": "^3.10.1" | ||
} | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.3.0", | ||
"istanbul": "^0.3.22", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.3.3", | ||
"pre-git": "^0.6.2" | ||
}, | ||
"pre-push": [ | ||
"npm test" | ||
] | ||
} |
# warm | ||
Warm up node modules before using | ||
[](https://travis-ci.org/vkfont/warm) | ||
[](https://www.npmjs.org/package/warm) | ||
[](https://coveralls.io/github/vkfont/warm?branch=master) | ||
This node.js module is useful if you want to load other modules and be sure all warming code is executed before you continue. | ||
### Quick usage | ||
Install module into own project | ||
``` | ||
# npm install --save warm | ||
``` | ||
Create new class and call `.parallel` method | ||
```javascript | ||
Warm = requre('warm'); | ||
new Warm('./database', './redis').parallel(function (err) { | ||
if(err) { throw err; } | ||
console.log('Initial code was executed in all described modules'); | ||
}); | ||
``` | ||
Modules `database` and `redis` should return special method (`.init` by default), which resolve promise or call callback on finish. See *Modules* | ||
### Options | ||
You can pass additional options like plain object: `new Warm({ ... }, 'module1', ...)`: | ||
| Name | Default | Description | | ||
| ------------- |:----------:| -----| | ||
| `.expect` | `promise` | will init method inform about finish with promise (reject/resolve it), or will fire callback with error (`expect: 'callback'`) | ||
| `.path` | `''` | prefix for requiring modules | ||
| `.strict` | `true` | should return error if init method not found in module | ||
| `.init` | `init` | init method name | ||
### Samples | ||
```javascript | ||
Warm = requre('warm'); | ||
// load modules in array or additional parameters with options | ||
options1 = { | ||
expect: 'callback' | ||
} | ||
options2 = { | ||
strict: true | ||
} | ||
new Warm(options1, ['./module1', './module2'], './module3', options2).parallel(console.log); | ||
// execute already loaded module | ||
new Warm(require('./module3')).parallel(console.log); | ||
// handle promise | ||
new Warm('./module1').parallel().then(function () { | ||
console.log('loaded'); | ||
}).catch(function (err) { | ||
console.log('error:', err); | ||
}) | ||
``` | ||
### Modules | ||
Module should contain method `.init` (or other, described in options) which will return promise or file callback. Example: | ||
```javascript | ||
module.exports = function () { | ||
return 'i will not fire'; | ||
}; | ||
module.exports.init = function (callback) { | ||
setTimeout(function () { | ||
console.log('>>> i will fire durning warming'); | ||
callback(); | ||
}, 100); | ||
}; | ||
``` | ||
Additional samples can be found in [test folder](./tetst/fixtures/) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20858
2
15
188
1
74
5
1
- Removedasync@^1.4.2
- Removedasync@1.5.2(transitive)