From 44f5a1135227ce81cf5d257dedd4f3d1f009227d Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Mon, 2 Sep 2019 06:27:22 +1000 Subject: [PATCH] Build the naming into Transports (so can remove need for objects-bundle) --- Naming.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ Transports.js | 3 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 Naming.js diff --git a/Naming.js b/Naming.js new file mode 100644 index 0000000..96b5981 --- /dev/null +++ b/Naming.js @@ -0,0 +1,94 @@ +const debug = require('debug')('dweb-transports:naming'); + +const domains = { + arc: { + "archive.org": { + ".": ["https://dweb.me/archive/archive.html"], + "about": ["https://archive.org/about/"], + "details": ["https://dweb.me/archive/archive.html?item="], + "examples": ["https://dweb.me/archive/examples/"], + "images": ["https://dweb.me/archive/images/"], + "serve": ["https://dweb.archive.org/download/"], + "metadata": [ + "wolk://dweb.archive.org/metadata/", + "gun:/gun/arc/archive.org/metadata/", + "https://dweb.me/arc/archive.org/metadata/"], + "search.php": ["https://dweb.me/archive/archive.html?query="], + "search": ["https://dweb.me/archive/archive.html?query="], + }, + }, + ipfs: [ "http://ipfs.io/ipfs/", "https://dweb.me/ipfs/"], +} + + +function expand(partialUrl, remainder) { + return partialUrl.endsWith("html") + ? [partialUrl, remainder.join('/')] // THis might always be an error. + : partialUrl.endsWith("=") + ? partialUrl + remainder.join('/') + : (partialUrl.endsWith("/")) + ? partialUrl+remainder.join('/') + : undefined; +} +function resolve(parent, table, path) { + /** + * parent = STRING "a/b/c" path matched so far + * table = { key: url || [url]} + * path = "d/e/f" + * returns [ url || [url,remainder]] || undefined + */ + //debug("Resolving %o in %s", path, parent); + const remainder = Array.isArray(path) ? path : path.split('/'); + const name = remainder.shift(); + const found = table[name] || table["."] + if (found) { + if (Array.isArray(found)) { + return (found.map(partialUrl => expand(partialUrl, remainder)).filter(url => !!url)); // [url || [url, remainder]] + } else if (typeof found === "object") { + return resolve([parent, name].join('/'), found, remainder); + } else if (typeof found === "string") { + return [ expand(found, remainder) ] + } + } else { + debug("WARNING unable to resolve %s in %s", name, parent.join('/') || '/' ) + return undefined; // Remainder not found + } +} + +function resolveName(url) { + return url.startsWith("dweb:/") ? resolve([], domains, url.slice(6)) : url; // +} +function naming(names) { + return [].concat(...names.map(n => resolveName(n))) +} +async function p_namingcb(names) { + return new Promise((resolve, reject) => { try { const res = naming(names); resolve(res); } catch(err) {reject(err)}}); // Promisify pattern v2b (no CB) +} + +/* +//TODO find in DM where its catching http://dweb.me and heading back to http://localhost:4244 +const testdata = { + "dweb:/arc/archive.org/metadata/foo": [ + "https://dweb.me/arc/archive.org/metadata/foo", + "gun:/gun/arc/archive.org/metadata/foo", + "wolk://dweb.archive.org/metadata/foo" ], + "dweb:/arc/archive.org/details/foo": [ + "https://dweb.me/archive/archive.html?item=foo"], +} + +function test() { + Object.entries(testdata).forEach(kv => { + const res = resolveName(kv[0]); + if ((!res + || res.length !== kv[1].length) + || res.some(r => !kv[1].includes(r))) { + debug("%s => %s expect %s", kv[0], res, kv[1]); + }}); + p_namingcb(["dweb:/arc/archive.org/details/foo","foo://bar.baz"]) + .then(res => debug("Got %o", res)) + .catch(err => debug("Fail %o", err.message)); +} +test(); +*/ + +exports = module.exports = {p_namingcb}; diff --git a/Transports.js b/Transports.js index 2ff5faa..249b0b3 100644 --- a/Transports.js +++ b/Transports.js @@ -5,6 +5,7 @@ const debug = require('debug')('dweb-transports'); const httptools = require('./httptools'); const each = require('async/each'); const map = require('async/map'); +const {p_namingcb} = require('./Naming.js') class Transports { /* @@ -855,7 +856,7 @@ class Transports { } } Transports._transports = []; // Array of transport instances connected -Transports.namingcb = undefined; // Will be defined by the naming component (turns URLs for names into URLs for transport) +Transports.namingcb = p_namingcb; // Will be defined by the naming component (turns URLs for names into URLs for transport) Transports._transportclasses = {}; // Pointers to classes whose code is loaded. Transports.httptools = httptools; // Static http tools exports = module.exports = Transports;