gcp-metadata
Advanced tools
Comparing version 2.0.4 to 3.0.0
@@ -12,2 +12,4 @@ /** | ||
export declare const BASE_URL: string; | ||
export declare const SECONDARY_HOST_ADDRESS = "http://metadata.google.internal."; | ||
export declare const SECONDARY_BASE_URL: string; | ||
export declare const HEADER_NAME = "Metadata-Flavor"; | ||
@@ -14,0 +16,0 @@ export declare const HEADER_VALUE = "Google"; |
@@ -14,2 +14,4 @@ "use strict"; | ||
exports.BASE_URL = exports.HOST_ADDRESS + exports.BASE_PATH; | ||
exports.SECONDARY_HOST_ADDRESS = 'http://metadata.google.internal.'; | ||
exports.SECONDARY_BASE_URL = exports.SECONDARY_HOST_ADDRESS + exports.BASE_PATH; | ||
exports.HEADER_NAME = 'Metadata-Flavor'; | ||
@@ -37,3 +39,3 @@ exports.HEADER_VALUE = 'Google'; | ||
} | ||
async function metadataAccessor(type, options, noResponseRetries = 3) { | ||
async function metadataAccessor(type, options, noResponseRetries = 3, fastFail = false) { | ||
options = options || {}; | ||
@@ -49,3 +51,4 @@ if (typeof options === 'string') { | ||
try { | ||
const res = await gaxios_1.request({ | ||
const requestMethod = fastFail ? fastFailMetadataRequest : gaxios_1.request; | ||
const res = await requestMethod({ | ||
url: `${exports.BASE_URL}/${type}${property}`, | ||
@@ -82,2 +85,6 @@ headers: Object.assign({}, exports.HEADERS, options.headers), | ||
} | ||
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)]); | ||
} | ||
// tslint:disable-next-line no-any | ||
@@ -101,10 +108,16 @@ function instance(options) { | ||
// if there is an ETIMEDOUT or ENOTFOUND error. | ||
await metadataAccessor('instance', undefined, 0); | ||
await metadataAccessor('instance', undefined, 0, true); | ||
return true; | ||
} | ||
catch (err) { | ||
// Failure to resolve the metadata service means that it is not available. | ||
if (err.code && (err.code === 'ENOTFOUND' || err.code === 'ENOENT')) { | ||
if (err.type === 'request-timeout') { | ||
// If running in a GCP environment, metadata endpoint should return | ||
// within ms. | ||
return false; | ||
} | ||
else if (err.code && | ||
(err.code === 'ENOTFOUND' || err.code === 'ENOENT')) { | ||
// Failure to resolve the metadata service means that it is not available. | ||
return false; | ||
} | ||
// Throw unexpected errors. | ||
@@ -111,0 +124,0 @@ throw err; |
@@ -7,2 +7,13 @@ # Changelog | ||
## [3.0.0](https://www.github.com/googleapis/gcp-metadata/compare/v2.0.4...v3.0.0) (2019-09-17) | ||
### ⚠ BREAKING CHANGES | ||
* isAvailable now tries both DNS and IP, choosing whichever responds first (#239) | ||
### Features | ||
* isAvailable now tries both DNS and IP, choosing whichever responds first ([#239](https://www.github.com/googleapis/gcp-metadata/issues/239)) ([25bc116](https://www.github.com/googleapis/gcp-metadata/commit/25bc116)) | ||
### [2.0.4](https://www.github.com/googleapis/gcp-metadata/compare/v2.0.3...v2.0.4) (2019-09-13) | ||
@@ -9,0 +20,0 @@ |
{ | ||
"name": "gcp-metadata", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"description": "Get the metadata from a Google Cloud Platform environment", | ||
@@ -20,3 +20,3 @@ "repository": "googleapis/gcp-metadata", | ||
"system-test": "mocha build/system-test --timeout 600000", | ||
"test": "c8 mocha build/test", | ||
"test": "c8 mocha --timeout=5000 build/test", | ||
"docs": "compodoc src/", | ||
@@ -23,0 +23,0 @@ "lint": "gts check && eslint '**/*.js'", |
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
38073
153