Skip to content
Merged
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
13 changes: 12 additions & 1 deletion packages/core/storage-js/src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ export default class StorageFileApi extends BaseApiClient<StorageError> {
if (metadata) {
headers['x-metadata'] = this.toBase64(this.encodeMetadata(metadata))
}

const isStream =
(typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) ||
(body && typeof body === 'object' && 'pipe' in body && typeof body.pipe === 'function')

if (isStream && !options.duplex) {
options.duplex = 'half'
}
}

if (fileOptions?.headers) {
Expand All @@ -306,7 +314,10 @@ export default class StorageFileApi extends BaseApiClient<StorageError> {
}
}

const data = await put(this.fetch, url.toString(), body as object, { headers })
const data = await put(this.fetch, url.toString(), body as object, {
headers,
...(options?.duplex ? { duplex: options.duplex } : {}),
})

return { path: cleanPath, fullPath: data.Key }
})
Expand Down
20 changes: 20 additions & 0 deletions packages/core/storage-js/test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,5 +1281,25 @@ describe('StorageFileApi Edge Cases', () => {
const [, , body] = mockPut.mock.calls[0] as [null, null, FormData]
expect(body.getAll('cacheControl')).toEqual(['7200'])
})

test('uploadToSignedUrl auto-sets duplex to half when raw body is a stream', async () => {
const fakeStream = { pipe: () => {} } as unknown as NodeJS.ReadableStream

await storage.from('test-bucket').uploadToSignedUrl('test-path', 'test-token', fakeStream)

expect(mockPut).toHaveBeenCalled()
const [, , , options] = mockPut.mock.calls[0]
expect(options.duplex).toBe('half')
})

test('uploadToSignedUrl forwards caller-provided duplex', async () => {
await storage
.from('test-bucket')
.uploadToSignedUrl('test-path', 'test-token', 'test content', { duplex: 'half' })

expect(mockPut).toHaveBeenCalled()
const [, , , options] = mockPut.mock.calls[0]
expect(options.duplex).toBe('half')
})
})
})
13 changes: 13 additions & 0 deletions packages/core/storage-js/test/storageFileApiNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,18 @@ describe('Object API', () => {
expect(res.error).toBeNull()
expect(res.data?.path).toEqual(uploadPathWithDuplex)
})

test('uploadToSignedUrl auto-detects stream and sets duplex', async () => {
const uploadPath = `testpath/signed-stream-${Date.now()}.txt`

const signed = await storage.from(bucketName).createSignedUploadUrl(uploadPath)
expect(signed.error).toBeNull()
const token = signed.data!.token

const file = await fs.createReadStream(uploadFilePath('file.txt'))
const res = await storage.from(bucketName).uploadToSignedUrl(uploadPath, token, file)
expect(res.error).toBeNull()
expect(res.data?.path).toEqual(uploadPath)
})
})
})
Loading