Get using multiple identifiers with automerge none.

This commit is contained in:
Spencer Ofwiti 2021-06-28 17:55:24 +03:00
parent 20ffaa3e68
commit 7c439b5963

View File

@ -108,19 +108,19 @@ function getIds(url: string): Array<string> {
return ids; return ids;
} }
function generateResponseBody(digest: string, data: string) { function generateResponseBody(digest: string, data: string | boolean): string {
let response = { let response = {
id: digest, id: digest,
status: 0, status: 0,
headers: {}, headers: {},
body: '' body: ''
} }
const responseContentLength = (new TextEncoder().encode(data)).length; if (typeof data === 'boolean' || data === undefined) {
if (data === undefined) {
response.body = `Metadata for identifier ${digest} not found!`; response.body = `Metadata for identifier ${digest} not found!`;
response.status = 404; response.status = 404;
response.headers = {"Content-Type": "text/plain"} response.headers = {"Content-Type": "text/plain"}
} else { } else {
const responseContentLength = (new TextEncoder().encode(data)).length;
response.body = data; response.body = data;
response.status = 200; response.status = 200;
response.headers = { response.headers = {
@ -156,6 +156,11 @@ async function processRequest(req, res) {
try { try {
if (req.url.includes('id')) { if (req.url.includes('id')) {
if (req.method !== 'GET') {
res.writeHead(405, {"Content-Type": "text/plain"});
res.end();
return;
}
digest = getIds(req.url); digest = getIds(req.url);
} else { } else {
digest = parseDigest(req.url.substring(1)); digest = parseDigest(req.url.substring(1));
@ -239,13 +244,30 @@ async function processRequest(req, res) {
// break; // break;
case 'get:automerge:none': case 'get:automerge:none':
r = await handlers.handleNoMergeGet(db, digest, keystore); if (digest instanceof Array) {
if (r == false) { let response = [];
res.writeHead(404, {"Content-Type": "text/plain"}); for (let dg of digest) {
const metadata = await handlers.handleNoMergeGet(db, dg, keystore);
response.push(generateResponseBody(dg, metadata));
}
const responseContentLength = (new TextEncoder().encode(response.toString())).length;
res.writeHead(207, {
"Access-Control-Allow-Origin": "*",
"Content-Type": contentType,
"Content-Length": responseContentLength,
});
res.write(response.toString());
res.end(); res.end();
return; return;
} else {
r = await handlers.handleNoMergeGet(db, digest, keystore);
if (r == false) {
res.writeHead(404, {"Content-Type": "text/plain"});
res.end();
return;
}
content = r;
} }
content = r;
break; break;
default: default: