You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -14,9 +13,16 @@ Outbound Workers sit between your customer's Workers and the public Internet. Th
14
13
15
14
Outbound Workers can be used to:
16
15
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.
let workerName =newURL(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
-
returnawaituserWorker.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
-
returnnewResponse('', { status:404 });
52
+
try {
53
+
// parse the URL, read the subdomain
54
+
let workerName =newURL(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
+
returnawaituserWorker.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
+
returnnewResponse("", { status:404 });
76
+
}
77
+
returnnewResponse(e.message, { status:500 });
70
78
}
71
-
returnnewResponse(e.message, { status:500 });
72
-
}
73
-
}
74
-
}
79
+
},
80
+
};
75
81
```
76
82
77
83
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
80
86
81
87
```js
82
88
exportdefault {
83
-
// this event is fired when the dispatched Workers make a subrequest
84
-
asyncfetch(request, env, ctx) {
85
-
// env contains the values we set in `dispatcher.get()`
86
-
constcustomer_name=env.customer_name;
87
-
constoriginal_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
-
consturl=newURL(original_url);
102
-
if (url.host==='api.example.com') {
103
-
// pre-auth requests to our API
104
-
constjwt=make_jwt_for_customer(customer_name);
105
-
106
-
let headers =newHeaders(request.headers);
107
-
headers.set('Authorization', `Bearer ${jwt}`);
108
-
109
-
// clone the request to set new headers using existing body
110
-
let new_request =newRequest(request, {headers});
111
-
112
-
returnfetch(new_request)
113
-
}
114
-
115
-
returnfetch(request)
116
-
}
89
+
// this event is fired when the dispatched Workers make a subrequest
90
+
asyncfetch(request, env, ctx) {
91
+
// env contains the values we set in `dispatcher.get()`
92
+
constcustomer_name=env.customer_name;
93
+
constoriginal_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
+
consturl=newURL(original_url);
107
+
if (url.host==="api.example.com") {
108
+
// pre-auth requests to our API
109
+
constjwt=make_jwt_for_customer(customer_name);
110
+
111
+
let headers =newHeaders(request.headers);
112
+
headers.set("Authorization", `Bearer ${jwt}`);
113
+
114
+
// clone the request to set new headers using existing body
115
+
let new_request =newRequest(request, { headers });
116
+
117
+
returnfetch(new_request);
118
+
}
119
+
120
+
returnfetch(request);
121
+
},
117
122
};
118
123
```
119
124
120
125
:::note
121
126
122
-
123
127
Outbound Workers do not intercept fetch requests made from [Durable Objects](/durable-objects/) or [mTLS certificate bindings](/workers/runtime-apis/bindings/mtls/).
0 commit comments