Skip to content

refactor: allow download as stream #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,14 @@ export default class StorageFileApi {
*
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
* @param options.transform Transform the asset before serving it to the client.
* @param options.stream If set to true, the response will be a ReadableStream. Otherwise, it will be a Blob (default).
*/
async download(
async download<Options extends { transform?: TransformOptions, stream?: boolean }>(
path: string,
options?: { transform?: TransformOptions }
options?: Options
): Promise<
| {
data: Blob
data: Options['stream'] extends true ? ReadableStream : Blob
error: null
}
| {
Expand All @@ -385,8 +386,16 @@ export default class StorageFileApi {
headers: this.headers,
noResolveJson: true,
})
const data = await res.blob()
return { data, error: null }

if (!options?.stream) {
const data = await res.blob()
return { data, error: null }
}

return {
data: res.body,
error: null,
}
} catch (error) {
if (isStorageError(error)) {
return { data: null, error }
Expand Down
10 changes: 10 additions & 0 deletions test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ describe('Object API', () => {
expect(res.data?.type).toEqual('text/plain;charset=utf-8')
})

test('downloads an object as a stream', async () => {
await storage.from(bucketName).upload(uploadPath, file)
const res = await storage.from(bucketName).download(uploadPath, {
stream: true,
})

expect(res.error).toBeNull()
expect(res.data).toBeInstanceOf(ReadableStream)
})

test('removes an object', async () => {
await storage.from(bucketName).upload(uploadPath, file)
const res = await storage.from(bucketName).remove([uploadPath])
Expand Down