Socket
Socket
Sign inDemoInstall

knifecycle

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

knifecycle - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

5

CHANGELOG.md

@@ -0,1 +1,6 @@

<a name="1.3.1"></a>
## [1.3.1](https://github.com/nfroidure/knifecycle/compare/v1.3.0...v1.3.1) (2017-03-14)
<a name="1.3.0"></a>

@@ -2,0 +7,0 @@ # [1.3.0](https://github.com/nfroidure/knifecycle/compare/v1.2.0...v1.3.0) (2017-03-08)

4

package.json
{
"name": "knifecycle",
"version": "1.3.0",
"version": "1.3.1",
"description": "Manage your NodeJS processes's lifecycle.",

@@ -34,3 +34,3 @@ "main": "dist/index.js",

"metapak": "metapak || echo 'Please `npm install --save-dev metapak`' && exit 0",
"postinstall": "npm run metapak; npm run metapak --silent",
"postinstall": "npm run metapak --silent",
"prepublish": "npm run compile",

@@ -37,0 +37,0 @@ "preversion": "npm t && npm run lint && npm run compile",

@@ -35,17 +35,20 @@ <!--

- services management: start services taking their dependencies in count and
shut them down the same way for graceful exits.
shut them down the same way for graceful exits (namely dependency injection
with inverted control);
- easy end to end testing: just replace your services per your own mocks and
stubs while ensuring your application integrity between testing and production.
- isolation: isolate processing in a clean manner, per concerns.
stubs while ensuring your application integrity between testing and production;
- isolation: isolate processing in a clean manner, per concerns;
- functional programming ready: encapsulate global states allowing the rest of
your application to be purely functional.
your application to be purely functional;
- no circular dependencies for services: while circular dependencies are not a
problem within purely functional libraries (require allows it), it may be
harmful for your services, knifecycle impeach that while providing an `$inject`
service à la Angular to allow accessing existing services references.
harmful for your services, `knifecycle` impeach that while providing an `$inject`
service à la Angular to allow accessing existing services references if you
really need to;
- generate Mermaid graphs of the dependency tree.
## Usage
Using Knifecycle is all about declaring the services our application need. Some
of them are simple constants:
Using Knifecycle is all about declaring the services our application needs.
Some of them are simple constants:
```js

@@ -108,6 +111,15 @@ // services/core.js

// services/db.js
import { depends, provider } from 'knifecycle/instance';
import { depends, provider, constant } from 'knifecycle/instance';
import MongoClient from 'mongodb';
constant('DB_CONFIG', { uri: 'mongo:xxxxx' });
// Register a service with the provider method.
provider('db',
// Declare the service dependencies with the depends decorator
depends(['DB_CONFIG', 'logger'],
dbProvider
)
);
// A service provider returns a service descriptor promise exposing:

@@ -117,22 +129,30 @@ // - a mandatory service property containing the actual service

// - an optional error promise to handle the service failure
provider('db',
// Declare the service dependencies with the depends decorator
depends(['ENV', 'logger'],
function dbProvider({ ENV, logger }) {
return MongoClient.connect(ENV.DB_URI)
.then(function(db) {
let fatalErrorPromise = new Promise((resolve, reject) {
db.once('error', reject);
});
function dbProvider({ DB_CONFIG, logger }) {
return MongoClient.connect(DB_CONFIG.uri)
.then(function(db) {
let fatalErrorPromise = new Promise((resolve, reject) {
db.once('error', reject);
});
logger.log('info', 'db service initialized!');
logger.log('info', 'db service initialized!');
return {
servicePromise: db,
shutdownProvider: db.close.bind(db, true),
errorPromise: fatalErrorPromise,
};
});
})
return {
servicePromise: db,
shutdownProvider: db.close.bind(db, true),
errorPromise: fatalErrorPromise,
};
});
}
// What if we need 2 mongodb clients?
// Just use service mapping!
constant('DB_CONFIG2', { uri: 'mongo:xxxxx' });
provider('db2',
// You can wire a dependency with an different name
// than the one expected by your service provider with
// the mapping feature
depends(['DB_CONFIG2:DB_CONFIG', 'logger'],
dbProvider
);
```

@@ -280,2 +300,6 @@

</dd>
<dt><a href="#toMermaidGraph">toMermaidGraph(options)</a> ⇒ <code>String</code></dt>
<dd><p>Outputs a Mermaid compatible dependency graph of the declared services.
See <a href="https://github.com/knsv/mermaid">Mermaid docs</a></p>
</dd>
<dt><a href="#run">run(dependenciesDeclarations)</a> ⇒ <code>Promise</code></dt>

@@ -448,2 +472,34 @@ <dd><p>Creates a new execution silo</p>

```
<a name="toMermaidGraph"></a>
## toMermaidGraph(options) ⇒ <code>String</code>
Outputs a Mermaid compatible dependency graph of the declared services.
See [Mermaid docs](https://github.com/knsv/mermaid)
**Kind**: global function
**Returns**: <code>String</code> - Returns a string containing the Mermaid dependency graph
| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | Options for generating the graph (destructured) |
| options.shapes | <code>Array.&lt;Object&gt;</code> | Various shapes to apply |
| options.styles | <code>Array.&lt;Object&gt;</code> | Various styles to apply |
| options.classes | <code>Object</code> | A hash of various classes contents |
**Example**
```js
import Knifecycle from 'knifecycle'
const $ = new Knifecycle();
$.constant('ENV', process.env);
$.constant('OS', require('os'));
$.service('app', $.depends(['ENV', 'OS'], () => Promise.resolve()));
$.toMermaidGraph();
// returns
graph TD
app-->ENV
app-->OS
```
<a name="run"></a>

@@ -455,3 +511,3 @@

**Kind**: global function
**Returns**: <code>Promise</code> - Service descriptor promise Returns the decorator function
**Returns**: <code>Promise</code> - Service descriptor promise

@@ -458,0 +514,0 @@ | Param | Type | Description |

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc