Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

gaxios

Package Overview
Dependencies
14
Maintainers
1
Versions
76
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 6.4.0 to 6.5.0

32

build/src/common.d.ts

@@ -100,2 +100,5 @@ /// <reference types="node" />

follow?: number;
/**
* A collection of parts to send as a `Content-Type: multipart/related` request.
*/
multipart?: GaxiosMultipartOptions[];

@@ -127,2 +130,29 @@ params?: any;

/**
* An optional proxy to use for requests.
* Available via `process.env.HTTP_PROXY` and `process.env.HTTPS_PROXY` as well - with a preference for the this config option when multiple are available.
* The {@link GaxiosOptions.agent `agent`} option overrides this.
*
* @see {@link GaxiosOptions.noProxy}
* @see {@link GaxiosOptions.agent}
*/
proxy?: string | URL;
/**
* A list for excluding traffic for proxies.
* Available via `process.env.NO_PROXY` as well as a common-separated list of strings - merged with any local `noProxy` rules.
*
* - When provided a string, it is matched by
* - Wildcard `*.` and `.` matching are available. (e.g. `.example.com` or `*.example.com`)
* - When provided a URL, it is matched by the `.origin` property.
* - For example, requesting `https://example.com` with the following `noProxy`s would result in a no proxy use:
* - new URL('https://example.com')
* - new URL('https://example.com:443')
* - The following would be used with a proxy:
* - new URL('http://example.com:80')
* - new URL('https://example.com:8443')
* - When provided a regular expression it is used to match the stringified URL
*
* @see {@link GaxiosOptions.proxy}
*/
noProxy?: (string | URL | RegExp)[];
/**
* An experimental error redactor.

@@ -175,3 +205,3 @@ *

* The HTTP response status codes that will automatically be retried.
* Defaults to: [[100, 199], [429, 429], [500, 599]]
* Defaults to: [[100, 199], [408, 408], [429, 429], [500, 599]]
*/

@@ -178,0 +208,0 @@ statusCodesToRetry?: number[][];

8

build/src/gaxios.d.ts

@@ -7,3 +7,4 @@ /// <reference types="node" />

