Compare commits

...

49 Commits

Author SHA1 Message Date
16b4d168db 2.0.17 2023-07-12 00:24:37 +02:00
cc017a9bbf fix(core): update 2023-07-12 00:24:36 +02:00
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
7b49bba0d2 2.0.10 2022-08-06 22:29:12 +02:00
6600a23a00 fix(core): update 2022-08-06 22:29:12 +02:00
e2845c9992 2.0.9 2022-08-01 17:15:53 +02:00
5e6f2c6fbf fix(core): update 2022-08-01 17:15:52 +02:00
d3d0649b73 2.0.8 2022-08-01 17:10:22 +02:00
fba43df3c4 fix(core): update 2022-08-01 17:10:22 +02:00
c6fa540543 2.0.7 2022-08-01 17:10:11 +02:00
1891b54389 fix(core): update 2022-08-01 17:10:11 +02:00
fee8443af1 2.0.6 2022-07-30 18:43:11 +02:00
c48f956ae3 fix(core): update 2022-07-30 18:43:10 +02:00
4a4b64a2c4 2.0.5 2022-07-30 02:01:20 +02:00
43d4b47782 fix(core): update 2022-07-30 02:01:20 +02:00
6d970cb925 2.0.4 2022-07-30 01:52:04 +02:00
43710c930e fix(core): update 2022-07-30 01:52:04 +02:00
306dd7c366 2.0.3 2022-07-29 15:45:04 +02:00
3d69d97891 fix(core): update 2022-07-29 15:45:04 +02:00
a6d52702fd 2.0.2 2022-07-29 15:43:56 +02:00
de31ee6093 fix(core): update 2022-07-29 15:43:55 +02:00
cd2d7b2525 2.0.1 2022-07-29 15:41:33 +02:00
2d4a75c9cd fix(core): update 2022-07-29 15:41:32 +02:00
557fec0386 2.0.0 2022-07-29 01:20:25 +02:00
e803f9e48d BREAKING CHANGE(core): switch to esm 2022-07-29 01:20:24 +02:00
76c714a931 1.1.57 2022-07-29 01:19:50 +02:00
e8669f0420 fix(core): update 2022-07-29 01:19:50 +02:00
d9e6214a7e 1.1.56 2022-02-15 23:09:15 +01:00
7c4227bfc6 fix(core): update 2022-02-15 23:09:15 +01:00
e55a521395 1.1.55 2022-02-15 19:02:44 +01:00
06fc279caf fix(core): update 2022-02-15 19:02:43 +01:00
e89e317bbc 1.1.54 2022-02-15 18:57:42 +01:00
d182832e47 fix(core): update 2022-02-15 18:57:42 +01:00
92059a50de 1.1.53 2022-02-15 18:53:02 +01:00
db80f2df7e fix(core): update 2022-02-15 18:53:02 +01:00
145505b891 1.1.52 2021-05-16 23:39:26 +00:00
f4f50c6a94 fix(core): update 2021-05-16 23:39:25 +00:00
17 changed files with 15886 additions and 8668 deletions

View File

@ -12,20 +12,12 @@ stages:
- release - release
- metadata - metadata
before_script:
- npm install -g @shipzone/npmci
# ==================== # ====================
# 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
@ -36,6 +28,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production - npmci command npm audit --audit-level=high --only=prod --production
tags: tags:
- docker - docker
allow_failure: true
auditDevDependencies: auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
@ -96,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
@ -119,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

24
.vscode/launch.json vendored
View File

@ -2,28 +2,10 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "current file", "command": "npm test",
"type": "node", "name": "Run npm test",
"request": "launch", "request": "launch",
"args": [ "type": "node-terminal"
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
} }
] ]
} }

View File

@ -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",
"shortDescription": "dropin replacement for request", "description": "dropin replacement for request",
"npmPackagename": "@pushrocks/smartrequest", "npmPackagename": "@push.rocks/smartrequest",
"license": "MIT" "license": "MIT"
} }
} }

