Skip to content

Commit 56c1c49

Browse files
authored
Merge pull request #230 from supabase/feat/use-storage-zone-to-allow-large-uploads
feat: use dedicated storage host for storage lib (allows >50GB uploads)
2 parents 1187e0c + 99b9de3 commit 56c1c49

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/packages/StorageBucketApi.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@ export default class StorageBucketApi {
1010
protected fetch: Fetch
1111

1212
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
13-
this.url = url
13+
const baseUrl = new URL(url)
14+
15+
// if legacy uri is used, replace with new storage host (disables request buffering to allow > 50GB uploads)
16+
// "project-ref.supabase.co/storage/v1" becomes "project-ref.storage.supabase.co/v1"
17+
const isSupabaseHost = /supabase\.(co|in|red)$/.test(baseUrl.hostname)
18+
const legacyStoragePrefix = '/storage'
19+
if (
20+
isSupabaseHost &&
21+
!baseUrl.hostname.includes('storage.supabase.') &&
22+
baseUrl.pathname.startsWith(legacyStoragePrefix)
23+
) {
24+
baseUrl.pathname = baseUrl.pathname.substring(legacyStoragePrefix.length)
25+
baseUrl.hostname = baseUrl.hostname.replace('supabase.', 'storage.supabase.')
26+
}
27+
28+
this.url = baseUrl.href
1429
this.headers = { ...DEFAULT_HEADERS, ...headers }
1530
this.fetch = resolveFetch(fetch)
1631
}

test/storageBucketApi.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,43 @@ describe('Bucket API Error Handling', () => {
3333
jest.restoreAllMocks()
3434
})
3535

36+
describe('URL Construction', () => {
37+
const urlTestCases = [
38+
[
39+
'https://blah.supabase.co/storage/v1',
40+
'https://blah.storage.supabase.co/v1',
41+
'update legacy prod host to new host',
42+
],
43+
[
44+
'https://blah.supabase.red/storage/v1',
45+
'https://blah.storage.supabase.red/v1',
46+
'update legacy staging host to new host',
47+
],
48+
[
49+
'https://blah.storage.supabase.co/v1',
50+
'https://blah.storage.supabase.co/v1',
51+
'accept new host without modification',
52+
],
53+
[
54+
'https://blah.supabase.co.example.com/storage/v1',
55+
'https://blah.supabase.co.example.com/storage/v1',
56+
'not modify non-platform hosts',
57+
],
58+
[
59+
'http://localhost:1234/storage/v1',
60+
'http://localhost:1234/storage/v1',
61+
'support local host with port without modification',
62+
],
63+
]
64+
65+
urlTestCases.forEach(([inputUrl, expectUrl, description]) => {
66+
it('should ' + description, () => {
67+
const storage = new StorageClient(inputUrl, { apikey: KEY })
68+
expect(storage['url']).toBe(expectUrl)
69+
})
70+
})
71+
})
72+
3673
describe('listBuckets', () => {
3774
it('handles network errors', async () => {
3875
const mockError = new Error('Network failure')

0 commit comments

Comments
 (0)