Skip to content

Commit

Permalink
fix(node-ws): ws wasn't created when upgrade process is async (#959)
Browse files Browse the repository at this point in the history
* fix(node-ws): ws wasn't created when upgrade process is async

* chore: add changeset

* add test

* Update big-pillows-shave.md
  • Loading branch information
nakasyou authored Feb 10, 2025
1 parent 4f927df commit c24efa6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-pillows-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/node-ws': patch
---

fix a bug of upgrading
26 changes: 26 additions & 0 deletions packages/node-ws/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/node-ws/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion packages/node-ws/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
"outDir": "./dist",
"types": [
"vitest/globals"
]
},
"include": [
"src/**/*.ts"
Expand Down
8 changes: 5 additions & 3 deletions packages/oidc-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c24efa6

Please sign in to comment.