UNPKG

3.27 kBJavaScriptView Raw
1const debug = require('debug')('dweb-transports:naming');
2
3const domains = {
4 arc: {
5 "archive.org": {
6 ".": ["https://dweb.me/archive/archive.html"],
7 "about": ["https://archive.org/about/"],
8 "details": ["https://dweb.me/archive/archive.html?item="],
9 "examples": ["https://dweb.me/archive/examples/"],
10 "images": ["https://dweb.me/archive/images/"],
11 "serve": ["https://dweb.archive.org/download/"],
12 "metadata": [
13 "wolk://dweb.archive.org/metadata/",
14 "gun:/gun/arc/archive.org/metadata/",
15 "https://dweb.me/arc/archive.org/metadata/"],
16 "search.php": ["https://dweb.me/archive/archive.html?query="],
17 "search": ["https://dweb.me/archive/archive.html?query="],
18 },
19 },
20 ipfs: [ "http://ipfs.io/ipfs/", "https://dweb.me/ipfs/"],
21}
22
23
24function expand(partialUrl, remainder) {
25 return partialUrl.endsWith("html")
26 ? [partialUrl, remainder.join('/')] // THis might always be an error.
27 : partialUrl.endsWith("=")
28 ? partialUrl + remainder.join('/')
29 : (partialUrl.endsWith("/"))
30 ? partialUrl+remainder.join('/')
31 : undefined;
32}
33function resolve(parent, table, path) {
34 /**
35 * parent = STRING "a/b/c" path matched so far
36 * table = { key: url || [url]}
37 * path = "d/e/f"
38 * returns [ url || [url,remainder]] || undefined
39 */
40 //debug("Resolving %o in %s", path, parent);
41 const remainder = Array.isArray(path) ? path : path.split('/');
42 const name = remainder.shift();
43 const found = table[name] || table["."]
44 if (found) {
45 if (Array.isArray(found)) {
46 return (found.map(partialUrl => expand(partialUrl, remainder)).filter(url => !!url)); // [url || [url, remainder]]
47 } else if (typeof found === "object") {
48 return resolve([parent, name].join('/'), found, remainder);
49 } else if (typeof found === "string") {
50 return [ expand(found, remainder) ]
51 }
52 } else {
53 debug("WARNING unable to resolve %s in %s", name, parent.join('/') || '/' )
54 return undefined; // Remainder not found
55 }
56}
57
58function resolveName(url) {
59 return url.startsWith("dweb:/") ? resolve([], domains, url.slice(6)) : url; //
60}
61function naming(names) {
62 return [].concat(...names.map(n => resolveName(n)))
63}
64async function p_namingcb(names) {
65 return new Promise((resolve, reject) => { try { const res = naming(names); resolve(res); } catch(err) {reject(err)}}); // Promisify pattern v2b (no CB)
66}
67
68/*
69//TODO find in DM where its catching http://dweb.me and heading back to http://localhost:4244
70const testdata = {
71 "dweb:/arc/archive.org/metadata/foo": [
72 "https://dweb.me/arc/archive.org/metadata/foo",
73 "gun:/gun/arc/archive.org/metadata/foo",
74 "wolk://dweb.archive.org/metadata/foo" ],
75 "dweb:/arc/archive.org/details/foo": [
76 "https://dweb.me/archive/archive.html?item=foo"],
77}
78
79function test() {
80 Object.entries(testdata).forEach(kv => {
81 const res = resolveName(kv[0]);
82 if ((!res
83 || res.length !== kv[1].length)
84 || res.some(r => !kv[1].includes(r))) {
85 debug("%s => %s expect %s", kv[0], res, kv[1]);
86 }});
87 p_namingcb(["dweb:/arc/archive.org/details/foo","foo://bar.baz"])
88 .then(res => debug("Got %o", res))
89 .catch(err => debug("Fail %o", err.message));
90}
91test();
92*/
93
94exports = module.exports = {naming, p_namingcb};