|
1 | 1 | import { AppBskyActorDefs } from './client' |
2 | 2 | import { ModerationPrefs } from './moderation/types' |
3 | 3 |
|
4 | | -/** |
5 | | - * Supported proxy targets |
6 | | - */ |
7 | | -type UnknownServiceType = string & NonNullable<unknown> |
| 4 | +export type UnknownServiceType = string & NonNullable<unknown> |
8 | 5 | export type AtprotoServiceType = 'atproto_labeler' | UnknownServiceType |
| 6 | +export function isAtprotoServiceType<T extends string>( |
| 7 | + input: T, |
| 8 | +): input is T & AtprotoServiceType { |
| 9 | + return !input.includes(' ') && !input.includes('#') |
| 10 | +} |
| 11 | + |
| 12 | +// @TODO use tools from @atproto/did |
| 13 | +export type Did = `did:${string}:${string}` |
| 14 | +export function isDid<T extends string>(input: T): input is T & Did { |
| 15 | + if (!input.startsWith('did:')) return false |
| 16 | + if (input.length < 8) return false |
| 17 | + if (input.length > 2048) return false |
| 18 | + const msidx = input.indexOf(':', 4) |
| 19 | + return msidx > 4 && msidx < input.length - 1 |
| 20 | +} |
| 21 | + |
| 22 | +export function assertDid(input: string): asserts input is Did { |
| 23 | + if (!isDid(input)) throw new TypeError(`Invalid DID: ${input}`) |
| 24 | +} |
| 25 | + |
| 26 | +export function asDid<T extends string>(input: T) { |
| 27 | + assertDid(input) |
| 28 | + return input |
| 29 | +} |
| 30 | + |
| 31 | +export type AtprotoProxy = `${Did}#${AtprotoServiceType}` |
| 32 | +export function isAtprotoProxy(input: string): input is AtprotoProxy { |
| 33 | + const { length, [0]: did, [1]: service } = input.split('#') |
| 34 | + return length === 2 && isDid(did) && isAtprotoServiceType(service) |
| 35 | +} |
| 36 | + |
| 37 | +export function assertAtprotoProxy( |
| 38 | + input: string, |
| 39 | +): asserts input is AtprotoProxy { |
| 40 | + if (!isAtprotoProxy(input)) { |
| 41 | + throw new TypeError( |
| 42 | + `Invalid DID reference: ${input} (must be of the form did:example:alice#service)`, |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +export function asAtprotoProxy<T extends string>(input: T) { |
| 48 | + assertAtprotoProxy(input) |
| 49 | + return input |
| 50 | +} |
9 | 51 |
|
10 | 52 | /** |
11 | 53 | * Used by the PersistSessionHandler to indicate what change occurred |
|
0 commit comments