Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d2bbcae2a | |||
deb25a3068 | |||
0a83d8b476 | |||
8e7c730d86 | |||
fe50adb1ff | |||
cd75f7acd8 | |||
bb0dd6426a | |||
d471376681 | |||
b882922f2b | |||
9a0d35c325 | |||
7b49bba0d2 | |||
6600a23a00 | |||
e2845c9992 | |||
5e6f2c6fbf | |||
d3d0649b73 | |||
fba43df3c4 | |||
c6fa540543 | |||
1891b54389 | |||
fee8443af1 | |||
c48f956ae3 | |||
4a4b64a2c4 | |||
43d4b47782 | |||
6d970cb925 | |||
43710c930e | |||
306dd7c366 | |||
3d69d97891 | |||
a6d52702fd | |||
de31ee6093 | |||
cd2d7b2525 | |||
2d4a75c9cd |
2641
package-lock.json
generated
2641
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartrequest",
|
||||
"version": "2.0.0",
|
||||
"version": "2.0.15",
|
||||
"private": false,
|
||||
"description": "dropin replacement for request",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -25,19 +25,17 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.1.7",
|
||||
"@pushrocks/smarturl": "^3.0.2",
|
||||
"agentkeepalive": "^4.2.1",
|
||||
"@pushrocks/smartpromise": "^4.0.2",
|
||||
"@pushrocks/smarturl": "^3.0.6",
|
||||
"agentkeepalive": "^4.3.0",
|
||||
"form-data": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.63",
|
||||
"@gitzone/tsrun": "^1.2.37",
|
||||
"@gitzone/tstest": "^1.0.72",
|
||||
"@gitzone/tsbuild": "^2.1.65",
|
||||
"@gitzone/tsrun": "^1.2.39",
|
||||
"@gitzone/tstest": "^1.0.74",
|
||||
"@pushrocks/tapbundle": "^5.0.4",
|
||||
"@types/node": "^18.6.2",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
"@types/node": "^18.15.11"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
|
4431
pnpm-lock.yaml
generated
Normal file
4431
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -13,13 +13,18 @@ tap.test('should request a JSON document over https', async () => {
|
||||
.toEqual(1);
|
||||
});
|
||||
|
||||
tap.skip.test('should post a JSON document over http', async () => {
|
||||
tap.test('should post a JSON document over http', async () => {
|
||||
await expectAsync(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
|
||||
.property('body')
|
||||
.property('md5')
|
||||
.toEqual('fa4c6baa0812e5b5c80ed8885e55a8a6');
|
||||
});
|
||||
|
||||
tap.test('should safe get stuff', async () => {
|
||||
smartrequest.safeGet('http://coffee.link/');
|
||||
smartrequest.safeGet('https://coffee.link/');
|
||||
});
|
||||
|
||||
tap.skip.test('should deal with unix socks', async () => {
|
||||
const socketResponse = await smartrequest.request(
|
||||
'http://unix:/var/run/docker.sock:/containers/json',
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartrequest',
|
||||
version: '2.0.0',
|
||||
version: '2.0.15',
|
||||
description: 'dropin replacement for request'
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
export { request } from './smartrequest.request.js';
|
||||
export { request, safeGet } from './smartrequest.request.js';
|
||||
export type { IExtendedIncomingMessage } from './smartrequest.request.js';
|
||||
export type { ISmartRequestOptions } from './smartrequest.interfaces.js';
|
||||
|
||||
|
@ -6,4 +6,5 @@ export interface ISmartRequestOptions extends https.RequestOptions {
|
||||
requestBody?: any;
|
||||
autoJsonParse?: boolean;
|
||||
queryParams?: { [key: string]: string };
|
||||
hardDataCuttingTimeout?: number;
|
||||
}
|
||||
|
@ -59,33 +59,37 @@ const parseSocketPathAndRoute = (stringToParseArg: string) => {
|
||||
*/
|
||||
const httpAgent = new plugins.agentkeepalive({
|
||||
keepAlive: true,
|
||||
freeSocketTimeout: 4000,
|
||||
maxFreeSockets: 1,
|
||||
maxFreeSockets: 10,
|
||||
maxSockets: 100,
|
||||
maxTotalSockets: 1000,
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
/**
|
||||
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||
*/
|
||||
const httpAgentKeepAliveFalse = new plugins.agentkeepalive({
|
||||
maxFreeSockets: 0,
|
||||
keepAlive: false,
|
||||
keepAliveMsecs: 0,
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
/**
|
||||
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||
*/
|
||||
const httpsAgent = new plugins.agentkeepalive.HttpsAgent();
|
||||
const httpsAgent = new plugins.agentkeepalive.HttpsAgent({
|
||||
keepAlive: true,
|
||||
maxFreeSockets: 10,
|
||||
maxSockets: 100,
|
||||
maxTotalSockets: 1000,
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
/**
|
||||
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||
*/
|
||||
const httpsAgentKeepAliveFalse = new plugins.https.Agent({
|
||||
maxFreeSockets: 0,
|
||||
const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
|
||||
keepAlive: false,
|
||||
keepAliveMsecs: 0,
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
export let request = async (
|
||||
@ -94,7 +98,7 @@ export let request = async (
|
||||
responseStreamArg: boolean = false,
|
||||
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
|
||||
): Promise<IExtendedIncomingMessage> => {
|
||||
const done = plugins.smartpromise.defer<any>();
|
||||
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
|
||||
|
||||
// merge options
|
||||
const defaultOptions: interfaces.ISmartRequestOptions = {
|
||||
@ -128,20 +132,30 @@ export let request = async (
|
||||
|
||||
// TODO: support tcp sockets
|
||||
|
||||
// lets determine agent
|
||||
switch (true) {
|
||||
case !!optionsArg.agent:
|
||||
break;
|
||||
case parsedUrl.protocol === 'https:' && optionsArg.keepAlive:
|
||||
optionsArg.agent = httpsAgent;
|
||||
break;
|
||||
case parsedUrl.protocol === 'https:' && !optionsArg.keepAlive:
|
||||
optionsArg.agent = httpsAgentKeepAliveFalse;
|
||||
break;
|
||||
case parsedUrl.protocol === 'http:' && optionsArg.keepAlive:
|
||||
optionsArg.agent = httpAgent;
|
||||
break;
|
||||
case parsedUrl.protocol === 'http:' && !optionsArg.keepAlive:
|
||||
optionsArg.agent = httpAgentKeepAliveFalse;
|
||||
break;
|
||||
}
|
||||
|
||||
// lets determine the request module to use
|
||||
const requestModule = (() => {
|
||||
switch (true) {
|
||||
case parsedUrl.protocol === 'https' && optionsArg.keepAlive:
|
||||
optionsArg.agent = httpsAgent;
|
||||
case parsedUrl.protocol === 'https:':
|
||||
return plugins.https;
|
||||
case parsedUrl.protocol === 'https' && !optionsArg.keepAlive:
|
||||
optionsArg.agent = httpsAgentKeepAliveFalse;
|
||||
return plugins.https;
|
||||
case parsedUrl.protocol === 'http' && optionsArg.keepAlive:
|
||||
optionsArg.agent = httpAgent;
|
||||
return plugins.http;
|
||||
case parsedUrl.protocol === 'http' && !optionsArg.keepAlive:
|
||||
optionsArg.agent = httpAgentKeepAliveFalse;
|
||||
case parsedUrl.protocol === 'http:':
|
||||
return plugins.http;
|
||||
}
|
||||
})() as typeof plugins.https;
|
||||
@ -152,11 +166,18 @@ export let request = async (
|
||||
}
|
||||
|
||||
// lets perform the actual request
|
||||
const requestToFire = requestModule.request(optionsArg, async (response) => {
|
||||
const requestToFire = requestModule.request(optionsArg, async (resArg) => {
|
||||
if (optionsArg.hardDataCuttingTimeout) {
|
||||
setTimeout(() => {
|
||||
resArg.destroy();
|
||||
done.reject(new Error('Request timed out'));
|
||||
}, optionsArg.hardDataCuttingTimeout)
|
||||
}
|
||||
|
||||
if (responseStreamArg) {
|
||||
done.resolve(response);
|
||||
done.resolve(resArg as IExtendedIncomingMessage);
|
||||
} else {
|
||||
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
||||
const builtResponse = await buildUtf8Response(resArg, optionsArg.autoJsonParse);
|
||||
done.resolve(builtResponse);
|
||||
}
|
||||
});
|
||||
@ -183,8 +204,30 @@ export let request = async (
|
||||
// lets handle an error
|
||||
requestToFire.on('error', (e) => {
|
||||
console.error(e);
|
||||
requestToFire.destroy();
|
||||
});
|
||||
|
||||
const result = await done.promise;
|
||||
return result;
|
||||
const response = await done.promise;
|
||||
response.on('error', (err) => {
|
||||
console.log(err);
|
||||
response.destroy();
|
||||
});
|
||||
return response;
|
||||
};
|
||||
|
||||
export const safeGet = async (urlArg: string) => {
|
||||
const agentToUse = urlArg.startsWith('http://') ? new plugins.http.Agent() : new plugins.https.Agent();
|
||||
try {
|
||||
const response = await request(urlArg, {
|
||||
method: 'GET',
|
||||
agent: agentToUse,
|
||||
timeout: 5000,
|
||||
hardDataCuttingTimeout: 5000,
|
||||
autoJsonParse: false,
|
||||
});
|
||||
return response;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user