From c24efa6b8af08d0d0b3315b7b5b7355b5dd7ff5a Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura <79000684+nakasyou@users.noreply.github.com> Date: Mon, 10 Feb 2025 18:10:03 +0900 Subject: [PATCH] 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 --- .changeset/big-pillows-shave.md | 5 +++++ packages/node-ws/src/index.test.ts | 26 ++++++++++++++++++++++++++ packages/node-ws/src/index.ts | 2 +- packages/node-ws/tsconfig.json | 5 ++++- packages/oidc-auth/src/index.ts | 8 +++++--- 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 .changeset/big-pillows-shave.md diff --git a/.changeset/big-pillows-shave.md b/.changeset/big-pillows-shave.md new file mode 100644 index 00000000..a6cc80a6 --- /dev/null +++ b/.changeset/big-pillows-shave.md @@ -0,0 +1,5 @@ +--- +'@hono/node-ws': patch +--- + +fix a bug of upgrading diff --git a/packages/node-ws/src/index.test.ts b/packages/node-ws/src/index.test.ts index b991fcb2..d7da9eea 100644 --- a/packages/node-ws/src/index.test.ts +++ b/packages/node-ws/src/index.test.ts @@ -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((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((resolve) => app.get( diff --git a/packages/node-ws/src/index.ts b/packages/node-ws/src/index.ts index df197b2d..ef1ed60d 100644 --- a/packages/node-ws/src/index.ts +++ b/packages/node-ws/src/index.ts @@ -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 = { binaryType: 'arraybuffer', diff --git a/packages/node-ws/tsconfig.json b/packages/node-ws/tsconfig.json index 6a9d7cc4..c4bdc510 100644 --- a/packages/node-ws/tsconfig.json +++ b/packages/node-ws/tsconfig.json @@ -2,7 +2,10 @@ "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": "./src", - "outDir": "./dist" + "outDir": "./dist", + "types": [ + "vitest/globals" + ] }, "include": [ "src/**/*.ts" diff --git a/packages/oidc-auth/src/index.ts b/packages/oidc-auth/src/index.ts index ba4566ca..8b1c714c 100644 --- a/packages/oidc-auth/src/index.ts +++ b/packages/oidc-auth/src/index.ts @@ -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 => { 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 => { 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)