Skip to content

Commit

Permalink
fix(build): resolve CJS/ESM WebSocket import with build-time code inj…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
RutamBhagat committed Jan 6, 2025
1 parent 950ceb9 commit 67db6a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
5 changes: 5 additions & 0 deletions apps/js-sdk/firecrawl/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {};

declare global {
const __WEBSOCKET_LOADER__: string;
}
42 changes: 16 additions & 26 deletions apps/js-sdk/firecrawl/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import axios, { type AxiosResponse, type AxiosRequestHeaders, AxiosError } from "axios";
import type * as zt from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { WebSocket as IsowsWebSocket } from "isows";
import { TypedEventTarget } from "typescript-event-target";


let WebSocket: typeof IsowsWebSocket;

if (typeof __WEBSOCKET_LOADER__ === 'string') {
WebSocket = new Function('return ' + __WEBSOCKET_LOADER__)();
}

/**
* Configuration interface for FirecrawlApp.
* @param apiKey - Optional API key for authentication.
Expand Down Expand Up @@ -941,39 +949,23 @@ interface CrawlWatcherEvents {
}

export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
private ws!: WebSocket;
private initialized: Promise<void>;
private ws: WebSocket;
public data: FirecrawlDocument<undefined>[];
public status: CrawlStatusResponse["status"];
public id: string;

private static async loadWebSocket() {
try {
if (typeof require !== 'undefined') {
return require('isows').WebSocket;
} else {
const module = await import('isows');
return module.WebSocket;
}
} catch (e) {
throw new FirecrawlError('Failed to load WebSocket implementation', 500);
}
}

constructor(id: string, app: FirecrawlApp) {
super();
this.id = id;
console.log('typeof WebSocket', typeof WebSocket)
// @ts-ignore
if (typeof WebSocket === "function" && WebSocket.prototype.then) {
throw new Error("WebSocket behaves like a Promise, which is unexpected.");
}
this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
this.status = "scraping";
this.data = [];

this.initialized = CrawlWatcher.loadWebSocket()
.then((WebSocket) => {
this.ws = new WebSocket(`${app.apiUrl}/v1/crawl/${id}`, app.apiKey);
this.initializeWebSocket();
});
}

private initializeWebSocket() {
type ErrorMessage = {
type: "error",
error: string,
Expand Down Expand Up @@ -1070,8 +1062,6 @@ export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
}

close() {
this.initialized.then(() => {
this.ws.close();
});
this.ws.close();
}
}
9 changes: 9 additions & 0 deletions apps/js-sdk/firecrawl/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ export default defineConfig({
dts: true,
outDir: "dist",
clean: true,
esbuildOptions(options, context) {
options.define = {
__WEBSOCKET_LOADER__: JSON.stringify(
context.format === 'cjs'
? `const { WebSocket } = require('isows'); WebSocket`
: `import('isows').then(m => m.WebSocket)`
)
};
},
});

0 comments on commit 67db6a1

Please sign in to comment.