Skip to content

Commit 187e9c3

Browse files
committed
fix: us normal storage/v1 prefix with storage zone
1 parent 76d3adc commit 187e9c3

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/StorageClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import StorageFileApi from './packages/StorageFileApi'
22
import StorageBucketApi from './packages/StorageBucketApi'
33
import { Fetch } from './lib/fetch'
44

5+
export interface StorageClientOptions {
6+
useNewHostname?: boolean
7+
}
8+
59
export class StorageClient extends StorageBucketApi {
610
constructor(
711
url: string,
812
headers: { [key: string]: string } = {},
913
fetch?: Fetch,
10-
opts?: { useNewHostname?: boolean }
14+
opts?: StorageClientOptions
1115
) {
1216
super(url, headers, fetch, opts)
1317
}

src/packages/StorageBucketApi.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { isStorageError, StorageError } from '../lib/errors'
33
import { Fetch, get, post, put, remove } from '../lib/fetch'
44
import { resolveFetch } from '../lib/helpers'
55
import { Bucket, BucketType } from '../lib/types'
6+
import { StorageClientOptions } from '../StorageClient'
67

78
export default class StorageBucketApi {
89
protected url: string
@@ -13,21 +14,15 @@ export default class StorageBucketApi {
1314
url: string,
1415
headers: { [key: string]: string } = {},
1516
fetch?: Fetch,
16-
opts?: { useNewHostname?: boolean }
17+
opts?: StorageClientOptions
1718
) {
1819
const baseUrl = new URL(url)
1920

2021
// if legacy uri is used, replace with new storage host (disables request buffering to allow > 50GB uploads)
21-
// "project-ref.supabase.co/storage/v1" becomes "project-ref.storage.supabase.co/v1"
22+
// "project-ref.supabase.co" becomes "project-ref.storage.supabase.co"
2223
if (opts?.useNewHostname) {
2324
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)
25+
if (isSupabaseHost && !baseUrl.hostname.includes('storage.supabase.')) {
3126
baseUrl.hostname = baseUrl.hostname.replace('supabase.', 'storage.supabase.')
3227
}
3328
}

test/storageBucketApi.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ describe('Bucket API Error Handling', () => {
3737
const urlTestCases = [
3838
[
3939
'https://blah.supabase.co/storage/v1',
40-
'https://blah.storage.supabase.co/v1',
40+
'https://blah.storage.supabase.co/storage/v1',
4141
'update legacy prod host to new host',
4242
],
4343
[
4444
'https://blah.supabase.red/storage/v1',
45-
'https://blah.storage.supabase.red/v1',
45+
'https://blah.storage.supabase.red/storage/v1',
4646
'update legacy staging host to new host',
4747
],
4848
[
49-
'https://blah.storage.supabase.co/v1',
50-
'https://blah.storage.supabase.co/v1',
49+
'https://blah.storage.supabase.co/storage/v1',
50+
'https://blah.storage.supabase.co/storage/v1',
5151
'accept new host without modification',
5252
],
5353
[
@@ -63,16 +63,30 @@ describe('Bucket API Error Handling', () => {
6363
]
6464

6565
urlTestCases.forEach(([inputUrl, expectUrl, description]) => {
66-
it('should ' + description, () => {
66+
it('should ' + description + ' if useNewHostname is true', () => {
6767
const storage = new StorageClient(inputUrl, { apikey: KEY }, undefined, {
6868
useNewHostname: true,
6969
})
7070
expect(storage['url']).toBe(expectUrl)
7171
})
72+
it('should not modify host if useNewHostname is false', () => {
73+
const storage = new StorageClient(inputUrl, { apikey: KEY }, undefined, {
74+
useNewHostname: false,
75+
})
76+
expect(storage['url']).toBe(inputUrl)
77+
})
7278
})
7379
})
7480

7581
describe('listBuckets', () => {
82+
it('handles missing authorization errors if header is not provided', async () => {
83+
const storage = new StorageClient(URL)
84+
const { data, error } = await storage.listBuckets()
85+
expect(data).toBeNull()
86+
expect(error).not.toBeNull()
87+
expect(error?.message).toBe(`headers must have required property 'authorization'`)
88+
})
89+
7690
it('handles network errors', async () => {
7791
const mockError = new Error('Network failure')
7892
global.fetch = jest.fn().mockImplementation(() => Promise.reject(mockError))

0 commit comments

Comments
 (0)