From 6002bf322872f1ad849bbecc0c26636e3d22b10f Mon Sep 17 00:00:00 2001 From: Thomas Kosmas Date: Thu, 19 Dec 2024 14:52:43 +0200 Subject: [PATCH] feat: dynamically import WebSocket module with error handling --- apps/js-sdk/firecrawl/src/index.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/js-sdk/firecrawl/src/index.ts b/apps/js-sdk/firecrawl/src/index.ts index 0d19ab60..7eef05f8 100644 --- a/apps/js-sdk/firecrawl/src/index.ts +++ b/apps/js-sdk/firecrawl/src/index.ts @@ -1,7 +1,24 @@ import axios, { type AxiosResponse, type AxiosRequestHeaders, AxiosError } from "axios"; import type * as zt from "zod"; import { zodToJsonSchema } from "zod-to-json-schema"; -import { WebSocket } from "isows"; + +import type { WebSocket as IsowsWebSocket } from 'isows'; +/** + * Dynamically imports the WebSocket class from 'isows'. + * If the import fails, WebSocket is set to null. + * This approach is used because some environments, such as Firebase Functions, + * might not support WebSocket natively. + */ +const WebSocket: typeof IsowsWebSocket | null = await (async () => { + try { + const module = await import('isows'); + return module.WebSocket; + } catch (error) { + console.error("Failed to load 'isows' module:", error); + return null; + } +})(); + import { TypedEventTarget } from "typescript-event-target"; /** @@ -938,6 +955,8 @@ export class CrawlWatcher extends TypedEventTarget { constructor(id: string, app: FirecrawlApp) { super(); + 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 = [];