Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
6e276eda00 | |||
021d26a23a | |||
c9c8a1234c | |||
dffabd905f | |||
36f2707141 | |||
b00d674b6f | |||
b09598d465 | |||
acc7b2d46f | |||
16a97a420c | |||
a73c78e54b |
@ -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
|
||||||
|
748
package-lock.json
generated
748
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrequest",
|
"name": "@pushrocks/smartrequest",
|
||||||
"version": "1.1.23",
|
"version": "1.1.28",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "dropin replacement for request",
|
"description": "dropin replacement for request",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -28,10 +28,10 @@
|
|||||||
"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": "^5.19.0",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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';
|
||||||
|
@ -14,7 +14,7 @@ const buildUtf8Response = (
|
|||||||
const 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', (chunkArg) => {
|
incomingMessageArg.on('data', chunkArg => {
|
||||||
body += chunkArg;
|
body += chunkArg;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -62,6 +62,15 @@ const httpAgent = new plugins.http.Agent({
|
|||||||
keepAliveMsecs: 600000
|
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
|
* a custom https agent to make sure we can set custom keepAlive options for speedy subsequent calls
|
||||||
*/
|
*/
|
||||||
@ -70,6 +79,15 @@ const httpsAgent = new plugins.https.Agent({
|
|||||||
keepAliveMsecs: 600000
|
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 = {},
|
||||||
@ -80,7 +98,8 @@ export let request = async (
|
|||||||
// merge options
|
// merge options
|
||||||
const defaultOptions: interfaces.ISmartRequestOptions = {
|
const defaultOptions: interfaces.ISmartRequestOptions = {
|
||||||
// agent: agent,
|
// agent: agent,
|
||||||
autoJsonParse: true
|
autoJsonParse: true,
|
||||||
|
keepAlive: true
|
||||||
};
|
};
|
||||||
|
|
||||||
optionsArg = {
|
optionsArg = {
|
||||||
@ -107,32 +126,44 @@ export let request = async (
|
|||||||
|
|
||||||
// lets determine the request module to use
|
// lets determine the request module to use
|
||||||
const requestModule = (() => {
|
const requestModule = (() => {
|
||||||
if (parsedUrl.protocol === 'https:') {
|
switch (true) {
|
||||||
optionsArg.agent = httpsAgent;
|
case parsedUrl.protocol === 'https:' && optionsArg.keepAlive:
|
||||||
return plugins.https;
|
optionsArg.agent = httpsAgent;
|
||||||
} else if (parsedUrl.protocol === 'http:') {
|
return plugins.https;
|
||||||
optionsArg.agent = httpAgent;
|
case parsedUrl.protocol === 'https:' && (!optionsArg.keepAlive):
|
||||||
return plugins.http;
|
optionsArg.agent = httpsAgentKeepAliveFalse;
|
||||||
} else {
|
return plugins.https;
|
||||||
throw new Error(`unsupported protocol: ${parsedUrl.protocol}`);
|
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
|
||||||
const requestToFire = requestModule.request(optionsArg);
|
const requestToFire = requestModule.request(optionsArg, async response => {
|
||||||
|
if (streamArg) {
|
||||||
|
done.resolve(response);
|
||||||
|
} else {
|
||||||
|
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
||||||
|
done.resolve(builtResponse);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 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 => {
|
||||||
|
requestToFire.end();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
if (typeof optionsArg.requestBody !== 'string') {
|
if (typeof optionsArg.requestBody !== 'string') {
|
||||||
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
optionsArg.requestBody = JSON.stringify(optionsArg.requestBody);
|
||||||
}
|
}
|
||||||
requestToFire.write(optionsArg.requestBody);
|
requestToFire.write(optionsArg.requestBody);
|
||||||
requestToFire.end();
|
requestToFire.end();
|
||||||
} else if (optionsArg.requestBody instanceof plugins.formData) {
|
|
||||||
optionsArg.requestBody.pipe(requestToFire).on('finish', event => {
|
|
||||||
requestToFire.end();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
requestToFire.end();
|
requestToFire.end();
|
||||||
@ -143,16 +174,6 @@ export let request = async (
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
// lets handle the response
|
|
||||||
requestToFire.on('response', async response => {
|
|
||||||
if (streamArg) {
|
|
||||||
done.resolve(response);
|
|
||||||
} else {
|
|
||||||
const builtResponse = await buildUtf8Response(response, optionsArg.autoJsonParse);
|
|
||||||
done.resolve(builtResponse);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await done.promise;
|
const result = await done.promise;
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user