v5.1.0
Minor Changes
-
#355
d8370e4Thanks @rexxars! - Typed response body chunks as buffers instead ofunknownReaderLiketyped chunks read off the response body asunknown, but they are handed to aTextDecoder, which only accepts buffers and throws on anything else. So a reader yielding anything else was never usable, and the type said otherwise. It also hid a type error in the client itself, which only surfaces when compiling against TypeScript'sdomlibrary: the node typings resolve the chunk toany, while thedomlibrary resolves it to{}.Chunks are now typed as
Uint8Array | DataView | ArrayBuffer, which is what both the node and DOM typings accept, and which everyfetch()implementation yields. Nothing changes at runtime.If you pass a custom
fetch()that returns a hand-rolled body, and your reader's chunk type is wider than the above, egunknownorany, it will no longer be assignable. Returning a realResponse, or a reader that yieldsUint8Arraychunks, is unaffected. -
#355
008f07eThanks @rexxars! - Declared thethistype for theonerror,onmessageandonopenpropertiesaddEventListener()already declared that listeners are called with the EventSource instance asthis, but theon*properties did not, sothiswas an implicitanyin handlers assigned to them (an error undernoImplicitThis). They now matchaddEventListener()and the nativeEventSource:eventSource.onmessage = function (event) { console.log(this.url, event.data) // `this` is now typed }
Handlers that declare an incompatible
this, eg an unbound class method typed withthis: MyClass, will now be rejected where they were previously accepted. Arrow functions and handlers that ignorethisare unaffected. -
#355
3512addThanks @rexxars! - Made the exportedEventSourcetype structural, so other implementations can satisfy itEventSourcewas exported as a class holding hard-private (#) fields, which makes TypeScript emit a#privatebrand into the declaration file and turns the exported type nominal. A consumer writingfunction connect(es: EventSource)against this package could not pass the nativeEventSource, a mock, or any other implementation, even when the shape matched exactly.The implementation class is now internal, and
EventSourceis exported as aninterfaceplus a const holding the constructor. As a result:- The native
EventSource, along with mocks and stubs, is assignable to the exportedEventSourcetype - The exported value and
globalThis.EventSourceare interchangeable in both directions, which helps libraries that accept an EventSource implementation to construct - A new
EventSourceConstructortype is exported for that case
Nothing changes at runtime:
new EventSource(...),instanceof, subclassing, the readyState statics,EventSource.name, inspected output and theeventsource.supports-fetch-overridesymbol all behave as before, and the internal state is still held in real#privatefields. - The native
Patch Changes
-
#357
8e5c691Thanks @rexxars! - FixedoriginandlastEventIdbeing empty on Cloudflare Workersworkerd accepts
datafrom theMessageEventconstructor's init dictionary but silently dropsoriginandlastEventId, so message events dispatched on Cloudflare Workers arrived withoriginset tonullandlastEventIdto an empty string. Both are now assigned explicitly when the constructor did not take them, which leaves every other runtime untouched.