Skip to content

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

Merged
merged 6 commits into from
Jul 14, 2025

Conversation

sam-goodwin
Copy link
Owner

Implements compatibility presets for Cloudflare Workers to simplify configuration and reduce user errors.

Changes

  • 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

Usage

const worker = await Worker("nodejs-worker", {
  compatibility: "node", // Automatically includes nodejs_compat flag
  entrypoint: "./src/api.ts",
});

Resolves #522

Generated with Claude Code

- 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]>
Comment on lines 8 to 19
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"],
};
Copy link
Owner Author

@sam-goodwin sam-goodwin Jul 2, 2025

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"]
}

Copy link
Contributor

@claude claude bot Jul 2, 2025

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 use keyof 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

Copy link

pkg-pr-new bot commented Jul 2, 2025

Open in StackBlitz

npm i https://pkg.pr.new/sam-goodwin/alchemy@526

commit: 90bddbe

Use keyof typeof pattern to infer CompatibilityPreset type from 
COMPATIBILITY_PRESETS object, reducing duplication and ensuring 
type safety.

Co-authored-by: sam <[email protected]>
Comment on lines 21 to 26
/**
* Get the compatibility flags for a given preset
*/
export function getCompatibilityFlags(preset: CompatibilityPreset): string[] {
return COMPATIBILITY_PRESETS[preset] || [];
}
Copy link
Owner Author

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

Copy link
Contributor

@claude claude bot Jul 2, 2025

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 to COMPATIBILITY_PRESETS[preset]
  • Updated unionCompatibilityFlags to access COMPATIBILITY_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]>
Copy link

github-actions bot commented Jul 2, 2025

🚀 Website Preview Deployed

Your 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.

@sam-goodwin
Copy link
Owner Author

@claude implement a test in worker.test.ts to check that it works

Copy link
Contributor

claude bot commented Jul 14, 2025

Claude finished @sam-goodwin's task —— View job


Implementing test for compatibility presets ✅

I successfully implemented a test in worker.test.ts to verify the compatibility presets functionality works correctly.

Todo List:

  • Read existing worker.test.ts to understand test patterns
  • Read compatibility-presets.ts to understand the new feature
  • Read worker.ts to see how presets are integrated
  • Implement test for compatibility presets functionality
  • Run tests to verify implementation works
  • Fix test with valid compatibility flag
  • Commit and push changes

Test Implementation Complete! ✅

The test now successfully verifies that:

  • Workers can be created with the "node" compatibility preset
  • The preset automatically includes the "nodejs_compat" flag
  • Preset flags are properly combined with user-provided flags (tested with "nodejs_als")
  • Both preset and user-provided flags are present in the final worker
  • The worker functions correctly with the compatibility settings

I also added a polyfill for the File constructor in the vitest setup to ensure compatibility with Node.js test environments.

Changes committed and pushed to claude/issue-522-20250702_012008

Copy link

github-actions bot commented Jul 14, 2025

🚀 Website Preview Deployed

Your 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.

claude bot and others added 2 commits July 14, 2025 06:29
- 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]>
@sam-goodwin sam-goodwin merged commit 5cdcb22 into main Jul 14, 2025
3 of 4 checks passed
@sam-goodwin sam-goodwin deleted the claude/issue-522-20250702_012008 branch July 14, 2025 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add compatibility presets to cloudflare/Worker
1 participant