Skip to content

Commit 5575122

Browse files
committed
feat: declare fetch override support symbol on class
1 parent f138661 commit 5575122

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ Use a package like [`undici`](https://github.com/nodejs/undici) that supports HT
153153

154154
```ts
155155
// npm install undici --save
156-
import {Agent, fetch} from "undici";
156+
import {Agent, fetch} from 'undici'
157157

158158
const http2Dispatcher = new Agent({allowH2: true})
159159

160160
const es = new EventSource('https://my-server.com/sse', {
161-
fetch: (url, init) => fetch(url, { ...init, dispatcher: http2Dispatcher })
161+
fetch: (url, init) => fetch(url, {...init, dispatcher: http2Dispatcher}),
162162
})
163163
```
164164

@@ -181,6 +181,31 @@ await fetch('https://my-server.com/sse', {
181181
})
182182
```
183183

184+
#### Feature checking
185+
186+
For library authors, you may want to feature-check if an EventSource implementation supports passing a custom `fetch` implementation (such as this library does). We declare (from 4.1.0 and onwards) a non-enumerable symbol on the EventSource class to indicate this, named `eventsource.supports-fetch-override`. It can be used downstream such as:
187+
188+
```ts
189+
function yourLibrary(options) {
190+
const OurEventSource = options.polyfills.EventSource || globalThis.EventSource
191+
192+
if (OurEventSource && Symbol.for('eventsource.supports-fetch-override') in OurEventSource) {
193+
// We can safely assume this supports overriding/specifying `fetch`!
194+
const es = new OurEventSource('https://some.url', {
195+
fetch: (input, init) =>
196+
fetch(input, {
197+
...init,
198+
headers: {
199+
...(init.headers || {}),
200+
'x-some-header': 'foo',
201+
},
202+
}),
203+
})
204+
//
205+
}
206+
}
207+
```
208+
184209
## License
185210

186211
MIT-licensed. See [LICENSE](LICENSE).

src/EventSource.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,15 @@ export class EventSource extends EventTarget {
577577
}
578578
}
579579

580+
// Provides a way to detect that the EventSource implementation supports passing `fetch`
581+
// that can be used to customize the request, eg custom headers and similar.
582+
Object.defineProperty(EventSource, Symbol.for('eventsource.supports-fetch-override'), {
583+
value: true,
584+
writable: false,
585+
configurable: false,
586+
enumerable: false,
587+
})
588+
580589
/**
581590
* According to spec, when constructing a URL:
582591
* > 1. Let baseURL be environment's base URL, if environment is a Document object

test/tests.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,12 @@ export function registerTests(options: {
768768
await deferClose(es)
769769
})
770770

771+
test('has non-enumarable `eventsource.supports-fetch-override` symbol', async () => {
772+
const supportsFetchOverride = Symbol.for('eventsource.supports-fetch-override')
773+
expect(Object.getOwnPropertySymbols(OurEventSource).includes(supportsFetchOverride)).toBe(true)
774+
expect(Object.keys(OurEventSource).includes('supports-fetch-override')).toBe(false)
775+
expect(Symbol.for('eventsource.supports-fetch-override') in OurEventSource)
776+
})
777+
771778
return runner
772779
}

0 commit comments

Comments
 (0)