Skip to content

Commit

Permalink
fix: addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
laktek committed Dec 4, 2023
1 parent 57ebe57 commit 7c65bbe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
30 changes: 13 additions & 17 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
)

const (
eszipContentType = "application/vnd.denoland.eszip"
eszipContentType = "application/vnd.denoland.eszip"
compressedEszipMagicId = "EZBR"

dockerFuncDirPath = utils.DockerDenoDir + "/functions"
// Import Map from CLI flag, i.e. --import-map, takes priority over config.toml & fallback.
dockerImportMapPath = utils.DockerDenoDir + "/import_map.json"
dockerOutputDir = "/root/eszips"
)

func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
Expand Down Expand Up @@ -74,19 +75,19 @@ func bundleFunction(ctx context.Context, dockerEntrypointPath, importMapPath str
}

// create temp directory to store generated eszip
tmpDir, err := afero.TempDir(fsys, "eszip")
tmpDir, err := os.MkdirTemp("", "eszip")
if err != nil {
return nil, err
}
defer fsys.RemoveAll(tmpDir)
outputPath := "/root/eszips/output.eszip"
defer os.RemoveAll(tmpDir)
outputPath := dockerOutputDir + "/output.eszip"

binds := []string{
// Reuse deno cache directory, ie. DENO_DIR, between container restarts
// https://denolib.gitbook.io/guide/advanced/deno_dir-code-fetch-and-cache
utils.EdgeRuntimeId + ":/root/.cache/deno:rw,z",
filepath.Join(cwd, utils.FunctionsDir) + ":" + dockerFuncDirPath + ":ro,z",
tmpDir + ":/root/eszips:rw,z",
filepath.Join(cwd, utils.FunctionsDir) + ":" + utils.DockerFuncDirPath + ":ro,z",
tmpDir + ":" + dockerOutputDir + ":rw,z",
}

if importMapPath != "" {
Expand Down Expand Up @@ -120,32 +121,27 @@ func bundleFunction(ctx context.Context, dockerEntrypointPath, importMapPath str
},
},
},
"edge-runtime-bundle",
"",
os.Stdout,
os.Stderr,
)
if err != nil {
return nil, err
}

eszipBytes, err := os.ReadFile(filepath.Join(tmpDir, "output.eszip"))
eszipBytes, err := afero.ReadFile(fsys, filepath.Join(tmpDir, "output.eszip"))
if err != nil {
return nil, err
}
eszipBuf := bytes.NewBuffer(eszipBytes)

compressedBuf := &bytes.Buffer{}
_, err = compressedBuf.WriteString("EZBR")
if err != nil {
return nil, err
}

compressedBuf := bytes.NewBufferString(compressedEszipMagicId)
brw := brotli.NewWriter(compressedBuf)
_, err = eszipBuf.WriteTo(brw)
if err != nil {
return nil, err
}
brw.Close()
defer brw.Close()

return compressedBuf, nil
}
Expand Down Expand Up @@ -215,7 +211,7 @@ func deployOne(ctx context.Context, slug, projectRef, importMapPath string, noVe
}
importMapPath = resolved
// 2. Bundle Function.
dockerEntrypointPath, err := filepath.Abs(filepath.Join(dockerFuncDirPath, slug, "index.ts"))
dockerEntrypointPath, err := filepath.Abs(filepath.Join(utils.DockerFuncDirPath, slug, "index.ts"))
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
)

const (
dockerFuncDirPath = utils.DockerDenoDir + "/functions"
// Import Map from CLI flag, i.e. --import-map, takes priority over config.toml & fallback.
dockerFlagImportMapPath = utils.DockerDenoDir + "/flag_import_map.json"
dockerFallbackImportMapPath = utils.DockerDenoDir + "/fallback_import_map.json"
Expand Down Expand Up @@ -92,7 +91,7 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
"SUPABASE_DB_URL=" + dbUrl,
"SUPABASE_INTERNAL_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
fmt.Sprintf("SUPABASE_INTERNAL_HOST_PORT=%d", utils.Config.Api.Port),
"SUPABASE_INTERNAL_FUNCTIONS_PATH=" + dockerFuncDirPath,
"SUPABASE_INTERNAL_FUNCTIONS_PATH=" + utils.DockerFuncDirPath,
}
if viper.GetBool("DEBUG") {
env = append(env, "SUPABASE_INTERNAL_DEBUG=true")
Expand All @@ -102,7 +101,7 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
// Reuse deno cache directory, ie. DENO_DIR, between container restarts
// https://denolib.gitbook.io/guide/advanced/deno_dir-code-fetch-and-cache
utils.EdgeRuntimeId + ":/root/.cache/deno:rw,z",
filepath.Join(cwd, utils.FunctionsDir) + ":" + dockerFuncDirPath + ":rw,z",
filepath.Join(cwd, utils.FunctionsDir) + ":" + utils.DockerFuncDirPath + ":rw,z",
}
if importMapPath != "" {
modules, err := utils.BindImportMap(importMapPath, dockerFlagImportMapPath, fsys)
Expand Down
11 changes: 6 additions & 5 deletions internal/utils/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var (
DenoPathOverride string
)

const (
DockerDenoDir = "/home/deno"
DockerModsDir = DockerDenoDir + "/modules"
DockerFuncDirPath = DockerDenoDir + "/functions"
)

func GetDenoPath() (string, error) {
if len(DenoPathOverride) > 0 {
return DenoPathOverride, nil
Expand Down Expand Up @@ -267,11 +273,6 @@ func (m *ImportMap) BindModules(resolved ImportMap) []string {
return binds
}

const (
DockerDenoDir = "/home/deno"
DockerModsDir = DockerDenoDir + "/modules"
)

func resolveHostPath(hostPath string, fsys afero.Fs) string {
// All local fs imports will be mounted to /home/deno/modules
if filepath.IsAbs(hostPath) {
Expand Down

0 comments on commit 7c65bbe

Please sign in to comment.