Skip to content

ConnInfo 헬퍼

ConnInfo 헬퍼는 연결 정보를 쉽게 가져올 수 있도록 도와준다. 예를 들어, 클라이언트의 원격 주소를 간단히 확인할 수 있다.

Import

ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/cloudflare-workers'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/deno'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/bun'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/vercel'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/lambda-edge'
ts
import { Hono } from 'hono'
import { getConnInfo } from '@hono/node-server/conninfo'

사용 방법

ts
const app = new Hono()

app.get('/', (c) => {
  const info = getConnInfo(c) // info는 `ConnInfo` 타입
  return c.text(`당신의 원격 주소는 ${info.remote.address}입니다`)
})

타입 정의

getConnInfo() 함수에서 반환할 수 있는 값들의 타입 정의는 다음과 같다:

ts
type AddressType = 'IPv6' | 'IPv4' | undefined

type NetAddrInfo = {
  /**
   * 전송 프로토콜 타입
   */
  transport?: 'tcp' | 'udp'
  /**
   * 전송 포트 번호
   */
  port?: number

  address?: string
  addressType?: AddressType
} & (
  | {
      /**
       * IP 주소와 같은 호스트 이름
       */
      address: string

      /**
       * 호스트 이름 타입
       */
      addressType: AddressType
    }
  | {}
)

/**
 * HTTP 연결 정보
 */
interface ConnInfo {
  /**
   * 원격 정보
   */
  remote: NetAddrInfo
}

Released under the MIT License.