File size: 15,000 Bytes
20dbb3c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | import { createParser } from "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, 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;
Object.defineProperty(EventSource, Symbol.for("eventsource.supports-fetch-override"), {
value: !0,
writable: !1,
configurable: !1,
enumerable: !1
});
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;
}
export {
ErrorEvent,
EventSource
};
//# sourceMappingURL=index.js.map
|