Compare commits

...

1 Commits

Author SHA1 Message Date
Jordan Bentley
967e7d2c10
Reported in Discord:
strin — 3:56 PM
Hi anyone...

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-21 18:30:35 -04:00
8 changed files with 95 additions and 3 deletions

View File

@ -22,7 +22,8 @@
"dotenv": "^16.4.7",
"electron-store": "^10.0.0",
"electron-updater": "^6.6.2",
"glob": "^11.0.0"
"glob": "^11.0.0",
"web-streams-polyfill": "^3.3.3"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.13",

View File

@ -1,3 +1,6 @@
// Load ReadableStream polyfill before any other imports
import './polyfills/readablestream';
import { app, BrowserWindow, ipcMain, shell } from 'electron';
import * as path from 'path';
import { TaskQueue } from './services/taskQueue';

View File

@ -0,0 +1,22 @@
# Polyfills
This directory contains polyfills needed for the Crystal application to run properly in different Node.js environments.
## ReadableStream Polyfill
The `readablestream.ts` file provides a polyfill for the Web Streams API (ReadableStream, WritableStream, TransformStream) which is required by the Claude Code SDK.
### Why is this needed?
The `@anthropic-ai/claude-code` SDK uses the ReadableStream API internally, but this API is not available in older Node.js versions or might not be globally available in some Electron contexts.
### How it works:
1. First, it checks if ReadableStream is already available globally
2. If not, it tries to use Node.js's built-in `stream/web` module (available in Node 16.5+)
3. If that fails, it falls back to the `web-streams-polyfill` package
4. The polyfill makes these APIs available globally for the Claude Code SDK to use
### Usage:
This polyfill is automatically loaded at the very beginning of the main process in `index.ts` before any other imports to ensure it's available when the Claude Code SDK is initialized.

View File

@ -0,0 +1,38 @@
/**
* ReadableStream polyfill for Node.js environments
* This ensures the Claude Code SDK has access to the ReadableStream API
*/
// Check if ReadableStream is already available
if (typeof globalThis.ReadableStream === 'undefined') {
try {
// Try to import from Node.js built-in stream/web module (Node 16.5+)
const { ReadableStream, WritableStream, TransformStream } = require('stream/web');
// Make them available globally
globalThis.ReadableStream = ReadableStream;
globalThis.WritableStream = WritableStream;
globalThis.TransformStream = TransformStream;
console.log('[Polyfill] Using Node.js built-in ReadableStream from stream/web');
} catch (error) {
// If stream/web is not available, use the web-streams-polyfill package
try {
const streams = require('web-streams-polyfill/ponyfill');
globalThis.ReadableStream = streams.ReadableStream;
globalThis.WritableStream = streams.WritableStream;
globalThis.TransformStream = streams.TransformStream;
console.log('[Polyfill] Using web-streams-polyfill for ReadableStream');
} catch (polyfillError) {
console.error('[Polyfill] Failed to load ReadableStream polyfill:', polyfillError);
console.error('[Polyfill] The Claude Code SDK may not function properly without ReadableStream support');
}
}
} else {
console.log('[Polyfill] ReadableStream already available, skipping polyfill');
}
// Export for TypeScript typing if needed
export {};

15
main/src/types/global.d.ts vendored Normal file
View File

@ -0,0 +1,15 @@
/// <reference lib="dom" />
/**
* Global type declarations for ReadableStream and related Web Streams API
* This ensures TypeScript knows these are available globally after polyfill
*/
declare global {
// These types are imported from the DOM lib, but we re-declare them here
// to ensure they're available in the Node.js context after polyfilling
var ReadableStream: typeof globalThis.ReadableStream;
var WritableStream: typeof globalThis.WritableStream;
var TransformStream: typeof globalThis.TransformStream;
}
export {};

View File

@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"lib": ["ES2022"],
"lib": ["ES2022", "DOM"],
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,

View File

@ -65,7 +65,8 @@
"dotenv": "^16.4.7",
"electron-updater": "^6.6.2",
"glob": "^11.0.0",
"openai": "^5.1.1"
"openai": "^5.1.1",
"web-streams-polyfill": "^3.3.3"
},
"devDependencies": {
"@playwright/test": "^1.52.0",

12
pnpm-lock.yaml generated
View File

@ -38,6 +38,9 @@ importers:
openai:
specifier: ^5.1.1
version: 5.10.1(zod@3.25.76)
web-streams-polyfill:
specifier: ^3.3.3
version: 3.3.3
devDependencies:
'@playwright/test':
specifier: ^1.52.0
@ -244,6 +247,9 @@ importers:
glob:
specifier: ^11.0.0
version: 11.0.3
web-streams-polyfill:
specifier: ^3.3.3
version: 3.3.3
devDependencies:
'@types/better-sqlite3':
specifier: ^7.6.13
@ -4634,6 +4640,10 @@ packages:
wcwidth@1.0.1:
resolution: {integrity: sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==}
web-streams-polyfill@3.3.3:
resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==}
engines: {node: '>= 8'}
when-exit@2.1.4:
resolution: {integrity: sha512-4rnvd3A1t16PWzrBUcSDZqcAmsUIy4minDXT/CZ8F2mVDgd65i4Aalimgz1aQkRGU0iH5eT5+6Rx2TK8o443Pg==}
@ -9678,6 +9688,8 @@ snapshots:
dependencies:
defaults: 1.0.4
web-streams-polyfill@3.3.3: {}
when-exit@2.1.4: {}
which@2.0.2: