Files
supabase/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.test.ts
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

292 lines
10 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
formatFunctionBodyToFiles,
getDefaultSelectedFileId,
getFunctionZipEntries,
} from './EdgeFunctions.utils'
describe('formatFunctionBodyToFiles', () => {
it('returns no files when there is no entrypoint path', () => {
const files = [{ name: 'index.ts', content: 'a' }]
const result = formatFunctionBodyToFiles({ functionBody: { files, metadata: {} } })
expect(result).toEqual([])
})
it('rewrites nested file paths relative to a nested entrypoint', () => {
const files = [
{ name: 'functions/hello/index.ts', content: 'a' },
{ name: 'functions/hello/utils/helper.ts', content: 'b' },
]
const result = formatFunctionBodyToFiles({
functionBody: { files, metadata: { deno2_entrypoint_path: 'functions/hello/index.ts' } },
})
expect(result.map((f) => f.name)).toEqual(['index.ts', 'utils/helper.ts'])
})
it('leaves file paths unmodified when the entrypoint is a bare filename at the root', () => {
const files = [{ name: 'index.ts', content: 'a' }]
const result = formatFunctionBodyToFiles({
functionBody: { files, metadata: { deno2_entrypoint_path: 'index.ts' } },
})
expect(result.map((f) => f.name)).toEqual(['index.ts'])
})
it('falls back to parsing a URL entrypoint when no file name matches', () => {
const files = [
{ name: 'functions/hello/index.ts', content: 'a' },
{ name: 'functions/hello/utils/helper.ts', content: 'b' },
]
const result = formatFunctionBodyToFiles({
functionBody: { files, metadata: {} },
entrypointPath: 'https://edge.supabase.com/deploy/abc123/main.ts',
})
// the URL's parsed base path ('/deploy/abc123') shares no common prefix with
// the relative file names, so they're left unmodified (per commonPath)
expect(result.map((f) => f.name)).toEqual([
'functions/hello/index.ts',
'functions/hello/utils/helper.ts',
])
})
it('leaves a file unmodified when it shares no common path with the base path', () => {
const files = [
{ name: 'functions/hello/index.ts', content: 'a' },
{ name: 'unrelated/other.ts', content: 'b' },
]
const result = formatFunctionBodyToFiles({
functionBody: { files, metadata: { deno2_entrypoint_path: 'functions/hello/index.ts' } },
})
expect(result.map((f) => f.name)).toEqual(['index.ts', 'unrelated/other.ts'])
})
it('assigns sequential ids and preserves content', () => {
const files = [
{ name: 'a.ts', content: 'foo' },
{ name: 'b.ts', content: 'bar' },
]
const result = formatFunctionBodyToFiles({
functionBody: { files, metadata: { deno2_entrypoint_path: 'a.ts' } },
})
expect(result).toEqual([
{ id: 1, name: 'a.ts', content: 'foo', state: 'unchanged' },
{ id: 2, name: 'b.ts', content: 'bar', state: 'unchanged' },
])
})
})
describe('formatFunctionBodyToFiles sorting', () => {
const format = (names: string[], entrypoint = 'index.ts') =>
formatFunctionBodyToFiles({
functionBody: {
files: names.map((name) => ({ name, content: '' })),
metadata: { deno2_entrypoint_path: entrypoint },
},
})
it('sorts files by name', () => {
const result = format(['utils.ts', 'index.ts', 'deno.json'])
expect(result.map((f) => f.name)).toEqual(['deno.json', 'index.ts', 'utils.ts'])
})
it('sorts numerically so file2 comes before file10', () => {
const result = format(['file10.ts', 'file2.ts', 'index.ts'])
expect(result.map((f) => f.name)).toEqual(['file2.ts', 'file10.ts', 'index.ts'])
})
it('sorts case-insensitively', () => {
const result = format(['b.ts', 'A.ts', 'index.ts'])
expect(result.map((f) => f.name)).toEqual(['A.ts', 'b.ts', 'index.ts'])
})
it('sorts by relative name after rewriting nested paths', () => {
const result = formatFunctionBodyToFiles({
functionBody: {
files: [
{ name: 'functions/hello/utils/helper.ts', content: '' },
{ name: 'functions/hello/index.ts', content: '' },
],
metadata: { deno2_entrypoint_path: 'functions/hello/index.ts' },
},
})
expect(result.map((f) => f.name)).toEqual(['index.ts', 'utils/helper.ts'])
})
it('assigns unique sequential ids following sorted order', () => {
const result = format(['c.ts', 'a.ts', 'index.ts', 'b.ts'])
expect(result.map((f) => [f.id, f.name])).toEqual([
[1, 'a.ts'],
[2, 'b.ts'],
[3, 'c.ts'],
[4, 'index.ts'],
])
})
})
describe('getDefaultSelectedFileId', () => {
const files = [
{ id: 1, name: 'a.ts' },
{ id: 2, name: 'handler.ts' },
{ id: 3, name: 'index.ts' },
]
it('selects the entrypoint file', () => {
expect(getDefaultSelectedFileId(files, 'handler.ts')).toBe(2)
})
it('matches on the basename of a nested entrypoint path', () => {
expect(getDefaultSelectedFileId(files, 'functions/hello/handler.ts')).toBe(2)
})
it('matches on the basename of a URL entrypoint', () => {
expect(
getDefaultSelectedFileId(files, 'https://edge.supabase.com/deploy/abc123/handler.ts')
).toBe(2)
})
it('falls back to index.ts when the entrypoint matches no file', () => {
expect(getDefaultSelectedFileId(files, 'missing.ts')).toBe(3)
})
it('falls back to index.ts when there is no entrypoint', () => {
expect(getDefaultSelectedFileId(files)).toBe(3)
expect(getDefaultSelectedFileId(files, null)).toBe(3)
})
it('falls back to the first file when there is no index.ts', () => {
expect(getDefaultSelectedFileId(files.slice(0, 2), 'missing.ts')).toBe(1)
})
it('returns undefined when there are no files', () => {
expect(getDefaultSelectedFileId([], 'index.ts')).toBeUndefined()
})
it('selects the entrypoint closest to the root when several files match its path', () => {
const entrypointPath = 'functions/hello/index.ts'
for (const names of [
['index.ts', 'functions/hello/index.ts'],
['functions/hello/index.ts', 'index.ts'],
]) {
const files = formatFunctionBodyToFiles({
functionBody: {
files: names.map((name) => ({ name, content: name })),
metadata: { deno2_entrypoint_path: entrypointPath },
},
})
const id = getDefaultSelectedFileId(files, entrypointPath)
expect(files.map((f) => f.name)).toEqual(['functions/hello/index.ts', 'index.ts'])
expect(files.find((f) => f.id === id)?.name).toBe('index.ts')
}
})
it('does not match a file name that only shares trailing characters', () => {
const entrypointPath = 'functions/hello/index.ts'
const files = formatFunctionBodyToFiles({
functionBody: {
files: ['dex.ts', 'index.ts'].map((name) => ({ name, content: name })),
metadata: { deno2_entrypoint_path: entrypointPath },
},
})
const id = getDefaultSelectedFileId(files, entrypointPath)
expect(files.find((f) => f.id === id)?.name).toBe('index.ts')
})
})
describe('getFunctionZipEntries', () => {
const paths = (entries: { path: string }[]) => entries.map((e) => e.path)
it('makes multipart file paths relative to the function slug', () => {
const files = [
{ name: 'source/hello/index.ts', content: 'a' },
{ name: 'source/hello/utils/helper.ts', content: 'b' },
]
const entries = getFunctionZipEntries({ files, slug: 'hello', source: 'multipart' })
expect(paths(entries)).toEqual(['index.ts', 'utils/helper.ts'])
})
it('nests multipart files that reach above the base under src/', () => {
const files = [
{ name: 'source/hello/index.ts', content: 'a' },
{ name: 'source/hello/../shared/db.ts', content: 'b' },
]
const entries = getFunctionZipEntries({ files, slug: 'hello', source: 'multipart' })
expect(paths(entries)).toEqual(['src/index.ts', 'shared/db.ts'])
})
it('never maps two multipart files to the same ZIP path when a real `src` folder exists', () => {
const files = [
{ name: 'source/hello/index.ts', content: 'a' },
{ name: 'source/hello/../src/index.ts', content: 'b' },
]
const entries = getFunctionZipEntries({ files, slug: 'hello', source: 'multipart' })
expect(new Set(paths(entries)).size).toBe(2)
expect(paths(entries)).toEqual(['src_/index.ts', 'src/index.ts'])
})
it('normalizes multipart paths with `..` in the middle so none escape the ZIP root', () => {
const files = [
{ name: 'source/hello/index.ts', content: 'a' },
{ name: 'source/hello/utils/../../../etc/passwd', content: 'b' },
]
const entries = getFunctionZipEntries({ files, slug: 'hello', source: 'multipart' })
expect(paths(entries)).toEqual(['src/src1/index.ts', 'etc/passwd'])
})
it('keeps eszip file paths as-is, even when a folder shares the slug name', () => {
const files = [
{ name: 'index.ts', content: 'a' },
{ name: 'auth/handler.ts', content: 'b' },
{ name: 'handler.ts', content: 'c' },
]
const entries = getFunctionZipEntries({ files, slug: 'auth', source: 'eszip' })
expect(paths(entries)).toEqual(['index.ts', 'auth/handler.ts', 'handler.ts'])
})
it('nests eszip files under src/ when shared files reach above the function root', () => {
const files = [
{ name: 'index.ts', content: 'a' },
{ name: '../_shared/cors.ts', content: 'b' },
]
const eszipEntries = getFunctionZipEntries({ files, slug: 'hello', source: 'eszip' })
const multipartEntries = getFunctionZipEntries({
files: [
{ name: 'source/hello/index.ts', content: 'a' },
{ name: 'source/hello/../_shared/cors.ts', content: 'b' },
],
slug: 'hello',
source: 'multipart',
})
expect(paths(eszipEntries)).toEqual(['src/index.ts', '_shared/cors.ts'])
expect(eszipEntries).toEqual(multipartEntries)
})
it('never produces eszip paths that escape the ZIP root', () => {
const files = [
{ name: 'index.ts', content: 'a' },
{ name: '../../evil.ts', content: 'b' },
{ name: '/etc/passwd', content: 'c' },
{ name: 'utils/./../../x.ts', content: 'd' },
{ name: 'a//b\\..\\..\\..\\win.ts', content: 'e' },
]
const entries = getFunctionZipEntries({ files, slug: 'hello', source: 'eszip' })
expect(paths(entries)).toEqual([
'src/src1/index.ts',
'evil.ts',
'src/src1/etc/passwd',
'src/x.ts',
'src/win.ts',
])
for (const path of paths(entries)) {
expect(path.startsWith('/')).toBe(false)
expect(path.split('/')).not.toContain('..')
expect(path.split('/')).not.toContain('')
}
})
})