~soapdog/ssb-custom-uri

d16bcea7b8283dc053766e92998f9548294261a8 — Andre Alves Garzia 4 years ago 4232360
set custom protocol schema
3 files changed, 62 insertions(+), 18 deletions(-)

M README.md
M demo.js
M ssb-custom-uri.js
M README.md => README.md +10 -0
@@ 74,6 74,16 @@ ssbUri.pubInviteToLink("ssb:invite/pub/foo.bar.co.nz/8009/93RP4aT6YNm%2B7%2B%2BA
// => foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=
~~~~

### setCustomProtocolSchema
You might want a protocol schema for your app so that you can launch a default SSB client for `ssb:` links and your own client for some more specific need. This function allows you to change the protocol schema being used. A potential use case is specific apps such as _Whakapapa Ora_ which could have their own `whakapapa-ora:` custom URL and still use this library.

~~~~
ssbUri.setCustomProtocolSchema("whakapapa-ora")
ssbUri.fromSigilLink("%EOgRe5ppgaugsqGyoWxJQ3QCFaGV7akjFUQ+9RR1jfk=.sha256")

// => whakapapa-ora:message/sha256/EOgRe5ppgaugsqGyoWxJQ3QCFaGV7akjFUQ+9RR1jfk=
~~~~

# License

MIT
\ No newline at end of file

M demo.js => demo.js +32 -2
@@ 1,4 1,5 @@
import ssbUri from "./ssb-custom-uri.js"
import ssbCustomUri from "./ssb-custom-uri.js"

let msg = "%g3hPVPDEO1Aj/uPl0+J2NlhFB2bbFLIHlty+YuqFZ3w=.sha256"
let url = ssbUri.fromSigilLink(msg)


@@ 29,7 30,7 @@ let table = {
    },
    "pub invite": {
        "SSB": "foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=",
        "Custom URL": ssbUri.peerInviteToLink("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=")
    },
    
}


@@ 54,5 55,34 @@ if (pubInvite == backToPubInvite) {
    console.log("pub invite -> url -> pub invite, ok!")
}


console.table(table)

console.log("Changing protocol schema from `ssb:` to `whakapapa-ora:`...")

ssbUri.setCustomProtocolSchema("whakapapa-ora")

table = {
    message: {
        "SSB": "%g3hPVPDEO1Aj/uPl0+J2NlhFB2bbFLIHlty+YuqFZ3w=.sha256",
        "Custom URL": ssbUri.fromSigilLink("%g3hPVPDEO1Aj/uPl0+J2NlhFB2bbFLIHlty+YuqFZ3w=.sha256")
    },
    feed: {
        "SSB": "@0xkjAty6RSr5uhbAvi0rbVR2g9Bz+89qiKth48ECQBE=.ed25519",
        "Custom URL": ssbUri.fromSigilLink("@0xkjAty6RSr5uhbAvi0rbVR2g9Bz+89qiKth48ECQBE=.ed25519")
    },
    blob: {
        "SSB": "&poIUI58nagfXmN/JkPob8VjBLzpcpnDuYuJzPCyelQ4=.sha256",
        "Custom URL": ssbUri.fromSigilLink("&poIUI58nagfXmN/JkPob8VjBLzpcpnDuYuJzPCyelQ4=.sha256")
    },
    "peer invite": {
        "SSB": "dht:DAy3i1tSOyqCsb+vVXc7enc+lo9XI26VknqCJ+PmCnc=:@FuvN1MpxWHGGYAswLAmfpTkkS5iKaFog2aPL4baU1cE=.ed25519",
        "Custom URL": ssbUri.peerInviteToLink("dht:DAy3i1tSOyqCsb+vVXc7enc+lo9XI26VknqCJ+PmCnc=:@FuvN1MpxWHGGYAswLAmfpTkkS5iKaFog2aPL4baU1cE=.ed25519")
    },
    "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=")
    },
    
}

console.table(table)
\ No newline at end of file

M ssb-custom-uri.js => ssb-custom-uri.js +20 -16
@@ 12,6 12,10 @@
 */

