|
| 1 | +/** |
| 2 | + * Copyright (c) 2018, Cloudflare, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +import { graphql, buildSchema } from "graphql"; |
| 10 | +import DataLoader from "dataloader"; |
| 11 | + |
| 12 | +function binary_to_string(array) { |
| 13 | + var result = ""; |
| 14 | + for (var i = 0; i < array.length; ++i) { |
| 15 | + result += String.fromCharCode(array[i]); |
| 16 | + } |
| 17 | + return result; |
| 18 | +} |
| 19 | + |
| 20 | +async function decodequery(request) { |
| 21 | + const reader = request.body.getReader(); |
| 22 | + let query = ""; |
| 23 | + while (true) { |
| 24 | + let { done, value } = await reader.read(); |
| 25 | + if (done) { |
| 26 | + break; |
| 27 | + } |
| 28 | + query = query + binary_to_string(value); |
| 29 | + } |
| 30 | + let gql = JSON.parse(query); |
| 31 | + return gql; |
| 32 | +} |
| 33 | + |
| 34 | +var schema = buildSchema(` |
| 35 | + "DNS record type." |
| 36 | + enum RecordType { |
| 37 | + A |
| 38 | + AAAA |
| 39 | + MX |
| 40 | + CNAME |
| 41 | + DNSKEY |
| 42 | + DS |
| 43 | + NAPTR |
| 44 | + NS |
| 45 | + PTR |
| 46 | + SPF |
| 47 | + SRV |
| 48 | + SSHFP |
| 49 | + TLSA |
| 50 | + TXT |
| 51 | + } |
| 52 | +
|
| 53 | + "DNS query response" |
| 54 | + type Answer { |
| 55 | + "The record owner." |
| 56 | + name: String |
| 57 | + """The type of DNS record. |
| 58 | + These are defined here: |
| 59 | + https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 |
| 60 | + """ |
| 61 | + type: Int |
| 62 | + """The number of seconds the answer can be stored |
| 63 | + in cache before it is considered stale.""" |
| 64 | + TTL: Int |
| 65 | + """The value of the DNS record for the given name and type. |
| 66 | + The data will be in text for standardized record types and in hex for unknown types.""" |
| 67 | + data: String |
| 68 | + } |
| 69 | + "A DNS query to resolve a DNS record of a given type." |
| 70 | + type Query { |
| 71 | + resolve(name: String!, type: RecordType!): [Answer] |
| 72 | + } |
| 73 | +`); |
| 74 | + |
| 75 | +async function resolve(event, x) { |
| 76 | + let req = new Request( |
| 77 | + "https://cloudflare-dns.com/dns-query?name=" + x.name + "&type=" + x.type, |
| 78 | + { |
| 79 | + headers: { |
| 80 | + Accept: "application/dns-json" |
| 81 | + } |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + let cache = await caches.open("dns"); |
| 86 | + let resp = await cache.match(req); |
| 87 | + |
| 88 | + if (!resp) { |
| 89 | + resp = await fetch(req); |
| 90 | + event.waitUntil(cache.put(req, resp.clone())); |
| 91 | + } |
| 92 | + let ans = await resp.json(); |
| 93 | + return ans.Answer; |
| 94 | +} |
| 95 | + |
| 96 | +async function batchResolver(event, keys) { |
| 97 | + return keys.map(id => resolve(event, id)); |
| 98 | +} |
| 99 | + |
| 100 | +self.cache = new Map(); |
| 101 | + |
| 102 | +class Root { |
| 103 | + constructor(event) { |
| 104 | + this.resolvers = new DataLoader(keys => batchResolver(event, keys), { |
| 105 | + cacheKeyFn: q => { |
| 106 | + q.type + q.name; |
| 107 | + }, |
| 108 | + cacheMap: self.cache |
| 109 | + }); |
| 110 | + } |
| 111 | + async resolve(x) { |
| 112 | + return this.resolvers.load(x); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export default async function handleGraphQLRequest(event) { |
| 117 | + let gql = await decodequery(event.request); |
| 118 | + let response = await graphql(schema, gql.query, new Root(event)); |
| 119 | + return new Response(JSON.stringify(response)); |
| 120 | +} |
0 commit comments