export declare class Gaxios {
protected agentCache: Map<string, Agent | ((parsedUrl: URL) => Agent)>;
#private;
protected agentCache: Map<string | URL, Agent | ((parsedUrl: URL) => Agent)>;
/**

@@ -31,7 +32,2 @@ * Default HTTP options that will be used for every HTTP request.

/**
* Validates the options, and merges them with defaults.
* @param opts The original options passed from the client.
*/
private validateOpts;
/**
* By default, throw for any non-2xx status code

@@ -38,0 +34,0 @@ * @param status status code from the HTTP response

@@ -14,5 +14,40 @@ "use strict";

// limitations under the License.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _Gaxios_instances, _a, _Gaxios_urlMayUseProxy, _Gaxios_prepareRequest, _Gaxios_proxyAgent, _Gaxios_getProxyAgent;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -29,3 +64,2 @@ exports.Gaxios = void 0;

const stream_1 = require("stream");
const https_proxy_agent_1 = require("https-proxy-agent");
const uuid_1 = require("uuid");

@@ -55,45 +89,2 @@ /* eslint-disable @typescript-eslint/no-explicit-any */

}
let HttpsProxyAgent;
function loadProxy() {
var _a, _b, _c, _d;
const proxy = ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.HTTPS_PROXY) ||
((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.https_proxy) ||
((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c.HTTP_PROXY) ||
((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.http_proxy);
if (proxy) {
HttpsProxyAgent = https_proxy_agent_1.HttpsProxyAgent;
}
return proxy;
}
loadProxy();
function skipProxy(url) {
var _a;
const noProxyEnv = (_a = process.env.NO_PROXY) !== null && _a !== void 0 ? _a : process.env.no_proxy;
if (!noProxyEnv) {
return false;
}
const noProxyUrls = noProxyEnv.split(',');
const parsedURL = url instanceof url_1.URL ? url : new url_1.URL(url);
return !!noProxyUrls.find(url => {
if (url.startsWith('*.') || url.startsWith('.')) {
url = url.replace(/^\*\./, '.');
return parsedURL.hostname.endsWith(url);
}
else {
return url === parsedURL.origin || url === parsedURL.hostname;
}
});
}
// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function getProxy(url) {
// If there is a match between the no_proxy env variables and the url, then do not proxy
if (skipProxy(url)) {
return undefined;
// If there is not a match between the no_proxy env variables and the url, check to see if there should be a proxy
}
else {
return loadProxy();
}
}
class Gaxios {

@@ -105,2 +96,3 @@ /**

constructor(defaults) {
_Gaxios_instances.add(this);
this.agentCache = new Map();

@@ -114,3 +106,3 @@ this.defaults = defaults || {};

async request(opts = {}) {
opts = this.validateOpts(opts);
opts = await __classPrivateFieldGet(this, _Gaxios_instances, "m", _Gaxios_prepareRequest).call(this, opts);
return this._request(opts);

@@ -129,3 +121,3 @@ }

async _request(opts = {}) {
var _a;
var _b;
try {

@@ -164,3 +156,3 @@ let translatedResponse;

// copy the retry state over to the existing config
opts.retryConfig = (_a = err.config) === null || _a === void 0 ? void 0 : _a.retryConfig;
opts.retryConfig = (_b = err.config) === null || _b === void 0 ? void 0 : _b.retryConfig;
return this._request(opts);

@@ -180,3 +172,3 @@ }

}
catch (_a) {
catch (_b) {
// continue

@@ -197,125 +189,2 @@ }

/**
* Validates the options, and merges them with defaults.
* @param opts The original options passed from the client.
*/
validateOpts(options) {
const opts = (0, extend_1.default)(true, {}, this.defaults, options);
if (!opts.url) {
throw new Error('URL is required.');
}
// baseUrl has been deprecated, remove in 2.0
const baseUrl = opts.baseUrl || opts.baseURL;
if (baseUrl) {
opts.url = baseUrl.toString() + opts.url;
}
opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
if (opts.params && Object.keys(opts.params).length > 0) {
let additionalQueryParams = opts.paramsSerializer(opts.params);
if (additionalQueryParams.startsWith('?')) {
additionalQueryParams = additionalQueryParams.slice(1);
}
const prefix = opts.url.toString().includes('?') ? '&' : '?';
opts.url = opts.url + prefix + additionalQueryParams;
}
if (typeof options.maxContentLength === 'number') {
opts.size = options.maxContentLength;
}
if (typeof options.maxRedirects === 'number') {
opts.follow = options.maxRedirects;
}
opts.headers = opts.headers || {};
if (opts.multipart === undefined && opts.data) {
const isFormData = typeof FormData === 'undefined'
? false
: (opts === null || opts === void 0 ? void 0 : opts.data) instanceof FormData;
if (is_stream_1.default.readable(opts.data)) {
opts.body = opts.data;
}
else if (hasBuffer() && Buffer.isBuffer(opts.data)) {
// Do not attempt to JSON.stringify() a Buffer:
opts.body = opts.data;
if (!hasHeader(opts, 'Content-Type')) {
opts.headers['Content-Type'] = 'application/json';
}
}
else if (typeof opts.data === 'object') {
// If www-form-urlencoded content type has been set, but data is
// provided as an object, serialize the content using querystring:
if (!isFormData) {
if (getHeader(opts, 'content-type') ===
'application/x-www-form-urlencoded') {
opts.body = opts.paramsSerializer(opts.data);
}
else {
// } else if (!(opts.data instanceof FormData)) {
if (!hasHeader(opts, 'Content-Type')) {
opts.headers['Content-Type'] = 'application/json';
}
opts.body = JSON.stringify(opts.data);
}
}
}
else {
opts.body = opts.data;
}
}
else if (opts.multipart && opts.multipart.length > 0) {
// note: once the minimum version reaches Node 16,
// this can be replaced with randomUUID() function from crypto
// and the dependency on UUID removed
const boundary = (0, uuid_1.v4)();
opts.headers['Content-Type'] = `multipart/related; boundary=${boundary}`;
const bodyStream = new stream_1.PassThrough();
opts.body = bodyStream;
(0, stream_1.pipeline)(this.getMultipartRequest(opts.multipart, boundary), bodyStream, () => { });
}
opts.validateStatus = opts.validateStatus || this.validateStatus;
opts.responseType = opts.responseType || 'unknown';
if (!opts.headers['Accept'] && opts.responseType === 'json') {
opts.headers['Accept'] = 'application/json';
}
opts.method = opts.method || 'GET';
const proxy = getProxy(opts.url);
if (proxy) {
if (this.agentCache.has(proxy)) {
opts.agent = this.agentCache.get(proxy);
}
else {
// Proxy is being used in conjunction with mTLS.
if (opts.cert && opts.key) {
const parsedURL = new url_1.URL(proxy);
opts.agent = new HttpsProxyAgent({
port: parsedURL.port,
host: parsedURL.host,
protocol: parsedURL.protocol,
cert: opts.cert,
key: opts.key,
});
}
else {
opts.agent = new HttpsProxyAgent(proxy);
}
this.agentCache.set(proxy, opts.agent);
}
}
else if (opts.cert && opts.key) {
// Configure client for mTLS:
if (this.agentCache.has(opts.key)) {
opts.agent = this.agentCache.get(opts.key);
}
else {
opts.agent = new https_1.Agent({
cert: opts.cert,
key: opts.key,
});
this.agentCache.set(opts.key, opts.agent);
}
}
if (typeof opts.errorRedactor !== 'function' &&
opts.errorRedactor !== false) {
opts.errorRedactor = common_1.defaultErrorRedactor;
}
return opts;
}
/**
* By default, throw for any non-2xx status code

@@ -369,3 +238,3 @@ * @param status status code from the HTTP response

}
catch (_a) {
catch (_b) {
// continue

@@ -409,2 +278,174 @@ }

exports.Gaxios = Gaxios;
_a = Gaxios, _Gaxios_instances = new WeakSet(), _Gaxios_urlMayUseProxy = function _Gaxios_urlMayUseProxy(url, noProxy = []) {
var _b, _c;
const candidate = new url_1.URL(url);
const noProxyList = [...noProxy];
const noProxyEnvList = ((_c = ((_b = process.env.NO_PROXY) !== null && _b !== void 0 ? _b : process.env.no_proxy)) === null || _c === void 0 ? void 0 : _c.split(',')) || [];
for (const rule of noProxyEnvList) {
noProxyList.push(rule.trim());
}
for (const rule of noProxyList) {
// Match regex
if (rule instanceof RegExp) {
if (rule.test(candidate.toString())) {
return false;
}
}
// Match URL
else if (rule instanceof url_1.URL) {
if (rule.origin === candidate.origin) {
return false;
}
}
// Match string regex
else if (rule.startsWith('*.') || rule.startsWith('.')) {
const cleanedRule = rule.replace(/^\*\./, '.');
if (candidate.hostname.endsWith(cleanedRule)) {
return false;
}
}
// Basic string match
else if (rule === candidate.origin ||
rule === candidate.hostname ||
rule === candidate.href) {
return false;
}
}
return true;
}, _Gaxios_prepareRequest =
/**
* Validates the options, merges them with defaults, and prepare request.
*
* @param options The original options passed from the client.
* @returns Prepared options, ready to make a request
*/
async function _Gaxios_prepareRequest(options) {
var _b, _c, _d, _e;
const opts = (0, extend_1.default)(true, {}, this.defaults, options);
if (!opts.url) {
throw new Error('URL is required.');
}
// baseUrl has been deprecated, remove in 2.0
const baseUrl = opts.baseUrl || opts.baseURL;
if (baseUrl) {
opts.url = baseUrl.toString() + opts.url;
}
opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
if (opts.params && Object.keys(opts.params).length > 0) {
let additionalQueryParams = opts.paramsSerializer(opts.params);
if (additionalQueryParams.startsWith('?')) {
additionalQueryParams = additionalQueryParams.slice(1);
}
const prefix = opts.url.toString().includes('?') ? '&' : '?';
opts.url = opts.url + prefix + additionalQueryParams;
}
if (typeof options.maxContentLength === 'number') {
opts.size = options.maxContentLength;
}
if (typeof options.maxRedirects === 'number') {
opts.follow = options.maxRedirects;
}
opts.headers = opts.headers || {};
if (opts.multipart === undefined && opts.data) {
const isFormData = typeof FormData === 'undefined'
? false
: (opts === null || opts === void 0 ? void 0 : opts.data) instanceof FormData;
if (is_stream_1.default.readable(opts.data)) {
opts.body = opts.data;
}
else if (hasBuffer() && Buffer.isBuffer(opts.data)) {
// Do not attempt to JSON.stringify() a Buffer:
opts.body = opts.data;
if (!hasHeader(opts, 'Content-Type')) {
opts.headers['Content-Type'] = 'application/json';
}
}
else if (typeof opts.data === 'object') {
// If www-form-urlencoded content type has been set, but data is
// provided as an object, serialize the content using querystring:
if (!isFormData) {
if (getHeader(opts, 'content-type') ===
'application/x-www-form-urlencoded') {
opts.body = opts.paramsSerializer(opts.data);
}
else {
// } else if (!(opts.data instanceof FormData)) {
if (!hasHeader(opts, 'Content-Type')) {
opts.headers['Content-Type'] = 'application/json';
}
opts.body = JSON.stringify(opts.data);
}
}
}
else {
opts.body = opts.data;
}
}
else if (opts.multipart && opts.multipart.length > 0) {
// note: once the minimum version reaches Node 16,
// this can be replaced with randomUUID() function from crypto
// and the dependency on UUID removed
const boundary = (0, uuid_1.v4)();
opts.headers['Content-Type'] = `multipart/related; boundary=${boundary}`;
const bodyStream = new stream_1.PassThrough();
opts.body = bodyStream;
(0, stream_1.pipeline)(this.getMultipartRequest(opts.multipart, boundary), bodyStream, () => { });
}
opts.validateStatus = opts.validateStatus || this.validateStatus;
opts.responseType = opts.responseType || 'unknown';
if (!opts.headers['Accept'] && opts.responseType === 'json') {
opts.headers['Accept'] = 'application/json';
}
opts.method = opts.method || 'GET';
const proxy = opts.proxy ||
((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.HTTPS_PROXY) ||
((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c.https_proxy) ||
((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.HTTP_PROXY) ||
((_e = process === null || process === void 0 ? void 0 : process.env) === null || _e === void 0 ? void 0 : _e.http_proxy);
const urlMayUseProxy = __classPrivateFieldGet(this, _Gaxios_instances, "m", _Gaxios_urlMayUseProxy).call(this, opts.url, opts.noProxy);
if (opts.agent) {
// don't do any of the following options - use the user-provided agent.
}
else if (proxy && urlMayUseProxy) {
const HttpsProxyAgent = await __classPrivateFieldGet(_a, _a, "m", _Gaxios_getProxyAgent).call(_a);
if (this.agentCache.has(proxy)) {
opts.agent = this.agentCache.get(proxy);
}
else {
opts.agent = new HttpsProxyAgent(proxy, {
cert: opts.cert,
key: opts.key,
});
this.agentCache.set(proxy, opts.agent);
}
}
else if (opts.cert && opts.key) {
// Configure client for mTLS
if (this.agentCache.has(opts.key)) {
opts.agent = this.agentCache.get(opts.key);
}
else {
opts.agent = new https_1.Agent({
cert: opts.cert,
key: opts.key,
});
this.agentCache.set(opts.key, opts.agent);
}
}
if (typeof opts.errorRedactor !== 'function' &&
opts.errorRedactor !== false) {
opts.errorRedactor = common_1.defaultErrorRedactor;
}
return opts;
}, _Gaxios_getProxyAgent = async function _Gaxios_getProxyAgent() {
__classPrivateFieldSet(this, _a, __classPrivateFieldGet(this, _a, "f", _Gaxios_proxyAgent) || (await Promise.resolve().then(() => __importStar(require('https-proxy-agent')))).HttpsProxyAgent, "f", _Gaxios_proxyAgent);
return __classPrivateFieldGet(this, _a, "f", _Gaxios_proxyAgent);
};
/**
* A cache for the lazily-loaded proxy agent.
*
* Should use {@link Gaxios[#getProxyAgent]} to retrieve.
*/
// using `import` to dynamically import the types here
_Gaxios_proxyAgent = { value: void 0 };
//# sourceMappingURL=gaxios.js.map

@@ -45,5 +45,7 @@ "use strict";

// 4xx - Do not retry (Client errors)
// 408 - Retry ("Request Timeout")
// 429 - Retry ("Too Many Requests")
// 5xx - Retry (Server errors)
[100, 199],
[408, 408],
[429, 429],

@@ -50,0 +52,0 @@ [500, 599],

# Changelog
## [6.5.0](https://github.com/googleapis/gaxios/compare/v6.4.0...v6.5.0) (2024-04-09)
### Features
* Retry `408` by Default ([#616](https://github.com/googleapis/gaxios/issues/616)) ([9331f79](https://github.com/googleapis/gaxios/commit/9331f79f9c9d0c1f4f1f995e1928323f4feb5427))
* Support `proxy` option ([#614](https://github.com/googleapis/gaxios/issues/614)) ([2d14b3f](https://github.com/googleapis/gaxios/commit/2d14b3f54bc97111cb184cecf2379b55ceaca3c2))
## [6.4.0](https://github.com/googleapis/gaxios/compare/v6.3.0...v6.4.0) (2024-04-03)

@@ -4,0 +12,0 @@

{
"name": "gaxios",
"version": "6.4.0",
"version": "6.5.0",
"description": "A simple common HTTP client specifically for Google APIs and services.",

@@ -5,0 +5,0 @@ "main": "build/src/index.js",

@@ -44,4 +44,4 @@ # gaxios

```js
{
```ts
interface GaxiosOptions = {
// The url to which the request should be sent. Required.

@@ -136,3 +136,3 @@ url: string,

// The HTTP response status codes that will automatically be retried.
// Defaults to: [[100, 199], [429, 429], [500, 599]]
// Defaults to: [[100, 199], [408, 408], [429, 429], [500, 599]]
statusCodesToRetry?: number[][];

@@ -161,2 +161,35 @@

/**
* A collection of parts to send as a `Content-Type: multipart/related` request.
*/
multipart?: GaxiosMultipartOptions;
/**
* An optional proxy to use for requests.
* Available via `process.env.HTTP_PROXY` and `process.env.HTTPS_PROXY` as well - with a preference for the this config option when multiple are available.
* The `agent` option overrides this.
*
* @see {@link GaxiosOptions.noProxy}
* @see {@link GaxiosOptions.agent}
*/
proxy?: string | URL;
/**
* A list for excluding traffic for proxies.
* Available via `process.env.NO_PROXY` as well as a common-separated list of strings - merged with any local `noProxy` rules.
*
* - When provided a string, it is matched by
* - Wildcard `*.` and `.` matching are available. (e.g. `.example.com` or `*.example.com`)
* - When provided a URL, it is matched by the `.origin` property.
* - For example, requesting `https://example.com` with the following `noProxy`s would result in a no proxy use:
* - new URL('https://example.com')
* - new URL('https://example.com:443')
* - The following would be used with a proxy:
* - new URL('http://example.com:80')
* - new URL('https://example.com:8443')
* - When provided a regular expression it is used to match the stringified URL
*
* @see {@link GaxiosOptions.proxy}
*/
noProxy?: (string | URL | RegExp)[];
/**
* An experimental, customizable error redactor.

@@ -163,0 +196,0 @@ *

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc