Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 7 additions & 43 deletions packages/rivetkit/src/common/eventsource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { logger } from "@/client/log";
// Global singleton promise that will be reused for subsequent calls
let eventSourcePromise: Promise<typeof EventSource> | null = null;

/**
* Import `eventsource` from the custom `eventsource` library. We need a custom implemnetation since we need to attach our own custom headers to the request.
**/
export async function importEventSource(): Promise<typeof EventSource> {
// IMPORTANT: Import `eventsource` from the custom `eventsource` library. We need a custom implementation
// since we need to attach our own custom headers to the request.
//
// We can't use the browser-provided EventSource since it does not allow providing custom headers.

// Return existing promise if we already started loading
if (eventSourcePromise !== null) {
return eventSourcePromise;
Expand All @@ -19,7 +21,8 @@ export async function importEventSource(): Promise<typeof EventSource> {

// Node.js environment
try {
const es = await import("eventsource");
const moduleName = "eventsource";
const es = await import(/* webpackIgnore: true */ moduleName);
_EventSource = es.EventSource;
logger().debug("using eventsource from npm");
} catch (err) {
Expand All @@ -39,42 +42,3 @@ export async function importEventSource(): Promise<typeof EventSource> {

return eventSourcePromise;
}

//export async function importEventSource(): Promise<typeof EventSource> {
// // Return existing promise if we already started loading
// if (eventSourcePromise !== null) {
// return eventSourcePromise;
// }
//
// // Create and store the promise
// eventSourcePromise = (async () => {
// let _EventSource: typeof EventSource;
//
// if (typeof EventSource !== "undefined") {
// // Browser environment
// _EventSource = EventSource;
// logger().debug("using native eventsource");
// } else {
// // Node.js environment
// try {
// const es = await import("eventsource");
// _EventSource = es.EventSource;
// logger().debug("using eventsource from npm");
// } catch (err) {
// // EventSource not available
// _EventSource = class MockEventSource {
// constructor() {
// throw new Error(
// 'EventSource support requires installing the "eventsource" peer dependency.',
// );
// }
// } as unknown as typeof EventSource;
// logger().debug("using mock eventsource");
// }
// }
//
// return _EventSource;
// })();
//
// return eventSourcePromise;
//}
3 changes: 2 additions & 1 deletion packages/rivetkit/src/common/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function importWebSocket(): Promise<typeof WebSocket> {
} else {
// Node.js environment
try {
const ws = await import("ws");
const moduleName = "ws";
const ws = await import(/* webpackIgnore: true */ moduleName);
_WebSocket = ws.default as unknown as typeof WebSocket;
logger().debug("using websocket from npm");
} catch {
Expand Down
Loading