* fix: use buffering to fix #1129 * chore: changeset * chore: fmtpull/1184/head
parent
2fccb8b764
commit
ccc49dd508
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/node-ws': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix a bug if upgrading process uses async function
|
|
@ -283,4 +283,27 @@ describe('WebSocket helper', () => {
|
||||||
|
|
||||||
expect(await mainPromise).toBe(true)
|
expect(await mainPromise).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('Should not async processes to create events affect message handling', async () => {
|
||||||
|
const mainPromise = new Promise<boolean>((resolve) =>
|
||||||
|
app.get(
|
||||||
|
'/',
|
||||||
|
upgradeWebSocket(async () => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||||
|
return {
|
||||||
|
onMessage() {
|
||||||
|
resolve(true)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
const ws = new WebSocket('ws://localhost:3030/')
|
||||||
|
ws.onopen = () => {
|
||||||
|
ws.send('Hello')
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(await mainPromise).toBe(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -106,6 +106,14 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
|
||||||
c.env[CONNECTION_SYMBOL_KEY] = connectionSymbol
|
c.env[CONNECTION_SYMBOL_KEY] = connectionSymbol
|
||||||
;(async () => {
|
;(async () => {
|
||||||
const ws = await nodeUpgradeWebSocket(c.env.incoming, connectionSymbol)
|
const ws = await nodeUpgradeWebSocket(c.env.incoming, connectionSymbol)
|
||||||
|
|
||||||
|
// buffer messages to handle messages received before the events are set up
|
||||||
|
const messagesReceivedInStarting: [data: WebSocket.RawData, isBinary: boolean][] = []
|
||||||
|
const bufferMessage = (data: WebSocket.RawData, isBinary: boolean) => {
|
||||||
|
messagesReceivedInStarting.push([data, isBinary])
|
||||||
|
}
|
||||||
|
ws.on('message', bufferMessage)
|
||||||
|
|
||||||
let events: WSEvents<WebSocket>
|
let events: WSEvents<WebSocket>
|
||||||
try {
|
try {
|
||||||
events = await createEvents(c)
|
events = await createEvents(c)
|
||||||
|
@ -137,7 +145,8 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
;(options?.onError ?? console.error)(e)
|
;(options?.onError ?? console.error)(e)
|
||||||
}
|
}
|
||||||
ws.on('message', (data, isBinary) => {
|
|
||||||
|
const handleMessage = (data: WebSocket.RawData, isBinary: boolean) => {
|
||||||
const datas = Array.isArray(data) ? data : [data]
|
const datas = Array.isArray(data) ? data : [data]
|
||||||
for (const data of datas) {
|
for (const data of datas) {
|
||||||
try {
|
try {
|
||||||
|
@ -155,6 +164,14 @@ export const createNodeWebSocket = (init: NodeWebSocketInit): NodeWebSocket => {
|
||||||
;(options?.onError ?? console.error)(e)
|
;(options?.onError ?? console.error)(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
ws.off('message', bufferMessage)
|
||||||
|
for (const message of messagesReceivedInStarting) {
|
||||||
|
handleMessage(...message)
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.on('message', (data, isBinary) => {
|
||||||
|
handleMessage(data, isBinary)
|
||||||
})
|
})
|
||||||
ws.on('close', (code, reason) => {
|
ws.on('close', (code, reason) => {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue