Skip to content

Commit 956ed99

Browse files
committed
feat(upgrade): add node.js support
1 parent 31dcc2b commit 956ed99

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# CHANGELOG
22

33
## 3.0.0
4+
- Add node.js support
45
- Upgrade to TypeScript 4.9
56
- Add exports in package.json
67
- This package is now pure ESM. Please [read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"tsconfig.esm.json"
4141
],
4242
"engines": {
43-
"node": ">=14.16"
43+
"node": ">=18.12"
4444
},
4545
"scripts": {
4646
"clean": "rimraf ./lib ./coverage",

src/fetch.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface FetchEventSourceInit extends RequestInit {
4949
*/
5050
openWhenHidden?: boolean;
5151

52-
/** The Fetch function to use. Defaults to window.fetch */
52+
/** The Fetch function to use. Defaults to fetch */
5353
fetch?: typeof fetch;
5454
}
5555

@@ -79,15 +79,17 @@ export function fetchEventSource(input: RequestInfo, {
7979
}
8080
}
8181

82-
if (!openWhenHidden) {
82+
if (typeof window !== 'undefined' && !openWhenHidden) {
8383
document.addEventListener('visibilitychange', onVisibilityChange);
8484
}
8585

8686
let retryInterval = DefaultRetryInterval;
8787
let retryTimer = 0;
8888
function dispose() {
89-
document.removeEventListener('visibilitychange', onVisibilityChange);
90-
window.clearTimeout(retryTimer);
89+
if (typeof window !== 'undefined' && !openWhenHidden) {
90+
document.removeEventListener('visibilitychange', onVisibilityChange);
91+
}
92+
clearTimeout(retryTimer);
9193
curRequestController.abort();
9294
}
9395

@@ -97,12 +99,12 @@ export function fetchEventSource(input: RequestInfo, {
9799
resolve(); // don't waste time constructing/logging errors
98100
});
99101

100-
const fetch = inputFetch ?? window.fetch;
102+
const fetchFn = inputFetch ?? fetch;
101103
const onopen = inputOnOpen ?? defaultOnOpen;
102104
async function create() {
103105
curRequestController = new AbortController();
104106
try {
105-
const response = await fetch(input, {
107+
const response = await fetchFn(input, {
106108
...rest,
107109
headers,
108110
signal: curRequestController.signal,
@@ -131,8 +133,8 @@ export function fetchEventSource(input: RequestInfo, {
131133
try {
132134
// check if we need to retry:
133135
const interval: any = onerror?.(err) ?? retryInterval;
134-
window.clearTimeout(retryTimer);
135-
retryTimer = window.setTimeout(create, interval);
136+
clearTimeout(retryTimer);
137+
retryTimer = setTimeout(create, interval);
136138
} catch (innerErr) {
137139
// we should not retry anymore:
138140
dispose();

0 commit comments

Comments
 (0)