-
-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
2,298 additions
and
177 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
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,57 @@ | ||
import { Context } from '../../context' | ||
import { HonoRequest } from '../../request' | ||
import { getConnInfo } from './conninfo' | ||
|
||
const createRandomBunServer = () => { | ||
const address = Math.random().toString() | ||
const port = Math.floor(Math.random() * (65535 + 1)) | ||
return { | ||
address, | ||
port, | ||
server: { | ||
requestIP() { | ||
return { | ||
address, | ||
family: 'IPv6', | ||
port, | ||
} | ||
}, | ||
}, | ||
} | ||
} | ||
describe('getConnInfo', () => { | ||
it('Should info is valid', () => { | ||
const { port, server, address } = createRandomBunServer() | ||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { env: server }) | ||
const info = getConnInfo(c) | ||
|
||
expect(info.remote.port).toBe(port) | ||
expect(info.remote.address).toBe(address) | ||
expect(info.remote.addressType).toBe('IPv6') | ||
expect(info.remote.transport).toBeUndefined() | ||
}) | ||
it('Should getConnInfo works when env is { server: server }', () => { | ||
const { port, server, address } = createRandomBunServer() | ||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { env: { server } }) | ||
|
||
const info = getConnInfo(c) | ||
|
||
expect(info.remote.port).toBe(port) | ||
expect(info.remote.address).toBe(address) | ||
expect(info.remote.addressType).toBe('IPv6') | ||
expect(info.remote.transport).toBeUndefined() | ||
}) | ||
it('Should throw error when user did not give server', () => { | ||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { env: {} }) | ||
|
||
expect(() => getConnInfo(c)).toThrowError(TypeError) | ||
}) | ||
it('Should throw error when requestIP is not function', () => { | ||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { | ||
env: { | ||
requestIP: 0, | ||
}, | ||
}) | ||
expect(() => getConnInfo(c)).toThrowError(TypeError) | ||
}) | ||
}) |
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,35 @@ | ||
import type { Context } from '../..' | ||
import type { GetConnInfo } from '../../helper/conninfo' | ||
|
||
/** | ||
* Get ConnInfo with Bun | ||
* @param c Context | ||
* @returns ConnInfo | ||
*/ | ||
export const getConnInfo: GetConnInfo = (c: Context) => { | ||
const server = ('server' in c.env ? c.env.server : c.env) as | ||
| { | ||
requestIP?: (req: Request) => { | ||
address: string | ||
family: string | ||
port: number | ||
} | ||
} | ||
| undefined | ||
|
||
if (!server) { | ||
throw new TypeError('env has to include the 2nd argument of fetch.') | ||
} | ||
if (typeof server.requestIP !== 'function') { | ||
throw new TypeError('server.requestIP is not a function.') | ||
} | ||
const info = server.requestIP(c.req.raw) | ||
|
||
return { | ||
remote: { | ||
address: info.address, | ||
addressType: info.family === 'IPv6' || info.family === 'IPv4' ? info.family : 'unknown', | ||
port: info.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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { serveStatic } from './serve-static' | ||
export { bunFileSystemModule, toSSG } from './ssg' | ||
export { createBunWebSocket } from './websocket' | ||
export { getConnInfo } from './conninfo' |
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,19 @@ | ||
import { Context } from '../../context' | ||
import { HonoRequest } from '../../request' | ||
import { getConnInfo } from './conninfo' | ||
|
||
describe('getConnInfo', () => { | ||
it('Should getConnInfo works', () => { | ||
const address = Math.random().toString() | ||
const req = new Request('http://localhost/', { | ||
headers: { | ||
'cf-connecting-ip': address, | ||
}, | ||
}) | ||
const c = new Context(new HonoRequest(req)) | ||
|
||
const info = getConnInfo(c) | ||
|
||
expect(info.remote.address).toBe(address) | ||
}) | ||
}) |
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,8 @@ | ||
import type { GetConnInfo } from '../../helper/conninfo' | ||
|
||
export const getConnInfo: GetConnInfo = (c) => ({ | ||
remote: { | ||
address: c.req.header('cf-connecting-ip'), | ||
addressType: 'unknown', | ||
}, | ||
}) |
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
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,25 @@ | ||
import { Context } from '../../context' | ||
import { HonoRequest } from '../../request' | ||
import { getConnInfo } from './conninfo' | ||
|
||
describe('getConnInfo', () => { | ||
it('Should info is valid', () => { | ||
const transport = 'tcp' | ||
const address = Math.random().toString() | ||
const port = Math.floor(Math.random() * (65535 + 1)) | ||
const c = new Context(new HonoRequest(new Request('http://localhost/')), { | ||
env: { | ||
remoteAddr: { | ||
transport, | ||
hostname: address, | ||
port, | ||
}, | ||
}, | ||
}) | ||
const info = getConnInfo(c) | ||
|
||
expect(info.remote.port).toBe(port) | ||
expect(info.remote.address).toBe(address) | ||
expect(info.remote.transport).toBe(transport) | ||
}) | ||
}) |
Oops, something went wrong.