Compare commits

...

20 Commits

Author SHA1 Message Date
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
d204059313 1.1.51 2020-09-29 15:22:25 +00:00
00210566d5 fix(core): update 2020-09-29 15:22:25 +00:00
14245b2521 1.1.50 2020-09-29 15:20:41 +00:00
f0fa91e2db fix(core): update 2020-09-29 15:20:40 +00:00
19a1fe1524 1.1.49 2020-08-24 12:04:11 +00:00
27770a8ad1 fix(core): update 2020-08-24 12:04:10 +00:00
ab48f11e83 1.1.48 2020-08-24 12:01:39 +00:00
a0a9e3f824 fix(core): update 2020-08-24 12:01:38 +00:00
c829b06169 1.1.47 2020-01-13 08:00:40 +00:00
80fa40baf4 fix(core): update 2020-01-13 08:00:39 +00:00
3659b80e1e 1.1.46 2020-01-13 07:58:54 +00:00
770e7d46ea fix(core): update 2020-01-13 07:58:54 +00:00
2a46f2a306 1.1.45 2020-01-12 19:36:58 +00:00
eae4d09664 fix(core): update 2020-01-12 19:36:58 +00:00
23a2f597fc 1.1.44 2020-01-12 19:32:20 +00:00
c278249c32 fix(core): update 2020-01-12 19:32:20 +00:00
15 changed files with 26659 additions and 894 deletions

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -19,22 +19,35 @@ mirror:
stage: security
script:
- npmci git mirror
only:
- tags
tags:
- lossless
- docker
- notpriv
snyk:
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command snyk test
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- lossless
- docker
- notpriv
allow_failure: true
# ====================
# test stage
@ -49,9 +62,7 @@ testStable:
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- docker
- priv
testBuild:
stage: test
@ -62,9 +73,7 @@ testBuild:
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- lossless
- docker
- notpriv
release:
stage: release
@ -84,6 +93,8 @@ release:
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"request": "launch",
"args": [
"${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"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -7,6 +7,7 @@
"npmAccessLevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",

27157
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
{
"name": "@pushrocks/smartrequest",
"version": "1.1.43",
"version": "1.1.53",
"private": false,
"description": "dropin replacement for request",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild)"
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)"
},
"repository": {
"type": "git",
@ -23,29 +23,33 @@
},
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
"dependencies": {
"@pushrocks/smartpromise": "^3.0.5",
"@types/form-data": "^2.5.0",
"agentkeepalive": "^4.0.2",
"form-data": "^2.5.1"
"@pushrocks/smartpromise": "^3.1.6",
"@pushrocks/smarturl": "^2.0.1",
"agentkeepalive": "^4.2.0",
"form-data": "^4.0.0"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^12.7.8",
"tslint": "^5.20.0",
"@gitzone/tsbuild": "^2.1.29",
"@gitzone/tsrun": "^1.2.18",
"@gitzone/tstest": "^1.0.64",
"@pushrocks/tapbundle": "^4.0.7",
"@types/node": "^17.0.18",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
},
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_web/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
}

96
readme.md Normal file
View File

