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 9fb0c05
Show file tree
Hide file tree
Showing 3 changed files with 23 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;
}
37 changes: 11 additions & 26 deletions apps/js-sdk/firecrawl/src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -941,39 +946,21 @@ 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;
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,
Expand Down Expand Up @@ -1070,8 +1057,6 @@ export class CrawlWatcher extends TypedEventTarget<CrawlWatcherEvents> {
}

close() {
this.initialized.then(() => {
this.ws.close();
});
this.ws.close();
}
}
7 changes: 7 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,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;"`,
};
},
});

0 comments on commit 9fb0c05

Please sign in to comment.