1 | const debug = require('debug')('dweb-transports:naming');
|
2 |
|
3 | const 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 |
|
24 | function expand(partialUrl, remainder) {
|
25 | return partialUrl.endsWith("html")
|
26 | ? [partialUrl, remainder.join('/')]
|
27 | : partialUrl.endsWith("=")
|
28 | ? partialUrl + remainder.join('/')
|
29 | : (partialUrl.endsWith("/"))
|
30 | ? partialUrl+remainder.join('/')
|
31 | : undefined;
|
32 | }
|
33 | function resolve(parent, table, path) {
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
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));
|
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;
|
55 | }
|
56 | }
|
57 |
|
58 | function resolveName(url) {
|
59 | return url.startsWith("dweb:/") ? resolve([], domains, url.slice(6)) : url;
|
60 | }
|
61 | function naming(names) {
|
62 | return [].concat(...names.map(n => resolveName(n)))
|
63 | }
|
64 | async function p_namingcb(names) {
|
65 | return new Promise((resolve, reject) => { try { const res = naming(names); resolve(res); } catch(err) {reject(err)}});
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | exports = module.exports = {naming, p_namingcb};
|