Skip to content

Commit

Permalink
use storage transport in bucket.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 committed Jun 17, 2024
1 parent 8557206 commit b256785
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 544 deletions.
332 changes: 137 additions & 195 deletions src/bucket.ts

Large diffs are not rendered by default.

25 changes: 8 additions & 17 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import {
} from './storage-transport.js';
import {GaxiosError, GaxiosInterceptor, GaxiosOptions, Gaxios} from 'gaxios';

export type GetExpirationDateResponse = [Date];
export interface GetExpirationDateCallback {
(
err: Error | null,
Expand Down Expand Up @@ -237,12 +236,6 @@ export interface CreateResumableUploadOptions
[GCCL_GCS_CMD_KEY]?: resumableUpload.UploadConfig[typeof GCCL_GCS_CMD_KEY];
}

export type CreateResumableUploadResponse = [string];

export interface CreateResumableUploadCallback {
(err: Error | null, uri?: string): void;
}

export interface CreateWriteStreamOptions extends CreateResumableUploadOptions {
contentType?: string;
gzip?: string | boolean;
Expand Down Expand Up @@ -1669,12 +1662,12 @@ class File extends ServiceObject<File, FileMetadata> {

createResumableUpload(
options?: CreateResumableUploadOptions
): Promise<CreateResumableUploadResponse>;
): Promise<string>;
createResumableUpload(
options: CreateResumableUploadOptions,
callback: CreateResumableUploadCallback
callback: StorageCallback<string>
): void;
createResumableUpload(callback: CreateResumableUploadCallback): void;
createResumableUpload(callback: StorageCallback<string>): void;
/**
* @callback CreateResumableUploadCallback
* @param {?Error} err Request error, if any.
Expand Down Expand Up @@ -1760,11 +1753,9 @@ class File extends ServiceObject<File, FileMetadata> {
* ```
*/
createResumableUpload(
optionsOrCallback?:
| CreateResumableUploadOptions
| CreateResumableUploadCallback,
callback?: CreateResumableUploadCallback
): void | Promise<CreateResumableUploadResponse> {
optionsOrCallback?: CreateResumableUploadOptions | StorageCallback<string>,
callback?: StorageCallback<string>
): void | Promise<string> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
callback =
Expand Down Expand Up @@ -2448,7 +2439,7 @@ class File extends ServiceObject<File, FileMetadata> {
return cb ? reqPromise.then(resp => cb(null, resp)).catch(cb) : reqPromise;
}

getExpirationDate(): Promise<GetExpirationDateResponse>;
getExpirationDate(): Promise<Date>;
getExpirationDate(callback: GetExpirationDateCallback): void;
/**
* @typedef {array} GetExpirationDateResponse
Expand Down Expand Up @@ -2482,7 +2473,7 @@ class File extends ServiceObject<File, FileMetadata> {
*/
getExpirationDate(
callback?: GetExpirationDateCallback
): void | Promise<GetExpirationDateResponse> {
): void | Promise<Date> {
this.getMetadata(
(
err: GaxiosError | null,
Expand Down
30 changes: 0 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,64 +87,37 @@ export {
export {
Bucket,
BucketExistsOptions,
BucketExistsResponse,
BucketLockCallback,
BucketLockResponse,
BucketMetadata,
CombineCallback,
CombineOptions,
CombineResponse,
CreateChannelCallback,
CreateChannelConfig,
CreateChannelOptions,
CreateChannelResponse,
CreateNotificationCallback,
CreateNotificationOptions,
CreateNotificationResponse,
DeleteBucketOptions,
DeleteBucketResponse,
DeleteFilesCallback,
DeleteFilesOptions,
DeleteLabelsCallback,
DeleteLabelsResponse,
DisableRequesterPaysCallback,
DisableRequesterPaysResponse,
EnableRequesterPaysCallback,
EnableRequesterPaysResponse,
GetBucketCallback,
GetBucketMetadataCallback,
GetBucketMetadataOptions,
GetBucketMetadataResponse,
GetBucketOptions,
GetBucketResponse,
GetBucketSignedUrlConfig,
GetFilesCallback,
GetFilesOptions,
GetFilesResponse,
GetLabelsCallback,
GetLabelsOptions,
GetLabelsResponse,
GetNotificationsCallback,
GetNotificationsOptions,
GetNotificationsResponse,
Labels,
LifecycleAction,
LifecycleCondition,
LifecycleRule,
MakeBucketPrivateCallback,
MakeBucketPrivateOptions,
MakeBucketPrivateResponse,
MakeBucketPublicCallback,
MakeBucketPublicOptions,
MakeBucketPublicResponse,
SetBucketMetadataCallback,
SetBucketMetadataOptions,
SetBucketMetadataResponse,
SetBucketStorageClassCallback,
SetBucketStorageClassOptions,
SetLabelsCallback,
SetLabelsOptions,
SetLabelsResponse,
UploadCallback,
UploadOptions,
UploadResponse,
Expand All @@ -156,9 +129,7 @@ export {
CopyOptions,
CopyResponse,
CreateReadStreamOptions,
CreateResumableUploadCallback,
CreateResumableUploadOptions,
CreateResumableUploadResponse,
CreateWriteStreamOptions,
DeleteFileCallback,
DeleteFileOptions,
Expand All @@ -174,7 +145,6 @@ export {
FileMetadata,
FileOptions,
GetExpirationDateCallback,
GetExpirationDateResponse,
GetFileCallback,
GetFileMetadataCallback,
GetFileMetadataOptions,
Expand Down
24 changes: 11 additions & 13 deletions src/resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {FileMetadata} from './file.js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import {getPackageJSON} from './package-json-helper.cjs';
import {StorageCallback} from './storage-transport.js';

const NOT_FOUND_STATUS_CODE = 404;
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
Expand All @@ -52,7 +53,6 @@ export interface ErrorWithCode extends Error {
status?: number | string;
}

export type CreateUriCallback = (err: Error | null, uri?: string) => void;
export interface Encryption {
key: {};
hash: {};
Expand Down Expand Up @@ -266,11 +266,6 @@ export interface GoogleInnerError {
reason?: string;
}

export interface ApiError extends Error {
code?: number;
errors?: GoogleInnerError[];
}

export interface CheckUploadStatusConfig {
/**
* Set to `false` to disable retries within this method.
Expand Down Expand Up @@ -668,8 +663,8 @@ export class Upload extends Writable {
}

createURI(): Promise<string>;
createURI(callback: CreateUriCallback): void;
createURI(callback?: CreateUriCallback): void | Promise<string> {
createURI(callback: StorageCallback<string>): void;
createURI(callback?: StorageCallback<string>): void | Promise<string> {
if (!callback) {
return this.createURIAsync();
}
Expand Down Expand Up @@ -1012,11 +1007,11 @@ export class Upload extends Writable {
!this.isSuccessfulResponse(resp.status) &&
!shouldContinueUploadInAnotherRequest
) {
const err: ApiError = new Error('Upload failed');
err.code = resp.status;
const err: GaxiosError = new GaxiosError('Upload failed', {});
err.status = resp.status;
err.name = 'Upload failed';
if (resp?.data) {
err.errors = [resp?.data];
err.code = resp?.data;
}

this.destroy(err);
Expand Down Expand Up @@ -1292,10 +1287,13 @@ export function upload(cfg: UploadConfig) {
}

export function createURI(cfg: UploadConfig): Promise<string>;
export function createURI(cfg: UploadConfig, callback: CreateUriCallback): void;
export function createURI(
cfg: UploadConfig,
callback?: CreateUriCallback
callback: StorageCallback<string>
): void;
export function createURI(
cfg: UploadConfig,
callback?: StorageCallback<string>
): void | Promise<string> {
const up = new Upload(cfg);
if (!callback) {
Expand Down
14 changes: 7 additions & 7 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ describe('storage', function () {
});

it('should get an expiration date', async () => {
const [expirationDate] = await FILE.getExpirationDate();
const expirationDate = await FILE.getExpirationDate();
assert(expirationDate instanceof Date);
});
});
Expand Down Expand Up @@ -1710,7 +1710,7 @@ describe('storage', function () {
const PREFIX = 'sys-test';

it('should enable logging on current bucket by default', async () => {
const [metadata] = await bucket.enableLogging({prefix: PREFIX});
const metadata = await bucket.enableLogging({prefix: PREFIX});
assert.deepStrictEqual(metadata.logging, {
logBucket: bucket.id,
logObjectPrefix: PREFIX,
Expand All @@ -1721,7 +1721,7 @@ describe('storage', function () {
const bucketForLogging = storage.bucket(generateName());
await bucketForLogging.create();

const [metadata] = await bucket.enableLogging({
const metadata = await bucket.enableLogging({
bucket: bucketForLogging,
prefix: PREFIX,
});
Expand Down Expand Up @@ -1970,7 +1970,7 @@ describe('storage', function () {
});

it('bucket#createNotification', async () => {
const [notif] = await requesterPaysDoubleTest(async options => {
const notif = await requesterPaysDoubleTest(async options => {
return bucketNonAllowList.createNotification(topicName, options);
});

Expand Down Expand Up @@ -3049,7 +3049,7 @@ describe('storage', function () {
const sourceFiles = files.map(x => x.file);
let destinationFile = bucket.file('file-one-and-two.txt');

[destinationFile] = await bucket.combine(sourceFiles, destinationFile);
destinationFile = await bucket.combine(sourceFiles, destinationFile);
const [contents] = await destinationFile.download();
assert.strictEqual(
contents.toString(),
Expand Down Expand Up @@ -3738,7 +3738,7 @@ describe('storage', function () {
eventTypes: ['OBJECT_FINALIZE'],
}
);
notification = createNotificationData[0];
notification = createNotificationData;
subscription = topic.subscription(generateName());
await subscription.create();
});
Expand Down Expand Up @@ -3805,7 +3805,7 @@ describe('storage', function () {
eventTypes: ['OBJECT_DELETE'],
})
.then(data => {
notification = data[0];
notification = data;
return bucket.getNotifications();
})
.then(data => {
Expand Down
Loading

0 comments on commit b256785

Please sign in to comment.