diff --git a/examples/test/src/index.ts b/examples/test/src/index.ts index 88ea5ffa8..c9d15948c 100644 --- a/examples/test/src/index.ts +++ b/examples/test/src/index.ts @@ -1,2 +1 @@ -import "./asfasdasd"; export function handler() {} diff --git a/examples/test/sst.config.ts b/examples/test/sst.config.ts index 809be7d7f..2de3db81d 100644 --- a/examples/test/sst.config.ts +++ b/examples/test/sst.config.ts @@ -7,14 +7,16 @@ export default $config({ removalPolicy: "retain-all", providers: { aws: {}, - cloudflare: {}, }, }; }, async run() { - new sst.aws.Function("MyFunction", { - handler: "src/lambda.handler", + const fn = new sst.aws.Function("MyFunction", { + handler: "src/index.handler", + url: true, }); - return {}; + return { + url: fn.url, + }; }, }); diff --git a/pkg/platform/bridge/bridge.go b/pkg/platform/bridge/bridge.go index 79b4c6623..9aa3bf17a 100644 --- a/pkg/platform/bridge/bridge.go +++ b/pkg/platform/bridge/bridge.go @@ -229,6 +229,13 @@ func run() error { return token.Error() } + if token := mqttClient.Subscribe(prefix+"/kill", 1, func(c MQTT.Client, m MQTT.Message) { + slog.Info("received kill message") + cancel() + }); token.Wait() && token.Error() != nil { + return token.Error() + } + if token := mqttClient.Publish(prefix+"/init", 1, false, initPayload); token.Wait() && token.Error() != nil { return token.Error() } diff --git a/pkg/platform/src/config.ts b/pkg/platform/src/config.ts index f0c140c04..ecdf0d8ee 100644 --- a/pkg/platform/src/config.ts +++ b/pkg/platform/src/config.ts @@ -1,20 +1,12 @@ -import type { ProviderArgs as AWS } from "@pulumi/aws"; -import type { ProviderArgs as Cloudflare } from "@pulumi/cloudflare"; - export interface App { name: string; removalPolicy?: "remove" | "retain" | "retain-all"; - providers?: { - aws?: AWS; - cloudflare?: Cloudflare & { - accountId?: string; - }; - }; } export interface Config { app(input: { stage?: string }): App; run(): any; + providers?: Record; } export function $config(input: Config): Config { diff --git a/pkg/project/install.go b/pkg/project/install.go index c2d1079c7..9d65f12f6 100644 --- a/pkg/project/install.go +++ b/pkg/project/install.go @@ -91,11 +91,22 @@ func (p *Project) writeTypes() error { file.WriteString("\n\n") for name := range p.app.Providers { - file.WriteString(`import _` + name + ` from "@pulumi/` + name + `";` + "\n") + file.WriteString(`import _` + name + `, { ProviderArgs as _` + name + `Args } from "@pulumi/` + name + `";` + "\n") } file.WriteString("\n\n") + file.WriteString(`declare module "./src/config" {` + "\n") + file.WriteString(` interface App {` + "\n") + file.WriteString(` providers?: {` + "\n") + for name := range p.app.Providers { + file.WriteString(` ` + name + `?: _` + name + `Args;` + "\n") + } + file.WriteString(` }` + "\n") + file.WriteString(` }` + "\n") + file.WriteString(`}` + "\n") + file.WriteString("\n") + file.WriteString(`declare global {` + "\n") for name := range p.app.Providers { file.WriteString(` // @ts-expect-error` + "\n") diff --git a/pkg/server/dev/aws/aws.go b/pkg/server/dev/aws/aws.go index 25c32cc45..46c8f7e3c 100644 --- a/pkg/server/dev/aws/aws.go +++ b/pkg/server/dev/aws/aws.go @@ -227,33 +227,43 @@ func Start( workerEnv := map[string][]string{} builds := map[string]*runtime.BuildOutput{} - run := func(functionID string, workerID string) bool { + getBuildOutput := func(functionID string) *runtime.BuildOutput { build := builds[functionID] + if build != nil { + return build + } warp := complete.Warps[functionID] - if build == nil { - build, err = runtime.Build(ctx, &runtime.BuildInput{ - Warp: warp, - Project: p, - Dev: true, - Links: complete.Links, + build, err = runtime.Build(ctx, &runtime.BuildInput{ + Warp: warp, + Project: p, + Dev: true, + Links: complete.Links, + }) + if err == nil { + bus.Publish(&FunctionBuildEvent{ + FunctionID: functionID, + Errors: build.Errors, + }) + } else { + bus.Publish(&FunctionBuildEvent{ + FunctionID: functionID, + Errors: []string{err.Error()}, }) - if err == nil { - bus.Publish(&FunctionBuildEvent{ - FunctionID: functionID, - Errors: build.Errors, - }) - } else { - bus.Publish(&FunctionBuildEvent{ - FunctionID: functionID, - Errors: []string{err.Error()}, - }) - } - if err != nil || len(build.Errors) > 0 { - delete(builds, functionID) - return false - } - builds[functionID] = build } + if err != nil || len(build.Errors) > 0 { + delete(builds, functionID) + return nil + } + builds[functionID] = build + return build + } + + run := func(functionID string, workerID string) bool { + build := getBuildOutput(functionID) + if build == nil { + return false + } + warp := complete.Warps[functionID] worker, _ := runtime.Run(ctx, &runtime.RunInput{ Server: server + workerID, Project: p, @@ -360,7 +370,16 @@ func Start( result, _ := http.Post("http://"+server+workerID+"/runtime/init/error", "application/json", strings.NewReader(`{"errorMessage":"Function failed to build"}`)) defer result.Body.Close() body, _ := io.ReadAll(result.Body) - slog.Info("error", "body", string(body)) + slog.Info("error", "body", string(body), "status", result.StatusCode) + + if result.StatusCode != 202 { + result, _ := http.Get("http://" + server + workerID + "/runtime/invocation/next") + requestID := result.Header.Get("lambda-runtime-aws-request-id") + result, _ = http.Post("http://"+server+workerID+"/runtime/invocation/"+requestID+"/error", "application/json", strings.NewReader(`{"errorMessage":"Function failed to build"}`)) + defer result.Body.Close() + body, _ := io.ReadAll(result.Body) + slog.Info("error", "body", string(body), "status", result.StatusCode) + } } break @@ -374,24 +393,31 @@ func Start( delete(workers, workerID) delete(workerEnv, workerID) case event := <-fileChan: - functions := map[string]bool{} + slog.Info("checking if code needs to be rebuilt", "file", event.Path) + toBuild := map[string]bool{} + for workerID, info := range workers { warp, ok := complete.Warps[info.FunctionID] if !ok { continue } - slog.Info("checking rebuild", "workerID", workerID, "functionID", info.FunctionID, "path", event.Path) if runtime.ShouldRebuild(warp.Runtime, warp.FunctionID, event.Path) { - slog.Info("rebuilding", "workerID", workerID, "functionID", info.FunctionID) + slog.Info("stopping", "workerID", workerID, "functionID", info.FunctionID) info.Worker.Stop() delete(builds, info.FunctionID) - functions[info.FunctionID] = true + toBuild[info.FunctionID] = true + } + } + + for functionID := range toBuild { + output := getBuildOutput(functionID) + if output == nil { + delete(toBuild, functionID) } } for workerID, info := range workers { - if functions[info.FunctionID] { - slog.Info("restarting", "workerID", workerID, "functionID", info.FunctionID) + if toBuild[info.FunctionID] { run(info.FunctionID, workerID) } }