Explorer

docs.http

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