node-webhooks
Advanced tools
Comparing version 1.2.4 to 1.3.0
77
index.js
@@ -31,6 +31,11 @@ /* | ||
if (typeof options !== 'object') throw new TypeError('Expected an Object') | ||
if (typeof options.db !== 'string') throw new TypeError('db Must be a String path') | ||
if (typeof options.db !== 'string' && typeof options.db !== 'object') { | ||
throw new TypeError('db Must be a String path or an object') | ||
} | ||
this.db = options.db | ||
// If webhooks data is kept in memory, we skip all disk operations | ||
this.isMemDb = typeof options.db === 'object' | ||
if (options.hasOwnProperty('httpSuccessCodes')) { | ||
@@ -48,17 +53,23 @@ if (!(options.httpSuccessCodes instanceof Array)) throw new TypeError('httpSuccessCodes must be an array') | ||
var self = this | ||
// sync loading: | ||
try { | ||
fs.accessSync(this.db, fs.R_OK | fs.W_OK) | ||
// DB already exists, set listeners for every URL. | ||
debug('webHook DB loaded, setting listeners...') | ||
if (this.isMemDb) { | ||
debug('setting listeners based on provided configuration object...') | ||
_setListeners(self) | ||
} catch (e) { | ||
// DB file not found, initialize it | ||
if (e.hasOwnProperty('code')) { | ||
if (e.code === 'ENOENT') { | ||
// file not found, init DB: | ||
debug('webHook DB init') | ||
_initDB(self.db) | ||
} else { | ||
// sync loading: | ||
try { | ||
fs.accessSync(this.db, fs.R_OK | fs.W_OK) | ||
// DB already exists, set listeners for every URL. | ||
debug('webHook DB loaded, setting listeners...') | ||
_setListeners(self) | ||
} catch (e) { | ||
// DB file not found, initialize it | ||
if (e.hasOwnProperty('code')) { | ||
if (e.code === 'ENOENT') { | ||
// file not found, init DB: | ||
debug('webHook DB init') | ||
_initDB(self.db) | ||
} else console.error(e) | ||
} else console.error(e) | ||
} else console.error(e) | ||
} | ||
} | ||
@@ -77,3 +88,3 @@ } | ||
try { | ||
var obj = jsonfile.readFileSync(self.db) | ||
var obj = self.isMemDb ? self.db : jsonfile.readFileSync(self.db) | ||
if (!obj) throw Error('can\'t read webHook DB content') | ||
@@ -149,3 +160,3 @@ | ||
try { | ||
var obj = jsonfile.readFileSync(self.db) | ||
var obj = self.isMemDb ? self.db : jsonfile.readFileSync(self.db) | ||
if (!obj) throw Error('can\'t read webHook DB content') | ||
@@ -178,3 +189,3 @@ | ||
if (modified) { | ||
jsonfile.writeFileSync(self.db, obj) | ||
if (!self.isMemDb) jsonfile.writeFileSync(self.db, obj) | ||
resolve(true) | ||
@@ -215,3 +226,3 @@ } else resolve(false) | ||
// delete all the callbacks in _functions for the specified shortname. Let's loop over the url taken from the DB. | ||
var obj = jsonfile.readFileSync(self.db) | ||
var obj = self.isMemDb ? self.db : jsonfile.readFileSync(self.db) | ||
@@ -231,3 +242,3 @@ if (obj.hasOwnProperty(shortname)) { | ||
} else { | ||
debug('webHook doesn\'t exists') | ||
debug('webHook doesn\'t exist') | ||
resolve(false) | ||
@@ -244,3 +255,3 @@ } | ||
try { | ||
var obj = jsonfile.readFileSync(self.db) | ||
var obj = self.isMemDb ? self.db : jsonfile.readFileSync(self.db) | ||
@@ -255,3 +266,3 @@ var deleted = false | ||
if (deleted) { | ||
jsonfile.writeFileSync(self.db, obj) | ||
if (!self.isMemDb) jsonfile.writeFileSync(self.db, obj) | ||
debug('url removed from existing shortname') | ||
@@ -267,6 +278,6 @@ callback(null, deleted) | ||
try { | ||
var obj = jsonfile.readFileSync(self.db) | ||
var obj = self.isMemDb ? self.db : jsonfile.readFileSync(self.db) | ||
delete obj[shortname] | ||
// save it back to the DB | ||
jsonfile.writeFileSync(self.db, obj) | ||
if (!self.isMemDb) jsonfile.writeFileSync(self.db, obj) | ||
debug('whole shortname urls removed') | ||
@@ -284,2 +295,3 @@ callback(null) | ||
return new Promise(function (resolve, reject) { | ||
if (self.isMemDb) resolve(self.db) | ||
jsonfile.readFile(self.db, function (err, obj) { | ||
@@ -300,14 +312,13 @@ if (err) { | ||
return new Promise(function (resolve, reject) { | ||
jsonfile.readFile(self.db, function (err, obj) { | ||
if (err) { | ||
reject(err) // file not found | ||
} else { | ||
// file exists | ||
if (obj[shortname]) { | ||
resolve(obj[shortname]) | ||
if (self.isMemDb) { | ||
resolve(self.db[shortname] || {}) | ||
} else { | ||
jsonfile.readFile(self.db, function (err, obj) { | ||
if (err) { | ||
reject(err) // file not found | ||
} else { | ||
resolve({}) | ||
resolve(obj[shortname] || {}) // file exists | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
@@ -314,0 +325,0 @@ } |
{ | ||
"name": "node-webhooks", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "Create and trigger your own webHooks", | ||
@@ -31,3 +31,3 @@ "main": "index.js", | ||
"bluebird": "^3.5.0", | ||
"debug": "^2.6.8", | ||
"debug": "^3.1.0", | ||
"eventemitter2": "^4.1.0", | ||
@@ -34,0 +34,0 @@ "jsonfile": "^3.0.0", |
@@ -1,2 +0,2 @@ | ||
# 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) [![Coverage Status](https://coveralls.io/repos/github/roccomuso/node-webhooks/badge.svg?branch=master)](https://coveralls.io/github/roccomuso/node-webhooks?branch=master) [![bitHound Overall Score](https://www.bithound.io/github/roccomuso/node-webhooks/badges/score.svg)](https://www.bithound.io/github/roccomuso/node-webhooks) [![Dependency Status](https://david-dm.org/roccomuso/node-webhooks.png)](https://david-dm.org/roccomuso/node-webhooks) | ||
# 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) [![Coverage Status](https://coveralls.io/repos/github/roccomuso/node-webhooks/badge.svg?branch=master)](https://coveralls.io/github/roccomuso/node-webhooks?branch=master) [![bitHound Overall Score](https://www.bithound.io/github/roccomuso/node-webhooks/badges/score.svg)](https://www.bithound.io/github/roccomuso/node-webhooks) [![Dependency Status](https://david-dm.org/roccomuso/node-webhooks.png)](https://david-dm.org/roccomuso/node-webhooks) <span class="badge-patreon"><a href="https://patreon.com/roccomuso" title="Donate to this project using Patreon"><img src="https://img.shields.io/badge/patreon-donate-yellow.svg" alt="Patreon donate button" /></a></span> | ||
@@ -32,3 +32,3 @@ | ||
// Initialize webhooks module from on-disk database | ||
var webHooks = new WebHooks({ | ||
@@ -39,2 +39,8 @@ db: './webHooksDB.json', // json file that store webhook URLs | ||
// Alternatively, initialize webhooks module with object; changes will only be | ||
// made in-memory | ||
webHooks = new WebHooks({ | ||
db: {"addPost": ["http://localhost:9100/posts"]}, // just an example | ||
}) | ||
// sync instantation - add a new webhook called 'shortname1' | ||
@@ -41,0 +47,0 @@ webHooks.add('shortname1', 'http://127.0.0.1:9000/prova/other_url').then(function(){ |
Sorry, the diff of this file is not supported yet
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
131
1
0
18075
5
305
+ Addeddebug@3.2.7(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^3.1.0