Files
Joshen Lim 4a73166675 Add support for downloading edge functions when file size exceeds max cap (#53709)
## Context

Just creating a separate PR cause I messed up the [previous
one](https://github.com/supabase/supabase/pull/53453) with a bad merge 🫠
Also changed the implementation to no longer use `@deno/eszip`.

For edge functions, functions which exceed 5MB cannot be fetched and
displayed in the dashboard editor (API will return a 507). The CTA we
give to users is to download the function via the CLI, but this PR
explores allowing users to download their edge functions via the browser
still, by calling `/v1/functions/slug/body` without the
`multi-part/data` header. This returns the function in a raw eszip
stream which we're then parsing using a custom reader

We'll do this via the "Download as ZIP" CTA here - but this way of
downloading only kicks in if the GET Edge Function body endpoint returns
a 507. Otherwise it'll just be status quo where we bundle the files that
were already fetched to render in the browser into a ZIP file.
<img width="343" height="186" alt="image"
src="https://github.com/user-attachments/assets/4ffc9fee-7e6d-46a6-bdbe-294610abc802"
/>

Also updated the warning for 507s:
- To use a normal admonition instead of `AlertError` since its
technically not an issue
- To avoid rendering the API error message in this case specifically as
the API message contains "Use multipart instead" which isn't relevant to
the dashboard from a user's POV
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/8242a875-f142-419d-81f0-0dbc30807f87"
/>

## To test

- [ ] Upload an edge function of size larger than 5MB to a staging
project via the CLI
- [ ] Verify that there's an Admonition for the large function instead
of an error
- [ ] Verify that you can still download the edge function via the
Download button (Which triggers a POST /body request without multipart)
2026-11-23 10:59:57 +07:00

391 lines
13 KiB
TypeScript

import { FileData } from '@/components/ui/FileExplorerAndEditor/FileExplorerAndEditor.types'
import { EdgeFunctionBodyData } from '@/data/edge-functions/edge-function-body-query'
// This file is bundled for both the browser (Next.js/webpack) and the TanStack Start (Vite) client,
// which handle Node.js builtins differently — Vite stubs `path` out entirely for the browser, so
// `path.posix` is undefined at runtime there even though it type-checks and builds fine under webpack.
// These are self-contained posix path helpers (no `path` import) so behavior is identical everywhere.
// Ported from the POSIX implementations in @std/path (JSR), which this file used to depend on directly.
function isPosixSeparator(code: number | undefined): boolean {
return code === 47 /* '/' */
}
function stripTrailingSeparators(segment: string): string {
if (segment.length <= 1) return segment
let end = segment.length
for (let i = segment.length - 1; i > 0; i--) {
if (isPosixSeparator(segment.charCodeAt(i))) end = i
else break
}
return segment.slice(0, end)
}
// Resolves "." and ".." segments in a posix path.
function normalizeString(path: string, allowAboveRoot: boolean): string {
let res = ''
let lastSegmentLength = 0
let lastSlash = -1
let dots = 0
let code: number | undefined
for (let i = 0; i <= path.length; ++i) {
if (i < path.length) code = path.charCodeAt(i)
else if (isPosixSeparator(code)) break
else code = 47
if (isPosixSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// noop
} else if (lastSlash !== i - 1 && dots === 2) {
if (
res.length < 2 ||
lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== 46 /* '.' */ ||
res.charCodeAt(res.length - 2) !== 46 /* '.' */
) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf('/')
if (lastSlashIndex === -1) {
res = ''
lastSegmentLength = 0
} else {
res = res.slice(0, lastSlashIndex)
lastSegmentLength = res.length - 1 - res.lastIndexOf('/')
}
lastSlash = i
dots = 0
continue
} else if (res.length === 2 || res.length === 1) {
res = ''
lastSegmentLength = 0
lastSlash = i
dots = 0
continue
}
}
if (allowAboveRoot) {
res += res.length > 0 ? '/..' : '..'
lastSegmentLength = 2
}
} else {
res += res.length > 0 ? '/' + path.slice(lastSlash + 1, i) : path.slice(lastSlash + 1, i)
lastSegmentLength = i - lastSlash - 1
}
lastSlash = i
dots = 0
} else if (code === 46 /* '.' */ && dots !== -1) {
++dots
} else {
dots = -1
}
}
return res
}
// Normalizes an absolute posix path (resolves "." / ".."). Assumes `path` starts with "/".
function resolveAbsolute(path: string): string {
const normalized = normalizeString(path, false)
return normalized.length > 0 ? '/' + normalized : '/'
}
function dirname(path: string): string {
if (path.length === 0) return '.'
let end = -1
let matchedNonSeparator = false
for (let i = path.length - 1; i >= 1; --i) {
if (isPosixSeparator(path.charCodeAt(i))) {
if (matchedNonSeparator) {
end = i
break
}
} else {
matchedNonSeparator = true
}
}
if (end === -1) return isPosixSeparator(path.charCodeAt(0)) ? '/' : '.'
return stripTrailingSeparators(path.slice(0, end))
}
// `from`/`to` must be absolute (leading "/"). Returns the relative path from `from` to `to`.
function relative(from: string, to: string): string {
from = resolveAbsolute(from)
to = resolveAbsolute(to)
if (from === to) return ''
let fromStart = 1
const fromEnd = from.length
for (; fromStart < fromEnd; ++fromStart) {
if (!isPosixSeparator(from.charCodeAt(fromStart))) break
}
const fromLen = fromEnd - fromStart
let toStart = 1
const toEnd = to.length
for (; toStart < toEnd; ++toStart) {
if (!isPosixSeparator(to.charCodeAt(toStart))) break
}
const toLen = toEnd - toStart
const length = fromLen < toLen ? fromLen : toLen
let lastCommonSep = -1
let i = 0
for (; i <= length; ++i) {
if (i === length) {
if (toLen > length) {
if (isPosixSeparator(to.charCodeAt(toStart + i))) {
return to.slice(toStart + i + 1)
} else if (i === 0) {
return to.slice(toStart + i)
}
} else if (fromLen > length) {
if (isPosixSeparator(from.charCodeAt(fromStart + i))) {
lastCommonSep = i
} else if (i === 0) {
lastCommonSep = 0
}
}
break
}
const fromCode = from.charCodeAt(fromStart + i)
const toCode = to.charCodeAt(toStart + i)
if (fromCode !== toCode) break
else if (isPosixSeparator(fromCode)) lastCommonSep = i
}
let out = ''
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
if (i === fromEnd || isPosixSeparator(from.charCodeAt(i))) {
out += out.length === 0 ? '..' : '/..'
}
}
if (out.length > 0) return out + to.slice(toStart + lastCommonSep)
let start = toStart + lastCommonSep
if (isPosixSeparator(to.charCodeAt(start))) ++start
return to.slice(start)
}
// Longest common path prefix across `paths`, comparing "/"-separated segments.
function commonPath(paths: string[]): string {
const [first = '', ...rest] = paths
const parts = first.split('/')
let endOfPrefix = parts.length
let append = ''
for (const p of rest) {
const compare = p.split('/')
if (compare.length <= endOfPrefix) {
endOfPrefix = compare.length
append = ''
}
for (let i = 0; i < endOfPrefix; i++) {
if (compare[i] !== parts[i]) {
endOfPrefix = i
append = i === 0 ? '' : '/'
break
}
}
}
return parts.slice(0, endOfPrefix).join('/') + append
}
export const getFallbackImportMapPath = (files: Omit<FileData, 'id' | 'content' | 'state'>[]) => {
// try to find a deno.json or import_map.json file
const regex = /^.*?(deno|import_map).json*$/i
return files.find(({ name }) => regex.test(name))?.name
}
export const getFallbackEntrypointPath = (files: Omit<FileData, 'id' | 'content' | 'state'>[]) => {
// when there's no matching entrypoint path is set,
// we use few heuristics to find an entrypoint file
// 1. If the function has only a single TS / JS file, if so set it as entrypoint
const jsFiles = files.filter(
({ name }) =>
name.endsWith('.js') || name.endsWith('.ts') || name.endsWith('.jsx') || name.endsWith('.tsx')
)
if (jsFiles.length === 1) {
return jsFiles[0].name
} else if (jsFiles.length) {
// 2. If function has a `index` or `main` file use it as the entrypoint
const regex = /^.*?(index|main).*$/i
const matchingFile = jsFiles.find(({ name }) => regex.test(name))
// 3. if no valid index / main file found, we set the entrypoint expliclty to first JS file
return matchingFile ? matchingFile.name : jsFiles[0].name
} else {
// no potential entrypoint files found, this will most likely result in an error on deploy
return 'index.ts'
}
}
export const getStaticPatterns = (files: Omit<FileData, 'id' | 'content' | 'state'>[]) => {
return files
.filter(({ name }) => !name.match(/\.(js|ts|jsx|tsx|json|wasm)$/i))
.map(({ name }) => name)
}
// Finds the body file the entrypoint refers to: any file whose path is a trailing path of the
// entrypoint. When several match (e.g. `index.ts` and `functions/hello/index.ts` for an entrypoint
// ending in `functions/hello/index.ts`), the one closest to the root wins.
function findEntrypointFileName(entrypoint: string, fileNames: string[]): string | undefined {
const depth = (name: string) => name.split('/').length
return fileNames
.filter((name) => entrypoint === name || entrypoint.endsWith(`/${name}`))
.sort((a, b) => depth(a) - depth(b))[0]
}
function getBasePath(entrypoint: string | undefined, fileNames: string[]): string {
if (!entrypoint) {
return '/'
}
let candidate = findEntrypointFileName(entrypoint, fileNames)
if (candidate) {
return dirname(candidate)
} else {
try {
return dirname(new URL(entrypoint).pathname)
} catch (e) {
console.error('Failed to parse entrypoint', entrypoint)
return '/'
}
}
}
export const formatFunctionBodyToFiles = ({
functionBody,
entrypointPath,
}: {
functionBody: EdgeFunctionBodyData
entrypointPath?: string
}) => {
const entrypoint_path = functionBody.metadata?.deno2_entrypoint_path ?? entrypointPath
// Set files from API response when available
if (entrypoint_path) {
const base_path = getBasePath(
entrypoint_path,
functionBody.files.map((file) => file.name)
)
const filesWithRelPath = functionBody.files
// set file paths relative to entrypoint
.map((file: { name: string; content: string }) => {
try {
// if the current file and base path doesn't share a common path,
// return unmodified file
const common_path = commonPath([base_path, file.name])
if (common_path === '' || common_path === '/tmp/') {
return file
}
// prepend "/" to turn relative paths to absolute
file.name = relative('/' + base_path, '/' + file.name)
return file
} catch (e) {
console.error(e)
// return unmodified file
return file
}
})
// sort by displayed (relative) name before assigning ids, so ids follow list order
const sortedFiles = [...filesWithRelPath].sort((a, b) =>
a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' })
)
return sortedFiles.map((file: { name: string; content: string }, index: number) => {
return {
id: index + 1,
name: file.name,
content: file.content,
state: 'unchanged',
} as FileData
})
}
return []
}
// Picks the file to open by default: the function's entrypoint, then `index.ts`, then the first file.
export const getDefaultSelectedFileId = (
files: Pick<FileData, 'id' | 'name'>[],
entrypointPath?: string | null
): number | undefined => {
const entrypointName = entrypointPath?.split('/').pop()
const entrypointFile = entrypointName
? files.find((file) => file.name === entrypointName)
: undefined
return (entrypointFile ?? files.find((file) => file.name === 'index.ts') ?? files[0])?.id
}
type FunctionZipFile = { name: string; content: string | Uint8Array<ArrayBuffer> }
// Resolves `.`, empty (e.g. from a leading `/`) and inner `..` segments, so the only `..` left are
// leading ones that reach above the base, e.g. `a/../../b` becomes `../b`.
const normalizePathSegments = (path: string): string[] =>
path.split(/[\\/]/).reduce<string[]>((segments, segment) => {
if (segment === '' || segment === '.') return segments
if (segment === '..' && segments.length > 0 && segments[segments.length - 1] !== '..') {
return segments.slice(0, -1)
}
return [...segments, segment]
}, [])
// Decides where each function file lands inside the downloaded ZIP. Paths are normalized so no
// entry can escape the extraction folder, and files that reach above the base (`../_shared`) are
// kept in place by nesting everything else under `src/`.
// - `multipart`: names include the deploy path, so they're first made relative to the function slug.
// - `eszip`: names are already relative to the function root. Running slug-relative logic on them
// would drop folders that happen to share the slug's name.
export const getFunctionZipEntries = ({
files,
slug,
source,
}: {
files: FunctionZipFile[]
slug: string
source: 'multipart' | 'eszip'
}): { path: string; content: string | Uint8Array<ArrayBuffer> }[] => {
const relativeNames = files.map(({ name }) => {
if (source === 'eszip') return normalizePathSegments(name)
const nameSections = name.split('/')
const slugIndex = nameSections.indexOf(slug)
return normalizePathSegments(nameSections.slice(slugIndex + 1).join('/'))
})
const countParentDirs = (segments: string[]) => {
const firstRealSegment = segments.findIndex((segment) => segment !== '..')
return firstRealSegment === -1 ? segments.length : firstRealSegment
}
// The deepest `../` reach decides how many `src` levels are needed to keep files in place
const maxDepth = Math.max(0, ...relativeNames.map(countParentDirs))
// The generated `src` folders must not share a name with a real top-level folder, otherwise
// e.g. `hello/index.ts` and `hello/../src/index.ts` would both end up at `src/index.ts`
const takenNames = new Set(relativeNames.map((segments) => segments[countParentDirs(segments)]))
const baseDirNames = Array.from({ length: maxDepth }, (_, i) => {
let name = i === 0 ? 'src' : `src${i}`
while (takenNames.has(name)) name += '_'
return name
})
return files.map(({ content }, index) => {
const segments = relativeNames[index]
const parentDirCount = countParentDirs(segments)
const remainingSegments = segments.slice(parentDirCount)
const depthFromBase = maxDepth - parentDirCount
const baseDirs = baseDirNames.slice(0, Math.max(depthFromBase, 0))
return { path: [...baseDirs, ...remainingSegments].join('/'), content }
})
}