Open14

syumai/workers で Cloudflare Pages Functions

zztkmzztkm

概要

Cloudflare Pages Functions が WebAssembly に対応した。

自分は普段Go言語で開発をしているので、syumai/workers を利用して Pages Functions を構築できたら嬉しいなというのが今回の動機。

これから作ろうと考えてるもの

  • BBS
  • Cloudflare Pages + Functions
  • D1 を使う

参考プロジェクトなど

https://blog.cloudflare.com/pages-functions-with-webassembly/

zztkmzztkm

お、以下のエラーが出てた

No routes found when building Functions directory: ./functions - skipping
zztkmzztkm

syumai さんたちが教えてくれたおかげで functions を呼び出すことはできた

//refs: https://github.com/syumai/worker-template-tinygo/blob/main/js/worker.mjs
import "./polyfill_performance.js";
import "./wasm_exec.js";
import mod from "./app.wasm";

const go = new Go();

const readyPromise = new Promise((resolve) => {
  globalThis.ready = resolve;
});

const load = WebAssembly.instantiate(mod, go.importObject).then((instance) => {
  go.run(instance);
  return instance;
});

export const onRequest = async (ctx) => {
  await load;
  await readyPromise;
  const {
    request,
    env,
  } = ctx;
  return handleRequest(request, { env, ctx });
}

しかし以下のようなエラーが発生してしまってる。。。

panic: unimplemented: (reflect.Type).Name()
[mf:err] GET /api/hello: RuntimeError: unreachable
    at asyncify_start_unwind (wasm://wasm/003dbf2e:1:331755)
    at tinygo_unwind (wasm://wasm/003dbf2e:1:1688)
    at internal/task.Pause (wasm://wasm/003dbf2e:1:36903)
    at runtime.deadlock (wasm://wasm/003dbf2e:1:127117)
    at runtime.resume$1$gowrapper (wasm://wasm/003dbf2e:1:124319)
    at tinygo_launch (wasm://wasm/003dbf2e:1:1718)
    at (*internal/task.Task).Resume (wasm://wasm/003dbf2e:1:35913)
    at runtime.scheduler (wasm://wasm/003dbf2e:1:123914)
    at resume (wasm://wasm/003dbf2e:1:124166)
    at resume.command_export (wasm://wasm/003dbf2e:1:331586)
GET /api/hello 500 Internal Server Error (48.56ms)
[mf:err] Unhandled Promise Rejection: RuntimeError: unreachable
    at runtime._panic (wasm://wasm/003dbf2e:1:7751)
    at (reflect.rawType).Name (wasm://wasm/003dbf2e:1:16645)
    at interface:{Align:func:{}{basic:int},AssignableTo:func:{named:reflect.Type}{basic:bool},Bits:func:{}{basic:int},ChanDir:func:{}{named:reflect.ChanDir},Comparable:func:{}{basic:bool},ConvertibleTo:func:{named:reflect.Type}{basic:bool},Elem:func:{}{named:reflect.Type},Field:func:{basic:int}{named:reflect.StructField},FieldAlign:func:{}{basic:int},FieldByIndex:func:{slice:basic:int}{named:reflect.StructField},FieldByName:func:{basic:string}{named:reflect.StructField,basic:bool},Implements:func:{named:reflect.Type}{basic:bool},In:func:{basic:int}{named:reflect.Type},IsVariadic:func:{}{basic:bool},Key:func:{}{named:reflect.Type},Kind:func:{}{named:reflect.Kind},Len:func:{}{basic:int},MethodByName:func:{basic:string}{named:reflect.Method,basic:bool},Name:func:{}{basic:string},NumField:func:{}{basic:int},NumIn:func:{}{basic:int},NumMethod:func:{}{basic:int},NumOut:func:{}{basic:int},Out:func:{basic:int}{named:reflect.Type},PkgPath:func:{}{basic:string},Size:func:{}{basic:uintptr},String:func:{}{basic:string}}.Name$invoke (wasm://wasm/003dbf2e:1:239708)
    at encoding/json.typeEncoder (wasm://wasm/003dbf2e:1:247111)
    at (*encoding/json.encodeState).reflectValue (wasm://wasm/003dbf2e:1:269835)
    at main.main$1 (wasm://wasm/003dbf2e:1:316016)
    at (net/http.HandlerFunc).ServeHTTP (wasm://wasm/003dbf2e:1:309976)
    at interface:{ServeHTTP:func:{named:net/http.ResponseWriter,pointer:named:net/http.Request}{}}.ServeHTTP$invoke (wasm://wasm/003dbf2e:1:299774)
    at interface:{ServeHTTP:func:{named:net/http.ResponseWriter,pointer:named:net/http.Request}{}}.ServeHTTP$invoke (wasm://wasm/003dbf2e:1:301592)
    at github.com/syumai/workers.handleRequest$1 (wasm://wasm/003dbf2e:1:330012)
zztkmzztkm

わかった。
多分おれの main.go の実装が悪い

zztkmzztkm

TinyGo 0.28 で encoding/json が動くようになるらしいので期待!