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

supabase / cli / 13911133452

17 Mar 2025 10:30PM UTC coverage: 51.06%. First build
13911133452

Pull #3314

github

web-flow
Merge da4dd25ba into bc0db16f3
Pull Request #3314: fix: return deploy metadata from bundle

37 of 100 new or added lines in 3 files covered. (37.0%)

6959 of 13629 relevant lines covered (51.06%)

186.36 hits per line

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

76.47
/pkg/function/bundle.go
1
package function
2

3
import (
4
        "context"
5
        "fmt"
6
        "io"
7
        "io/fs"
8
        "net/url"
9
        "os"
10
        "os/exec"
11
        "path/filepath"
12
        "strings"
13

14
        "github.com/andybalholm/brotli"
15
        "github.com/go-errors/errors"
16
        "github.com/supabase/cli/pkg/api"
17
        "github.com/supabase/cli/pkg/cast"
18
)
19

20
type nativeBundler struct {
21
        tempDir string
22
        fsys    fs.FS
23
}
24

25
func NewNativeBundler(tempDir string, fsys fs.FS) EszipBundler {
×
26
        return &nativeBundler{
×
27
                tempDir: tempDir,
×
28
                fsys:    fsys,
×
29
        }
×
30
}
×
31

32
// Use a package private variable to allow testing without gosec complaining about G204
33
var edgeRuntimeBin = "edge-runtime"
34

35
func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (api.FunctionDeployMetadata, error) {
2✔
36
        meta := NewMetadata(slug, entrypoint, importMap, staticFiles)
2✔
37
        outputPath := filepath.Join(b.tempDir, slug+".eszip")
2✔
38
        // TODO: make edge runtime write to stdout
2✔
39
        args := []string{"bundle", "--entrypoint", entrypoint, "--output", outputPath}
2✔
40
        if len(importMap) > 0 {
3✔
41
                args = append(args, "--import-map", importMap)
1✔
42
        }
1✔
43
        for _, staticFile := range staticFiles {
3✔
44
                args = append(args, "--static", staticFile)
1✔
45
        }
1✔
46
        cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...)
2✔
47
        cmd.Stderr = os.Stderr
2✔
48
        cmd.Stdout = os.Stdout
2✔
49
        if err := cmd.Run(); err != nil {
2✔
NEW
50
                return meta, errors.Errorf("failed to bundle function: %w", err)
×
51
        }
×
52
        defer os.Remove(outputPath)
2✔
53
        // Compress the output
2✔
54
        eszipBytes, err := b.fsys.Open(outputPath)
2✔
55
        if err != nil {
2✔
NEW
56
                return meta, errors.Errorf("failed to open eszip: %w", err)
×
57
        }
×
58
        defer eszipBytes.Close()
2✔
59
        return meta, Compress(eszipBytes, output)
2✔
60
}
61

62
const compressedEszipMagicID = "EZBR"
63

64
func Compress(r io.Reader, w io.Writer) error {
2✔
65
        if _, err := fmt.Fprint(w, compressedEszipMagicID); err != nil {
2✔
66
                return errors.Errorf("failed to append magic id: %w", err)
×
67
        }
×
68
        brw := brotli.NewWriter(w)
2✔
69
        defer brw.Close()
2✔
70
        if _, err := io.Copy(brw, r); err != nil {
2✔
71
                return errors.Errorf("failed to compress eszip: %w", err)
×
72
        }
×
73
        return nil
2✔
74
}
75

76
func NewMetadata(slug, entrypoint, importMap string, staticFiles []string) api.FunctionDeployMetadata {
2✔
77
        meta := api.FunctionDeployMetadata{
2✔
78
                Name:           &slug,
2✔
79
                EntrypointPath: toFileURL(entrypoint),
2✔
80
        }
2✔
81
        if len(importMap) > 0 {
3✔
82
                meta.ImportMapPath = cast.Ptr(toFileURL(importMap))
1✔
83
        }
1✔
84
        files := make([]string, len(staticFiles))
2✔
85
        for i, sf := range staticFiles {
3✔
86
                files[i] = toFileURL(sf)
1✔
87
        }
1✔
88
        meta.StaticPatterns = &files
2✔
89
        return meta
2✔
90
}
91

92
func toFileURL(hostPath string) string {
4✔
93
        absHostPath, err := filepath.Abs(hostPath)
4✔
94
        if err != nil {
4✔
NEW
95
                return hostPath
×
NEW
96
        }
×
97
        // Convert to unix path because edge runtime only supports linux
98
        unixPath := toUnixPath(absHostPath)
4✔
99
        parsed := url.URL{Scheme: "file", Path: unixPath}
4✔
100
        return parsed.String()
4✔
101
}
102

103
func toUnixPath(absHostPath string) string {
4✔
104
        prefix := filepath.VolumeName(absHostPath)
4✔
105
        unixPath := filepath.ToSlash(absHostPath)
4✔
106
        return strings.TrimPrefix(unixPath, prefix)
4✔
107
}
4✔
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