containers-network
v0.0.1
Published
Network routing utilities for Cloudflare Containers with TCP and HTTP proxy support
Maintainers
Readme
containers-network
Network routing utilities for Cloudflare Containers. Provides TCP and HTTP proxy classes that intercept outbound connections from containers and route them to other services.
Installation
npm install containers-networkUsage
Create proxy classes using makeTCPProxy or makeHTTPProxy, then route container traffic through them.
TCP Proxy
import {
ContainerInNetwork,
makeTCPProxy,
VIRTUAL_HOST_IP,
} from "containers-network";
// Create proxy classes for your services
export const Database = makeTCPProxy(
(env) => env.DATABASE.getByName("production"), // returns a Container
5432,
);
export const Validator = makeTCPProxy(
(env) => env.VALIDATOR.getByName("production"), // returns a Container
8080,
);
// Use in your container class
export class ComputeContainer extends ContainerInNetwork<Env> {
async onStart() {
// Route outbound connections to your proxies
await this.route(this.ctx.exports.Database({}));
await this.route(this.ctx.exports.Validator({}));
}
...
}Inside the container, connect to 10.0.0.2:5432 to reach the database or use VIRTUAL_HOST_IP which equals "10.0.0.2".
HTTP Proxy
import { makeHTTPProxy } from "containers-network";
export const MyAPI = makeHTTPProxy(
(env) => env.API_SERVICE, // Returns a HTTP service (Fetcher)
"api.example.com",
);
// Use in your container class
export class ComputeContainer extends ContainerInNetwork<Env> {
async onStart() {
// Route outbound connections to your proxies
await this.route(this.ctx.exports.MyAPI({}));
}
...
}Inside the container, call api.example.com to reach the your API service.
API
makeTCPProxy<Env>(getTarget, port)
Creates a TCP proxy class that intercepts outbound TCP connections.
getTarget: Function that returns aContainerInNetworkinstance from envport: Target port number- Returns: Class extending
WorkerEntrypointfor RPC routing
makeHTTPProxy<Env>(getTarget, hostname)
Creates an HTTP proxy class that intercepts outbound HTTP connections.
getTarget: Function that returns aFetcherinstance from envhostname: Target hostname for routing- Returns: Class extending
WorkerEntrypointfor RPC routing
ContainerInNetwork<Env>
Base container class with network routing capabilities.
Methods
route(proxy): Registers a proxy to intercept outbound connections
