fix(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartrequest',
|
||||
version: '2.0.11',
|
||||
version: '2.0.12',
|
||||
description: 'dropin replacement for request'
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ const httpAgent = new plugins.agentkeepalive({
|
||||
*/
|
||||
const httpAgentKeepAliveFalse = new plugins.agentkeepalive({
|
||||
keepAlive: false,
|
||||
timeout: 60000
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
/**
|
||||
@ -81,7 +81,7 @@ const httpsAgent = new plugins.agentkeepalive.HttpsAgent({
|
||||
maxFreeSockets: 10,
|
||||
maxSockets: 100,
|
||||
maxTotalSockets: 1000,
|
||||
timeout: 60000
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
/**
|
||||
@ -89,7 +89,7 @@ const httpsAgent = new plugins.agentkeepalive.HttpsAgent({
|
||||
*/
|
||||
const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
|
||||
keepAlive: false,
|
||||
timeout: 60000
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
export let request = async (
|
||||
@ -132,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;
|
||||
@ -194,6 +204,21 @@ export let request = async (
|
||||
response.on('error', (err) => {
|
||||
console.log(err);
|
||||
response.destroy();
|
||||
})
|
||||
});
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user