Explorer

Docs / docs.http

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 つを入れたサーバー