File tree Expand file tree Collapse file tree 2 files changed +14
-23
lines changed
Expand file tree Collapse file tree 2 files changed +14
-23
lines changed Original file line number Diff line number Diff line change 11import type { GetConnInfo } from 'hono/conninfo'
2- import type { AddressInfo } from 'net'
32import type { HttpBindings } from './types'
43
54/**
@@ -10,20 +9,15 @@ import type { HttpBindings } from './types'
109export const getConnInfo : GetConnInfo = ( c ) => {
1110 const bindings = ( c . env . server ? c . env . server : c . env ) as HttpBindings
1211
13- const address = bindings . incoming . socket . address ( ) as AddressInfo
14-
15- if ( ! ( 'address' in address ) ) {
16- return {
17- remote : { } ,
18- }
19- }
12+ const address = bindings . incoming . socket . remoteAddress
13+ const port = bindings . incoming . socket . remotePort
14+ const family = bindings . incoming . socket . remoteFamily
2015
2116 return {
2217 remote : {
23- address : address . address ,
24- addressType :
25- address . family === 'IPv4' ? 'IPv4' : address . family === 'IPv6' ? 'IPv6' : 'unknown' ,
26- port : address . port ,
18+ address,
19+ port,
20+ addressType : family === 'IPv4' ? 'IPv4' : family === 'IPv6' ? 'IPv6' : void 0 ,
2721 } ,
2822 }
2923}
Original file line number Diff line number Diff line change 11import { Hono } from 'hono'
22import type { AddressType , ConnInfo } from 'hono/conninfo'
3- import type { AddressInfo } from 'net'
43import { getConnInfo } from '../src/conninfo'
54
65describe ( 'ConnInfo' , ( ) => {
76 it ( 'Should works' , async ( ) => {
87 const app = new Hono ( ) . get ( '/' , ( c ) => c . json ( getConnInfo ( c ) ) )
98
10- const address : AddressInfo = {
11- address : '0.0.0.0' ,
12- family : 'IPv4' ,
13- port : 3030 ,
9+ const socket = {
10+ remoteAddress : '0.0.0.0' ,
11+ remoteFamily : 'IPv4' ,
12+ remotePort : 3030 ,
1413 }
1514 expect (
1615 await (
@@ -19,18 +18,16 @@ describe('ConnInfo', () => {
1918 { } ,
2019 {
2120 incoming : {
22- socket : {
23- address : ( ) => address ,
24- } ,
21+ socket,
2522 } ,
2623 }
2724 )
2825 ) . json ( )
2926 ) . toEqual ( {
3027 remote : {
31- address : address . address ,
32- addressType : address . family as AddressType ,
33- port : address . port ,
28+ address : socket . remoteAddress ,
29+ addressType : socket . remoteFamily as AddressType ,
30+ port : socket . remotePort ,
3431 } ,
3532 } satisfies ConnInfo )
3633 } )
You can’t perform that action at this time.
0 commit comments