Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add Cloudflare Workers support to Twilio SDK#1129

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Copilot wants to merge5 commits intomain
base:main
Choose a base branch
Loading
fromcopilot/fix-1096

Conversation

Copilot
Copy link
Contributor

@CopilotCopilotAI commentedSep 12, 2025
edited
Loading

This PR implements cross-platform compatibility for the Twilio Node.js SDK, enabling it to run in Cloudflare Workers and other JavaScript runtimes without Node.js-specific dependencies.

Problem

The Twilio SDK previously failed in Cloudflare Workers with the error:

Cannot read properties of undefined (reading 'fd')

This occurred because the SDK relied on Node.js-specific modules likefs,https,Buffer, andprocess that aren't available in the Workers runtime.

Solution

Implemented automatic runtime detection and conditional module loading:

  1. Runtime Detection (src/base/runtime.ts) - Detects Node.js vs Cloudflare Workers vs Browser environments
  2. Cross-Platform HTTP Client (src/base/FetchRequestClient.ts) - Uses fetch API instead of axios for non-Node.js environments
  3. Adaptive RequestClient - Automatically selects the appropriate HTTP implementation
  4. Cross-Platform Authentication - Updated auth strategies to use Web APIs when Node.js Buffer isn't available
  5. Environment Variable Handling - Accepts env vars via options parameter in Workers

Usage in Cloudflare Workers

importTwiliofrom'twilio';exportdefault{asyncfetch(request,env){constclient=newTwilio(env.TWILIO_ACCOUNT_SID,env.TWILIO_AUTH_TOKEN,{env:{TWILIO_ACCOUNT_SID:env.TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN:env.TWILIO_AUTH_TOKEN}});constmessage=awaitclient.messages.create({body:'Hello from Cloudflare Workers!',from:'+1234567890',to:'+0987654321'});returnnewResponse(JSON.stringify({sid:message.sid}));}}

Benefits

  • Zero Breaking Changes - Existing Node.js code continues to work unchanged
  • Automatic Environment Detection - No manual configuration required
  • Full API Compatibility - All Twilio features work in Workers
  • Better Performance - Uses native fetch API optimized for edge environments
  • Smaller Bundle Size - Eliminates Node.js dependencies in Workers

Files Changed

  • src/base/runtime.ts - New runtime detection utilities
  • src/base/FetchRequestClient.ts - New fetch-based HTTP client
  • src/base/RequestClient.ts - Updated to auto-select HTTP implementation
  • src/base/BaseTwilio.ts - Cross-platform environment variable handling
  • src/auth_strategy/BasicAuthStrategy.ts - Cross-platform base64 encoding
  • src/auth_strategy/TokenAuthStrategy.ts - Conditional JWT parsing

The implementation maintains full backward compatibility while extending support to new JavaScript runtimes.

Fixes#1096.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn moreCopilot coding agent tips in the docs.

CopilotAIand others added3 commitsSeptember 12, 2025 12:43
Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
…upportCo-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
@CopilotCopilotAI changed the title[WIP] [Feature Request]: Run on Cloudflare Workersfeat: Add Cloudflare Workers support to Twilio SDKSep 12, 2025
Copilot finished work on behalf oftiwarishubham635September 12, 2025 12:53
@abegehr
Copy link

🥰

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@tiwarishubham635tiwarishubham635Awaiting requested review from tiwarishubham635

At least 1 approving review is required to merge this pull request.

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Run on Cloudflare Workers
4 participants
@Copilot@abegehr@tiwarishubham635@manisha1997

[8]ページ先頭

©2009-2025 Movatter.jp