MCPcopy
hub / github.com/nodejs/undici / constructor

Method constructor

lib/web/websocket/stream/websocketstream.js:79–167  ·  view source on GitHub ↗
(url, options = undefined)

Source from the content-addressed store, hash-verified

77 #parser
78
79 constructor (url, options = undefined) {
80 if (!emittedExperimentalWarning) {
81 process.emitWarning('WebSocketStream is experimental! Expect it to change at any time.', {
82 code: 'UNDICI-WSS'
83 })
84 emittedExperimentalWarning = true
85 }
86
87 webidl.argumentLengthCheck(arguments, 1, 'WebSocket')
88
89 url = webidl.converters.USVString(url)
90 if (options !== null) {
91 options = webidl.converters.WebSocketStreamOptions(options)
92 }
93
94 // 1. Let baseURL be this 's relevant settings object 's API base URL .
95 const baseURL = environmentSettingsObject.settingsObject.baseUrl
96
97 // 2. Let urlRecord be the result of getting a URL record given url and baseURL .
98 const urlRecord = getURLRecord(url, baseURL)
99
100 // 3. Let protocols be options [" protocols "] if it exists , otherwise an empty sequence.
101 const protocols = options.protocols
102
103 // 4. If any of the values in protocols occur more than once or otherwise fail to match the requirements for elements that comprise the value of ` Sec-WebSocket-Protocol ` fields as defined by The WebSocket Protocol , then throw a " SyntaxError " DOMException . [WSP]
104 if (protocols.length !== new Set(protocols.map(p => p.toLowerCase())).size) {
105 throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
106 }
107
108 if (protocols.length > 0 && !protocols.every(p => isValidSubprotocol(p))) {
109 throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
110 }
111
112 // 5. Set this 's url to urlRecord .
113 this.#url = urlRecord.toString()
114
115 // 6. Set this 's opened promise and closed promise to new promises.
116 this.#openedPromise = Promise.withResolvers()
117 this.#closedPromise = Promise.withResolvers()
118
119 // 7. Apply backpressure to the WebSocket.
120 // TODO
121
122 // 8. If options [" signal "] exists ,
123 if (options.signal != null) {
124 // 8.1. Let signal be options [" signal "].
125 const signal = options.signal
126
127 // 8.2. If signal is aborted , then reject this 's opened promise and closed promise with signal ’s abort reason
128 // and return.
129 if (signal.aborted) {
130 this.#openedPromise.reject(signal.reason)
131 this.#closedPromise.reject(signal.reason)
132 return
133 }
134
135 // 8.3. Add the following abort steps to signal :
136 addAbortListener(signal, () => {

Callers

nothing calls this directly

Calls 11

getURLRecordFunction · 0.85
isValidSubprotocolFunction · 0.85
addAbortListenerFunction · 0.85
isEstablishedFunction · 0.85
failWebsocketConnectionFunction · 0.85
argumentLengthCheckMethod · 0.80
USVStringMethod · 0.80
mapMethod · 0.80
everyMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected