Comparing version 1.0.0 to 1.1.0
@@ -13,4 +13,4 @@ const gulel = require('../src/gulel') | ||
twitch: { | ||
accessToken: '<access token>', | ||
clientId: '<client id>' | ||
accessToken: '9e7ppqxazc802kp11z00vsj2hmjqem', | ||
clientId: 'gp762nuuoqcoxypju8c569th9wz7q5' | ||
}, | ||
@@ -17,0 +17,0 @@ server: { |
{ | ||
"name": "gulel", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Gulel is an express based application server that manages Twitch Webhooks with minimal setup", | ||
@@ -30,2 +30,2 @@ "keywords": [ | ||
} | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
![npm (scoped)](https://img.shields.io/npm/v/gulel?label=NPM) ![NPM](https://img.shields.io/npm/l/gulel?label=License) ![npm](https://img.shields.io/npm/dt/gulel?label=Downloads) | ||
# Gulel - Local Server for Twitch Webhooks | ||
@@ -79,2 +81,4 @@ Gulel is an [express](https://expressjs.com) based application server that manages [Twitch Webhooks](https://dev.twitch.tv/docs/api/guide#webhooks) with minimal setup. It uses [ngrok](https://ngrok.com/) to make the local server reachable by Twitch servers. | ||
Also, `gulel.shutdown()` can be used to shut it down. | ||
## Examples | ||
@@ -81,0 +85,0 @@ Few examples are available in the [examples](./examples) directory. |
111
src/gulel.js
@@ -21,5 +21,11 @@ const express = require('express') | ||
app.isListening = false | ||
app.subscriptions = [] | ||
app.subscribe = (topic, callback) => { | ||
app.getSubscription = (topic) => { | ||
if (!topic.startsWith(twitchBaseUrl)) topic = twitchBaseUrl + topic | ||
return app.subscriptions.find((sub) => sub.hub['hub.topic'] === topic) | ||
} | ||
app.subscribe = async (topic, callback) => { | ||
// remove the twitch base url from the topic if present | ||
@@ -33,3 +39,3 @@ if (topic.startsWith(twitchBaseUrl)) topic = topic.replace(twitchBaseUrl, '') | ||
try { | ||
const sub = app.subscriptions.find((sub) => sub.hub['hub.topic'] === req.query['hub.topic']) | ||
const sub = app.getSubscription(req.query['hub.topic']) | ||
@@ -40,3 +46,3 @@ if (sub) { | ||
if (req.query['hub.mode'] === 'subscribe')sub.subscribed = true | ||
if (req.query['hub.mode'] === 'subscribe') sub.markSubscribed() | ||
else if (req.query['hub.mode'] === 'unsubscribe') sub.subscribed = false | ||
@@ -77,18 +83,27 @@ | ||
app.subscriptions.push({ | ||
const subscription = { | ||
hub: { | ||
'hub.callback': context, // domain will be added later | ||
'hub.topic': twitchBaseUrl + topic, | ||
'hub.lease_seconds': 3600 | ||
'hub.lease_seconds': 120 | ||
}, | ||
getHub: function () { | ||
const hub = { ...this.hub } | ||
hub['hub.callback'] = app.config.server.baseUrl + '/' + hub['hub.callback'] | ||
hub['hub.secret'] = app.config.twitch.secret | ||
return hub | ||
}, | ||
subscribed: false, | ||
subscribe: async function () { | ||
await app.twitch.subscribe(this.hub) | ||
// const sub = this | ||
// setTimeout(() => sub.unsubscribe(), 5000) | ||
markSubscribed: function () { | ||
this.subscribed = true | ||
this.enableRenewal() | ||
}, | ||
subscribe: function () { | ||
app.twitch.subscribe(this.getHub()) | ||
}, | ||
unsubscribe: async function () { | ||
if (this.subscribed) { | ||
await app.twitch.unsubscribe(this.hub) | ||
await app.twitch.unsubscribe(this.getHub()) | ||
} | ||
@@ -108,3 +123,12 @@ }, | ||
} | ||
}) | ||
} | ||
app.subscriptions.push(subscription) | ||
// server is running | ||
if (app.isListening) { | ||
await subscription.subscribe() | ||
} | ||
return subscription | ||
} | ||
@@ -121,2 +145,28 @@ | ||
app.shutdown = async () => { | ||
// unsubcribe active subscriptions | ||
console.log('Gulel is shutting down') | ||
console.log('Unsubscribing from active subscriptions') | ||
for (let index = 0; index < app.subscriptions.length; index++) { | ||
const sub = app.subscriptions[index] | ||
await sub.unsubscribe() | ||
} | ||
// wait till all active subscriptions are unsubscribed | ||
while (true) { | ||
const sub = app.subscriptions.find((sub) => sub.subscribed === true) | ||
if (sub) { | ||
await sleep(1000) | ||
} else { | ||
break | ||
} | ||
} | ||
process.exit(0) | ||
} | ||
app.start = (config, done) => { | ||
@@ -145,9 +195,7 @@ app.config = config | ||
let baseUrl | ||
if (config.server.tunnel) { | ||
baseUrl = await ngrok.connect(port) | ||
console.log(`Gulel is listening at ${baseUrl} -> ${localhost}`) | ||
app.config.server.baseUrl = await ngrok.connect(port) | ||
console.log(`Gulel is listening at ${app.config.server.baseUrl} -> ${localhost}`) | ||
} else { | ||
baseUrl = localhost | ||
app.config.server.baseUrl = localhost | ||
console.log(`Gulel is listening at ${localhost}`) | ||
@@ -160,5 +208,2 @@ } | ||
const sub = app.subscriptions[index] | ||
const { hub } = sub | ||
hub['hub.callback'] = baseUrl + '/' + hub['hub.callback'] | ||
hub['hub.secret'] = app.config.twitch.secret | ||
@@ -168,32 +213,8 @@ await sub.subscribe() | ||
const shutDown = async () => { | ||
// unsubcribe active subscriptions | ||
app.isListening = true | ||
console.log('Gulel is shutting down') | ||
console.log('Unsubscribing from active subscriptions') | ||
for (let index = 0; index < app.subscriptions.length; index++) { | ||
const sub = app.subscriptions[index] | ||
await sub.unsubscribe() | ||
} | ||
// wait till all active subscriptions are unsubscribed | ||
while (true) { | ||
const sub = app.subscriptions.find((sub) => sub.subscribed === true) | ||
if (sub) { | ||
await sleep(1000) | ||
} else { | ||
break | ||
} | ||
} | ||
process.exit(0) | ||
} | ||
// shut down the server when `quit` key word is entered in terminal | ||
process.stdin.resume() | ||
process.stdin.on('data', data => { | ||
if (Buffer.compare(data, Buffer.from('quit\n')) === 0) shutDown() | ||
if (Buffer.compare(data, Buffer.from('quit\n')) === 0) app.shutdown() | ||
}) | ||
@@ -200,0 +221,0 @@ |
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
14876
9
240
117