Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/functions/deploy/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewDockerBundler(fsys afero.Fs) function.EszipBundler {
return &dockerBundler{fsys: fsys}
}

func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (function.FunctionDeployMetadata, error) {
func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (function.FunctionDeployMetadata, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to avoid changing the Bundle method signature? This interface is part of our public package so I'd rather not break it.

If we need to pass in additional args, we can use variadic editor args for backwards compatibility.

Copy link
Contributor Author

@nyannyacha nyannyacha Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sweatybridge

I also dislike the lengthening iface signature, but I can't think of a suitable way to pass usePackageJson.
Should I define it directly as a field in the dockerBundler struct (and any struct that implements the Bundle interface)?

meta := function.NewMetadata(slug, entrypoint, importMap, staticFiles)
fmt.Fprintln(os.Stderr, "Bundling Function:", utils.Bold(slug))
cwd, err := os.Getwd()
Expand Down Expand Up @@ -62,9 +62,18 @@ func (b *dockerBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
cmd = append(cmd, function.BundleFlags...)

env := []string{}
denoNoPackageJsonValue := "1"
if usePackageJson {
denoNoPackageJsonValue = "0"
}
if custom_registry := os.Getenv("NPM_CONFIG_REGISTRY"); custom_registry != "" {
env = append(env, "NPM_CONFIG_REGISTRY="+custom_registry)
}
if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" {
env = append(env, "DENO_NO_PACKAGE_JSON="+deno_no_package_json)
} else {
env = append(env, "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue)
}
// Run bundle
if err := utils.DockerRunOnceWithConfig(
ctx,
Expand Down
2 changes: 2 additions & 0 deletions internal/functions/deploy/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestDockerBundle(t *testing.T) {
filepath.Join("hello", "index.ts"),
filepath.Join("hello", "deno.json"),
[]string{filepath.Join("hello", "data.pdf")},
false,
&body,
)
// Check error
Expand Down Expand Up @@ -86,6 +87,7 @@ func TestDockerBundle(t *testing.T) {
"hello/index.ts",
"",
nil,
false,
nil,
)
// Check error
Expand Down
6 changes: 6 additions & 0 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
functionsUsingDeprecatedGlobalFallback = append(functionsUsingDeprecatedGlobalFallback, name)
}
}
packageJsonPath := filepath.Join(functionDir, "package.json")
packageJsonExists := false
if _, err := fsys.Stat(packageJsonPath); err == nil {
packageJsonExists = true
}
function.UsePackageJson = len(function.ImportMap) == 0 && packageJsonExists
if noVerifyJWT != nil {
function.VerifyJWT = !*noVerifyJWT
}
Expand Down
13 changes: 13 additions & 0 deletions internal/functions/serve/templates/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ async function verifyJWT(jwt: string): Promise<boolean> {
return true;
}

async function shouldUsePackageJsonDiscovery(absDir: string): Promise<boolean> {
const [a, b, c, d] = (await Promise.allSettled([
Deno.stat(posix.join(absDir, "deno.json")),
Deno.stat(posix.join(absDir, "deno.jsonc")),
Deno.stat(posix.join(absDir, "import_map.json")),
Deno.stat(posix.join(absDir, "package.json")),
])).map(v => v.status === "fulfilled");

return !a && !b && !c && d;
}

Deno.serve({
handler: async (req: Request) => {
const url = new URL(req.url);
Expand Down Expand Up @@ -178,6 +189,7 @@ Deno.serve({

const absEntrypoint = posix.join(Deno.cwd(), functionsConfig[functionName].entrypointPath);
const maybeEntrypoint = posix.toFileUrl(absEntrypoint).href;
const usePackageJsonDiscovery = await shouldUsePackageJsonDiscovery(posix.dirname(absEntrypoint));

const staticPatterns = functionsConfig[functionName].staticFiles;

Expand All @@ -187,6 +199,7 @@ Deno.serve({
memoryLimitMb,
workerTimeoutMs,
noModuleCache,
noNpm: !usePackageJsonDiscovery,
importMapPath: functionsConfig[functionName].importMapPath,
envVars,
forceCreate,
Expand Down
11 changes: 6 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ type (
FunctionConfig map[string]function

function struct {
Enabled bool `toml:"enabled" json:"-"`
VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"`
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"`
Enabled bool `toml:"enabled" json:"-"`
UsePackageJson bool `toml:"-" json:"-"`
VerifyJWT bool `toml:"verify_jwt" json:"verifyJWT"`
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
Entrypoint string `toml:"entrypoint" json:"entrypointPath,omitempty"`
StaticFiles Glob `toml:"static_files" json:"staticFiles,omitempty"`
}

analytics struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/function/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type FunctionDeployMetadata struct {
}

type EszipBundler interface {
Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error)
Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error)
}

func NewEdgeRuntimeAPI(project string, client api.ClientWithResponses, opts ...withOption) EdgeRuntimeAPI {
Expand Down
2 changes: 1 addition & 1 deletion pkg/function/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ OUTER:
}
}
var body bytes.Buffer
meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, &body)
meta, err := s.eszip.Bundle(ctx, slug, function.Entrypoint, function.ImportMap, function.StaticFiles, function.UsePackageJson, &body)
if errors.Is(err, ErrNoDeploy) {
fmt.Fprintln(os.Stderr, "Skipping undeployable Function:", slug)
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/function/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type MockBundler struct {
}

func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) {
func (b *MockBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) {
if staticFiles == nil {
staticFiles = []string{}
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/function/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
}
)

func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, output io.Writer) (FunctionDeployMetadata, error) {
func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap string, staticFiles []string, usePackageJson bool, output io.Writer) (FunctionDeployMetadata, error) {
meta := NewMetadata(slug, entrypoint, importMap, staticFiles)
outputPath := filepath.Join(b.tempDir, slug+".eszip")
// TODO: make edge runtime write to stdout
Expand All @@ -65,9 +65,17 @@ func (b *nativeBundler) Bundle(ctx context.Context, slug, entrypoint, importMap
defer cancel() // release resources if command exits before timeout
ctx = timeoutCtx
}
denoNoPackageJsonValue := "1"
if usePackageJson {
denoNoPackageJsonValue = "0"
}

cmd := exec.CommandContext(ctx, edgeRuntimeBin, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if deno_no_package_json := os.Getenv("DENO_NO_PACKAGE_JSON"); deno_no_package_json != "" {
cmd.Env = append(os.Environ(), "DENO_NO_PACKAGE_JSON="+denoNoPackageJsonValue)
}
if err := cmd.Run(); err != nil {
return meta, errors.Errorf("failed to bundle function: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/function/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestBundleFunction(t *testing.T) {
"hello/index.ts",
"hello/deno.json",
[]string{"hello/data.pdf"},
true,
&body,
)
// Check error
Expand Down Expand Up @@ -78,6 +79,7 @@ func TestBundleFunction(t *testing.T) {
"hello/index.ts",
"",
nil,
true,
&body,
)
// Check error
Expand Down