From d029265558039f5b7a5dac3d047c2c2cbcaaebfe Mon Sep 17 00:00:00 2001 From: Andre Alves Garzia Date: Thu, 14 May 2020 16:33:25 +0100 Subject: [PATCH] support for URIs from ssb-uri --- VERSIONS.md | 8 ++++ demo.js | 23 ++++++++-- ssb-custom-uri.js | 106 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 111 insertions(+), 26 deletions(-) create mode 100644 VERSIONS.md diff --git a/VERSIONS.md b/VERSIONS.md new file mode 100644 index 0000000..ed539a6 --- /dev/null +++ b/VERSIONS.md @@ -0,0 +1,8 @@ +# Version 2.0.0 + +* Add support for decoding `ssb-uri` style URLs to `toSigilLink()`. +* Refactored `fromSigilLink()` to generate URIs in the format of `ssb-uri`. +* Added `URLFromSigilLink()` that generates URLs with slashes. +* Added `URIFromSigilLink()` which is an alias for `fromSigilLink()`. + +** BREAKING CHANGE, DEFAULT BEHAVIOR CHANGED TO GENERATE URIS ** \ No newline at end of file diff --git a/demo.js b/demo.js index d177a58..a24fccb 100644 --- a/demo.js +++ b/demo.js @@ -31,8 +31,8 @@ let table = { "pub invite": { "SSB": "foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=", "Custom URL": ssbUri.pubInviteToLink("foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=") - }, - + } + } let peerInvite = "dht:DAy3i1tSOyqCsb+vVXc7enc+lo9XI26VknqCJ+PmCnc=:@FuvN1MpxWHGGYAswLAmfpTkkS5iKaFog2aPL4baU1cE=.ed25519" @@ -82,7 +82,22 @@ table = { "SSB": "foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=", "Custom URL": ssbUri.pubInviteToLink("foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=") }, - + } -console.table(table) \ No newline at end of file +console.table(table) + +console.log("ssb-uri support") +ssbUri.setCustomProtocolSchema("ssb") + +let uri = "ssb:message:sha256:g3hPVPDEO1Aj_uPl0-J2NlhFB2bbFLIHlty-YuqFZ3w=" +let link = "%g3hPVPDEO1Aj/uPl0+J2NlhFB2bbFLIHlty+YuqFZ3w=.sha256" +let linkFromURI = ssbUri.toSigilLink(uri) + +if (link === linkFromURI) { + console.log("ssb-custom-uri can decode uris from ssb-uri") +} else { + console.log() + console.log(link) + console.log(linkFromURI) +} \ No newline at end of file diff --git a/ssb-custom-uri.js b/ssb-custom-uri.js index c430ca1..7dbec83 100644 --- a/ssb-custom-uri.js +++ b/ssb-custom-uri.js @@ -12,6 +12,7 @@ */ let ssbCustomUri = { + version: "2.0.0", protocolSchema: "ssb", setCustomProtocolSchema: (schema) => { ssbCustomUri.protocolSchema = schema @@ -29,6 +30,9 @@ let ssbCustomUri = { return link }, fromSigilLink: (sigilLinkString) => { + return ssbCustomUri.URIFromSigilLink(sigilLinkString) + }, + URIFromSigilLink: (sigilLinkString) => { let sigil = sigilLinkString[0] let prefix let format = sigilLinkString.split(".")[1] @@ -49,38 +53,96 @@ let ssbCustomUri = { return false } - return `${ssbCustomUri.protocolSchema}:${prefix}/${format}/${encodeURIComponent(hash)}` - - }, - toSigilLink: (link) => { - link = ssbCustomUri.removeProtocolSchemaFromLink(link) - - let prefix = link.slice(0, link.indexOf("/")) - link = link.slice(link.indexOf("/") + 1) // remove prefix. + hash = hash.replace(/\+/g, '-').replace(/\//g, '_') - let format = link.slice(0, link.indexOf("/")) - link = link.slice(link.indexOf("/") + 1) // remove format. + return `${ssbCustomUri.protocolSchema}:${prefix}:${format}:${hash}` - let hash = decodeURIComponent(link) - - let sigil; + }, + URLFromSigilLink: (sigilLinkString) => { + let sigil = sigilLinkString[0] + let prefix + let format = sigilLinkString.split(".")[1] + let hash = sigilLinkString.slice(1, sigilLinkString.indexOf(".")) - switch (prefix) { - case "message": - sigil = "%" + switch (sigil) { + case "%": + prefix = "message" break - case "blob": - sigil = "&" + case "&": + prefix = "blob" break - case "feed": - sigil = "@" + case "@": + prefix = "feed" break default: - console.error(`Not a valid url prefix: ${prefix}`) + console.error(`Not a valid sigil prefix: ${prefix}`) return false } - return `${sigil}${hash}.${format}` + return `${ssbCustomUri.protocolSchema}:${prefix}/${format}/${encodeURIComponent(hash)}` + + }, + toSigilLink: (link) => { + link = ssbCustomUri.removeProtocolSchemaFromLink(link) + + // check if ssb-uri or ssb-custom-uri kind of URL + if (link.indexOf(":") == -1) { + + let prefix = link.slice(0, link.indexOf("/")) + link = link.slice(link.indexOf("/") + 1) // remove prefix. + + let format = link.slice(0, link.indexOf("/")) + link = link.slice(link.indexOf("/") + 1) // remove format. + + let hash = decodeURIComponent(link) + + let sigil; + + switch (prefix) { + case "message": + sigil = "%" + break + case "blob": + sigil = "&" + break + case "feed": + sigil = "@" + break + default: + console.error(`Not a valid url prefix: ${prefix}`) + return false + } + + return `${sigil}${hash}.${format}` + } else { + // ssb-uri style of URIs. + let prefix = link.slice(0, link.indexOf(":")) + link = link.slice(link.indexOf(":") + 1) // remove prefix. + + let format = link.slice(0, link.indexOf(":")) + link = link.slice(link.indexOf(":") + 1) // remove format. + + let hash = link.replace(/-/gi, "+").replace(/_/gi, "/") + + let sigil; + + switch (prefix) { + case "message": + sigil = "%" + break + case "blob": + sigil = "&" + break + case "feed": + sigil = "@" + break + default: + console.error(`Not a valid url prefix: ${prefix}`) + return false + } + + return `${sigil}${hash}.${format}` + } }, peerInviteToLink: (peerInvite) => { /* -- 2.45.2