Compare commits

..

13 Commits

Author SHA1 Message Date
2adf7e9ee2 2.0.16 2023-07-12 00:23:26 +02:00
2303b6da7e fix(core): update 2023-07-12 00:23:25 +02:00
4487579bfd switch to new org scheme 2023-07-10 10:17:43 +02:00
8d2bbcae2a 2.0.15 2023-04-19 14:38:28 +02:00
deb25a3068 fix(core): update 2023-04-19 14:38:28 +02:00
0a83d8b476 2.0.14 2023-04-19 14:24:43 +02:00
8e7c730d86 fix(core): update 2023-04-19 14:24:43 +02:00
fe50adb1ff 2.0.13 2023-04-19 14:13:35 +02:00
cd75f7acd8 fix(core): update 2023-04-19 14:13:34 +02:00
bb0dd6426a 2.0.12 2023-04-19 04:07:44 +02:00
d471376681 fix(core): update 2023-04-19 04:07:44 +02:00
b882922f2b 2.0.11 2022-08-21 14:03:18 +02:00
9a0d35c325 fix(core): update 2022-08-21 14:03:18 +02:00
9 changed files with 4534 additions and 47 deletions

View File

@ -10,10 +10,10 @@
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitscope": "push.rocks",
"gitrepo": "smartrequest",
"description": "dropin replacement for request",
"npmPackagename": "@pushrocks/smartrequest",
"npmPackagename": "@push.rocks/smartrequest",
"license": "MIT"
}
}

32
package-lock.json generated
View File

@ -1,16 +1,16 @@
{
"name": "@pushrocks/smartrequest",
"version": "2.0.10",
"version": "2.0.16",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@pushrocks/smartrequest",
"version": "2.0.10",
"version": "2.0.16",
"license": "MIT",
"dependencies": {
"@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smarturl": "^3.0.3",
"@pushrocks/smarturl": "^3.0.5",
"agentkeepalive": "^4.2.1",
"form-data": "^4.0.0"
},
@ -19,7 +19,7 @@
"@gitzone/tsrun": "^1.2.37",
"@gitzone/tstest": "^1.0.73",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.6.4"
"@types/node": "^18.7.8"
}
},
"node_modules/@adobe/helix-fetch": {
@ -1242,9 +1242,9 @@
}
},
"node_modules/@pushrocks/smarturl": {
"version": "3.0.3",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarturl/-/smarturl-3.0.3.tgz",
"integrity": "sha512-vhDve6S8d5mlQuO5Fk41d+MTpu8s11oxMLvX6OVlHHIPPD2PCkFrBihlcVe/PWudbNBBMYS0CC2t9J8iQvM0BA==",
"version": "3.0.5",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarturl/-/smarturl-3.0.5.tgz",
"integrity": "sha512-XKS+GpIOvMhxr855PmO39SOvv/hdhBbLZ45dkAA6uGL3XdW036jAp61nu7qeB3c6FPShCyhJzo2z5x51wW7OwQ==",
"license": "MIT"
},
"node_modules/@pushrocks/smartxml": {
@ -1833,9 +1833,9 @@
"license": "MIT"
},
"node_modules/@types/node": {
"version": "18.6.4",
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-18.6.4.tgz",
"integrity": "sha512-I4BD3L+6AWiUobfxZ49DlU43gtI+FTHSv9pE2Zekg6KjMpre4ByusaljW3vYSLJrvQ1ck1hUaeVu8HVlY3vzHg==",
"version": "18.7.8",
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-18.7.8.tgz",
"integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag==",
"dev": true,
"license": "MIT"
},
@ -9001,9 +9001,9 @@
}
},
"@pushrocks/smarturl": {
"version": "3.0.3",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarturl/-/smarturl-3.0.3.tgz",
"integrity": "sha512-vhDve6S8d5mlQuO5Fk41d+MTpu8s11oxMLvX6OVlHHIPPD2PCkFrBihlcVe/PWudbNBBMYS0CC2t9J8iQvM0BA=="
"version": "3.0.5",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarturl/-/smarturl-3.0.5.tgz",
"integrity": "sha512-XKS+GpIOvMhxr855PmO39SOvv/hdhBbLZ45dkAA6uGL3XdW036jAp61nu7qeB3c6FPShCyhJzo2z5x51wW7OwQ=="
},
"@pushrocks/smartxml": {
"version": "1.0.6",
@ -9524,9 +9524,9 @@
"dev": true
},
"@types/node": {
"version": "18.6.4",
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-18.6.4.tgz",
"integrity": "sha512-I4BD3L+6AWiUobfxZ49DlU43gtI+FTHSv9pE2Zekg6KjMpre4ByusaljW3vYSLJrvQ1ck1hUaeVu8HVlY3vzHg==",
"version": "18.7.8",
"resolved": "https://verdaccio.lossless.one/@types%2fnode/-/node-18.7.8.tgz",
"integrity": "sha512-/YP55EMK2341JkODUb8DM9O0x1SIz2aBvyF33Uf1c76St3VpsMXEIW0nxuKkq/5cxnbz0RD9cfwNZHEAZQD3ag==",
"dev": true
},
"@types/parse5": {

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartrequest",
"version": "2.0.10",
"name": "@push.rocks/smartrequest",
"version": "2.0.16",
"private": false,
"description": "dropin replacement for request",
"main": "dist_ts/index.js",
@ -25,17 +25,17 @@
},
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
"dependencies": {
"@pushrocks/smartpromise": "^3.1.7",
"@pushrocks/smarturl": "^3.0.3",
"agentkeepalive": "^4.2.1",
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smarturl": "^3.0.6",
"agentkeepalive": "^4.3.0",
"form-data": "^4.0.0"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.65",
"@gitzone/tsrun": "^1.2.37",
"@gitzone/tstest": "^1.0.73",
"@gitzone/tsrun": "^1.2.39",
"@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.6.4"
"@types/node": "^18.15.11"
},
"files": [
"ts/**/*",

4447
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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',

View File

@ -2,7 +2,7 @@
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartrequest',
version: '2.0.10',
name: '@push.rocks/smartrequest',
version: '2.0.16',
description: 'dropin replacement for request'
}

View File

@ -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';

View File

@ -6,4 +6,5 @@ export interface ISmartRequestOptions extends https.RequestOptions {
requestBody?: any;
autoJsonParse?: boolean;
queryParams?: { [key: string]: string };
hardDataCuttingTimeout?: number;
}

View File

@ -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;
@ -156,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 as IExtendedIncomingMessage);
done.resolve(resArg as IExtendedIncomingMessage);
} else {
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
const builtResponse = await buildUtf8Response(resArg, optionsArg.autoJsonParse);
done.resolve(builtResponse);
}
});
@ -194,6 +211,23 @@ export let request = async (
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;
}
};