HTTP (http プロジェクト) と, ブラウザ・サーバーの CBOR の通信
2026-12-05 ユーザーの依頼「昔みたいに CBOR で通信するように。wasi preview3 スタイルの HTTP リクエストで。cbor プロジェクトと http プロジェクトを作ってコードを分ける」で, HTTP の型を 1 つにそろえ, ブラウザとサーバーの通信を CBOR にした。サーバーが受け取る側と送る側もそろえる (ユーザーの決定カード)。
http プロジェクト (モジュール http)
wasi:http (WASI 0.3, preview 3) の types と handler を, リソースではなくただの値で表す。本文はストリームではなく全体のバイト列 (トレーラーは無い)。
どのプロジェクトからも使える (定義は core と std だけを使う)。正本は definy のストアの http プロジェクト (main の作成者は opus5.5)。
http.method = get | head | post | put | delete | connect | options | trace | patch | other(String)
http.scheme = http | https | other(String)
http.field = { name: String, value: String } -- WASI では値はバイト列。ブラウザとホストは文字列で扱うので文字列にした
http.request = { method: http.method, scheme: option<http.scheme>, authority: option<String>,
path_with_query: option<String>, headers: List<http.field>, body: List<Number> }
http.response = { status: Number, headers: List<http.field>, body: List<Number> }
http.error-code = dns_timeout | dns_error | destination_not_found | ... | loop_detected | configuration_error
| internal_error(option<String>) -- WASI の error-code から, 中身のある種類を internal_error 以外除いたもの
http.result = ok(http.response) | error(http.error-code) -- wasi:http/handler の handle の結果schemeとauthorityが無いリクエストは同じオリジン (ブラウザではページのサーバー) に送る。path_with_queryが無ければ/- 部品:
http.url,http.path,http.query,http.path-with-query,http.field-get(名前の大文字小文字を区別しない),http.method-text,http.method-of-text,http.scheme-text,http.error-text,http.body-text(UTF-8),http.post(path_with_query, content_type, body),http.request-of-url(method, url, headers, body)(http.url-targetで URL を分ける) - テスト
http.test-*(4 個)
HTTP を送る: handle 1 つに
definy.io の HTTP のタグは handle だけ (wasi:http/handler の handle)。以前の outgoing_request (wasi.outgoing-request, プログラム用) と
http_fetch (definy.http-fetch-*, サーバー用) は消した。
handle { request: http.request, secret_headers: List<{ name, prefix, secret: Secret }>, then: fn(result: http.result) => definy.io(A) }secret_headersは WASI には無い definy の拡張で, 値はホストが送る直前に, そのシークレットに許された URL にだけ入れる (secret.md)- リダイレクトはたどらない (WASI の handler と同じ。以前の
http_fetchと同じ) - ホストは fetch の失敗を
http.error-codeにする (Node のcause.code:ECONNREFUSED→connection_refused,ENOTFOUND→dns_error,ETIMEDOUT→connection_timeoutなど。他とブラウザはinternal_error(メッセージ))。サーバーで authority の無いリクエストはhttp_request_uri_invalid - 動かすのは
definy.cloudflare-service-runtime-script(サーバーの処理とrun_wasi_ioの両方),html-ui.browser-run-script(ブラウザで実行),html-ui.client-runtime-script(ブラウザ側の wasm のhtml-ui.client-ioのhandle)
HTTP を受け取る: Fn(request: http.request) -> definy.service-io
- サーバーの入口 (
definy.handle-request,html-ui.local-handle,html-ui.fly-handle) はhttp.requestを受け取る。definy.http-requestは消した - 本文もリクエストに入っている (バイト列)。
read_request_json/read_request_textの能力は消した。JSON はdefiny.request-json-of(request)(definy のdefiny.parse-json), 文字列はstd.bytes-to-string(bytes: request.body) - 以前リクエストにあった
storageとhas_assetsはホストの設定read_host_settingsの{run_expressions, storage, has_assets}で読む - ヘッダーの型は
http.field(definy.http-headerは消した) - サーバーの答え (
definy.service-response) はhttp.responseにしていない。本文definy.http-bodyはempty/text/json/base64にbytesを足した。JSON を値のまま持つのは, html-ui がサーバーの中から RPC の処理を呼んで答えをそのまま読む (文字列にして読み直さない) ためと,/stage1.wasmなど大きなバイナリを Base64 の定数のまま返すため - 速さ: 受け取った本文をバイト列の値にして definy で JSON を読むのは 1.2 MB で約 0.6 秒 (クラウドの Node)。MCP の大きな呼び出し (124 KB) は前と同じ約 3.2 秒
ブラウザとサーバーの通信を CBOR に
Connect-RPC の codec として application/cbor を受け付ける。サーバーは, 本文が CBOR (Content-Type: application/cbor) か
Accept に application/cbor があるリクエスト (definy.request-wants-cbor) に CBOR で答える。それ以外は今までどおり JSON。
- RPC の処理は JSON (
definy.json) で書いたまま。definy.rpc-request-jsonが CBOR の本文をdefiny.cbor-to-jsonで JSON にし (バイト列は Base64 の文字列, Connect-RPC の JSON と同じ),definy.rpc-respondが答えの JSON をdefiny.json-to-cborで CBOR にする。 キーがBytesで終わる Base64 の文字列 (signedEventBytes,contentBytes) は CBOR のバイト列にする。テキストの答えは CBOR のテキスト - 使う所:
/definy.v1.*の RPC (definy.rpc-call,definy.handle-event-service), html-ui のPOST /api/<name>(html-ui.api-body),GET /run-parts(答えのexpressionBytesとparts[].cborBytesがバイト列) - ブラウザ:
- アカウント作成 (
html-ui.client-create-account) はSubmitEventに CBOR の{signedEventBytes: バイト列}をhandleで送り, 失敗の理由は CBOR の{code, message}から出す (html-ui.client-response-error) - ページのスクリプト (
html-ui.tool-script,snapshot-script,batch-script) はhtml-ui.cbor-script(globalThis.definyCbor:encode,decode,api(path, args)) で/apiを CBOR で呼ぶ。各スクリプトは@core.string-concat(left: @html-ui.cbor-script, right: ...)の定数 html-ui.browser-run-scriptは/run-partsをAccept: application/cborで読む- JSON のまま残したもの:
/mcp(MCP は JSON-RPC が決まり), サーバー間の送受信 (definy.sync-remote-rpc。公開の fly がまだ古い間も送れるように。 受け取る側は JSON も CBOR も受け付ける), ページの HTML と/tree-parts(HTML の断片)
確かめたこと (クラウドの写し)
- テスト: definy 159 個 (コンパイル版も), cbor 4 個, http 4 個, html-ui 1 個
curl相当: CBOR のGetEventsはsignedEventBytesがバイト列の CBOR, JSON はそのまま JSON, 壊れた CBOR は CBOR のinvalid_argument(400)- Playwright: ブラウザでアカウント作成 (手元形と fly 形の両方),
/api/definy_get_part(成功と 400), 型の確認 (/api/snapshot), ブラウザで実行 (sample.io-all-race:/run-partsが CBOR,handleの失敗とraceのsleep) - MCP, fly からの受信 (
definy_receive), fly 形の写しへの送信 (definy_send),definy_run_io(handle,dns_error),html-ui.build-release(シークレット付きのhandleで 1.17 MB の MCP 呼び出し) と, できたリリース 2 つを入れたサーバー