node-webhooks
Advanced tools
Comparing version 1.1.2 to 1.1.3
35
index.js
@@ -23,4 +23,3 @@ /* | ||
var request = require('request'); | ||
var events = require('events'); | ||
var eventEmitter = new events.EventEmitter(); | ||
var events = require('eventemitter2'); | ||
@@ -37,2 +36,4 @@ // will contain all the functions. We need to store them to be able to remove the listener callbacks | ||
this.emitter = new events.EventEmitter2({ wildcard: true }); | ||
var self = this; | ||
@@ -77,3 +78,3 @@ // sync loading: | ||
_functions[enc_url] = _getRequestFunction(self, url); | ||
eventEmitter.on(key, _functions[enc_url]); | ||
self.emitter.on(key, _functions[enc_url]); | ||
}); | ||
@@ -93,3 +94,3 @@ } | ||
// return the function then called by the event listener. | ||
var func = function(json_data, headers_data){ // argument required when eventEmitter.emit() | ||
var func = function(shortname, json_data, headers_data){ // argument required when eventEmitter.emit() | ||
var obj = {'Content-Type': 'application/json'}; | ||
@@ -108,4 +109,12 @@ var headers = headers_data ? _.merge(obj, headers_data) : obj; | ||
function (error, response, body) { | ||
if ((error || response.statusCode !== 200 )) return debug('HTTP failed: '+ error); | ||
debug('Request sent - Server responded with:', body); | ||
var statusCode = response ? response.statusCode : null; | ||
body = body ? body : null; | ||
debug('Request sent - Server responded with:', statusCode, body); | ||
if ((error || statusCode !== 200 )) { | ||
self.emitter.emit(shortname + '.failure', shortname, statusCode, body); | ||
return debug('HTTP failed: '+ error); | ||
} | ||
self.emitter.emit(shortname + '.success', shortname, statusCode, body); | ||
} | ||
@@ -123,3 +132,3 @@ ); | ||
// trigger a webHook | ||
eventEmitter.emit(shortname, json_data, headers_data); | ||
this.emitter.emit(shortname, shortname, json_data, headers_data); | ||
}; | ||
@@ -149,3 +158,3 @@ | ||
_functions[enc_url] = _getRequestFunction(self, url); | ||
eventEmitter.on(shortname, _functions[enc_url]); | ||
self.emitter.on(shortname, _functions[enc_url]); | ||
modified = true; | ||
@@ -159,3 +168,3 @@ } | ||
_functions[enc_url] = _getRequestFunction(self, url); | ||
eventEmitter.on(shortname, _functions[enc_url]); | ||
self.emitter.on(shortname, _functions[enc_url]); | ||
modified = true; | ||
@@ -195,3 +204,3 @@ } | ||
var url_key = crypto.createHash('md5').update(url).digest('hex'); | ||
eventEmitter.removeListener(shortname, _functions[url_key]); | ||
self.emitter.removeListener(shortname, _functions[url_key]); | ||
delete _functions[url_key]; | ||
@@ -205,3 +214,3 @@ resolve(true); | ||
// remove every event listener attached to the webHook shortname. | ||
eventEmitter.removeAllListeners(shortname); | ||
self.emitter.removeAllListeners(shortname); | ||
@@ -323,2 +332,6 @@ // delete all the callbacks in _functions for the specified shortname. Let's loop over the url taken from the DB. | ||
WebHooks.prototype.getEmitter = function(){ | ||
return this.emitter; | ||
}; | ||
module.exports = WebHooks; |
{ | ||
"name": "node-webhooks", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Create and trigger your own webHooks", | ||
@@ -28,2 +28,3 @@ "main": "index.js", | ||
"debug": "^2.2.0", | ||
"eventemitter2": "^2.1.3", | ||
"jsonfile": "^2.2.3", | ||
@@ -30,0 +31,0 @@ "lodash": "^4.15.0", |
@@ -21,3 +21,3 @@ # node-webhooks [![Build Status](https://travis-ci.org/roccomuso/node-webhooks.svg?branch=master)](https://travis-ci.org/roccomuso/node-webhooks) [![NPM Version](https://img.shields.io/npm/v/node-webhooks.svg)](https://www.npmjs.com/package/node-webhooks) | ||
This module makes use of the popular [debug](https://github.com/visionmedia/debug) package.Use the env variable to enable debug: <code>DEBUG=node-webhooks</code>. | ||
This module makes use of the popular [debug](https://github.com/visionmedia/debug) package. Use the env variable to enable debug: <code>DEBUG=node-webhooks</code>. | ||
To launch the example and enable debug: <code>DEBUG=node-webhooks node example.js</code> | ||
@@ -30,3 +30,3 @@ | ||
// Initialize WebHooks module. | ||
var WebHooks = require('./index'); | ||
var WebHooks = require('node-webhooks'); | ||
@@ -64,2 +64,34 @@ | ||
## Available events | ||
We're using an event emitter library to expose request information on webHook trigger. | ||
```javascript | ||
var webHooks = new WebHooks({ | ||
db: WEBHOOKS_DB, | ||
DEBUG: true | ||
}); | ||
var emitter = webHooks.getEmitter(); | ||
emitter.on('*.success', function (shortname, statusCode, body) { | ||
console.log('Success on trigger webHook' + shortname + 'with status code', statusCode, 'and body', body); | ||
}); | ||
emitter.on('*.failure', function (shortname, statusCode, body) { | ||
console.error('Error on trigger webHook' + shortname + 'with status code', statusCode, 'and body', body); | ||
}); | ||
``` | ||
This makes possible checking if a webHook trigger was successful or not getting request information such as status code or response body. | ||
The format for the events is built as `eventName.result`. The choosen library `eventemitter2` provides a lot of freedom for listening events. For example: | ||
- `eventName.success` | ||
- `eventName.failure` | ||
- `eventName.*` | ||
- `*.success` | ||
- `*.*` | ||
## API examples | ||
@@ -66,0 +98,0 @@ |
@@ -10,2 +10,3 @@ var chai = require('chai'); | ||
var webHooks; | ||
var emitter; | ||
var DB_FILE = path.join(__dirname, './webHooksDB.json'); // json file that store webhook URLs | ||
@@ -55,3 +56,7 @@ | ||
debug('body:', body); | ||
response.end('It Works!! Path Hit: ' + request.url); | ||
if (request.url.indexOf('/fail') !== -1) | ||
response.writeHead(400, {'Content-Type': 'application/json'}); | ||
else | ||
response.writeHead(200, {'Content-Type': 'application/json'}); | ||
response.end('Path Hit: ' + request.url); | ||
@@ -75,8 +80,2 @@ }); | ||
}); | ||
after(function(done) { | ||
// stop the server | ||
server.close(function() { | ||
done(); | ||
}); | ||
}); | ||
@@ -287,5 +286,5 @@ it('eventually delete old DB', function(done) { | ||
it('should fire the webHook 1000 times and 2000 REST calls are expected', function(done) { | ||
this.timeout(20 * 1000); | ||
this.timeout(25 * 1000); | ||
// disabling debug to avoid console flooding | ||
debug = function() {}; | ||
//debug = function() {}; | ||
@@ -299,5 +298,8 @@ for (var i = 1; i <= 1000; i++) | ||
setInterval(function() { | ||
var loop = setInterval(function() { | ||
console.log('Got', LOADTEST + '/2000', 'REST calls'); | ||
if (LOADTEST === 2000) done(); | ||
if (LOADTEST === 2000){ | ||
clearInterval(loop); | ||
done(); | ||
} | ||
}, 500); | ||
@@ -309,1 +311,161 @@ | ||
}); | ||
describe('Events >', function(){ | ||
it('Should get the emitter', function(done){ | ||
emitter = webHooks.getEmitter(); // get the emitter | ||
should.exist(emitter); | ||
done(); | ||
}); | ||
it('Should add a new Hook #3', function(done){ | ||
webHooks.add('hook3', URI + '/3/aaa').then(function() { | ||
done(); | ||
}).catch(function(err) { | ||
throw new Error(err); | ||
}); | ||
}); | ||
it('Should catch a specific success event', function(done){ | ||
emitter.on('hook3.failure', function(shortname, stCode, body){ | ||
debug('hook3.failure:', shortname, stCode, body); | ||
done('hook3.failure error: wrong event catched.'); | ||
}); | ||
emitter.on('hook3.success', function(shortname, statusCode, body){ | ||
debug('hook3.success:', {shortname: shortname, statusCode: statusCode, body: body}); | ||
should.exist(shortname); | ||
should.exist(statusCode); | ||
should.exist(body); | ||
shortname.should.equal('hook3'); | ||
statusCode.should.equal(200); | ||
body.should.equal('Path Hit: /3/aaa'); // body response from the server | ||
done(); | ||
}); | ||
// fire the hook | ||
webHooks.trigger('hook3', { | ||
header1: 'pippo' | ||
}, { | ||
prop1 : 'paperino' | ||
}); | ||
}); | ||
it('Should remove the specific event listener and fire the hook', function(done){ | ||
this.timeout(4000); | ||
emitter.removeAllListeners('hook3'); | ||
emitter.on('hook3.success', function(s, st, body){ | ||
debug('hook3.success error:', s, st, body); | ||
done('error: removed listener should not be called!'); | ||
}); | ||
emitter.removeAllListeners('hook3'); | ||
webHooks.trigger('hook3'); | ||
setTimeout(function(){ | ||
done(); | ||
}, 2000); | ||
}); | ||
it('add a failing webHook called hook4', function(done){ | ||
webHooks.add('hook4', URI + '/4/fail').then(function() { | ||
done(); | ||
}).catch(function(err) { | ||
throw new Error(err); | ||
}); | ||
}); | ||
it('Should catch a specific failure event', function(done){ | ||
emitter.on('hook4.success', function(){ | ||
done('error: wrong event catched!'); | ||
}); | ||
emitter.on('hook4.failure', function(shortname, statusCode, body){ | ||
should.exist(shortname); | ||
should.exist(statusCode); | ||
should.exist(body); | ||
shortname.should.equal('hook4'); | ||
statusCode.should.equal(400); | ||
body.should.equal('Path Hit: /4/fail'); | ||
done(); | ||
}); | ||
// fire the hook | ||
webHooks.trigger('hook4', { | ||
header1: 'foo' | ||
}, { | ||
prop2 : 'peterpan' | ||
}); | ||
}); | ||
it('Should add new hooks for multiple events catch', function(done){ | ||
webHooks.add('hook5', URI + '/5/success').then(function() { | ||
webHooks.add('hook6', URI + '/6/success').then(function() { | ||
webHooks.add('hook7', URI + '/7/fail').then(function() { | ||
webHooks.add('hook8', URI + '/8/fail').then(function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}).catch(function(err) { | ||
throw new Error(err); | ||
}); | ||
}); | ||
it('Should catch all the success events', function(done){ | ||
var got = 0; | ||
emitter.on('*.failure', function(shortname, stCode, body){ | ||
debug('error *.failure:', shortname, stCode, body); | ||
done('*.failure error: wrong event catched.'); | ||
}); | ||
emitter.on('*.success', function(shortname, statusCode, body){ | ||
debug('captured events:', got); | ||
should.exist(shortname); | ||
should.exist(statusCode); | ||
should.exist(body); | ||
expect(shortname).to.be.oneOf(['hook5','hook6']); | ||
statusCode.should.equal(200); | ||
expect(body).to.be.oneOf(['Path Hit: /5/success', 'Path Hit: /6/success']); | ||
++got; | ||
if (got === 2){ | ||
emitter.removeAllListeners('*.success'); | ||
emitter.removeAllListeners('*.failure'); | ||
done(); | ||
} | ||
}); | ||
// fire the hooks | ||
webHooks.trigger('hook5'); | ||
webHooks.trigger('hook6'); | ||
}); | ||
it('Should catch all the failure events', function(done){ | ||
var got = 0; | ||
emitter.on('*.success', function(shortname, stCode, body){ | ||
debug('error *.success:', shortname, stCode, body); | ||
done('*.success error: wrong event catched.'); | ||
}); | ||
emitter.on('*.failure', function(shortname, statusCode, body){ | ||
debug('captured events:', got); | ||
should.exist(shortname); | ||
should.exist(statusCode); | ||
should.exist(body); | ||
expect(shortname).to.be.oneOf(['hook7','hook8']); | ||
statusCode.should.equal(400); | ||
expect(body).to.be.oneOf(['Path Hit: /7/fail', 'Path Hit: /8/fail']); | ||
++got; | ||
if (got === 2){ | ||
emitter.removeAllListeners('*.success'); | ||
emitter.removeAllListeners('*.failure'); | ||
done(); | ||
} | ||
}); | ||
// fire the hooks | ||
webHooks.trigger('hook7'); | ||
webHooks.trigger('hook8'); | ||
}); | ||
after(function(done) { | ||
// stop the server | ||
server.close(function() { | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31471
704
123
6
+ Addedeventemitter2@^2.1.3
+ Addedeventemitter2@2.2.2(transitive)