smartrequest/ts/smartrequest.request.ts

185 lines
5.2 KiB
TypeScript
Raw Normal View History

2018-08-13 23:47:54 +00:00
import * as plugins from './smartrequest.plugins';
import * as interfaces from './smartrequest.interfaces';
2017-01-28 23:51:47 +00:00
2018-08-13 23:47:54 +00:00
import { IncomingMessage } from 'http';
2018-06-13 20:34:49 +00:00
2018-07-16 21:39:25 +00:00
export interface IExtendedIncomingMessage extends IncomingMessage {
body: any;
}
2018-06-13 20:34:49 +00:00
2018-07-16 21:39:25 +00:00
const buildUtf8Response = (
2019-06-12 13:16:27 +00:00
incomingMessageArg: IncomingMessage,
autoJsonParse = true
2018-07-16 21:39:25 +00:00
): Promise<IExtendedIncomingMessage> => {
2019-08-22 10:38:11 +00:00
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
2017-06-05 17:09:40 +00:00
// Continuously update stream with data
2018-08-13 23:47:54 +00:00
let body = '';
2020-08-24 12:01:38 +00:00
incomingMessageArg.on('data', (chunkArg) => {
2018-06-13 20:34:49 +00:00
body += chunkArg;
});
2019-08-22 10:38:55 +00:00
incomingMessageArg.on('end', () => {
2019-06-12 13:16:27 +00:00
if (autoJsonParse) {
try {
(incomingMessageArg as IExtendedIncomingMessage).body = JSON.parse(body);
} catch (err) {
(incomingMessageArg as IExtendedIncomingMessage).body = body;
}
} else {
2018-07-16 21:39:25 +00:00
(incomingMessageArg as IExtendedIncomingMessage).body = body;
2017-06-05 17:09:40 +00:00
}
2018-07-16 21:39:25 +00:00
done.resolve(incomingMessageArg as IExtendedIncomingMessage);
2018-06-13 20:34:49 +00:00
});
return done.promise;
};
2017-01-28 23:51:47 +00:00
2018-07-16 21:39:25 +00:00
/**
* determine wether a url is a unix sock
* @param urlArg
*/
const testForUnixSock = (urlArg: string): boolean => {
const unixRegex = /^(http:\/\/|https:\/\/|)unix:/;
return unixRegex.test(urlArg);
};
/**
* determine socketPath and path for unixsock
*/
const parseSocketPathAndRoute = (stringToParseArg: string) => {
const parseRegex = /(.*):(.*)/;
const result = parseRegex.exec(stringToParseArg);
return {
socketPath: result[1],
2020-08-24 12:01:38 +00:00
path: result[2],
2018-08-13 23:47:54 +00:00
};
};
2018-07-16 21:39:25 +00:00
2019-08-22 10:40:19 +00:00
/**
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
2021-05-16 23:39:25 +00:00
const httpAgent = new plugins.agentkeepalive();
2019-08-21 10:55:19 +00:00
2019-09-08 15:47:30 +00:00
/**
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpAgentKeepAliveFalse = new plugins.http.Agent({
2019-09-08 15:57:28 +00:00
maxFreeSockets: 0,
2019-09-08 15:47:30 +00:00
keepAlive: false,
2020-08-24 12:01:38 +00:00
keepAliveMsecs: 0,
2019-09-08 15:47:30 +00:00
});
2019-08-22 10:40:19 +00:00
/**
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
2019-09-28 20:50:35 +00:00
const httpsAgent = new plugins.agentkeepalive.HttpsAgent();
2019-08-21 10:55:19 +00:00
2019-09-08 15:47:30 +00:00
/**
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpsAgentKeepAliveFalse = new plugins.https.Agent({
2019-09-08 15:57:28 +00:00
maxFreeSockets: 0,
2019-09-08 15:47:30 +00:00
keepAlive: false,
2020-08-24 12:01:38 +00:00
keepAliveMsecs: 0,
2019-09-08 15:47:30 +00:00
});
2018-06-13 20:34:49 +00:00
export let request = async (
2020-09-29 15:22:25 +00:00
urlArg: string,
2018-06-13 20:34:49 +00:00
optionsArg: interfaces.ISmartRequestOptions = {},
2019-09-28 22:56:56 +00:00
responseStreamArg: boolean = false,
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
2018-07-16 21:39:25 +00:00
): Promise<IExtendedIncomingMessage> => {
2019-08-22 10:38:11 +00:00
const done = plugins.smartpromise.defer<any>();
2018-07-16 21:39:25 +00:00
2019-06-12 13:16:27 +00:00
// merge options
const defaultOptions: interfaces.ISmartRequestOptions = {
2019-08-21 10:55:19 +00:00
// agent: agent,
2019-09-08 15:47:30 +00:00
autoJsonParse: true,
2020-08-24 12:01:38 +00:00
keepAlive: true,
2019-08-16 19:38:50 +00:00
};
2019-06-12 13:16:27 +00:00
optionsArg = {
...defaultOptions,
2020-08-24 12:01:38 +00:00
...optionsArg,
2019-08-16 19:38:50 +00:00
};
2019-06-12 13:16:27 +00:00
2018-07-16 21:39:25 +00:00
// parse url
2021-05-16 23:39:25 +00:00
const parsedUrl = plugins.smarturl.Smarturl.createFromUrl(urlArg, {
2022-02-15 17:57:42 +00:00
searchParams: optionsArg.queryParams || {},
2021-05-16 23:39:25 +00:00
});
2018-07-16 21:39:25 +00:00
optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) {
2019-08-22 10:38:11 +00:00
optionsArg.port = parseInt(parsedUrl.port, 10);
2018-07-16 21:39:25 +00:00
}
optionsArg.path = parsedUrl.path;
2021-05-16 23:39:25 +00:00
optionsArg.queryParams = parsedUrl.searchParams;
2018-07-16 21:39:25 +00:00
// determine if unixsock
2020-09-29 15:22:25 +00:00
if (testForUnixSock(urlArg)) {
2018-08-13 23:47:54 +00:00
const detailedUnixPath = parseSocketPathAndRoute(optionsArg.path);
2018-07-16 21:39:25 +00:00
optionsArg.socketPath = detailedUnixPath.socketPath;
optionsArg.path = detailedUnixPath.path;
2017-06-05 17:09:40 +00:00
}
2018-08-13 23:47:54 +00:00
2019-08-16 20:00:01 +00:00
// TODO: support tcp sockets
2018-07-16 21:39:25 +00:00
// lets determine the request module to use
const requestModule = (() => {
2019-09-08 15:47:30 +00:00
switch (true) {
case parsedUrl.protocol === 'https:' && optionsArg.keepAlive:
optionsArg.agent = httpsAgent;
return plugins.https;
2019-09-29 14:42:56 +00:00
case parsedUrl.protocol === 'https:' && !optionsArg.keepAlive:
2019-09-08 15:47:30 +00:00
optionsArg.agent = httpsAgentKeepAliveFalse;
return plugins.https;
case parsedUrl.protocol === 'http:' && optionsArg.keepAlive:
optionsArg.agent = httpAgent;
return plugins.http;
2019-09-29 14:42:56 +00:00
case parsedUrl.protocol === 'http:' && !optionsArg.keepAlive:
2019-09-08 15:47:30 +00:00
optionsArg.agent = httpAgentKeepAliveFalse;
return plugins.http;
}
2018-07-16 21:39:25 +00:00
})() as typeof plugins.https;
2020-09-29 15:20:40 +00:00
if (!requestModule) {
2020-09-29 15:22:25 +00:00
console.error(`The request to ${urlArg} is missing a viable protocol. Must be http or https`);
2020-09-29 15:20:40 +00:00
return;
}
2018-07-16 21:39:25 +00:00
// lets perform the actual request
2020-08-24 12:01:38 +00:00
const requestToFire = requestModule.request(optionsArg, async (response) => {
2019-09-28 22:56:56 +00:00
if (responseStreamArg) {
2019-09-28 19:33:13 +00:00
done.resolve(response);
} else {
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
done.resolve(builtResponse);
}
});
2018-07-16 21:39:25 +00:00
// lets write the requestBody
if (optionsArg.requestBody) {
2019-09-28 19:33:13 +00:00
if (optionsArg.requestBody instanceof plugins.formData) {
2022-02-15 17:53:02 +00:00
optionsArg.requestBody.pipe(requestToFire).on('finish', (event: any) => {
2019-09-28 19:33:13 +00:00
requestToFire.end();
});
} else {
2018-08-13 23:47:54 +00:00
if (typeof optionsArg.requestBody !== 'string') {
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
}
2019-08-22 10:38:11 +00:00
requestToFire.write(optionsArg.requestBody);
requestToFire.end();
2017-01-28 23:51:47 +00:00
}
2019-09-28 22:56:56 +00:00
} else if (requestDataFunc) {
requestDataFunc(requestToFire);
2018-07-19 21:22:11 +00:00
} else {
2019-08-22 10:38:11 +00:00
requestToFire.end();
2017-06-05 17:09:40 +00:00
}
2018-07-19 14:16:02 +00:00
// lets handle an error
2020-08-24 12:01:38 +00:00
requestToFire.on('error', (e) => {
2018-07-16 21:39:25 +00:00
console.error(e);
});
2018-07-19 14:16:02 +00:00
2018-07-16 21:39:25 +00:00
const result = await done.promise;
return result;
2018-06-13 20:34:49 +00:00
};