Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
d9e6214a7e | |||
7c4227bfc6 | |||
e55a521395 | |||
06fc279caf | |||
e89e317bbc | |||
d182832e47 | |||
92059a50de | |||
db80f2df7e | |||
145505b891 | |||
f4f50c6a94 | |||
d204059313 | |||
00210566d5 | |||
14245b2521 | |||
f0fa91e2db |
@ -12,6 +12,9 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
@ -36,6 +39,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
|
||||||
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -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"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartrequest",
|
"gitrepo": "smartrequest",
|
||||||
"shortDescription": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"npmPackagename": "@pushrocks/smartrequest",
|
"npmPackagename": "@pushrocks/smartrequest",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
24010
package-lock.json
generated
24010
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@pushrocks/smartrequest",
|
||||||
"version": "1.1.49",
|
"version": "1.1.56",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -23,16 +23,17 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
"homepage": "https://gitlab.com/pushrocks/smartrequest#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.0.6",
|
"@pushrocks/smartpromise": "^3.1.6",
|
||||||
"agentkeepalive": "^4.1.3",
|
"@pushrocks/smarturl": "^2.0.1",
|
||||||
"form-data": "^3.0.0"
|
"agentkeepalive": "^4.2.0",
|
||||||
|
"form-data": "^4.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.29",
|
||||||
"@gitzone/tsrun": "^1.2.12",
|
"@gitzone/tsrun": "^1.2.18",
|
||||||
"@gitzone/tstest": "^1.0.44",
|
"@gitzone/tstest": "^1.0.64",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@pushrocks/tapbundle": "^4.0.7",
|
||||||
"@types/node": "^14.6.0",
|
"@types/node": "^17.0.18",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
|
24
test/test.ts
24
test/test.ts
@ -1,31 +1,23 @@
|
|||||||
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';
|
||||||
|
|
||||||
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.skip.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.skip.test('should deal with unix socks', async () => {
|
tap.skip.test('should deal with unix socks', async () => {
|
||||||
|
@ -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 () {
|
||||||
|
@ -66,3 +66,35 @@ 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;
|
||||||
|
};
|
||||||
|
@ -5,4 +5,5 @@ export interface ISmartRequestOptions extends https.RequestOptions {
|
|||||||
keepAlive?: boolean;
|
keepAlive?: boolean;
|
||||||
requestBody?: any;
|
requestBody?: any;
|
||||||
autoJsonParse?: boolean;
|
autoJsonParse?: boolean;
|
||||||
|
queryParams?: { [key: string]: string };
|
||||||
}
|
}
|
||||||
|
@ -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';
|
|
||||||
|
|
||||||
|
export { http, https, fs, path };
|
||||||
|
|
||||||
|
// pushrocks scope
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
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
|
// third party scope
|
||||||
import * as agentkeepalive from 'agentkeepalive';
|
import agentkeepalive from 'agentkeepalive';
|
||||||
|
import formData from 'form-data';
|
||||||
|
|
||||||
export { agentkeepalive };
|
export { agentkeepalive, formData };
|
||||||
|
@ -57,7 +57,7 @@ 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();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@ -83,7 +83,7 @@ const httpsAgentKeepAliveFalse = new plugins.https.Agent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export let request = async (
|
export let request = async (
|
||||||
domainArg: string,
|
urlArg: string,
|
||||||
optionsArg: interfaces.ISmartRequestOptions = {},
|
optionsArg: interfaces.ISmartRequestOptions = {},
|
||||||
responseStreamArg: boolean = false,
|
responseStreamArg: boolean = false,
|
||||||
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
|
requestDataFunc: (req: plugins.http.ClientRequest) => void = null
|
||||||
@ -103,15 +103,18 @@ export let request = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
// parse url
|
// parse url
|
||||||
const parsedUrl = plugins.url.parse(domainArg);
|
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(domainArg)) {
|
if (testForUnixSock(urlArg)) {
|
||||||
const detailedUnixPath = parseSocketPathAndRoute(optionsArg.path);
|
const detailedUnixPath = parseSocketPathAndRoute(optionsArg.path);
|
||||||
optionsArg.socketPath = detailedUnixPath.socketPath;
|
optionsArg.socketPath = detailedUnixPath.socketPath;
|
||||||
optionsArg.path = detailedUnixPath.path;
|
optionsArg.path = detailedUnixPath.path;
|
||||||
@ -137,6 +140,11 @@ export let request = async (
|
|||||||
}
|
}
|
||||||
})() as typeof plugins.https;
|
})() 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
|
// lets perform the actual request
|
||||||
const requestToFire = requestModule.request(optionsArg, async (response) => {
|
const requestToFire = requestModule.request(optionsArg, async (response) => {
|
||||||
if (responseStreamArg) {
|
if (responseStreamArg) {
|
||||||
@ -150,7 +158,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 {
|
||||||
|
Reference in New Issue
Block a user