Skip to content

feat: support "CONNECT", socket-based interception#2

Merged
luisAzcuaga merged 60 commits intomainfrom
feat/support-connect-method
Sep 17, 2025
Merged

feat: support "CONNECT", socket-based interception#2
luisAzcuaga merged 60 commits intomainfrom
feat/support-connect-method

Conversation

@luisAzcuaga
Copy link
Copy Markdown
Owner

@luisAzcuaga luisAzcuaga commented Sep 17, 2025

Continuing the work of on the PR mswjs#483 I've done this fully functional.

Summary

This PR adds comprehensive support for the HTTP CONNECT method in the interceptors library, enabling proper handling of proxy tunneling scenarios. The implementation includes socket-based
interception capabilities and fixes a critical issue with proxy agent cleanup that was causing runtime errors.

Changes

🚀 Features

  1. CONNECT Method Support (95df373)
    - Added support for the CONNECT HTTP method in the interceptor
    - Enables handling of proxy tunnel establishment requests
  2. Socket-Based Interception (be7838f, 0ee564c)
    - Implemented socket-based interception mechanism
    - Allows intercepting and handling CONNECT tunnel requests at the socket level
    - Added Duplex socket handling for tunnel communication
  3. Built Files for GitHub Dependencies (44534e5)
    - Included compiled TypeScript files in the repository
    - Enables direct installation from GitHub without requiring a build step
    - Supports development workflows that depend on GitHub references

🐛 Bug Fixes

  1. Proxy Agent Safe Destroy (00ed0a1)
    - Critical Fix: Added safe destroy check for proxy agents in the terminate() method
    - Problem: Proxy agents (like tunnel-agent, hpagent) don't implement the standard destroy() method, causing TypeError: _a.destroy is not a function
    - Solution: Added proper type checking before calling agent.destroy() to prevent crashes with CONNECT tunneling requests
    - Impact: Eliminates runtime errors when using VCR libraries or other tools that intercept proxy requests

🔧 Maintenance

  1. Debug Logging Cleanup (bf5f568, 158eb2c)
    - Added debugging support for CONNECT method during development
    - Removed debug logging from production code

Test Coverage

  • ✅ All existing tests pass
  • ✅ CONNECT method integration tests pass
  • ✅ Proxy agent tests (got-hpagent-proxy.test.ts) validate the fix
  • ✅ Socket-based interception properly handles tunnel requests

Breaking Changes

None. All changes are backward compatible.

Use Cases

This PR enables the interceptors library to properly handle:

  1. HTTP Proxy Tunneling: CONNECT requests for establishing secure tunnels through HTTP proxies
  2. VCR Libraries: Recording/replaying HTTP interactions that involve proxy requests
  3. Corporate Proxies: Applications running behind corporate proxy infrastructure
  4. Testing Scenarios: Mocking proxy behavior in test environments

Example Usage

interceptor.on('request', ({ request }) => {
// Handle CONNECT requests for proxy tunneling
if (request.method === 'CONNECT') {
return request.respondWith(
new Response(null, {
status: 200,
statusText: 'Connection Established',
})
);
}

// Handle regular requests
// ... existing logic

});

Impact

  • No Breaking Changes: Existing functionality remains unchanged
  • Enhanced Compatibility: Works with all major proxy libraries (tunnel-agent, hpagent, etc.)
  • Improved Reliability: Eliminates runtime crashes during proxy request cleanup
  • Extended Use Cases: Opens up new possibilities for testing and intercepting proxy-based traffic

This implementation makes the interceptors library more robust and suitable for enterprise environments where proxy infrastructure is common.

🤖 Generated with Claude Code

Antonio Cheong and others added 30 commits August 25, 2023 19:01
…js#397)

Co-authored-by: avivasyuta <avivasyuta@avito.ru>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
Co-authored-by: avivasyuta <avivasyuta@avito.ru>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
Co-authored-by: Antonio Cheong <acheong@student.dalat.org>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
Co-authored-by: Aleksey Ivasyuta <avivasyuta@gmail.com>
Co-authored-by: avivasyuta <avivasyuta@avito.ru>
kettanaito and others added 21 commits October 4, 2023 19:09
mswjs#453)

Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
Clean up verbose debug logging that was added for CONNECT method debugging.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Proxy agents like tunnel-agent and hpagent may not implement the standard
destroy() method, causing TypeError when the interceptor attempts cleanup.
Added proper type checking before calling agent.destroy() to prevent crashes
with CONNECT tunneling requests.

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

Co-Authored-By: Claude <noreply@anthropic.com>
@luisAzcuaga luisAzcuaga self-assigned this Sep 17, 2025
@luisAzcuaga luisAzcuaga force-pushed the feat/support-connect-method branch from 44534e5 to 00ed0a1 Compare September 17, 2025 21:52
@luisAzcuaga luisAzcuaga reopened this Sep 17, 2025
@luisAzcuaga luisAzcuaga merged commit 100b572 into main Sep 17, 2025
@luisAzcuaga luisAzcuaga deleted the feat/support-connect-method branch September 17, 2025 21:58
@luisAzcuaga luisAzcuaga changed the title feat: include built files for github dependency support feat: support "CONNECT", socket-based interception Sep 17, 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.

7 participants