Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/modern-clubs-dig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/node-ws': major
---

Environment variable support
22 changes: 20 additions & 2 deletions packages/node-ws/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { ServerType } from '@hono/node-server/dist/types'
import { Hono } from 'hono'
import { Env, Hono } from 'hono'
import { cors } from 'hono/cors'
import { HTTPException } from 'hono/http-exception'
import type { WSMessageReceive } from 'hono/ws'
Expand All @@ -15,10 +15,12 @@
let injectWebSocket: ReturnType<typeof createNodeWebSocket>['injectWebSocket']
let upgradeWebSocket: ReturnType<typeof createNodeWebSocket>['upgradeWebSocket']
let wss: ReturnType<typeof createNodeWebSocket>['wss']
let env: Env["Bindings"]

beforeEach(async () => {
app = new Hono()
;({ injectWebSocket, upgradeWebSocket, wss } = createNodeWebSocket({ app }))
env = { customVar: '1' } as Env["Bindings"]
({ injectWebSocket, upgradeWebSocket, wss } = createNodeWebSocket({ app, env }))

server = await new Promise<ServerType>((resolve) => {
const server = serve({ fetch: app.fetch, port: 3030 }, () => {
Expand All @@ -26,7 +28,7 @@
})
})
injectWebSocket(server)
})

Check failure on line 31 in packages/node-ws/src/index.test.ts

View workflow job for this annotation

GitHub Actions / autofix

Unsafe argument of type error typed assigned to a parameter of type `Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse> | Http2SecureServer<...>`

afterEach(() => {
server.close()
Expand Down Expand Up @@ -329,4 +331,20 @@
})
)
})

it('Should server can obtain environment variables', async () => {
const mainPromise = new Promise<string>((resolve) =>
app.get(
'/',
(c, next) => {
let ws = upgradeWebSocket((c) => ({}))

Check failure on line 340 in packages/node-ws/src/index.test.ts

View workflow job for this annotation

GitHub Actions / build

'c' is declared but its value is never read.
resolve(c.env.customVar)

Check failure on line 341 in packages/node-ws/src/index.test.ts

View workflow job for this annotation

GitHub Actions / build

'c.env' is of type 'unknown'.
return ws(c, next)

Check failure on line 342 in packages/node-ws/src/index.test.ts

View workflow job for this annotation

GitHub Actions / autofix

Unsafe argument of type error typed assigned to a parameter of type `string | PromiseLike<string>`
}
)
)

new WebSocket('ws://localhost:3030/')
expect(await mainPromise).toBe('1')
})
})
4 changes: 3 additions & 1 deletion packages/node-ws/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Hono } from 'hono'
import type { Hono, Env } from 'hono'
import { defineWebSocketHelper } from 'hono/ws'
import type { UpgradeWebSocket, WSContext } from 'hono/ws'
import type { WebSocket } from 'ws'
Expand All @@ -23,6 +23,7 @@ export interface NodeWebSocketInit {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
app: Hono<any, any, any>
baseUrl?: string | URL
env?: Env["Bindings"]
}

const generateConnectionSymbol = () => Symbol('connection')
Expand Down Expand Up @@ -77,6 +78,7 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
} = {
incoming: request,
outgoing: undefined,
...init.env
}
const response = await init.app.request(url, { headers: headers }, env)
const waiter = waiterMap.get(request)
Expand Down
Loading