MCPcopy Create free account
hub / github.com/nodejs/undici / constructor

Method constructor

lib/web/websocket/websocket.js:133–199  ·  view source on GitHub ↗

* @param {string} url * @param {string|string[]} protocols

(url, protocols = [])

Source from the content-addressed store, hash-verified

131 * @param {string|string[]} protocols
132 */
133 constructor (url, protocols = []) {
134 super()
135
136 webidl.util.markAsUncloneable(this)
137
138 const prefix = 'WebSocket constructor'
139 webidl.argumentLengthCheck(arguments, 1, prefix)
140
141 const options = webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'](protocols, prefix, 'options')
142
143 url = webidl.converters.USVString(url)
144 protocols = options.protocols
145
146 // 1. Let baseURL be this's relevant settings object's API base URL.
147 const baseURL = environmentSettingsObject.settingsObject.baseUrl
148
149 // 2. Let urlRecord be the result of getting a URL record given url and baseURL.
150 const urlRecord = getURLRecord(url, baseURL)
151
152 // 3. If protocols is a string, set protocols to a sequence consisting
153 // of just that string.
154 if (typeof protocols === 'string') {
155 protocols = [protocols]
156 }
157
158 // 4. If any of the values in protocols occur more than once or otherwise
159 // fail to match the requirements for elements that comprise the value
160 // of `Sec-WebSocket-Protocol` fields as defined by The WebSocket
161 // protocol, then throw a "SyntaxError" DOMException.
162 if (protocols.length !== new Set(protocols.map(p => p.toLowerCase())).size) {
163 throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
164 }
165
166 if (protocols.length > 0 && !protocols.every(p => isValidSubprotocol(p))) {
167 throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
168 }
169
170 // 5. Set this's url to urlRecord.
171 this.#url = new URL(urlRecord.href)
172
173 // 6. Let client be this's relevant settings object.
174 const client = environmentSettingsObject.settingsObject
175
176 // 7. Run this step in parallel:
177 // 7.1. Establish a WebSocket connection given urlRecord, protocols,
178 // and client.
179 this.#handler.controller = establishWebSocketConnection(
180 urlRecord,
181 protocols,
182 client,
183 this.#handler,
184 options
185 )
186
187 // Each WebSocket object has an associated ready state, which is a
188 // number representing the state of the connection. Initially it must
189 // be CONNECTING (0).
190 this.#handler.readyState = WebSocket.CONNECTING

Callers

nothing calls this directly

Calls 8

getURLRecordFunction · 0.85
isValidSubprotocolFunction · 0.85
markAsUncloneableMethod · 0.80
argumentLengthCheckMethod · 0.80
USVStringMethod · 0.80
mapMethod · 0.80
everyMethod · 0.80

Tested by

no test coverage detected