Skip to content

Instantly share code, notes, and snippets.

@bnewbold
Created May 23, 2023 23:29
Show Gist options
  • Save bnewbold/0f6c24f51d4e37dcf03224388ab53738 to your computer and use it in GitHub Desktop.
Save bnewbold/0f6c24f51d4e37dcf03224388ab53738 to your computer and use it in GitHub Desktop.

AT-URIs with DIDs are not "URLs". Can they be parsed using exiting libraries in popular programming langauges?

Typescript / Javascript

Typescript implementation (@atproto/uri) is implemented "from scratch".

Assuming that the "URL" class in the formal web platform does not work.

The popular url-parse NPM package works:

var parse = require('url-parse');
console.log(parse('at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk25', {}));

Go

net/url (stdlib) does not work.

github.com/fredbi/uri does not work:

package main

import (
    "fmt"
    "github.com/fredbi/uri"
)

func main() {
    u, err := uri.Parse("at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk25")
    if err != nil {
        fmt.Printf("Invalid URI: %v", err)
    } else {
        fmt.Printf("%s", u.Authority())
    }
}
// Invalid URI: invalid port in URI

Rust

url and http are the most popular crates.

use url;
fn main() {
    println!("{:?}", url::Url::parse("at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk25"))
}
// Err(InvalidPort)

use http;
fn main() {
    println!("{:?}", http::uri::Uri::from_static("at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk25"));
}
// thread 'main' panicked at 'static str is not valid URI: invalid authority'

uriparse is the most popular alternative; it also does not work

use std::convert::TryFrom;
use uriparse;
fn main() {
    println!("{:?}", uriparse::URI::try_from("at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk
25"));
}
// Err(Authority(Host(InvalidIPv4OrRegisteredNameCharacter)))

Python

urllib.parse (stdlib) works fine; DID comes back in "netloc" section. tested with python3.11

from urllib.parse import urlparse

aturi = "at://did:plc:24ybard66vv44zksje25o7dz/app.bsky.feed.post/3jwdwj2ctlk25
urlparse(aturi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment