Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
557fec0386 | |||
e803f9e48d | |||
76c714a931 | |||
e8669f0420 |
@ -18,17 +18,6 @@ before_script:
|
|||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
auditProductionDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
@ -100,10 +89,9 @@ codequality:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
@ -123,11 +111,10 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
23721
package-lock.json
generated
23721
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
24
package.json
24
package.json
@ -1,13 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@pushrocks/smartrequest",
|
||||||
"version": "1.1.56",
|
"version": "2.0.13",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild --web)"
|
"build": "(tsbuild --web)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -23,19 +25,17 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.1.6",
|
"@pushrocks/smartpromise": "^4.0.2",
|
||||||
"@pushrocks/smarturl": "^2.0.1",
|
"@pushrocks/smarturl": "^3.0.6",
|
||||||
"agentkeepalive": "^4.2.0",
|
"agentkeepalive": "^4.3.0",
|
||||||
"form-data": "^4.0.0"
|
"form-data": "^4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.29",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tsrun": "^1.2.18",
|
"@gitzone/tsrun": "^1.2.39",
|
||||||
"@gitzone/tstest": "^1.0.64",
|
"@gitzone/tstest": "^1.0.74",
|
||||||
"@pushrocks/tapbundle": "^4.0.7",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^17.0.18",
|
"@types/node": "^18.15.11"
|
||||||
"tslint": "^6.1.3",
|
|
||||||
"tslint-config-prettier": "^1.18.0"
|
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"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
@ -1,6 +1,6 @@
|
|||||||
import { tap, expect, expectAsync } from '@pushrocks/tapbundle';
|
import { tap, expect, expectAsync } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
import * as smartrequest from '../ts/index';
|
import * as smartrequest from '../ts/index.js';
|
||||||
|
|
||||||
tap.test('should request a html document over https', async () => {
|
tap.test('should request a html document over https', async () => {
|
||||||
await expectAsync(smartrequest.getJson('https://encrypted.google.com/')).toHaveProperty('body');
|
await expectAsync(smartrequest.getJson('https://encrypted.google.com/')).toHaveProperty('body');
|
||||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartrequest',
|
||||||
|
version: '2.0.13',
|
||||||
|
description: 'dropin replacement for request'
|
||||||
|
}
|
11
ts/index.ts
11
ts/index.ts
@ -1,6 +1,7 @@
|
|||||||
export { request, IExtendedIncomingMessage } from './smartrequest.request';
|
export { request, safeGet } from './smartrequest.request.js';
|
||||||
export { ISmartRequestOptions } from './smartrequest.interfaces';
|
export type { IExtendedIncomingMessage } from './smartrequest.request.js';
|
||||||
|
export type { ISmartRequestOptions } from './smartrequest.interfaces.js';
|
||||||
|
|
||||||
export * from './smartrequest.jsonrest';
|
export * from './smartrequest.jsonrest.js';
|
||||||
export * from './smartrequest.binaryrest';
|
export * from './smartrequest.binaryrest.js';
|
||||||
export * from './smartrequest.formdata';
|
export * from './smartrequest.formdata.js';
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// this file implements methods to get and post binary data.
|
// this file implements methods to get and post binary data.
|
||||||
import * as interfaces from './smartrequest.interfaces';
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
import { request } from './smartrequest.request';
|
import { request } from './smartrequest.request.js';
|
||||||
|
|
||||||
import * as plugins from './smartrequest.plugins';
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
|
|
||||||
export const getBinary = async (
|
export const getBinary = async (
|
||||||
domainArg: string,
|
domainArg: string,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import * as plugins from './smartrequest.plugins';
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
import * as interfaces from './smartrequest.interfaces';
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
import { request } from './smartrequest.request';
|
import { request } from './smartrequest.request.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the interfae for FormFieldData
|
* the interfae for FormFieldData
|
||||||
@ -72,7 +72,6 @@ export const postFormDataUrlEncoded = async (
|
|||||||
optionsArg: interfaces.ISmartRequestOptions = {},
|
optionsArg: interfaces.ISmartRequestOptions = {},
|
||||||
payloadArg: { key: string; content: string }[]
|
payloadArg: { key: string; content: string }[]
|
||||||
) => {
|
) => {
|
||||||
|
|
||||||
let resultString = '';
|
let resultString = '';
|
||||||
|
|
||||||
for (const keyContentPair of payloadArg) {
|
for (const keyContentPair of payloadArg) {
|
||||||
@ -91,7 +90,7 @@ export const postFormDataUrlEncoded = async (
|
|||||||
...optionsArg.headers,
|
...optionsArg.headers,
|
||||||
'content-type': 'application/x-www-form-urlencoded',
|
'content-type': 'application/x-www-form-urlencoded',
|
||||||
},
|
},
|
||||||
requestBody: resultString
|
requestBody: resultString,
|
||||||
};
|
};
|
||||||
|
|
||||||
// lets fire the actual request for sending the formdata
|
// lets fire the actual request for sending the formdata
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartrequest.plugins';
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
|
|
||||||
export interface ISmartRequestOptions extends https.RequestOptions {
|
export interface ISmartRequestOptions extends https.RequestOptions {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// This file implements methods to get and post JSON in a simple manner.
|
// This file implements methods to get and post JSON in a simple manner.
|
||||||
|
|
||||||
import * as interfaces from './smartrequest.interfaces';
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
import { request } from './smartrequest.request';
|
import { request } from './smartrequest.request.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets Json and puts the right headers + handles response aggregation
|
* gets Json and puts the right headers + handles response aggregation
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './smartrequest.plugins';
|
import * as plugins from './smartrequest.plugins.js';
|
||||||
import * as interfaces from './smartrequest.interfaces';
|
import * as interfaces from './smartrequest.interfaces.js';
|
||||||
|
|
||||||
import { IncomingMessage } from 'http';
|
import { IncomingMessage } from 'http';
|
||||||
|
|
||||||
@ -57,29 +57,39 @@ const parseSocketPathAndRoute = (stringToParseArg: string) => {
|
|||||||
/**
|
/**
|
||||||
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||||
*/
|
*/
|
||||||
const httpAgent = new plugins.agentkeepalive();
|
const httpAgent = new plugins.agentkeepalive({
|
||||||
|
keepAlive: true,
|
||||||
|
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
|
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||||
*/
|
*/
|
||||||
const httpAgentKeepAliveFalse = new plugins.http.Agent({
|
const httpAgentKeepAliveFalse = new plugins.agentkeepalive({
|
||||||
maxFreeSockets: 0,
|
|
||||||
keepAlive: false,
|
keepAlive: false,
|
||||||
keepAliveMsecs: 0,
|
timeout: 60000,
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
* 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
|
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||||
*/
|
*/
|
||||||
const httpsAgentKeepAliveFalse = new plugins.https.Agent({
|
const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
|
||||||
maxFreeSockets: 0,
|
|
||||||
keepAlive: false,
|
keepAlive: false,
|
||||||
keepAliveMsecs: 0,
|
timeout: 60000,
|
||||||
});
|
});
|
||||||
|
|
||||||
export let request = async (
|
export let request = async (
|
||||||
@ -88,7 +98,7 @@ export let request = async (
|
|||||||
responseStreamArg: boolean = false,
|
responseStreamArg: boolean = false,
|
||||||
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
|
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
|
||||||
): Promise<IExtendedIncomingMessage> => {
|
): Promise<IExtendedIncomingMessage> => {
|
||||||
const done = plugins.smartpromise.defer<any>();
|
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
|
||||||
|
|
||||||
// merge options
|
// merge options
|
||||||
const defaultOptions: interfaces.ISmartRequestOptions = {
|
const defaultOptions: interfaces.ISmartRequestOptions = {
|
||||||
@ -122,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;
|
||||||
@ -148,7 +168,7 @@ 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 (response) => {
|
||||||
if (responseStreamArg) {
|
if (responseStreamArg) {
|
||||||
done.resolve(response);
|
done.resolve(response as IExtendedIncomingMessage);
|
||||||
} else {
|
} else {
|
||||||
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
||||||
done.resolve(builtResponse);
|
done.resolve(builtResponse);
|
||||||
@ -177,8 +197,28 @@ export let request = async (
|
|||||||
// lets handle an error
|
// lets handle an error
|
||||||
requestToFire.on('error', (e) => {
|
requestToFire.on('error', (e) => {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
requestToFire.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await done.promise;
|
const response = await done.promise;
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Reference in New Issue
Block a user