Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(edge-functions): add warning for edge-functions configuration #2984

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
// Although some functions do not require import map, it's more convenient to setup
// vscode deno extension with a single import map for all functions.
fallbackExists := true
functionsUsingDeprecatedGlobalFallback := []string{}
functionsUsingDeprecatedImportMap := []string{}
if _, err := fsys.Stat(utils.FallbackImportMapPath); errors.Is(err, os.ErrNotExist) {
fallbackExists = false
} else if err != nil {
Expand All @@ -94,18 +96,30 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
} else if len(function.ImportMap) == 0 {
denoJsonPath := filepath.Join(functionDir, "deno.json")
denoJsoncPath := filepath.Join(functionDir, "deno.jsonc")
importMapPath := filepath.Join(functionDir, "import_map.json")
if _, err := fsys.Stat(denoJsonPath); err == nil {
function.ImportMap = denoJsonPath
} else if _, err := fsys.Stat(denoJsoncPath); err == nil {
function.ImportMap = denoJsoncPath
} else if _, err := fsys.Stat(importMapPath); err == nil {
function.ImportMap = importMapPath
functionsUsingDeprecatedImportMap = append(functionsUsingDeprecatedImportMap, name)
sweatybridge marked this conversation as resolved.
Show resolved Hide resolved
} else if fallbackExists {
function.ImportMap = utils.FallbackImportMapPath
functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name)
}
}
if noVerifyJWT != nil {
function.VerifyJWT = cast.Ptr(!*noVerifyJWT)
}
functionConfig[name] = function
}
if len(functionsUsingDeprecatedImportMap) > 0 {
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "Functions using deprecated import_map.json (please migrate to deno.jsonc):", utils.Aqua(strings.Join(functionsUsingDeprecatedImportMap, ", ")))
}
if len(functionsUsingDeprecatedGlobalFallback) > 0 {
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "Functions using fallback import map:", utils.Aqua(strings.Join(functionsUsingDeprecatedGlobalFallback, ", ")))
fmt.Fprintln(os.Stderr, "Please use recommended per function dependency declaration ", utils.Aqua("https://supabase.com/docs/guides/functions/import-maps"))
}
return functionConfig, nil
}
45 changes: 33 additions & 12 deletions internal/functions/new/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import (

var (
//go:embed templates/index.ts
indexEmbed string
indexTemplate = template.Must(template.New("indexl").Parse(indexEmbed))
indexEmbed string
//go:embed templates/deno.jsonc
denoEmbed string
//go:embed templates/.npmrc
npmrcEmbed string

indexTemplate = template.Must(template.New("index").Parse(indexEmbed))
)

type indexConfig struct {
Expand All @@ -38,25 +43,41 @@ func Run(ctx context.Context, slug string, fsys afero.Fs) error {
if err := utils.MkdirIfNotExistFS(fsys, funcDir); err != nil {
return err
}
path := filepath.Join(funcDir, "index.ts")
f, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return errors.Errorf("failed to create function entrypoint: %w", err)
}
defer f.Close()
// Templatize index.ts by config.toml if available

// Load config if available
if err := utils.LoadConfigFS(fsys); err != nil {
utils.CmdSuggestion = ""
}
config := indexConfig{

if err := createTemplateFile(fsys, filepath.Join(funcDir, "index.ts"), indexTemplate, indexConfig{
URL: utils.GetApiUrl("/functions/v1/" + slug),
Token: utils.Config.Auth.AnonKey,
}); err != nil {
return errors.Errorf("failed to create function entrypoint: %w", err)
}

if err := afero.WriteFile(fsys, filepath.Join(funcDir, "deno.jsonc"), []byte(denoEmbed), 0644); err != nil {
return errors.Errorf("failed to create deno.jsonc config: %w", err)
}
if err := indexTemplate.Option("missingkey=error").Execute(f, config); err != nil {
return errors.Errorf("failed to initialise function entrypoint: %w", err)

if err := afero.WriteFile(fsys, filepath.Join(funcDir, ".npmrc"), []byte(npmrcEmbed), 0644); err != nil {
return errors.Errorf("failed to create .npmrc config: %w", err)
}
}

fmt.Println("Created new Function at " + utils.Bold(funcDir))
return nil
}

func createTemplateFile(fsys afero.Fs, path string, tmpl *template.Template, data interface{}) error {
avallete marked this conversation as resolved.
Show resolved Hide resolved
f, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
defer f.Close()

if data != nil {
return tmpl.Option("missingkey=error").Execute(f, data)
}
return tmpl.Execute(f, nil)
}
10 changes: 10 additions & 0 deletions internal/functions/new/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ func TestNewCommand(t *testing.T) {
assert.Contains(t, string(content),
"curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/test-func'",
)

// Verify deno.jsonc exists
denoPath := filepath.Join(utils.FunctionsDir, "test-func", "deno.jsonc")
_, err = afero.ReadFile(fsys, denoPath)
assert.NoError(t, err, "deno.jsonc should be created")

// Verify .npmrc exists
npmrcPath := filepath.Join(utils.FunctionsDir, "test-func", ".npmrc")
_, err = afero.ReadFile(fsys, npmrcPath)
assert.NoError(t, err, ".npmrc should be created")
})

t.Run("throws error on malformed slug", func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions internal/functions/new/templates/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Configuration for private npm package dependencies
# For more information on using private registries with Edge Functions, see:
# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries
avallete marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions internal/functions/new/templates/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
// Add your dependencies here
// See: https://supabase.com/docs/guides/functions/import-maps#using-denojson-recommended
}
}
Loading