Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 12, 2025

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 like fs, https, Buffer, and process 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

import Twilio from 'twilio';

export default {
  async fetch(request, env) {
    const client = new Twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN, {
      env: {
        TWILIO_ACCOUNT_SID: env.TWILIO_ACCOUNT_SID,
        TWILIO_AUTH_TOKEN: env.TWILIO_AUTH_TOKEN
      }
    });

    const message = await client.messages.create({
      body: 'Hello from Cloudflare Workers!',
      from: '+1234567890',
      to: '+0987654321'
    });

    return new Response(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 more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Feature Request]: Run on Cloudflare Workers feat: Add Cloudflare Workers support to Twilio SDK Sep 12, 2025
Copilot finished work on behalf of tiwarishubham635 September 12, 2025 12:53
@abegehr
Copy link

abegehr commented Sep 13, 2025

🥰

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.

[Feature Request]: Run on Cloudflare Workers
4 participants