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

Method constructor

deps/undici/src/lib/web/eventsource/eventsource.js:107–181  ·  view source on GitHub ↗

* Creates a new EventSource object. * @param {string} url * @param {EventSourceInit} [eventSourceInitDict={}] * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface

(url, eventSourceInitDict = {})

Source from the content-addressed store, hash-verified

105 * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface
106 */
107 constructor (url, eventSourceInitDict = {}) {
108 // 1. Let ev be a new EventSource object.
109 super()
110
111 webidl.util.markAsUncloneable(this)
112
113 const prefix = 'EventSource constructor'
114 webidl.argumentLengthCheck(arguments, 1, prefix)
115
116 if (!experimentalWarned) {
117 experimentalWarned = true
118 process.emitWarning('EventSource is experimental, expect them to change at any time.', {
119 code: 'UNDICI-ES'
120 })
121 }
122
123 url = webidl.converters.USVString(url)
124 eventSourceInitDict = webidl.converters.EventSourceInitDict(eventSourceInitDict, prefix, 'eventSourceInitDict')
125
126 this.#dispatcher = eventSourceInitDict.node.dispatcher || eventSourceInitDict.dispatcher
127 this.#state = {
128 lastEventId: '',
129 reconnectionTime: eventSourceInitDict.node.reconnectionTime
130 }
131
132 // 2. Let settings be ev's relevant settings object.
133 // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
134 const settings = environmentSettingsObject
135
136 let urlRecord
137
138 try {
139 // 3. Let urlRecord be the result of encoding-parsing a URL given url, relative to settings.
140 urlRecord = new URL(url, settings.settingsObject.baseUrl)
141 this.#state.origin = urlRecord.origin
142 } catch (e) {
143 // 4. If urlRecord is failure, then throw a "SyntaxError" DOMException.
144 throw new DOMException(e, 'SyntaxError')
145 }
146
147 // 5. Set ev's url to urlRecord.
148 this.#url = urlRecord.href
149
150 // 6. Let corsAttributeState be Anonymous.
151 let corsAttributeState = ANONYMOUS
152
153 // 7. If the value of eventSourceInitDict's withCredentials member is true,
154 // then set corsAttributeState to Use Credentials and set ev's
155 // withCredentials attribute to true.
156 if (eventSourceInitDict.withCredentials === true) {
157 corsAttributeState = USE_CREDENTIALS
158 this.#withCredentials = true
159 }
160
161 // 8. Let request be the result of creating a potential-CORS request given
162 // urlRecord, the empty string, and corsAttributeState.
163 const request = createPotentialCORSRequest(urlRecord, '', corsAttributeState)
164

Callers

nothing calls this directly

Calls 6

#connectMethod · 0.95
markAsUncloneableMethod · 0.80
argumentLengthCheckMethod · 0.80
USVStringMethod · 0.80
setMethod · 0.45

Tested by

no test coverage detected