Skip to content

Commit

Permalink
fix: import map resolution for all relative paths (#2420)
Browse files Browse the repository at this point in the history
* fix: import map resolution for all relative paths

* chore: stop rewriting import map modules
  • Loading branch information
sweatybridge authored Jun 13, 2024
1 parent f468893 commit d8141c7
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 168 deletions.
51 changes: 30 additions & 21 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import (
const (
eszipContentType = "application/vnd.denoland.eszip"
compressedEszipMagicId = "EZBR"

// 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 @@ -80,7 +76,13 @@ func GetFunctionSlugs(fsys afero.Fs) ([]string, error) {
return slugs, nil
}

func bundleFunction(ctx context.Context, slug, dockerEntrypointPath, importMapPath string, fsys afero.Fs) (*bytes.Buffer, error) {
type eszipFunction struct {
compressedBody *bytes.Buffer
entrypointPath string
importMapPath string
}

func bundleFunction(ctx context.Context, slug, hostImportMapPath string, fsys afero.Fs) (*eszipFunction, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Errorf("failed to get working directory: %w", err)
Expand All @@ -98,27 +100,35 @@ func bundleFunction(ctx context.Context, slug, dockerEntrypointPath, importMapPa
}
}()

outputPath := dockerOutputDir + "/output.eszip"
hostFuncDir := filepath.Join(cwd, utils.FunctionsDir)
dockerFuncDir := filepath.ToSlash(hostFuncDir)

outputPath := utils.DockerEszipDir + "/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",
filepath.Join(cwd, utils.FunctionsDir) + ":" + utils.DockerFuncDirPath + ":ro",
filepath.Join(cwd, hostOutputDir) + ":" + dockerOutputDir + ":rw",
hostFuncDir + ":" + dockerFuncDir + ":ro",
filepath.Join(cwd, hostOutputDir) + ":" + utils.DockerEszipDir + ":rw",
}

cmd := []string{"bundle", "--entrypoint", dockerEntrypointPath, "--output", outputPath}
result := eszipFunction{
entrypointPath: path.Join(dockerFuncDir, slug, "index.ts"),
importMapPath: path.Join(dockerFuncDir, "import_map.json"),
}
cmd := []string{"bundle", "--entrypoint", result.entrypointPath, "--output", outputPath}
if viper.GetBool("DEBUG") {
cmd = append(cmd, "--verbose")
}

if importMapPath != "" {
modules, err := utils.BindImportMap(importMapPath, dockerImportMapPath, fsys)
if hostImportMapPath != "" {
modules, dockerImportMapPath, err := utils.BindImportMap(hostImportMapPath, fsys)
if err != nil {
return nil, err
}
binds = append(binds, modules...)
cmd = append(cmd, "--import-map", dockerImportMapPath)
result.importMapPath = dockerImportMapPath
cmd = append(cmd, "--import-map", result.importMapPath)
}

err = utils.DockerRunOnceWithConfig(
Expand Down Expand Up @@ -146,16 +156,16 @@ func bundleFunction(ctx context.Context, slug, dockerEntrypointPath, importMapPa
}
defer eszipBytes.Close()

compressedBuf := bytes.NewBufferString(compressedEszipMagicId)
brw := brotli.NewWriter(compressedBuf)
result.compressedBody = bytes.NewBufferString(compressedEszipMagicId)
brw := brotli.NewWriter(result.compressedBody)
defer brw.Close()

_, err = io.Copy(brw, eszipBytes)
if err != nil {
return nil, errors.Errorf("failed to compress brotli: %w", err)
}

return compressedBuf, nil
return &result, nil
}

func deployFunction(ctx context.Context, projectRef, slug, entrypointUrl, importMapUrl string, verifyJWT bool, functionBody io.Reader) error {
Expand Down Expand Up @@ -205,24 +215,23 @@ func deployOne(ctx context.Context, slug, projectRef, importMapPath string, noVe
// 1. Bundle Function.
fmt.Println("Bundling " + utils.Bold(slug))
fc := utils.GetFunctionConfig(slug, importMapPath, noVerifyJWT, fsys)
dockerEntrypointPath := path.Join(utils.DockerFuncDirPath, slug, "index.ts")
functionBody, err := bundleFunction(ctx, slug, dockerEntrypointPath, fc.ImportMap, fsys)
eszip, err := bundleFunction(ctx, slug, fc.ImportMap, fsys)
if err != nil {
return err
}
// 2. Deploy new Function.
functionSize := units.HumanSize(float64(functionBody.Len()))
functionSize := units.HumanSize(float64(eszip.compressedBody.Len()))
fmt.Println("Deploying " + utils.Bold(slug) + " (script size: " + utils.Bold(functionSize) + ")")
policy := backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 3), ctx)
return backoff.Retry(func() error {
return deployFunction(
ctx,
projectRef,
slug,
"file://"+dockerEntrypointPath,
"file://"+dockerImportMapPath,
"file://"+eszip.entrypointPath,
"file://"+eszip.importMapPath,
*fc.VerifyJWT,
functionBody,
eszip.compressedBody,
)
}, policy)
}
Expand Down
4 changes: 1 addition & 3 deletions internal/functions/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ func downloadFunction(ctx context.Context, projectRef, slug, extractScriptPath s
return nil
}

const dockerEszipDir = "/root/eszips"

func Run(ctx context.Context, slug string, projectRef string, useLegacyBundle bool, fsys afero.Fs) error {
if useLegacyBundle {
return RunLegacy(ctx, slug, projectRef, fsys)
Expand Down Expand Up @@ -171,7 +169,7 @@ func extractOne(ctx context.Context, slug, eszipPath string) error {
if err != nil {
return errors.Errorf("failed to resolve eszip path: %w", err)
}
dockerEszipPath := path.Join(dockerEszipDir, filepath.Base(hostEszipPath))
dockerEszipPath := path.Join(utils.DockerEszipDir, filepath.Base(hostEszipPath))

binds := []string{
// Reuse deno cache directory, ie. DENO_DIR, between container restarts
Expand Down
30 changes: 13 additions & 17 deletions internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -61,12 +60,8 @@ func (i *RuntimeOption) toArgs() []string {
}

const (
// 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"
dockerRuntimeMainPath = utils.DockerDenoDir + "/main"
dockerRuntimeServerPort = 8081
dockerRuntimeInspectorPort = 8083
dockerRuntimeServerPort = 8081
dockerRuntimeInspectorPort = 8083
)

var (
Expand Down Expand Up @@ -114,14 +109,19 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
if err != nil {
return err
}
hostFuncDir, err := filepath.Abs(utils.FunctionsDir)
if err != nil {
return errors.Errorf("failed to resolve functions dir: %w", err)
}
dockerFuncDir := filepath.ToSlash(hostFuncDir)
env = append(env,
fmt.Sprintf("SUPABASE_URL=http://%s:8000", utils.KongAliases[0]),
"SUPABASE_ANON_KEY="+utils.Config.Auth.AnonKey,
"SUPABASE_SERVICE_ROLE_KEY="+utils.Config.Auth.ServiceRoleKey,
"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="+utils.DockerFuncDirPath,
"SUPABASE_INTERNAL_FUNCTIONS_PATH="+dockerFuncDir,
)
if viper.GetBool("DEBUG") {
env = append(env, "SUPABASE_INTERNAL_DEBUG=true")
Expand All @@ -134,15 +134,11 @@ func ServeFunctions(ctx context.Context, envFilePath string, noVerifyJWT *bool,
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return errors.Errorf("failed to get working directory: %w", err)
}
binds = append(binds,
// 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",
filepath.Join(cwd, utils.FunctionsDir)+":"+utils.DockerFuncDirPath+":rw",
hostFuncDir+":"+dockerFuncDir+":rw",
)
env = append(env, "SUPABASE_INTERNAL_FUNCTIONS_CONFIG="+functionsConfigString)
// 4. Parse entrypoint script
Expand Down Expand Up @@ -179,7 +175,7 @@ EOF
Env: env,
Entrypoint: entrypoint,
ExposedPorts: exposedPorts,
WorkingDir: dockerRuntimeMainPath,
WorkingDir: utils.DockerDenoDir,
// No tcp health check because edge runtime logs them as client connection error
},
start.WithSyslogConfig(container.HostConfig{
Expand Down Expand Up @@ -227,13 +223,13 @@ func populatePerFunctionConfigs(importMapPath string, noVerifyJWT *bool, fsys af
functionsConfig := make(map[string]interface{}, len(slugs))
for _, functionName := range slugs {
fc := utils.GetFunctionConfig(functionName, importMapPath, noVerifyJWT, fsys)
if hostImportMapPath := fc.ImportMap; hostImportMapPath != "" {
fc.ImportMap = path.Join(utils.DockerDenoDir, "import_maps", functionName, "import_map.json")
modules, err := utils.BindImportMap(hostImportMapPath, fc.ImportMap, fsys)
if fc.ImportMap != "" {
modules, dockerImportMapPath, err := utils.BindImportMap(fc.ImportMap, fsys)
if err != nil {
return nil, "", err
}
binds = append(binds, modules...)
fc.ImportMap = dockerImportMapPath
}
functionsConfig[functionName] = fc
}
Expand Down
Loading

0 comments on commit d8141c7

Please sign in to comment.