twitter-autohook
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -31,6 +31,11 @@ class TwitterError extends Error { | ||
const requestAllowed = response.headers['x-rate-limit-limit']; | ||
const resetAt = response.headers['x-rate-limit-reset'] * 1000 - (new Date().getTime()); | ||
const resetAtMin = Math.round(resetAt / 60 / 1000); | ||
super(`You exceeded the rate limit for ${response.req.path} (${requestAllowed} requests available, 0 remaining). Wait ${resetAtMin} minutes before trying again.`); | ||
if ('x-rate-limit-limit' in response.headers && 'x-rate-limit-reset' in response.headers) { | ||
const requestAllowed = response.headers['x-rate-limit-limit']; | ||
const resetAt = response.headers['x-rate-limit-reset'] * 1000 - (new Date().getTime()); | ||
const resetAtMin = Math.round(resetAt / 60 / 1000); | ||
super(`You exceeded the rate limit for ${response.req.path} (${requestAllowed} requests available, 0 remaining). Wait ${resetAtMin} minutes before trying again.`); | ||
} else { | ||
super(`You exceeded the rate limit for ${response.req.path}. Wait until rate limit resets and try again.`); | ||
} | ||
this.resetAt = response.headers['x-rate-limit-reset'] * 1000; | ||
@@ -37,0 +42,0 @@ this.name = this.constructor.name; |
28
index.js
@@ -46,2 +46,3 @@ #!/usr/bin/env node | ||
}, | ||
...this.headers, | ||
}; | ||
@@ -64,2 +65,3 @@ | ||
auth: { bearer: token }, | ||
...this.headers, | ||
}; | ||
@@ -96,6 +98,6 @@ | ||
oauth: auth, | ||
} | ||
...this.headers, | ||
}; | ||
const response = await get(requestConfig); | ||
switch (response.statusCode) { | ||
@@ -129,2 +131,3 @@ case 200: | ||
oauth: auth, | ||
...this.headers, | ||
} | ||
@@ -154,7 +157,5 @@ | ||
const validateWebhook = (token, auth, res) => { | ||
const validateWebhook = (token, auth) => { | ||
const responseToken = crypto.createHmac('sha256', auth.consumer_secret).update(token).digest('base64'); | ||
res.writeHead(200, {'content-type': 'application/json'}); | ||
res.end(JSON.stringify({response_token: `sha256=${responseToken}`})); | ||
return {response_token: `sha256=${responseToken}`}; | ||
} | ||
@@ -179,2 +180,3 @@ | ||
oauth: auth, | ||
...this.headers, | ||
} | ||
@@ -188,2 +190,3 @@ | ||
break; | ||
case 400: | ||
case 403: | ||
@@ -193,2 +196,3 @@ throw new WebhookURIError(response); | ||
case 429: | ||
console.log(response.headers); | ||
throw new RateLimitError(response); | ||
@@ -212,2 +216,3 @@ return null; | ||
oauth: auth, | ||
...this.headers, | ||
}; | ||
@@ -232,2 +237,3 @@ | ||
port = process.env.PORT || DEFAULT_PORT, | ||
headers = [], | ||
} = {}) { | ||
@@ -246,2 +252,3 @@ | ||
this.port = port; | ||
this.headers = headers; | ||
} | ||
@@ -258,3 +265,5 @@ | ||
if (route.query.crc_token) { | ||
return validateWebhook(route.query.crc_token, this.auth, res); | ||
const crc = validateWebhook(route.query.crc_token, this.auth); | ||
res.writeHead(200, {'content-type': 'application/json'}); | ||
res.end(JSON.stringify(crc)); | ||
} | ||
@@ -282,5 +291,5 @@ | ||
async start(webhookUrl = null) { | ||
this.startServer(); | ||
if (!webhookUrl) { | ||
this.startServer(); | ||
const url = await ngrok.connect(this.port); | ||
@@ -327,2 +336,3 @@ webhookUrl = `${url}${WEBHOOK_ROUTE}`; | ||
oauth: auth, | ||
...this.headers, | ||
}; | ||
@@ -342,2 +352,2 @@ | ||
module.exports = {Autohook, WebhookURIError, UserSubscriptionError, TooManySubscriptionsError, setWebhook, validateWebhook}; | ||
module.exports = {Autohook, WebhookURIError, UserSubscriptionError, TooManySubscriptionsError, validateWebhook}; |
{ | ||
"name": "twitter-autohook", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Automatically setup and serve webhooks for the Twitter Account Activity API", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
const {Autohook, setWebhook, validateWebhook} = require('..'); | ||
const {Autohook} = require('..'); | ||
@@ -3,0 +3,0 @@ const qs = require('querystring'); |
@@ -1,2 +0,2 @@ | ||
const {Autohook, setWebhook, validateWebhook} = require('..'); | ||
const {Autohook, validateWebhook} = require('..'); | ||
@@ -17,3 +17,5 @@ const url = require('url'); | ||
if (route.query.crc_token) { | ||
return validateWebhook(route.query.crc_token, auth, res); | ||
const crc = validateWebhook(route.query.crc_token, auth, res); | ||
res.writeHead(200, {'content-type': 'application/json'}); | ||
res.end(JSON.stringify(crc)); | ||
} | ||
@@ -53,3 +55,3 @@ | ||
await webhook.removeWebhooks(); | ||
await setWebhook(url, config, config.env); | ||
await webhook.start(webhookURL); | ||
await webhook.subscribe({ | ||
@@ -56,0 +58,0 @@ oauth_token: config.token, |
30219
622