Compare commits

...

16 Commits

Author SHA1 Message Date
36f2707141 1.1.26 2019-09-08 17:57:28 +02:00
b00d674b6f fix(core): update 2019-09-08 17:57:28 +02:00
b09598d465 1.1.25 2019-09-08 17:49:33 +02:00
acc7b2d46f fix(core): update 2019-09-08 17:49:32 +02:00
16a97a420c 1.1.24 2019-09-08 17:47:31 +02:00
a73c78e54b fix(core): update 2019-09-08 17:47:30 +02:00
1f408b5123 1.1.23 2019-08-22 12:40:19 +02:00
284f4967f4 fix(core): update 2019-08-22 12:40:19 +02:00
55c80c1403 1.1.22 2019-08-22 12:38:55 +02:00
7a3e565dbb fix(core): update 2019-08-22 12:38:55 +02:00
6f5d10ccd3 1.1.21 2019-08-22 12:38:12 +02:00
f1ddab72f6 fix(core): update 2019-08-22 12:38:11 +02:00
376225888c 1.1.20 2019-08-21 12:55:20 +02:00
63e8660f6c fix(core): update 2019-08-21 12:55:19 +02:00
2358b1d48f 1.1.19 2019-08-16 22:00:01 +02:00
9db29bacc2 fix(core): update 2019-08-16 22:00:01 +02:00
6 changed files with 607 additions and 405 deletions

View File

@ -38,11 +38,11 @@ snyk:
# test stage # test stage
# ==================== # ====================
testLTS: testStable:
stage: test stage: test
script: script:
- npmci npm prepare - npmci npm prepare
- npmci node install lts - npmci node install stable
- npmci npm install - npmci npm install
- npmci npm test - npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
@ -100,7 +100,7 @@ trigger:
pages: pages:
image: hosttoday/ht-docker-dbase:npmci image: hosttoday/ht-docker-dbase:npmci
services: services:
- docker:18-dind - docker:stable-dind
stage: metadata stage: metadata
script: script:
- npmci command npm install -g @gitzone/tsdoc - npmci command npm install -g @gitzone/tsdoc

