Skip to content

Commit c0ddc67

Browse files
authored
Merge pull request #166 from syumai/split-runtime-param-of-workers-assets-gen
split runtime param of workers-assets-gen command
2 parents 427980b + 6b44b14 commit c0ddc67

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "./wasm_exec.js";
2-
import { connect } from 'cloudflare:sockets';
2+
import { createRuntimeContext } from "./runtime.mjs";
33

44
let mod;
55

@@ -8,12 +8,12 @@ globalThis.tryCatch = (fn) => {
88
return {
99
result: fn(),
1010
};
11-
} catch(e) {
11+
} catch (e) {
1212
return {
1313
error: e,
1414
};
1515
}
16-
}
16+
};
1717

1818
export function init(m) {
1919
mod = m;
@@ -29,44 +29,37 @@ async function run(ctx) {
2929
const instance = new WebAssembly.Instance(mod, {
3030
...go.importObject,
3131
workers: {
32-
ready: () => { ready() }
32+
ready: () => {
33+
ready();
34+
},
3335
},
3436
});
3537
go.run(instance, ctx);
3638
await readyPromise;
3739
}
3840

39-
function createRuntimeContext(env, ctx, binding) {
40-
return {
41-
env,
42-
ctx,
43-
connect,
44-
binding,
45-
};
46-
}
47-
4841
export async function fetch(req, env, ctx) {
4942
const binding = {};
50-
await run(createRuntimeContext(env, ctx, binding));
43+
await run(createRuntimeContext({ env, ctx, binding }));
5144
return binding.handleRequest(req);
5245
}
5346

5447
export async function scheduled(event, env, ctx) {
5548
const binding = {};
56-
await run(createRuntimeContext(env, ctx, binding));
49+
await run(createRuntimeContext({ env, ctx, binding }));
5750
return binding.runScheduler(event);
5851
}
5952

6053
// onRequest handles request to Cloudflare Pages
6154
export async function onRequest(ctx) {
6255
const binding = {};
6356
const { request, env } = ctx;
64-
await run(createRuntimeContext(env, ctx, binding));
57+
await run(createRuntimeContext({ env, ctx, binding }));
6558
return binding.handleRequest(request);
6659
}
6760

6861
export async function queue(batch, env, ctx) {
6962
const binding = {};
70-
await run(createRuntimeContext(env, ctx, binding));
63+
await run(createRuntimeContext({ env, ctx, binding }));
7164
return binding.handleQueueMessageBatch(batch);
7265
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { connect } from "cloudflare:sockets";
2+
3+
export function createRuntimeContext({ env, ctx, binding }) {
4+
return {
5+
env,
6+
ctx,
7+
connect,
8+
binding,
9+
};
10+
}

cmd/workers-assets-gen/main.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,37 @@ var assets embed.FS
1616
const (
1717
assetDirPath = "assets"
1818
commonDirPath = "assets/common"
19+
runtimeDirPath = "assets/runtime"
1920
defaultBuildDirPath = "build"
2021
)
2122

2223
func main() {
23-
var mode string
24-
var buildDirPath string
24+
var (
25+
mode string
26+
runtime string
27+
buildDirPath string
28+
)
2529
flag.StringVar(&mode, "mode", string(ModeTinygo), `build mode: tinygo or go`)
30+
flag.StringVar(&runtime, "runtime", string(RuntimeCloudflare), `runtime: cloudflare`)
2631
flag.StringVar(&buildDirPath, "o", defaultBuildDirPath, `output dir path: defaults to "build"`)
2732
flag.Parse()
2833
if !Mode(mode).IsValid() {
2934
flag.PrintDefaults()
3035
os.Exit(1)
3136
return
3237
}
33-
if err := runMain(Mode(mode), buildDirPath); err != nil {
38+
if !Runtime(runtime).IsValid() {
39+
flag.PrintDefaults()
40+
os.Exit(1)
41+
return
42+
}
43+
if err := runMain(Mode(mode), Runtime(runtime), buildDirPath); err != nil {
3444
fmt.Fprintf(os.Stderr, "err: %v", err)
3545
os.Exit(1)
3646
}
3747
}
3848

39-
func runMain(mode Mode, buildDirPath string) error {
49+
func runMain(mode Mode, runtime Runtime, buildDirPath string) error {
4050
if err := os.RemoveAll(buildDirPath); err != nil {
4151
return err
4252
}
@@ -46,6 +56,9 @@ func runMain(mode Mode, buildDirPath string) error {
4656
if err := copyWasmExecJS(mode, buildDirPath); err != nil {
4757
return err
4858
}
59+
if err := copyRuntimeAssets(runtime, buildDirPath); err != nil {
60+
return err
61+
}
4962
if err := copyCommonAssets(buildDirPath); err != nil {
5063
return err
5164
}
@@ -70,6 +83,15 @@ func copyWasmExecJS(mode Mode, buildDirPath string) error {
7083
return nil
7184
}
7285

86+
func copyRuntimeAssets(runtime Runtime, buildDirPath string) error {
87+
destPath := path.Join(buildDirPath, "runtime.mjs")
88+
originPath := path.Join(runtimeDirPath, runtime.AssetFileName())
89+
if err := copyFile(destPath, originPath); err != nil {
90+
return err
91+
}
92+
return nil
93+
}
94+
7395
func copyCommonAssets(buildDirPath string) error {
7496
entries, err := assets.ReadDir(commonDirPath)
7597
if err != nil {
@@ -90,9 +112,6 @@ func copyFile(destPath, originPath string) error {
90112
if err != nil {
91113
return err
92114
}
93-
if err != nil {
94-
return err
95-
}
96115
dest, err := os.Create(destPath)
97116
if err != nil {
98117
return err
@@ -102,8 +121,5 @@ func copyFile(destPath, originPath string) error {
102121
if err != nil {
103122
return err
104123
}
105-
if err != nil {
106-
return err
107-
}
108124
return nil
109125
}

cmd/workers-assets-gen/runtime.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
type Runtime string
4+
5+
const (
6+
RuntimeCloudflare Runtime = "cloudflare"
7+
)
8+
9+
func (r Runtime) IsValid() bool {
10+
switch r {
11+
case RuntimeCloudflare:
12+
return true
13+
}
14+
return false
15+
}
16+
17+
func (r Runtime) AssetFileName() string {
18+
return string(r) + ".mjs"
19+
}

0 commit comments

Comments
 (0)