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): CloseEvent is not defined #648

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 32 additions & 0 deletions packages/node-ws/src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
interface CloseEventInit extends EventInit {
code?: number;
reason?: string;
wasClean?: boolean;
}

/**
* @link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
*/
export class CloseEvent extends Event {
Yovach marked this conversation as resolved.
Show resolved Hide resolved
#eventInitDict

constructor(
type: string,
eventInitDict: CloseEventInit = {}
) {
super(type, eventInitDict)
this.#eventInitDict = eventInitDict
}

get wasClean(): boolean {
return this.#eventInitDict.wasClean ?? false
}

get code(): number {
return this.#eventInitDict.code ?? 0
}

get reason(): string {
return this.#eventInitDict.reason ?? ''
}
}
18 changes: 17 additions & 1 deletion packages/node-ws/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('WebSocket helper', () => {

beforeEach(async () => {
app = new Hono()

;({ injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app }))

server = await new Promise<ServerType>((resolve) => {
Expand Down Expand Up @@ -108,6 +109,21 @@ describe('WebSocket helper', () => {
connections.forEach((ws) => ws.close())
})

it('CloseEvent should be executed without crash', async () => {
app.get(
'/',
upgradeWebSocket(() => ({
onClose() {
// doing some stuff here
},
}))
)

const ws = new WebSocket('ws://localhost:3030/')
await new Promise<void>((resolve) => ws.on('open', resolve))
ws.close()
})

it('Should be able to send and receive binary content with good length', async () => {
const mainPromise = new Promise<WSMessageReceive>((resolve) =>
app.get(
Expand All @@ -126,7 +142,7 @@ describe('WebSocket helper', () => {
await new Promise<void>((resolve) => ws.on('open', resolve))
ws.send(binaryData)

const receivedMessage = await mainPromise;
const receivedMessage = await mainPromise
expect(receivedMessage).toBeInstanceOf(Buffer)
expect((receivedMessage as Buffer).byteLength).toBe(binaryData.length)

Expand Down
2 changes: 1 addition & 1 deletion packages/node-ws/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Buffer } from 'buffer'
import type { Server } from 'node:http'
import type { Http2SecureServer, Http2Server } from 'node:http2'
import type { Hono } from 'hono'
import type { UpgradeWebSocket, WSContext } from 'hono/ws'
import type { WebSocket } from 'ws'
import { WebSocketServer } from 'ws'
import type { IncomingMessage } from 'http'
import { CloseEvent } from './events'

export interface NodeWebSocket {
upgradeWebSocket: UpgradeWebSocket
Expand Down