3 Star 2 Fork 1

Gitee 极速下载 / serverless-dns

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/serverless-dns/serverless-dns
克隆/下载
doh.js 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
Murtaza Aliakbar 提交于 2023-06-13 12:55 . jsdoc: fix typedefs
/*
* 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 RethinkPlugin from "./plugin.js";
import * as pres from "../plugins/plugin-response.js";
import * as util from "../commons/util.js";
import * as dnsutil from "../commons/dnsutil.js";
import IOState from "./io-state.js";
// TODO: define FetchEventLike
/**
* @typedef {any} FetchEventLike
*/
/**
* @param {FetchEvent|FetchEventLike} event
* @returns {Promise<Response>}
*/
export function handleRequest(event) {
return proxyRequest(event);
}
/**
* @param {FetchEvent} event
* @returns {Promise<Response>}
*/
async function proxyRequest(event) {
if (optionsRequest(event.request)) return util.respond204();
const io = new IOState();
const ua = event.request.headers.get("User-Agent");
try {
const plugin = new RethinkPlugin(event);
await plugin.initIoState(io);
// if an early response has been set by plugin.initIoState, return it
if (io.httpResponse) {
return withCors(io, ua);
}
await util.timedSafeAsyncOp(
/* op*/ async () => plugin.execute(),
/* waitMs*/ dnsutil.requestTimeout(),
/* onTimeout*/ async () => errorResponse(io)
);
} catch (err) {
log.e("doh", "proxy-request error", err.stack);
errorResponse(io, err);
}
return withCors(io, ua);
}
function optionsRequest(request) {
return request.method === "OPTIONS";
}
function errorResponse(io, err = null) {
const eres = pres.errResponse("doh.js", err);
io.dnsExceptionResponse(eres);
}
function withCors(io, ua) {
if (util.fromBrowser(ua)) io.setCorsHeadersIfNeeded();
return io.httpResponse;
}
JavaScript
1
https://gitee.com/mirrors/serverless-dns.git
git@gitee.com:mirrors/serverless-dns.git
mirrors
serverless-dns
serverless-dns
main

搜索帮助