-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
38 lines (33 loc) · 1.2 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export default {
fetch: async (req, env) => {
const { pathname } = new URL(req.url)
if (pathname == "/worker") {
// Call WfP worker from this worker: this works
const wfp_worker = env.dispatcher.get("wfp_worker")
const response = await wfp_worker.fetch(req.url).then(res => res.text())
return new Response(response)
} else if (pathname == "/do") {
// Call into the DO and have the DO call the WfP worker: this does NOT work
const doStub = env.DO.get(env.DO.idFromName("test"))
const response = await doStub.fetch(req.url).then(res => res.text())
return new Response(response)
} else {
throw 'use path /worker or /do to initiate fetch from worker or do'
}
}
}
export class DO {
constructor(state, env) {
this.env = env
}
async fetch(req) {
try {
const wfp_worker = this.env.dispatcher.get("wfp_worker")
const response = await wfp_worker.fetch(req.url)
const msg = "wpf fetch from within the DO returned status " + response.status + ": " + await response.text()
return new Response(msg)
} catch (e) {
return new Response("wpf fetch from within the DO threw: " + e.message)
}
}
}