mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
import { queryOptions } from '@tanstack/react-query'
|
|
|
|
import { listBucketObjects } from './bucket-objects-list-mutation'
|
|
import { storageKeys } from './keys'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
|
|
/** Number of objects requested per folder listing */
|
|
const PAGE_LIMIT = 1000
|
|
/** Cap on how many listings the crawl issues */
|
|
const MAX_LISTINGS = 200
|
|
/** Cap on how many folders are collected */
|
|
const MAX_FOLDERS = 1000
|
|
/** How many listings are issued at a time while crawling */
|
|
const CONCURRENCY = 5
|
|
|
|
export type StorageFolder = {
|
|
name: string
|
|
path: string
|
|
}
|
|
|
|
export type BucketFoldersVariables = {
|
|
projectRef?: string
|
|
bucketId?: string
|
|
}
|
|
|
|
export type BucketFoldersData = {
|
|
folders: StorageFolder[]
|
|
/** True when the crawl hit its limits, so `folders` is only part of the bucket */
|
|
isTruncated: boolean
|
|
}
|
|
|
|
export type BucketFoldersError = StudioError
|
|
|
|
/** Pages through a single folder, counting each page fetched against the shared `listingsState` */
|
|
const listFolderNames = async (
|
|
{ projectRef, bucketId, path }: { projectRef: string; bucketId: string; path: string },
|
|
listingsState: { count: number },
|
|
signal?: AbortSignal
|
|
): Promise<{ names: string[]; isTruncated: boolean }> => {
|
|
const names: string[] = []
|
|
let offset = 0
|
|
|
|
while (true) {
|
|
if (listingsState.count >= MAX_LISTINGS) {
|
|
return { names, isTruncated: true }
|
|
}
|
|
|
|
listingsState.count++
|
|
const objects = await listBucketObjects(
|
|
{
|
|
projectRef,
|
|
bucketId,
|
|
path,
|
|
options: { limit: PAGE_LIMIT, offset, sortBy: { column: 'name', order: 'asc' } },
|
|
},
|
|
signal
|
|
)
|
|
|
|
const page = objects ?? []
|
|
// Objects without an id are prefixes (folders) rather than files
|
|
names.push(...page.filter((object) => !object.id).map((object) => object.name))
|
|
|
|
if (page.length < PAGE_LIMIT) return { names, isTruncated: false }
|
|
offset += PAGE_LIMIT
|
|
}
|
|
}
|
|
|
|
/** Crawls a bucket breadth first for every folder, stopping early once `MAX_LISTINGS`/`MAX_FOLDERS` is hit */
|
|
async function getBucketFolders(
|
|
{ projectRef, bucketId }: BucketFoldersVariables,
|
|
signal?: AbortSignal
|
|
): Promise<BucketFoldersData> {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
if (!bucketId) throw new Error('bucketId is required')
|
|
|
|
const folders: StorageFolder[] = []
|
|
let queue = ['']
|
|
const listingsState = { count: 0 }
|
|
let isTruncated = false
|
|
|
|
while (queue.length > 0) {
|
|
if (listingsState.count >= MAX_LISTINGS || folders.length >= MAX_FOLDERS) {
|
|
isTruncated = true
|
|
break
|
|
}
|
|
|
|
const batch = queue.slice(0, CONCURRENCY)
|
|
queue = queue.slice(CONCURRENCY)
|
|
|
|
const results = await Promise.all(
|
|
batch.map(async (path) => {
|
|
const { names, isTruncated: isPathTruncated } = await listFolderNames(
|
|
{ projectRef, bucketId, path },
|
|
listingsState,
|
|
signal
|
|
)
|
|
return { path, names, isTruncated: isPathTruncated }
|
|
})
|
|
)
|
|
|
|
for (const { path, names, isTruncated: isPathTruncated } of results) {
|
|
if (isPathTruncated) isTruncated = true
|
|
for (const name of names) {
|
|
const folderPath = path.length > 0 ? `${path}/${name}` : name
|
|
folders.push({ name, path: folderPath })
|
|
queue.push(folderPath)
|
|
}
|
|
}
|
|
}
|
|
|
|
return { folders: folders.slice(0, MAX_FOLDERS), isTruncated: isTruncated || queue.length > 0 }
|
|
}
|
|
|
|
export const bucketFoldersQueryOptions = ({ projectRef, bucketId }: BucketFoldersVariables) =>
|
|
queryOptions({
|
|
queryKey: storageKeys.folders(projectRef, bucketId),
|
|
queryFn: ({ signal }) => getBucketFolders({ projectRef, bucketId }, signal),
|
|
enabled: typeof projectRef !== 'undefined' && typeof bucketId !== 'undefined',
|
|
// The crawl is expensive, so hold onto the result for the lifetime of a picker session
|
|
staleTime: 60 * 1000,
|
|
})
|