diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index b31f6e22f..00fdca983 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -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 { @@ -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 != "" { @@ -120,7 +121,7 @@ func bundleFunction(ctx context.Context, dockerEntrypointPath, importMapPath str }, }, }, - "edge-runtime-bundle", + "", os.Stdout, os.Stderr, ) @@ -128,24 +129,19 @@ func bundleFunction(ctx context.Context, dockerEntrypointPath, importMapPath str 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 } @@ -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 } diff --git a/internal/functions/serve/serve.go b/internal/functions/serve/serve.go index 4cb8c6f9c..8a1bb32bb 100644 --- a/internal/functions/serve/serve.go +++ b/internal/functions/serve/serve.go @@ -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" @@ -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") @@ -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) diff --git a/internal/utils/deno.go b/internal/utils/deno.go index 999b7ba78..173669096 100644 --- a/internal/utils/deno.go +++ b/internal/utils/deno.go @@ -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 @@ -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) {