UNPKG

@google-cloud/storage

Version:
182 lines (181 loc) • 6.75 kB
/*! * Copyright 2022 Google LLC. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*! * @module common/util */ import { AuthClient, GoogleAuth, GoogleAuthOptions } from 'google-auth-library'; import { Duplex, DuplexOptions, Readable, Transform, Writable } from 'stream'; import { GaxiosError, GaxiosResponse } from 'gaxios'; /** * A unique symbol for providing a `gccl-gcs-cmd` value * for the `X-Goog-API-Client` header. * * E.g. the `V` in `X-Goog-API-Client: gccl-gcs-cmd/V` **/ export declare const GCCL_GCS_CMD_KEY: unique symbol; export type ResponseBody = any; export type Headers = { [header: string]: any; }; export interface DuplexifyOptions extends DuplexOptions { autoDestroy?: boolean; end?: boolean; } export interface Duplexify extends Duplex { readonly destroyed: boolean; setWritable(writable: Writable | false | null): void; setReadable(readable: Readable | false | null): void; } export interface DuplexifyConstructor { obj(writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify; new (writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify; (writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify; } export interface ParsedHttpRespMessage { resp: GaxiosResponse; err?: GaxiosError; } export interface Abortable { abort(): void; } export type AbortableDuplex = Duplexify & Abortable; export interface PackageJson { name: string; version: string; } export interface MakeAuthenticatedRequestFactoryConfig extends Omit<GoogleAuthOptions, 'authClient'> { /** * Automatically retry requests if the response is related to rate limits or * certain intermittent server errors. We will exponentially backoff * subsequent requests by default. (default: true) */ autoRetry?: boolean; /** * If true, just return the provided request options. Default: false. */ customEndpoint?: boolean; /** * If true, will authenticate when using a custom endpoint. Default: false. */ useAuthWithCustomEndpoint?: boolean; /** * Account email address, required for PEM/P12 usage. */ email?: string; /** * Maximum number of automatic retries attempted before returning the error. * (default: 3) */ maxRetries?: number; stream?: Duplexify; /** * A pre-instantiated `AuthClient` or `GoogleAuth` client that should be used. * A new client will be created if this is not set. */ authClient?: AuthClient | GoogleAuth<AuthClient>; /** * Determines if a projectId is required for authenticated requests. Defaults to `true`. */ projectIdRequired?: boolean; } export interface GoogleErrorBody { code: number; errors?: GoogleInnerError[]; response: GaxiosResponse; message?: string; } export interface GoogleInnerError { reason?: string; message?: string; } export interface ParsedHttpResponseBody { body: ResponseBody; err?: Error; } export interface BodyResponseCallback { (err: GaxiosError | null, body?: ResponseBody, res?: GaxiosResponse): void; } export interface RetryOptions { retryDelayMultiplier?: number; totalTimeout?: number; maxRetryDelay?: number; autoRetry?: boolean; maxRetries?: number; retryableErrorFn?: (err: GaxiosError) => boolean; } export declare class Util { /** * No op. * * @example * function doSomething(callback) { * callback = callback || noop; * } */ noop(): void; /** * Returns true if the API request should be retried, given the error that was * given the first time the request was attempted. This is used for rate limit * related errors as well as intermittent server errors. * * @param {error} err - The API error to check if it is appropriate to retry. * @return {boolean} True if the API request should be retried, false otherwise. */ shouldRetryRequest(err?: GaxiosError): boolean; isCustomType(unknown: any, module: string): boolean; /** * Given two parameters, figure out if this is either: * - Just a callback function * - An options object, and then a callback function * @param optionsOrCallback An options object or callback. * @param cb A potentially undefined callback. */ maybeOptionsOrCallback<T = {}, C = (err?: Error) => void>(optionsOrCallback?: T | C, cb?: C): [T, C]; decorateHeaders(headers?: Headers, options?: DecorateHeadersOptions): DecorateHeadersResult; _getDefaultHeaders(gcclGcsCmd?: string): Headers; } export interface DecorateHeadersOptions { idempotencyToken?: string; packageJson?: PackageJson; providedUserAgent?: string; gcclGcsCmd?: string; } export interface DecorateHeadersResult { headers: Headers; idempotencyToken: string; } /** * Decorates and sanitizes headers for GCS requests: * - Checks for user-provided `x-goog-gcs-idempotency-token` case-insensitively. * - If a valid non-empty string user token is provided, uses it as the idempotency token and preserves the header. * - If not provided or invalid, removes any invalid header key and sets `x-goog-gcs-idempotency-token` to either the provided fallback token or a generated UUID. * - Adds `User-Agent` and `x-goog-api-client` (with tracking string, package version, gccl-invocation-id, and optional gccl-gcs-cmd). * * @param headers Existing headers object (optional). * @param options Decoration options (idempotencyToken, packageJson, providedUserAgent, gcclGcsCmd). * @returns An object containing the decorated headers and the effective idempotency token. */ export declare function decorateHeaders(headers?: Headers, options?: DecorateHeadersOptions): DecorateHeadersResult; /** * Basic Passthrough Stream that records the number of bytes read * every time the cursor is moved. */ export declare class ProgressStream extends Transform { bytesRead: number; _transform(chunk: any, encoding: string, callback: Function): void; } declare const util: Util; export { util };