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' )
5
4
6
5
function findHandler ( url , arr ) {
7
- if ( ! Array . isArray ( arr ) )
6
+ if ( ! Array . isArray ( arr ) ) {
8
7
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 ) {
12
13
ret = arr [ i ]
14
+ }
13
15
}
16
+
14
17
return ret
15
18
}
16
19
17
20
function checkType ( options ) {
18
- if ( typeof options != 'object' )
21
+ if ( typeof options !== 'object' ) {
19
22
throw new TypeError ( 'must provide an options object' )
23
+ }
20
24
21
- if ( typeof options . path != 'string' )
25
+ if ( typeof options . path !== 'string' ) {
22
26
throw new TypeError ( 'must provide a \'path\' option' )
27
+ }
23
28
24
- if ( typeof options . secret != 'string' )
29
+ if ( typeof options . secret !== 'string' ) {
25
30
throw new TypeError ( 'must provide a \'secret\' option' )
31
+ }
26
32
}
27
33
28
34
function create ( initOptions ) {
29
-
30
- var options
31
- //validate type of options
35
+ let options
36
+ // validate type of options
32
37
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
+ }
36
41
} else {
37
42
checkType ( initOptions )
38
43
}
39
44
40
- // make it an EventEmitter, sort of
41
- handler . __proto__ = EventEmitter . prototype
45
+ // make it an EventEmitter
46
+ Object . setPrototypeOf ( handler , EventEmitter . prototype )
42
47
EventEmitter . call ( handler )
43
48
44
49
handler . sign = sign
45
50
handler . verify = verify
46
51
47
52
return handler
48
53
49
-
50
54
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' ) } `
52
56
}
53
57
54
58
function verify ( signature , data ) {
55
59
return crypto . timingSafeEqual ( Buffer . from ( signature ) , Buffer . from ( sign ( data ) ) )
56
60
}
57
61
58
62
function handler ( req , res , callback ) {
59
- var events
63
+ let events
60
64
61
65
options = findHandler ( req . url , initOptions )
62
66
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 ) {
67
70
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' ) {
70
74
return callback ( )
75
+ }
71
76
72
77
function hasError ( msg ) {
73
78
res . writeHead ( 400 , { 'content-type' : 'application/json' } )
74
79
res . end ( JSON . stringify ( { error : msg } ) )
75
80
76
- var err = new Error ( msg )
81
+ const err = new Error ( msg )
77
82
78
83
handler . emit ( 'error' , err , req )
79
84
callback ( err )
80
85
}
81
86
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' ]
85
90
86
- if ( ! sig )
91
+ if ( ! sig ) {
87
92
return hasError ( 'No X-Hub-Signature found on request' )
93
+ }
88
94
89
- if ( ! event )
95
+ if ( ! event ) {
90
96
return hasError ( 'No X-Github-Event found on request' )
97
+ }
91
98
92
- if ( ! id )
99
+ if ( ! id ) {
93
100
return hasError ( 'No X-Github-Delivery found on request' )
101
+ }
94
102
95
- if ( events && events . indexOf ( event ) == - 1 )
103
+ if ( events && events . indexOf ( event ) === - 1 ) {
96
104
return hasError ( 'X-Github-Event is not acceptable' )
105
+ }
97
106
98
- req . pipe ( bl ( function ( err , data ) {
107
+ req . pipe ( bl ( ( err , data ) => {
99
108
if ( err ) {
100
109
return hasError ( err . message )
101
110
}
102
111
103
- var obj
112
+ let obj
104
113
105
- if ( ! verify ( sig , data ) )
114
+ if ( ! verify ( sig , data ) ) {
106
115
return hasError ( 'X-Hub-Signature does not match blob signature' )
116
+ }
107
117
108
118
try {
109
119
obj = JSON . parse ( data . toString ( ) )
@@ -114,14 +124,14 @@ function create (initOptions) {
114
124
res . writeHead ( 200 , { 'content-type' : 'application/json' } )
115
125
res . end ( '{"ok":true}' )
116
126
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
125
135
}
126
136
127
137
handler . emit ( event , emitData )
@@ -130,5 +140,4 @@ function create (initOptions) {
130
140
}
131
141
}
132
142
133
-
134
143
module . exports = create
0 commit comments