teeny-request
Advanced tools
Comparing version 6.0.3 to 7.0.0
@@ -18,3 +18,6 @@ /*! | ||
import { Agent as HTTPAgent } from 'http'; | ||
import { Agent as HTTPSAgent } from 'https'; | ||
import { Options } from './'; | ||
export declare const pool: Map<string, HTTPAgent>; | ||
export declare type HttpAnyAgent = HTTPAgent | HTTPSAgent; | ||
/** | ||
@@ -25,5 +28,5 @@ * Returns a custom request Agent if one is found, otherwise returns undefined | ||
* @param {string} uri The request uri | ||
* @param {object} reqOpts The request options | ||
* @returns {Agent|undefined} | ||
* @param {Options} reqOpts The request options | ||
* @returns {HttpAnyAgent|undefined} | ||
*/ | ||
export declare function getAgent(uri: string, reqOpts: Options): HTTPAgent | undefined; | ||
export declare function getAgent(uri: string, reqOpts: Options): HttpAnyAgent | undefined; |
@@ -18,5 +18,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var http_1 = require("http"); | ||
var https_1 = require("https"); | ||
var pool = new Map(); | ||
exports.getAgent = exports.pool = void 0; | ||
const http_1 = require("http"); | ||
const https_1 = require("https"); | ||
// eslint-disable-next-line node/no-deprecated-api | ||
const url_1 = require("url"); | ||
exports.pool = new Map(); | ||
/** | ||
@@ -27,8 +30,8 @@ * Returns a custom request Agent if one is found, otherwise returns undefined | ||
* @param {string} uri The request uri | ||
* @param {object} reqOpts The request options | ||
* @returns {Agent|undefined} | ||
* @param {Options} reqOpts The request options | ||
* @returns {HttpAnyAgent|undefined} | ||
*/ | ||
function getAgent(uri, reqOpts) { | ||
var isHttp = uri.startsWith('http://'); | ||
var proxy = reqOpts.proxy || | ||
const isHttp = uri.startsWith('http://'); | ||
const proxy = reqOpts.proxy || | ||
process.env.HTTP_PROXY || | ||
@@ -38,21 +41,23 @@ process.env.http_proxy || | ||
process.env.https_proxy; | ||
const poolOptions = Object.assign({}, reqOpts.pool); | ||
if (proxy) { | ||
// tslint:disable-next-line variable-name | ||
var Agent = isHttp | ||
const Agent = isHttp | ||
? require('http-proxy-agent') | ||
: require('https-proxy-agent'); | ||
return new Agent(proxy); | ||
const proxyOpts = { ...url_1.parse(proxy), ...poolOptions }; | ||
return new Agent(proxyOpts); | ||
} | ||
var key = isHttp ? 'http' : 'https'; | ||
let key = isHttp ? 'http' : 'https'; | ||
if (reqOpts.forever) { | ||
key += ':forever'; | ||
if (!pool.has(key)) { | ||
if (!exports.pool.has(key)) { | ||
// tslint:disable-next-line variable-name | ||
var Agent = isHttp ? http_1.Agent : https_1.Agent; | ||
pool.set(key, new Agent({ keepAlive: true })); | ||
const Agent = isHttp ? http_1.Agent : https_1.Agent; | ||
exports.pool.set(key, new Agent({ ...poolOptions, keepAlive: true })); | ||
} | ||
} | ||
return pool.get(key); | ||
return exports.pool.get(key); | ||
} | ||
exports.getAgent = getAgent; | ||
//# sourceMappingURL=agents.js.map |
@@ -17,4 +17,6 @@ /*! | ||
/// <reference types="node" /> | ||
import { Agent } from 'https'; | ||
import { Agent, AgentOptions as HttpsAgentOptions } from 'https'; | ||
import { AgentOptions as HttpAgentOptions } from 'http'; | ||
import { PassThrough, Readable } from 'stream'; | ||
import { TeenyStatistics } from './TeenyStatistics'; | ||
export interface CoreOptions { | ||
@@ -32,2 +34,3 @@ method?: string; | ||
forever?: boolean; | ||
pool?: HttpsAgentOptions | HttpAgentOptions; | ||
} | ||
@@ -68,2 +71,4 @@ export interface OptionsWithUri extends CoreOptions { | ||
var defaults: (defaults: CoreOptions) => (reqOpts: Options, callback?: RequestCallback<any> | undefined) => void | Request; | ||
var stats: TeenyStatistics; | ||
var resetStats: () => void; | ||
} | ||
@@ -73,3 +78,5 @@ declare function teenyRequest(reqOpts: Options, callback: RequestCallback): void; | ||
var defaults: (defaults: CoreOptions) => (reqOpts: Options, callback?: RequestCallback<any> | undefined) => void | Request; | ||
var stats: TeenyStatistics; | ||
var resetStats: () => void; | ||
} | ||
export { teenyRequest }; |
@@ -17,39 +17,13 @@ "use strict"; | ||
*/ | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var node_fetch_1 = require("node-fetch"); | ||
var stream_1 = require("stream"); | ||
var uuid = require("uuid"); | ||
var agents_1 = require("./agents"); | ||
var streamEvents = require('stream-events'); | ||
var RequestError = /** @class */ (function (_super) { | ||
__extends(RequestError, _super); | ||
function RequestError() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
return RequestError; | ||
}(Error)); | ||
exports.teenyRequest = exports.RequestError = void 0; | ||
const node_fetch_1 = require("node-fetch"); | ||
const stream_1 = require("stream"); | ||
const uuid = require("uuid"); | ||
const agents_1 = require("./agents"); | ||
const TeenyStatistics_1 = require("./TeenyStatistics"); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const streamEvents = require('stream-events'); | ||
class RequestError extends Error { | ||
} | ||
exports.RequestError = RequestError; | ||
@@ -62,3 +36,7 @@ /** | ||
function requestToFetchOptions(reqOpts) { | ||
var options = __assign(__assign({ method: reqOpts.method || 'GET' }, (reqOpts.timeout && { timeout: reqOpts.timeout })), (typeof reqOpts.gzip === 'boolean' && { compress: reqOpts.gzip })); | ||
const options = { | ||
method: reqOpts.method || 'GET', | ||
...(reqOpts.timeout && { timeout: reqOpts.timeout }), | ||
...(typeof reqOpts.gzip === 'boolean' && { compress: reqOpts.gzip }), | ||
}; | ||
if (typeof reqOpts.json === 'object') { | ||
@@ -79,13 +57,14 @@ // Add Content-type: application/json header | ||
} | ||
// tslint:disable-next-line no-any | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
options.headers = reqOpts.headers; | ||
var uri = (reqOpts.uri || | ||
let uri = (reqOpts.uri || | ||
reqOpts.url); | ||
if (reqOpts.useQuerystring === true || typeof reqOpts.qs === 'object') { | ||
var qs = require('querystring'); | ||
var params = qs.stringify(reqOpts.qs); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const qs = require('querystring'); | ||
const params = qs.stringify(reqOpts.qs); | ||
uri = uri + '?' + params; | ||
} | ||
options.agent = agents_1.getAgent(uri, reqOpts); | ||
return { uri: uri, options: options }; | ||
return { uri, options }; | ||
} | ||
@@ -100,3 +79,3 @@ /** | ||
function fetchToRequestResponse(opts, res) { | ||
var request = {}; | ||
const request = {}; | ||
request.agent = opts.agent || false; | ||
@@ -106,11 +85,11 @@ request.headers = (opts.headers || {}); | ||
// headers need to be converted from a map to an obj | ||
var resHeaders = {}; | ||
res.headers.forEach(function (value, key) { return (resHeaders[key] = value); }); | ||
var response = Object.assign(res.body, { | ||
const resHeaders = {}; | ||
res.headers.forEach((value, key) => (resHeaders[key] = value)); | ||
const response = Object.assign(res.body, { | ||
statusCode: res.status, | ||
statusMessage: res.statusText, | ||
request: request, | ||
request, | ||
body: res.body, | ||
headers: resHeaders, | ||
toJSON: function () { return ({ headers: resHeaders }); }, | ||
toJSON: () => ({ headers: resHeaders }), | ||
}); | ||
@@ -126,7 +105,6 @@ return response; | ||
function createMultipartStream(boundary, multipart) { | ||
var finale = "--" + boundary + "--"; | ||
var stream = new stream_1.PassThrough(); | ||
for (var _i = 0, multipart_1 = multipart; _i < multipart_1.length; _i++) { | ||
var part = multipart_1[_i]; | ||
var preamble = "--" + boundary + "\r\nContent-Type: " + part['Content-Type'] + "\r\n\r\n"; | ||
const finale = `--${boundary}--`; | ||
const stream = new stream_1.PassThrough(); | ||
for (const part of multipart) { | ||
const preamble = `--${boundary}\r\nContent-Type: ${part['Content-Type']}\r\n\r\n`; | ||
stream.write(preamble); | ||
@@ -139,3 +117,3 @@ if (typeof part.body === 'string') { | ||
part.body.pipe(stream, { end: false }); | ||
part.body.on('end', function () { | ||
part.body.on('end', () => { | ||
stream.write('\r\n'); | ||
@@ -150,4 +128,4 @@ stream.write(finale); | ||
function teenyRequest(reqOpts, callback) { | ||
var _a = requestToFetchOptions(reqOpts), uri = _a.uri, options = _a.options; | ||
var multipart = reqOpts.multipart; | ||
const { uri, options } = requestToFetchOptions(reqOpts); | ||
const multipart = reqOpts.multipart; | ||
if (reqOpts.multipart && multipart.length === 2) { | ||
@@ -158,16 +136,18 @@ if (!callback) { | ||
} | ||
var boundary = uuid.v4(); | ||
options.headers['Content-Type'] = "multipart/related; boundary=" + boundary; | ||
const boundary = uuid.v4(); | ||
options.headers['Content-Type'] = `multipart/related; boundary=${boundary}`; | ||
options.body = createMultipartStream(boundary, multipart); | ||
// Multipart upload | ||
node_fetch_1.default(uri, options).then(function (res) { | ||
var header = res.headers.get('content-type'); | ||
var response = fetchToRequestResponse(options, res); | ||
var body = response.body; | ||
teenyRequest.stats.requestStarting(); | ||
node_fetch_1.default(uri, options).then(res => { | ||
teenyRequest.stats.requestFinished(); | ||
const header = res.headers.get('content-type'); | ||
const response = fetchToRequestResponse(options, res); | ||
const body = response.body; | ||
if (header === 'application/json' || | ||
header === 'application/json; charset=utf-8') { | ||
res.json().then(function (json) { | ||
res.json().then(json => { | ||
response.body = json; | ||
callback(null, response, json); | ||
}, function (err) { | ||
}, (err) => { | ||
callback(err, response, body); | ||
@@ -177,9 +157,10 @@ }); | ||
} | ||
res.text().then(function (text) { | ||
res.text().then(text => { | ||
response.body = text; | ||
callback(null, response, text); | ||
}, function (err) { | ||
}, err => { | ||
callback(err, response, body); | ||
}); | ||
}, function (err) { | ||
}, err => { | ||
teenyRequest.stats.requestFinished(); | ||
callback(err, null, null); | ||
@@ -191,12 +172,12 @@ }); | ||
// Stream mode | ||
var requestStream_1 = streamEvents(new stream_1.PassThrough()); | ||
// tslint:disable-next-line no-any | ||
var responseStream_1; | ||
requestStream_1.once('reading', function () { | ||
if (responseStream_1) { | ||
responseStream_1.pipe(requestStream_1); | ||
const requestStream = streamEvents(new stream_1.PassThrough()); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let responseStream; | ||
requestStream.once('reading', () => { | ||
if (responseStream) { | ||
responseStream.pipe(requestStream); | ||
} | ||
else { | ||
requestStream_1.once('response', function () { | ||
responseStream_1.pipe(requestStream_1); | ||
requestStream.once('response', () => { | ||
responseStream.pipe(requestStream); | ||
}); | ||
@@ -206,11 +187,14 @@ } | ||
options.compress = false; | ||
node_fetch_1.default(uri, options).then(function (res) { | ||
responseStream_1 = res.body; | ||
responseStream_1.on('error', function (err) { | ||
requestStream_1.emit('error', err); | ||
teenyRequest.stats.requestStarting(); | ||
node_fetch_1.default(uri, options).then(res => { | ||
teenyRequest.stats.requestFinished(); | ||
responseStream = res.body; | ||
responseStream.on('error', (err) => { | ||
requestStream.emit('error', err); | ||
}); | ||
var response = fetchToRequestResponse(options, res); | ||
requestStream_1.emit('response', response); | ||
}, function (err) { | ||
requestStream_1.emit('error', err); | ||
const response = fetchToRequestResponse(options, res); | ||
requestStream.emit('response', response); | ||
}, err => { | ||
teenyRequest.stats.requestFinished(); | ||
requestStream.emit('error', err); | ||
}); | ||
@@ -220,9 +204,11 @@ // fetch doesn't supply the raw HTTP stream, instead it | ||
// stream. | ||
return requestStream_1; | ||
return requestStream; | ||
} | ||
// GET or POST with callback | ||
node_fetch_1.default(uri, options).then(function (res) { | ||
var header = res.headers.get('content-type'); | ||
var response = fetchToRequestResponse(options, res); | ||
var body = response.body; | ||
teenyRequest.stats.requestStarting(); | ||
node_fetch_1.default(uri, options).then(res => { | ||
teenyRequest.stats.requestFinished(); | ||
const header = res.headers.get('content-type'); | ||
const response = fetchToRequestResponse(options, res); | ||
const body = response.body; | ||
if (header === 'application/json' || | ||
@@ -235,6 +221,6 @@ header === 'application/json; charset=utf-8') { | ||
} | ||
res.json().then(function (json) { | ||
res.json().then(json => { | ||
response.body = json; | ||
callback(null, response, json); | ||
}, function (err) { | ||
}, err => { | ||
callback(err, response, body); | ||
@@ -244,10 +230,11 @@ }); | ||
} | ||
res.text().then(function (text) { | ||
var response = fetchToRequestResponse(options, res); | ||
res.text().then(text => { | ||
const response = fetchToRequestResponse(options, res); | ||
response.body = text; | ||
callback(null, response, text); | ||
}, function (err) { | ||
}, err => { | ||
callback(err, response, body); | ||
}); | ||
}, function (err) { | ||
}, err => { | ||
teenyRequest.stats.requestFinished(); | ||
callback(err, null, null); | ||
@@ -258,5 +245,5 @@ }); | ||
exports.teenyRequest = teenyRequest; | ||
teenyRequest.defaults = function (defaults) { | ||
return function (reqOpts, callback) { | ||
var opts = __assign(__assign({}, defaults), reqOpts); | ||
teenyRequest.defaults = (defaults) => { | ||
return (reqOpts, callback) => { | ||
const opts = { ...defaults, ...reqOpts }; | ||
if (callback === undefined) { | ||
@@ -268,2 +255,9 @@ return teenyRequest(opts); | ||
}; | ||
/** | ||
* Single instance of an interface for keeping track of things. | ||
*/ | ||
teenyRequest.stats = new TeenyStatistics_1.TeenyStatistics(); | ||
teenyRequest.resetStats = () => { | ||
teenyRequest.stats = new TeenyStatistics_1.TeenyStatistics(teenyRequest.stats.getOptions()); | ||
}; | ||
//# sourceMappingURL=index.js.map |
# Changelog | ||
## [7.0.0](https://www.github.com/googleapis/teeny-request/compare/v6.0.3...v7.0.0) (2020-06-01) | ||
### ⚠ BREAKING CHANGES | ||
* dropping support for Node.js 8.x | ||
### Features | ||
* pass agent options when using agent pool config ([#149](https://www.github.com/googleapis/teeny-request/issues/149)) ([38ece79](https://www.github.com/googleapis/teeny-request/commit/38ece79151b667ec1a72ec50b1c7a58258924794)) | ||
* warn on too many concurrent requests ([#165](https://www.github.com/googleapis/teeny-request/issues/165)) ([88ff2d0](https://www.github.com/googleapis/teeny-request/commit/88ff2d0d8e0fc25a4219ef5625b8de353ed4aa29)) | ||
### Bug Fixes | ||
* apache license URL ([#468](https://www.github.com/googleapis/teeny-request/issues/468)) ([#156](https://www.github.com/googleapis/teeny-request/issues/156)) ([01ac7bd](https://www.github.com/googleapis/teeny-request/commit/01ac7bd01e870796fd15355e079649633d5d5983)) | ||
* update template files for Node.js libraries ([#152](https://www.github.com/googleapis/teeny-request/issues/152)) ([89833c3](https://www.github.com/googleapis/teeny-request/commit/89833c3c3e8afea04c85a60811f122c5a6d37e48)) | ||
* **deps:** update dependency uuid to v8 ([#164](https://www.github.com/googleapis/teeny-request/issues/164)) ([2ab8155](https://www.github.com/googleapis/teeny-request/commit/2ab81550aeb8ca914516ff4ac20ebbb7b3d73fa5)) | ||
### Build System | ||
* drop support for node.js 8.x ([#159](https://www.github.com/googleapis/teeny-request/issues/159)) ([d87aa73](https://www.github.com/googleapis/teeny-request/commit/d87aa73d3fafbdc013b03b7629a41decda6da98a)) | ||
### [6.0.3](https://www.github.com/googleapis/teeny-request/compare/v6.0.2...v6.0.3) (2020-03-06) | ||
@@ -4,0 +28,0 @@ |
{ | ||
"name": "teeny-request", | ||
"version": "6.0.3", | ||
"version": "7.0.0", | ||
"description": "Like request, but smaller.", | ||
"main": "./build/src/index.js", | ||
"types": "./build/src/index.d.ts", | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
@@ -15,3 +18,2 @@ "test": "c8 mocha build/test", | ||
"prepare": "npm run compile", | ||
"posttest": "npm run lint", | ||
"docs": "compodoc src/", | ||
@@ -21,3 +23,4 @@ "predocs-test": "npm run docs", | ||
"samples-test": "echo no sample tests!", | ||
"system-test": "echo no system tests!" | ||
"system-test": "echo no system tests!", | ||
"precompile": "gts clean" | ||
}, | ||
@@ -44,3 +47,3 @@ "files": [ | ||
"stream-events": "^1.0.5", | ||
"uuid": "^7.0.0" | ||
"uuid": "^8.0.0" | ||
}, | ||
@@ -51,7 +54,7 @@ "devDependencies": { | ||
"@types/node-fetch": "^2.1.2", | ||
"@types/sinon": "^7.0.13", | ||
"@types/uuid": "^7.0.0", | ||
"@types/sinon": "^9.0.0", | ||
"@types/uuid": "^8.0.0", | ||
"c8": "^7.0.0", | ||
"codecov": "^3.1.0", | ||
"gts": "^1.0.0", | ||
"gts": "^2.0.0", | ||
"linkinator": "^2.0.0", | ||
@@ -61,3 +64,3 @@ "mocha": "^7.0.0", | ||
"sinon": "^9.0.0", | ||
"typescript": "~3.8.2" | ||
"typescript": "^3.8.3" | ||
}, | ||
@@ -64,0 +67,0 @@ "nyc": { |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://travis-ci.org/fhinkel/teeny-request.svg?branch=master)](https://travis-ci.org/fhinkel/teeny-request) | ||
[![Build Status](https://travis-ci.org/googleapis/teeny-request.svg?branch=master)](https://travis-ci.org/googleapis/teeny-request) | ||
@@ -3,0 +3,0 @@ # teeny-request |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59944
13
692
+ Addeduuid@8.3.2(transitive)
- Removeduuid@7.0.3(transitive)
Updateduuid@^8.0.0