Add support for longer records

This makes it possible to handle more content in a single round.

Signed-off-by: Jacob Kiers <jacob@jacobkiers.net>
This commit is contained in:
Jacob Kiers 2021-08-21 11:17:50 +02:00
parent 74619915ca
commit 4b117a4743

View File

@ -40,7 +40,7 @@ const dohServer = "https://cloudflare-dns.com/dns-query?ct=application/dns-json&
const baseDomain = "hod.experiments.jacobkiers.net"; const baseDomain = "hod.experiments.jacobkiers.net";
async function readUrl(domain) { async function readUrl(domain) {
var index = await fetchIndex(`${dohServer}${domain}.${baseDomain}`); var index = await fetchIndex(`${domain}.${baseDomain}`);
var chunk_promises = []; var chunk_promises = [];
for(i = 0; i < index.chunks; i++) for(i = 0; i < index.chunks; i++)
@ -48,28 +48,30 @@ async function readUrl(domain) {
chunk_promises[i] = fetchChunk(i, index.hash); chunk_promises[i] = fetchChunk(i, index.hash);
} }
var chunks = await Promise.all(chunk_promises); const chunks = await Promise.all(chunk_promises);
var content = chunks.reduce((built, current) => built += current); const base64 = chunks.reduce((built, current) => built += current);
return handleContent(new Content(atob(content), index.mimeType, index.metaData)); return handleContent(new Content(atob(content), index.mimeType, index.metaData));
} }
async function fetchChunk(id, hash) async function fetchChunk(id, hash)
{ {
var domain = `${id}.${hash}.${baseDomain}`; const domain = `${id}.${hash}.${baseDomain}`;
const json = await fetch(`${dohServer}${domain}`) return await fetchData(domain);
}
async function fetchData(domain)
{
const json = await fetch(`${dohServer}${domain}`)
.then(response => response.json()); .then(response => response.json());
const raw_data = json.Answer[0].data;
const data = json.Answer[0].data.slice(1, -1); const data = raw_data.replaceAll(/[\s\"]/g, '');
return data; return data;
} }
async function fetchIndex(domain) async function fetchIndex(domain)
{ {
const response = await fetch(`${dohServer}.${domain}`); const index = await fetchData(domain);
const json = await response.json();
const index = json.Answer[0].data.slice(1, -1);
let ret = {}; let ret = {};
let items = index.split(';'); let items = index.split(';');