diff --git a/apps/js-sdk/firecrawl/src/global.d.ts b/apps/js-sdk/firecrawl/src/global.d.ts new file mode 100644 index 000000000..bf9177ded --- /dev/null +++ b/apps/js-sdk/firecrawl/src/global.d.ts @@ -0,0 +1,5 @@ +export {}; + +declare global { + const __WEBSOCKET_LOADER__: string; +} \ No newline at end of file diff --git a/apps/js-sdk/firecrawl/src/index.ts b/apps/js-sdk/firecrawl/src/index.ts index cd405c5ec..048d7d19d 100644 --- a/apps/js-sdk/firecrawl/src/index.ts +++ b/apps/js-sdk/firecrawl/src/index.ts @@ -1,8 +1,13 @@ 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"; + +const loaderCode = __WEBSOCKET_LOADER__; +const WebSocket = new Function('return ' + loaderCode)()() as typeof IsowsWebSocket; + /** * Configuration interface for FirecrawlApp. * @param apiKey - Optional API key for authentication. @@ -941,39 +946,21 @@ interface CrawlWatcherEvents { } export class CrawlWatcher extends TypedEventTarget { - private ws!: WebSocket; - private initialized: Promise; + private ws: WebSocket; public data: FirecrawlDocument[]; 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; + if (!WebSocket) { + throw new FirecrawlError("WebSocket module failed to load. Your system might not support WebSocket.", 500); + } + 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, @@ -1070,8 +1057,6 @@ export class CrawlWatcher extends TypedEventTarget { } close() { - this.initialized.then(() => { - this.ws.close(); - }); + this.ws.close(); } } diff --git a/apps/js-sdk/firecrawl/tsup.config.ts b/apps/js-sdk/firecrawl/tsup.config.ts index b3b7e42d5..d2daa7761 100644 --- a/apps/js-sdk/firecrawl/tsup.config.ts +++ b/apps/js-sdk/firecrawl/tsup.config.ts @@ -6,4 +6,11 @@ export default defineConfig({ dts: true, outDir: "dist", clean: true, + esbuildOptions(options, context) { + options.define = { + __WEBSOCKET_LOADER__: context.format === 'cjs' + ? `"const { WebSocket } = require('isows'); export default WebSocket;"` + : `"import { WebSocket } from 'isows'; export default WebSocket;"`, + }; + }, }); \ No newline at end of file