0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HonoのマージされたPRを読んでいく#41 - #45

0
Last updated at Posted at 2025-12-08

この記事何?

エンジニア歴1年半。業務で利用しているHonoが大好きだが、TSもプログラミング知識も弱々すぎて上手く使いこなせない。速いはずが多分生かせてない。
ちゃんと理解するためにソースコードリーディングしたい。

でも目的なくソースコード眺めても意味ないし……ねむい……
そうだ!PRを全部読めば、変遷やなぜ変更されているかストーリー的に分かるのでは。
と思ったのでチャレンジしてみる。PR2000以上あるので、全部できるかは知らん。
https://github.com/honojs/hono

#41 feat(miniflare): repalce service-worker-mock with miniflare

metrueさんのPR
テスト環境を service-worker-mock から Miniflare に移行

Miniflareってなんだ
https://developers.cloudflare.com/workers/testing/miniflare/

Miniflareは、 Cloudflare Workers ↗の開発とテストのためのシミュレーターです 。TypeScriptで記述されており、WorkersのランタイムAPIを実装したサンドボックス内でコードを実行します。

fmfm...そんなのがあるのか。実際の環境に近いものでテストできるの便利。

#42 なし

#43 Support module workers syntax for Cloudflare Workers

Module Workers構文(Cloudflare Workers の新しい書き方)をサポート。

// 変更前 (Service Worker 構文)

  // 旧Service Worker 構文
  import { Hono } from 'hono'

  const app = new Hono()

  app.get('/', (c) => c.text('Hello'))

  app.fire()  // ← addEventListener を自動で呼ぶ

  // 内部実装:
  fire() {
    addEventListener('fetch', (event: FetchEvent): void => {
      event.respondWith(this.handleEvent(event))
    })
  }

  // 変更後 (Module Workers 構文)

  import { Hono } from 'hono'

  const app = new Hono()

  app.get('/', (c) => c.text('Hello'))

  // export default でfetchメソッドを公開
  export default app  // ← app.fetch() が自動的に呼ばれる

  // 内部実装 (新規追加):
  // src/hono.ts
  async fetch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {
    return this.dispatch(request, env, event)
  }

  // src/context.ts の変更:
  export interface Env {
    Bindings?: Record<string, unknown>
    Variables?: Record<string, unknown>
  }

  export class Context {
    req: HonoRequest
    env: Env  // ← 環境変数を保持

    constructor(req: Request, env?: Env, event?: FetchEvent) {
      this.req = new HonoRequest(req)
      this.env = env || {}  // ← envを受け取る
      // ...
    }
  }

うん?? ちょっとよくわかんなかった。ふむふむ....

#44 fix: correct typo for change directory

README の typo(誤字)修正。

#45 docs: fixed readme

README の文言・フォーマット修正。

今回はあんまり集中してコードリーディングできなかったぁああああ。Miniflareの存在を新たに知ったのでヨシ!次に行きます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?