Updating NPM dependencies

This commit is contained in:
Drew Heavner
2020-11-11 13:30:52 +00:00
committed by GitHub
parent 7a6fb66c63
commit bd487ef324
9068 changed files with 1400893 additions and 587661 deletions

View File

@@ -6,15 +6,13 @@
*/
/// <reference types="node" />
import { OutgoingHttpHeaders } from 'http';
export declare const HOST_ADDRESS = "http://169.254.169.254";
export declare const BASE_PATH = "/computeMetadata/v1";
export declare const BASE_URL: string;
export declare const HOST_ADDRESS = "http://169.254.169.254";
export declare const SECONDARY_HOST_ADDRESS = "http://metadata.google.internal.";
export declare const SECONDARY_BASE_URL: string;
export declare const HEADER_NAME = "Metadata-Flavor";
export declare const HEADER_VALUE = "Google";
export declare const HEADERS: Readonly<{
[HEADER_NAME]: string;
"Metadata-Flavor": string;
}>;
export interface Options {
params?: {
@@ -25,7 +23,9 @@ export interface Options {
}
export declare function instance<T = any>(options?: string | Options): Promise<T>;
export declare function project<T = any>(options?: string | Options): Promise<T>;
/**
* Determine if the metadata server is currently available.
*/
export declare function isAvailable(): Promise<boolean>;
/**
* reset the memoized isAvailable() lookup.
*/
export declare function resetIsAvailableCache(): void;
export declare function requestTimeout(): number;

View File

@@ -6,16 +6,34 @@
* See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestTimeout = exports.resetIsAvailableCache = exports.isAvailable = exports.project = exports.instance = exports.HEADERS = exports.HEADER_VALUE = exports.HEADER_NAME = exports.SECONDARY_HOST_ADDRESS = exports.HOST_ADDRESS = exports.BASE_PATH = void 0;
const gaxios_1 = require("gaxios");
const jsonBigint = require('json-bigint');
exports.HOST_ADDRESS = 'http://169.254.169.254';
const jsonBigint = require('json-bigint'); // eslint-disable-line
exports.BASE_PATH = '/computeMetadata/v1';
exports.BASE_URL = exports.HOST_ADDRESS + exports.BASE_PATH;
exports.HOST_ADDRESS = 'http://169.254.169.254';
exports.SECONDARY_HOST_ADDRESS = 'http://metadata.google.internal.';
exports.SECONDARY_BASE_URL = exports.SECONDARY_HOST_ADDRESS + exports.BASE_PATH;
exports.HEADER_NAME = 'Metadata-Flavor';
exports.HEADER_VALUE = 'Google';
exports.HEADERS = Object.freeze({ [exports.HEADER_NAME]: exports.HEADER_VALUE });
/**
* Returns the base URL while taking into account the GCE_METADATA_HOST
* environment variable if it exists.
*
* @returns The base URL, e.g., http://169.254.169.254/computeMetadata/v1.
*/
function getBaseUrl(baseUrl) {
if (!baseUrl) {
baseUrl =
process.env.GCE_METADATA_IP ||
process.env.GCE_METADATA_HOST ||
exports.HOST_ADDRESS;
}
// If no scheme is provided default to HTTP:
if (!/^https?:\/\//.test(baseUrl)) {
baseUrl = `http://${baseUrl}`;
}
return new URL(exports.BASE_PATH, baseUrl).href;
}
// Accepts an options object passed from the user to the API. In previous
// versions of the API, it referred to a `Request` or an `Axios` request
// options object. Now it refers to an object with very limited property
@@ -29,7 +47,7 @@ function validate(options) {
case 'headers':
break;
case 'qs':
throw new Error(`'qs' is not a valid configuration option. Please use 'params' instead.`);
throw new Error("'qs' is not a valid configuration option. Please use 'params' instead.");
default:
throw new Error(`'${key}' is not a valid configuration option.`);
}
@@ -48,12 +66,12 @@ async function metadataAccessor(type, options, noResponseRetries = 3, fastFail =
try {
const requestMethod = fastFail ? fastFailMetadataRequest : gaxios_1.request;
const res = await requestMethod({
url: `${exports.BASE_URL}/${type}${property}`,
url: `${getBaseUrl()}/${type}${property}`,
headers: Object.assign({}, exports.HEADERS, options.headers),
retryConfig: { noResponseRetries },
params: options.params,
responseType: 'text',
timeout: 3000,
timeout: requestTimeout(),
});
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[exports.HEADER_NAME.toLowerCase()] !== exports.HEADER_VALUE) {
@@ -80,28 +98,92 @@ async function metadataAccessor(type, options, noResponseRetries = 3, fastFail =
}
}
async function fastFailMetadataRequest(options) {
const secondaryOptions = Object.assign(Object.assign({}, options), { url: options.url.replace(exports.BASE_URL, exports.SECONDARY_BASE_URL) });
return Promise.race([gaxios_1.request(options), gaxios_1.request(secondaryOptions)]);
const secondaryOptions = {
...options,
url: options.url.replace(getBaseUrl(), getBaseUrl(exports.SECONDARY_HOST_ADDRESS)),
};
// We race a connection between DNS/IP to metadata server. There are a couple
// reasons for this:
//
// 1. the DNS is slow in some GCP environments; by checking both, we might
// detect the runtime environment signficantly faster.
// 2. we can't just check the IP, which is tarpitted and slow to respond
// on a user's local machine.
//
// Additional logic has been added to make sure that we don't create an
// unhandled rejection in scenarios where a failure happens sometime
// after a success.
//
// Note, however, if a failure happens prior to a success, a rejection should
// occur, this is for folks running locally.
//
let responded = false;
const r1 = gaxios_1.request(options)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r2;
}
else {
responded = true;
throw err;
}
});
const r2 = gaxios_1.request(secondaryOptions)
.then(res => {
responded = true;
return res;
})
.catch(err => {
if (responded) {
return r1;
}
else {
responded = true;
throw err;
}
});
return Promise.race([r1, r2]);
}
// tslint:disable-next-line no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function instance(options) {
return metadataAccessor('instance', options);
}
exports.instance = instance;
// tslint:disable-next-line no-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function project(options) {
return metadataAccessor('project', options);
}
exports.project = project;
/*
* How many times should we retry detecting GCP environment.
*/
function detectGCPAvailableRetries() {
return process.env.DETECT_GCP_RETRIES
? Number(process.env.DETECT_GCP_RETRIES)
: 0;
}
/**
* Determine if the metadata server is currently available.
*/
let cachedIsAvailableResponse;
async function isAvailable() {
try {
// Attempt to read instance metadata. As configured, this will
// retry 3 times if there is a valid response, and fail fast
// if there is an ETIMEDOUT or ENOTFOUND error.
await metadataAccessor('instance', undefined, 0, true);
// If a user is instantiating several GCP libraries at the same time,
// this may result in multiple calls to isAvailable(), to detect the
// runtime environment. We use the same promise for each of these calls
// to reduce the network load.
if (cachedIsAvailableResponse === undefined) {
cachedIsAvailableResponse = metadataAccessor('instance', undefined, detectGCPAvailableRetries(),
// If the default HOST_ADDRESS has been overridden, we should not
// make an effort to try SECONDARY_HOST_ADDRESS (as we are likely in
// a non-GCP environment):
!(process.env.GCE_METADATA_IP || process.env.GCE_METADATA_HOST));
}
await cachedIsAvailableResponse;
return true;
}
catch (err) {
@@ -113,16 +195,52 @@ async function isAvailable() {
// within ms.
return false;
}
else if (err.code &&
(err.code === 'ENOTFOUND' ||
err.code === 'ENOENT' ||
err.code === 'ENETUNREACH')) {
if (err.response && err.response.status === 404) {
return false;
}
else {
if (!(err.response && err.response.status === 404) &&
// A warning is emitted if we see an unexpected err.code, or err.code
// is not populated:
(!err.code ||
![
'EHOSTDOWN',
'EHOSTUNREACH',
'ENETUNREACH',
'ENOENT',
'ENOTFOUND',
'ECONNREFUSED',
].includes(err.code))) {
let code = 'UNKNOWN';
if (err.code)
code = err.code;
process.emitWarning(`received unexpected error = ${err.message} code = ${code}`, 'MetadataLookupWarning');
}
// Failure to resolve the metadata service means that it is not available.
return false;
}
// Throw unexpected errors.
throw err;
}
}
exports.isAvailable = isAvailable;
/**
* reset the memoized isAvailable() lookup.
*/
function resetIsAvailableCache() {
cachedIsAvailableResponse = undefined;
}
exports.resetIsAvailableCache = resetIsAvailableCache;
function requestTimeout() {
// In testing, we were able to reproduce behavior similar to
// https://github.com/googleapis/google-auth-library-nodejs/issues/798
// by making many concurrent network requests. Requests do not actually fail,
// rather they take significantly longer to complete (and we hit our
// default 3000ms timeout).
//
// This logic detects a GCF environment, using the documented environment
// variables K_SERVICE and FUNCTION_NAME:
// https://cloud.google.com/functions/docs/env-var and, in a GCF environment
// eliminates timeouts (by setting the value to 0 to disable).
return process.env.K_SERVICE || process.env.FUNCTION_NAME ? 0 : 3000;
}
exports.requestTimeout = requestTimeout;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long