@ -0,0 +1,96 @@
# @pushrocks/smartrequest
dropin replacement for request
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartrequest)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartrequest)
* [github.com (source mirror)](https://github.com/pushrocks/smartrequest)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartrequest/)
## Status for master
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartrequest/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartrequest/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartrequest)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartrequest)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartrequest)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartrequest)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartrequest)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
## Usage
Use TypeScript for best in class instellisense.
### Features
- supports http
- supports https
- supports unix socks
- supports formData
- supports file uploads
- supports best practice keepAlive
- dedicated functions for working with JSON request/response cycles
- written in TypeScript
- continuously updated
- uses node native http and https modules
- used in modules like @pushrocks/smartproxy and @apiglobal/typedrequest
- commercial support available at [https://lossless.support](https://lossless.support)
> note: smartrequest uses the **native** node http/https modules under the hood (not the bloated one from npm)
```javascript
import * as smartrequest from 'smartrequest'
// simple post
const options: smartrequest.ISmartRequestOptions = { // typed options
headers: {
"Content-Type": "application/json"
"Authorization": "Bearer token"
},
requestBody: JSON.stringify({
key1: 'value1',
key2: 3
})
}
smartrequest.request('https://example.com', options).then(res => {
console.log(res.status)
console.log(res.body) // if json, body will be parsed automatically
}).catch(err => {
console.log(err)
})
// dedicated JSON methods are available:
smartrequest.getJson(...)
smartrequest.postJson(...)
smartrequest.putJson(...)
smartrequest.delJson(...)
// streaming
smartrequest.get('https://example.com/bigfile.mp4', optionsArg, true).then(res => { // third arg = true signals streaming
console.log(res.status)
res.on('data', data => {
// do something with the data chunk here
}
res.on('end', () => {
// do something when things have ended
})
})
```
## Contribution
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

@ -1,31 +1,23 @@
import { tap, expect } from '@pushrocks/tapbundle';
import { tap, expect, expectAsync } from '@pushrocks/tapbundle';
import * as smartrequest from '../ts/index';
tap.test('should request a html document over https', async () => {
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');
await expect(smartrequest.getJson('https://encrypted.google.com/'))
.to.eventually.property('body')
.be.a('string');
await expectAsync(smartrequest.getJson('https://encrypted.google.com/')).toHaveProperty('body')
});
tap.test('should request a JSON document over https', async () => {
await expect(smartrequest.getJson('https://jsonplaceholder.typicode.com/posts/1'))
.to.eventually.property('body')
await expectAsync(smartrequest.getJson('https://jsonplaceholder.typicode.com/posts/1'))
.property('body')
.property('id')
.equal(1);
.toEqual(1);
});
tap.skip.test('should post a JSON document over http', async () => {
await expect(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
.to.eventually.property('body')
await expectAsync(smartrequest.postJson('http://md5.jsontest.com/?text=example_text'))
.property('body')
.property('md5')
.equal('fa4c6baa0812e5b5c80ed8885e55a8a6');
.toEqual('fa4c6baa0812e5b5c80ed8885e55a8a6');
});
tap.skip.test('should deal with unix socks', async () => {
@ -34,8 +26,8 @@ tap.skip.test('should deal with unix socks', async () => {
{
headers: {
'Content-Type': 'application/json',
Host: 'docker.sock'
}
Host: 'docker.sock',
},
}
);
console.log(socketResponse.body);

View File

@ -8,12 +8,16 @@ export const getBinary = async (
domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {}
) => {
optionsArg = {
...optionsArg,
autoJsonParse: false,
};
const done = plugins.smartpromise.defer();
const response = await request(domainArg, optionsArg, true);
const data = [];
const data: Array<Buffer> = [];
response
.on('data', function(chunk) {
.on('data', function (chunk: Buffer) {
data.push(chunk);
})
.on('end', function () {
@ -21,7 +25,7 @@ export const getBinary = async (
//so Buffer.concat() can make us a new Buffer
//of all of them together
const buffer = Buffer.concat(data);
response.body = buffer.toString('binary');
response.body = buffer;
done.resolve();
});
await done.promise;

View File

@ -10,6 +10,7 @@ export interface IFormField {
type: 'string' | 'filePath' | 'Buffer';
payload: string | Buffer;
fileName?: string;
contentType?: string;
}
const appendFormField = async (formDataArg: plugins.formData, formDataField: IFormField) => {
@ -30,13 +31,13 @@ const appendFormField = async (formDataArg: plugins.formData, formDataField: IFo
);
formDataArg.append('file', fileData, {
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
contentType: 'application/pdf'
contentType: 'application/pdf',
});
break;
case 'Buffer':
formDataArg.append('file', formDataField.payload, {
formDataArg.append(formDataField.name, formDataField.payload, {
filename: formDataField.fileName ? formDataField.fileName : 'upload.pdf',
contentType: 'application/pdf'
contentType: formDataField.contentType ? formDataField.contentType : 'application/pdf',
});
break;
}
@ -56,12 +57,41 @@ export const postFormData = async (
method: 'POST',
headers: {
...optionsArg.headers,
...form.getHeaders()
...form.getHeaders(),
},
requestBody: form
requestBody: form,
};
// lets fire the actual request for sending the formdata
const response = await request(urlArg, requestOptions);
return response;
};
export const postFormDataUrlEncoded = async (
urlArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
payloadArg: {key: string, content: string}[]
) => {
const requestOptions = {
...optionsArg,
method: 'POST',
headers: {
...optionsArg.headers,
'content-type': 'application/x-www-form-urlencoded'
}
};
let resultString = '';
for (const keyContentPair of payloadArg) {
if (resultString) {
resultString += '&';
}
resultString += `${encodeURIComponent(keyContentPair.key)}=${encodeURIComponent(keyContentPair.content)}`;
}
// lets fire the actual request for sending the formdata
const response = await request(urlArg + resultString, requestOptions);
return response;
};

View File

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

View File

@ -14,7 +14,7 @@ export const getJson = async (
) => {
optionsArg.method = 'GET';
optionsArg.headers = {
...optionsArg.headers
...optionsArg.headers,
};
let response = await request(domainArg, optionsArg);
return response;
@ -37,7 +37,7 @@ export const postJson = async (
// assign the right Content-Type, leaving all other headers in place
optionsArg.headers = {
...optionsArg.headers,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
};
}
let response = await request(domainArg, optionsArg);

View File

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

View File

@ -14,7 +14,7 @@ const buildUtf8Response = (
const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
// Continuously update stream with data
let body = '';
incomingMessageArg.on('data', chunkArg => {
incomingMessageArg.on('data', (chunkArg) => {
body += chunkArg;
});
@ -50,14 +50,14 @@ const parseSocketPathAndRoute = (stringToParseArg: string) => {
const result = parseRegex.exec(stringToParseArg);
return {
socketPath: result[1],
path: result[2]
path: result[2],
};
};
/**
* 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();
/**
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
@ -65,7 +65,7 @@ const httpAgent = new plugins.agentkeepalive.default();
const httpAgentKeepAliveFalse = new plugins.http.Agent({
maxFreeSockets: 0,
keepAlive: false,
keepAliveMsecs: 0
keepAliveMsecs: 0,
});
/**
@ -79,11 +79,11 @@ const httpsAgent = new plugins.agentkeepalive.HttpsAgent();
const httpsAgentKeepAliveFalse = new plugins.https.Agent({
maxFreeSockets: 0,
keepAlive: false,
keepAliveMsecs: 0
keepAliveMsecs: 0,
});
export let request = async (
domainArg: string,
urlArg: string,
optionsArg: interfaces.ISmartRequestOptions = {},
responseStreamArg: boolean = false,
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
@ -94,24 +94,27 @@ export let request = async (
const defaultOptions: interfaces.ISmartRequestOptions = {
// agent: agent,
autoJsonParse: true,
keepAlive: true
keepAlive: true,
};
optionsArg = {
...defaultOptions,
...optionsArg
...optionsArg,
};
// parse url
const parsedUrl = plugins.url.parse(domainArg);
const parsedUrl = plugins.smarturl.Smarturl.createFromUrl(urlArg, {
searchParams: optionsArg.queryParams || {}
});
optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) {
optionsArg.port = parseInt(parsedUrl.port, 10);
}
optionsArg.path = parsedUrl.path;
optionsArg.queryParams = parsedUrl.searchParams;
// determine if unixsock
if (testForUnixSock(domainArg)) {
if (testForUnixSock(urlArg)) {
const detailedUnixPath = parseSocketPathAndRoute(optionsArg.path);
optionsArg.socketPath = detailedUnixPath.socketPath;
optionsArg.path = detailedUnixPath.path;
@ -137,8 +140,13 @@ export let request = async (
}
})() as typeof plugins.https;
if (!requestModule) {
console.error(`The request to ${urlArg} is missing a viable protocol. Must be http or https`);
return;
}
// lets perform the actual request
const requestToFire = requestModule.request(optionsArg, async response => {
const requestToFire = requestModule.request(optionsArg, async (response) => {
if (responseStreamArg) {
done.resolve(response);
} else {
@ -150,7 +158,7 @@ export let request = async (
// lets write the requestBody
if (optionsArg.requestBody) {
if (optionsArg.requestBody instanceof plugins.formData) {
optionsArg.requestBody.pipe(requestToFire).on('finish', event => {
optionsArg.requestBody.pipe(requestToFire).on('finish', (event: any) => {
requestToFire.end();
});
} else {
@ -167,7 +175,7 @@ export let request = async (
}
// lets handle an error
requestToFire.on('error', e => {
requestToFire.on('error', (e) => {
console.error(e);
});