forked from serverless-dns/serverless-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server-workers.js
48 lines (43 loc) · 1.28 KB
/
server-workers.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
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2021 RethinkDNS and its authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import "./core/workers/config.js";
import { handleRequest } from "./core/doh.js";
import * as system from "./system.js";
import * as util from "./commons/util.js";
export default {
// workers/runtime-apis/fetch-event#syntax-module-worker
async fetch(request, env, context) {
return await serveDoh(request, env, context);
},
};
function serveDoh(request, env, ctx) {
// on Workers, the network-context is only available in an event listener
// and so, publish system prepare from here instead of from main which
// runs in global-scope.
system.pub("prepare", { env: env });
const event = util.mkFetchEvent(
request,
null,
ctx.waitUntil.bind(ctx),
ctx.passThroughOnException.bind(ctx)
);
return new Promise((accept) => {
system
.when("go")
.then((v) => {
return handleRequest(event);
})
.then((response) => {
accept(response);
})
.catch((e) => {
console.error("server", "serveDoh err", e);
accept(util.respond405());
});
});
}