@fresh8/copacetic
Advanced tools
Comparing version 3.5.2 to 3.6.0
@@ -39,2 +39,11 @@ /// <reference types="node" /> | ||
/** | ||
* A full health report | ||
*/ | ||
healthReport: { | ||
isHealthy: boolean, | ||
name: string, | ||
dependencies: Array<copacetic.Health> | ||
} | ||
/** | ||
* Returns a registered dependency | ||
@@ -285,2 +294,7 @@ * Examples: | ||
/** | ||
* If in cluster mode and this instance has been attached to it, ask the master process for health information of the full cluster | ||
*/ | ||
checkCluster: function () :Promise<Copacetic.healthReport> | ||
/** | ||
* Waits for a single dependency to become healthy | ||
@@ -671,4 +685,13 @@ * Example: | ||
export function HealthStrategy(options: HealthStrategyOptions): Function | ||
export namespace cluster { | ||
interface AttachOptions { | ||
dependency?: { | ||
level: dependencyLevel | ||
} | ||
} | ||
export function attach (copacetic: Copacetic, attachOptions?: AttachOptions): Function | ||
} | ||
} | ||
export = Copacetic |
@@ -21,1 +21,3 @@ const path = require('path') | ||
module.exports.HealthStrategy = HealthFactoryProvider(injector) | ||
module.exports.cluster = require('./lib/cluster')(injector) |
@@ -47,2 +47,20 @@ const EventEmitter = require('events') | ||
/** | ||
* @return {HealthReport} A full report of health information and dependencies | ||
*/ | ||
get healthReport () { | ||
/** | ||
* The full health report including isHealthy and dependencies | ||
* @typedef {Object} HealthReport | ||
* @property {Boolean} isHealthy The result of {@link Copacetic#isHealthy} | ||
* @property {String} Name | ||
* @property {Array<DependencyHealth>} dependencies The result of {@link Copacetic#healthInfo} | ||
*/ | ||
return { | ||
name: this.name, | ||
isHealthy: this.isHealthy, | ||
dependencies: this.healthInfo | ||
} | ||
} | ||
/** | ||
* @param {Dependency|String} dependency | ||
@@ -49,0 +67,0 @@ * @return {Dependency} |
@@ -37,4 +37,7 @@ const isObject = require('lodash.isobject') | ||
precond.checkIsString(name, 'The dependency name must be a string') | ||
precond.checkIsString(url, 'The dependency url must be a string') | ||
if (url) { | ||
precond.checkIsString(url, 'The dependency url must be a string') | ||
} | ||
this.backoffStrategy = backoffStrategy | ||
@@ -54,2 +57,3 @@ this.healthStrategy = healthStrategy | ||
* Health information about a dependency | ||
* Strategies may alter this summary to provide additional information | ||
* @typedef {Object} Dependency~HealthInfo | ||
@@ -61,3 +65,3 @@ * @property {String} name | ||
*/ | ||
return { | ||
const summary = { | ||
name: this.name, | ||
@@ -68,2 +72,6 @@ healthy: this.healthy, | ||
} | ||
if (this.healthStrategy.improveSummary) { | ||
this.healthStrategy.improveSummary(summary, this.lastCheckResult) | ||
} | ||
return summary | ||
} | ||
@@ -109,3 +117,17 @@ | ||
}) | ||
.then(() => { | ||
.catch((e) => { | ||
this.onUnhealthy() | ||
this.lastCheckResult = undefined | ||
return Promise.reject(this.healthSummary) | ||
}) | ||
.then((result) => { | ||
this.lastCheckResult = result | ||
if (this.healthStrategy.areYouOk) { | ||
if (!this.healthStrategy.areYouOk(result)) { | ||
this.onUnhealthy() | ||
return Promise.reject(this.healthSummary) | ||
} | ||
} | ||
this.onHealthy() | ||
@@ -115,7 +137,2 @@ | ||
}) | ||
.catch(() => { | ||
this.onUnhealthy() | ||
return Promise.reject(this.healthSummary) | ||
}) | ||
} | ||
@@ -122,0 +139,0 @@ } |
@@ -5,2 +5,3 @@ const HttpStrategyFactory = require('../http') | ||
const PostgresStrategyFactory = require('../postgres') | ||
const CopaceticStrategyFactory = require('../copacetic') | ||
@@ -18,2 +19,3 @@ /** | ||
const PostgresStrategy = PostgresStrategyFactory(injector) | ||
const CopaceticStrategy = CopaceticStrategyFactory(injector) | ||
@@ -31,2 +33,4 @@ /** | ||
case 'postgres': return PostgresStrategy(adapter, opts) | ||
case 'copacetic': return CopaceticStrategy(adapter, opts) | ||
case 'mockedStrategy': return injector.provideOne('mockedStrategy', true) | ||
default: return null | ||
@@ -33,0 +37,0 @@ } |
@@ -36,4 +36,11 @@ /** | ||
provideOne (moduleName, optional = false) { | ||
return this.require(moduleName, { optional }) | ||
provideOne (moduleName, optional = false, allowSystem = false) { | ||
try { | ||
return this.require(moduleName, { optional }) | ||
} catch (e) { | ||
if (e.message.indexOf('not found') > -1 && allowSystem) { | ||
return require(moduleName) | ||
} | ||
throw e | ||
} | ||
} | ||
@@ -40,0 +47,0 @@ } |
{ | ||
"name": "@fresh8/copacetic", | ||
"version": "3.5.2", | ||
"version": "3.6.0", | ||
"description": "A package to help your service check the health of its dependencies.", | ||
"main": "index.js", | ||
"scripts": { | ||
"coverage": "nyc npm test && nyc report --reporter=lcov", | ||
"coverage": "nyc npm test && nyc report", | ||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"doc": "jsdoc2md --template README.hbs lib/copacetic.js > README.md", | ||
"test": "mocha --recursive ./test", | ||
@@ -41,3 +42,8 @@ "lint": "standard \"test/**/*.js\" \"lib/**/*.js\" --fix" | ||
] | ||
} | ||
}, | ||
"reporter": [ | ||
"html", | ||
"json" | ||
], | ||
"report-dir": "./coverage" | ||
}, | ||
@@ -57,4 +63,6 @@ "repository": { | ||
"chai": "^3.5.0", | ||
"cluster-messages": "1.1.2", | ||
"coveralls": "^2.13.1", | ||
"ioredis": "3.x", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"mocha": "^3.4.2", | ||
@@ -68,3 +76,3 @@ "mongodb": "2.x", | ||
"sinon": "^2.3.6", | ||
"standard": "^10.0.2" | ||
"standard": "^10.0.3" | ||
}, | ||
@@ -81,2 +89,3 @@ "dependencies": { | ||
"optionalPeerDependencies": { | ||
"cluster-messages": "1.1.2", | ||
"node-fetch": "1.x", | ||
@@ -83,0 +92,0 @@ "mongodb": "2.x", |
@@ -101,2 +101,43 @@ # Copacetic | ||
``` | ||
#### Quick Start - cluster mode | ||
```js | ||
const Copacetic = require('@fresh8/copacetic') | ||
const level = require('@fresh8/copacetic').dependencyLevel | ||
const copacetic = Copacetic("A name", false) | ||
Copacetic.cluster.attach(copacetic) | ||
if (process.worker) { | ||
//register your usual dependencies like databases | ||
//use the line below to have the worker ask the master process a full health report | ||
copacetic.checkCluster() //`checkCluster` is only defined if the process is a worker and if you called `attach()` | ||
.then(console.log) | ||
} else { | ||
copacetic.checkAll() | ||
.then(() => { | ||
console.log(copacetic.healthReport) //Contains health information as reported by the workers | ||
}) | ||
} | ||
``` | ||
## Classes | ||
<dl> | ||
<dt><a href="#Copacetic">Copacetic</a> ⇐ <code>EventEmitter</code></dt> | ||
<dd></dd> | ||
</dl> | ||
## Typedefs | ||
<dl> | ||
<dt><a href="#HealthReport">HealthReport</a> : <code>Object</code></dt> | ||
<dd><p>The full health report including isHealthy and dependencies</p> | ||
</dd> | ||
</dl> | ||
<a name="Copacetic"></a> | ||
@@ -112,2 +153,3 @@ | ||
* [.healthInfo](#Copacetic+healthInfo) ⇒ <code>Array.<DependencyHealth></code> | ||
* [.healthReport](#Copacetic+healthReport) ⇒ [<code>HealthReport</code>](#HealthReport) | ||
* [.getDependency(dependency)](#Copacetic+getDependency) ⇒ <code>Dependency</code> | ||
@@ -123,2 +165,4 @@ * [.isDependencyRegistered(dependency)](#Copacetic+isDependencyRegistered) ⇒ <code>Boolean</code> | ||
* [.waitFor(opts)](#Copacetic+waitFor) | ||
* [._checkOne(name, maxDelay)](#Copacetic+_checkOne) ⇒ <code>Promise</code> | ||
* [._checkMany(dependencies, parallel)](#Copacetic+_checkMany) ⇒ <code>Promise</code> | ||
* ["healthy"](#Copacetic+event_healthy) | ||
@@ -146,2 +190,7 @@ * ["unhealthy"](#Copacetic+event_unhealthy) | ||
**Returns**: <code>Array.<DependencyHealth></code> - Health information on all dependencies | ||
<a name="Copacetic+healthReport"></a> | ||
### copacetic.healthReport ⇒ [<code>HealthReport</code>](#HealthReport) | ||
**Kind**: instance property of [<code>Copacetic</code>](#Copacetic) | ||
**Returns**: [<code>HealthReport</code>](#HealthReport) - A full report of health information and dependencies | ||
<a name="Copacetic+getDependency"></a> | ||
@@ -154,3 +203,3 @@ | ||
| --- | --- | | ||
| dependency | <code>Dependency</code> \| <code>String</code> | | ||
| dependency | <code>Dependency</code> \| <code>String</code> | | ||
@@ -165,3 +214,3 @@ <a name="Copacetic+isDependencyRegistered"></a> | ||
| --- | --- | | ||
| dependency | <code>Dependency</code> \| <code>String</code> | | ||
| dependency | <code>Dependency</code> \| <code>String</code> | | ||
@@ -199,5 +248,5 @@ <a name="Copacetic+registerDependency"></a> | ||
| --- | --- | --- | | ||
| [interval] | <code>String</code> | <code>'5 seconds'</code> | | ||
| [parallel] | <code>Boolean</code> | <code>true</code> | | ||
| [schedule] | <code>String</code> | <code>'start'</code> | | ||
| [interval] | <code>String</code> | <code>'5 seconds'</code> | | ||
| [parallel] | <code>Boolean</code> | <code>true</code> | | ||
| [schedule] | <code>String</code> | <code>'start'</code> | | ||
@@ -327,2 +376,24 @@ <a name="Copacetic+poll"></a> | ||
``` | ||
<a name="Copacetic+_checkOne"></a> | ||
### copacetic._checkOne(name, maxDelay) ⇒ <code>Promise</code> | ||
**Kind**: instance method of [<code>Copacetic</code>](#Copacetic) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | The name used to identify a dependency | | ||
| maxDelay | <code>Integer</code> | The maximum interval of time to wait when retrying | | ||
<a name="Copacetic+_checkMany"></a> | ||
### copacetic._checkMany(dependencies, parallel) ⇒ <code>Promise</code> | ||
Checks an array of dependencies in parallel or sequantially | ||
**Kind**: instance method of [<code>Copacetic</code>](#Copacetic) | ||
| Param | Type | | ||
| --- | --- | | ||
| dependencies | <code>Array.<Dependency></code> | | ||
| parallel | <code>Boolean</code> | | ||
<a name="Copacetic+event_healthy"></a> | ||
@@ -346,1 +417,15 @@ | ||
**Kind**: event emitted by [<code>Copacetic</code>](#Copacetic) | ||
<a name="HealthReport"></a> | ||
## HealthReport : <code>Object</code> | ||
The full health report including isHealthy and dependencies | ||
**Kind**: global typedef | ||
**Properties** | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| isHealthy | <code>Boolean</code> | The result of [isHealthy](#Copacetic+isHealthy) | | ||
| Name | <code>String</code> | | | ||
| dependencies | <code>Array.<DependencyHealth></code> | The result of [healthInfo](#Copacetic+healthInfo) | | ||
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
353141
91
10158
423
15
3
3