Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js-sdk): Add CJS-compatible WebSocket loading #1021

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(js-sdk): WebSocket initialization handles promises correctly
RutamBhagat committed Dec 30, 2024
commit 15398cc80182bc946d8814e034e558d26d71e06e
16 changes: 14 additions & 2 deletions apps/js-sdk/firecrawl/src/index.ts
Original file line number Diff line number Diff line change
@@ -942,7 +942,7 @@ interface CrawlWatcherEvents {
}

export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
private ws: WebSocket;
private ws!: WebSocket;
public data: FirecrawlDocument<undefined>[];
public status: CrawlStatusResponse["status"];
public id: string;
@@ -963,10 +963,22 @@ export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
super();
const WS = CrawlWatcher.loadWebSocket();
this.id = id;
this.ws = new WS(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);

if (WS instanceof Promise) {
WS.then(WebSocketClass => {
this.ws = new WebSocketClass(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
this.initializeWebSocket();
});
} else {
this.ws = new WS(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
this.initializeWebSocket();
}

this.status = "scraping";
this.data = [];
}

private initializeWebSocket() {
type ErrorMessage = {
type: "error",
error: string,