eventsource
Advanced tools
Comparing version 3.0.3 to 3.0.4
@@ -37,2 +37,3 @@ /** | ||
message?: string | undefined | ||
constructor(type: string, code?: number, message?: string) | ||
} | ||
@@ -39,0 +40,0 @@ |
import { createParser } from "eventsource-parser"; | ||
class ErrorEvent extends Event { | ||
constructor(type, code, message) { | ||
super(type), this.code = code != null ? code : void 0, this.message = message != null ? message : void 0; | ||
} | ||
} | ||
@@ -8,2 +11,5 @@ function 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}`; | ||
} | ||
var __typeError = (msg) => { | ||
@@ -46,3 +52,3 @@ throw TypeError(msg); | ||
}), __privateAdd(this, _onFetchError, (err) => { | ||
__privateSet(this, _controller, void 0), !(err.name === "AbortError" || err.type === "aborted") && __privateMethod(this, _EventSource_instances, scheduleReconnect_fn).call(this); | ||
__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) => { | ||
@@ -176,2 +182,3 @@ typeof event.id == "string" && __privateSet(this, _lastEventId, event.id); | ||
* @param error - The error causing the connection to fail | ||
* @param code - The HTTP status code, if available | ||
* @internal | ||
@@ -182,10 +189,12 @@ */ | ||
__privateGet(this, _readyState) !== this.CLOSED && __privateSet(this, _readyState, this.CLOSED); | ||
const errorEvent = new ErrorEvent("error"); | ||
errorEvent.code = code, errorEvent.message = error, (_a = __privateGet(this, _onError)) == null || _a.call(this, errorEvent), this.dispatchEvent(errorEvent); | ||
const errorEvent = new ErrorEvent("error", code, error); | ||
(_a = __privateGet(this, _onError)) == null || _a.call(this, errorEvent), this.dispatchEvent(errorEvent); | ||
}, /** | ||
* Schedules a reconnection attempt against the EventSource endpoint. | ||
* | ||
* @param error - The error causing the connection to fail | ||
* @param code - The HTTP status code, if available | ||
* @internal | ||
*/ | ||
scheduleReconnect_fn = function() { | ||
scheduleReconnect_fn = function(error, code) { | ||
var _a; | ||
@@ -195,3 +204,3 @@ if (__privateGet(this, _readyState) === this.CLOSED) | ||
__privateSet(this, _readyState, this.CONNECTING); | ||
const errorEvent = new ErrorEvent("error"); | ||
const errorEvent = new ErrorEvent("error", code, error); | ||
(_a = __privateGet(this, _onError)) == null || _a.call(this, errorEvent), this.dispatchEvent(errorEvent), __privateSet(this, _reconnectTimer, setTimeout(__privateGet(this, _reconnect), __privateGet(this, _reconnectInterval))); | ||
@@ -198,0 +207,0 @@ }, _reconnect = /* @__PURE__ */ new WeakMap(), /** |
{ | ||
"name": "eventsource", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "WhatWG/W3C compliant EventSource client for Node.js and browsers", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -24,2 +24,8 @@ /** | ||
public message?: string | undefined | ||
constructor(type: string, code?: number, message?: string) { | ||
super(type) | ||
this.code = code ?? undefined | ||
this.message = message ?? undefined | ||
} | ||
} | ||
@@ -47,1 +53,25 @@ | ||
} | ||
/** | ||
* Flatten an error into a single error message string. | ||
* Unwraps nested errors and joins them with a comma. | ||
* | ||
* @param err - The error to flatten | ||
* @returns A string representation of the error | ||
* @internal | ||
*/ | ||
export function flattenError(err: unknown): string { | ||
if (!(err instanceof Error)) { | ||
return `${err}` | ||
} | ||
if ('errors' in err && Array.isArray(err.errors)) { | ||
return err.errors.map(flattenError).join(', ') | ||
} | ||
if ('cause' in err && err.cause instanceof Error) { | ||
return `${err}: ${flattenError(err.cause)}` | ||
} | ||
return err.message | ||
} |
import {createParser, type EventSourceMessage, type EventSourceParser} from 'eventsource-parser' | ||
import {ErrorEvent, syntaxError} from './errors.js' | ||
import {ErrorEvent, flattenError, syntaxError} from './errors.js' | ||
import type { | ||
@@ -441,3 +441,3 @@ AddEventListenerOptions, | ||
this.#scheduleReconnect() | ||
this.#scheduleReconnect(flattenError(err)) | ||
} | ||
@@ -518,2 +518,3 @@ | ||
* @param error - The error causing the connection to fail | ||
* @param code - The HTTP status code, if available | ||
* @internal | ||
@@ -530,4 +531,2 @@ */ | ||
// [spec] Once the user agent has failed the connection, it does not attempt to reconnect. | ||
const errorEvent = new ErrorEvent('error') | ||
// [spec] > Implementations are especially encouraged to report detailed information | ||
@@ -537,4 +536,3 @@ // [spec] > to their development consoles whenever an error event is fired, since little | ||
// Printing to console is not very programatically helpful, though, so we emit a custom event. | ||
errorEvent.code = code | ||
errorEvent.message = error | ||
const errorEvent = new ErrorEvent('error', code, error) | ||
@@ -548,5 +546,7 @@ this.#onError?.(errorEvent) | ||
* | ||
* @param error - The error causing the connection to fail | ||
* @param code - The HTTP status code, if available | ||
* @internal | ||
*/ | ||
#scheduleReconnect() { | ||
#scheduleReconnect(error?: string, code?: number) { | ||
// [spec] If the readyState attribute is set to CLOSED, abort the task. | ||
@@ -561,3 +561,3 @@ if (this.#readyState === this.CLOSED) { | ||
// [spec] Fire an event named `error` at the EventSource object. | ||
const errorEvent = new ErrorEvent('error') | ||
const errorEvent = new ErrorEvent('error', code, error) | ||
this.#onError?.(errorEvent) | ||
@@ -564,0 +564,0 @@ this.dispatchEvent(errorEvent) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
132068
1476