fix(core): update

This commit is contained in:
Philipp Kunz 2023-04-19 04:07:44 +02:00
parent b882922f2b
commit d471376681
4 changed files with 4477 additions and 21 deletions

View File

@ -25,17 +25,17 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartrequest#README", "homepage": "https://gitlab.com/pushrocks/smartrequest#README",
"dependencies": { "dependencies": {
"@pushrocks/smartpromise": "^3.1.7", "@pushrocks/smartpromise": "^4.0.2",
"@pushrocks/smarturl": "^3.0.5", "@pushrocks/smarturl": "^3.0.6",
"agentkeepalive": "^4.2.1", "agentkeepalive": "^4.3.0",
"form-data": "^4.0.0" "form-data": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.65", "@gitzone/tsbuild": "^2.1.65",
"@gitzone/tsrun": "^1.2.37", "@gitzone/tsrun": "^1.2.39",
"@gitzone/tstest": "^1.0.73", "@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^5.0.4", "@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.7.8" "@types/node": "^18.15.11"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

4431
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartrequest', name: '@pushrocks/smartrequest',
version: '2.0.11', version: '2.0.12',
description: 'dropin replacement for request' description: 'dropin replacement for request'
} }

View File

@ -70,7 +70,7 @@ const httpAgent = new plugins.agentkeepalive({
*/ */
const httpAgentKeepAliveFalse = new plugins.agentkeepalive({ const httpAgentKeepAliveFalse = new plugins.agentkeepalive({
keepAlive: false, keepAlive: false,
timeout: 60000 timeout: 60000,
}); });
/** /**
@ -81,7 +81,7 @@ const httpsAgent = new plugins.agentkeepalive.HttpsAgent({
maxFreeSockets: 10, maxFreeSockets: 10,
maxSockets: 100, maxSockets: 100,
maxTotalSockets: 1000, maxTotalSockets: 1000,
timeout: 60000 timeout: 60000,
}); });
/** /**
@ -89,7 +89,7 @@ const httpsAgent = new plugins.agentkeepalive.HttpsAgent({
*/ */
const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({ const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
keepAlive: false, keepAlive: false,
timeout: 60000 timeout: 60000,
}); });
export let request = async ( export let request = async (
@ -132,20 +132,30 @@ export let request = async (
// TODO: support tcp sockets // 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 // lets determine the request module to use
const requestModule = (() => { const requestModule = (() => {
switch (true) { switch (true) {
case parsedUrl.protocol === 'https:' && optionsArg.keepAlive: case parsedUrl.protocol === 'https:':
optionsArg.agent = httpsAgent;
return plugins.https; return plugins.https;
case parsedUrl.protocol === 'https:' && !optionsArg.keepAlive: case parsedUrl.protocol === 'http:':
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;
return plugins.http; return plugins.http;
} }
})() as typeof plugins.https; })() as typeof plugins.https;
@ -194,6 +204,21 @@ export let request = async (
response.on('error', (err) => { response.on('error', (err) => {
console.log(err); console.log(err);
response.destroy(); response.destroy();
}) });
return response; return response;
}; };
export const safeGet = async (urlArg: string) => {
try {
const response = await request(urlArg, {
method: 'GET',
agent: plugins.http.globalAgent,
timeout: 5000,
autoJsonParse: false,
});
return response;
} catch (err) {
console.log(err);
return null;
}
};