@@ -5,6 +5,7 @@ A class for interacting with Containers on Cloudflare Workers.
55## Features
66
77- HTTP request proxying and WebSocket forwarding
8+ - Outbound request interception by host or catch-all handler
89- Simple container lifecycle management (starting and stopping containers)
910- Event hooks for container lifecycle events (onStart, onStop, onError)
1011- Configurable sleep timeout that renews on requests
@@ -110,7 +111,6 @@ See [this example](#http-example-with-lifecycle-hooks).
110111- ` onStart() `
111112
112113 Called when container starts successfully.
113-
114114 - called when states transition from ` stopped ` -> ` running ` , ` running ` -> ` healthy `
115115
116116- ` onStop() `
@@ -150,7 +150,6 @@ See [this example](#http-example-with-lifecycle-hooks).
150150 Note: ` containerFetch ` does not work with websockets.
151151
152152 Sends an HTTP request to the container. Supports both standard fetch API signatures:
153-
154153 - ` containerFetch(request, port?) ` : Traditional signature with Request object
155154 - ` containerFetch(url, init?, port?) ` : Standard fetch-like signature with URL string/object and RequestInit options
156155
@@ -188,7 +187,6 @@ See [this example](#http-example-with-lifecycle-hooks).
188187 Starts the container, without waiting for any ports to be ready.
189188
190189 You might want to use this instead of ` startAndWaitForPorts ` if you want to:
191-
192190 - Start a container without blocking until a port is available
193191 - Initialize a container that doesn't expose ports
194192 - Perform custom port availability checks separately
@@ -249,10 +247,52 @@ See [this example](#http-example-with-lifecycle-hooks).
249247
250248 Manually renews the container activity timeout (extends container lifetime).
251249
250+ ##### Outbound Interception
251+
252+ Use outbound interception when you want to control what the container can reach, or proxy outbound requests through Worker code.
253+
254+ - ` setOutboundByHost(host: string, method: string): Promise<void> `
255+
256+ Routes a specific hostname to a named handler from ` static outboundHandlers ` .
257+
258+ - ` setOutboundByHosts(handlers: Record<string, string>): Promise<void> `
259+
260+ Replaces all runtime host-specific overrides at once.
261+
262+ - ` removeOutboundByHost(host: string): Promise<void> `
263+
264+ Removes a runtime host-specific override.
265+
266+ - ` setOutboundHandler(method: string): Promise<void> `
267+
268+ Sets the catch-all outbound handler to a named handler from ` static outboundHandlers ` .
269+
270+ To configure interception on the class itself:
271+
272+ - ` static outbound = (req, env, ctx) => Response `
273+
274+ Catch-all handler for outbound requests.
275+
276+ - ` static outboundByHost = { [host]: handler } `
277+
278+ Per-host handlers for exact hostname matches such as ` google.com ` or an IP address.
279+
280+ - ` static outboundHandlers = { [name]: handler } `
281+
282+ Named handlers that can be selected at runtime with ` setOutboundHandler ` and ` setOutboundByHost ` .
283+
284+ Matching order is:
285+
286+ 1 . Runtime ` setOutboundByHost ` override
287+ 2 . Static ` outboundByHost `
288+ 3 . Runtime ` setOutboundHandler ` catch-all
289+ 4 . Static ` outbound `
290+
291+ If nothing matches, the request goes out normally when ` enableInternet ` is ` true ` , and is blocked when ` enableInternet ` is ` false ` .
292+
252293- ` schedule<T = string>(when: Date | number, callback: string, payload?: T): Promise<Schedule<T>> `
253294
254295 Options:
255-
256296 - ` when ` : When to execute the task (Date object or number of seconds delay)
257297 - ` callback ` : Name of the function to call as a string
258298 - ` payload ` : Data to pass to the callback
@@ -371,6 +411,45 @@ export class ConfiguredContainer extends Container {
371411
372412You can also set these on a per-instance basis with ` start ` or ` startAndWaitForPorts `
373413
414+ ### Outbound Interception
415+
416+ This lets you intercept requests the container makes to the outside world.
417+
418+ ``` typescript
419+ import { Container , OutboundHandlerContext } from ' @cloudflare/containers' ;
420+
421+ export class MyContainer extends Container {
422+ defaultPort = 8080 ;
423+ enableInternet = false ;
424+
425+ static outboundByHost = {
426+ ' google.com' : (_req : Request , _env : unknown , ctx : OutboundHandlerContext ) => {
427+ return new Response (' hi ' + ctx .containerId + ' i am google' );
428+ },
429+ };
430+
431+ static outboundHandlers = {
432+ async github(_req : Request , _env : unknown , _ctx : OutboundHandlerContext ) {
433+ return new Response (' i am github' );
434+ },
435+ };
436+
437+ static outbound = (req : Request ) => {
438+ return new Response (` Hi ${req .url }, I can't handle you ` );
439+ };
440+
441+ async routeGithubThroughHandler(): Promise <void > {
442+ await this .setOutboundByHost (' github.com' , ' github' );
443+ }
444+
445+ async makeEverythingUseGithubHandler(): Promise <void > {
446+ await this .setOutboundHandler (' github' );
447+ }
448+ }
449+ ```
450+
451+ Use ` outboundByHost ` for fixed host rules, ` outbound ` for a default catch-all, and ` outboundHandlers ` for reusable named handlers you want to switch on at runtime.
452+
374453### Multiple Ports and Custom Routing
375454
376455You can create a container that doesn't use a default port and instead routes traffic to different ports based on request path or other factors:
0 commit comments