@@ -4,10 +4,12 @@ import { logger } from "@/client/log";
44// Global singleton promise that will be reused for subsequent calls
55let eventSourcePromise : Promise < typeof EventSource > | null = null ;
66
7- /**
8- * Import `eventsource` from the custom `eventsource` library. We need a custom implemnetation since we need to attach our own custom headers to the request.
9- **/
107export async function importEventSource ( ) : Promise < typeof EventSource > {
8+ // IMPORTANT: Import `eventsource` from the custom `eventsource` library. We need a custom implementation
9+ // since we need to attach our own custom headers to the request.
10+ //
11+ // We can't use the browser-provided EventSource since it does not allow providing custom headers.
12+
1113 // Return existing promise if we already started loading
1214 if ( eventSourcePromise !== null ) {
1315 return eventSourcePromise ;
@@ -19,7 +21,8 @@ export async function importEventSource(): Promise<typeof EventSource> {
1921
2022 // Node.js environment
2123 try {
22- const es = await import ( "eventsource" ) ;
24+ const moduleName = "eventsource" ;
25+ const es = await import ( /* webpackIgnore: true */ moduleName ) ;
2326 _EventSource = es . EventSource ;
2427 logger ( ) . debug ( "using eventsource from npm" ) ;
2528 } catch ( err ) {
@@ -39,42 +42,3 @@ export async function importEventSource(): Promise<typeof EventSource> {
3942
4043 return eventSourcePromise ;
4144}
42-
43- //export async function importEventSource(): Promise<typeof EventSource> {
44- // // Return existing promise if we already started loading
45- // if (eventSourcePromise !== null) {
46- // return eventSourcePromise;
47- // }
48- //
49- // // Create and store the promise
50- // eventSourcePromise = (async () => {
51- // let _EventSource: typeof EventSource;
52- //
53- // if (typeof EventSource !== "undefined") {
54- // // Browser environment
55- // _EventSource = EventSource;
56- // logger().debug("using native eventsource");
57- // } else {
58- // // Node.js environment
59- // try {
60- // const es = await import("eventsource");
61- // _EventSource = es.EventSource;
62- // logger().debug("using eventsource from npm");
63- // } catch (err) {
64- // // EventSource not available
65- // _EventSource = class MockEventSource {
66- // constructor() {
67- // throw new Error(
68- // 'EventSource support requires installing the "eventsource" peer dependency.',
69- // );
70- // }
71- // } as unknown as typeof EventSource;
72- // logger().debug("using mock eventsource");
73- // }
74- // }
75- //
76- // return _EventSource;
77- // })();
78- //
79- // return eventSourcePromise;
80- //}
0 commit comments