Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): Bump gaxios #649

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"author": "Google LLC",
"license": "Apache-2.0",
"dependencies": {
"gaxios": "^6.7.1",
"gaxios": "^7.0.0-rc.1",
"google-logging-utils": "^1.0.0",
"json-bigint": "^1.0.0"
},
Expand Down
27 changes: 16 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import {GaxiosError, GaxiosOptions, GaxiosResponse, request} from 'gaxios';
import {OutgoingHttpHeaders} from 'http';
import jsonBigint = require('json-bigint');
import {detectGCPResidency} from './gcp-residency';
import * as logger from 'google-logging-utils';
Expand Down Expand Up @@ -47,7 +46,7 @@ export const METADATA_SERVER_DETECTION = Object.freeze({
export interface Options {
params?: {[index: string]: string};
property?: string;
headers?: OutgoingHttpHeaders;
headers?: HeadersInit;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HeadersInit is a broader, spec-compliant type when compared to OutgoingHttpHeaders.

}

export interface MetadataAccessor {
Expand Down Expand Up @@ -124,16 +123,19 @@ async function metadataAccessor<T>(
noResponseRetries = 3,
fastFail = false,
): Promise<T> {
const headers = new Headers(HEADERS);
let metadataKey = '';
let params: {} = {};
let headers: OutgoingHttpHeaders = {};

if (typeof type === 'object') {
const metadataAccessor: MetadataAccessor = type;

new Headers(metadataAccessor.headers).forEach((value, key) =>
Copy link
Contributor Author

@d-goog d-goog Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging headers; matching existing behavior (as seen on line 159 on the current state).

headers.set(key, value),
);

metadataKey = metadataAccessor.metadataKey;
params = metadataAccessor.params || params;
headers = metadataAccessor.headers || headers;
noResponseRetries = metadataAccessor.noResponseRetries || noResponseRetries;
fastFail = metadataAccessor.fastFail || fastFail;
} else {
Expand All @@ -149,14 +151,16 @@ async function metadataAccessor<T>(
metadataKey += `/${options.property}`;
}

headers = options.headers || headers;
new Headers(options.headers).forEach((value, key) =>
headers.set(key, value),
);
params = options.params || params;
}

const requestMethod = fastFail ? fastFailMetadataRequest : request;
const req: GaxiosOptions = {
url: `${getBaseUrl()}/${metadataKey}`,
headers: {...HEADERS, ...headers},
headers,
retryConfig: {noResponseRetries},
params,
responseType: 'text',
Expand All @@ -166,10 +170,11 @@ async function metadataAccessor<T>(

const res = await requestMethod<T>(req);
log.info('instance metadata is %s', res.data);
// NOTE: node.js converts all incoming headers to lower case.
if (res.headers[HEADER_NAME.toLowerCase()] !== HEADER_VALUE) {
throw new Error(
`Invalid response from metadata service: incorrect ${HEADER_NAME} header. Expected '${HEADER_VALUE}', got ${res.headers[HEADER_NAME.toLowerCase()] ? `'${res.headers[HEADER_NAME.toLowerCase()]}'` : 'no header'}`,

const metadataFlavor = res.headers.get(HEADER_NAME);
if (metadataFlavor !== HEADER_VALUE) {
throw new RangeError(
`Invalid response from metadata service: incorrect ${HEADER_NAME} header. Expected '${HEADER_VALUE}', got ${metadataFlavor ? `'${metadataFlavor}'` : 'no header'}`,
);
}

Expand Down Expand Up @@ -197,7 +202,7 @@ async function fastFailMetadataRequest<T>(
// reasons for this:
//
// 1. the DNS is slow in some GCP environments; by checking both, we might
// detect the runtime environment signficantly faster.
// detect the runtime environment significantly faster.
// 2. we can't just check the IP, which is tarpitted and slow to respond
// on a user's local machine.
//
Expand Down