loadmill
Version:
A node.js module for running load tests and functional tests on loadmill.com
58 lines (57 loc) • 2.02 kB
JavaScript
"use strict";
exports.__esModule = true;
var tslib_1 = require("tslib");
var superagent = require("superagent");
var HTTPSAgent = require('agentkeepalive').HttpsAgent;
var HTTPAgent = require('agentkeepalive');
var DEFAULT_SOCKET_TIMEOUT = 60000;
/**
* Factory for creating a singleton superagent instance with connection pooling
*/
var SuperagentFactory = /** @class */ (function () {
function SuperagentFactory() {
this.instance = null;
this.agentOptions = {
timeout: DEFAULT_SOCKET_TIMEOUT,
freeSocketTimeout: DEFAULT_SOCKET_TIMEOUT / 2
};
this.httpAgent = new HTTPAgent(this.agentOptions);
this.httpsAgent = new HTTPSAgent(this.agentOptions);
}
/**
* Get the singleton superagent instance
* @returns {SuperAgentStatic} Configured superagent instance
*/
SuperagentFactory.prototype.getInstance = function () {
if (!this.instance) {
this.instance = superagent;
if (this.instance.agent) {
this.instance.agent.http = this.httpAgent;
this.instance.agent.https = this.httpsAgent;
}
}
return this.instance;
};
SuperagentFactory.prototype.reset = function () {
this.instance = null;
};
/**
* Update agent options and recreate agents
* @param {Partial<AgentOptions>} options - New agent options
*/
SuperagentFactory.prototype.updateAgentOptions = function (options) {
this.agentOptions = tslib_1.__assign(tslib_1.__assign({}, this.agentOptions), options);
if (this.httpAgent) {
this.httpAgent.destroy();
}
if (this.httpsAgent) {
this.httpsAgent.destroy();
}
this.httpAgent = new HTTPAgent(this.agentOptions);
this.httpsAgent = new HTTPSAgent(this.agentOptions);
this.reset();
};
return SuperagentFactory;
}());
var httpClientFactory = new SuperagentFactory();
exports["default"] = httpClientFactory;