diff --git a/cmd/workers-assets-gen/assets/common/shim.mjs b/cmd/workers-assets-gen/assets/common/shim.mjs index 2d93a85f..52ec993a 100644 --- a/cmd/workers-assets-gen/assets/common/shim.mjs +++ b/cmd/workers-assets-gen/assets/common/shim.mjs @@ -1,5 +1,5 @@ import "./wasm_exec.js"; -import { connect } from 'cloudflare:sockets'; +import { createRuntimeContext } from "./runtime.mjs"; let mod; @@ -8,12 +8,12 @@ globalThis.tryCatch = (fn) => { return { result: fn(), }; - } catch(e) { + } catch (e) { return { error: e, }; } -} +}; export function init(m) { mod = m; @@ -29,31 +29,24 @@ async function run(ctx) { const instance = new WebAssembly.Instance(mod, { ...go.importObject, workers: { - ready: () => { ready() } + ready: () => { + ready(); + }, }, }); go.run(instance, ctx); await readyPromise; } -function createRuntimeContext(env, ctx, binding) { - return { - env, - ctx, - connect, - binding, - }; -} - export async function fetch(req, env, ctx) { const binding = {}; - await run(createRuntimeContext(env, ctx, binding)); + await run(createRuntimeContext({ env, ctx, binding })); return binding.handleRequest(req); } export async function scheduled(event, env, ctx) { const binding = {}; - await run(createRuntimeContext(env, ctx, binding)); + await run(createRuntimeContext({ env, ctx, binding })); return binding.runScheduler(event); } @@ -61,12 +54,12 @@ export async function scheduled(event, env, ctx) { export async function onRequest(ctx) { const binding = {}; const { request, env } = ctx; - await run(createRuntimeContext(env, ctx, binding)); + await run(createRuntimeContext({ env, ctx, binding })); return binding.handleRequest(request); } export async function queue(batch, env, ctx) { const binding = {}; - await run(createRuntimeContext(env, ctx, binding)); + await run(createRuntimeContext({ env, ctx, binding })); return binding.handleQueueMessageBatch(batch); } diff --git a/cmd/workers-assets-gen/assets/runtime/cloudflare.mjs b/cmd/workers-assets-gen/assets/runtime/cloudflare.mjs new file mode 100644 index 00000000..baa446d2 --- /dev/null +++ b/cmd/workers-assets-gen/assets/runtime/cloudflare.mjs @@ -0,0 +1,10 @@ +import { connect } from "cloudflare:sockets"; + +export function createRuntimeContext({ env, ctx, binding }) { + return { + env, + ctx, + connect, + binding, + }; +} diff --git a/cmd/workers-assets-gen/main.go b/cmd/workers-assets-gen/main.go index 7ce13245..5f082065 100644 --- a/cmd/workers-assets-gen/main.go +++ b/cmd/workers-assets-gen/main.go @@ -16,13 +16,18 @@ var assets embed.FS const ( assetDirPath = "assets" commonDirPath = "assets/common" + runtimeDirPath = "assets/runtime" defaultBuildDirPath = "build" ) func main() { - var mode string - var buildDirPath string + var ( + mode string + runtime string + buildDirPath string + ) flag.StringVar(&mode, "mode", string(ModeTinygo), `build mode: tinygo or go`) + flag.StringVar(&runtime, "runtime", string(RuntimeCloudflare), `runtime: cloudflare`) flag.StringVar(&buildDirPath, "o", defaultBuildDirPath, `output dir path: defaults to "build"`) flag.Parse() if !Mode(mode).IsValid() { @@ -30,13 +35,18 @@ func main() { os.Exit(1) return } - if err := runMain(Mode(mode), buildDirPath); err != nil { + if !Runtime(runtime).IsValid() { + flag.PrintDefaults() + os.Exit(1) + return + } + if err := runMain(Mode(mode), Runtime(runtime), buildDirPath); err != nil { fmt.Fprintf(os.Stderr, "err: %v", err) os.Exit(1) } } -func runMain(mode Mode, buildDirPath string) error { +func runMain(mode Mode, runtime Runtime, buildDirPath string) error { if err := os.RemoveAll(buildDirPath); err != nil { return err } @@ -46,6 +56,9 @@ func runMain(mode Mode, buildDirPath string) error { if err := copyWasmExecJS(mode, buildDirPath); err != nil { return err } + if err := copyRuntimeAssets(runtime, buildDirPath); err != nil { + return err + } if err := copyCommonAssets(buildDirPath); err != nil { return err } @@ -70,6 +83,15 @@ func copyWasmExecJS(mode Mode, buildDirPath string) error { return nil } +func copyRuntimeAssets(runtime Runtime, buildDirPath string) error { + destPath := path.Join(buildDirPath, "runtime.mjs") + originPath := path.Join(runtimeDirPath, runtime.AssetFileName()) + if err := copyFile(destPath, originPath); err != nil { + return err + } + return nil +} + func copyCommonAssets(buildDirPath string) error { entries, err := assets.ReadDir(commonDirPath) if err != nil { @@ -90,9 +112,6 @@ func copyFile(destPath, originPath string) error { if err != nil { return err } - if err != nil { - return err - } dest, err := os.Create(destPath) if err != nil { return err @@ -102,8 +121,5 @@ func copyFile(destPath, originPath string) error { if err != nil { return err } - if err != nil { - return err - } return nil } diff --git a/cmd/workers-assets-gen/runtime.go b/cmd/workers-assets-gen/runtime.go new file mode 100644 index 00000000..aa19ab20 --- /dev/null +++ b/cmd/workers-assets-gen/runtime.go @@ -0,0 +1,19 @@ +package main + +type Runtime string + +const ( + RuntimeCloudflare Runtime = "cloudflare" +) + +func (r Runtime) IsValid() bool { + switch r { + case RuntimeCloudflare: + return true + } + return false +} + +func (r Runtime) AssetFileName() string { + return string(r) + ".mjs" +}