Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
29ee46b3a2 | |||
18dd110d4e | |||
fa93f13306 | |||
81694cf58c | |||
fdd1c7cdb3 | |||
40f330791f | |||
16b4d168db | |||
cc017a9bbf | |||
2adf7e9ee2 | |||
2303b6da7e | |||
4487579bfd | |||
8d2bbcae2a | |||
deb25a3068 | |||
0a83d8b476 | |||
8e7c730d86 | |||
fe50adb1ff | |||
cd75f7acd8 | |||
bb0dd6426a | |||
d471376681 |
@ -10,10 +10,10 @@
|
|||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "push.rocks",
|
||||||
"gitrepo": "smartrequest",
|
"gitrepo": "smartrequest",
|
||||||
"description": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"npmPackagename": "@pushrocks/smartrequest",
|
"npmPackagename": "@push.rocks/smartrequest",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@pushrocks/smartrequest",
|
||||||
"version": "2.0.11",
|
"version": "2.0.20",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@pushrocks/smartrequest",
|
||||||
"version": "2.0.11",
|
"version": "2.0.20",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.1.7",
|
"@pushrocks/smartpromise": "^3.1.7",
|
||||||
|
16
package.json
16
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@push.rocks/smartrequest",
|
||||||
"version": "2.0.11",
|
"version": "2.0.20",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -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",
|
"@push.rocks/smartpromise": "^4.0.2",
|
||||||
"@pushrocks/smarturl": "^3.0.5",
|
"@push.rocks/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/**/*",
|
||||||
|
4662
pnpm-lock.yaml
generated
Normal file
4662
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);
|
.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'))
|
await expectAsync(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
|
||||||
.property('body')
|
.property('body')
|
||||||
.property('md5')
|
.property('md5')
|
||||||
.toEqual('fa4c6baa0812e5b5c80ed8885e55a8a6');
|
.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 () => {
|
tap.skip.test('should deal with unix socks', async () => {
|
||||||
const socketResponse = await smartrequest.request(
|
const socketResponse = await smartrequest.request(
|
||||||
'http://unix:/var/run/docker.sock:/containers/json',
|
'http://unix:/var/run/docker.sock:/containers/json',
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* autocreated commitinfo by @pushrocks/commitinfo
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/smartrequest',
|
name: '@push.rocks/smartrequest',
|
||||||
version: '2.0.11',
|
version: '2.0.20',
|
||||||
description: 'dropin replacement for request'
|
description: 'dropin replacement for request'
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
export { request } from './smartrequest.request.js';
|
export { request, safeGet } from './smartrequest.request.js';
|
||||||
export type { IExtendedIncomingMessage } from './smartrequest.request.js';
|
export type { IExtendedIncomingMessage } from './smartrequest.request.js';
|
||||||
export type { ISmartRequestOptions } from './smartrequest.interfaces.js';
|
export type { ISmartRequestOptions } from './smartrequest.interfaces.js';
|
||||||
|
|
||||||
export * from './smartrequest.jsonrest.js';
|
export * from './smartrequest.jsonrest.js';
|
||||||
export * from './smartrequest.binaryrest.js';
|
export * from './smartrequest.binaryrest.js';
|
||||||
export * from './smartrequest.formdata.js';
|
export * from './smartrequest.formdata.js';
|
||||||
|
export * from './smartrequest.stream.js';
|
||||||
|
@ -6,4 +6,5 @@ export interface ISmartRequestOptions extends https.RequestOptions {
|
|||||||
requestBody?: any;
|
requestBody?: any;
|
||||||
autoJsonParse?: boolean;
|
autoJsonParse?: boolean;
|
||||||
queryParams?: { [key: string]: string };
|
queryParams?: { [key: string]: string };
|
||||||
|
hardDataCuttingTimeout?: number;
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@ import * as path from 'path';
|
|||||||
export { http, https, fs, path };
|
export { http, https, fs, path };
|
||||||
|
|
||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@push.rocks/smartpromise';
|
||||||
import * as smarturl from '@pushrocks/smarturl';
|
import * as smarturl from '@push.rocks/smarturl';
|
||||||
|
|
||||||
export { smartpromise, smarturl };
|
export { smartpromise, smarturl };
|
||||||
|
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
import * as plugins from './smartrequest.plugins.js';
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
import * as interfaces from './smartrequest.interfaces.js';
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
|
|
||||||
import { IncomingMessage } from 'http';
|
export interface IExtendedIncomingMessage extends plugins.http.IncomingMessage {
|
||||||
|
|
||||||
export interface IExtendedIncomingMessage extends IncomingMessage {
|
|
||||||
body: any;
|
body: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
const buildUtf8Response = (
|
const buildUtf8Response = (
|
||||||
incomingMessageArg: IncomingMessage,
|
incomingMessageArg: plugins.http.IncomingMessage,
|
||||||
autoJsonParse = true
|
autoJsonParse = true
|
||||||
): Promise<IExtendedIncomingMessage> => {
|
): Promise<IExtendedIncomingMessage> => {
|
||||||
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
|
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
|
||||||
@ -70,7 +68,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 +79,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 +87,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 +130,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;
|
||||||
@ -156,11 +164,18 @@ export let request = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// lets perform the actual request
|
// 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) {
|
if (responseStreamArg) {
|
||||||
done.resolve(response as IExtendedIncomingMessage);
|
done.resolve(resArg as IExtendedIncomingMessage);
|
||||||
} else {
|
} else {
|
||||||
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
const builtResponse = await buildUtf8Response(resArg, optionsArg.autoJsonParse);
|
||||||
done.resolve(builtResponse);
|
done.resolve(builtResponse);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -194,6 +209,23 @@ 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) => {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
17
ts/smartrequest.stream.ts
Normal file
17
ts/smartrequest.stream.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
|
import { request } from './smartrequest.request.js';
|
||||||
|
|
||||||
|
export const getStream = async (
|
||||||
|
urlArg: string,
|
||||||
|
optionsArg: interfaces.ISmartRequestOptions = {}
|
||||||
|
): Promise<plugins.http.IncomingMessage> => {
|
||||||
|
try {
|
||||||
|
// Call the existing request function with responseStreamArg set to true.
|
||||||
|
const responseStream = await request(urlArg, optionsArg, true);
|
||||||
|
return responseStream;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('An error occurred while getting the stream:', err);
|
||||||
|
throw err; // Rethrow the error to be handled by the caller.
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user