Skip to content

Commit

Permalink
feat(bun): WebSocket helper supports that env be { server: server } (
Browse files Browse the repository at this point in the history
…#2812)

* feat: WebSocket helper supports `{ server }`

* test: add test

* chore: format code

* fix: test

* chore: format code

* fix: format code with new rule

* chore: format code

Fixes #2645
Fixes #2696
  • Loading branch information
nakasyou authored May 28, 2024
1 parent a3d9cc7 commit 3d0fbfb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
11 changes: 2 additions & 9 deletions src/adapter/bun/conninfo.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import type { Context } from '../..'
import type { GetConnInfo } from '../../helper/conninfo'
import { getBunServer } from './server'

/**
* 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
const server = getBunServer(c)

if (!server) {
throw new TypeError('env has to include the 2nd argument of fetch.')
Expand Down
19 changes: 19 additions & 0 deletions src/adapter/bun/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Context } from '../../context'
import { HonoRequest } from '../../request'
import { getBunServer } from './server'
import type { BunServer } from './server'

describe('getBunServer', () => {
it('Should success to pick Server', () => {
const server = {} as BunServer

expect(
getBunServer(new Context(new HonoRequest(new Request('http://localhost/')), { env: server }))
).toBe(server)
expect(
getBunServer(
new Context(new HonoRequest(new Request('http://localhost/')), { env: { server } })
)
).toBe(server)
})
})
30 changes: 30 additions & 0 deletions src/adapter/bun/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Getting Bun Server Object for Bun adapters
* @module
*/
import type { Context } from '../../context'

/**
* Bun Server Object
*/
export interface BunServer {
requestIP?: (req: Request) => {
address: string
family: string
port: number
}
upgrade<T>(
req: Request,
options?: {
data: T
}
): boolean
}

/**
* Get Bun Server Object from Context
* @param c Context
* @returns Bun Server
*/
export const getBunServer = (c: Context): BunServer | undefined =>
('server' in c.env ? c.env.server : c.env) as BunServer | undefined
15 changes: 6 additions & 9 deletions src/adapter/bun/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import type {
WSEvents,
WSMessageReceive,
} from '../../helper/websocket'
import { getBunServer } from './server'

interface BunServerWebSocket<T> {
send(data: string | ArrayBufferLike, compress?: boolean): void
close(code?: number, reason?: string): void
data: T
readyState: 0 | 1 | 2 | 3
}
interface BunServer {
upgrade<T>(
req: Request,
options?: {
data: T
}
): boolean
}

interface BunWebSocketHandler<T> {
open(ws: BunServerWebSocket<T>): void
close(ws: BunServerWebSocket<T>, code?: number, reason?: string): void
Expand Down Expand Up @@ -60,7 +54,10 @@ export const createBunWebSocket: CreateWebSocket = () => {

const upgradeWebSocket: UpgradeWebSocket = (createEvents) => {
return async (c, next) => {
const server = c.env as BunServer
const server = getBunServer(c)
if (!server) {
throw new TypeError('env has to include the 2nd argument of fetch.')
}
const connId = websocketConns.push(await createEvents(c)) - 1
const upgradeResult = server.upgrade<BunWebSocketData>(c.req.raw, {
data: {
Expand Down

0 comments on commit 3d0fbfb

Please sign in to comment.