Skip to content

Commit

Permalink
chore(cleanup)!: Remove redundant callback types, move storage.ts to …
Browse files Browse the repository at this point in the history
…calling gaxios
  • Loading branch information
ddelgrosso1 committed Apr 24, 2024
1 parent 2eacda8 commit 62604ae
Show file tree
Hide file tree
Showing 12 changed files with 331 additions and 168 deletions.
4 changes: 2 additions & 2 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
};

super({
parent: storage,
//parent: storage,
baseUrl: '/b',
id: name,
createMethod: storage.createBucket.bind(storage),
Expand Down Expand Up @@ -3169,7 +3169,7 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {

if (!this.signer) {
this.signer = new URLSigner(
this.storage.authClient,
this.storage.storageTransport.authClient,
this,
undefined,
this.storage
Expand Down
39 changes: 21 additions & 18 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ class File extends ServiceObject<File, FileMetadata> {
};

super({
parent: bucket,
//parent: bucket,
baseUrl: '/o',
id: encodeURIComponent(name),
methods,
Expand Down Expand Up @@ -1790,7 +1790,7 @@ class File extends ServiceObject<File, FileMetadata> {

resumableUpload.createURI(
{
authClient: this.storage.authClient,
authClient: this.storage.storageTransport.authClient,
apiEndpoint: this.storage.apiEndpoint,
bucket: this.bucket.name,
customRequestOptions: this.getRequestInterceptors().reduce(
Expand Down Expand Up @@ -2691,18 +2691,20 @@ class File extends ServiceObject<File, FileMetadata> {
const policyString = JSON.stringify(policy);
const policyBase64 = Buffer.from(policyString).toString('base64');

this.storage.authClient.sign(policyBase64, options.signingEndpoint).then(
signature => {
callback(null, {
string: policyString,
base64: policyBase64,
signature,
});
},
err => {
callback(new SigningError(err.message));
}
);
this.storage.storageTransport.authClient
.sign(policyBase64, options.signingEndpoint)
.then(
signature => {
callback(null, {
string: policyString,
base64: policyBase64,
signature,
});
},
err => {
callback(new SigningError(err.message));
}
);
}

generateSignedPostPolicyV4(
Expand Down Expand Up @@ -2841,7 +2843,8 @@ class File extends ServiceObject<File, FileMetadata> {
const todayISO = formatAsUTCISO(now);

const sign = async () => {
const {client_email} = await this.storage.authClient.getCredentials();
const {client_email} =
await this.storage.storageTransport.authClient.getCredentials();
const credential = `${client_email}/${todayISO}/auto/storage/goog4_request`;

fields = {
Expand Down Expand Up @@ -2874,7 +2877,7 @@ class File extends ServiceObject<File, FileMetadata> {
const policyBase64 = Buffer.from(policyString).toString('base64');

try {
const signature = await this.storage.authClient.sign(
const signature = await this.storage.storageTransport.authClient.sign(
policyBase64,
options.signingEndpoint
);
Expand Down Expand Up @@ -3132,7 +3135,7 @@ class File extends ServiceObject<File, FileMetadata> {

if (!this.signer) {
this.signer = new URLSigner(
this.storage.authClient,
this.storage.storageTransport.authClient,
this.bucket,
this,
this.storage
Expand Down Expand Up @@ -4134,7 +4137,7 @@ class File extends ServiceObject<File, FileMetadata> {
}

const uploadStream = resumableUpload.upload({
authClient: this.storage.authClient,
authClient: this.storage.storageTransport.authClient,
apiEndpoint: this.storage.apiEndpoint,
bucket: this.bucket.name,
customRequestOptions: this.getRequestInterceptors().reduce(
Expand Down
3 changes: 2 additions & 1 deletion src/hmacKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class HmacKey extends ServiceObject<HmacKey, HmacKeyMetadata> {
*/
storage: Storage;
private instanceRetryValue?: boolean;
secret?: string;

/**
* @typedef {object} HmacKeyOptions
Expand Down Expand Up @@ -350,7 +351,7 @@ export class HmacKey extends ServiceObject<HmacKey, HmacKeyMetadata> {
const projectId = (options && options.projectId) || storage.projectId;

super({
parent: storage,
//parent: storage,
id: accessId,
baseUrl: `/projects/${projectId}/hmacKeys`,
methods,
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,16 @@
*/
export {ApiError} from './nodejs-common/index.js';
export {
BucketCallback,
BucketOptions,
CreateBucketQuery,
CreateBucketRequest,
CreateBucketResponse,
CreateHmacKeyCallback,
CreateHmacKeyOptions,
CreateHmacKeyResponse,
GetBucketsCallback,
GetBucketsRequest,
GetBucketsResponse,
GetHmacKeysCallback,
GetHmacKeysOptions,
GetHmacKeysResponse,
GetServiceAccountCallback,
GetServiceAccountOptions,
GetServiceAccountResponse,
HmacKeyResourceResponse,
Expand Down Expand Up @@ -265,3 +260,4 @@ export {
} from './notification.js';
export {GetSignedUrlCallback, GetSignedUrlResponse} from './signer.js';
export * from './transfer-manager.js';
export * from './storage-transport.js';
13 changes: 7 additions & 6 deletions src/nodejs-common/service-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface ServiceObjectConfig {
* The parent service instance. For example, an instance of Storage if the
* object is Bucket.
*/
parent: ServiceObjectParent;
//parent: ServiceObjectParent;

/**
* Override of projectId, used to allow access to resources in another project.
Expand Down Expand Up @@ -159,7 +159,7 @@ export interface BaseMetadata {
class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
metadata: K;
baseUrl?: string;
parent: ServiceObjectParent;
//parent: ServiceObjectParent;
id?: string;
private createMethod?: Function;
protected methods: Methods;
Expand Down Expand Up @@ -188,7 +188,7 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
super();
this.metadata = {} as K;
this.baseUrl = config.baseUrl;
this.parent = config.parent; // Parent class.
//this.parent = config.parent; // Parent class.
this.id = config.id; // Name or ID (e.g. dataset ID, bucket name, etc).
this.createMethod = config.createMethod;
this.methods = config.methods || {};
Expand Down Expand Up @@ -471,7 +471,8 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
const localInterceptors = this.interceptors

Check warning on line 471 in src/nodejs-common/service-object.ts

View workflow job for this annotation

GitHub Actions / lint

'localInterceptors' is assigned a value but never used
.filter(interceptor => typeof interceptor.request === 'function')
.map(interceptor => interceptor.request);
return this.parent.getRequestInterceptors().concat(localInterceptors);
//return this.parent.getRequestInterceptors().concat(localInterceptors);
return [];
}

/**
Expand Down Expand Up @@ -580,9 +581,9 @@ class ServiceObject<T, K extends BaseMetadata> extends EventEmitter {
reqOpts.interceptors_ = childInterceptors.concat(localInterceptors);

if (reqOpts.shouldReturnStream) {
return this.parent.requestStream(reqOpts);
//return this.parent.requestStream(reqOpts);
}
this.parent.request(reqOpts, callback!);
//this.parent.request(reqOpts, callback!);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class Notification extends ServiceObject<Notification, NotificationMetadata> {
};

super({
parent: bucket,
//parent: bucket,
baseUrl: '/notificationConfigs',
id: id.toString(),
createMethod: bucket.createNotification.bind(bucket),
Expand Down
138 changes: 138 additions & 0 deletions src/storage-transport.ts
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;
}
}

0 comments on commit 62604ae

Please sign in to comment.