let ssbCustomUri = {
    protocolSchema: "ssb",
    setCustomProtocolSchema: (schema) => {
        ssbCustomUri.protocolSchema = schema
    },
    fromSigilLink: (sigilLinkString) => {
        let sigil = sigilLinkString[0]
        let prefix


@@ 33,12 37,12 @@ let ssbCustomUri = {
                return false
        }

        return `ssb:${prefix}/${format}/${encodeURIComponent(hash)}`
        return `${ssbCustomUri.protocolSchema}:${prefix}/${format}/${encodeURIComponent(hash)}`

    },
    toSigilLink: (uriLinkString) => {
        // remove `ssb:` if present.
        if (uriLinkString.slice(0, 4) === "ssb:") {
        if (uriLinkString.slice(0, ssbCustomUri.protocolSchema.length + 1) === `${ssbCustomUri.protocolSchema}:`) {
            uriLinkString = uriLinkString.slice(4)
        }



@@ 79,13 83,13 @@ let ssbCustomUri = {

        let type = parts[0]
        let invite = encodeURIComponent(parts[1])
        let feed = ssbCustomUri.fromSigilLink(parts[2]).replace("ssb:", "") // beware of `ssb:`
        let feed = ssbCustomUri.fromSigilLink(parts[2]).replace(`${ssbCustomUri.protocolSchema}:`, "") // beware of `ssb:`

        return `ssb:invite/${type}/${invite}/${feed}`
        return `${ssbCustomUri.protocolSchema}:invite/${type}/${invite}/${feed}`
    },
    peerInviteFromLink: (link) => {
        // remove `ssb:` if present.
        if (link.slice(0, 4) === "ssb:") {
        if (link.slice(0, ssbCustomUri.protocolSchema.length + 1) === `${ssbCustomUri.protocolSchema}:`) {
            link = link.slice(4)
        }



@@ 108,7 112,7 @@ let ssbCustomUri = {
        let invite = decodeURIComponent(link.slice(0, link.indexOf("/")))
        link = link.slice(link.indexOf("/") + 1) // remove invite.

        let feed = ssbCustomUri.toSigilLink(`ssb:${link}`)
        let feed = ssbCustomUri.toSigilLink(`${ssbCustomUri.protocolSchema}:${link}`)

        return `${type}:${invite}:${feed}`



@@ 120,19 124,19 @@ let ssbCustomUri = {
        foo.bar.co.nz:8009:@6lOh+rLq4MQubPQoKenbB827dh5NVc2FAjy30MTc08o=.ed25519~93RP4aT6YNm+7++Ab3VQd+w/CL2w6p8QuX7fRaUs9cY=
        */

       let parts = pubInvite.split(":")
        let parts = pubInvite.split(":")

       let type = "pub"
       let host = parts[0]
       let port = parts[1]
       let invite = encodeURIComponent(parts[2].slice(parts[2].indexOf("~") + 1))
       let feed = ssbCustomUri.fromSigilLink(parts[2].slice(0, parts[2].indexOf("~"))).replace("ssb:", "") // beware of `ssb:`
        let type = "pub"
        let host = parts[0]
        let port = parts[1]
        let invite = encodeURIComponent(parts[2].slice(parts[2].indexOf("~") + 1))
        let feed = ssbCustomUri.fromSigilLink(parts[2].slice(0, parts[2].indexOf("~"))).replace(`${ssbCustomUri.protocolSchema}:`, "") // beware of `ssb:`

       return `ssb:invite/${type}/${host}/${port}/${invite}/${feed}`
        return `${ssbCustomUri.protocolSchema}:invite/${type}/${host}/${port}/${invite}/${feed}`
    },
    pubInviteFromLink: (link) => {
         // remove `ssb:` if present.
         if (link.slice(0, 4) === "ssb:") {
        // remove `ssb:` if present.
        if (link.slice(0, ssbCustomUri.protocolSchema.length + 1) === `${ssbCustomUri.protocolSchema}:`) {
            link = link.slice(4)
        }



@@ 161,7 165,7 @@ let ssbCustomUri = {
        let invite = decodeURIComponent(link.slice(0, link.indexOf("/")))
        link = link.slice(link.indexOf("/") + 1) // remove invite.

        let feed = ssbCustomUri.toSigilLink(`ssb:${link}`)
        let feed = ssbCustomUri.toSigilLink(`${ssbCustomUri.protocolSchema}:${link}`)

        return `${host}:${port}:${feed}~${invite}`