19769
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,15 @@
{ {
"name": "@pushrocks/smartrequest", "name": "@push.rocks/smartrequest",
"version": "1.1.51", "version": "2.0.17",
"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,18 +25,17 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartrequest#README", "homepage": "https://gitlab.com/pushrocks/smartrequest#README",
"dependencies": { "dependencies": {
"@pushrocks/smartpromise": "^3.0.6", "@push.rocks/smartpromise": "^4.0.2",
"agentkeepalive": "^4.1.3", "@push.rocks/smarturl": "^3.0.6",
"form-data": "^3.0.0" "agentkeepalive": "^4.3.0",
"form-data": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.65",
"@gitzone/tsrun": "^1.2.12", "@gitzone/tsrun": "^1.2.39",
"@gitzone/tstest": "^1.0.44", "@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^3.2.9", "@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^14.6.0", "@types/node": "^18.15.11"
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

4447
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,28 @@
import { tap, expect } 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 expect(smartrequest.getJson('https://encrypted.google.com/')) await expectAsync(smartrequest.getJson('https://encrypted.google.com/')).toHaveProperty('body');
.to.eventually.property('body')
.be.a('string');
await expect(smartrequest.getJson('https://encrypted.google.com/'))
.to.eventually.property('body')
.be.a('string');
await expect(smartrequest.getJson('https://encrypted.google.com/'))
.to.eventually.property('body')
.be.a('string');
}); });
tap.test('should request a JSON document over https', async () => { tap.test('should request a JSON document over https', async () => {
await expect(smartrequest.getJson('https://jsonplaceholder.typicode.com/posts/1')) await expectAsync(smartrequest.getJson('https://jsonplaceholder.typicode.com/posts/1'))
.to.eventually.property('body') .property('body')
.property('id') .property('id')
.equal(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 expect(smartrequest.postJson('http://md5.jsontest.com/?text=example_text')) await expectAsync(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
.to.eventually.property('body') .property('body')
.property('md5') .property('md5')
.equal('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 () => {

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartrequest',
version: '2.0.17',
description: 'dropin replacement for request'
}

View File

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

View File

@ -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,
@ -14,10 +14,10 @@ export const getBinary = async (
}; };
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const response = await request(domainArg, optionsArg, true); const response = await request(domainArg, optionsArg, true);
const data = []; const data: Array<Buffer> = [];
response response
.on('data', function (chunk) { .on('data', function (chunk: Buffer) {
data.push(chunk); data.push(chunk);
}) })
.on('end', function () { .on('end', function () {

View File

@ -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
@ -66,3 +66,34 @@ export const postFormData = async (
const response = await request(urlArg, requestOptions); const response = await request(urlArg, requestOptions);
return response; return response;
}; };
export const postFormDataUrlEncoded = async (
urlArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
payloadArg: { key: string; content: string }[]
) => {
let resultString = '';
for (const keyContentPair of payloadArg) {
if (resultString) {
resultString += '&';
}
resultString += `${encodeURIComponent(keyContentPair.key)}=${encodeURIComponent(
keyContentPair.content
)}`;
}
const requestOptions: interfaces.ISmartRequestOptions = {
...optionsArg,
method: 'POST',
headers: {
...optionsArg.headers,
'content-type': 'application/x-www-form-urlencoded',
},
requestBody: resultString,
};
// lets fire the actual request for sending the formdata
const response = await request(urlArg, requestOptions);
return response;
};

View File

@ -1,8 +1,10 @@
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 {
keepAlive?: boolean; keepAlive?: boolean;
requestBody?: any; requestBody?: any;
autoJsonParse?: boolean; autoJsonParse?: boolean;
queryParams?: { [key: string]: string };
hardDataCuttingTimeout?: number;
} }

View File

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

View File

@ -1,15 +1,19 @@
import formData from 'form-data'; // node native scope
import * as fs from 'fs'; import * as fs from 'fs';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import * as path from 'path'; import * as path from 'path';
import * as url from 'url';
import * as smartpromise from '@pushrocks/smartpromise'; export { http, https, fs, path };
export { formData, http, https, fs, path, url, smartpromise }; // pushrocks scope
import * as smartpromise from '@push.rocks/smartpromise';
import * as smarturl from '@push.rocks/smarturl';
export { smartpromise, smarturl };
// third party scope // third party scope
import * as agentkeepalive from 'agentkeepalive'; import agentkeepalive from 'agentkeepalive';
import formData from 'form-data';
export { agentkeepalive }; export { agentkeepalive, formData };

View File

@ -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.default(); 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 = {
@ -103,12 +113,15 @@ export let request = async (
}; };
// parse url // parse url
const parsedUrl = plugins.url.parse(urlArg); const parsedUrl = plugins.smarturl.Smarturl.createFromUrl(urlArg, {
searchParams: optionsArg.queryParams || {},
});
optionsArg.hostname = parsedUrl.hostname; optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) { if (parsedUrl.port) {
optionsArg.port = parseInt(parsedUrl.port, 10); optionsArg.port = parseInt(parsedUrl.port, 10);
} }
optionsArg.path = parsedUrl.path; optionsArg.path = parsedUrl.path;
optionsArg.queryParams = parsedUrl.searchParams;
// determine if unixsock // determine if unixsock
if (testForUnixSock(urlArg)) { if (testForUnixSock(urlArg)) {
@ -119,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;
@ -143,11 +166,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); 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);
} }
}); });
@ -155,7 +185,7 @@ export let request = async (
// lets write the requestBody // lets write the requestBody
if (optionsArg.requestBody) { if (optionsArg.requestBody) {
if (optionsArg.requestBody instanceof plugins.formData) { if (optionsArg.requestBody instanceof plugins.formData) {
optionsArg.requestBody.pipe(requestToFire).on('finish', (event) => { optionsArg.requestBody.pipe(requestToFire).on('finish', (event: any) => {
requestToFire.end(); requestToFire.end();
}); });
} else { } else {
@ -174,8 +204,30 @@ 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) => {
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;
}
}; };

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
}
}

View File

@ -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"
}