Skip to content
Merged
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
27 changes: 10 additions & 17 deletions cmd/workers-assets-gen/assets/common/shim.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./wasm_exec.js";
import { connect } from 'cloudflare:sockets';
import { createRuntimeContext } from "./runtime.mjs";

let mod;

Expand All @@ -8,12 +8,12 @@ globalThis.tryCatch = (fn) => {
return {
result: fn(),
};
} catch(e) {
} catch (e) {
return {
error: e,
};
}
}
};

export function init(m) {
mod = m;
Expand All @@ -29,44 +29,37 @@ 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);
}

// onRequest handles request to Cloudflare Pages
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);
}
10 changes: 10 additions & 0 deletions cmd/workers-assets-gen/assets/runtime/cloudflare.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { connect } from "cloudflare:sockets";

export function createRuntimeContext({ env, ctx, binding }) {
return {
env,
ctx,
connect,
binding,
};
}
36 changes: 26 additions & 10 deletions cmd/workers-assets-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,37 @@ 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() {
flag.PrintDefaults()
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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -102,8 +121,5 @@ func copyFile(destPath, originPath string) error {
if err != nil {
return err
}
if err != nil {
return err
}
return nil
}
19 changes: 19 additions & 0 deletions cmd/workers-assets-gen/runtime.go
Original file line number Diff line number Diff line change
@@ -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"
}
Loading