Skip to content

Commit f0357fd

Browse files
committed
Documents connect being shut off when outbound worker is used
1 parent f9903c2 commit f0357fd

File tree

1 file changed

+71
-68
lines changed

1 file changed

+71
-68
lines changed
Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
pcx_content_type: concept
33
title: Outbound Workers
4-
54
---
65

76
import { WranglerConfig } from "~/components";
@@ -14,9 +13,16 @@ Outbound Workers sit between your customer's Workers and the public Internet. Th
1413

1514
Outbound Workers can be used to:
1615

17-
* Log all subrequests to identify malicious domains or usage patterns.
18-
* Create, allow, or block lists for hostnames requested by user Workers.
19-
* Configure authentication to your APIs behind the scenes (without end developers needing to set credentials).
16+
- Log all subrequests to identify malicious domains or usage patterns.
17+
- Create, allow, or block lists for hostnames requested by user Workers.
18+
- Configure authentication to your APIs behind the scenes (without end developers needing to set credentials).
19+
20+
:::note
21+
22+
When an Outbound Worker is enabled, your customer's Worker will no longer be able to use the [`connect() API`](/workers/runtime-apis/tcp-sockets/#connect)
23+
to create outbound TCP Sockets. This is to ensure all outbound communication goes through the Outbound Worker's `fetch` method.
24+
25+
:::
2026

2127
## Use Outbound Workers
2228

@@ -43,35 +49,35 @@ outbound = {service = "<SERVICE_NAME>", parameters = ["params_object"]}
4349
```js
4450
export default {
4551
async fetch(request, env) {
46-
try {
47-
48-
// parse the URL, read the subdomain
49-
let workerName = new URL(request.url).host.split('.')[0];
50-
51-
let context_from_dispatcher = {
52-
'customer_name': workerName,
53-
'url': request.url,
54-
}
55-
56-
let userWorker = env.dispatcher.get(
57-
workerName,
58-
{},
59-
{// outbound arguments. object name must match parameters in the binding
60-
outbound: {
61-
params_object: context_from_dispatcher,
62-
}
63-
}
64-
);
65-
return await userWorker.fetch(request);
66-
} catch (e) {
67-
if (e.message.startsWith('Worker not found')) {
68-
// we tried to get a worker that doesn't exist in our dispatch namespace
69-
return new Response('', { status: 404 });
52+
try {
53+
// parse the URL, read the subdomain
54+
let workerName = new URL(request.url).host.split(".")[0];
55+
56+
let context_from_dispatcher = {
57+
customer_name: workerName,
58+
url: request.url,
59+
};
60+
61+
let userWorker = env.dispatcher.get(
62+
workerName,
63+
{},
64+
{
65+
// outbound arguments. object name must match parameters in the binding
66+
outbound: {
67+
params_object: context_from_dispatcher,
68+
},
69+
},
70+
);
71+
return await userWorker.fetch(request);
72+
} catch (e) {
73+
if (e.message.startsWith("Worker not found")) {
74+
// we tried to get a worker that doesn't exist in our dispatch namespace
75+
return new Response("", { status: 404 });
76+
}
77+
return new Response(e.message, { status: 500 });
7078
}
71-
return new Response(e.message, { status: 500 });
72-
}
73-
}
74-
}
79+
},
80+
};
7581
```
7682

7783
4. The Outbound Worker will now be invoked on any `fetch()` requests from a user Worker. The user Worker will trigger a [FetchEvent](/workers/runtime-apis/handlers/fetch/) on the Outbound Worker. The variables declared in the binding can be accessed in the Outbound Worker through `env.<VAR_NAME>`.
@@ -80,47 +86,44 @@ The following is an example of an Outbound Worker that logs the fetch request fr
8086

8187
```js
8288
export default {
83-
// this event is fired when the dispatched Workers make a subrequest
84-
async fetch(request, env, ctx) {
85-
// env contains the values we set in `dispatcher.get()`
86-
const customer_name = env.customer_name;
87-
const original_url = env.url;
88-
89-
// log the request
90-
ctx.waitUntil(fetch(
91-
'https://logs.example.com',
92-
{
93-
method: 'POST',
94-
body: JSON.stringify({
95-
customer_name,
96-
original_url,
97-
}),
98-
},
99-
));
100-
101-
const url = new URL(original_url);
102-
if (url.host === 'api.example.com') {
103-
// pre-auth requests to our API
104-
const jwt = make_jwt_for_customer(customer_name);
105-
106-
let headers = new Headers(request.headers);
107-
headers.set('Authorization', `Bearer ${jwt}`);
108-
109-
// clone the request to set new headers using existing body
110-
let new_request = new Request(request, {headers});
111-
112-
return fetch(new_request)
113-
}
114-
115-
return fetch(request)
116-
}
89+
// this event is fired when the dispatched Workers make a subrequest
90+
async fetch(request, env, ctx) {
91+
// env contains the values we set in `dispatcher.get()`
92+
const customer_name = env.customer_name;
93+
const original_url = env.url;
94+
95+
// log the request
96+
ctx.waitUntil(
97+
fetch("https://logs.example.com", {
98+
method: "POST",
99+
body: JSON.stringify({
100+
customer_name,
101+
original_url,
102+
}),
103+
}),
104+
);
105+
106+
const url = new URL(original_url);
107+
if (url.host === "api.example.com") {
108+
// pre-auth requests to our API
109+
const jwt = make_jwt_for_customer(customer_name);
110+
111+
let headers = new Headers(request.headers);
112+
headers.set("Authorization", `Bearer ${jwt}`);
113+
114+
// clone the request to set new headers using existing body
115+
let new_request = new Request(request, { headers });
116+
117+
return fetch(new_request);
118+
}
119+
120+
return fetch(request);
121+
},
117122
};
118123
```
119124

120125
:::note
121126

122-
123127
Outbound Workers do not intercept fetch requests made from [Durable Objects](/durable-objects/) or [mTLS certificate bindings](/workers/runtime-apis/bindings/mtls/).
124128

125-
126129
:::

0 commit comments

Comments
 (0)