Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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/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
2 changes: 1 addition & 1 deletion packages/node-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
"engines": {
"node": ">=18.14.1"
}
}
}
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
2 changes: 1 addition & 1 deletion packages/typebox-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"jest": "^29.7.0",
"rimraf": "^5.0.5"
}
}
}