Commit Detail
Commit 03ecba9
Add internalNewHttpClient API for Socket objects (#4005)
This change adds a new method to the Socket API that allows for creating an HTTP client
from an existing Socket connection. This enables making standard HTTP requests over an
already established socket connection, providing a higher-level interface for HTTP
communication compared to raw socket operations.
Key components:
1. Added a new `createHttpClient` method to SocketsModule that takes a Socket object
and returns a Fetcher that can perform HTTP requests over that socket
2. Implemented a new `StreamWorkerInterface` class that handles HTTP requests by
forwarding them through the socket's connection stream
3. Created a `SocketOutgoingFactory` class that generates WorkerInterface instances
from the underlying socket
4. Added TypeScript definitions for the new API in the cloudflare modules
5. Added comprehensive tests for the new API
The implementation uses KJ's existing HTTP client capabilities via `kj::internalNewHttpClient`
to create an HTTP client from the socket's stream, then wraps it in a WorkerInterface
implementation that can be used by a Fetcher to provide a familiar fetch API.
Usage example:
```js
import { connect, createHttpClient } from 'cloudflare:sockets';
export default {
async fetch(req, env) {
let socket = connect("example.com:80");
let httpClient = await internalNewHttpClient(socket);
return await httpClient.fetch("/path");
}
}
```
This makes it easier to work with various HTTP servers over socket connections
without having to manually implement HTTP protocol handling.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-authored-by: Claude <noreply@anthropic.com>Files changed
8 files changed+3 added~5 modified