Skip to content

Commit 0dee601

Browse files
authored
fix: optionally use storage new hostname (#234)
1 parent f7b40b3 commit 0dee601

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/StorageClient.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import StorageBucketApi from './packages/StorageBucketApi'
33
import { Fetch } from './lib/fetch'
44

55
export class StorageClient extends StorageBucketApi {
6-
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
7-
super(url, headers, fetch)
6+
constructor(
7+
url: string,
8+
headers: { [key: string]: string } = {},
9+
fetch?: Fetch,
10+
opts?: { useNewHostname?: boolean }
11+
) {
12+
super(url, headers, fetch, opts)
813
}
914

1015
/**

src/packages/StorageBucketApi.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,27 @@ export default class StorageBucketApi {
99
protected headers: { [key: string]: string }
1010
protected fetch: Fetch
1111

12-
constructor(url: string, headers: { [key: string]: string } = {}, fetch?: Fetch) {
12+
constructor(
13+
url: string,
14+
headers: { [key: string]: string } = {},
15+
fetch?: Fetch,
16+
opts?: { useNewHostname?: boolean }
17+
) {
1318
const baseUrl = new URL(url)
1419

1520
// if legacy uri is used, replace with new storage host (disables request buffering to allow > 50GB uploads)
1621
// "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.')
22+
if (opts?.useNewHostname) {
23+
const isSupabaseHost = /supabase\.(co|in|red)$/.test(baseUrl.hostname)
24+
const legacyStoragePrefix = '/storage'
25+
if (
26+
isSupabaseHost &&
27+
!baseUrl.hostname.includes('storage.supabase.') &&
28+
baseUrl.pathname.startsWith(legacyStoragePrefix)
29+
) {
30+
baseUrl.pathname = baseUrl.pathname.substring(legacyStoragePrefix.length)
31+
baseUrl.hostname = baseUrl.hostname.replace('supabase.', 'storage.supabase.')
32+
}
2633
}
2734

2835
this.url = baseUrl.href

test/storageBucketApi.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ describe('Bucket API Error Handling', () => {
6464

6565
urlTestCases.forEach(([inputUrl, expectUrl, description]) => {
6666
it('should ' + description, () => {
67-
const storage = new StorageClient(inputUrl, { apikey: KEY })
67+
const storage = new StorageClient(inputUrl, { apikey: KEY }, undefined, {
68+
useNewHostname: true,
69+
})
6870
expect(storage['url']).toBe(expectUrl)
6971
})
7072
})

0 commit comments

Comments
 (0)