| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: !0 });
- var eventsourceParser = require("eventsource-parser");
- class ErrorEvent extends Event {
- /**
- * Constructs a new `ErrorEvent` instance. This is typically not called directly,
- * but rather emitted by the `EventSource` object when an error occurs.
- *
- * @param type - The type of the event (should be "error")
- * @param errorEventInitDict - Optional properties to include in the error 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;
- }
- /**
- * Node.js "hides" the `message` and `code` properties of the `ErrorEvent` instance,
- * when it is `console.log`'ed. This makes it harder to debug errors. To ease debugging,
- * we explicitly include the properties in the `inspect` method.
- *
- * This is automatically called by Node.js when you `console.log` an instance of this class.
- *
- * @param _depth - The current depth
- * @param options - The options passed to `util.inspect`
- * @param inspect - The inspect function to use (prevents having to import it from `util`)
- * @returns A string representation of the error
- */
- [Symbol.for("nodejs.util.inspect.custom")](_depth, options, inspect) {
- return inspect(inspectableError(this), options);
- }
- /**
- * Deno "hides" the `message` and `code` properties of the `ErrorEvent` instance,
- * when it is `console.log`'ed. This makes it harder to debug errors. To ease debugging,
- * we explicitly include the properties in the `inspect` method.
- *
- * This is automatically called by Deno when you `console.log` an instance of this class.
- *
- * @param inspect - The inspect function to use (prevents having to import it from `util`)
- * @param options - The options passed to `Deno.inspect`
- * @returns A string representation of the error
- */
- [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);
- }
- /**
- * Returns the state of this EventSource object's connection. It can have the values described below.
- *
- * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState)
- *
- * Note: typed as `number` instead of `0 | 1 | 2` for compatibility with the `EventSource` interface,
- * defined in the TypeScript `dom` library.
- *
- * @public
- */
- get readyState() {
- return __privateGet(this, _readyState);
- }
- /**
- * Returns the URL providing the event stream.
- *
- * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url)
- *
- * @public
- */
- get url() {
- return __privateGet(this, _url).href;
- }
- /**
- * Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.
- *
- * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials)
- */
- get withCredentials() {
- return __privateGet(this, _withCredentials);
- }
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */
- get onerror() {
- return __privateGet(this, _onError);
- }
- set onerror(value) {
- __privateSet(this, _onError, value);
- }
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */
- get onmessage() {
- return __privateGet(this, _onMessage);
- }
- set onmessage(value) {
- __privateSet(this, _onMessage, value);
- }
- /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */
- 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);
- }
- /**
- * Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.
- *
- * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close)
- *
- * @public
- */
- 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 = /* @__PURE__ */ new WeakMap(), _url = /* @__PURE__ */ new WeakMap(), _redirectUrl = /* @__PURE__ */ new WeakMap(), _withCredentials = /* @__PURE__ */ new WeakMap(), _fetch = /* @__PURE__ */ new WeakMap(), _reconnectInterval = /* @__PURE__ */ new WeakMap(), _reconnectTimer = /* @__PURE__ */ new WeakMap(), _lastEventId = /* @__PURE__ */ new WeakMap(), _controller = /* @__PURE__ */ new WeakMap(), _parser = /* @__PURE__ */ new WeakMap(), _onError = /* @__PURE__ */ new WeakMap(), _onMessage = /* @__PURE__ */ new WeakMap(), _onOpen = /* @__PURE__ */ new WeakMap(), _EventSource_instances = /* @__PURE__ */ new WeakSet(), /**
- * Connect to the given URL and start receiving events
- *
- * @internal
- */
- 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 = /* @__PURE__ */ new WeakMap(), _onFetchError = /* @__PURE__ */ new WeakMap(), /**
- * Get request options for the `fetch()` request
- *
- * @returns The request options
- * @internal
- */
- getRequestOptions_fn = function() {
- var _a;
- const init = {
- // [spec] Let `corsAttributeState` be `Anonymous`…
- // [spec] …will have their mode set to "cors"…
- 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 = /* @__PURE__ */ new WeakMap(), _onRetryChange = /* @__PURE__ */ new WeakMap(), /**
- * Handles the process referred to in the EventSource specification as "failing a connection".
- *
- * @param error - The error causing the connection to fail
- * @param code - The HTTP status code, if available
- * @internal
- */
- 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);
- }, /**
- * Schedules a reconnection attempt against the EventSource endpoint.
- *
- * @param message - The error causing the connection to fail
- * @param code - The HTTP status code, if available
- * @internal
- */
- 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 = /* @__PURE__ */ new WeakMap(), /**
- * ReadyState representing an EventSource currently trying to connect
- *
- * @public
- */
- EventSource.CONNECTING = 0, /**
- * ReadyState representing an EventSource connection that is open (eg connected)
- *
- * @public
- */
- EventSource.OPEN = 1, /**
- * ReadyState representing an EventSource connection that is closed (eg disconnected)
- *
- * @public
- */
- 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;
- //# sourceMappingURL=index.cjs.map
|