-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement ConnInfo helper (#180)
* feat: implement ConnInfo helper * chore: change package.json to export ConnInfo helper * fix: lint * remove `bun.lockb` --------- Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { AddressInfo } from 'net' | ||
import type { GetConnInfo } from 'hono/conninfo' | ||
import type { HttpBindings } from './types' | ||
|
||
/** | ||
* ConnInfo Helper for Node.js | ||
* @param c Context | ||
* @returns ConnInfo | ||
*/ | ||
export const getConnInfo: GetConnInfo = (c) => { | ||
const bindings = (c.env.server ? c.env.server : c.env) as HttpBindings | ||
|
||
const address = bindings.incoming.socket.address() as AddressInfo | ||
|
||
if (!('address' in address)) { | ||
return { | ||
remote: {}, | ||
} | ||
} | ||
|
||
return { | ||
remote: { | ||
address: address.address, | ||
addressType: | ||
address.family === 'IPv4' ? 'IPv4' : address.family === 'IPv6' ? 'IPv6' : 'unknown', | ||
port: address.port, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { AddressInfo } from 'net' | ||
import { Hono } from 'hono' | ||
import type { AddressType, ConnInfo } from 'hono/conninfo' | ||
import { getConnInfo } from '../src/conninfo' | ||
|
||
describe('ConnInfo', () => { | ||
it('Should works', async () => { | ||
const app = new Hono().get('/', (c) => c.json(getConnInfo(c))) | ||
|
||
const address: AddressInfo = { | ||
address: '0.0.0.0', | ||
family: 'IPv4', | ||
port: 3030, | ||
} | ||
expect( | ||
await ( | ||
await app.request( | ||
'/', | ||
{}, | ||
{ | ||
incoming: { | ||
socket: { | ||
address: () => address, | ||
}, | ||
}, | ||
} | ||
) | ||
).json() | ||
).toEqual({ | ||
remote: { | ||
address: address.address, | ||
addressType: address.family as AddressType, | ||
port: address.port, | ||
}, | ||
} satisfies ConnInfo) | ||
}) | ||
}) |