900
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartrequest", "name": "@pushrocks/smartrequest",
"version": "1.1.18", "version": "1.1.26",
"private": false, "private": false,
"description": "dropin replacement for request", "description": "dropin replacement for request",
"main": "dist/index.js", "main": "dist/index.js",
@ -28,11 +28,13 @@
"form-data": "^2.3.3" "form-data": "^2.3.3"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.8", "@gitzone/tsbuild": "^2.1.17",
"@gitzone/tsrun": "^1.2.5", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.20", "@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.9", "@pushrocks/tapbundle": "^3.0.13",
"@types/node": "^11.13.6" "@types/node": "^11.13.6",
"tslint": "^5.19.0",
"tslint-config-prettier": "^1.18.0"
}, },
"files": [ "files": [
"ts/*", "ts/*",

View File

@ -2,6 +2,7 @@ import * as plugins from './smartrequest.plugins';
import * as https from 'https'; import * as https from 'https';
export interface ISmartRequestOptions extends https.RequestOptions { export interface ISmartRequestOptions extends https.RequestOptions {
keepAlive?: boolean;
requestBody?: any; requestBody?: any;
autoJsonParse?: boolean; autoJsonParse?: boolean;
} }

View File

@ -1,4 +1,4 @@
import * as formData from 'form-data'; import formData from 'form-data';
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';

View File

@ -1,4 +1,3 @@
import * as https from 'https';
import * as plugins from './smartrequest.plugins'; import * as plugins from './smartrequest.plugins';
import * as interfaces from './smartrequest.interfaces'; import * as interfaces from './smartrequest.interfaces';
@ -12,14 +11,14 @@ const buildUtf8Response = (
incomingMessageArg: IncomingMessage, incomingMessageArg: IncomingMessage,
autoJsonParse = true autoJsonParse = true
): Promise<IExtendedIncomingMessage> => { ): Promise<IExtendedIncomingMessage> => {
let done = plugins.smartpromise.defer<IExtendedIncomingMessage>(); const done = plugins.smartpromise.defer<IExtendedIncomingMessage>();
// Continuously update stream with data // Continuously update stream with data
let body = ''; let body = '';
incomingMessageArg.on('data', function(chunkArg) { incomingMessageArg.on('data', chunkArg => {
body += chunkArg; body += chunkArg;
}); });
incomingMessageArg.on('end', function() { incomingMessageArg.on('end', () => {
if (autoJsonParse) { if (autoJsonParse) {
try { try {
(incomingMessageArg as IExtendedIncomingMessage).body = JSON.parse(body); (incomingMessageArg as IExtendedIncomingMessage).body = JSON.parse(body);
@ -55,16 +54,52 @@ const parseSocketPathAndRoute = (stringToParseArg: string) => {
}; };
}; };
/**
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpAgent = new plugins.http.Agent({
keepAlive: true,
keepAliveMsecs: 600000
});
/**
* a custom http agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpAgentKeepAliveFalse = new plugins.http.Agent({
maxFreeSockets: 0,
keepAlive: false,
keepAliveMsecs: 0
});
/**
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpsAgent = new plugins.https.Agent({
keepAlive: true,
keepAliveMsecs: 600000
});
/**
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
*/
const httpsAgentKeepAliveFalse = new plugins.https.Agent({
maxFreeSockets: 0,
keepAlive: false,
keepAliveMsecs: 0
});
export let request = async ( export let request = async (
domainArg: string, domainArg: string,
optionsArg: interfaces.ISmartRequestOptions = {}, optionsArg: interfaces.ISmartRequestOptions = {},
streamArg: boolean = false streamArg: boolean = false
): Promise<IExtendedIncomingMessage> => { ): Promise<IExtendedIncomingMessage> => {
let done = plugins.smartpromise.defer<any>(); const done = plugins.smartpromise.defer<any>();
// merge options // merge options
const defaultOptions: interfaces.ISmartRequestOptions = { const defaultOptions: interfaces.ISmartRequestOptions = {
autoJsonParse: true // agent: agent,
autoJsonParse: true,
keepAlive: true
}; };
optionsArg = { optionsArg = {
@ -73,11 +108,10 @@ export let request = async (
}; };
// parse url // parse url
let parsedUrl: plugins.url.Url; const parsedUrl = plugins.url.parse(domainArg);
parsedUrl = plugins.url.parse(domainArg);
optionsArg.hostname = parsedUrl.hostname; optionsArg.hostname = parsedUrl.hostname;
if (parsedUrl.port) { if (parsedUrl.port) {
optionsArg.port = parseInt(parsedUrl.port); optionsArg.port = parseInt(parsedUrl.port, 10);
} }
optionsArg.path = parsedUrl.path; optionsArg.path = parsedUrl.path;
@ -88,19 +122,28 @@ export let request = async (
optionsArg.path = detailedUnixPath.path; optionsArg.path = detailedUnixPath.path;
} }
// TODO: support tcp sockets
// lets determine the request module to use // lets determine the request module to use
const requestModule = (() => { const requestModule = (() => {
if (parsedUrl.protocol === 'https:') { switch (true) {
return plugins.https; case parsedUrl.protocol === 'https:' && optionsArg.keepAlive:
} else if (parsedUrl.protocol === 'http:') { optionsArg.agent = httpsAgent;
return plugins.http; return plugins.https;
} else { case parsedUrl.protocol === 'https:' && !optionsArg.keepAlive:
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`); 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;
} }
})() as typeof plugins.https; })() as typeof plugins.https;
// lets perform the actual request // lets perform the actual request
let request = requestModule.request(optionsArg); const requestToFire = requestModule.request(optionsArg);
// lets write the requestBody // lets write the requestBody
if (optionsArg.requestBody) { if (optionsArg.requestBody) {
@ -108,24 +151,24 @@ export let request = async (
if (typeof optionsArg.requestBody !== 'string') { if (typeof optionsArg.requestBody !== 'string') {
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody); optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
} }
request.write(optionsArg.requestBody); requestToFire.write(optionsArg.requestBody);
request.end(); requestToFire.end();
} else if (optionsArg.requestBody instanceof plugins.formData) { } else if (optionsArg.requestBody instanceof plugins.formData) {
optionsArg.requestBody.pipe(request).on('finish', event => { optionsArg.requestBody.pipe(requestToFire).on('finish', event => {
request.end(); requestToFire.end();
}); });
} }
} else { } else {
request.end(); requestToFire.end();
} }
// lets handle an error // lets handle an error
request.on('error', e => { requestToFire.on('error', e => {
console.error(e); console.error(e);
}); });
// lets handle the response // lets handle the response
request.on('response', async response => { requestToFire.on('response', async response => {
if (streamArg) { if (streamArg) {
done.resolve(response); done.resolve(response);
} else { } else {