Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(js-sdk): fix watcher duplicate events, start() Promise, and done event fields

Devin AIabeb2a7e3d0e
diff --git a/apps/js-sdk/firecrawl/src/v2/watcher.ts b/apps/js-sdk/firecrawl/src/v2/watcher.ts
index 4f456d681..fe4979a21 100644
--- a/apps/js-sdk/firecrawl/src/v2/watcher.ts
+++ b/apps/js-sdk/firecrawl/src/v2/watcher.ts
@@ -109,24 +109,37 @@ export class Watcher extends EventEmitter {
}
async start(): Promise<void> {
- try {
- const url = this.buildWsUrl();
- const wsCtor = await getWebSocketCtor();
- if (!wsCtor) {
- this.pollLoop();
- return;
- }
- this.ws = new wsCtor(url, this.http.getApiKey()) as any;
- if (this.ws && "binaryType" in this.ws) {
- (this.ws as any).binaryType = "arraybuffer";
- }
-
- if (this.ws) {
- this.attachWsHandlers(this.ws);
- }
- } catch (err) {
- this.pollLoop();
- }
+ return new Promise<void>((resolve, reject) => {
+ const onDone = () => { cleanup(); resolve(); };
+ const onError = (err: any) => { cleanup(); resolve(); };
+ const cleanup = () => {
+ this.removeListener("done", onDone);
+ this.removeListener("error", onError);
+ };
+ this.on("done", onDone);
+ this.on("error", onError);
+
+ (async () => {
+ try {
+ const url = this.buildWsUrl();
+ const wsCtor = await getWebSocketCtor();
+ if (!wsCtor) {
+ this.pollLoop();
+ return;
+ }
+ this.ws = new wsCtor(url, this.http.getApiKey()) as any;
+ if (this.ws && "binaryType" in this.ws) {
+ (this.ws as any).binaryType = "arraybuffer";
+ }
+
+ if (this.ws) {
+ this.attachWsHandlers(this.ws);
+ }
+ } catch (err) {
+ this.pollLoop();
+ }
+ })();
+ });
}
private attachWsHandlers(ws: WebSocket) {
@@ -150,14 +163,14 @@ export class Watcher extends EventEmitter {
}
if (type === "document") {
const doc = body.data;
- if (doc) this.emit("document", doc as Document & { id: string });
+ if (doc) this.emitDocuments([doc]);
return;
}
if (type === "done") {
const payload = body.data || body;
const data = (payload.data || []) as Document[];
if (data.length) this.emitDocuments(data);
- this.emit("done", { status: "completed", data, id: this.jobId });
+ this.emit("done", { status: "completed", data, id: this.jobId, total: payload.total, completed: payload.completed, creditsUsed: payload.creditsUsed });
this.close();
return;
}
@@ -227,7 +240,7 @@ export class Watcher extends EventEmitter {
};
this.emit("snapshot", snap);
if (["completed", "failed", "cancelled"].includes(status)) {
- this.emit("done", { status, data, id: this.jobId });
+ this.emit("done", { status, data, id: this.jobId, total: payload.total ?? 0, completed: payload.completed ?? 0, creditsUsed: payload.creditsUsed });
this.close();
}
}
@@ -243,7 +256,7 @@ export class Watcher extends EventEmitter {
this.emitDocuments((snap.data || []) as Document[]);
this.emit("snapshot", snap);
if (["completed", "failed", "cancelled"].includes(snap.status)) {
- this.emit("done", { status: snap.status, data: snap.data, id: this.jobId });
+ this.emit("done", { status: snap.status, data: snap.data, id: this.jobId, total: (snap as any).total ?? 0, completed: (snap as any).completed ?? 0, creditsUsed: (snap as any).creditsUsed });
this.close();
break;
}