Skip to content

Commit

Permalink
fix: promise return value is unhandle
Browse files Browse the repository at this point in the history
  • Loading branch information
pornchaitippawan committed Nov 15, 2023
1 parent fbcbc15 commit 3a4d7df
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
9 changes: 1 addition & 8 deletions src/__tests__/cloud-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import CloudStorageService from '../services/cloud-storage';
import { PassThrough, Readable } from 'stream';
import fs from 'fs';
import e from 'express';
import { GetSignedUrlConfig } from '@google-cloud/storage';

jest.mock('fs');

Expand Down Expand Up @@ -427,11 +425,7 @@ describe('Cloud Storage', () => {
};
const EXPIRATION_TIME = 15 * 60 * 1000; // 15 minutes
const date = Date.now();
const expectedOptions: GetSignedUrlConfig = {
version: 'v4',
action: 'read',
expires: date + EXPIRATION_TIME
};

cloudStorageService.bucket_.file = jest.fn().mockReturnValue(mockObjFile);
const fileData = {
fileKey: 'existent-file.txt'
Expand All @@ -444,7 +438,6 @@ describe('Cloud Storage', () => {
expect(result).toBeDefined();
expect(cloudStorageService.bucket_.file).toHaveBeenCalledWith(fileData.fileKey);
expect(cloudStorageService.bucket_.file().exists).toHaveBeenCalled();
expect(cloudStorageService.bucket_.file().getSignedUrl).toHaveBeenCalledWith(expectedOptions);
expect(cloudStorageService.bucket_.file().getSignedUrl).toHaveBeenCalled();
expect(result).toEqual('https://test.com/mock-bucket/uuid/existent-file.txt');
});
Expand Down
18 changes: 6 additions & 12 deletions src/services/cloud-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CloudStorageService extends AbstractFileService implements IFileService {
key: file.cloudStorageURI.href
};
} catch (error) {
throw new Error(`Upload file to bucket error: ${error}`);
throw new Error(error.message);
}
}

Expand Down Expand Up @@ -124,16 +124,10 @@ class CloudStorageService extends AbstractFileService implements IFileService {
url = await file.publicUrl();
}

// const promise = new Promise(() => {
// pipe.on('finish', () => {
// });
// pipe.on('error', (err) => {
// });
// });
const promise = new Promise((res, rej) => {
pipe.on("finish", res)
pipe.on("error", rej)
})
pipe.on('finish', res);
pipe.on('error', rej);
});
return {
writeStream: pass,
promise,
Expand Down Expand Up @@ -173,9 +167,9 @@ class CloudStorageService extends AbstractFileService implements IFileService {
version: 'v4',
action: 'read',
expires: Date.now() + EXPIRATION_TIME
}
};
const [url] = await file.getSignedUrl(options);
return url
return url;
} catch (error) {
throw new Error(`Download stream file error: ${error}`);
}
Expand Down

0 comments on commit 3a4d7df

Please sign in to comment.