Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Honoのおもしろいミドルウェアをみてみよう
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Yusuke Wada
February 15, 2025
Programming
690
2
Share
Honoのおもしろいミドルウェアをみてみよう
湘.なんか #2
Yusuke Wada / 2025—02-15
Yusuke Wada
February 15, 2025
More Decks by Yusuke Wada
See All by Yusuke Wada
飯MCP
yusukebe
0
550
へんな働き方
yusukebe
6
3k
Cap'n Webについて
yusukebe
0
330
OSS開発者の憂鬱
yusukebe
17
30k
r2-image-worker
yusukebe
1
250
Introduce Hono CLI
yusukebe
6
4.1k
私はどうやって技術力を上げたのか
yusukebe
47
21k
Reactをクライアントで使わない
yusukebe
8
7.1k
AI時代のUIはどこへ行く?
yusukebe
23
12k
Other Decks in Programming
See All in Programming
空間オーディオの活用
objectiveaudio
0
160
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
460
Sans tests, vos agents ne sont pas fiables
nabondance
0
140
(Re)make Regexp in Ruby: Democratizing internals for the JIT
makenowjust
3
1.1k
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
3
400
ローカルLLMでどこまでコードが書けるか / How much code can be written on a local LLM
kishida
2
370
次世代リンターで探る、tsgo 時代における型認識カスタムルールの現実解
ytakahashii
0
190
ECR拡張スキャンでSBOMを収集して サプライチェーン攻撃の影響調査を 爆速で終わらせてみた
akihisaikeda
1
140
リセットCSSを1行消したらアクセシビリティが向上した話
pvcresin
4
520
AI時代だからこそ「Bloc」を採用する価値があるのかもしれない
takuroabe
0
190
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.7k
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
0
270
Featured
See All Featured
Exploring anti-patterns in Rails
aemeredith
3
360
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
WENDY [Excerpt]
tessaabrams
10
37k
First, design no harm
axbom
PRO
2
1.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
300
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Evolving SEO for Evolving Search Engines
ryanjones
0
200
Google's AI Overviews - The New Search
badams
0
1k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
410
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
199
73k
Transcript
Yusuke Wada / 2025 — 02-15 Honoのおもしろいミドルウェア をみてみよう 湘.なんか #2
Honoのおもしろいミドルウェアをみてみよう • その名の通りHonoのおもしろいミドルウェアをピックアップしてみます • おもしろいは個 人 的主観です
Honoのミドルウェア • Custom Middleware - 自 分で定義する • Built-in Middleware
- `hono`パッケージに同封されている • 外部のライブラリに依存しない • 3rd-party Middleware - `hono`パッケージには 入 っていない • 外部のライブラリに依存していい • `@hono`ネームスペース(例:`@hono/hello`)に置かれる場合がある
Built-in Middleware 23個ある 1.Basic Authentication 2.Bearer Authentication 3.Body Limit 4.Cache
5.Combine 6.Compress 7.Context Storage 8.CORS 9.CSRF Protection 10.ETag 11.IP Restriction 12.JSX Renderer 13.JWK 14.JWT 15.Logger 16.Language 17.Method Override 18.Pretty JSON 19.Request ID 20.Secure Headers 21.Timeout 22.Timing 23.Trailing Slash
3rd-party Middleware Webサイトに掲載されているものだけで34個ある
ミドルウェアの使い 方
ではいってみよう
Standard Scheme Validator Middleware
Standard Scheme Validator Middleware • 3rd-party Middleware • Standard Schema
Specに準拠したバリデータ • Standard Schema? • TypeScript/JavaScriptのいくつかあるバリデータの標準インター フェースをつくるプロジェクト • Zod / Valibot / ArkType などが対応している • これまであったZod Validator / Valibot Validator / ArkType Validatorの代わりになり、ひとつのミドルウェアで全バリデータに対応で きる
使い 方 Zodの場合
使い 方 全部使ってみる ぜんぶsValidatorでいい!
JSX Renderer Middleware
JSX Renderer Middleware • 個 人 的に好きなミドルウェア • JSXを扱う時に`c.render`の挙動を簡単に定義できる
c.render / c.setRenderer 「コンテンツを受け取ってResponseを返す」を定義する c.renderでコンテンツを渡す Responseなんで直接返せる
JSX Renderer MiddlewareはJSXをコンテンツとして扱うのに便利
`children`以外にも渡せる
Context Storage Middleware
Context Storage Middleware • `Context`オブジェクトにグローバルでアクセスすることができるようになる • `AsyncLocalStorage`ってのを使っている • `Context`へはハンドラかミドルウェア内じゃないアクセスできなかった •
グローバルに定義するのは基本的にしない • Request => Handler => Responseの中で`Context`を扱うのが基本 • AsyncLocalStorageはそこで使えるストレージを提供する
`Context`へのアクセス
`Context`にグローバルでアクセスできる
Server-Timing Middleware
Server-Timing Middleware • パフォーマンスをServer-Timingレスポンスヘッダに書き出す
None
ChromeのDevToolsで 見 れるの便利
esbuild Transpiler Middleware
esbuild Transpiler Middleware • 3rd-party • TypeScript/TSXなどをアクセスが来たらビルドして返却する • バンドルしなくて済む! •
毎回ビルドしなくてはいけないがうまくキャッシュさせることもできそう • wasmのesbuildを使っている
None
以上!
おもしろいミドルウェアでした • Standard Schema Validator Middleware • JSX Renderer Middleware
• Context Storage Middleware • Server-Timing Middleware • esbuild Transpiler Middleware
おしまい