Skip to content

Commit

Permalink
Merge pull request #28 from FleekHQ/feature/ch21234/add-progress-call…
Browse files Browse the repository at this point in the history
…-to-open-file

Feature: Add progress callback to openFile and openFileByUuid
  • Loading branch information
perfectmak authored Jan 27, 2021
2 parents 2209e7e + 3d74658 commit 08dc705
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
10 changes: 9 additions & 1 deletion etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export interface OpenFileRequest {
bucket: string;
// (undocumented)
path: string;
progress?: (bytesRead?: number) => void;
}

// @public (undocumented)
Expand All @@ -241,6 +242,13 @@ export interface OpenFileResponse {
stream: AsyncIterableIterator<Uint8Array>;
}

// @public (undocumented)
export interface OpenUuidFileRequest {
progress?: (bytesRead?: number) => void;
// (undocumented)
uuid: string;
}

// @public (undocumented)
export interface OpenUuidFileResponse {
consumeStream: () => Promise<Uint8Array>;
Expand Down Expand Up @@ -361,7 +369,7 @@ export class UserStorage {
initListener(): Promise<void>;
listDirectory(request: ListDirectoryRequest): Promise<ListDirectoryResponse>;
openFile(request: OpenFileRequest): Promise<OpenFileResponse>;
openFileByUuid(uuid: string): Promise<OpenUuidFileResponse>;
openFileByUuid(request: OpenUuidFileRequest): Promise<OpenUuidFileResponse>;
txlSubscribe(): Promise<TxlSubscribeResponse>;
}

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/storage_interactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('Users storing data', () => {
expect(file?.bucket).to.not.be.empty;
expect(file?.dbId).to.not.be.empty;

const fileResponse = await storage.openFileByUuid(file?.uuid || '');
const fileResponse = await storage.openFileByUuid({ uuid: file?.uuid || '' });
expect(fileResponse?.entry?.bucket).to.not.be.empty;
expect(fileResponse?.entry?.dbId).to.not.be.empty;
expect(fileResponse.entry.name).to.equal('top.txt');
Expand All @@ -221,7 +221,7 @@ describe('Users storing data', () => {
const { user: unauthorizedUser } = await authenticateAnonymousUser();
const unauthorizedStorage = new UserStorage(unauthorizedUser);

await expect(unauthorizedStorage.openFileByUuid(file?.uuid || ''))
await expect(unauthorizedStorage.openFileByUuid({ uuid: file?.uuid || '' }))
.to.eventually.be.rejectedWith(FileNotFoundError);
}).timeout(TestsDefaultTimeout);

Expand Down
16 changes: 16 additions & 0 deletions packages/storage/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ export interface ListDirectoryResponse {
export interface OpenFileRequest {
path: string;
bucket: string;
/**
* progress callback if provided will be called with bytes read from
* remote while opening the file.
*
*/
progress?: (bytesRead?: number) => void;
}

export interface OpenUuidFileRequest {
uuid: string;
/**
* progress callback if provided will be called with bytes read from
* remote while opening the file.
*
*/
progress?: (bytesRead?: number) => void;
}

export interface OpenFileResponse {
Expand Down
8 changes: 4 additions & 4 deletions packages/storage/src/userStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('UserStorage', () => {

it('should throw if file is not found', async () => {
const { storage, mockBuckets } = initStubbedStorage();
when(mockBuckets.pullPath('myBucketKey', '/file.txt')).thenThrow(
when(mockBuckets.pullPath('myBucketKey', '/file.txt', anything())).thenThrow(
new Error('Error: no link named "file.txt" under QmVQWu2C3ZgdoAmBsffFASrgynAfgvYX8CCK4o9SxRvC4p'),
);

Expand All @@ -212,7 +212,7 @@ describe('UserStorage', () => {
it('should return a valid stream of files data', async () => {
const { storage, mockBuckets } = initStubbedStorage();
const actualFileContent = "file.txt's file content";
when(mockBuckets.pullPath('myBucketKey', '/file.txt')).thenReturn(
when(mockBuckets.pullPath('myBucketKey', '/file.txt', anything())).thenReturn(
makeAsyncIterableString(actualFileContent) as AsyncIterableIterator<Uint8Array>,
);

Expand Down Expand Up @@ -253,11 +253,11 @@ describe('UserStorage', () => {
},
});

when(mockBuckets.pullPath('myBucketKey', anyString())).thenReturn(
when(mockBuckets.pullPath('myBucketKey', anyString(), anything())).thenReturn(
makeAsyncIterableString(actualFileContent) as AsyncIterableIterator<Uint8Array>,
);

const result = await storage.openFileByUuid(fileUuid);
const result = await storage.openFileByUuid({ uuid: fileUuid });
const filesData = await result.consumeStream();

expect(new TextDecoder('utf8').decode(filesData)).to.equal(actualFileContent);
Expand Down
29 changes: 19 additions & 10 deletions packages/storage/src/userStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { DirEntryNotFoundError, FileNotFoundError, UnauthenticatedError } from '
import { Listener } from './listener/listener';
import { GundbMetadataStore } from './metadata/gundbMetadataStore';
import { BucketMetadata, FileMetadata, UserMetadataStore } from './metadata/metadataStore';
import { AddItemsRequest,
import {
AddItemsRequest,
AddItemsResponse,
AddItemsResultSummary,
AddItemsStatus,
Expand All @@ -19,10 +20,12 @@ import { AddItemsRequest,
FileMember,
ListDirectoryRequest,
ListDirectoryResponse,
OpenUuidFileRequest,
OpenFileRequest,
OpenFileResponse,
OpenUuidFileResponse,
TxlSubscribeResponse } from './types';
TxlSubscribeResponse,
} from './types';
import { filePathFromIpfsPath,
getParentPath,
isTopLevelPath,
Expand Down Expand Up @@ -271,7 +274,7 @@ export class UserStorage {
const fileMetadata = await metadataStore.findFileMetadata(bucket.slug, bucket.dbId, path);

try {
const fileData = client.pullPath(bucket.root?.key || '', path);
const fileData = client.pullPath(bucket.root?.key || '', path, { progress: request.progress });
return {
stream: fileData,
consumeStream: () => consumeStream(fileData),
Expand All @@ -296,7 +299,9 @@ export class UserStorage {
* ```typescript
* const spaceStorage = new UserStorage(spaceUser);
*
* const response = await spaceStorage.openFileByUuid('file-uu-id');
* const response = await spaceStorage.openFileByUuid({
* uuid: 'file-uu-id',
* });
* const filename = response.entry.name;
*
* // response.stream is an async iterable
Expand All @@ -308,12 +313,11 @@ export class UserStorage {
* const fileBytes = await response.consumeStream();
*```
*
* @param uuid - Uuid of file to be open
*/
public async openFileByUuid(uuid: string): Promise<OpenUuidFileResponse> {
public async openFileByUuid(request: OpenUuidFileRequest): Promise<OpenUuidFileResponse> {
const metadataStore = await this.getMetadataStore();
const client = this.getUserBucketsClient();
const fileMetadata = await metadataStore.findFileMetadataByUuid(uuid);
const fileMetadata = await metadataStore.findFileMetadataByUuid(request.uuid);
if (!fileMetadata) {
throw new FileNotFoundError();
}
Expand All @@ -326,9 +330,14 @@ export class UserStorage {
try {
// fetch entry information
const existingFile = await client.listPath(existingRoot.key, fileMetadata.path);
const [fileEntry] = UserStorage.parsePathItems([existingFile.item!], { [fileMetadata.path]: fileMetadata }, fileMetadata.bucketSlug, fileMetadata.dbId);

const fileData = client.pullPath(existingRoot.key, fileMetadata.path);
const [fileEntry] = UserStorage.parsePathItems(
[existingFile.item!],
{ [fileMetadata.path]: fileMetadata },
fileMetadata.bucketSlug,
fileMetadata.dbId,
);

const fileData = client.pullPath(existingRoot.key, fileMetadata.path, { progress: request.progress });
return {
stream: fileData,
consumeStream: () => consumeStream(fileData),
Expand Down

0 comments on commit 08dc705

Please sign in to comment.