-
Notifications
You must be signed in to change notification settings - Fork 53
feat(cloudflare): add compatibility presets to Cloudflare Worker #526
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
Conversation
- Add `compatibility-presets.ts` with "node" preset for nodejs_compat flag - Add `compatibility` property to Worker interface accepting "node" preset - Union preset flags with user-provided compatibilityFlags - Update documentation with examples and usage guidance - Handle both regular workers and runtime workers properly Resolves #522 Co-authored-by: sam <[email protected]>
export type CompatibilityPreset = "node"; | ||
|
||
/** | ||
* Mapping of compatibility presets to their respective compatibility flags | ||
*/ | ||
export const COMPATIBILITY_PRESETS: Record<CompatibilityPreset, string[]> = { | ||
/** | ||
* Node.js compatibility preset | ||
* Enables Node.js APIs and runtime compatibility | ||
*/ | ||
node: ["nodejs_compat"], | ||
}; |
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.
@claude
This could be inferred instead
export type CompatibilityPreset = keyof typeof COMPATIBILITY_PRESETS;
export const COMPATIBILITY_PRESETS = {
node: ["nodejs_compat"]
}
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.
Claude finished @sam-goodwin's task —— View job
Refactoring compatibility presets type inference ✅
- Read current implementation in compatibility-presets.ts
- Apply suggested refactor to use type inference
- Commit changes to existing branch
Changes Applied:
- Refactored
CompatibilityPreset
type to usekeyof typeof COMPATIBILITY_PRESETS
pattern - Added
as const
assertion to the presets object for better type inference - Moved the type definition after the object definition to avoid circular references
The type is now automatically inferred from the object keys, eliminating duplication and ensuring the type stays in sync with the actual presets. This is a TypeScript best practice that prevents inconsistencies between the type definition and the runtime object.
Commit: ea741a7
commit: |
Use keyof typeof pattern to infer CompatibilityPreset type from COMPATIBILITY_PRESETS object, reducing duplication and ensuring type safety. Co-authored-by: sam <[email protected]>
/** | ||
* Get the compatibility flags for a given preset | ||
*/ | ||
export function getCompatibilityFlags(preset: CompatibilityPreset): string[] { | ||
return COMPATIBILITY_PRESETS[preset] || []; | ||
} |
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.
@claude we don't need this function, just export the const
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.
Claude finished @sam-goodwin's task —— View job
Removing unnecessary function from compatibility presets ✅
- Read current implementation in compatibility-presets.ts
- Remove unnecessary function and export const directly
- Commit changes to existing branch
Changes Applied:
- Removed the unnecessary
getCompatibilityFlags
function that was just wrapping access toCOMPATIBILITY_PRESETS[preset]
- Updated
unionCompatibilityFlags
to accessCOMPATIBILITY_PRESETS[preset]
directly - This simplifies the code by removing the wrapper function and accessing the const directly as requested
Commit: b23e533
Access COMPATIBILITY_PRESETS directly instead of using wrapper function Co-authored-by: sam <[email protected]>
🚀 Website Preview DeployedYour website preview is ready! Preview URL: https://pr-526-alchemy-website.alchemy-run.workers.dev This preview was built from commit b23e533 🤖 This comment will be updated automatically when you push new commits to this PR. |
@claude implement a test in worker.test.ts to check that it works |
Claude finished @sam-goodwin's task —— View job Implementing test for compatibility presets ✅I successfully implemented a test in Todo List:
Test Implementation Complete! ✅ The test now successfully verifies that:
I also added a polyfill for the Changes committed and pushed to |
🚀 Website Preview DeployedYour website preview is ready! Preview URL: https://b5cf54ef-alchemy-website.alchemy-run.workers.dev This preview was built from commit 7611b60 🤖 This comment will be updated automatically when you push new commits to this PR. |
- Implement comprehensive test for ''node'' compatibility preset - Verify preset automatically includes nodejs_compat flag - Test that preset flags combine with user-provided flags - Add File constructor polyfill for Node.js test environment - Ensure proper cleanup and error handling Co-authored-by: sam <[email protected]>
Implements compatibility presets for Cloudflare Workers to simplify configuration and reduce user errors.
Changes
compatibility-presets.ts
with "node" preset for nodejs_compat flagcompatibility
property to Worker interface accepting "node" presetUsage
Resolves #522
Generated with Claude Code