~soapdog/ssb-custom-uri

d029265558039f5b7a5dac3d047c2c2cbcaaebfe — Andre Alves Garzia 4 years ago 6eb4d3a master v2.0.0
support for URIs from ssb-uri
3 files changed, 111 insertions(+), 26 deletions(-)

A VERSIONS.md
M demo.js
M ssb-custom-uri.js
A VERSIONS.md => VERSIONS.md +8 -0
@@ 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

M demo.js => demo.js +19 -4
@@ 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

M ssb-custom-uri.js => ssb-custom-uri.js +84 -22
@@ 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) => {
        /*