MCPcopy Create free account
hub / github.com/nodejs/node / #onMessage

Method #onMessage

deps/undici/src/lib/web/websocket/websocket.js:526–565  ·  view source on GitHub ↗
(type, data)

Source from the content-addressed store, hash-verified

524 }
525
526 #onMessage (type, data) {
527 // 1. If ready state is not OPEN (1), then return.
528 if (this.#handler.readyState !== states.OPEN) {
529 return
530 }
531
532 // 2. Let dataForEvent be determined by switching on type and binary type:
533 let dataForEvent
534
535 if (type === opcodes.TEXT) {
536 // -> type indicates that the data is Text
537 // a new DOMString containing data
538 try {
539 dataForEvent = utf8Decode(data)
540 } catch {
541 failWebsocketConnection(this.#handler, 1007, 'Received invalid UTF-8 in text frame.')
542 return
543 }
544 } else if (type === opcodes.BINARY) {
545 if (this.#binaryType === 'blob') {
546 // -> type indicates that the data is Binary and binary type is "blob"
547 // a new Blob object, created in the relevant Realm of the WebSocket
548 // object, that represents data as its raw data
549 dataForEvent = new Blob([data])
550 } else {
551 // -> type indicates that the data is Binary and binary type is "arraybuffer"
552 // a new ArrayBuffer object, created in the relevant Realm of the
553 // WebSocket object, whose contents are data
554 dataForEvent = toArrayBuffer(data)
555 }
556 }
557
558 // 3. Fire an event named message at the WebSocket object, using MessageEvent,
559 // with the origin attribute initialized to the serialization of the WebSocket
560 // object’s url's origin, and the data attribute initialized to dataForEvent.
561 fireEvent('message', this, createFastMessageEvent, {
562 origin: this.#url.origin,
563 data: dataForEvent
564 })
565 }
566
567 #onParserDrain () {
568 this.#handler.socket.resume()

Callers 1

WebSocketClass · 0.95

Calls 3

failWebsocketConnectionFunction · 0.70
toArrayBufferFunction · 0.70
fireEventFunction · 0.70

Tested by

no test coverage detected