• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 13365596164

17 Feb 2025 08:07AM UTC coverage: 58.167% (-0.4%) from 58.596%
13365596164

push

github

sweatybridge
fix: verify jwt by default when deploying without config

18 of 20 new or added lines in 5 files covered. (90.0%)

57 existing lines in 2 files now uncovered.

7760 of 13341 relevant lines covered (58.17%)

201.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

26.47
/pkg/function/batch.go
1
package function
2

3
import (
4
        "bytes"
5
        "context"
6
        "fmt"
7
        "net/url"
8
        "os"
9
        "path/filepath"
10
        "strings"
11

12
        "github.com/cenkalti/backoff/v4"
13
        "github.com/docker/go-units"
14
        "github.com/go-errors/errors"
15
        "github.com/supabase/cli/pkg/api"
16
        "github.com/supabase/cli/pkg/config"
17
)
18

19
const (
20
        eszipContentType = "application/vnd.denoland.eszip"
21
        maxRetries       = 3
22
)
23

24
func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig config.FunctionConfig, filter ...func(string) bool) error {
4✔
25
        var result []api.FunctionResponse
4✔
26
        if resp, err := s.client.V1ListAllFunctionsWithResponse(ctx, s.project); err != nil {
5✔
27
                return errors.Errorf("failed to list functions: %w", err)
1✔
28
        } else if resp.JSON200 == nil {
5✔
29
                return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
1✔
30
        } else {
3✔
31
                result = *resp.JSON200
2✔
32
        }
2✔
33
        exists := make(map[string]struct{}, len(result))
2✔
34
        for _, f := range result {
3✔
35
                exists[f.Slug] = struct{}{}
1✔
36
        }
1✔
37
        for slug, function := range functionConfig {
4✔
38
                if !function.Enabled {
4✔
39
                        fmt.Fprintln(os.Stderr, "Skipped deploying Function:", slug)
2✔
40
                        continue
2✔
41
                }
UNCOV
42
                for _, keep := range filter {
×
43
                        if !keep(slug) {
×
44
                                continue
×
45
                        }
46
                }
UNCOV
47
                var body bytes.Buffer
×
UNCOV
48
                if err := s.eszip.Bundle(ctx, function.Entrypoint, function.ImportMap, function.StaticFiles, &body); err != nil {
×
49
                        return err
×
50
                }
×
51
                // Update if function already exists
UNCOV
52
                upsert := func() error {
×
UNCOV
53
                        if _, ok := exists[slug]; ok {
×
UNCOV
54
                                if resp, err := s.client.V1UpdateAFunctionWithBodyWithResponse(ctx, s.project, slug, &api.V1UpdateAFunctionParams{
×
NEW
55
                                        VerifyJwt:      &function.VerifyJWT,
×
UNCOV
56
                                        ImportMapPath:  toFileURL(function.ImportMap),
×
UNCOV
57
                                        EntrypointPath: toFileURL(function.Entrypoint),
×
UNCOV
58
                                }, eszipContentType, bytes.NewReader(body.Bytes())); err != nil {
×
UNCOV
59
                                        return errors.Errorf("failed to update function: %w", err)
×
UNCOV
60
                                } else if resp.JSON200 == nil {
×
UNCOV
61
                                        return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
×
UNCOV
62
                                }
×
UNCOV
63
                        } else {
×
UNCOV
64
                                if resp, err := s.client.V1CreateAFunctionWithBodyWithResponse(ctx, s.project, &api.V1CreateAFunctionParams{
×
UNCOV
65
                                        Slug:           &slug,
×
UNCOV
66
                                        Name:           &slug,
×
NEW
67
                                        VerifyJwt:      &function.VerifyJWT,
×
UNCOV
68
                                        ImportMapPath:  toFileURL(function.ImportMap),
×
UNCOV
69
                                        EntrypointPath: toFileURL(function.Entrypoint),
×
UNCOV
70
                                }, eszipContentType, bytes.NewReader(body.Bytes())); err != nil {
×
UNCOV
71
                                        return errors.Errorf("failed to create function: %w", err)
×
UNCOV
72
                                } else if resp.JSON201 == nil {
×
UNCOV
73
                                        return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
×
UNCOV
74
                                }
×
75
                        }
UNCOV
76
                        return nil
×
77
                }
UNCOV
78
                functionSize := units.HumanSize(float64(body.Len()))
×
UNCOV
79
                fmt.Fprintf(os.Stderr, "Deploying Function: %s (script size: %s)\n", slug, functionSize)
×
UNCOV
80
                policy := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), maxRetries), ctx)
×
UNCOV
81
                if err := backoff.Retry(upsert, policy); err != nil {
×
82
                        return err
×
83
                }
×
84
        }
85
        return nil
2✔
86
}
87

UNCOV
88
func toFileURL(hostPath string) *string {
×
UNCOV
89
        absHostPath, err := filepath.Abs(hostPath)
×
UNCOV
90
        if err != nil {
×
91
                return nil
×
92
        }
×
93
        // Convert to unix path because edge runtime only supports linux
UNCOV
94
        parsed := url.URL{Scheme: "file", Path: toUnixPath(absHostPath)}
×
UNCOV
95
        result := parsed.String()
×
UNCOV
96
        return &result
×
97
}
98

UNCOV
99
func toUnixPath(absHostPath string) string {
×
UNCOV
100
        prefix := filepath.VolumeName(absHostPath)
×
UNCOV
101
        unixPath := filepath.ToSlash(absHostPath)
×
UNCOV
102
        return strings.TrimPrefix(unixPath, prefix)
×
UNCOV
103
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc