fix(node-ws): ws wasn't created when upgrade process is async (#959)

* fix(node-ws): ws wasn't created when upgrade process is async

* chore: add changeset

* add test

* Update big-pillows-shave.md
pull/966/head
Shotaro Nakamura 2025-02-10 18:10:03 +09:00 committed by GitHub
parent 4f927dfaa8
commit c24efa6b8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 5 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/node-ws': patch
---
fix a bug of upgrading

View File

@ -25,6 +25,32 @@ describe('WebSocket helper', () => {
server.close()
})
it('Should be inited WebSocket Context even if upgrading process is asynchronous', async () => {
const mainPromise = new Promise<boolean>((resolve) =>
app.get(
'/',
upgradeWebSocket(
() =>
new Promise((resolveWS) =>
setTimeout(
() =>
resolveWS({
onOpen() {
resolve(true)
},
}),
100
)
)
)
)
)
new WebSocket('ws://localhost:3030/')
expect(await mainPromise).toBe(true)
})
it('Should be able to connect', async () => {
const mainPromise = new Promise<boolean>((resolve) =>
app.get(

View File

@ -71,8 +71,8 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
}
;(async () => {
const events = await createEvents(c)
const ws = await nodeUpgradeWebSocket(c.env.incoming)
const events = await createEvents(c)
const ctx: WSContext<WebSocket> = {
binaryType: 'arraybuffer',

View File

@ -2,7 +2,10 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
"outDir": "./dist",
"types": [
"vitest/globals"
]
},
"include": [
"src/**/*.ts"

View File

@ -89,7 +89,9 @@ const getOidcAuthEnv = (c: Context) => {
try {
new URL(oidcAuthEnv.OIDC_REDIRECT_URI)
} catch (e) {
throw new HTTPException(500, { message: 'The OIDC redirect URI is invalid. It must be a full URL or an absolute path' })
throw new HTTPException(500, {
message: 'The OIDC redirect URI is invalid. It must be a full URL or an absolute path',
})
}
}
oidcAuthEnv.OIDC_COOKIE_PATH = oidcAuthEnv.OIDC_COOKIE_PATH ?? defaultOidcAuthCookiePath
@ -149,7 +151,7 @@ export const getAuth = async (c: Context): Promise<OidcAuth | null> => {
return null
}
try {
auth = await verify(session_jwt, env.OIDC_AUTH_SECRET) as OidcAuth
auth = (await verify(session_jwt, env.OIDC_AUTH_SECRET)) as OidcAuth
} catch (e) {
deleteCookie(c, env.OIDC_COOKIE_NAME, { path: env.OIDC_COOKIE_PATH })
return null
@ -239,7 +241,7 @@ export const revokeSession = async (c: Context): Promise<void> => {
const session_jwt = getCookie(c, env.OIDC_COOKIE_NAME)
if (session_jwt !== undefined) {
deleteCookie(c, env.OIDC_COOKIE_NAME, { path: env.OIDC_COOKIE_PATH })
const auth = await verify(session_jwt, env.OIDC_AUTH_SECRET) as OidcAuth
const auth = (await verify(session_jwt, env.OIDC_AUTH_SECRET)) as OidcAuth
if (auth.rtk !== undefined && auth.rtk !== '') {
// revoke refresh token
const as = await getAuthorizationServer(c)