Compare commits

...

6 Commits

Author SHA1 Message Date
Spencer Ofwiti
7990bf32b1 Merge branch 'master' into spencer/meta-multiple-hashes 2021-06-29 14:01:07 +03:00
Spencer Ofwiti
8a0eee2cc6 Merge branch 'master' into spencer/meta-multiple-hashes 2021-06-29 09:20:35 +03:00
Spencer Ofwiti
4fd976b1ae Merge branch 'master' into spencer/meta-multiple-hashes 2021-06-28 18:37:26 +03:00
Spencer Ofwiti
7c439b5963 Get using multiple identifiers with automerge none. 2021-06-28 17:55:24 +03:00
Spencer Ofwiti
20ffaa3e68 Refactor meta server to take multiple identifiers. 2021-06-28 17:27:10 +03:00
Spencer Ofwiti
4be0b9d3ae Return 404 if resource is not found. 2021-06-28 10:26:09 +03:00
2 changed files with 88 additions and 9 deletions

View File

@ -147,7 +147,7 @@ function handleClientMergeGet(db, digest, keystore) {
doh(e); doh(e);
}); });
}).catch((e) => { }).catch((e) => {
console.error('mesage', e); console.error('message', e);
doh(e); doh(e);
}); });
}); });

View File

@ -87,7 +87,7 @@ async function startServer() {
http.createServer(processRequest).listen(config.get('SERVER_PORT')); http.createServer(processRequest).listen(config.get('SERVER_PORT'));
} }
const re_digest = /^\/([a-fA-F0-9]{64})\/?$/; const re_digest = /^([a-fA-F0-9]{64})\/?$/;
function parseDigest(url) { function parseDigest(url) {
const digest_test = url.match(re_digest); const digest_test = url.match(re_digest);
if (digest_test === null) { if (digest_test === null) {
@ -96,6 +96,42 @@ function parseDigest(url) {
return digest_test[1].toLowerCase(); return digest_test[1].toLowerCase();
} }
function getIds(url: string): Array<string> {
const params: Array<string> = url.split('?')[1].split('&');
let ids: Array<string> = [];
for (let param of params) {
const splitParam: Array<string> = param.split('=');
if (splitParam[0] === 'id') {
ids.push(parseDigest(splitParam[1]));
}
}
return ids;
}
function generateResponseBody(digest: string, data: string | boolean): string {
let response = {
id: digest,
status: 0,
headers: {},
body: ''
}
if (typeof data === 'boolean' || data === undefined) {
response.body = `Metadata for identifier ${digest} not found!`;
response.status = 404;
response.headers = {"Content-Type": "text/plain"}
} else {
const responseContentLength = (new TextEncoder().encode(data)).length;
response.body = data;
response.status = 200;
response.headers = {
"Access-Control-Allow-Origin": "*",
"Content-Type": 'application/json',
"Content-Length": responseContentLength,
}
}
return JSON.stringify(response);
}
async function processRequest(req, res) { async function processRequest(req, res) {
let digest = undefined; let digest = undefined;
const headers = { const headers = {
@ -119,7 +155,16 @@ async function processRequest(req, res) {
} }
try { try {
digest = parseDigest(req.url); if (req.url.includes('id')) {
if (req.method !== 'GET') {
res.writeHead(405, {"Content-Type": "text/plain"});
res.end();
return;
}
digest = getIds(req.url);
} else {
digest = parseDigest(req.url.substring(1));
}
} catch(e) { } catch(e) {
console.error('digest error: ' + e) console.error('digest error: ' + e)
res.writeHead(400, {"Content-Type": "text/plain"}); res.writeHead(400, {"Content-Type": "text/plain"});
@ -162,7 +207,24 @@ async function processRequest(req, res) {
break; break;
case 'get:automerge:client': case 'get:automerge:client':
content = await handlers.handleClientMergeGet(db, digest, keystore); if (digest instanceof Array) {
let response = [];
for (let dg of digest) {
const metadata = await handlers.handleClientMergeGet(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();
return;
} else {
content = await handlers.handleClientMergeGet(db, digest, keystore);
}
break; break;
case 'post:automerge:server': case 'post:automerge:server':
@ -182,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:
@ -205,7 +284,7 @@ async function processRequest(req, res) {
if (content === undefined) { if (content === undefined) {
console.error('empty content', data); console.error('empty content', data);
res.writeHead(400, {"Content-Type": "text/plain"}); res.writeHead(404, {"Content-Type": "text/plain"});
res.end(); res.end();
return; return;
} }