@captain-hook/core
Advanced tools
Comparing version 0.2.14 to 0.2.15
@@ -7,3 +7,3 @@ 'use strict'; | ||
var _asyncIterator = require('@babel/runtime/helpers/asyncIterator'); | ||
var eventsEngine = require('@captain-hook/events-engine'); | ||
var events = require('events'); | ||
@@ -15,2 +15,133 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
let _Symbol$asyncIterator; | ||
_Symbol$asyncIterator = Symbol.asyncIterator; | ||
class WebhookIterator { | ||
constructor(engine, triggers) { | ||
_defineProperty__default['default'](this, "pullQueue", []); | ||
_defineProperty__default['default'](this, "pushQueue", []); | ||
_defineProperty__default['default'](this, "done", false); | ||
_defineProperty__default['default'](this, _Symbol$asyncIterator, () => this); | ||
_defineProperty__default['default'](this, "pushValue", async event => { | ||
await this.allSubscribed; | ||
if (this.pullQueue.length !== 0) { | ||
const element = this.pullQueue.shift(); | ||
if (element) { | ||
element({ | ||
value: event, | ||
done: false | ||
}); | ||
} | ||
} else { | ||
this.pushQueue.push(event); | ||
} | ||
}); | ||
_defineProperty__default['default'](this, "pullValue", () => new Promise(resolve => { | ||
if (this.pushQueue.length !== 0) { | ||
resolve({ | ||
done: false, | ||
value: this.pushQueue.shift() | ||
}); | ||
} else { | ||
this.pullQueue.push(resolve); | ||
} | ||
})); | ||
_defineProperty__default['default'](this, "emptyQueue", subscriptionIds => { | ||
if (this.listening) { | ||
this.listening = false; | ||
this.unsubscribeAll(subscriptionIds); | ||
this.pullQueue.forEach(resolve => resolve({ | ||
done: true, | ||
value: undefined | ||
})); | ||
this.pullQueue.length = 0; | ||
this.pushQueue.length = 0; | ||
} | ||
}); | ||
_defineProperty__default['default'](this, "subscribeAll", () => Promise.all(this.triggers.map(eventName => this.engine.subscribe(eventName, this.pushValue.bind(this), {})))); | ||
_defineProperty__default['default'](this, "unsubscribeAll", subscriptionIds => { | ||
for (const subscriptionId of subscriptionIds) { | ||
this.engine.unsubscribe(subscriptionId); | ||
} | ||
}); | ||
this.engine = engine; | ||
this.pullQueue = []; | ||
this.pushQueue = []; | ||
this.listening = true; | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.allSubscribed = this.subscribeAll(); | ||
} | ||
async next() { | ||
await this.allSubscribed; | ||
return this.listening ? this.pullValue() : this.return(); | ||
} | ||
async throw(error) { | ||
this.emptyQueue(await this.allSubscribed); | ||
return Promise.reject(error); | ||
} | ||
async return() { | ||
this.emptyQueue(await this.allSubscribed); | ||
return { | ||
done: true, | ||
value: undefined | ||
}; | ||
} | ||
} | ||
class WebhookEngine { | ||
constructor() { | ||
_defineProperty__default['default'](this, "listen", triggers => new WebhookIterator(this, triggers)); | ||
} | ||
} | ||
class CapnEventsEngine extends WebhookEngine { | ||
constructor() { | ||
super(); | ||
_defineProperty__default['default'](this, "engine", new events.EventEmitter()); | ||
_defineProperty__default['default'](this, "subCount", 0); | ||
_defineProperty__default['default'](this, "subscriptions", {}); | ||
_defineProperty__default['default'](this, "publish", (trigger, data) => { | ||
this.engine.emit(trigger, data); | ||
return Promise.resolve(); | ||
}); | ||
_defineProperty__default['default'](this, "subscribe", (trigger, handler) => { | ||
this.engine.on(trigger, handler); | ||
this.subCount = this.subCount++; | ||
this.subscriptions[this.subCount] = [trigger, handler]; | ||
return Promise.resolve(this.subCount); | ||
}); | ||
_defineProperty__default['default'](this, "unsubscribe", subId => { | ||
const [triggerName, onMessage] = this.subscriptions[subId]; | ||
delete this.subscriptions[subId]; | ||
this.engine.off(triggerName, onMessage); | ||
}); | ||
this.engine = new events.EventEmitter(); | ||
} | ||
} | ||
class CaptainHook { | ||
@@ -20,3 +151,5 @@ constructor(options = {}) { | ||
_defineProperty__default['default'](this, "publish", (trigger, data) => this.engine.publish(trigger, data)); | ||
_defineProperty__default['default'](this, "publish", (trigger, data) => this.engine.publish(trigger, { ...data, | ||
trigger | ||
})); | ||
@@ -30,3 +163,4 @@ _defineProperty__default['default'](this, "subscribe", (trigger, handler) => this.engine.subscribe(trigger, handler)); | ||
headers: req.headers, | ||
sender: req.get('origin') || req.get('host') | ||
originHost: req.get('origin') || req.get('host'), | ||
trigger: 'capn:event' | ||
}; | ||
@@ -45,3 +179,3 @@ | ||
this.publish('capn:event', payload); | ||
this.publish(payload.trigger, payload); | ||
res.sendStatus(202); | ||
@@ -82,3 +216,3 @@ return next(); | ||
const { | ||
engine: Engine = eventsEngine.CapnEventsEngine, | ||
engine: Engine = CapnEventsEngine, | ||
plugins = [], | ||
@@ -85,0 +219,0 @@ pre = [] |
import _defineProperty from '@babel/runtime/helpers/defineProperty'; | ||
import _asyncIterator from '@babel/runtime/helpers/asyncIterator'; | ||
import { CapnEventsEngine } from '@captain-hook/events-engine'; | ||
import { EventEmitter } from 'events'; | ||
let _Symbol$asyncIterator; | ||
_Symbol$asyncIterator = Symbol.asyncIterator; | ||
class WebhookIterator { | ||
constructor(engine, triggers) { | ||
_defineProperty(this, "pullQueue", []); | ||
_defineProperty(this, "pushQueue", []); | ||
_defineProperty(this, "done", false); | ||
_defineProperty(this, _Symbol$asyncIterator, () => this); | ||
_defineProperty(this, "pushValue", async event => { | ||
await this.allSubscribed; | ||
if (this.pullQueue.length !== 0) { | ||
const element = this.pullQueue.shift(); | ||
if (element) { | ||
element({ | ||
value: event, | ||
done: false | ||
}); | ||
} | ||
} else { | ||
this.pushQueue.push(event); | ||
} | ||
}); | ||
_defineProperty(this, "pullValue", () => new Promise(resolve => { | ||
if (this.pushQueue.length !== 0) { | ||
resolve({ | ||
done: false, | ||
value: this.pushQueue.shift() | ||
}); | ||
} else { | ||
this.pullQueue.push(resolve); | ||
} | ||
})); | ||
_defineProperty(this, "emptyQueue", subscriptionIds => { | ||
if (this.listening) { | ||
this.listening = false; | ||
this.unsubscribeAll(subscriptionIds); | ||
this.pullQueue.forEach(resolve => resolve({ | ||
done: true, | ||
value: undefined | ||
})); | ||
this.pullQueue.length = 0; | ||
this.pushQueue.length = 0; | ||
} | ||
}); | ||
_defineProperty(this, "subscribeAll", () => Promise.all(this.triggers.map(eventName => this.engine.subscribe(eventName, this.pushValue.bind(this), {})))); | ||
_defineProperty(this, "unsubscribeAll", subscriptionIds => { | ||
for (const subscriptionId of subscriptionIds) { | ||
this.engine.unsubscribe(subscriptionId); | ||
} | ||
}); | ||
this.engine = engine; | ||
this.pullQueue = []; | ||
this.pushQueue = []; | ||
this.listening = true; | ||
this.triggers = typeof triggers === 'string' ? [triggers] : triggers; | ||
this.allSubscribed = this.subscribeAll(); | ||
} | ||
async next() { | ||
await this.allSubscribed; | ||
return this.listening ? this.pullValue() : this.return(); | ||
} | ||
async throw(error) { | ||
this.emptyQueue(await this.allSubscribed); | ||
return Promise.reject(error); | ||
} | ||
async return() { | ||
this.emptyQueue(await this.allSubscribed); | ||
return { | ||
done: true, | ||
value: undefined | ||
}; | ||
} | ||
} | ||
class WebhookEngine { | ||
constructor() { | ||
_defineProperty(this, "listen", triggers => new WebhookIterator(this, triggers)); | ||
} | ||
} | ||
class CapnEventsEngine extends WebhookEngine { | ||
constructor() { | ||
super(); | ||
_defineProperty(this, "engine", new EventEmitter()); | ||
_defineProperty(this, "subCount", 0); | ||
_defineProperty(this, "subscriptions", {}); | ||
_defineProperty(this, "publish", (trigger, data) => { | ||
this.engine.emit(trigger, data); | ||
return Promise.resolve(); | ||
}); | ||
_defineProperty(this, "subscribe", (trigger, handler) => { | ||
this.engine.on(trigger, handler); | ||
this.subCount = this.subCount++; | ||
this.subscriptions[this.subCount] = [trigger, handler]; | ||
return Promise.resolve(this.subCount); | ||
}); | ||
_defineProperty(this, "unsubscribe", subId => { | ||
const [triggerName, onMessage] = this.subscriptions[subId]; | ||
delete this.subscriptions[subId]; | ||
this.engine.off(triggerName, onMessage); | ||
}); | ||
this.engine = new EventEmitter(); | ||
} | ||
} | ||
class CaptainHook { | ||
@@ -9,3 +140,5 @@ constructor(options = {}) { | ||
_defineProperty(this, "publish", (trigger, data) => this.engine.publish(trigger, data)); | ||
_defineProperty(this, "publish", (trigger, data) => this.engine.publish(trigger, { ...data, | ||
trigger | ||
})); | ||
@@ -19,3 +152,4 @@ _defineProperty(this, "subscribe", (trigger, handler) => this.engine.subscribe(trigger, handler)); | ||
headers: req.headers, | ||
sender: req.get('origin') || req.get('host') | ||
originHost: req.get('origin') || req.get('host'), | ||
trigger: 'capn:event' | ||
}; | ||
@@ -34,3 +168,3 @@ | ||
this.publish('capn:event', payload); | ||
this.publish(payload.trigger, payload); | ||
res.sendStatus(202); | ||
@@ -37,0 +171,0 @@ return next(); |
{ | ||
"name": "@captain-hook/core", | ||
"version": "0.2.14", | ||
"version": "0.2.15", | ||
"description": "web(hook hand)lers", | ||
@@ -19,3 +19,3 @@ "author": "Luke Smetham <luke@smetham.dev>", | ||
}, | ||
"gitHead": "fa2ba6c882809d183f98e17c80ce389536fe2856" | ||
"gitHead": "22c5100ae09ef6fc0a8d27feeb90722120ed24c8" | ||
} |
@@ -28,3 +28,7 @@ import { CapnEventsEngine } from '@captain-hook/events-engine'; | ||
*/ | ||
publish = (trigger, data) => this.engine.publish(trigger, data); | ||
publish = (trigger, data) => | ||
this.engine.publish(trigger, { | ||
...data, | ||
trigger, | ||
}); | ||
@@ -38,3 +42,4 @@ subscribe = (trigger, handler) => this.engine.subscribe(trigger, handler); | ||
headers: req.headers, | ||
sender: req.get('origin') || req.get('host'), | ||
originHost: req.get('origin') || req.get('host'), | ||
trigger: 'capn:event', | ||
}; | ||
@@ -59,3 +64,3 @@ | ||
this.publish('capn:event', payload); | ||
this.publish(payload.trigger, payload); | ||
@@ -62,0 +67,0 @@ res.sendStatus(202); |
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
529979
966