Catch magnet links in canonical

This commit is contained in:
Mitra Ardron 2018-10-22 17:46:25 -07:00
parent e0a38ab5ad
commit ececfb5a6b
2 changed files with 28 additions and 3 deletions

View File

@ -699,7 +699,7 @@ class Transports {
const patts = [ // No overlap between patts & arcpatts, so order unimportant const patts = [ // No overlap between patts & arcpatts, so order unimportant
/^dweb:[/]+(\w+)[/]+(.*)/i, // dweb://(proto)/(internal) /^dweb:[/]+(\w+)[/]+(.*)/i, // dweb://(proto)/(internal)
/^\w+:[/]+(\w+)[/](.*)/i, // proto1://proto2//(internal) - maybe only match if proto1=proto2 (must be before proto:/internal) /^\w+:[/]+(\w+)[/](.*)/i, // proto1://proto2//(internal) - maybe only match if proto1=proto2 (must be before proto:/internal)
/^(\w+):[/]+(.*)/i, // (proto)://(internal) # must be after proto1://proto2 /^(\w+):[/]*(.*)/i, // (proto)://(internal) # must be after proto1://proto2
/^[/]*(\w+)[/](.*)/i, // /(proto)//(internal) - maybe only match if proto1=proto2 /^[/]*(\w+)[/](.*)/i, // /(proto)//(internal) - maybe only match if proto1=proto2
/^[/]*dweb[/]*(\w+)[/](.*)/i, // /dweb/(proto)//(internal) /^[/]*dweb[/]*(\w+)[/](.*)/i, // /dweb/(proto)//(internal)
] ]
@ -738,8 +738,9 @@ class Transports {
return o.protocol + ":/" + o.internal; return o.protocol + ":/" + o.internal;
} }
static gatewayUrl(url) { static gatewayUrl(url) {
// Convert url to gateway url, if not canonicalizable then just pass the url along
let o = Transports.canonicalName(url); let o = Transports.canonicalName(url);
return [this.mirror, o.proto, o.internal].join('/'); return o ? [this.mirror, o.proto, o.internal].join('/') : url;
} }
} }
Transports._transports = []; // Array of transport instances connected Transports._transports = []; // Array of transport instances connected

26
test.js
View File

@ -66,6 +66,8 @@ function canonicalNameTests() {
["gun://ipfs/internal", "ipfs", "internal"], ["gun://ipfs/internal", "ipfs", "internal"],
["/ipfs/internal", "ipfs", "internal"], ["/ipfs/internal", "ipfs", "internal"],
["/dweb/ipfs/internal", "ipfs", "internal"], ["/dweb/ipfs/internal", "ipfs", "internal"],
[ "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json",
"magnet","?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"],
["http://dweb.dom.ain/internal", "arc", "dom.ain/internal"], ["http://dweb.dom.ain/internal", "arc", "dom.ain/internal"],
["http://localhost:123/archive.org/internal", "arc", "archive.org/internal"], ["http://localhost:123/archive.org/internal", "arc", "archive.org/internal"],
["https://dweb.arc.dom.ain/internal", "arc", "dom.ain/internal"], ["https://dweb.arc.dom.ain/internal", "arc", "dom.ain/internal"],
@ -80,6 +82,28 @@ function canonicalNameTests() {
} }
/* /*
p_test() p_test({transport: ["GUN"])
.then(() => test_transports); .then(() => test_transports);
*/ */
// Intentionally testing this with no connection
const sampleMagnetURL = "magnet:?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json";
const sampleMagnetURLMirrorresolve = "http://localhost:4244/magnet/?xt=urn:btih:465HQWPEN374LABVHUBUPBUX4WZU6HDS&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org%3A6969&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Ffav-mitra/fav-mitra_members.json"
const sampleGatewayURL = "http://dweb.me/arc/archive.org/download/foo/bar";
const sampleHttpURL = "http://somewhere.com/zzz/xxx/download/foo/bar"; // XXX Looks like this is handled wrong
const sampleGatewayURLMirrorResolve = "http://localhost:4244/arc/archive.org/download/foo/bar";
let tests = [
{u: sampleMagnetURL, cn_proto:"magnet", gw: sampleMagnetURLMirrorresolve, resolve:sampleMagnetURL, resolveM: sampleMagnetURLMirrorresolve},
{u: sampleGatewayURL, cn_proto: "arc", gw: sampleGatewayURLMirrorResolve, resolve: sampleGatewayURL, resolveM: sampleGatewayURLMirrorResolve},
//FAILS ! {u: sampleHttpURL, cn_proto: "http", gw: sampleHttpURL, resolve: sampleHttpURL, resolveM: sampleHttpURL}
]
tests.forEach(t => {
let url = t.u;
let res = DwebTransports.canonicalName(url).proto; console.assert( res=== t.cn_proto, "Canonical fail",url, t.cn_proto, "!==", res);
DwebTransports.mirror = undefined;
res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolve, "Resolve", url, t.resolve, "!==", res ))
DwebTransports.mirror = "http://localhost:4244";
res = DwebTransports.gatewayUrl(url); console.assert( res === t.gw, "GatewayURL:", url, t.gw,"!==", res);
res = DwebTransports.p_resolveNames([url]).then(res => console.assert(res[0] === t.resolveM, "Resolve with Mirror", url, t.resolveM, "!==", res ))
})