-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(js-sdk): Add CJS-compatible WebSocket loading #1021
Conversation
apps/js-sdk/firecrawl/src/index.ts
Outdated
return require('isows').WebSocket; | ||
} catch { | ||
try { | ||
return import('isows').then(m => m.WebSocket); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing an await here, right? Either here or on line 964.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that! I've updated the code to properly handle the WebSocket loading. Could you please review the changes again?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can cause a race condition. I'd prefer importing it lazily in an already-asynchronous function, like the watch crawl endpoint's function.
apps/js-sdk/firecrawl/src/index.ts
Outdated
return require('isows').WebSocket; | ||
} catch { | ||
try { | ||
return import('isows').then(m => m.WebSocket); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can cause a race condition. I'd prefer importing it lazily in an already-asynchronous function, like the watch crawl endpoint's function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the work you've put into this! I believe the best approach here would be to use top-level await for ESM, while keeping the current implementation for CJS. If you're open to merging this, it might be worth exploring how to configure tsup to use distinct implementations for ESM and CJS.
I really appreciate your efforts on this, and I'm happy to assist further if needed. Let me know how I can help!
apps/js-sdk/firecrawl/src/index.ts
Outdated
this.status = "scraping"; | ||
this.data = []; | ||
|
||
this.initialized = CrawlWatcher.loadWebSocket() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think this is good enough, cause ws is not initialized when the constructor is called but later async, so there can be a race condition.
9fb0c05
to
00f6d58
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of that approach, is there no way with tsup to have a different module for cjs and esm? Also we should take into account the the import of isows might fail if the platform doesnt have ws support.
00f6d58
to
67db6a1
Compare
Hi Rutam, we're going a different way to fix this, so I'm closing this PR. Thanks for the work. |
Fixes: #1011
The build passes for both ESM and CJS