1 | /*!
|
2 | * Copyright 2022 Google LLC. All Rights Reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | import { AuthClient, GoogleAuth, GoogleAuthOptions } from 'google-auth-library';
|
17 | import { CredentialBody } from 'google-auth-library';
|
18 | import * as r from 'teeny-request';
|
19 | import { Duplex, DuplexOptions, Readable, Writable } from 'stream';
|
20 | import { Interceptor } from './service-object.js';
|
21 | /**
|
22 | * A unique symbol for providing a `gccl-gcs-cmd` value
|
23 | * for the `X-Goog-API-Client` header.
|
24 | *
|
25 | * E.g. the `V` in `X-Goog-API-Client: gccl-gcs-cmd/V`
|
26 | **/
|
27 | export declare const GCCL_GCS_CMD_KEY: unique symbol;
|
28 | export type ResponseBody = any;
|
29 | export interface DuplexifyOptions extends DuplexOptions {
|
30 | autoDestroy?: boolean;
|
31 | end?: boolean;
|
32 | }
|
33 | export interface Duplexify extends Duplex {
|
34 | readonly destroyed: boolean;
|
35 | setWritable(writable: Writable | false | null): void;
|
36 | setReadable(readable: Readable | false | null): void;
|
37 | }
|
38 | export interface DuplexifyConstructor {
|
39 | obj(writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify;
|
40 | new (writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify;
|
41 | (writable?: Writable | false | null, readable?: Readable | false | null, options?: DuplexifyOptions): Duplexify;
|
42 | }
|
43 | export interface ParsedHttpRespMessage {
|
44 | resp: r.Response;
|
45 | err?: ApiError;
|
46 | }
|
47 | export interface MakeAuthenticatedRequest {
|
48 | (reqOpts: DecorateRequestOptions): Duplexify;
|
49 | (reqOpts: DecorateRequestOptions, options?: MakeAuthenticatedRequestOptions): void | Abortable;
|
50 | (reqOpts: DecorateRequestOptions, callback?: BodyResponseCallback): void | Abortable;
|
51 | (reqOpts: DecorateRequestOptions, optionsOrCallback?: MakeAuthenticatedRequestOptions | BodyResponseCallback): void | Abortable | Duplexify;
|
52 | getCredentials: (callback: (err?: Error | null, credentials?: CredentialBody) => void) => void;
|
53 | authClient: GoogleAuth<AuthClient>;
|
54 | }
|
55 | export interface Abortable {
|
56 | abort(): void;
|
57 | }
|
58 | export type AbortableDuplex = Duplexify & Abortable;
|
59 | export interface PackageJson {
|
60 | name: string;
|
61 | version: string;
|
62 | }
|
63 | export interface MakeAuthenticatedRequestFactoryConfig extends Omit<GoogleAuthOptions, 'authClient'> {
|
64 | /**
|
65 | * Automatically retry requests if the response is related to rate limits or
|
66 | * certain intermittent server errors. We will exponentially backoff
|
67 | * subsequent requests by default. (default: true)
|
68 | */
|
69 | autoRetry?: boolean;
|
70 | /**
|
71 | * If true, just return the provided request options. Default: false.
|
72 | */
|
73 | customEndpoint?: boolean;
|
74 | /**
|
75 | * If true, will authenticate when using a custom endpoint. Default: false.
|
76 | */
|
77 | useAuthWithCustomEndpoint?: boolean;
|
78 | /**
|
79 | * Account email address, required for PEM/P12 usage.
|
80 | */
|
81 | email?: string;
|
82 | /**
|
83 | * Maximum number of automatic retries attempted before returning the error.
|
84 | * (default: 3)
|
85 | */
|
86 | maxRetries?: number;
|
87 | stream?: Duplexify;
|
88 | /**
|
89 | * A pre-instantiated `AuthClient` or `GoogleAuth` client that should be used.
|
90 | * A new client will be created if this is not set.
|
91 | */
|
92 | authClient?: AuthClient | GoogleAuth;
|
93 | /**
|
94 | * Determines if a projectId is required for authenticated requests. Defaults to `true`.
|
95 | */
|
96 | projectIdRequired?: boolean;
|
97 | }
|
98 | export interface MakeAuthenticatedRequestOptions {
|
99 | onAuthenticated: OnAuthenticatedCallback;
|
100 | }
|
101 | export interface OnAuthenticatedCallback {
|
102 | (err: Error | null, reqOpts?: DecorateRequestOptions): void;
|
103 | }
|
104 | export interface GoogleErrorBody {
|
105 | code: number;
|
106 | errors?: GoogleInnerError[];
|
107 | response: r.Response;
|
108 | message?: string;
|
109 | }
|
110 | export interface GoogleInnerError {
|
111 | reason?: string;
|
112 | message?: string;
|
113 | }
|
114 | export interface MakeWritableStreamOptions {
|
115 | /**
|
116 | * A connection instance used to get a token with and send the request
|
117 | * through.
|
118 | */
|
119 | connection?: {};
|
120 | /**
|
121 | * Metadata to send at the head of the request.
|
122 | */
|
123 | metadata?: {
|
124 | contentType?: string;
|
125 | };
|
126 | /**
|
127 | * Request object, in the format of a standard Node.js http.request() object.
|
128 | */
|
129 | request?: r.Options;
|
130 | makeAuthenticatedRequest(reqOpts: r.OptionsWithUri & {
|
131 | [GCCL_GCS_CMD_KEY]?: string;
|
132 | }, fnobj: {
|
133 | onAuthenticated(err: Error | null, authenticatedReqOpts?: r.Options): void;
|
134 | }): void;
|
135 | }
|
136 | export interface DecorateRequestOptions extends r.CoreOptions {
|
137 | autoPaginate?: boolean;
|
138 | autoPaginateVal?: boolean;
|
139 | objectMode?: boolean;
|
140 | maxRetries?: number;
|
141 | uri: string;
|
142 | interceptors_?: Interceptor[];
|
143 | shouldReturnStream?: boolean;
|
144 | projectId?: string;
|
145 | [GCCL_GCS_CMD_KEY]?: string;
|
146 | }
|
147 | export interface ParsedHttpResponseBody {
|
148 | body: ResponseBody;
|
149 | err?: Error;
|
150 | }
|
151 | /**
|
152 | * Custom error type for API errors.
|
153 | *
|
154 | * @param {object} errorBody - Error object.
|
155 | */
|
156 | export declare class ApiError extends Error {
|
157 | code?: number;
|
158 | errors?: GoogleInnerError[];
|
159 | response?: r.Response;
|
160 | constructor(errorMessage: string);
|
161 | constructor(errorBody: GoogleErrorBody);
|
162 | /**
|
163 | * Pieces together an error message by combining all unique error messages
|
164 | * returned from a single GoogleError
|
165 | *
|
166 | * @private
|
167 | *
|
168 | * @param {GoogleErrorBody} err The original error.
|
169 | * @param {GoogleInnerError[]} [errors] Inner errors, if any.
|
170 | * string}
{ |
171 | */
|
172 | static createMultiErrorMessage(err: GoogleErrorBody, errors?: GoogleInnerError[]): string;
|
173 | }
|
174 | /**
|
175 | * Custom error type for partial errors returned from the API.
|
176 | *
|
177 | * @param {object} b - Error object.
|
178 | */
|
179 | export declare class PartialFailureError extends Error {
|
180 | errors?: GoogleInnerError[];
|
181 | response?: r.Response;
|
182 | constructor(b: GoogleErrorBody);
|
183 | }
|
184 | export interface BodyResponseCallback {
|
185 | (err: Error | ApiError | null, body?: ResponseBody, res?: r.Response): void;
|
186 | }
|
187 | export interface RetryOptions {
|
188 | retryDelayMultiplier?: number;
|
189 | totalTimeout?: number;
|
190 | maxRetryDelay?: number;
|
191 | autoRetry?: boolean;
|
192 | maxRetries?: number;
|
193 | retryableErrorFn?: (err: ApiError) => boolean;
|
194 | }
|
195 | export interface MakeRequestConfig {
|
196 | /**
|
197 | * Automatically retry requests if the response is related to rate limits or
|
198 | * certain intermittent server errors. We will exponentially backoff
|
199 | * subsequent requests by default. (default: true)
|
200 | */
|
201 | autoRetry?: boolean;
|
202 | /**
|
203 | * Maximum number of automatic retries attempted before returning the error.
|
204 | * (default: 3)
|
205 | */
|
206 | maxRetries?: number;
|
207 | retries?: number;
|
208 | retryOptions?: RetryOptions;
|
209 | stream?: Duplexify;
|
210 | shouldRetryFn?: (response?: r.Response) => boolean;
|
211 | }
|
212 | export declare class Util {
|
213 | ApiError: typeof ApiError;
|
214 | PartialFailureError: typeof PartialFailureError;
|
215 | /**
|
216 | * No op.
|
217 | *
|
218 | * @example
|
219 | * function doSomething(callback) {
|
220 | * callback = callback || noop;
|
221 | * }
|
222 | */
|
223 | noop(): void;
|
224 | /**
|
225 | * Uniformly process an API response.
|
226 | *
|
227 | * @param {*} err - Error value.
|
228 | * @param {*} resp - Response value.
|
229 | * @param {*} body - Body value.
|
230 | * @param {function} callback - The callback function.
|
231 | */
|
232 | handleResp(err: Error | null, resp?: r.Response | null, body?: ResponseBody, callback?: BodyResponseCallback): void;
|
233 | /**
|
234 | * Sniff an incoming HTTP response message for errors.
|
235 | *
|
236 | * @param {object} httpRespMessage - An incoming HTTP response message from `request`.
|
237 | * @return {object} parsedHttpRespMessage - The parsed response.
|
238 | * @param {?error} parsedHttpRespMessage.err - An error detected.
|
239 | * @param {object} parsedHttpRespMessage.resp - The original response object.
|
240 | */
|
241 | parseHttpRespMessage(httpRespMessage: r.Response): ParsedHttpRespMessage;
|
242 | /**
|
243 | * Parse the response body from an HTTP request.
|
244 | *
|
245 | * @param {object} body - The response body.
|
246 | * @return {object} parsedHttpRespMessage - The parsed response.
|
247 | * @param {?error} parsedHttpRespMessage.err - An error detected.
|
248 | * @param {object} parsedHttpRespMessage.body - The original body value provided
|
249 | * will try to be JSON.parse'd. If it's successful, the parsed value will
|
250 | * be returned here, otherwise the original value and an error will be returned.
|
251 | */
|
252 | parseHttpRespBody(body: ResponseBody): ParsedHttpResponseBody;
|
253 | /**
|
254 | * Take a Duplexify stream, fetch an authenticated connection header, and
|
255 | * create an outgoing writable stream.
|
256 | *
|
257 | * @param {Duplexify} dup - Duplexify stream.
|
258 | * @param {object} options - Configuration object.
|
259 | * @param {module:common/connection} options.connection - A connection instance used to get a token with and send the request through.
|
260 | * @param {object} options.metadata - Metadata to send at the head of the request.
|
261 | * @param {object} options.request - Request object, in the format of a standard Node.js http.request() object.
|
262 | * @param {string=} options.request.method - Default: "POST".
|
263 | * @param {string=} options.request.qs.uploadType - Default: "multipart".
|
264 | * @param {string=} options.streamContentType - Default: "application/octet-stream".
|
265 | * @param {function} onComplete - Callback, executed after the writable Request stream has completed.
|
266 | */
|
267 | makeWritableStream(dup: Duplexify, options: MakeWritableStreamOptions, onComplete?: Function): void;
|
268 | /**
|
269 | * Returns true if the API request should be retried, given the error that was
|
270 | * given the first time the request was attempted. This is used for rate limit
|
271 | * related errors as well as intermittent server errors.
|
272 | *
|
273 | * @param {error} err - The API error to check if it is appropriate to retry.
|
274 | * @return {boolean} True if the API request should be retried, false otherwise.
|
275 | */
|
276 | shouldRetryRequest(err?: ApiError): boolean;
|
277 | /**
|
278 | * Get a function for making authenticated requests.
|
279 | *
|
280 | * @param {object} config - Configuration object.
|
281 | * @param {boolean=} config.autoRetry - Automatically retry requests if the
|
282 | * response is related to rate limits or certain intermittent server
|
283 | * errors. We will exponentially backoff subsequent requests by default.
|
284 | * (default: true)
|
285 | * @param {object=} config.credentials - Credentials object.
|
286 | * @param {boolean=} config.customEndpoint - If true, just return the provided request options. Default: false.
|
287 | * @param {boolean=} config.useAuthWithCustomEndpoint - If true, will authenticate when using a custom endpoint. Default: false.
|
288 | * @param {string=} config.email - Account email address, required for PEM/P12 usage.
|
289 | * @param {number=} config.maxRetries - Maximum number of automatic retries attempted before returning the error. (default: 3)
|
290 | * @param {string=} config.keyFile - Path to a .json, .pem, or .p12 keyfile.
|
291 | * @param {array} config.scopes - Array of scopes required for the API.
|
292 | */
|
293 | makeAuthenticatedRequestFactory(config: MakeAuthenticatedRequestFactoryConfig): MakeAuthenticatedRequest;
|
294 | /**
|
295 | * Make a request through the `retryRequest` module with built-in error
|
296 | * handling and exponential back off.
|
297 | *
|
298 | * @param {object} reqOpts - Request options in the format `request` expects.
|
299 | * @param {object=} config - Configuration object.
|
300 | * @param {boolean=} config.autoRetry - Automatically retry requests if the
|
301 | * response is related to rate limits or certain intermittent server
|
302 | * errors. We will exponentially backoff subsequent requests by default.
|
303 | * (default: true)
|
304 | * @param {number=} config.maxRetries - Maximum number of automatic retries
|
305 | * attempted before returning the error. (default: 3)
|
306 | * @param {object=} config.request - HTTP module for request calls.
|
307 | * @param {function} callback - The callback function.
|
308 | */
|
309 | makeRequest(reqOpts: DecorateRequestOptions, config: MakeRequestConfig, callback: BodyResponseCallback): void | Abortable;
|
310 | /**
|
311 | * Decorate the options about to be made in a request.
|
312 | *
|
313 | * @param {object} reqOpts - The options to be passed to `request`.
|
314 | * @param {string} projectId - The project ID.
|
315 | * @return {object} reqOpts - The decorated reqOpts.
|
316 | */
|
317 | decorateRequest(reqOpts: DecorateRequestOptions, projectId: string): DecorateRequestOptions;
|
318 | isCustomType(unknown: any, module: string): boolean;
|
319 | /**
|
320 | * Given two parameters, figure out if this is either:
|
321 | * - Just a callback function
|
322 | * - An options object, and then a callback function
|
323 | * @param optionsOrCallback An options object or callback.
|
324 | * @param cb A potentially undefined callback.
|
325 | */
|
326 | maybeOptionsOrCallback<T = {}, C = (err?: Error) => void>(optionsOrCallback?: T | C, cb?: C): [T, C];
|
327 | _getDefaultHeaders(gcclGcsCmd?: string): {
|
328 | 'User-Agent': string;
|
329 | 'x-goog-api-client': string;
|
330 | };
|
331 | }
|
332 | declare const util: Util;
|
333 | export { util };
|