Use http agents for hook requests (#4791)
This commit is contained in:
committed by
Florent Vilmart
parent
32d8c36ff5
commit
82fb06f9c9
@@ -6,18 +6,26 @@ import * as Parse from "parse/node";
|
||||
// @flow-disable-next
|
||||
import * as request from "request";
|
||||
import { logger } from '../logger';
|
||||
import http from 'http';
|
||||
import https from 'https';
|
||||
|
||||
const DefaultHooksCollectionName = "_Hooks";
|
||||
const HTTPAgents = {
|
||||
http: new http.Agent({ keepAlive: true }),
|
||||
https: new https.Agent({ keepAlive: true }),
|
||||
}
|
||||
|
||||
export class HooksController {
|
||||
_applicationId:string;
|
||||
_webhookKey:string;
|
||||
database: any;
|
||||
keepAlive: boolean;
|
||||
|
||||
constructor(applicationId:string, databaseController, webhookKey) {
|
||||
constructor(applicationId:string, databaseController, webhookKey, keepAlive) {
|
||||
this._applicationId = applicationId;
|
||||
this._webhookKey = webhookKey;
|
||||
this.database = databaseController;
|
||||
this.keepAlive = keepAlive;
|
||||
}
|
||||
|
||||
load() {
|
||||
@@ -85,7 +93,7 @@ export class HooksController {
|
||||
}
|
||||
|
||||
addHookToTriggers(hook) {
|
||||
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
|
||||
var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey, this.keepAlive);
|
||||
wrappedFunction.url = hook.url;
|
||||
if (hook.className) {
|
||||
triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId)
|
||||
@@ -159,7 +167,7 @@ export class HooksController {
|
||||
}
|
||||
}
|
||||
|
||||
function wrapToHTTPRequest(hook, key) {
|
||||
function wrapToHTTPRequest(hook, key, keepAlive) {
|
||||
return (req, res) => {
|
||||
const jsonBody = {};
|
||||
for (var i in req) {
|
||||
@@ -177,9 +185,14 @@ function wrapToHTTPRequest(hook, key) {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(jsonBody)
|
||||
body: JSON.stringify(jsonBody),
|
||||
};
|
||||
|
||||
if (keepAlive) {
|
||||
const agent = hook.url.startsWith('https') ? HTTPAgents['https'] : HTTPAgents['http'];
|
||||
jsonRequest.agent = agent;
|
||||
}
|
||||
|
||||
if (key) {
|
||||
jsonRequest.headers['X-Parse-Webhook-Key'] = key;
|
||||
} else {
|
||||
|
||||
@@ -149,9 +149,10 @@ export function getDatabaseController(options: ParseServerOptions, cacheControll
|
||||
export function getHooksController(options: ParseServerOptions, databaseController: DatabaseController): HooksController {
|
||||
const {
|
||||
appId,
|
||||
webhookKey
|
||||
webhookKey,
|
||||
hookKeepAlive,
|
||||
} = options;
|
||||
return new HooksController(appId, databaseController, webhookKey);
|
||||
return new HooksController(appId, databaseController, webhookKey, hookKeepAlive);
|
||||
}
|
||||
|
||||
interface PushControlling {
|
||||
@@ -228,4 +229,3 @@ export function getDatabaseAdapter(databaseURI, collectionPrefix, databaseOption
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,6 +131,8 @@ export interface ParseServerOptions {
|
||||
startLiveQueryServer: ?boolean;
|
||||
/* Live query server configuration options (will start the liveQuery server) */
|
||||
liveQueryServerOptions: ?LiveQueryServerOptions;
|
||||
/* Keep hook HTTP connections alive */
|
||||
hookKeepAlive: ?boolean;
|
||||
|
||||
__indexBuildCompletionCallbackForTests: ?()=>void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user