@atproto/syntax 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/handle.ts", "../src/did.ts", "../src/nsid.ts", "../src/aturi_validation.ts", "../src/aturi.ts"],
4
+ "sourcesContent": ["export * from './handle'\nexport * from './did'\nexport * from './nsid'\nexport * from './aturi'\n", "export const INVALID_HANDLE = 'handle.invalid'\n\n// Currently these are registration-time restrictions, not protocol-level\n// restrictions. We have a couple accounts in the wild that we need to clean up\n// before hard-disallow.\n// See also: https://en.wikipedia.org/wiki/Top-level_domain#Reserved_domains\nexport const DISALLOWED_TLDS = [\n '.local',\n '.arpa',\n '.invalid',\n '.localhost',\n '.internal',\n // policy could concievably change on \".onion\" some day\n '.onion',\n // NOTE: .test is allowed in testing and devopment. In practical terms\n // \"should\" \"never\" actually resolve and get registered in production\n]\n\n// Handle constraints, in English:\n// - must be a possible domain name\n// - RFC-1035 is commonly referenced, but has been updated. eg, RFC-3696,\n// section 2. and RFC-3986, section 3. can now have leading numbers (eg,\n// 4chan.org)\n// - \"labels\" (sub-names) are made of ASCII letters, digits, hyphens\n// - can not start or end with a hyphen\n// - TLD (last component) should not start with a digit\n// - can't end with a hyphen (can end with digit)\n// - each segment must be between 1 and 63 characters (not including any periods)\n// - overall length can't be more than 253 characters\n// - separated by (ASCII) periods; does not start or end with period\n// - case insensitive\n// - domains (handles) are equal if they are the same lower-case\n// - punycode allowed for internationalization\n// - no whitespace, null bytes, joining chars, etc\n// - does not validate whether domain or TLD exists, or is a reserved or\n// special TLD (eg, .onion or .local)\n// - does not validate punycode\nexport const ensureValidHandle = (handle: string): void => {\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9.-]*$/.test(handle)) {\n throw new InvalidHandleError(\n 'Disallowed characters in handle (ASCII letters, digits, dashes, periods only)',\n )\n }\n\n if (handle.length > 253) {\n throw new InvalidHandleError('Handle is too long (253 chars max)')\n }\n const labels = handle.split('.')\n if (labels.length < 2) {\n throw new InvalidHandleError('Handle domain needs at least two parts')\n }\n for (let i = 0; i < labels.length; i++) {\n const l = labels[i]\n if (l.length < 1) {\n throw new InvalidHandleError('Handle parts can not be empty')\n }\n if (l.length > 63) {\n throw new InvalidHandleError('Handle part too long (max 63 chars)')\n }\n if (l.endsWith('-') || l.startsWith('-')) {\n throw new InvalidHandleError(\n 'Handle parts can not start or end with hyphens',\n )\n }\n if (i + 1 == labels.length && !/^[a-zA-Z]/.test(l)) {\n throw new InvalidHandleError(\n 'Handle final component (TLD) must start with ASCII letter',\n )\n }\n }\n}\n\n// simple regex translation of above constraints\nexport const ensureValidHandleRegex = (handle: string): void => {\n if (\n !/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/.test(\n handle,\n )\n ) {\n throw new InvalidHandleError(\"Handle didn't validate via regex\")\n }\n if (handle.length > 253) {\n throw new InvalidHandleError('Handle is too long (253 chars max)')\n }\n}\n\nexport const normalizeHandle = (handle: string): string => {\n return handle.toLowerCase()\n}\n\nexport const normalizeAndEnsureValidHandle = (handle: string): string => {\n const normalized = normalizeHandle(handle)\n ensureValidHandle(normalized)\n return normalized\n}\n\nexport const isValidHandle = (handle: string): boolean => {\n try {\n ensureValidHandle(handle)\n } catch (err) {\n if (err instanceof InvalidHandleError) {\n return false\n }\n throw err\n }\n\n return true\n}\n\nexport const isValidTld = (handle: string): boolean => {\n return !DISALLOWED_TLDS.some((domain) => handle.endsWith(domain))\n}\n\nexport class InvalidHandleError extends Error {}\nexport class ReservedHandleError extends Error {}\nexport class UnsupportedDomainError extends Error {}\nexport class DisallowedDomainError extends Error {}\n", "// Human-readable constraints:\n// - valid W3C DID (https://www.w3.org/TR/did-core/#did-syntax)\n// - entire URI is ASCII: [a-zA-Z0-9._:%-]\n// - always starts \"did:\" (lower-case)\n// - method name is one or more lower-case letters, followed by \":\"\n// - remaining identifier can have any of the above chars, but can not end in \":\"\n// - it seems that a bunch of \":\" can be included, and don't need spaces between\n// - \"%\" is used only for \"percent encoding\" and must be followed by two hex characters (and thus can't end in \"%\")\n// - query (\"?\") and fragment (\"#\") stuff is defined for \"DID URIs\", but not as part of identifier itself\n// - \"The current specification does not take a position on the maximum length of a DID\"\n// - in current atproto, only allowing did:plc and did:web. But not *forcing* this at lexicon layer\n// - hard length limit of 8KBytes\n// - not going to validate \"percent encoding\" here\nexport const ensureValidDid = (did: string): void => {\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9._:%-]*$/.test(did)) {\n throw new InvalidDidError(\n 'Disallowed characters in DID (ASCII letters, digits, and a couple other characters only)',\n )\n }\n\n const parts = did.split(':')\n if (parts.length < 3) {\n throw new InvalidDidError(\n 'DID requires prefix, method, and method-specific content',\n )\n }\n\n if (parts[0] != 'did') {\n throw new InvalidDidError('DID requires \"did:\" prefix')\n }\n\n if (!/^[a-z]+$/.test(parts[1])) {\n throw new InvalidDidError('DID method must be lower-case letters')\n }\n\n if (did.endsWith(':') || did.endsWith('%')) {\n throw new InvalidDidError('DID can not end with \":\" or \"%\"')\n }\n\n if (did.length > 2 * 1024) {\n throw new InvalidDidError('DID is too long (2048 chars max)')\n }\n}\n\nexport const ensureValidDidRegex = (did: string): void => {\n // simple regex to enforce most constraints via just regex and length.\n // hand wrote this regex based on above constraints\n if (!/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/.test(did)) {\n throw new InvalidDidError(\"DID didn't validate via regex\")\n }\n\n if (did.length > 2 * 1024) {\n throw new InvalidDidError('DID is too long (2048 chars max)')\n }\n}\n\nexport class InvalidDidError extends Error {}\n", "/*\nGrammar:\n\nalpha = \"a\" / \"b\" / \"c\" / \"d\" / \"e\" / \"f\" / \"g\" / \"h\" / \"i\" / \"j\" / \"k\" / \"l\" / \"m\" / \"n\" / \"o\" / \"p\" / \"q\" / \"r\" / \"s\" / \"t\" / \"u\" / \"v\" / \"w\" / \"x\" / \"y\" / \"z\" / \"A\" / \"B\" / \"C\" / \"D\" / \"E\" / \"F\" / \"G\" / \"H\" / \"I\" / \"J\" / \"K\" / \"L\" / \"M\" / \"N\" / \"O\" / \"P\" / \"Q\" / \"R\" / \"S\" / \"T\" / \"U\" / \"V\" / \"W\" / \"X\" / \"Y\" / \"Z\"\nnumber = \"1\" / \"2\" / \"3\" / \"4\" / \"5\" / \"6\" / \"7\" / \"8\" / \"9\" / \"0\"\ndelim = \".\"\nsegment = alpha *( alpha / number / \"-\" )\nauthority = segment *( delim segment )\nname = alpha *( alpha )\nnsid = authority delim name\n\n*/\n\nexport class NSID {\n segments: string[] = []\n\n static parse(nsid: string): NSID {\n return new NSID(nsid)\n }\n\n static create(authority: string, name: string): NSID {\n const segments = [...authority.split('.').reverse(), name].join('.')\n return new NSID(segments)\n }\n\n static isValid(nsid: string): boolean {\n try {\n NSID.parse(nsid)\n return true\n } catch (e) {\n return false\n }\n }\n\n constructor(nsid: string) {\n ensureValidNsid(nsid)\n this.segments = nsid.split('.')\n }\n\n get authority() {\n return this.segments\n .slice(0, this.segments.length - 1)\n .reverse()\n .join('.')\n }\n\n get name() {\n return this.segments.at(this.segments.length - 1)\n }\n\n toString() {\n return this.segments.join('.')\n }\n}\n\n// Human readable constraints on NSID:\n// - a valid domain in reversed notation\n// - followed by an additional period-separated name, which is camel-case letters\nexport const ensureValidNsid = (nsid: string): void => {\n const toCheck = nsid\n\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9.-]*$/.test(toCheck)) {\n throw new InvalidNsidError(\n 'Disallowed characters in NSID (ASCII letters, digits, dashes, periods only)',\n )\n }\n\n if (toCheck.length > 253 + 1 + 63) {\n throw new InvalidNsidError('NSID is too long (317 chars max)')\n }\n const labels = toCheck.split('.')\n if (labels.length < 3) {\n throw new InvalidNsidError('NSID needs at least three parts')\n }\n for (let i = 0; i < labels.length; i++) {\n const l = labels[i]\n if (l.length < 1) {\n throw new InvalidNsidError('NSID parts can not be empty')\n }\n if (l.length > 63) {\n throw new InvalidNsidError('NSID part too long (max 63 chars)')\n }\n if (l.endsWith('-') || l.startsWith('-')) {\n throw new InvalidNsidError('NSID parts can not start or end with hyphen')\n }\n if (/^[0-9]/.test(l) && i == 0) {\n throw new InvalidNsidError('NSID first part may not start with a digit')\n }\n if (!/^[a-zA-Z]+$/.test(l) && i + 1 == labels.length) {\n throw new InvalidNsidError('NSID name part must be only letters')\n }\n }\n}\n\nexport const ensureValidNsidRegex = (nsid: string): void => {\n // simple regex to enforce most constraints via just regex and length.\n // hand wrote this regex based on above constraints\n if (\n !/^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$/.test(\n nsid,\n )\n ) {\n throw new InvalidNsidError(\"NSID didn't validate via regex\")\n }\n if (nsid.length > 253 + 1 + 63) {\n throw new InvalidNsidError('NSID is too long (317 chars max)')\n }\n}\n\nexport class InvalidNsidError extends Error {}\n", "import { ensureValidHandle, ensureValidHandleRegex } from './handle'\nimport { ensureValidDid, ensureValidDidRegex } from './did'\nimport { ensureValidNsid, ensureValidNsidRegex } from './nsid'\n\n// Human-readable constraints on ATURI:\n// - following regular URLs, a 8KByte hard total length limit\n// - follows ATURI docs on website\n// - all ASCII characters, no whitespace. non-ASCII could be URL-encoded\n// - starts \"at://\"\n// - \"authority\" is a valid DID or a valid handle\n// - optionally, follow \"authority\" with \"/\" and valid NSID as start of path\n// - optionally, if NSID given, follow that with \"/\" and rkey\n// - rkey path component can include URL-encoded (\"percent encoded\"), or:\n// ALPHA / DIGIT / \"-\" / \".\" / \"_\" / \"~\" / \":\" / \"@\" / \"!\" / \"$\" / \"&\" / \"'\" / \"(\" / \")\" / \"*\" / \"+\" / \",\" / \";\" / \"=\"\n// [a-zA-Z0-9._~:@!$&'\\(\\)*+,;=-]\n// - rkey must have at least one char\n// - regardless of path component, a fragment can follow as \"#\" and then a JSON pointer (RFC-6901)\nexport const ensureValidAtUri = (uri: string) => {\n // JSON pointer is pretty different from rest of URI, so split that out first\n const uriParts = uri.split('#')\n if (uriParts.length > 2) {\n throw new Error('ATURI can have at most one \"#\", separating fragment out')\n }\n const fragmentPart = uriParts[1] || null\n uri = uriParts[0]\n\n // check that all chars are boring ASCII\n if (!/^[a-zA-Z0-9._~:@!$&')(*+,;=%/-]*$/.test(uri)) {\n throw new Error('Disallowed characters in ATURI (ASCII)')\n }\n\n const parts = uri.split('/')\n if (parts.length >= 3 && (parts[0] != 'at:' || parts[1].length != 0)) {\n throw new Error('ATURI must start with \"at://\"')\n }\n if (parts.length < 3) {\n throw new Error('ATURI requires at least method and authority sections')\n }\n\n try {\n ensureValidHandle(parts[2])\n } catch {\n try {\n ensureValidDid(parts[2])\n } catch {\n throw new Error('ATURI authority must be a valid handle or DID')\n }\n }\n\n if (parts.length >= 4) {\n if (parts[3].length == 0) {\n throw new Error(\n 'ATURI can not have a slash after authority without a path segment',\n )\n }\n try {\n ensureValidNsid(parts[3])\n } catch {\n throw new Error(\n 'ATURI requires first path segment (if supplied) to be valid NSID',\n )\n }\n }\n\n if (parts.length >= 5) {\n if (parts[4].length == 0) {\n throw new Error(\n 'ATURI can not have a slash after collection, unless record key is provided',\n )\n }\n // would validate rkey here, but there are basically no constraints!\n }\n\n if (parts.length >= 6) {\n throw new Error(\n 'ATURI path can have at most two parts, and no trailing slash',\n )\n }\n\n if (uriParts.length >= 2 && fragmentPart == null) {\n throw new Error('ATURI fragment must be non-empty and start with slash')\n }\n\n if (fragmentPart != null) {\n if (fragmentPart.length == 0 || fragmentPart[0] != '/') {\n throw new Error('ATURI fragment must be non-empty and start with slash')\n }\n // NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace\n if (!/^\\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\\]/-]*$/.test(fragmentPart)) {\n throw new Error('Disallowed characters in ATURI fragment (ASCII)')\n }\n }\n\n if (uri.length > 8 * 1024) {\n throw new Error('ATURI is far too long')\n }\n}\n\nexport const ensureValidAtUriRegex = (uri: string): void => {\n // simple regex to enforce most constraints via just regex and length.\n // hand wrote this regex based on above constraints. whew!\n const aturiRegex =\n /^at:\\/\\/(?<authority>[a-zA-Z0-9._:%-]+)(\\/(?<collection>[a-zA-Z0-9-.]+)(\\/(?<rkey>[a-zA-Z0-9._~:@!$&%')(*+,;=-]+))?)?(#(?<fragment>\\/[a-zA-Z0-9._~:@!$&%')(*+,;=\\-[\\]/\\\\]*))?$/\n const rm = uri.match(aturiRegex)\n if (!rm || !rm.groups) {\n throw new Error(\"ATURI didn't validate via regex\")\n }\n const groups = rm.groups\n\n try {\n ensureValidHandleRegex(groups.authority)\n } catch {\n try {\n ensureValidDidRegex(groups.authority)\n } catch {\n throw new Error('ATURI authority must be a valid handle or DID')\n }\n }\n\n if (groups.collection) {\n try {\n ensureValidNsidRegex(groups.collection)\n } catch {\n throw new Error('ATURI collection path segment must be a valid NSID')\n }\n }\n\n if (uri.length > 8 * 1024) {\n throw new Error('ATURI is far too long')\n }\n}\n", "export * from './aturi_validation'\n\nexport const ATP_URI_REGEX =\n // proto- --did-------------- --name---------------- --path---- --query-- --hash--\n /^(at:\\/\\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\\/[^?#\\s]*)?(\\?[^#\\s]+)?(#[^\\s]+)?$/i\n// --path----- --query-- --hash--\nconst RELATIVE_REGEX = /^(\\/[^?#\\s]*)?(\\?[^#\\s]+)?(#[^\\s]+)?$/i\n\nexport class AtUri {\n hash: string\n host: string\n pathname: string\n searchParams: URLSearchParams\n\n constructor(uri: string, base?: string) {\n let parsed\n if (base) {\n parsed = parse(base)\n if (!parsed) {\n throw new Error(`Invalid at uri: ${base}`)\n }\n const relativep = parseRelative(uri)\n if (!relativep) {\n throw new Error(`Invalid path: ${uri}`)\n }\n Object.assign(parsed, relativep)\n } else {\n parsed = parse(uri)\n if (!parsed) {\n throw new Error(`Invalid at uri: ${uri}`)\n }\n }\n\n this.hash = parsed.hash\n this.host = parsed.host\n this.pathname = parsed.pathname\n this.searchParams = parsed.searchParams\n }\n\n static make(handleOrDid: string, collection?: string, rkey?: string) {\n let str = handleOrDid\n if (collection) str += '/' + collection\n if (rkey) str += '/' + rkey\n return new AtUri(str)\n }\n\n get protocol() {\n return 'at:'\n }\n\n get origin() {\n return `at://${this.host}`\n }\n\n get hostname() {\n return this.host\n }\n\n set hostname(v: string) {\n this.host = v\n }\n\n get search() {\n return this.searchParams.toString()\n }\n\n set search(v: string) {\n this.searchParams = new URLSearchParams(v)\n }\n\n get collection() {\n return this.pathname.split('/').filter(Boolean)[0] || ''\n }\n\n set collection(v: string) {\n const parts = this.pathname.split('/').filter(Boolean)\n parts[0] = v\n this.pathname = parts.join('/')\n }\n\n get rkey() {\n return this.pathname.split('/').filter(Boolean)[1] || ''\n }\n\n set rkey(v: string) {\n const parts = this.pathname.split('/').filter(Boolean)\n if (!parts[0]) parts[0] = 'undefined'\n parts[1] = v\n this.pathname = parts.join('/')\n }\n\n get href() {\n return this.toString()\n }\n\n toString() {\n let path = this.pathname || '/'\n if (!path.startsWith('/')) {\n path = `/${path}`\n }\n let qs = this.searchParams.toString()\n if (qs && !qs.startsWith('?')) {\n qs = `?${qs}`\n }\n let hash = this.hash\n if (hash && !hash.startsWith('#')) {\n hash = `#${hash}`\n }\n return `at://${this.host}${path}${qs}${hash}`\n }\n}\n\nfunction parse(str: string) {\n const match = ATP_URI_REGEX.exec(str)\n if (match) {\n return {\n hash: match[5] || '',\n host: match[2] || '',\n pathname: match[3] || '',\n searchParams: new URLSearchParams(match[4] || ''),\n }\n }\n return undefined\n}\n\nfunction parseRelative(str: string) {\n const match = RELATIVE_REGEX.exec(str)\n if (match) {\n return {\n hash: match[3] || '',\n pathname: match[1] || '',\n searchParams: new URLSearchParams(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,EAEA,IAAI,OAAO;AACT,WAAO,KAAK,SAAS;AAAA,EACvB;AAAA,EAEA,WAAW;AACT,QAAI,OAAO,KAAK,YAAY;AAC5B,QAAI,CAAC,KAAK,WAAW,GAAG,GAAG;AACzB,aAAO,IAAI;AAAA,IACb;AACA,QAAI,KAAK,KAAK,aAAa,SAAS;AACpC,QAAI,MAAM,CAAC,GAAG,WAAW,GAAG,GAAG;AAC7B,WAAK,IAAI;AAAA,IACX;AACA,QAAI,OAAO,KAAK;AAChB,QAAI,QAAQ,CAAC,KAAK,WAAW,GAAG,GAAG;AACjC,aAAO,IAAI;AAAA,IACb;AACA,WAAO,QAAQ,KAAK,OAAO,OAAO,KAAK;AAAA,EACzC;AACF;AAEA,SAAS,MAAM,KAAa;AAC1B,QAAM,QAAQ,cAAc,KAAK,GAAG;AACpC,MAAI,OAAO;AACT,WAAO;AAAA,MACL,MAAM,MAAM,MAAM;AAAA,MAClB,MAAM,MAAM,MAAM;AAAA,MAClB,UAAU,MAAM,MAAM;AAAA,MACtB,cAAc,IAAI,gBAAgB,MAAM,MAAM,EAAE;AAAA,IAClD;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,cAAc,KAAa;AAClC,QAAM,QAAQ,eAAe,KAAK,GAAG;AACrC,MAAI,OAAO;AACT,WAAO;AAAA,MACL,MAAM,MAAM,MAAM;AAAA,MAClB,UAAU,MAAM,MAAM;AAAA,MACtB,cAAc,IAAI,gBAAgB,MAAM,MAAM,EAAE;AAAA,IAClD;AAAA,EACF;AACA,SAAO;AACT;",
6
+ "names": []
7
+ }
package/dist/nsid.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export declare class NSID {
2
+ segments: string[];
3
+ static parse(nsid: string): NSID;
4
+ static create(authority: string, name: string): NSID;
5
+ static isValid(nsid: string): boolean;
6
+ constructor(nsid: string);
7
+ get authority(): string;
8
+ get name(): string | undefined;
9
+ toString(): string;
10
+ }
11
+ export declare const ensureValidNsid: (nsid: string) => void;
12
+ export declare const ensureValidNsidRegex: (nsid: string) => void;
13
+ export declare class InvalidNsidError extends Error {
14
+ }
package/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ const base = require('../../jest.config.base.js')
2
+
3
+ module.exports = {
4
+ ...base,
5
+ displayName: 'Identifier',
6
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@atproto/syntax",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "scripts": {
6
+ "test": "jest",
7
+ "prettier": "prettier --check src/ tests/",
8
+ "prettier:fix": "prettier --write src/ tests/",
9
+ "lint": "eslint . --ext .ts,.tsx",
10
+ "lint:fix": "yarn lint --fix",
11
+ "verify": "run-p prettier lint",
12
+ "verify:fix": "yarn prettier:fix && yarn lint:fix",
13
+ "build": "node ./build.js",
14
+ "postbuild": "tsc --build tsconfig.build.json",
15
+ "update-main-to-dist": "node ./update-pkg.js --update-main-to-dist",
16
+ "update-main-to-src": "node ./update-pkg.js --update-main-to-src",
17
+ "prepublish": "npm run update-main-to-dist",
18
+ "postpublish": "npm run update-main-to-src"
19
+ },
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/bluesky-social/atproto.git",
24
+ "directory": "packages/syntax"
25
+ },
26
+ "dependencies": {
27
+ "@atproto/common-web": "*"
28
+ },
29
+ "browser": {
30
+ "dns/promises": false
31
+ }
32
+ }
package/src/aturi.ts ADDED
@@ -0,0 +1,136 @@
1
+ export * from './aturi_validation'
2
+
3
+ export const ATP_URI_REGEX =
4
+ // proto- --did-------------- --name---------------- --path---- --query-- --hash--
5
+ /^(at:\/\/)?((?:did:[a-z0-9:%-]+)|(?:[a-z0-9][a-z0-9.:-]*))(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i
6
+ // --path----- --query-- --hash--
7
+ const RELATIVE_REGEX = /^(\/[^?#\s]*)?(\?[^#\s]+)?(#[^\s]+)?$/i
8
+
9
+ export class AtUri {
10
+ hash: string
11
+ host: string
12
+ pathname: string
13
+ searchParams: URLSearchParams
14
+
15
+ constructor(uri: string, base?: string) {
16
+ let parsed
17
+ if (base) {
18
+ parsed = parse(base)
19
+ if (!parsed) {
20
+ throw new Error(`Invalid at uri: ${base}`)
21
+ }
22
+ const relativep = parseRelative(uri)
23
+ if (!relativep) {
24
+ throw new Error(`Invalid path: ${uri}`)
25
+ }
26
+ Object.assign(parsed, relativep)
27
+ } else {
28
+ parsed = parse(uri)
29
+ if (!parsed) {
30
+ throw new Error(`Invalid at uri: ${uri}`)
31
+ }
32
+ }
33
+
34
+ this.hash = parsed.hash
35
+ this.host = parsed.host
36
+ this.pathname = parsed.pathname
37
+ this.searchParams = parsed.searchParams
38
+ }
39
+
40
+ static make(handleOrDid: string, collection?: string, rkey?: string) {
41
+ let str = handleOrDid
42
+ if (collection) str += '/' + collection
43
+ if (rkey) str += '/' + rkey
44
+ return new AtUri(str)
45
+ }
46
+
47
+ get protocol() {
48
+ return 'at:'
49
+ }
50
+
51
+ get origin() {
52
+ return `at://${this.host}`
53
+ }
54
+
55
+ get hostname() {
56
+ return this.host
57
+ }
58
+
59
+ set hostname(v: string) {
60
+ this.host = v
61
+ }
62
+
63
+ get search() {
64
+ return this.searchParams.toString()
65
+ }
66
+
67
+ set search(v: string) {
68
+ this.searchParams = new URLSearchParams(v)
69
+ }
70
+
71
+ get collection() {
72
+ return this.pathname.split('/').filter(Boolean)[0] || ''
73
+ }
74
+
75
+ set collection(v: string) {
76
+ const parts = this.pathname.split('/').filter(Boolean)
77
+ parts[0] = v
78
+ this.pathname = parts.join('/')
79
+ }
80
+
81
+ get rkey() {
82
+ return this.pathname.split('/').filter(Boolean)[1] || ''
83
+ }
84
+
85
+ set rkey(v: string) {
86
+ const parts = this.pathname.split('/').filter(Boolean)
87
+ if (!parts[0]) parts[0] = 'undefined'
88
+ parts[1] = v
89
+ this.pathname = parts.join('/')
90
+ }
91
+
92
+ get href() {
93
+ return this.toString()
94
+ }
95
+
96
+ toString() {
97
+ let path = this.pathname || '/'
98
+ if (!path.startsWith('/')) {
99
+ path = `/${path}`
100
+ }
101
+ let qs = this.searchParams.toString()
102
+ if (qs && !qs.startsWith('?')) {
103
+ qs = `?${qs}`
104
+ }
105
+ let hash = this.hash
106
+ if (hash && !hash.startsWith('#')) {
107
+ hash = `#${hash}`
108
+ }
109
+ return `at://${this.host}${path}${qs}${hash}`
110
+ }
111
+ }
112
+
113
+ function parse(str: string) {
114
+ const match = ATP_URI_REGEX.exec(str)
115
+ if (match) {
116
+ return {
117
+ hash: match[5] || '',
118
+ host: match[2] || '',
119
+ pathname: match[3] || '',
120
+ searchParams: new URLSearchParams(match[4] || ''),
121
+ }
122
+ }
123
+ return undefined
124
+ }
125
+
126
+ function parseRelative(str: string) {
127
+ const match = RELATIVE_REGEX.exec(str)
128
+ if (match) {
129
+ return {
130
+ hash: match[3] || '',
131
+ pathname: match[1] || '',
132
+ searchParams: new URLSearchParams(match[2] || ''),
133
+ }
134
+ }
135
+ return undefined
136
+ }
@@ -0,0 +1,131 @@
1
+ import { ensureValidHandle, ensureValidHandleRegex } from './handle'
2
+ import { ensureValidDid, ensureValidDidRegex } from './did'
3
+ import { ensureValidNsid, ensureValidNsidRegex } from './nsid'
4
+
5
+ // Human-readable constraints on ATURI:
6
+ // - following regular URLs, a 8KByte hard total length limit
7
+ // - follows ATURI docs on website
8
+ // - all ASCII characters, no whitespace. non-ASCII could be URL-encoded
9
+ // - starts "at://"
10
+ // - "authority" is a valid DID or a valid handle
11
+ // - optionally, follow "authority" with "/" and valid NSID as start of path
12
+ // - optionally, if NSID given, follow that with "/" and rkey
13
+ // - rkey path component can include URL-encoded ("percent encoded"), or:
14
+ // ALPHA / DIGIT / "-" / "." / "_" / "~" / ":" / "@" / "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
15
+ // [a-zA-Z0-9._~:@!$&'\(\)*+,;=-]
16
+ // - rkey must have at least one char
17
+ // - regardless of path component, a fragment can follow as "#" and then a JSON pointer (RFC-6901)
18
+ export const ensureValidAtUri = (uri: string) => {
19
+ // JSON pointer is pretty different from rest of URI, so split that out first
20
+ const uriParts = uri.split('#')
21
+ if (uriParts.length > 2) {
22
+ throw new Error('ATURI can have at most one "#", separating fragment out')
23
+ }
24
+ const fragmentPart = uriParts[1] || null
25
+ uri = uriParts[0]
26
+
27
+ // check that all chars are boring ASCII
28
+ if (!/^[a-zA-Z0-9._~:@!$&')(*+,;=%/-]*$/.test(uri)) {
29
+ throw new Error('Disallowed characters in ATURI (ASCII)')
30
+ }
31
+
32
+ const parts = uri.split('/')
33
+ if (parts.length >= 3 && (parts[0] != 'at:' || parts[1].length != 0)) {
34
+ throw new Error('ATURI must start with "at://"')
35
+ }
36
+ if (parts.length < 3) {
37
+ throw new Error('ATURI requires at least method and authority sections')
38
+ }
39
+
40
+ try {
41
+ ensureValidHandle(parts[2])
42
+ } catch {
43
+ try {
44
+ ensureValidDid(parts[2])
45
+ } catch {
46
+ throw new Error('ATURI authority must be a valid handle or DID')
47
+ }
48
+ }
49
+
50
+ if (parts.length >= 4) {
51
+ if (parts[3].length == 0) {
52
+ throw new Error(
53
+ 'ATURI can not have a slash after authority without a path segment',
54
+ )
55
+ }
56
+ try {
57
+ ensureValidNsid(parts[3])
58
+ } catch {
59
+ throw new Error(
60
+ 'ATURI requires first path segment (if supplied) to be valid NSID',
61
+ )
62
+ }
63
+ }
64
+
65
+ if (parts.length >= 5) {
66
+ if (parts[4].length == 0) {
67
+ throw new Error(
68
+ 'ATURI can not have a slash after collection, unless record key is provided',
69
+ )
70
+ }
71
+ // would validate rkey here, but there are basically no constraints!
72
+ }
73
+
74
+ if (parts.length >= 6) {
75
+ throw new Error(
76
+ 'ATURI path can have at most two parts, and no trailing slash',
77
+ )
78
+ }
79
+
80
+ if (uriParts.length >= 2 && fragmentPart == null) {
81
+ throw new Error('ATURI fragment must be non-empty and start with slash')
82
+ }
83
+
84
+ if (fragmentPart != null) {
85
+ if (fragmentPart.length == 0 || fragmentPart[0] != '/') {
86
+ throw new Error('ATURI fragment must be non-empty and start with slash')
87
+ }
88
+ // NOTE: enforcing *some* checks here for sanity. Eg, at least no whitespace
89
+ if (!/^\/[a-zA-Z0-9._~:@!$&')(*+,;=%[\]/-]*$/.test(fragmentPart)) {
90
+ throw new Error('Disallowed characters in ATURI fragment (ASCII)')
91
+ }
92
+ }
93
+
94
+ if (uri.length > 8 * 1024) {
95
+ throw new Error('ATURI is far too long')
96
+ }
97
+ }
98
+
99
+ export const ensureValidAtUriRegex = (uri: string): void => {
100
+ // simple regex to enforce most constraints via just regex and length.
101
+ // hand wrote this regex based on above constraints. whew!
102
+ const aturiRegex =
103
+ /^at:\/\/(?<authority>[a-zA-Z0-9._:%-]+)(\/(?<collection>[a-zA-Z0-9-.]+)(\/(?<rkey>[a-zA-Z0-9._~:@!$&%')(*+,;=-]+))?)?(#(?<fragment>\/[a-zA-Z0-9._~:@!$&%')(*+,;=\-[\]/\\]*))?$/
104
+ const rm = uri.match(aturiRegex)
105
+ if (!rm || !rm.groups) {
106
+ throw new Error("ATURI didn't validate via regex")
107
+ }
108
+ const groups = rm.groups
109
+
110
+ try {
111
+ ensureValidHandleRegex(groups.authority)
112
+ } catch {
113
+ try {
114
+ ensureValidDidRegex(groups.authority)
115
+ } catch {
116
+ throw new Error('ATURI authority must be a valid handle or DID')
117
+ }
118
+ }
119
+
120
+ if (groups.collection) {
121
+ try {
122
+ ensureValidNsidRegex(groups.collection)
123
+ } catch {
124
+ throw new Error('ATURI collection path segment must be a valid NSID')
125
+ }
126
+ }
127
+
128
+ if (uri.length > 8 * 1024) {
129
+ throw new Error('ATURI is far too long')
130
+ }
131
+ }
package/src/did.ts ADDED
@@ -0,0 +1,58 @@
1
+ // Human-readable constraints:
2
+ // - valid W3C DID (https://www.w3.org/TR/did-core/#did-syntax)
3
+ // - entire URI is ASCII: [a-zA-Z0-9._:%-]
4
+ // - always starts "did:" (lower-case)
5
+ // - method name is one or more lower-case letters, followed by ":"
6
+ // - remaining identifier can have any of the above chars, but can not end in ":"
7
+ // - it seems that a bunch of ":" can be included, and don't need spaces between
8
+ // - "%" is used only for "percent encoding" and must be followed by two hex characters (and thus can't end in "%")
9
+ // - query ("?") and fragment ("#") stuff is defined for "DID URIs", but not as part of identifier itself
10
+ // - "The current specification does not take a position on the maximum length of a DID"
11
+ // - in current atproto, only allowing did:plc and did:web. But not *forcing* this at lexicon layer
12
+ // - hard length limit of 8KBytes
13
+ // - not going to validate "percent encoding" here
14
+ export const ensureValidDid = (did: string): void => {
15
+ // check that all chars are boring ASCII
16
+ if (!/^[a-zA-Z0-9._:%-]*$/.test(did)) {
17
+ throw new InvalidDidError(
18
+ 'Disallowed characters in DID (ASCII letters, digits, and a couple other characters only)',
19
+ )
20
+ }
21
+
22
+ const parts = did.split(':')
23
+ if (parts.length < 3) {
24
+ throw new InvalidDidError(
25
+ 'DID requires prefix, method, and method-specific content',
26
+ )
27
+ }
28
+
29
+ if (parts[0] != 'did') {
30
+ throw new InvalidDidError('DID requires "did:" prefix')
31
+ }
32
+
33
+ if (!/^[a-z]+$/.test(parts[1])) {
34
+ throw new InvalidDidError('DID method must be lower-case letters')
35
+ }
36
+
37
+ if (did.endsWith(':') || did.endsWith('%')) {
38
+ throw new InvalidDidError('DID can not end with ":" or "%"')
39
+ }
40
+
41
+ if (did.length > 2 * 1024) {
42
+ throw new InvalidDidError('DID is too long (2048 chars max)')
43
+ }
44
+ }
45
+
46
+ export const ensureValidDidRegex = (did: string): void => {
47
+ // simple regex to enforce most constraints via just regex and length.
48
+ // hand wrote this regex based on above constraints
49
+ if (!/^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/.test(did)) {
50
+ throw new InvalidDidError("DID didn't validate via regex")
51
+ }
52
+
53
+ if (did.length > 2 * 1024) {
54
+ throw new InvalidDidError('DID is too long (2048 chars max)')
55
+ }
56
+ }
57
+
58
+ export class InvalidDidError extends Error {}
package/src/handle.ts ADDED
@@ -0,0 +1,118 @@
1
+ export const INVALID_HANDLE = 'handle.invalid'
2
+
3
+ // Currently these are registration-time restrictions, not protocol-level
4
+ // restrictions. We have a couple accounts in the wild that we need to clean up
5
+ // before hard-disallow.
6
+ // See also: https://en.wikipedia.org/wiki/Top-level_domain#Reserved_domains
7
+ export const DISALLOWED_TLDS = [
8
+ '.local',
9
+ '.arpa',
10
+ '.invalid',
11
+ '.localhost',
12
+ '.internal',
13
+ // policy could concievably change on ".onion" some day
14
+ '.onion',
15
+ // NOTE: .test is allowed in testing and devopment. In practical terms
16
+ // "should" "never" actually resolve and get registered in production
17
+ ]
18
+
19
+ // Handle constraints, in English:
20
+ // - must be a possible domain name
21
+ // - RFC-1035 is commonly referenced, but has been updated. eg, RFC-3696,
22
+ // section 2. and RFC-3986, section 3. can now have leading numbers (eg,
23
+ // 4chan.org)
24
+ // - "labels" (sub-names) are made of ASCII letters, digits, hyphens
25
+ // - can not start or end with a hyphen
26
+ // - TLD (last component) should not start with a digit
27
+ // - can't end with a hyphen (can end with digit)
28
+ // - each segment must be between 1 and 63 characters (not including any periods)
29
+ // - overall length can't be more than 253 characters
30
+ // - separated by (ASCII) periods; does not start or end with period
31
+ // - case insensitive
32
+ // - domains (handles) are equal if they are the same lower-case
33
+ // - punycode allowed for internationalization
34
+ // - no whitespace, null bytes, joining chars, etc
35
+ // - does not validate whether domain or TLD exists, or is a reserved or
36
+ // special TLD (eg, .onion or .local)
37
+ // - does not validate punycode
38
+ export const ensureValidHandle = (handle: string): void => {
39
+ // check that all chars are boring ASCII
40
+ if (!/^[a-zA-Z0-9.-]*$/.test(handle)) {
41
+ throw new InvalidHandleError(
42
+ 'Disallowed characters in handle (ASCII letters, digits, dashes, periods only)',
43
+ )
44
+ }
45
+
46
+ if (handle.length > 253) {
47
+ throw new InvalidHandleError('Handle is too long (253 chars max)')
48
+ }
49
+ const labels = handle.split('.')
50
+ if (labels.length < 2) {
51
+ throw new InvalidHandleError('Handle domain needs at least two parts')
52
+ }
53
+ for (let i = 0; i < labels.length; i++) {
54
+ const l = labels[i]
55
+ if (l.length < 1) {
56
+ throw new InvalidHandleError('Handle parts can not be empty')
57
+ }
58
+ if (l.length > 63) {
59
+ throw new InvalidHandleError('Handle part too long (max 63 chars)')
60
+ }
61
+ if (l.endsWith('-') || l.startsWith('-')) {
62
+ throw new InvalidHandleError(
63
+ 'Handle parts can not start or end with hyphens',
64
+ )
65
+ }
66
+ if (i + 1 == labels.length && !/^[a-zA-Z]/.test(l)) {
67
+ throw new InvalidHandleError(
68
+ 'Handle final component (TLD) must start with ASCII letter',
69
+ )
70
+ }
71
+ }
72
+ }
73
+
74
+ // simple regex translation of above constraints
75
+ export const ensureValidHandleRegex = (handle: string): void => {
76
+ if (
77
+ !/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/.test(
78
+ handle,
79
+ )
80
+ ) {
81
+ throw new InvalidHandleError("Handle didn't validate via regex")
82
+ }
83
+ if (handle.length > 253) {
84
+ throw new InvalidHandleError('Handle is too long (253 chars max)')
85
+ }
86
+ }
87
+
88
+ export const normalizeHandle = (handle: string): string => {
89
+ return handle.toLowerCase()
90
+ }
91
+
92
+ export const normalizeAndEnsureValidHandle = (handle: string): string => {
93
+ const normalized = normalizeHandle(handle)
94
+ ensureValidHandle(normalized)
95
+ return normalized
96
+ }
97
+
98
+ export const isValidHandle = (handle: string): boolean => {
99
+ try {
100
+ ensureValidHandle(handle)
101
+ } catch (err) {
102
+ if (err instanceof InvalidHandleError) {
103
+ return false
104
+ }
105
+ throw err
106
+ }
107
+
108
+ return true
109
+ }
110
+
111
+ export const isValidTld = (handle: string): boolean => {
112
+ return !DISALLOWED_TLDS.some((domain) => handle.endsWith(domain))
113
+ }
114
+
115
+ export class InvalidHandleError extends Error {}
116
+ export class ReservedHandleError extends Error {}
117
+ export class UnsupportedDomainError extends Error {}
118
+ export class DisallowedDomainError extends Error {}
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './handle'
2
+ export * from './did'
3
+ export * from './nsid'
4
+ export * from './aturi'