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?

この記事何?

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

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

#121 refactor: compose

変更前: 不要なパラメータと変数があった

  export const compose = <T>(middleware: Function[], onError?: ErrorHandler) => {
    return function (context: T, next?: Function) {  // ← next は使われていない
      let index = -1
      return dispatch(0)

      async function dispatch(i: number): Promise<T> {
        if (i <= index) return Promise.reject(new Error('next() called multiple times'))
        const handler = middleware[i]  // ← この変数は使わない
        index = i
        let fn = middleware[i]         // ← こっちを使っている(重複)
        if (i === middleware.length) fn = next
        if (!fn) return context
        // ...
      }
    }
  }

変更後

  export const compose = <C>(middleware: Function[], onError?: ErrorHandler) => {
    return function (context: C) {  // next を削除
      let index = -1
      return dispatch(0)

      async function dispatch(i: number): Promise<C> {
        // 終了条件を最初にチェック
        if (i === middleware.length) {
          return context
        }
        if (i <= index) {
          return Promise.reject(new Error('next() called multiple times'))
        }

        const handler = middleware[i]  // 1つの変数だけ使う
        index = i

        try {
          return Promise.resolve(handler(context, dispatch.bind(null, i + 1)))
            .then(() => { return context })
            .catch((err) => {
              if (onError && context instanceof Context) {
                context.res = onError(err, context)
                return context
              } else {
                throw err
              }
            })
        } catch (err) {
          return Promise.reject(err)
        }
      }
    }
  }

#122 fix: mime-type bug

MIMEタイプのバグ修正

変更前: undefined のチェックがない
変更後: undefined チェックを追加

#123 chore: update examples

サンプルコードの細かい修正

#124 なし

#125 docs: write instructions

ドキュメントの改善

  • Cloudflare Workers のセットアップ手順を更新
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?