-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node-ws):
CloseEvent
is not defined (#648)
* feat: add CloseEvent class as it doesn't exist for some versions of Node.js * chore(style): remove semicolon * test: add promise to check if "onClose" doesn't crashes * chore(style): apply eslint rules * ref: use `globalThis.CloseEvent` whenever possible or fallback to custom CloseEvent class * chore: add changeset
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/node-ws': patch | ||
--- | ||
|
||
Add a `CloseEvent` class to avoid exception "CloseEvent is not defined" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 const CloseEvent = globalThis.CloseEvent ?? class extends Event { | ||
#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 ?? '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters