Skip to content

Commit cff31d7

Browse files
committed
ignore QS ? in URLs, use standard for linting, syntax fixes
1 parent 58c8fb5 commit cff31d7

File tree

5 files changed

+201
-270
lines changed

5 files changed

+201
-270
lines changed

Diff for: .jshintrc

-59
This file was deleted.

Diff for: .travis.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- '4'
5-
- '6'
6-
- '8'
4+
- 8
5+
- 10
6+
- 12
7+
- lts/*
8+
- current
79
branches:
810
only:
911
- master

Diff for: github-webhook-handler.js

+57-48
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,119 @@
1-
const EventEmitter = require('events').EventEmitter
2-
, inherits = require('util').inherits
3-
, crypto = require('crypto')
4-
, bl = require('bl')
1+
const EventEmitter = require('events')
2+
const crypto = require('crypto')
3+
const bl = require('bl')
54

65
function findHandler (url, arr) {
7-
if (!Array.isArray(arr))
6+
if (!Array.isArray(arr)) {
87
return arr
9-
var ret = arr[0]
10-
for (var i = 0; i < arr.length; i++) {
11-
if (url.split('?')[0] === arr[i].path)
8+
}
9+
10+
let ret = arr[0]
11+
for (let i = 0; i < arr.length; i++) {
12+
if (url === arr[i].path) {
1213
ret = arr[i]
14+
}
1315
}
16+
1417
return ret
1518
}
1619

1720
function checkType (options) {
18-
if (typeof options != 'object')
21+
if (typeof options !== 'object') {
1922
throw new TypeError('must provide an options object')
23+
}
2024

21-
if (typeof options.path != 'string')
25+
if (typeof options.path !== 'string') {
2226
throw new TypeError('must provide a \'path\' option')
27+
}
2328

24-
if (typeof options.secret != 'string')
29+
if (typeof options.secret !== 'string') {
2530
throw new TypeError('must provide a \'secret\' option')
31+
}
2632
}
2733

2834
function create (initOptions) {
29-
30-
var options
31-
//validate type of options
35+
let options
36+
// validate type of options
3237
if (Array.isArray(initOptions)) {
33-
initOptions.forEach(function(item){
34-
checkType(item)
35-
})
38+
for (let i = 0; i < initOptions.length; i++) {
39+
checkType(initOptions[i])
40+
}
3641
} else {
3742
checkType(initOptions)
3843
}
3944

40-
// make it an EventEmitter, sort of
41-
handler.__proto__ = EventEmitter.prototype
45+
// make it an EventEmitter
46+
Object.setPrototypeOf(handler, EventEmitter.prototype)
4247
EventEmitter.call(handler)
4348

4449
handler.sign = sign
4550
handler.verify = verify
4651

4752
return handler
4853

49-
5054
function sign (data) {
51-
return 'sha1=' + crypto.createHmac('sha1', options.secret).update(data).digest('hex')
55+
return `sha1=${crypto.createHmac('sha1', options.secret).update(data).digest('hex')}`
5256
}
5357

5458
function verify (signature, data) {
5559
return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(sign(data)))
5660
}
5761

5862
function handler (req, res, callback) {
59-
var events
63+
let events
6064

6165
options = findHandler(req.url, initOptions)
6266

63-
if (typeof options.events == 'string' && options.events != '*')
64-
events = [ options.events ]
65-
66-
else if (Array.isArray(options.events) && options.events.indexOf('*') == -1)
67+
if (typeof options.events === 'string' && options.events !== '*') {
68+
events = [options.events]
69+
} else if (Array.isArray(options.events) && options.events.indexOf('*') === -1) {
6770
events = options.events
68-
69-
if (req.url.split('?').shift() !== options.path || req.method !== 'POST')
71+
}
72+
73+
if (req.url !== options.path || req.method !== 'POST') {
7074
return callback()
75+
}
7176

7277
function hasError (msg) {
7378
res.writeHead(400, { 'content-type': 'application/json' })
7479
res.end(JSON.stringify({ error: msg }))
7580

76-
var err = new Error(msg)
81+
const err = new Error(msg)
7782

7883
handler.emit('error', err, req)
7984
callback(err)
8085
}
8186

82-
var sig = req.headers['x-hub-signature']
83-
, event = req.headers['x-github-event']
84-
, id = req.headers['x-github-delivery']
87+
const sig = req.headers['x-hub-signature']
88+
const event = req.headers['x-github-event']
89+
const id = req.headers['x-github-delivery']
8590

86-
if (!sig)
91+
if (!sig) {
8792
return hasError('No X-Hub-Signature found on request')
93+
}
8894

89-
if (!event)
95+
if (!event) {
9096
return hasError('No X-Github-Event found on request')
97+
}
9198

92-
if (!id)
99+
if (!id) {
93100
return hasError('No X-Github-Delivery found on request')
101+
}
94102

95-
if (events && events.indexOf(event) == -1)
103+
if (events && events.indexOf(event) === -1) {
96104
return hasError('X-Github-Event is not acceptable')
105+
}
97106

98-
req.pipe(bl(function (err, data) {
107+
req.pipe(bl((err, data) => {
99108
if (err) {
100109
return hasError(err.message)
101110
}
102111

103-
var obj
112+
let obj
104113

105-
if (!verify(sig, data))
114+
if (!verify(sig, data)) {
106115
return hasError('X-Hub-Signature does not match blob signature')
116+
}
107117

108118
try {
109119
obj = JSON.parse(data.toString())
@@ -114,14 +124,14 @@ function create (initOptions) {
114124
res.writeHead(200, { 'content-type': 'application/json' })
115125
res.end('{"ok":true}')
116126

117-
var emitData = {
118-
event : event
119-
, id : id
120-
, payload : obj
121-
, protocol: req.protocol
122-
, host : req.headers['host']
123-
, url : req.url
124-
, path : options.path
127+
const emitData = {
128+
event: event,
129+
id: id,
130+
payload: obj,
131+
protocol: req.protocol,
132+
host: req.headers.host,
133+
url: req.url,
134+
path: options.path
125135
}
126136

127137
handler.emit(event, emitData)
@@ -130,5 +140,4 @@ function create (initOptions) {
130140
}
131141
}
132142

133-
134143
module.exports = create

Diff for: package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "github-webhook-handler.js",
66
"types": "github-webhook-handler.d.ts",
77
"scripts": {
8-
"test": "node test.js"
8+
"lint": "standard *.js",
9+
"test": "npm run lint && node test.js"
910
},
1011
"keywords": [
1112
"github",
@@ -18,12 +19,13 @@
1819
},
1920
"license": "MIT",
2021
"dependencies": {
21-
"bl": "~1.1.2"
22+
"bl": "~4.0.0"
2223
},
2324
"devDependencies": {
2425
"@types/node": "*",
25-
"run-series": "~1.1.4",
26-
"tape": "~4.6.0",
27-
"through2": "~2.0.1"
26+
"run-series": "~1.1.8",
27+
"standard": "~14.3.1",
28+
"tape": "~4.11.0",
29+
"through2": "~3.0.1"
2830
}
2931
}

0 commit comments

Comments
 (0)