| "use strict"; |
| Object.defineProperty(exports, "__esModule", { value: !0 }); |
| var eventsourceParser = require("eventsource-parser"); |
| class ErrorEvent extends Event { |
| |
| |
| |
| |
| |
| |
| |
| constructor(type, errorEventInitDict) { |
| var _a, _b; |
| super(type), this.code = (_a = errorEventInitDict == null ? void 0 : errorEventInitDict.code) != null ? _a : void 0, this.message = (_b = errorEventInitDict == null ? void 0 : errorEventInitDict.message) != null ? _b : void 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [Symbol.for("nodejs.util.inspect.custom")](_depth, options, inspect) { |
| return inspect(inspectableError(this), options); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [Symbol.for("Deno.customInspect")](inspect, options) { |
| return inspect(inspectableError(this), options); |
| } |
| } |
| function syntaxError(message) { |
| const DomException = globalThis.DOMException; |
| return typeof DomException == "function" ? new DomException(message, "SyntaxError") : new SyntaxError(message); |
| } |
| function flattenError(err) { |
| return err instanceof Error ? "errors" in err && Array.isArray(err.errors) ? err.errors.map(flattenError).join(", ") : "cause" in err && err.cause instanceof Error ? `${err}: ${flattenError(err.cause)}` : err.message : `${err}`; |
| } |
| function inspectableError(err) { |
| return { |
| type: err.type, |
| message: err.message, |
| code: err.code, |
| defaultPrevented: err.defaultPrevented, |
| cancelable: err.cancelable, |
| timeStamp: err.timeStamp |
| }; |
| } |
| var __typeError = (msg) => { |
| throw TypeError(msg); |
| }, __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg), __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)), __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value), __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value), __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method), _readyState, _url, _redirectUrl, _withCredentials, _fetch, _reconnectInterval, _reconnectTimer, _lastEventId, _controller, _parser, _onError, _onMessage, _onOpen, _EventSource_instances, connect_fn, _onFetchResponse, _onFetchError, getRequestOptions_fn, _onEvent, _onRetryChange, failConnection_fn, scheduleReconnect_fn, _reconnect; |
| class EventSource extends EventTarget { |
| constructor(url, eventSourceInitDict) { |
| var _a, _b; |
| super(), __privateAdd(this, _EventSource_instances), this.CONNECTING = 0, this.OPEN = 1, this.CLOSED = 2, __privateAdd(this, _readyState), __privateAdd(this, _url), __privateAdd(this, _redirectUrl), __privateAdd(this, _withCredentials), __privateAdd(this, _fetch), __privateAdd(this, _reconnectInterval), __privateAdd(this, _reconnectTimer), __privateAdd(this, _lastEventId, null), __privateAdd(this, _controller), __privateAdd(this, _parser), __privateAdd(this, _onError, null), __privateAdd(this, _onMessage, null), __privateAdd(this, _onOpen, null), __privateAdd(this, _onFetchResponse, async (response) => { |
| var _a2; |
| __privateGet(this, _parser).reset(); |
| const { body, redirected, status, headers } = response; |
| if (status === 204) { |
| __privateMethod(this, _EventSource_instances, failConnection_fn).call(this, "Server sent HTTP 204, not reconnecting", 204), this.close(); |
| return; |
| } |
| if (redirected ? __privateSet(this, _redirectUrl, new URL(response.url)) : __privateSet(this, _redirectUrl, void 0), status !== 200) { |
| __privateMethod(this, _EventSource_instances, failConnection_fn).call(this, `Non-200 status code (${status})`, status); |
| return; |
| } |
| if (!(headers.get("content-type") || "").startsWith("text/event-stream")) { |
| __privateMethod(this, _EventSource_instances, failConnection_fn).call(this, 'Invalid content type, expected "text/event-stream"', status); |
| return; |
| } |
| if (__privateGet(this, _readyState) === this.CLOSED) |
| return; |
| __privateSet(this, _readyState, this.OPEN); |
| const openEvent = new Event("open"); |
| if ((_a2 = __privateGet(this, _onOpen)) == null || _a2.call(this, openEvent), this.dispatchEvent(openEvent), typeof body != "object" || !body || !("getReader" in body)) { |
| __privateMethod(this, _EventSource_instances, failConnection_fn).call(this, "Invalid response body, expected a web ReadableStream", status), this.close(); |
| return; |
| } |
| const decoder = new TextDecoder(), reader = body.getReader(); |
| let open = !0; |
| do { |
| const { done, value } = await reader.read(); |
| value && __privateGet(this, _parser).feed(decoder.decode(value, { stream: !done })), done && (open = !1, __privateGet(this, _parser).reset(), __privateMethod(this, _EventSource_instances, scheduleReconnect_fn).call(this)); |
| } while (open); |
| }), __privateAdd(this, _onFetchError, (err) => { |
| __privateSet(this, _controller, void 0), !(err.name === "AbortError" || err.type === "aborted") && __privateMethod(this, _EventSource_instances, scheduleReconnect_fn).call(this, flattenError(err)); |
| }), __privateAdd(this, _onEvent, (event) => { |
| typeof event.id == "string" && __privateSet(this, _lastEventId, event.id); |
| const messageEvent = new MessageEvent(event.event || "message", { |
| data: event.data, |
| origin: __privateGet(this, _redirectUrl) ? __privateGet(this, _redirectUrl).origin : __privateGet(this, _url).origin, |
| lastEventId: event.id || "" |
| }); |
| __privateGet(this, _onMessage) && (!event.event || event.event === "message") && __privateGet(this, _onMessage).call(this, messageEvent), this.dispatchEvent(messageEvent); |
| }), __privateAdd(this, _onRetryChange, (value) => { |
| __privateSet(this, _reconnectInterval, value); |
| }), __privateAdd(this, _reconnect, () => { |
| __privateSet(this, _reconnectTimer, void 0), __privateGet(this, _readyState) === this.CONNECTING && __privateMethod(this, _EventSource_instances, connect_fn).call(this); |
| }); |
| try { |
| if (url instanceof URL) |
| __privateSet(this, _url, url); |
| else if (typeof url == "string") |
| __privateSet(this, _url, new URL(url, getBaseURL())); |
| else |
| throw new Error("Invalid URL"); |
| } catch { |
| throw syntaxError("An invalid or illegal string was specified"); |
| } |
| __privateSet(this, _parser, eventsourceParser.createParser({ |
| onEvent: __privateGet(this, _onEvent), |
| onRetry: __privateGet(this, _onRetryChange) |
| })), __privateSet(this, _readyState, this.CONNECTING), __privateSet(this, _reconnectInterval, 3e3), __privateSet(this, _fetch, (_a = eventSourceInitDict == null ? void 0 : eventSourceInitDict.fetch) != null ? _a : globalThis.fetch), __privateSet(this, _withCredentials, (_b = eventSourceInitDict == null ? void 0 : eventSourceInitDict.withCredentials) != null ? _b : !1), __privateMethod(this, _EventSource_instances, connect_fn).call(this); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| get readyState() { |
| return __privateGet(this, _readyState); |
| } |
| |
| |
| |
| |
| |
| |
| |
| get url() { |
| return __privateGet(this, _url).href; |
| } |
| |
| |
| |
| |
| |
| get withCredentials() { |
| return __privateGet(this, _withCredentials); |
| } |
| |
| get onerror() { |
| return __privateGet(this, _onError); |
| } |
| set onerror(value) { |
| __privateSet(this, _onError, value); |
| } |
| |
| get onmessage() { |
| return __privateGet(this, _onMessage); |
| } |
| set onmessage(value) { |
| __privateSet(this, _onMessage, value); |
| } |
| |
| get onopen() { |
| return __privateGet(this, _onOpen); |
| } |
| set onopen(value) { |
| __privateSet(this, _onOpen, value); |
| } |
| addEventListener(type, listener, options) { |
| const listen = listener; |
| super.addEventListener(type, listen, options); |
| } |
| removeEventListener(type, listener, options) { |
| const listen = listener; |
| super.removeEventListener(type, listen, options); |
| } |
| |
| |
| |
| |
| |
| |
| |
| close() { |
| __privateGet(this, _reconnectTimer) && clearTimeout(__privateGet(this, _reconnectTimer)), __privateGet(this, _readyState) !== this.CLOSED && (__privateGet(this, _controller) && __privateGet(this, _controller).abort(), __privateSet(this, _readyState, this.CLOSED), __privateSet(this, _controller, void 0)); |
| } |
| } |
| _readyState = new WeakMap(), _url = new WeakMap(), _redirectUrl = new WeakMap(), _withCredentials = new WeakMap(), _fetch = new WeakMap(), _reconnectInterval = new WeakMap(), _reconnectTimer = new WeakMap(), _lastEventId = new WeakMap(), _controller = new WeakMap(), _parser = new WeakMap(), _onError = new WeakMap(), _onMessage = new WeakMap(), _onOpen = new WeakMap(), _EventSource_instances = new WeakSet(), |
| |
| |
| |
| |
| connect_fn = function() { |
| __privateSet(this, _readyState, this.CONNECTING), __privateSet(this, _controller, new AbortController()), __privateGet(this, _fetch)(__privateGet(this, _url), __privateMethod(this, _EventSource_instances, getRequestOptions_fn).call(this)).then(__privateGet(this, _onFetchResponse)).catch(__privateGet(this, _onFetchError)); |
| }, _onFetchResponse = new WeakMap(), _onFetchError = new WeakMap(), |
| |
| |
| |
| |
| |
| getRequestOptions_fn = function() { |
| var _a; |
| const init = { |
| |
| |
| mode: "cors", |
| redirect: "follow", |
| headers: { Accept: "text/event-stream", ...__privateGet(this, _lastEventId) ? { "Last-Event-ID": __privateGet(this, _lastEventId) } : void 0 }, |
| cache: "no-store", |
| signal: (_a = __privateGet(this, _controller)) == null ? void 0 : _a.signal |
| }; |
| return "window" in globalThis && (init.credentials = this.withCredentials ? "include" : "same-origin"), init; |
| }, _onEvent = new WeakMap(), _onRetryChange = new WeakMap(), |
| |
| |
| |
| |
| |
| |
| failConnection_fn = function(message, code) { |
| var _a; |
| __privateGet(this, _readyState) !== this.CLOSED && __privateSet(this, _readyState, this.CLOSED); |
| const errorEvent = new ErrorEvent("error", { code, message }); |
| (_a = __privateGet(this, _onError)) == null || _a.call(this, errorEvent), this.dispatchEvent(errorEvent); |
| }, |
| |
| |
| |
| |
| |
| |
| scheduleReconnect_fn = function(message, code) { |
| var _a; |
| if (__privateGet(this, _readyState) === this.CLOSED) |
| return; |
| __privateSet(this, _readyState, this.CONNECTING); |
| const errorEvent = new ErrorEvent("error", { code, message }); |
| (_a = __privateGet(this, _onError)) == null || _a.call(this, errorEvent), this.dispatchEvent(errorEvent), __privateSet(this, _reconnectTimer, setTimeout(__privateGet(this, _reconnect), __privateGet(this, _reconnectInterval))); |
| }, _reconnect = new WeakMap(), |
| |
| |
| |
| |
| EventSource.CONNECTING = 0, |
| |
| |
| |
| |
| EventSource.OPEN = 1, |
| |
| |
| |
| |
| EventSource.CLOSED = 2; |
| function getBaseURL() { |
| const doc = "document" in globalThis ? globalThis.document : void 0; |
| return doc && typeof doc == "object" && "baseURI" in doc && typeof doc.baseURI == "string" ? doc.baseURI : void 0; |
| } |
| exports.ErrorEvent = ErrorEvent; |
| exports.EventSource = EventSource; |
| |
|
|