@@ -6,18 +6,26 @@ import * as Parse from "parse/node";
6
6
// @flow -disable-next
7
7
import * as request from "request" ;
8
8
import { logger } from '../logger' ;
9
+ import http from 'http' ;
10
+ import https from 'https' ;
9
11
10
12
const DefaultHooksCollectionName = "_Hooks" ;
13
+ const HTTPAgents = {
14
+ http : new http . Agent ( { keepAlive : true } ) ,
15
+ https : new https . Agent ( { keepAlive : true } ) ,
16
+ }
11
17
12
18
export class HooksController {
13
19
_applicationId :string ;
14
20
_webhookKey:string ;
15
21
database: any ;
22
+ keepAlive: boolean ;
16
23
17
- constructor ( applicationId :string , databaseController , webhookKey ) {
24
+ constructor ( applicationId :string , databaseController , webhookKey , keepAlive ) {
18
25
this . _applicationId = applicationId ;
19
26
this . _webhookKey = webhookKey ;
20
27
this . database = databaseController ;
28
+ this . keepAlive = keepAlive ;
21
29
}
22
30
23
31
load ( ) {
@@ -85,7 +93,7 @@ export class HooksController {
85
93
}
86
94
87
95
addHookToTriggers ( hook ) {
88
- var wrappedFunction = wrapToHTTPRequest ( hook , this . _webhookKey ) ;
96
+ var wrappedFunction = wrapToHTTPRequest ( hook , this . _webhookKey , this . keepAlive ) ;
89
97
wrappedFunction . url = hook . url ;
90
98
if ( hook . className ) {
91
99
triggers . addTrigger ( hook . triggerName , hook . className , wrappedFunction , this . _applicationId )
@@ -159,7 +167,7 @@ export class HooksController {
159
167
}
160
168
}
161
169
162
- function wrapToHTTPRequest ( hook , key ) {
170
+ function wrapToHTTPRequest ( hook , key , keepAlive ) {
163
171
return ( req , res ) => {
164
172
const jsonBody = { } ;
165
173
for ( var i in req ) {
@@ -177,9 +185,14 @@ function wrapToHTTPRequest(hook, key) {
177
185
headers : {
178
186
'Content-Type' : 'application/json'
179
187
} ,
180
- body : JSON . stringify ( jsonBody )
188
+ body : JSON . stringify ( jsonBody ) ,
181
189
} ;
182
190
191
+ if ( keepAlive ) {
192
+ const agent = hook . url . startsWith ( 'https' ) ? HTTPAgents [ 'https' ] : HTTPAgents [ 'http' ] ;
193
+ jsonRequest . agent = agent ;
194
+ }
195
+
183
196
if ( key ) {
184
197
jsonRequest . headers [ 'X-Parse-Webhook-Key' ] = key ;
185
198
} else {
0 commit comments