|
2 | 2 |
|
3 | 3 | const { kReadyState, kController, kResponse, kBinaryType, kWebSocketURL } = require('./symbols') |
4 | 4 | const { states, opcodes } = require('./constants') |
5 | | -const { MessageEvent, ErrorEvent } = require('./events') |
| 5 | +const { ErrorEvent, createFastMessageEvent } = require('./events') |
6 | 6 | const { isUtf8 } = require('node:buffer') |
7 | 7 |
|
8 | 8 | /* globals Blob */ |
@@ -51,15 +51,16 @@ function isClosed (ws) { |
51 | 51 | * @see https://dom.spec.whatwg.org/#concept-event-fire |
52 | 52 | * @param {string} e |
53 | 53 | * @param {EventTarget} target |
| 54 | + * @param {(...args: ConstructorParameters<typeof Event>) => Event} eventFactory |
54 | 55 | * @param {EventInit | undefined} eventInitDict |
55 | 56 | */ |
56 | | -function fireEvent (e, target, eventConstructor = Event, eventInitDict = {}) { |
| 57 | +function fireEvent (e, target, eventFactory = (type, init) => new Event(type, init), eventInitDict = {}) { |
57 | 58 | // 1. If eventConstructor is not given, then let eventConstructor be Event. |
58 | 59 |
|
59 | 60 | // 2. Let event be the result of creating an event given eventConstructor, |
60 | 61 | // in the relevant realm of target. |
61 | 62 | // 3. Initialize event’s type attribute to e. |
62 | | - const event = new eventConstructor(e, eventInitDict) // eslint-disable-line new-cap |
| 63 | + const event = eventFactory(e, eventInitDict) |
63 | 64 |
|
64 | 65 | // 4. Initialize any other IDL attributes of event as described in the |
65 | 66 | // invocation of this algorithm. |
@@ -110,7 +111,7 @@ function websocketMessageReceived (ws, type, data) { |
110 | 111 | // 3. Fire an event named message at the WebSocket object, using MessageEvent, |
111 | 112 | // with the origin attribute initialized to the serialization of the WebSocket |
112 | 113 | // object’s url's origin, and the data attribute initialized to dataForEvent. |
113 | | - fireEvent('message', ws, MessageEvent, { |
| 114 | + fireEvent('message', ws, createFastMessageEvent, { |
114 | 115 | origin: ws[kWebSocketURL].origin, |
115 | 116 | data: dataForEvent |
116 | 117 | }) |
@@ -195,7 +196,7 @@ function failWebsocketConnection (ws, reason) { |
195 | 196 |
|
196 | 197 | if (reason) { |
197 | 198 | // TODO: process.nextTick |
198 | | - fireEvent('error', ws, ErrorEvent, { |
| 199 | + fireEvent('error', ws, (type, init) => new ErrorEvent(type, init), { |
199 | 200 | error: new Error(reason) |
200 | 201 | }) |
201 | 202 | } |
|
0 commit comments