-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cleanup)!: Remove redundant callback types, move storage.ts to …
…calling gaxios
- Loading branch information
1 parent
2eacda8
commit 62604ae
Showing
12 changed files
with
331 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import {GaxiosOptions, GaxiosResponse, Headers} from 'gaxios'; | ||
import {AuthClient, GoogleAuth, GoogleAuthOptions} from 'google-auth-library'; | ||
import { | ||
getModuleFormat, | ||
getRuntimeTrackingString, | ||
getUserAgentString, | ||
} from './util'; | ||
import {randomUUID} from 'crypto'; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import {getPackageJSON} from './package-json-helper.cjs'; | ||
import {GCCL_GCS_CMD_KEY} from './nodejs-common/util'; | ||
import {RetryOptions} from './storage'; | ||
|
||
interface StandardStorageQueryParams { | ||
alt?: 'json' | 'media'; | ||
callback?: string; | ||
fields?: string; | ||
key?: string; | ||
prettyPrint?: boolean; | ||
quotaUser?: string; | ||
userProject?: string; | ||
} | ||
|
||
export interface StorageQueryParameters extends StandardStorageQueryParams { | ||
[key: string]: string | boolean | undefined; | ||
} | ||
|
||
export interface StorageRequestOptions extends GaxiosOptions { | ||
[GCCL_GCS_CMD_KEY]?: string; | ||
queryParameters?: StorageQueryParameters; | ||
} | ||
|
||
export interface StorageCallback<T> { | ||
(err: Error | null, data?: T): void; | ||
} | ||
|
||
interface TransportParameters extends Omit<GoogleAuthOptions, 'authClient'> { | ||
apiEndpoint: string; | ||
authClient?: GoogleAuth | AuthClient; | ||
baseUrl: string; | ||
customEndpoint?: boolean; | ||
email?: string; | ||
packageJson: PackageJson; | ||
retryOptions: RetryOptions; | ||
scopes: string | string[]; | ||
timeout?: number; | ||
token?: string; | ||
useAuthWithCustomEndpoint?: boolean; | ||
userAgent?: string; | ||
} | ||
|
||
interface PackageJson { | ||
name: string; | ||
version: string; | ||
} | ||
|
||
export class StorageTransport { | ||
authClient: GoogleAuth<AuthClient>; | ||
providedUserAgent?: string; | ||
packageJson: PackageJson; | ||
retryOptions: RetryOptions; | ||
baseUrl: string; | ||
|
||
constructor(options: TransportParameters) { | ||
if (options.authClient instanceof GoogleAuth) { | ||
this.authClient = options.authClient; | ||
} else { | ||
this.authClient = new GoogleAuth({ | ||
...options, | ||
authClient: options.authClient, | ||
clientOptions: options.clientOptions, | ||
}); | ||
} | ||
this.providedUserAgent = options.userAgent; | ||
this.packageJson = getPackageJSON(); | ||
this.retryOptions = options.retryOptions; | ||
this.baseUrl = options.baseUrl; | ||
} | ||
|
||
makeRequest<T>( | ||
reqOpts: StorageRequestOptions, | ||
callback?: StorageCallback<T> | ||
): Promise<GaxiosResponse> | Promise<void> { | ||
const headers = this.#buildRequestHeaders(reqOpts.headers); | ||
if (reqOpts[GCCL_GCS_CMD_KEY]) { | ||
headers['x-goog-api-client'] += | ||
` gccl-gcs-cmd/${reqOpts[GCCL_GCS_CMD_KEY]}`; | ||
} | ||
const requestPromise = this.authClient.request({ | ||
//TODO: Retry Options | ||
...reqOpts, | ||
headers, | ||
url: this.#buildUrl(reqOpts.url?.toString(), reqOpts.queryParameters), | ||
}); | ||
|
||
return callback | ||
? requestPromise.then(resp => callback(null, resp.data)).catch(callback) | ||
: requestPromise; | ||
} | ||
|
||
#buildUrl(pathUri = '', queryParameters: StorageQueryParameters = {}): URL { | ||
const qp = this.#buildRequestQueryParams(queryParameters); | ||
const url = new URL(`${this.baseUrl}${pathUri}`); | ||
url.search = qp; | ||
|
||
return url; | ||
} | ||
|
||
#buildRequestHeaders(requestHeaders: Headers = {}) { | ||
const headers = { | ||
...requestHeaders, | ||
'User-Agent': this.#getUserAgentString(), | ||
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${ | ||
this.packageJson.version | ||
}-${getModuleFormat()} gccl-invocation-id/${randomUUID()}`, | ||
}; | ||
|
||
return headers; | ||
} | ||
|
||
#buildRequestQueryParams(queryParameters: StorageQueryParameters): string { | ||
const qp = new URLSearchParams( | ||
queryParameters as unknown as Record<string, string> | ||
); | ||
|
||
return qp.toString(); | ||
} | ||
|
||
#getUserAgentString(): string { | ||
let userAgent = getUserAgentString(); | ||
if (this.providedUserAgent) { | ||
userAgent = `${this.providedUserAgent} ${userAgent}`; | ||
} | ||
|
||
return userAgent; | ||
} | ||
} |
Oops, something went wrong.