@rayjp2010/fetch-event-source 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Microsoft Corporation.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,215 @@
1
+ # Fetch Event Source
2
+ This package provides a better API for making [Event Source requests](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) - also known as server-sent events - with all the features available in the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
3
+
4
+ The [default browser EventSource API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) imposes several restrictions on the type of request you're allowed to make: the [only parameters](https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource#Parameters) you're allowed to pass in are the `url` and `withCredentials`, so:
5
+ * You cannot pass in a request body: you have to encode all the information necessary to execute the request inside the URL, which is [limited to 2000 characters](https://stackoverflow.com/questions/417142) in most browsers.
6
+ * You cannot pass in custom request headers
7
+ * You can only make GET requests - there is no way to specify another method.
8
+ * If the connection is cut, you don't have any control over the retry strategy: the browser will silently retry for you a few times and then stop, which is not good enough for any sort of robust application.
9
+
10
+ This library provides an alternate interface for consuming server-sent events, based on the Fetch API. It is fully compatible with the [Event Stream format](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format), so if you already have a server emitting these events, you can consume it just like before. However, you now have greater control over the request and response so:
11
+
12
+ * You can use any request method/headers/body, plus all the other functionality exposed by fetch(). You can even provide an alternate fetch() implementation, if the default browser implementation doesn't work for you.
13
+ * You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.
14
+ * If the connection gets cut or an error occurs, you have full control over the retry strategy.
15
+
16
+ In addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)
17
+
18
+ # Install
19
+ ```sh
20
+ npm install @rayjp2010/fetch-event-source
21
+ ```
22
+
23
+ # Usage
24
+ ```ts
25
+ // BEFORE:
26
+ const sse = new EventSource('/api/sse');
27
+ sse.onmessage = (ev) => {
28
+ console.log(ev.data);
29
+ };
30
+
31
+ // AFTER:
32
+ import { fetchEventSource } from '@rayjp2010/fetch-event-source';
33
+
34
+ await fetchEventSource('/api/sse', {
35
+ onmessage(ev) {
36
+ console.log(ev.data);
37
+ }
38
+ });
39
+ ```
40
+
41
+ You can pass in all the [other parameters](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) exposed by the default fetch API, for example:
42
+ ```ts
43
+ const ctrl = new AbortController();
44
+ fetchEventSource('/api/sse', {
45
+ method: 'POST',
46
+ headers: {
47
+ 'Content-Type': 'application/json',
48
+ },
49
+ body: JSON.stringify({
50
+ foo: 'bar'
51
+ }),
52
+ signal: ctrl.signal,
53
+ });
54
+ ```
55
+
56
+ You can use a function for `headers` to get fresh values on each request/retry, useful for refreshing auth tokens:
57
+ ```ts
58
+ fetchEventSource('/api/sse', {
59
+ headers: () => ({
60
+ Authorization: `Bearer ${getLatestToken()}`,
61
+ }),
62
+ onmessage(msg) {
63
+ console.log(msg.data);
64
+ }
65
+ });
66
+ ```
67
+
68
+ You can add better error handling, for example:
69
+ ```ts
70
+ class RetriableError extends Error { }
71
+ class FatalError extends Error { }
72
+
73
+ fetchEventSource('/api/sse', {
74
+ async onopen(response) {
75
+ if (response.ok && response.headers.get('content-type') === EventStreamContentType) {
76
+ return; // everything's good
77
+ } else if (response.status >= 400 && response.status < 500 && response.status !== 429) {
78
+ // client-side errors are usually non-retriable:
79
+ throw new FatalError();
80
+ } else {
81
+ throw new RetriableError();
82
+ }
83
+ },
84
+ onmessage(msg) {
85
+ // if the server emits an error message, throw an exception
86
+ // so it gets handled by the onerror callback below:
87
+ if (msg.event === 'FatalError') {
88
+ throw new FatalError(msg.data);
89
+ }
90
+ },
91
+ onclose() {
92
+ // if the server closes the connection unexpectedly, retry:
93
+ throw new RetriableError();
94
+ },
95
+ onerror(err) {
96
+ if (err instanceof FatalError) {
97
+ throw err; // rethrow to stop the operation
98
+ } else {
99
+ // do nothing to automatically retry. You can also
100
+ // return a specific retry interval here.
101
+ }
102
+ }
103
+ });
104
+ ```
105
+
106
+ You can implement exponential backoff to avoid overwhelming the server during outages:
107
+
108
+ ```ts
109
+ let retryCount = 0;
110
+
111
+ fetchEventSource('/api/sse', {
112
+ async onopen(response) {
113
+ if (response.ok) {
114
+ retryCount = 0; // Reset on successful connection
115
+ }
116
+ },
117
+ onerror(err) {
118
+ retryCount++;
119
+ // Exponential backoff: 1s, 2s, 4s, 8s... max 30s
120
+ const backoff = Math.min(1000 * Math.pow(2, retryCount - 1), 30000);
121
+ console.log(`Retrying in ${backoff}ms...`);
122
+ return backoff;
123
+ }
124
+ });
125
+ ```
126
+
127
+ # Closing the Connection
128
+
129
+ To close the connection from the client side, use an `AbortController` and call `abort()` when you want to stop:
130
+
131
+ ```ts
132
+ const ctrl = new AbortController();
133
+
134
+ fetchEventSource('/api/sse', {
135
+ signal: ctrl.signal,
136
+ onmessage(msg) {
137
+ console.log(msg.data);
138
+
139
+ // Close based on a message from the server:
140
+ if (msg.event === 'done') {
141
+ ctrl.abort();
142
+ }
143
+ }
144
+ });
145
+
146
+ // Or close from elsewhere, e.g., when the user clicks a button:
147
+ document.getElementById('stop').addEventListener('click', () => {
148
+ ctrl.abort();
149
+ });
150
+
151
+ // Or close when a component unmounts (React example):
152
+ useEffect(() => {
153
+ const ctrl = new AbortController();
154
+ fetchEventSource('/api/sse', { signal: ctrl.signal, ... });
155
+ return () => ctrl.abort();
156
+ }, []);
157
+ ```
158
+
159
+ # Page Visibility
160
+
161
+ **Important:** By default, this library closes the connection when the page is hidden (e.g., user switches tabs or minimizes the window) and reconnects when visible again. This differs from the native `EventSource` which stays connected.
162
+
163
+ To keep the connection open even when the page is hidden, set `openWhenHidden: true`:
164
+
165
+ ```ts
166
+ fetchEventSource('/api/sse', {
167
+ openWhenHidden: true, // Keep connection open when page is hidden
168
+ onmessage(msg) {
169
+ console.log(msg.data);
170
+ }
171
+ });
172
+ ```
173
+
174
+ | `openWhenHidden` | Behavior |
175
+ |------------------|----------|
176
+ | `false` (default) | Connection closes when hidden, reconnects when visible |
177
+ | `true` | Connection stays open regardless of page visibility |
178
+
179
+ # Compatibility
180
+ This library is written in typescript and targets ES2017 features supported by all evergreen browsers (Chrome, Firefox, Safari, Edge.) You might need to [polyfill TextDecoder](https://www.npmjs.com/package/fast-text-encoding) for old Edge (versions < 79), though:
181
+ ```js
182
+ require('fast-text-encoding');
183
+ ```
184
+
185
+ # Connection Limits
186
+
187
+ This library does not impose any connection limits, but browsers and servers do. Keep these limitations in mind when designing your application:
188
+
189
+ | Factor | Typical Limit | Notes |
190
+ |--------|---------------|-------|
191
+ | HTTP/1.1 connections per domain | ~6 | Browser-enforced limit |
192
+ | HTTP/2 streams per connection | ~100-256 | Better multiplexing, but still limited |
193
+ | Browser memory | Varies | Each connection consumes memory |
194
+ | Server connections | Varies | Depends on server configuration |
195
+
196
+ **Recommendations for applications requiring many concurrent streams:**
197
+
198
+ 1. **Use HTTP/2** on your server to benefit from connection multiplexing
199
+ 2. **Consolidate streams** - use a single SSE connection with event routing instead of many separate connections
200
+ 3. **Consider alternatives** - for 100+ real-time streams, WebSockets or a pub/sub architecture may be more appropriate
201
+ 4. **Close unused connections** - use the `AbortController` to close connections when they're no longer needed
202
+
203
+ # Contributing
204
+
205
+ This project welcomes contributions and suggestions. Most contributions require you to agree to a
206
+ Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
207
+ the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
208
+
209
+ When you submit a pull request, a CLA bot will automatically determine whether you need to provide
210
+ a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
211
+ provided by the bot. You will only need to do this once across all repos using our CLA.
212
+
213
+ This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
214
+ For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
215
+ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
@@ -0,0 +1,46 @@
1
+ import { EventSourceMessage } from './parse.js';
2
+ export declare const EventStreamContentType = "text/event-stream";
3
+ export interface FetchEventSourceInit extends Omit<RequestInit, 'headers'> {
4
+ /**
5
+ * The request headers. FetchEventSource only supports the Record<string,string> format,
6
+ * or a function that returns headers. Using a function allows dynamic headers that are
7
+ * refreshed on each retry, useful for updating Authorization tokens.
8
+ */
9
+ headers?: Record<string, string> | (() => Record<string, string>);
10
+ /**
11
+ * Called when a response is received. Use this to validate that the response
12
+ * actually matches what you expect (and throw if it doesn't.) If not provided,
13
+ * will default to a basic validation to ensure the content-type is text/event-stream.
14
+ */
15
+ onopen?: (response: Response) => Promise<void>;
16
+ /**
17
+ * Called when a message is received. NOTE: Unlike the default browser
18
+ * EventSource.onmessage, this callback is called for _all_ events,
19
+ * even ones with a custom `event` field.
20
+ */
21
+ onmessage?: (ev: EventSourceMessage) => void;
22
+ /**
23
+ * Called when a response finishes. If you don't expect the server to kill
24
+ * the connection, you can throw an exception here and retry using onerror.
25
+ */
26
+ onclose?: () => void;
27
+ /**
28
+ * Called when there is any error making the request / processing messages /
29
+ * handling callbacks etc. Use this to control the retry strategy: if the
30
+ * error is fatal, rethrow the error inside the callback to stop the entire
31
+ * operation. Otherwise, you can return an interval (in milliseconds) after
32
+ * which the request will automatically retry (with the last-event-id).
33
+ * If this callback is not specified, or it returns undefined, fetchEventSource
34
+ * will treat every error as retriable and will try again after 1 second.
35
+ */
36
+ onerror?: (err: any) => number | null | undefined | void;
37
+ /**
38
+ * If true, will keep the request open even if the document is hidden.
39
+ * By default, fetchEventSource will close the request and reopen it
40
+ * automatically when the document becomes visible again.
41
+ */
42
+ openWhenHidden?: boolean;
43
+ /** The Fetch function to use. Defaults to window.fetch */
44
+ fetch?: typeof fetch;
45
+ }
46
+ export declare function fetchEventSource(input: RequestInfo, { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch, ...rest }: FetchEventSourceInit): Promise<void>;
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.EventStreamContentType = void 0;
15
+ exports.fetchEventSource = fetchEventSource;
16
+ const parse_js_1 = require("./parse.js");
17
+ exports.EventStreamContentType = 'text/event-stream';
18
+ const DefaultRetryInterval = 1000;
19
+ const LastEventId = 'last-event-id';
20
+ function fetchEventSource(input, _a) {
21
+ var { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch } = _a, rest = __rest(_a, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
22
+ // Create a function to get headers, supporting both static and dynamic headers
23
+ const getHeaders = typeof inputHeaders === 'function'
24
+ ? inputHeaders
25
+ : () => (Object.assign({}, (inputHeaders !== null && inputHeaders !== void 0 ? inputHeaders : {})));
26
+ return new Promise((resolve, reject) => {
27
+ // Handle already-aborted signal immediately to match native fetch behavior
28
+ if (inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.aborted) {
29
+ resolve();
30
+ return;
31
+ }
32
+ // Track last-event-id separately so it persists across retries even with dynamic headers
33
+ let lastEventId;
34
+ let curRequestController;
35
+ function onVisibilityChange() {
36
+ curRequestController.abort(); // close existing request on every visibility change
37
+ if (!document.hidden) {
38
+ create(); // page is now visible again, recreate request.
39
+ }
40
+ }
41
+ if (!openWhenHidden) {
42
+ document.addEventListener('visibilitychange', onVisibilityChange);
43
+ }
44
+ let retryInterval = DefaultRetryInterval;
45
+ let retryTimer = 0;
46
+ function dispose() {
47
+ document.removeEventListener('visibilitychange', onVisibilityChange);
48
+ window.clearTimeout(retryTimer);
49
+ // Only abort if not already aborted to avoid unnecessary DOMException logs
50
+ if (!curRequestController.signal.aborted) {
51
+ curRequestController.abort();
52
+ }
53
+ }
54
+ // if the incoming signal aborts, dispose resources and resolve:
55
+ inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.addEventListener('abort', () => {
56
+ dispose();
57
+ resolve(); // don't waste time constructing/logging errors
58
+ });
59
+ const fetch = inputFetch !== null && inputFetch !== void 0 ? inputFetch : window.fetch;
60
+ const onopen = inputOnOpen !== null && inputOnOpen !== void 0 ? inputOnOpen : defaultOnOpen;
61
+ async function create() {
62
+ var _a;
63
+ // Get fresh headers on each attempt (supports dynamic headers for token refresh)
64
+ const headers = getHeaders();
65
+ if (!headers.accept) {
66
+ headers.accept = exports.EventStreamContentType;
67
+ }
68
+ // Add last-event-id if we have one from a previous connection
69
+ if (lastEventId) {
70
+ headers[LastEventId] = lastEventId;
71
+ }
72
+ // Store controller in local scope to avoid race condition during rapid tab switches.
73
+ // Without this, the catch block might check a different controller's abort status.
74
+ const currentController = new AbortController();
75
+ curRequestController = currentController;
76
+ try {
77
+ const response = await fetch(input, Object.assign(Object.assign({}, rest), { headers, signal: currentController.signal }));
78
+ await onopen(response);
79
+ await (0, parse_js_1.getBytes)(response.body, (0, parse_js_1.getLines)((0, parse_js_1.getMessages)(id => {
80
+ if (id) {
81
+ // store the id and send it back on the next retry:
82
+ lastEventId = id;
83
+ }
84
+ else {
85
+ // don't send the last-event-id header anymore:
86
+ lastEventId = undefined;
87
+ }
88
+ }, retry => {
89
+ retryInterval = retry;
90
+ }, onmessage)));
91
+ onclose === null || onclose === void 0 ? void 0 : onclose();
92
+ dispose();
93
+ resolve();
94
+ }
95
+ catch (err) {
96
+ if (!currentController.signal.aborted) {
97
+ // if we haven't aborted the request ourselves:
98
+ try {
99
+ // check if we need to retry:
100
+ const interval = (_a = onerror === null || onerror === void 0 ? void 0 : onerror(err)) !== null && _a !== void 0 ? _a : retryInterval;
101
+ window.clearTimeout(retryTimer);
102
+ // if user aborted during onerror callback, stop retrying:
103
+ if (inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.aborted) {
104
+ dispose();
105
+ resolve();
106
+ return;
107
+ }
108
+ // if page is hidden and openWhenHidden is false, don't retry now.
109
+ // onVisibilityChange will call create() when page becomes visible.
110
+ if (!openWhenHidden && document.hidden) {
111
+ return;
112
+ }
113
+ retryTimer = window.setTimeout(create, interval);
114
+ }
115
+ catch (innerErr) {
116
+ // we should not retry anymore:
117
+ dispose();
118
+ reject(innerErr);
119
+ }
120
+ }
121
+ }
122
+ }
123
+ create();
124
+ });
125
+ }
126
+ function defaultOnOpen(response) {
127
+ if (!response.ok) {
128
+ throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);
129
+ }
130
+ const contentType = response.headers.get('content-type');
131
+ if (!(contentType === null || contentType === void 0 ? void 0 : contentType.startsWith(exports.EventStreamContentType))) {
132
+ throw new Error(`Expected content-type to be ${exports.EventStreamContentType}, Actual: ${contentType}`);
133
+ }
134
+ }
135
+ //# sourceMappingURL=fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAyDA,4CA8HC;AAvLD,yCAAiF;AAEpE,QAAA,sBAAsB,GAAG,mBAAmB,CAAC;AAE1D,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAClC,MAAM,WAAW,GAAG,eAAe,CAAC;AAoDpC,SAAgB,gBAAgB,CAAC,KAAkB,EAAE,EAU9B;QAV8B,EACjD,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,WAAW,EACnB,SAAS,EACT,OAAO,EACP,OAAO,EACP,cAAc,EACd,KAAK,EAAE,UAAU,OAEE,EADhB,IAAI,cAT0C,6FAUpD,CADU;IAEP,+EAA+E;IAC/E,MAAM,UAAU,GAAiC,OAAO,YAAY,KAAK,UAAU;QAC/E,CAAC,CAAC,YAAY;QACd,CAAC,CAAC,GAAG,EAAE,CAAC,mBAAM,CAAC,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,EAAE,CAAC,EAAG,CAAC;IAE1C,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EA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\r\n\r\nexport const EventStreamContentType = 'text/event-stream';\r\n\r\nconst DefaultRetryInterval = 1000;\r\nconst LastEventId = 'last-event-id';\r\n\r\nexport interface FetchEventSourceInit extends Omit<RequestInit, 'headers'> {\r\n /**\r\n * The request headers. FetchEventSource only supports the Record<string,string> format,\r\n * or a function that returns headers. Using a function allows dynamic headers that are\r\n * refreshed on each retry, useful for updating Authorization tokens.\r\n */\r\n headers?: Record<string, string> | (() => Record<string, string>),\r\n\r\n /**\r\n * Called when a response is received. Use this to validate that the response\r\n * actually matches what you expect (and throw if it doesn't.) If not provided,\r\n * will default to a basic validation to ensure the content-type is text/event-stream.\r\n */\r\n onopen?: (response: Response) => Promise<void>,\r\n\r\n /**\r\n * Called when a message is received. NOTE: Unlike the default browser\r\n * EventSource.onmessage, this callback is called for _all_ events,\r\n * even ones with a custom `event` field.\r\n */\r\n onmessage?: (ev: EventSourceMessage) => void;\r\n\r\n /**\r\n * Called when a response finishes. If you don't expect the server to kill\r\n * the connection, you can throw an exception here and retry using onerror.\r\n */\r\n onclose?: () => void;\r\n\r\n /**\r\n * Called when there is any error making the request / processing messages /\r\n * handling callbacks etc. Use this to control the retry strategy: if the\r\n * error is fatal, rethrow the error inside the callback to stop the entire\r\n * operation. Otherwise, you can return an interval (in milliseconds) after\r\n * which the request will automatically retry (with the last-event-id).\r\n * If this callback is not specified, or it returns undefined, fetchEventSource\r\n * will treat every error as retriable and will try again after 1 second.\r\n */\r\n onerror?: (err: any) => number | null | undefined | void,\r\n\r\n /**\r\n * If true, will keep the request open even if the document is hidden.\r\n * By default, fetchEventSource will close the request and reopen it\r\n * automatically when the document becomes visible again.\r\n */\r\n openWhenHidden?: boolean;\r\n\r\n /** The Fetch function to use. Defaults to window.fetch */\r\n fetch?: typeof fetch;\r\n}\r\n\r\nexport function fetchEventSource(input: RequestInfo, {\r\n signal: inputSignal,\r\n headers: inputHeaders,\r\n onopen: inputOnOpen,\r\n onmessage,\r\n onclose,\r\n onerror,\r\n openWhenHidden,\r\n fetch: inputFetch,\r\n ...rest\r\n}: FetchEventSourceInit) {\r\n // Create a function to get headers, supporting both static and dynamic headers\r\n const getHeaders: () => Record<string, string> = typeof inputHeaders === 'function'\r\n ? inputHeaders\r\n : () => ({ ...(inputHeaders ?? {}) });\r\n\r\n return new Promise<void>((resolve, reject) => {\r\n // Handle already-aborted signal immediately to match native fetch behavior\r\n if (inputSignal?.aborted) {\r\n resolve();\r\n return;\r\n }\r\n\r\n // Track last-event-id separately so it persists across retries even with dynamic headers\r\n let lastEventId: string | undefined;\r\n\r\n let curRequestController: AbortController;\r\n function onVisibilityChange() {\r\n curRequestController.abort(); // close existing request on every visibility change\r\n if (!document.hidden) {\r\n create(); // page is now visible again, recreate request.\r\n }\r\n }\r\n\r\n if (!openWhenHidden) {\r\n document.addEventListener('visibilitychange', onVisibilityChange);\r\n }\r\n\r\n let retryInterval = DefaultRetryInterval;\r\n let retryTimer = 0;\r\n function dispose() {\r\n document.removeEventListener('visibilitychange', onVisibilityChange);\r\n window.clearTimeout(retryTimer);\r\n // Only abort if not already aborted to avoid unnecessary DOMException logs\r\n if (!curRequestController.signal.aborted) {\r\n curRequestController.abort();\r\n }\r\n }\r\n\r\n // if the incoming signal aborts, dispose resources and resolve:\r\n inputSignal?.addEventListener('abort', () => {\r\n dispose();\r\n resolve(); // don't waste time constructing/logging errors\r\n });\r\n\r\n const fetch = inputFetch ?? window.fetch;\r\n const onopen = inputOnOpen ?? defaultOnOpen;\r\n async function create() {\r\n // Get fresh headers on each attempt (supports dynamic headers for token refresh)\r\n const headers = getHeaders();\r\n if (!headers.accept) {\r\n headers.accept = EventStreamContentType;\r\n }\r\n // Add last-event-id if we have one from a previous connection\r\n if (lastEventId) {\r\n headers[LastEventId] = lastEventId;\r\n }\r\n\r\n // Store controller in local scope to avoid race condition during rapid tab switches.\r\n // Without this, the catch block might check a different controller's abort status.\r\n const currentController = new AbortController();\r\n curRequestController = currentController;\r\n try {\r\n const response = await fetch(input, {\r\n ...rest,\r\n headers,\r\n signal: currentController.signal,\r\n });\r\n\r\n await onopen(response);\r\n\r\n await getBytes(response.body!, getLines(getMessages(id => {\r\n if (id) {\r\n // store the id and send it back on the next retry:\r\n lastEventId = id;\r\n } else {\r\n // don't send the last-event-id header anymore:\r\n lastEventId = undefined;\r\n }\r\n }, retry => {\r\n retryInterval = retry;\r\n }, onmessage)));\r\n\r\n onclose?.();\r\n dispose();\r\n resolve();\r\n } catch (err) {\r\n if (!currentController.signal.aborted) {\r\n // if we haven't aborted the request ourselves:\r\n try {\r\n // check if we need to retry:\r\n const interval: any = onerror?.(err) ?? retryInterval;\r\n window.clearTimeout(retryTimer);\r\n // if user aborted during onerror callback, stop retrying:\r\n if (inputSignal?.aborted) {\r\n dispose();\r\n resolve();\r\n return;\r\n }\r\n // if page is hidden and openWhenHidden is false, don't retry now.\r\n // onVisibilityChange will call create() when page becomes visible.\r\n if (!openWhenHidden && document.hidden) {\r\n return;\r\n }\r\n retryTimer = window.setTimeout(create, interval);\r\n } catch (innerErr) {\r\n // we should not retry anymore:\r\n dispose();\r\n reject(innerErr);\r\n }\r\n }\r\n }\r\n }\r\n\r\n create();\r\n });\r\n}\r\n\r\nfunction defaultOnOpen(response: Response) {\r\n if (!response.ok) {\r\n throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);\r\n }\r\n const contentType = response.headers.get('content-type');\r\n if (!contentType?.startsWith(EventStreamContentType)) {\r\n throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`);\r\n }\r\n}\r\n"]}
@@ -0,0 +1,2 @@
1
+ export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch.js';
2
+ export { EventSourceMessage } from './parse.js';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventStreamContentType = exports.fetchEventSource = void 0;
4
+ var fetch_js_1 = require("./fetch.js");
5
+ Object.defineProperty(exports, "fetchEventSource", { enumerable: true, get: function () { return fetch_js_1.fetchEventSource; } });
6
+ Object.defineProperty(exports, "EventStreamContentType", { enumerable: true, get: function () { return fetch_js_1.EventStreamContentType; } });
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAA4F;AAAnF,4GAAA,gBAAgB,OAAA;AAAwB,kHAAA,sBAAsB,OAAA","sourcesContent":["export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch.js';\r\nexport { EventSourceMessage } from './parse.js';\r\n"]}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Represents a message sent in an event stream
3
+ * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
4
+ */
5
+ export interface EventSourceMessage {
6
+ /** The event ID to set the EventSource object's last event ID value. */
7
+ id: string;
8
+ /** A string identifying the type of event described. */
9
+ event: string;
10
+ /** The event data */
11
+ data: string;
12
+ /** The reconnection interval (in milliseconds) to wait before retrying the connection */
13
+ retry?: number;
14
+ }
15
+ /**
16
+ * Converts a ReadableStream into a callback pattern.
17
+ * @param stream The input ReadableStream.
18
+ * @param onChunk A function that will be called on each new byte chunk in the stream.
19
+ * @returns {Promise<void>} A promise that will be resolved when the stream closes.
20
+ */
21
+ export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void): Promise<void>;
22
+ /**
23
+ * Parses arbitary byte chunks into EventSource line buffers.
24
+ * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
25
+ * @param onLine A function that will be called on each new EventSource line.
26
+ * @returns A function that should be called for each incoming byte chunk.
27
+ */
28
+ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number) => void): (arr: Uint8Array) => void;
29
+ /**
30
+ * Parses line buffers into EventSourceMessages.
31
+ * @param onId A function that will be called on each `id` field.
32
+ * @param onRetry A function that will be called on each `retry` field.
33
+ * @param onMessage A function that will be called on each message.
34
+ * @returns A function that should be called for each incoming line buffer.
35
+ */
36
+ export declare function getMessages(onId: (id: string) => void, onRetry: (retry: number) => void, onMessage?: (msg: EventSourceMessage) => void): (line: Uint8Array, fieldLength: number) => void;
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBytes = getBytes;
4
+ exports.getLines = getLines;
5
+ exports.getMessages = getMessages;
6
+ /**
7
+ * Converts a ReadableStream into a callback pattern.
8
+ * @param stream The input ReadableStream.
9
+ * @param onChunk A function that will be called on each new byte chunk in the stream.
10
+ * @returns {Promise<void>} A promise that will be resolved when the stream closes.
11
+ */
12
+ async function getBytes(stream, onChunk) {
13
+ const reader = stream.getReader();
14
+ let result;
15
+ while (!(result = await reader.read()).done) {
16
+ onChunk(result.value);
17
+ }
18
+ }
19
+ /**
20
+ * Parses arbitary byte chunks into EventSource line buffers.
21
+ * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
22
+ * @param onLine A function that will be called on each new EventSource line.
23
+ * @returns A function that should be called for each incoming byte chunk.
24
+ */
25
+ function getLines(onLine) {
26
+ let buffer;
27
+ let position; // current read position
28
+ let fieldLength; // length of the `field` portion of the line
29
+ let discardTrailingNewline = false;
30
+ // return a function that can process each incoming byte chunk:
31
+ return function onChunk(arr) {
32
+ if (buffer === undefined) {
33
+ buffer = arr;
34
+ position = 0;
35
+ fieldLength = -1;
36
+ }
37
+ else {
38
+ // we're still parsing the old line. Append the new bytes into buffer:
39
+ buffer = concat(buffer, arr);
40
+ }
41
+ const bufLength = buffer.length;
42
+ let lineStart = 0; // index where the current line starts
43
+ while (position < bufLength) {
44
+ if (discardTrailingNewline) {
45
+ if (buffer[position] === 10 /* ControlChars.NewLine */) {
46
+ lineStart = ++position; // skip to next char
47
+ }
48
+ discardTrailingNewline = false;
49
+ }
50
+ // start looking forward till the end of line:
51
+ let lineEnd = -1; // index of the \r or \n char
52
+ for (; position < bufLength && lineEnd === -1; ++position) {
53
+ switch (buffer[position]) {
54
+ case 58 /* ControlChars.Colon */:
55
+ if (fieldLength === -1) { // first colon in line
56
+ fieldLength = position - lineStart;
57
+ }
58
+ break;
59
+ // @ts-ignore:7029 \r case below should fallthrough to \n:
60
+ case 13 /* ControlChars.CarriageReturn */:
61
+ discardTrailingNewline = true;
62
+ case 10 /* ControlChars.NewLine */:
63
+ lineEnd = position;
64
+ break;
65
+ }
66
+ }
67
+ if (lineEnd === -1) {
68
+ // We reached the end of the buffer but the line hasn't ended.
69
+ // Wait for the next arr and then continue parsing:
70
+ break;
71
+ }
72
+ // we've reached the line end, send it out:
73
+ onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
74
+ lineStart = position; // we're now on the next line
75
+ fieldLength = -1;
76
+ }
77
+ if (lineStart === bufLength) {
78
+ buffer = undefined; // we've finished reading it
79
+ }
80
+ else if (lineStart !== 0) {
81
+ // Create a new view into buffer beginning at lineStart so we don't
82
+ // need to copy over the previous lines when we get the new arr:
83
+ buffer = buffer.subarray(lineStart);
84
+ position -= lineStart;
85
+ }
86
+ };
87
+ }
88
+ /**
89
+ * Parses line buffers into EventSourceMessages.
90
+ * @param onId A function that will be called on each `id` field.
91
+ * @param onRetry A function that will be called on each `retry` field.
92
+ * @param onMessage A function that will be called on each message.
93
+ * @returns A function that should be called for each incoming line buffer.
94
+ */
95
+ function getMessages(onId, onRetry, onMessage) {
96
+ let message = newMessage();
97
+ const decoder = new TextDecoder();
98
+ // return a function that can process each incoming line buffer:
99
+ return function onLine(line, fieldLength) {
100
+ if (line.length === 0) {
101
+ // empty line denotes end of message. Trigger the callback and start a new message:
102
+ // per spec: "If the data buffer's last character is a U+000A LINE FEED (LF) character,
103
+ // then remove the last character from the data buffer."
104
+ if (message.data.endsWith('\n')) {
105
+ message.data = message.data.slice(0, -1);
106
+ }
107
+ // per spec, only dispatch if data is not empty (ignore comment-only messages)
108
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage
109
+ if (message.data) {
110
+ onMessage === null || onMessage === void 0 ? void 0 : onMessage(message);
111
+ }
112
+ message = newMessage();
113
+ }
114
+ else if (fieldLength > 0) { // exclude comments and lines with no values
115
+ // line is of format "<field>:<value>" or "<field>: <value>"
116
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
117
+ const field = decoder.decode(line.subarray(0, fieldLength));
118
+ const valueOffset = fieldLength + (line[fieldLength + 1] === 32 /* ControlChars.Space */ ? 2 : 1);
119
+ const value = decoder.decode(line.subarray(valueOffset));
120
+ switch (field) {
121
+ case 'data':
122
+ // per spec: "Append the field value to the data buffer,
123
+ // then append a single U+000A LINE FEED (LF) character to the data buffer."
124
+ message.data += value + '\n';
125
+ break;
126
+ case 'event':
127
+ message.event = value;
128
+ break;
129
+ case 'id':
130
+ onId(message.id = value);
131
+ break;
132
+ case 'retry':
133
+ const retry = parseInt(value, 10);
134
+ if (!isNaN(retry)) { // per spec, ignore non-integers
135
+ onRetry(message.retry = retry);
136
+ }
137
+ break;
138
+ }
139
+ }
140
+ };
141
+ }
142
+ function concat(a, b) {
143
+ const res = new Uint8Array(a.length + b.length);
144
+ res.set(a);
145
+ res.set(b, a.length);
146
+ return res;
147
+ }
148
+ function newMessage() {
149
+ // data, event, and id must be initialized to empty strings:
150
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
151
+ // retry should be initialized to undefined so we return a consistent shape
152
+ // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways
153
+ return {
154
+ data: '',
155
+ event: '',
156
+ id: '',
157
+ retry: undefined,
158
+ };
159
+ }
160
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../../src/parse.ts"],"names":[],"mappings":";;AAqBA,4BAMC;AAeD,4BAmEC;AASD,kCAmDC;AA1JD;;;;;GAKG;AACI,KAAK,UAAU,QAAQ,CAAC,MAAkC,EAAE,OAAkC;IACjG,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CA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ClC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,gCAAgC;wBACjD,OAAO,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;oBACnC,CAAC;oBACD,MAAM;YACd,CAAC;QACL,CAAC;IACL,CAAC,CAAA;AACL,CAAC;AAED,SAAS,MAAM,CAAC,CAAa,EAAE,CAAa;IACxC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IAChD,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACX,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IACrB,OAAO,GAAG,CAAC;AACf,CAAC;AAED,SAAS,UAAU;IACf,4DAA4D;IAC5D,6FAA6F;IAC7F,2EAA2E;IAC3E,qFAAqF;IACrF,OAAO;QACH,IAAI,EAAE,EAAE;QACR,KAAK,EAAE,EAAE;QACT,EAAE,EAAE,EAAE;QACN,KAAK,EAAE,SAAS;KACnB,CAAC;AACN,CAAC","sourcesContent":["/**\r\n * Represents a message sent in an event stream\r\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\r\n */\r\nexport interface EventSourceMessage {\r\n /** The event ID to set the EventSource object's last event ID value. */\r\n id: string;\r\n /** A string identifying the type of event described. */\r\n event: string;\r\n /** The event data */\r\n data: string;\r\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\r\n retry?: number;\r\n}\r\n\r\n/**\r\n * Converts a ReadableStream into a callback pattern.\r\n * @param stream The input ReadableStream.\r\n * @param onChunk A function that will be called on each new byte chunk in the stream.\r\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\r\n */\r\nexport async function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void) {\r\n const reader = stream.getReader();\r\n let result: ReadableStreamReadResult<Uint8Array>;\r\n while (!(result = await reader.read()).done) {\r\n onChunk(result.value);\r\n }\r\n}\r\n\r\nconst enum ControlChars {\r\n NewLine = 10,\r\n CarriageReturn = 13,\r\n Space = 32,\r\n Colon = 58,\r\n}\r\n\r\n/** \r\n * Parses arbitary byte chunks into EventSource line buffers.\r\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n. \r\n * @param onLine A function that will be called on each new EventSource line.\r\n * @returns A function that should be called for each incoming byte chunk.\r\n */\r\nexport function getLines(onLine: (line: Uint8Array, fieldLength: number) => void) {\r\n let buffer: Uint8Array | undefined;\r\n let position: number; // current read position\r\n let fieldLength: number; // length of the `field` portion of the line\r\n let discardTrailingNewline = false;\r\n\r\n // return a function that can process each incoming byte chunk:\r\n return function onChunk(arr: Uint8Array) {\r\n if (buffer === undefined) {\r\n buffer = arr;\r\n position = 0;\r\n fieldLength = -1;\r\n } else {\r\n // we're still parsing the old line. Append the new bytes into buffer:\r\n buffer = concat(buffer, arr);\r\n }\r\n\r\n const bufLength = buffer.length;\r\n let lineStart = 0; // index where the current line starts\r\n while (position < bufLength) {\r\n if (discardTrailingNewline) {\r\n if (buffer[position] === ControlChars.NewLine) {\r\n lineStart = ++position; // skip to next char\r\n }\r\n \r\n discardTrailingNewline = false;\r\n }\r\n \r\n // start looking forward till the end of line:\r\n let lineEnd = -1; // index of the \\r or \\n char\r\n for (; position < bufLength && lineEnd === -1; ++position) {\r\n switch (buffer[position]) {\r\n case ControlChars.Colon:\r\n if (fieldLength === -1) { // first colon in line\r\n fieldLength = position - lineStart;\r\n }\r\n break;\r\n // @ts-ignore:7029 \\r case below should fallthrough to \\n:\r\n case ControlChars.CarriageReturn:\r\n discardTrailingNewline = true;\r\n case ControlChars.NewLine:\r\n lineEnd = position;\r\n break;\r\n }\r\n }\r\n\r\n if (lineEnd === -1) {\r\n // We reached the end of the buffer but the line hasn't ended.\r\n // Wait for the next arr and then continue parsing:\r\n break;\r\n }\r\n\r\n // we've reached the line end, send it out:\r\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\r\n lineStart = position; // we're now on the next line\r\n fieldLength = -1;\r\n }\r\n\r\n if (lineStart === bufLength) {\r\n buffer = undefined; // we've finished reading it\r\n } else if (lineStart !== 0) {\r\n // Create a new view into buffer beginning at lineStart so we don't\r\n // need to copy over the previous lines when we get the new arr:\r\n buffer = buffer.subarray(lineStart);\r\n position -= lineStart;\r\n }\r\n }\r\n}\r\n\r\n/** \r\n * Parses line buffers into EventSourceMessages.\r\n * @param onId A function that will be called on each `id` field.\r\n * @param onRetry A function that will be called on each `retry` field.\r\n * @param onMessage A function that will be called on each message.\r\n * @returns A function that should be called for each incoming line buffer.\r\n */\r\nexport function getMessages(\r\n onId: (id: string) => void,\r\n onRetry: (retry: number) => void,\r\n onMessage?: (msg: EventSourceMessage) => void\r\n) {\r\n let message = newMessage();\r\n const decoder = new TextDecoder();\r\n\r\n // return a function that can process each incoming line buffer:\r\n return function onLine(line: Uint8Array, fieldLength: number) {\r\n if (line.length === 0) {\r\n // empty line denotes end of message. Trigger the callback and start a new message:\r\n // per spec: \"If the data buffer's last character is a U+000A LINE FEED (LF) character,\r\n // then remove the last character from the data buffer.\"\r\n if (message.data.endsWith('\\n')) {\r\n message.data = message.data.slice(0, -1);\r\n }\r\n // per spec, only dispatch if data is not empty (ignore comment-only messages)\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage\r\n if (message.data) {\r\n onMessage?.(message);\r\n }\r\n message = newMessage();\r\n } else if (fieldLength > 0) { // exclude comments and lines with no values\r\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\r\n const field = decoder.decode(line.subarray(0, fieldLength));\r\n const valueOffset = fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\r\n const value = decoder.decode(line.subarray(valueOffset));\r\n\r\n switch (field) {\r\n case 'data':\r\n // per spec: \"Append the field value to the data buffer,\r\n // then append a single U+000A LINE FEED (LF) character to the data buffer.\"\r\n message.data += value + '\\n';\r\n break;\r\n case 'event':\r\n message.event = value;\r\n break;\r\n case 'id':\r\n onId(message.id = value);\r\n break;\r\n case 'retry':\r\n const retry = parseInt(value, 10);\r\n if (!isNaN(retry)) { // per spec, ignore non-integers\r\n onRetry(message.retry = retry);\r\n }\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n\r\nfunction concat(a: Uint8Array, b: Uint8Array) {\r\n const res = new Uint8Array(a.length + b.length);\r\n res.set(a);\r\n res.set(b, a.length);\r\n return res;\r\n}\r\n\r\nfunction newMessage(): EventSourceMessage {\r\n // data, event, and id must be initialized to empty strings:\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\r\n // retry should be initialized to undefined so we return a consistent shape\r\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\r\n return {\r\n data: '',\r\n event: '',\r\n id: '',\r\n retry: undefined,\r\n };\r\n}\r\n"]}
@@ -0,0 +1,46 @@
1
+ import { EventSourceMessage } from './parse.js';
2
+ export declare const EventStreamContentType = "text/event-stream";
3
+ export interface FetchEventSourceInit extends Omit<RequestInit, 'headers'> {
4
+ /**
5
+ * The request headers. FetchEventSource only supports the Record<string,string> format,
6
+ * or a function that returns headers. Using a function allows dynamic headers that are
7
+ * refreshed on each retry, useful for updating Authorization tokens.
8
+ */
9
+ headers?: Record<string, string> | (() => Record<string, string>);
10
+ /**
11
+ * Called when a response is received. Use this to validate that the response
12
+ * actually matches what you expect (and throw if it doesn't.) If not provided,
13
+ * will default to a basic validation to ensure the content-type is text/event-stream.
14
+ */
15
+ onopen?: (response: Response) => Promise<void>;
16
+ /**
17
+ * Called when a message is received. NOTE: Unlike the default browser
18
+ * EventSource.onmessage, this callback is called for _all_ events,
19
+ * even ones with a custom `event` field.
20
+ */
21
+ onmessage?: (ev: EventSourceMessage) => void;
22
+ /**
23
+ * Called when a response finishes. If you don't expect the server to kill
24
+ * the connection, you can throw an exception here and retry using onerror.
25
+ */
26
+ onclose?: () => void;
27
+ /**
28
+ * Called when there is any error making the request / processing messages /
29
+ * handling callbacks etc. Use this to control the retry strategy: if the
30
+ * error is fatal, rethrow the error inside the callback to stop the entire
31
+ * operation. Otherwise, you can return an interval (in milliseconds) after
32
+ * which the request will automatically retry (with the last-event-id).
33
+ * If this callback is not specified, or it returns undefined, fetchEventSource
34
+ * will treat every error as retriable and will try again after 1 second.
35
+ */
36
+ onerror?: (err: any) => number | null | undefined | void;
37
+ /**
38
+ * If true, will keep the request open even if the document is hidden.
39
+ * By default, fetchEventSource will close the request and reopen it
40
+ * automatically when the document becomes visible again.
41
+ */
42
+ openWhenHidden?: boolean;
43
+ /** The Fetch function to use. Defaults to window.fetch */
44
+ fetch?: typeof fetch;
45
+ }
46
+ export declare function fetchEventSource(input: RequestInfo, { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch, ...rest }: FetchEventSourceInit): Promise<void>;
@@ -0,0 +1,131 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { getBytes, getLines, getMessages } from './parse.js';
13
+ export const EventStreamContentType = 'text/event-stream';
14
+ const DefaultRetryInterval = 1000;
15
+ const LastEventId = 'last-event-id';
16
+ export function fetchEventSource(input, _a) {
17
+ var { signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, onmessage, onclose, onerror, openWhenHidden, fetch: inputFetch } = _a, rest = __rest(_a, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
18
+ // Create a function to get headers, supporting both static and dynamic headers
19
+ const getHeaders = typeof inputHeaders === 'function'
20
+ ? inputHeaders
21
+ : () => (Object.assign({}, (inputHeaders !== null && inputHeaders !== void 0 ? inputHeaders : {})));
22
+ return new Promise((resolve, reject) => {
23
+ // Handle already-aborted signal immediately to match native fetch behavior
24
+ if (inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.aborted) {
25
+ resolve();
26
+ return;
27
+ }
28
+ // Track last-event-id separately so it persists across retries even with dynamic headers
29
+ let lastEventId;
30
+ let curRequestController;
31
+ function onVisibilityChange() {
32
+ curRequestController.abort(); // close existing request on every visibility change
33
+ if (!document.hidden) {
34
+ create(); // page is now visible again, recreate request.
35
+ }
36
+ }
37
+ if (!openWhenHidden) {
38
+ document.addEventListener('visibilitychange', onVisibilityChange);
39
+ }
40
+ let retryInterval = DefaultRetryInterval;
41
+ let retryTimer = 0;
42
+ function dispose() {
43
+ document.removeEventListener('visibilitychange', onVisibilityChange);
44
+ window.clearTimeout(retryTimer);
45
+ // Only abort if not already aborted to avoid unnecessary DOMException logs
46
+ if (!curRequestController.signal.aborted) {
47
+ curRequestController.abort();
48
+ }
49
+ }
50
+ // if the incoming signal aborts, dispose resources and resolve:
51
+ inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.addEventListener('abort', () => {
52
+ dispose();
53
+ resolve(); // don't waste time constructing/logging errors
54
+ });
55
+ const fetch = inputFetch !== null && inputFetch !== void 0 ? inputFetch : window.fetch;
56
+ const onopen = inputOnOpen !== null && inputOnOpen !== void 0 ? inputOnOpen : defaultOnOpen;
57
+ async function create() {
58
+ var _a;
59
+ // Get fresh headers on each attempt (supports dynamic headers for token refresh)
60
+ const headers = getHeaders();
61
+ if (!headers.accept) {
62
+ headers.accept = EventStreamContentType;
63
+ }
64
+ // Add last-event-id if we have one from a previous connection
65
+ if (lastEventId) {
66
+ headers[LastEventId] = lastEventId;
67
+ }
68
+ // Store controller in local scope to avoid race condition during rapid tab switches.
69
+ // Without this, the catch block might check a different controller's abort status.
70
+ const currentController = new AbortController();
71
+ curRequestController = currentController;
72
+ try {
73
+ const response = await fetch(input, Object.assign(Object.assign({}, rest), { headers, signal: currentController.signal }));
74
+ await onopen(response);
75
+ await getBytes(response.body, getLines(getMessages(id => {
76
+ if (id) {
77
+ // store the id and send it back on the next retry:
78
+ lastEventId = id;
79
+ }
80
+ else {
81
+ // don't send the last-event-id header anymore:
82
+ lastEventId = undefined;
83
+ }
84
+ }, retry => {
85
+ retryInterval = retry;
86
+ }, onmessage)));
87
+ onclose === null || onclose === void 0 ? void 0 : onclose();
88
+ dispose();
89
+ resolve();
90
+ }
91
+ catch (err) {
92
+ if (!currentController.signal.aborted) {
93
+ // if we haven't aborted the request ourselves:
94
+ try {
95
+ // check if we need to retry:
96
+ const interval = (_a = onerror === null || onerror === void 0 ? void 0 : onerror(err)) !== null && _a !== void 0 ? _a : retryInterval;
97
+ window.clearTimeout(retryTimer);
98
+ // if user aborted during onerror callback, stop retrying:
99
+ if (inputSignal === null || inputSignal === void 0 ? void 0 : inputSignal.aborted) {
100
+ dispose();
101
+ resolve();
102
+ return;
103
+ }
104
+ // if page is hidden and openWhenHidden is false, don't retry now.
105
+ // onVisibilityChange will call create() when page becomes visible.
106
+ if (!openWhenHidden && document.hidden) {
107
+ return;
108
+ }
109
+ retryTimer = window.setTimeout(create, interval);
110
+ }
111
+ catch (innerErr) {
112
+ // we should not retry anymore:
113
+ dispose();
114
+ reject(innerErr);
115
+ }
116
+ }
117
+ }
118
+ }
119
+ create();
120
+ });
121
+ }
122
+ function defaultOnOpen(response) {
123
+ if (!response.ok) {
124
+ throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);
125
+ }
126
+ const contentType = response.headers.get('content-type');
127
+ if (!(contentType === null || contentType === void 0 ? void 0 : contentType.startsWith(EventStreamContentType))) {
128
+ throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`);
129
+ }
130
+ }
131
+ //# sourceMapp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AC,UAAU,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;oBACrD,CAAC;oBAAC,OAAO,QAAQ,EAAE,CAAC;wBAChB,+BAA+B;wBAC/B,OAAO,EAAE,CAAC;wBACV,MAAM,CAAC,QAAQ,CAAC,CAAC;oBACrB,CAAC;gBACL,CAAC;YACL,CAAC;QACL,CAAC;QAED,MAAM,EAAE,CAAC;IACb,CAAC,CAAC,CAAC;AACP,CAAC;AAED,SAAS,aAAa,CAAC,QAAkB;IACrC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,8BAA8B,QAAQ,CAAC,MAAM,KAAK,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;IAC7F,CAAC;IACD,MAAM,WAAW,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IACzD,IAAI,CAAC,CAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,UAAU,CAAC,sBAAsB,CAAC,CAAA,EAAE,CAAC;QACnD,MAAM,IAAI,KAAK,CAAC,+BAA+B,sBAAsB,aAAa,WAAW,EAAE,CAAC,CAAC;IACrG,CAAC;AACL,CAAC","sourcesContent":["import { EventSourceMessage, getBytes, getLines, getMessages } from './parse.js';\r\n\r\nexport const EventStreamContentType = 'text/event-stream';\r\n\r\nconst DefaultRetryInterval = 1000;\r\nconst LastEventId = 'last-event-id';\r\n\r\nexport interface FetchEventSourceInit extends Omit<RequestInit, 'headers'> {\r\n /**\r\n * The request headers. FetchEventSource only supports the Record<string,string> format,\r\n * or a function that returns headers. Using a function allows dynamic headers that are\r\n * refreshed on each retry, useful for updating Authorization tokens.\r\n */\r\n headers?: Record<string, string> | (() => Record<string, string>),\r\n\r\n /**\r\n * Called when a response is received. Use this to validate that the response\r\n * actually matches what you expect (and throw if it doesn't.) If not provided,\r\n * will default to a basic validation to ensure the content-type is text/event-stream.\r\n */\r\n onopen?: (response: Response) => Promise<void>,\r\n\r\n /**\r\n * Called when a message is received. NOTE: Unlike the default browser\r\n * EventSource.onmessage, this callback is called for _all_ events,\r\n * even ones with a custom `event` field.\r\n */\r\n onmessage?: (ev: EventSourceMessage) => void;\r\n\r\n /**\r\n * Called when a response finishes. If you don't expect the server to kill\r\n * the connection, you can throw an exception here and retry using onerror.\r\n */\r\n onclose?: () => void;\r\n\r\n /**\r\n * Called when there is any error making the request / processing messages /\r\n * handling callbacks etc. Use this to control the retry strategy: if the\r\n * error is fatal, rethrow the error inside the callback to stop the entire\r\n * operation. Otherwise, you can return an interval (in milliseconds) after\r\n * which the request will automatically retry (with the last-event-id).\r\n * If this callback is not specified, or it returns undefined, fetchEventSource\r\n * will treat every error as retriable and will try again after 1 second.\r\n */\r\n onerror?: (err: any) => number | null | undefined | void,\r\n\r\n /**\r\n * If true, will keep the request open even if the document is hidden.\r\n * By default, fetchEventSource will close the request and reopen it\r\n * automatically when the document becomes visible again.\r\n */\r\n openWhenHidden?: boolean;\r\n\r\n /** The Fetch function to use. Defaults to window.fetch */\r\n fetch?: typeof fetch;\r\n}\r\n\r\nexport function fetchEventSource(input: RequestInfo, {\r\n signal: inputSignal,\r\n headers: inputHeaders,\r\n onopen: inputOnOpen,\r\n onmessage,\r\n onclose,\r\n onerror,\r\n openWhenHidden,\r\n fetch: inputFetch,\r\n ...rest\r\n}: FetchEventSourceInit) {\r\n // Create a function to get headers, supporting both static and dynamic headers\r\n const getHeaders: () => Record<string, string> = typeof inputHeaders === 'function'\r\n ? inputHeaders\r\n : () => ({ ...(inputHeaders ?? {}) });\r\n\r\n return new Promise<void>((resolve, reject) => {\r\n // Handle already-aborted signal immediately to match native fetch behavior\r\n if (inputSignal?.aborted) {\r\n resolve();\r\n return;\r\n }\r\n\r\n // Track last-event-id separately so it persists across retries even with dynamic headers\r\n let lastEventId: string | undefined;\r\n\r\n let curRequestController: AbortController;\r\n function onVisibilityChange() {\r\n curRequestController.abort(); // close existing request on every visibility change\r\n if (!document.hidden) {\r\n create(); // page is now visible again, recreate request.\r\n }\r\n }\r\n\r\n if (!openWhenHidden) {\r\n document.addEventListener('visibilitychange', onVisibilityChange);\r\n }\r\n\r\n let retryInterval = DefaultRetryInterval;\r\n let retryTimer = 0;\r\n function dispose() {\r\n document.removeEventListener('visibilitychange', onVisibilityChange);\r\n window.clearTimeout(retryTimer);\r\n // Only abort if not already aborted to avoid unnecessary DOMException logs\r\n if (!curRequestController.signal.aborted) {\r\n curRequestController.abort();\r\n }\r\n }\r\n\r\n // if the incoming signal aborts, dispose resources and resolve:\r\n inputSignal?.addEventListener('abort', () => {\r\n dispose();\r\n resolve(); // don't waste time constructing/logging errors\r\n });\r\n\r\n const fetch = inputFetch ?? window.fetch;\r\n const onopen = inputOnOpen ?? defaultOnOpen;\r\n async function create() {\r\n // Get fresh headers on each attempt (supports dynamic headers for token refresh)\r\n const headers = getHeaders();\r\n if (!headers.accept) {\r\n headers.accept = EventStreamContentType;\r\n }\r\n // Add last-event-id if we have one from a previous connection\r\n if (lastEventId) {\r\n headers[LastEventId] = lastEventId;\r\n }\r\n\r\n // Store controller in local scope to avoid race condition during rapid tab switches.\r\n // Without this, the catch block might check a different controller's abort status.\r\n const currentController = new AbortController();\r\n curRequestController = currentController;\r\n try {\r\n const response = await fetch(input, {\r\n ...rest,\r\n headers,\r\n signal: currentController.signal,\r\n });\r\n\r\n await onopen(response);\r\n\r\n await getBytes(response.body!, getLines(getMessages(id => {\r\n if (id) {\r\n // store the id and send it back on the next retry:\r\n lastEventId = id;\r\n } else {\r\n // don't send the last-event-id header anymore:\r\n lastEventId = undefined;\r\n }\r\n }, retry => {\r\n retryInterval = retry;\r\n }, onmessage)));\r\n\r\n onclose?.();\r\n dispose();\r\n resolve();\r\n } catch (err) {\r\n if (!currentController.signal.aborted) {\r\n // if we haven't aborted the request ourselves:\r\n try {\r\n // check if we need to retry:\r\n const interval: any = onerror?.(err) ?? retryInterval;\r\n window.clearTimeout(retryTimer);\r\n // if user aborted during onerror callback, stop retrying:\r\n if (inputSignal?.aborted) {\r\n dispose();\r\n resolve();\r\n return;\r\n }\r\n // if page is hidden and openWhenHidden is false, don't retry now.\r\n // onVisibilityChange will call create() when page becomes visible.\r\n if (!openWhenHidden && document.hidden) {\r\n return;\r\n }\r\n retryTimer = window.setTimeout(create, interval);\r\n } catch (innerErr) {\r\n // we should not retry anymore:\r\n dispose();\r\n reject(innerErr);\r\n }\r\n }\r\n }\r\n }\r\n\r\n create();\r\n });\r\n}\r\n\r\nfunction defaultOnOpen(response: Response) {\r\n if (!response.ok) {\r\n throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);\r\n }\r\n const contentType = response.headers.get('content-type');\r\n if (!contentType?.startsWith(EventStreamContentType)) {\r\n throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`);\r\n }\r\n}\r\n"]}
@@ -0,0 +1,2 @@
1
+ export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch.js';
2
+ export { EventSourceMessage } from './parse.js';
@@ -0,0 +1,2 @@
1
+ export { fetchEventSource, EventStreamContentType } from './fetch.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAwB,sBAAsB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch.js';\r\nexport { EventSourceMessage } from './parse.js';\r\n"]}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Represents a message sent in an event stream
3
+ * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
4
+ */
5
+ export interface EventSourceMessage {
6
+ /** The event ID to set the EventSource object's last event ID value. */
7
+ id: string;
8
+ /** A string identifying the type of event described. */
9
+ event: string;
10
+ /** The event data */
11
+ data: string;
12
+ /** The reconnection interval (in milliseconds) to wait before retrying the connection */
13
+ retry?: number;
14
+ }
15
+ /**
16
+ * Converts a ReadableStream into a callback pattern.
17
+ * @param stream The input ReadableStream.
18
+ * @param onChunk A function that will be called on each new byte chunk in the stream.
19
+ * @returns {Promise<void>} A promise that will be resolved when the stream closes.
20
+ */
21
+ export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void): Promise<void>;
22
+ /**
23
+ * Parses arbitary byte chunks into EventSource line buffers.
24
+ * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
25
+ * @param onLine A function that will be called on each new EventSource line.
26
+ * @returns A function that should be called for each incoming byte chunk.
27
+ */
28
+ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number) => void): (arr: Uint8Array) => void;
29
+ /**
30
+ * Parses line buffers into EventSourceMessages.
31
+ * @param onId A function that will be called on each `id` field.
32
+ * @param onRetry A function that will be called on each `retry` field.
33
+ * @param onMessage A function that will be called on each message.
34
+ * @returns A function that should be called for each incoming line buffer.
35
+ */
36
+ export declare function getMessages(onId: (id: string) => void, onRetry: (retry: number) => void, onMessage?: (msg: EventSourceMessage) => void): (line: Uint8Array, fieldLength: number) => void;
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Converts a ReadableStream into a callback pattern.
3
+ * @param stream The input ReadableStream.
4
+ * @param onChunk A function that will be called on each new byte chunk in the stream.
5
+ * @returns {Promise<void>} A promise that will be resolved when the stream closes.
6
+ */
7
+ export async function getBytes(stream, onChunk) {
8
+ const reader = stream.getReader();
9
+ let result;
10
+ while (!(result = await reader.read()).done) {
11
+ onChunk(result.value);
12
+ }
13
+ }
14
+ /**
15
+ * Parses arbitary byte chunks into EventSource line buffers.
16
+ * Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
17
+ * @param onLine A function that will be called on each new EventSource line.
18
+ * @returns A function that should be called for each incoming byte chunk.
19
+ */
20
+ export function getLines(onLine) {
21
+ let buffer;
22
+ let position; // current read position
23
+ let fieldLength; // length of the `field` portion of the line
24
+ let discardTrailingNewline = false;
25
+ // return a function that can process each incoming byte chunk:
26
+ return function onChunk(arr) {
27
+ if (buffer === undefined) {
28
+ buffer = arr;
29
+ position = 0;
30
+ fieldLength = -1;
31
+ }
32
+ else {
33
+ // we're still parsing the old line. Append the new bytes into buffer:
34
+ buffer = concat(buffer, arr);
35
+ }
36
+ const bufLength = buffer.length;
37
+ let lineStart = 0; // index where the current line starts
38
+ while (position < bufLength) {
39
+ if (discardTrailingNewline) {
40
+ if (buffer[position] === 10 /* ControlChars.NewLine */) {
41
+ lineStart = ++position; // skip to next char
42
+ }
43
+ discardTrailingNewline = false;
44
+ }
45
+ // start looking forward till the end of line:
46
+ let lineEnd = -1; // index of the \r or \n char
47
+ for (; position < bufLength && lineEnd === -1; ++position) {
48
+ switch (buffer[position]) {
49
+ case 58 /* ControlChars.Colon */:
50
+ if (fieldLength === -1) { // first colon in line
51
+ fieldLength = position - lineStart;
52
+ }
53
+ break;
54
+ // @ts-ignore:7029 \r case below should fallthrough to \n:
55
+ case 13 /* ControlChars.CarriageReturn */:
56
+ discardTrailingNewline = true;
57
+ case 10 /* ControlChars.NewLine */:
58
+ lineEnd = position;
59
+ break;
60
+ }
61
+ }
62
+ if (lineEnd === -1) {
63
+ // We reached the end of the buffer but the line hasn't ended.
64
+ // Wait for the next arr and then continue parsing:
65
+ break;
66
+ }
67
+ // we've reached the line end, send it out:
68
+ onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
69
+ lineStart = position; // we're now on the next line
70
+ fieldLength = -1;
71
+ }
72
+ if (lineStart === bufLength) {
73
+ buffer = undefined; // we've finished reading it
74
+ }
75
+ else if (lineStart !== 0) {
76
+ // Create a new view into buffer beginning at lineStart so we don't
77
+ // need to copy over the previous lines when we get the new arr:
78
+ buffer = buffer.subarray(lineStart);
79
+ position -= lineStart;
80
+ }
81
+ };
82
+ }
83
+ /**
84
+ * Parses line buffers into EventSourceMessages.
85
+ * @param onId A function that will be called on each `id` field.
86
+ * @param onRetry A function that will be called on each `retry` field.
87
+ * @param onMessage A function that will be called on each message.
88
+ * @returns A function that should be called for each incoming line buffer.
89
+ */
90
+ export function getMessages(onId, onRetry, onMessage) {
91
+ let message = newMessage();
92
+ const decoder = new TextDecoder();
93
+ // return a function that can process each incoming line buffer:
94
+ return function onLine(line, fieldLength) {
95
+ if (line.length === 0) {
96
+ // empty line denotes end of message. Trigger the callback and start a new message:
97
+ // per spec: "If the data buffer's last character is a U+000A LINE FEED (LF) character,
98
+ // then remove the last character from the data buffer."
99
+ if (message.data.endsWith('\n')) {
100
+ message.data = message.data.slice(0, -1);
101
+ }
102
+ // per spec, only dispatch if data is not empty (ignore comment-only messages)
103
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage
104
+ if (message.data) {
105
+ onMessage === null || onMessage === void 0 ? void 0 : onMessage(message);
106
+ }
107
+ message = newMessage();
108
+ }
109
+ else if (fieldLength > 0) { // exclude comments and lines with no values
110
+ // line is of format "<field>:<value>" or "<field>: <value>"
111
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
112
+ const field = decoder.decode(line.subarray(0, fieldLength));
113
+ const valueOffset = fieldLength + (line[fieldLength + 1] === 32 /* ControlChars.Space */ ? 2 : 1);
114
+ const value = decoder.decode(line.subarray(valueOffset));
115
+ switch (field) {
116
+ case 'data':
117
+ // per spec: "Append the field value to the data buffer,
118
+ // then append a single U+000A LINE FEED (LF) character to the data buffer."
119
+ message.data += value + '\n';
120
+ break;
121
+ case 'event':
122
+ message.event = value;
123
+ break;
124
+ case 'id':
125
+ onId(message.id = value);
126
+ break;
127
+ case 'retry':
128
+ const retry = parseInt(value, 10);
129
+ if (!isNaN(retry)) { // per spec, ignore non-integers
130
+ onRetry(message.retry = retry);
131
+ }
132
+ break;
133
+ }
134
+ }
135
+ };
136
+ }
137
+ function concat(a, b) {
138
+ const res = new Uint8Array(a.length + b.length);
139
+ res.set(a);
140
+ res.set(b, a.length);
141
+ return res;
142
+ }
143
+ function newMessage() {
144
+ // data, event, and id must be initialized to empty strings:
145
+ // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
146
+ // retry should be initialized to undefined so we return a consistent shape
147
+ // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways
148
+ return {
149
+ data: '',
150
+ event: '',
151
+ id: '',
152
+ retry: undefined,
153
+ };
154
+ }
155
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../../src/parse.ts"],"names":[],"mappings":"AAeA;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAkC,EAAE,OAAkC;IACjG,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAC;IAClC,IAAI,MAA4C,CAAC;IACjD,OAAO,CAAC,CAAC,MAAM,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAC1C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;AACL,CAAC;AASD;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAuD;IAC5E,IAAI,MAA8B,CAAC;IACnC,IAAI,QAAgB,CAAC,CAAC,wBAAwB;IAC9C,IAAI,WAAmB,CAAC,CAAC,4CAA4C;IACrE,IAAI,sBAAsB,GAAG,KAAK,CAAC;IAEnC,+DAA+D;IAC/D,OAAO,SAAS,OAAO,CAAC,GAAe;QACnC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACvB,MAAM,GAAG,GAAG,CAAC;YACb,QAAQ,GAAG,CAAC,CAAC;YACb,WAAW,GAAG,CAAC,CAAC,CAAC;QACrB,CAAC;aAAM,CAAC;YACJ,sEAAsE;YACtE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC;QAChC,IAAI,SAAS,GAAG,CAAC,CAAC,CAAC,sCAAsC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AC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC;YAEzD,QAAQ,KAAK,EAAE,CAAC;gBACZ,KAAK,MAAM;oBACP,wDAAwD;oBACxD,4EAA4E;oBAC5E,OAAO,CAAC,IAAI,IAAI,KAAK,GAAG,IAAI,CAAC;oBAC7B,MAAM;gBACV,KAAK,OAAO;oBACR,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC;oBACtB,MAAM;gBACV,KAAK,IAAI;oBACL,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC;oBACzB,MAAM;gBACV,KAAK,OAAO;oBACR,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;oBAClC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,gCAAgC;wBACjD,OAAO,CAAC,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;oBACnC,CAAC;oBACD,MAAM;YACd,CAAC;QACL,CAAC;IACL,CAAC,CAAA;AACL,CAAC;AAED,SAAS,MAAM,CAAC,CAAa,EAAE,CAAa;IACxC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC;IAChD,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACX,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IACrB,OAAO,GAAG,CAAC;AACf,CAAC;AAED,SAAS,UAAU;IACf,4DAA4D;IAC5D,6FAA6F;IAC7F,2EAA2E;IAC3E,qFAAqF;IACrF,OAAO;QACH,IAAI,EAAE,EAAE;QACR,KAAK,EAAE,EAAE;QACT,EAAE,EAAE,EAAE;QACN,KAAK,EAAE,SAAS;KACnB,CAAC;AACN,CAAC","sourcesContent":["/**\r\n * Represents a message sent in an event stream\r\n * https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format\r\n */\r\nexport interface EventSourceMessage {\r\n /** The event ID to set the EventSource object's last event ID value. */\r\n id: string;\r\n /** A string identifying the type of event described. */\r\n event: string;\r\n /** The event data */\r\n data: string;\r\n /** The reconnection interval (in milliseconds) to wait before retrying the connection */\r\n retry?: number;\r\n}\r\n\r\n/**\r\n * Converts a ReadableStream into a callback pattern.\r\n * @param stream The input ReadableStream.\r\n * @param onChunk A function that will be called on each new byte chunk in the stream.\r\n * @returns {Promise<void>} A promise that will be resolved when the stream closes.\r\n */\r\nexport async function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array) => void) {\r\n const reader = stream.getReader();\r\n let result: ReadableStreamReadResult<Uint8Array>;\r\n while (!(result = await reader.read()).done) {\r\n onChunk(result.value);\r\n }\r\n}\r\n\r\nconst enum ControlChars {\r\n NewLine = 10,\r\n CarriageReturn = 13,\r\n Space = 32,\r\n Colon = 58,\r\n}\r\n\r\n/** \r\n * Parses arbitary byte chunks into EventSource line buffers.\r\n * Each line should be of the format \"field: value\" and ends with \\r, \\n, or \\r\\n. \r\n * @param onLine A function that will be called on each new EventSource line.\r\n * @returns A function that should be called for each incoming byte chunk.\r\n */\r\nexport function getLines(onLine: (line: Uint8Array, fieldLength: number) => void) {\r\n let buffer: Uint8Array | undefined;\r\n let position: number; // current read position\r\n let fieldLength: number; // length of the `field` portion of the line\r\n let discardTrailingNewline = false;\r\n\r\n // return a function that can process each incoming byte chunk:\r\n return function onChunk(arr: Uint8Array) {\r\n if (buffer === undefined) {\r\n buffer = arr;\r\n position = 0;\r\n fieldLength = -1;\r\n } else {\r\n // we're still parsing the old line. Append the new bytes into buffer:\r\n buffer = concat(buffer, arr);\r\n }\r\n\r\n const bufLength = buffer.length;\r\n let lineStart = 0; // index where the current line starts\r\n while (position < bufLength) {\r\n if (discardTrailingNewline) {\r\n if (buffer[position] === ControlChars.NewLine) {\r\n lineStart = ++position; // skip to next char\r\n }\r\n \r\n discardTrailingNewline = false;\r\n }\r\n \r\n // start looking forward till the end of line:\r\n let lineEnd = -1; // index of the \\r or \\n char\r\n for (; position < bufLength && lineEnd === -1; ++position) {\r\n switch (buffer[position]) {\r\n case ControlChars.Colon:\r\n if (fieldLength === -1) { // first colon in line\r\n fieldLength = position - lineStart;\r\n }\r\n break;\r\n // @ts-ignore:7029 \\r case below should fallthrough to \\n:\r\n case ControlChars.CarriageReturn:\r\n discardTrailingNewline = true;\r\n case ControlChars.NewLine:\r\n lineEnd = position;\r\n break;\r\n }\r\n }\r\n\r\n if (lineEnd === -1) {\r\n // We reached the end of the buffer but the line hasn't ended.\r\n // Wait for the next arr and then continue parsing:\r\n break;\r\n }\r\n\r\n // we've reached the line end, send it out:\r\n onLine(buffer.subarray(lineStart, lineEnd), fieldLength);\r\n lineStart = position; // we're now on the next line\r\n fieldLength = -1;\r\n }\r\n\r\n if (lineStart === bufLength) {\r\n buffer = undefined; // we've finished reading it\r\n } else if (lineStart !== 0) {\r\n // Create a new view into buffer beginning at lineStart so we don't\r\n // need to copy over the previous lines when we get the new arr:\r\n buffer = buffer.subarray(lineStart);\r\n position -= lineStart;\r\n }\r\n }\r\n}\r\n\r\n/** \r\n * Parses line buffers into EventSourceMessages.\r\n * @param onId A function that will be called on each `id` field.\r\n * @param onRetry A function that will be called on each `retry` field.\r\n * @param onMessage A function that will be called on each message.\r\n * @returns A function that should be called for each incoming line buffer.\r\n */\r\nexport function getMessages(\r\n onId: (id: string) => void,\r\n onRetry: (retry: number) => void,\r\n onMessage?: (msg: EventSourceMessage) => void\r\n) {\r\n let message = newMessage();\r\n const decoder = new TextDecoder();\r\n\r\n // return a function that can process each incoming line buffer:\r\n return function onLine(line: Uint8Array, fieldLength: number) {\r\n if (line.length === 0) {\r\n // empty line denotes end of message. Trigger the callback and start a new message:\r\n // per spec: \"If the data buffer's last character is a U+000A LINE FEED (LF) character,\r\n // then remove the last character from the data buffer.\"\r\n if (message.data.endsWith('\\n')) {\r\n message.data = message.data.slice(0, -1);\r\n }\r\n // per spec, only dispatch if data is not empty (ignore comment-only messages)\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessage\r\n if (message.data) {\r\n onMessage?.(message);\r\n }\r\n message = newMessage();\r\n } else if (fieldLength > 0) { // exclude comments and lines with no values\r\n // line is of format \"<field>:<value>\" or \"<field>: <value>\"\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\r\n const field = decoder.decode(line.subarray(0, fieldLength));\r\n const valueOffset = fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);\r\n const value = decoder.decode(line.subarray(valueOffset));\r\n\r\n switch (field) {\r\n case 'data':\r\n // per spec: \"Append the field value to the data buffer,\r\n // then append a single U+000A LINE FEED (LF) character to the data buffer.\"\r\n message.data += value + '\\n';\r\n break;\r\n case 'event':\r\n message.event = value;\r\n break;\r\n case 'id':\r\n onId(message.id = value);\r\n break;\r\n case 'retry':\r\n const retry = parseInt(value, 10);\r\n if (!isNaN(retry)) { // per spec, ignore non-integers\r\n onRetry(message.retry = retry);\r\n }\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n\r\nfunction concat(a: Uint8Array, b: Uint8Array) {\r\n const res = new Uint8Array(a.length + b.length);\r\n res.set(a);\r\n res.set(b, a.length);\r\n return res;\r\n}\r\n\r\nfunction newMessage(): EventSourceMessage {\r\n // data, event, and id must be initialized to empty strings:\r\n // https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation\r\n // retry should be initialized to undefined so we return a consistent shape\r\n // to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways\r\n return {\r\n data: '',\r\n event: '',\r\n id: '',\r\n retry: undefined,\r\n };\r\n}\r\n"]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@rayjp2010/fetch-event-source",
3
+ "version": "3.0.0",
4
+ "description": "A better API for making Event Source requests, with all the features of fetch()",
5
+ "keywords": [
6
+ "sse",
7
+ "server-sent-events",
8
+ "eventsource",
9
+ "fetch",
10
+ "streaming",
11
+ "event-stream"
12
+ ],
13
+ "homepage": "https://github.com/rayjp2010/fetch-event-source#readme",
14
+ "repository": "github:rayjp2010/fetch-event-source",
15
+ "bugs": {
16
+ "url": "https://github.com/rayjp2010/fetch-event-source/issues"
17
+ },
18
+ "author": "rayjp2010",
19
+ "license": "MIT",
20
+ "main": "lib/cjs/index.js",
21
+ "module": "lib/esm/index.js",
22
+ "types": "lib/cjs/index.d.ts",
23
+ "sideEffects": false,
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "devDependencies": {
28
+ "@types/jest": "^30.0.0",
29
+ "@types/node": "^25.1.0",
30
+ "jest": "^30.2.0",
31
+ "rimraf": "^6.1.2",
32
+ "source-map-support": "^0.5.21",
33
+ "ts-jest": "^29.4.6",
34
+ "typescript": "^5.9.3"
35
+ },
36
+ "scripts": {
37
+ "clean": "rimraf ./lib ./coverage",
38
+ "prebuild": "pnpm run clean",
39
+ "build": "tsc && tsc -p tsconfig.esm.json",
40
+ "test": "jest",
41
+ "test:coverage": "jest --coverage"
42
+ }
43
+ }