@atproto/lex-resolver 0.0.13 → 0.0.14
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.
- package/CHANGELOG.md +14 -0
- package/dist/lex-resolver-error.d.ts +99 -2
- package/dist/lex-resolver-error.d.ts.map +1 -1
- package/dist/lex-resolver-error.js +94 -3
- package/dist/lex-resolver-error.js.map +1 -1
- package/dist/lex-resolver.d.ts +264 -1
- package/dist/lex-resolver.d.ts.map +1 -1
- package/dist/lex-resolver.js +163 -0
- package/dist/lex-resolver.js.map +1 -1
- package/package.json +8 -7
- package/src/lex-resolver-error.ts +91 -2
- package/src/lex-resolver.ts +269 -2
package/dist/lex-resolver.js
CHANGED
|
@@ -13,6 +13,80 @@ Object.defineProperty(exports, "NSID", { enumerable: true, get: function () { re
|
|
|
13
13
|
const did_resolver_1 = require("@atproto-labs/did-resolver");
|
|
14
14
|
const lex_resolver_error_js_1 = require("./lex-resolver-error.js");
|
|
15
15
|
const index_js_1 = require("./lexicons/index.js");
|
|
16
|
+
/**
|
|
17
|
+
* Resolves Lexicon documents from the AT Protocol network.
|
|
18
|
+
*
|
|
19
|
+
* The {@link LexResolver} handles the complete process of resolving a lexicon
|
|
20
|
+
* by NSID:
|
|
21
|
+
* 1. **Authority Resolution**: Looks up the `_lexicon.<authority>` DNS TXT record
|
|
22
|
+
* to find the DID that controls lexicons for that namespace
|
|
23
|
+
* 2. **DID Resolution**: Resolves the DID document to find the PDS endpoint and
|
|
24
|
+
* signing key
|
|
25
|
+
* 3. **Record Fetch**: Fetches the lexicon record from the PDS with cryptographic
|
|
26
|
+
* proof verification
|
|
27
|
+
* 4. **Validation**: Validates the lexicon document structure
|
|
28
|
+
*
|
|
29
|
+
* @example Basic usage - resolve a lexicon by NSID
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { LexResolver } from '@atproto/lex-resolver'
|
|
32
|
+
*
|
|
33
|
+
* const resolver = new LexResolver({})
|
|
34
|
+
*
|
|
35
|
+
* // Get a lexicon document by its NSID
|
|
36
|
+
* const result = await resolver.get('app.bsky.feed.post')
|
|
37
|
+
* console.log(result.lexicon) // The parsed lexicon document
|
|
38
|
+
* console.log(result.uri) // AT URI where it was found
|
|
39
|
+
* console.log(result.cid) // Content identifier for verification
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example Two-step resolution for more control
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { LexResolver } from '@atproto/lex-resolver'
|
|
45
|
+
*
|
|
46
|
+
* const resolver = new LexResolver({})
|
|
47
|
+
*
|
|
48
|
+
* // Step 1: Resolve the authority to get the AT URI
|
|
49
|
+
* const uri = await resolver.resolve('app.bsky.feed.post')
|
|
50
|
+
* console.log(uri.toString()) // 'at://did:plc:xxx/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
51
|
+
*
|
|
52
|
+
* // Step 2: Fetch the lexicon from the URI
|
|
53
|
+
* const result = await resolver.fetch(uri)
|
|
54
|
+
* console.log(result.lexicon)
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @example Using hooks for caching
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { LexResolver, LexResolverFetchResult } from '@atproto/lex-resolver'
|
|
60
|
+
*
|
|
61
|
+
* const cache = new Map<string, LexResolverFetchResult>()
|
|
62
|
+
*
|
|
63
|
+
* const resolver = new LexResolver({
|
|
64
|
+
* hooks: {
|
|
65
|
+
* onFetch({ uri }) {
|
|
66
|
+
* return cache.get(uri.toString())
|
|
67
|
+
* },
|
|
68
|
+
* onFetchResult({ uri, cid, lexicon }) {
|
|
69
|
+
* cache.set(uri.toString(), { cid, lexicon })
|
|
70
|
+
* }
|
|
71
|
+
* }
|
|
72
|
+
* })
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @example Error handling
|
|
76
|
+
* ```typescript
|
|
77
|
+
* import { LexResolver, LexResolverError } from '@atproto/lex-resolver'
|
|
78
|
+
*
|
|
79
|
+
* const resolver = new LexResolver({})
|
|
80
|
+
*
|
|
81
|
+
* try {
|
|
82
|
+
* const result = await resolver.get('com.example.unknown')
|
|
83
|
+
* } catch (error) {
|
|
84
|
+
* if (error instanceof LexResolverError) {
|
|
85
|
+
* console.error(`Failed to resolve ${error.nsid}: ${error.description}`)
|
|
86
|
+
* }
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
16
90
|
class LexResolver {
|
|
17
91
|
options;
|
|
18
92
|
didResolver;
|
|
@@ -20,10 +94,66 @@ class LexResolver {
|
|
|
20
94
|
this.options = options;
|
|
21
95
|
this.didResolver = (0, did_resolver_1.createDidResolver)(options);
|
|
22
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets a lexicon document by its NSID.
|
|
99
|
+
*
|
|
100
|
+
* This is the primary method for resolving lexicons. It combines
|
|
101
|
+
* {@link resolve} and {@link fetch} into a single operation, handling
|
|
102
|
+
* authority resolution, DID lookup, and record fetching.
|
|
103
|
+
*
|
|
104
|
+
* @param nsidStr - The NSID to resolve, either as a string or NSID object
|
|
105
|
+
* @param options - Optional DID resolution options (e.g., signal for cancellation)
|
|
106
|
+
* @returns The resolved lexicon result containing URI, CID, and lexicon document
|
|
107
|
+
* @throws {LexResolverError} If resolution fails at any stage
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* // Resolve using string NSID
|
|
112
|
+
* const result = await resolver.get('app.bsky.feed.post')
|
|
113
|
+
*
|
|
114
|
+
* // Resolve using NSID object
|
|
115
|
+
* import { NSID } from '@atproto/syntax'
|
|
116
|
+
* const nsid = NSID.from('app.bsky.feed.post')
|
|
117
|
+
* const result = await resolver.get(nsid)
|
|
118
|
+
*
|
|
119
|
+
* // With abort signal for cancellation
|
|
120
|
+
* const controller = new AbortController()
|
|
121
|
+
* const result = await resolver.get('app.bsky.feed.post', {
|
|
122
|
+
* signal: controller.signal
|
|
123
|
+
* })
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
23
126
|
async get(nsidStr, options) {
|
|
24
127
|
const uri = await this.resolve(nsidStr);
|
|
25
128
|
return this.fetch(uri, options);
|
|
26
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Resolves the authority for an NSID and returns the AT URI for the lexicon.
|
|
132
|
+
*
|
|
133
|
+
* This method performs the first stage of lexicon resolution:
|
|
134
|
+
* 1. Parses the NSID to extract the authority domain
|
|
135
|
+
* 2. Looks up the `_lexicon.<authority>` DNS TXT record
|
|
136
|
+
* 3. Extracts the DID from the TXT record (format: `did=<did>`)
|
|
137
|
+
* 4. Constructs the AT URI for the lexicon record
|
|
138
|
+
*
|
|
139
|
+
* Use this when you need the URI without fetching the actual document,
|
|
140
|
+
* or when you want to implement custom fetching logic.
|
|
141
|
+
*
|
|
142
|
+
* @param nsidStr - The NSID to resolve, either as a string or NSID object
|
|
143
|
+
* @returns The AT URI pointing to the lexicon record
|
|
144
|
+
* @throws {LexResolverError} If authority resolution fails (e.g., DNS lookup fails)
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* // Resolve to get the AT URI
|
|
149
|
+
* const uri = await resolver.resolve('app.bsky.feed.post')
|
|
150
|
+
* console.log(uri.toString())
|
|
151
|
+
* // Output: 'at://did:plc:z72i7hdynmk6r22z27h6tvur/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
152
|
+
*
|
|
153
|
+
* // The URI can then be used with fetch() or stored for later use
|
|
154
|
+
* const result = await resolver.fetch(uri)
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
27
157
|
async resolve(nsidStr) {
|
|
28
158
|
const nsid = syntax_1.NSID.from(nsidStr);
|
|
29
159
|
const did = (await this.options.hooks?.onResolveAuthority?.({ nsid })) ??
|
|
@@ -48,6 +178,39 @@ class LexResolver {
|
|
|
48
178
|
throw new lex_resolver_error_js_1.LexResolverError(nsid, `Failed to resolve lexicon DID authority for ${nsid}`, { cause });
|
|
49
179
|
}
|
|
50
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Fetches a lexicon document from a specific AT URI.
|
|
183
|
+
*
|
|
184
|
+
* This method performs the second stage of lexicon resolution:
|
|
185
|
+
* 1. Resolves the DID from the URI to find the PDS endpoint
|
|
186
|
+
* 2. Fetches the record from the PDS using `com.atproto.sync.getRecord`
|
|
187
|
+
* 3. Verifies the cryptographic proof (commit signature)
|
|
188
|
+
* 4. Validates the lexicon document structure
|
|
189
|
+
* 5. Ensures the document ID matches the URI rkey
|
|
190
|
+
*
|
|
191
|
+
* Use this when you already have an AT URI (e.g., from {@link resolve})
|
|
192
|
+
* and want to fetch the lexicon document.
|
|
193
|
+
*
|
|
194
|
+
* @param uriStr - The AT URI to fetch, either as a string or AtUri object
|
|
195
|
+
* @param options - Optional DID resolution options (e.g., signal for cancellation, noCache)
|
|
196
|
+
* @returns The resolved lexicon result containing URI, CID, and lexicon document
|
|
197
|
+
* @throws {LexResolverError} If fetching or validation fails
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* // Fetch from a known URI
|
|
202
|
+
* const result = await resolver.fetch(
|
|
203
|
+
* 'at://did:plc:xyz/com.atproto.lexicon.schema/app.bsky.feed.post'
|
|
204
|
+
* )
|
|
205
|
+
*
|
|
206
|
+
* // Fetch with no-cache to bypass any upstream caching
|
|
207
|
+
* const result = await resolver.fetch(uri, { noCache: true })
|
|
208
|
+
*
|
|
209
|
+
* // Fetch with abort signal
|
|
210
|
+
* const controller = new AbortController()
|
|
211
|
+
* const result = await resolver.fetch(uri, { signal: controller.signal })
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
51
214
|
async fetch(uriStr, options) {
|
|
52
215
|
const uri = typeof uriStr === 'string' ? new syntax_1.AtUri(uriStr) : uriStr;
|
|
53
216
|
const { lexicon, cid } = (await this.options.hooks?.onFetch?.({ uri })) ??
|
package/dist/lex-resolver.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":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QAAQ,GAAG,CAAC,IAAI,EAAE,CAAC;QACjB,KAAK,mCAAmC,CAAC,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;YAChE,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAA;QAC3D,CAAC;QACD,KAAK,mCAAmC,CAAC,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;YAChE,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAA;QAChE,CAAC;QACD,KAAK,UAAU,CAAC,CAAC,CAAC;YAChB,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;YACzE,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACR,sBAAsB;YACtB,MAAM,IAAI,KAAK,CAAC,yCAAyC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;QACtE,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { resolveTxt } from 'node:dns/promises'\nimport * as crypto from '@atproto/crypto'\nimport { buildAgent, xrpc } from '@atproto/lex-client'\nimport { Cid } from '@atproto/lex-data'\nimport { LexiconDocument, lexiconDocumentSchema } from '@atproto/lex-document'\nimport {\n MST,\n MemoryBlockstore,\n def as repoDef,\n readCarWithRoot,\n verifyCommitSig,\n} from '@atproto/repo'\nimport { AtUri, NSID, NsidString } from '@atproto/syntax'\nimport {\n AtprotoVerificationMethod,\n CreateDidResolverOptions,\n Did,\n DidResolver,\n ResolveDidOptions,\n assertDid,\n createDidResolver,\n extractAtprotoData,\n} from '@atproto-labs/did-resolver'\nimport { LexResolverError } from './lex-resolver-error.js'\nimport { com } from './lexicons/index.js'\n\nexport type LexResolverResult = {\n uri: AtUri\n cid: Cid\n lexicon: LexiconDocument\n}\n\nexport type LexResolverFetchResult = {\n cid: Cid\n lexicon: LexiconDocument\n}\n\ntype Awaitable<T> = T | Promise<T>\n\nexport type LexResolverHooks = {\n /**\n * Hook called before resolving a lexicon authority DID. If a DID is returned,\n * it will be used instead of performing the default resolution. In that case,\n * the `onResolveAuthorityResult` and `onResolveAuthorityError` hooks will\n * not be called.\n */\n onResolveAuthority?(data: { nsid: NSID }): Awaitable<void | Did>\n onResolveAuthorityResult?(data: { nsid: NSID; did: Did }): Awaitable<void>\n onResolveAuthorityError?(data: { nsid: NSID; err: unknown }): Awaitable<void>\n\n /**\n * Hook called before fetching a lexicon URI. If a result is returned, it will\n * be used instead of performing the default fetch. In that case, the\n * `onFetchResult` and `onFetchError` hooks will not be called.\n */\n onFetch?(data: { uri: AtUri }): Awaitable<void | LexResolverFetchResult>\n onFetchResult?(data: {\n uri: AtUri\n cid: Cid\n lexicon: LexiconDocument\n }): Awaitable<void>\n onFetchError?(data: { uri: AtUri; err: unknown }): Awaitable<void>\n}\n\nexport type LexResolverOptions = CreateDidResolverOptions & {\n hooks?: LexResolverHooks\n}\n\nexport { AtUri, type Cid, NSID }\nexport type { LexiconDocument, ResolveDidOptions }\n\nexport class LexResolver {\n protected readonly didResolver: DidResolver<'plc' | 'web'>\n\n constructor(protected readonly options: LexResolverOptions) {\n this.didResolver = createDidResolver(options)\n }\n\n async get(\n nsidStr: NSID | string,\n options?: ResolveDidOptions,\n ): Promise<LexResolverResult> {\n const uri = await this.resolve(nsidStr)\n return this.fetch(uri, options)\n }\n\n async resolve(nsidStr: NSID | string): Promise<AtUri> {\n const nsid = NSID.from(nsidStr)\n\n const did =\n (await this.options.hooks?.onResolveAuthority?.({ nsid })) ??\n (await this.resolveLexiconAuthority(nsid).then(\n async (did) => {\n await this.options.hooks?.onResolveAuthorityResult?.({ nsid, did })\n return did\n },\n async (err) => {\n await this.options.hooks?.onResolveAuthorityError?.({ nsid, err })\n throw err\n },\n ))\n\n return AtUri.make(did, 'com.atproto.lexicon.schema', nsid.toString())\n }\n\n // @TODO This class could be made compatible with browsers by making the\n // following method abstract and/or by allowing the caller to inject a DNS\n // resolver implementation (based on DNS-over-HTTPS or similar), instead of\n // using the Node.js built-in resolver.\n protected async resolveLexiconAuthority(nsid: NSID): Promise<Did> {\n try {\n return await getDomainTxtDid(`_lexicon.${nsid.authority}`)\n } catch (cause) {\n throw new LexResolverError(\n nsid,\n `Failed to resolve lexicon DID authority for ${nsid}`,\n { cause },\n )\n }\n }\n\n async fetch(\n uriStr: AtUri | string,\n options?: ResolveDidOptions,\n ): Promise<LexResolverResult> {\n const uri = typeof uriStr === 'string' ? new AtUri(uriStr) : uriStr\n\n const { lexicon, cid } =\n (await this.options.hooks?.onFetch?.({ uri })) ??\n (await this.fetchLexiconUri(uri, options).then(\n async (res) => {\n await this.options.hooks?.onFetchResult?.({ uri, ...res })\n return res\n },\n async (err) => {\n await this.options.hooks?.onFetchError?.({ uri, err })\n throw err\n },\n ))\n\n return { uri, cid, lexicon }\n }\n\n protected async fetchLexiconUri(\n uri: AtUri,\n options?: ResolveDidOptions,\n ): Promise<LexResolverFetchResult> {\n const { did, nsid } = parseLexiconUri(uri)\n\n const { pds, key } = await this.didResolver\n .resolve(did, options)\n .then(extractAtprotoData)\n .catch((cause) => {\n throw new LexResolverError(\n nsid,\n `Failed to resolve DID document for ${did}`,\n { cause },\n )\n })\n\n if (!key || !pds || !URL.canParse(pds.serviceEndpoint)) {\n throw new LexResolverError(\n nsid,\n `No atproto PDS service endpoint or signing key found in ${did} DID document`,\n )\n }\n\n const agent = buildAgent({\n service: pds.serviceEndpoint,\n fetch: this.options.fetch,\n })\n\n const collection = 'com.atproto.lexicon.schema'\n const rkey = nsid.toString()\n\n const { cid, record } = await xrpc(agent, com.atproto.sync.getRecord, {\n signal: options?.signal,\n headers: options?.noCache ? { 'Cache-Control': 'no-cache' } : undefined,\n params: { did, collection, rkey },\n }).then(\n ({ body }) => {\n return verifyRecordProof(body, did, key, collection, rkey).catch(\n (cause) => {\n throw new LexResolverError(\n nsid,\n `Failed to verify Lexicon record proof at ${uri}`,\n { cause },\n )\n },\n )\n },\n (cause) => {\n throw new LexResolverError(nsid, `Failed to fetch Record ${uri}`, {\n cause,\n })\n },\n )\n\n const validationResult = lexiconDocumentSchema.safeParse(record)\n if (!validationResult.success) {\n throw new LexResolverError(nsid, `Invalid Lexicon document at ${uri}`, {\n cause: validationResult.reason,\n })\n }\n\n const lexicon = validationResult.value\n if (lexicon.id !== uri.rkey) {\n throw new LexResolverError(\n nsid,\n `Invalid document id \"${lexicon.id}\" at ${uri}`,\n )\n }\n\n return { lexicon, cid }\n }\n}\n\nfunction parseLexiconUri(uri: AtUri): {\n did: Did\n nsid: NSID\n} {\n // Validate input URI\n const nsid = NSID.from(uri.rkey)\n try {\n const did = uri.host\n assertDid(did)\n return { did, nsid }\n } catch (cause) {\n throw new LexResolverError(nsid, `URI host is not a DID ${uri}`, { cause })\n }\n}\n\nasync function getDomainTxtDid(domain: string): Promise<Did> {\n const didLines = (await resolveTxt(domain))\n .map((chunks) => chunks.join(''))\n .filter((i) => i.startsWith('did='))\n\n if (didLines.length === 1) {\n const did = didLines[0].slice(4)\n assertDid(did)\n return did\n }\n\n throw didLines.length > 1\n ? new Error('Multiple DIDs found in DNS TXT records')\n : new Error('No DID found in DNS TXT records')\n}\n\nasync function verifyRecordProof(\n car: Uint8Array,\n did: Did,\n key: AtprotoVerificationMethod,\n collection: NsidString,\n rkey: string,\n) {\n const { root, blocks } = await readCarWithRoot(car)\n const blockstore = new MemoryBlockstore(blocks)\n\n const commit = await blockstore.readObj(root, repoDef.commit)\n if (commit.did !== did) {\n throw new Error(`Invalid repo did: ${commit.did}`)\n }\n\n const signingKey = getDidKeyFromMultibase(key)\n const validSig = await verifyCommitSig(commit, signingKey)\n if (!validSig) {\n throw new Error(`Invalid signature on commit: ${root.toString()}`)\n }\n\n const mst = MST.load(blockstore, commit.data)\n\n const cid = await mst.get(`${collection}/${rkey}`)\n if (!cid) throw new Error('Record not found in proof')\n\n const record = await blockstore.readRecord(cid)\n if (record?.$type !== collection) {\n throw new Error(\n `Invalid record type: expected ${collection}, got ${record?.$type}`,\n )\n }\n\n return { cid, record }\n}\n\nfunction getDidKeyFromMultibase(key: AtprotoVerificationMethod) {\n switch (key.type) {\n case 'EcdsaSecp256r1VerificationKey2019': {\n const keyBytes = crypto.multibaseToBytes(key.publicKeyMultibase)\n return crypto.formatDidKey(crypto.P256_JWT_ALG, keyBytes)\n }\n case 'EcdsaSecp256k1VerificationKey2019': {\n const keyBytes = crypto.multibaseToBytes(key.publicKeyMultibase)\n return crypto.formatDidKey(crypto.SECP256K1_JWT_ALG, keyBytes)\n }\n case 'Multikey': {\n const { jwtAlg, keyBytes } = crypto.parseMultikey(key.publicKeyMultibase)\n return crypto.formatDidKey(jwtAlg, keyBytes)\n }\n default: {\n // Should never happen\n throw new Error(`Unsupported verification method type: ${key.type}`)\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"lex-resolver.js","sourceRoot":"","sources":["../src/lex-resolver.ts"],"names":[],"mappings":";;;;AAAA,gDAA8C;AAC9C,gEAAyC;AACzC,oDAAsD;AAEtD,wDAA8E;AAC9E,wCAMsB;AACtB,4CAAyD;AAgKhD,sFAhKA,cAAK,OAgKA;AAAY,qFAhKV,aAAI,OAgKU;AA/J9B,6DASmC;AACnC,mEAA0D;AAC1D,kDAAyC;AAuJzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AACH,MAAa,WAAW;IAGS;IAFZ,WAAW,CAA4B;IAE1D,YAA+B,OAA2B;QAA3B,YAAO,GAAP,OAAO,CAAoB;QACxD,IAAI,CAAC,WAAW,GAAG,IAAA,gCAAiB,EAAC,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,CAAC,CAAC;YACzC,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;YAChE,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,iBAAiB,EAAE,QAAQ,CAAC,CAAA;QAChE,CAAC;QACD,KAAK,UAAU,CAAC,CAAC,CAAC;YAChB,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;YACzE,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACR,sBAAsB;YACtB,MAAM,IAAI,KAAK,CAAC,yCAAyC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAA;QACtE,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { resolveTxt } from 'node:dns/promises'\nimport * as crypto from '@atproto/crypto'\nimport { buildAgent, xrpc } from '@atproto/lex-client'\nimport { Cid } from '@atproto/lex-data'\nimport { LexiconDocument, lexiconDocumentSchema } from '@atproto/lex-document'\nimport {\n MST,\n MemoryBlockstore,\n def as repoDef,\n readCarWithRoot,\n verifyCommitSig,\n} from '@atproto/repo'\nimport { AtUri, NSID, NsidString } from '@atproto/syntax'\nimport {\n AtprotoVerificationMethod,\n CreateDidResolverOptions,\n Did,\n DidResolver,\n ResolveDidOptions,\n assertDid,\n createDidResolver,\n extractAtprotoData,\n} from '@atproto-labs/did-resolver'\nimport { LexResolverError } from './lex-resolver-error.js'\nimport { com } from './lexicons/index.js'\n\n/**\n * Result returned when successfully resolving a lexicon document.\n *\n * Contains the full AT URI where the lexicon was found, the content-addressed\n * identifier (CID) for integrity verification, and the parsed lexicon document.\n */\nexport type LexResolverResult = {\n /** The AT URI where the lexicon document was found */\n uri: AtUri\n /** Content identifier (CID) of the lexicon record for integrity verification */\n cid: Cid\n /** The parsed and validated lexicon document */\n lexicon: LexiconDocument\n}\n\n/**\n * Result returned when fetching a lexicon document from a specific URI.\n *\n * This is a subset of {@link LexResolverResult} used internally and by hooks,\n * containing only the CID and lexicon document (without the URI, which is\n * already known from the fetch request).\n */\nexport type LexResolverFetchResult = {\n /** Content identifier (CID) of the lexicon record */\n cid: Cid\n /** The parsed and validated lexicon document */\n lexicon: LexiconDocument\n}\n\nexport type Awaitable<T> = T | Promise<T>\n\n/**\n * Callback hooks for customizing the lexicon resolution process.\n *\n * Hooks allow you to intercept, cache, or override the default resolution\n * behavior at various stages. Each hook can be synchronous or asynchronous.\n *\n * @example Implementing a cache with hooks\n * ```typescript\n * import { LexResolver, LexResolverHooks, LexResolverFetchResult } from '@atproto/lex-resolver'\n * import { AtUri } from '@atproto/syntax'\n *\n * const cache = new Map<string, LexResolverFetchResult>()\n *\n * const hooks: LexResolverHooks = {\n * // Return cached result if available, bypassing network fetch\n * onFetch({ uri }) {\n * return cache.get(uri.toString())\n * },\n * // Cache successful fetches\n * onFetchResult({ uri, cid, lexicon }) {\n * cache.set(uri.toString(), { cid, lexicon })\n * },\n * // Log errors for monitoring\n * onFetchError({ uri, err }) {\n * console.error(`Failed to fetch ${uri}:`, err)\n * }\n * }\n *\n * const resolver = new LexResolver({ hooks })\n * ```\n *\n * @example Overriding authority resolution for testing\n * ```typescript\n * const hooks: LexResolverHooks = {\n * // Always resolve to a test DID\n * onResolveAuthority({ nsid }) {\n * if (nsid.authority === 'test.example') {\n * return 'did:plc:test123'\n * }\n * // Return undefined to use default resolution\n * }\n * }\n * ```\n */\nexport type LexResolverHooks = {\n /**\n * Hook called before resolving a lexicon authority DID. If a DID is returned,\n * it will be used instead of performing the default resolution. In that case,\n * the `onResolveAuthorityResult` and `onResolveAuthorityError` hooks will\n * not be called.\n *\n * @param data - Object containing the NSID being resolved\n * @returns A DID to use instead of default resolution, or void/undefined to proceed normally\n */\n onResolveAuthority?(data: { nsid: NSID }): Awaitable<void | Did>\n\n /**\n * Hook called after successfully resolving a lexicon authority DID.\n *\n * @param data - Object containing the NSID and resolved DID\n */\n onResolveAuthorityResult?(data: { nsid: NSID; did: Did }): Awaitable<void>\n\n /**\n * Hook called when authority resolution fails.\n *\n * @param data - Object containing the NSID and error that occurred\n */\n onResolveAuthorityError?(data: { nsid: NSID; err: unknown }): Awaitable<void>\n\n /**\n * Hook called before fetching a lexicon URI. If a result is returned, it will\n * be used instead of performing the default fetch. In that case, the\n * `onFetchResult` and `onFetchError` hooks will not be called.\n *\n * @param data - Object containing the URI being fetched\n * @returns A fetch result to use instead of default fetch, or void/undefined to proceed normally\n */\n onFetch?(data: { uri: AtUri }): Awaitable<void | LexResolverFetchResult>\n\n /**\n * Hook called after successfully fetching a lexicon document.\n *\n * @param data - Object containing the URI, CID, and parsed lexicon document\n */\n onFetchResult?(data: {\n uri: AtUri\n cid: Cid\n lexicon: LexiconDocument\n }): Awaitable<void>\n\n /**\n * Hook called when fetching fails.\n *\n * @param data - Object containing the URI and error that occurred\n */\n onFetchError?(data: { uri: AtUri; err: unknown }): Awaitable<void>\n}\n\n/**\n * Configuration options for the {@link LexResolver}.\n *\n * Extends DID resolver options with lexicon-specific hooks for customizing\n * the resolution process.\n *\n * @see {@link CreateDidResolverOptions} for DID resolver configuration\n */\nexport type LexResolverOptions = CreateDidResolverOptions & {\n /**\n * Optional hooks for customizing the resolution process.\n * See {@link LexResolverHooks} for available callbacks.\n */\n hooks?: LexResolverHooks\n}\n\nexport { AtUri, type Cid, NSID }\nexport type { LexiconDocument, ResolveDidOptions }\n\n/**\n * Resolves Lexicon documents from the AT Protocol network.\n *\n * The {@link LexResolver} handles the complete process of resolving a lexicon\n * by NSID:\n * 1. **Authority Resolution**: Looks up the `_lexicon.<authority>` DNS TXT record\n * to find the DID that controls lexicons for that namespace\n * 2. **DID Resolution**: Resolves the DID document to find the PDS endpoint and\n * signing key\n * 3. **Record Fetch**: Fetches the lexicon record from the PDS with cryptographic\n * proof verification\n * 4. **Validation**: Validates the lexicon document structure\n *\n * @example Basic usage - resolve a lexicon by NSID\n * ```typescript\n * import { LexResolver } from '@atproto/lex-resolver'\n *\n * const resolver = new LexResolver({})\n *\n * // Get a lexicon document by its NSID\n * const result = await resolver.get('app.bsky.feed.post')\n * console.log(result.lexicon) // The parsed lexicon document\n * console.log(result.uri) // AT URI where it was found\n * console.log(result.cid) // Content identifier for verification\n * ```\n *\n * @example Two-step resolution for more control\n * ```typescript\n * import { LexResolver } from '@atproto/lex-resolver'\n *\n * const resolver = new LexResolver({})\n *\n * // Step 1: Resolve the authority to get the AT URI\n * const uri = await resolver.resolve('app.bsky.feed.post')\n * console.log(uri.toString()) // 'at://did:plc:xxx/com.atproto.lexicon.schema/app.bsky.feed.post'\n *\n * // Step 2: Fetch the lexicon from the URI\n * const result = await resolver.fetch(uri)\n * console.log(result.lexicon)\n * ```\n *\n * @example Using hooks for caching\n * ```typescript\n * import { LexResolver, LexResolverFetchResult } from '@atproto/lex-resolver'\n *\n * const cache = new Map<string, LexResolverFetchResult>()\n *\n * const resolver = new LexResolver({\n * hooks: {\n * onFetch({ uri }) {\n * return cache.get(uri.toString())\n * },\n * onFetchResult({ uri, cid, lexicon }) {\n * cache.set(uri.toString(), { cid, lexicon })\n * }\n * }\n * })\n * ```\n *\n * @example Error handling\n * ```typescript\n * import { LexResolver, LexResolverError } from '@atproto/lex-resolver'\n *\n * const resolver = new LexResolver({})\n *\n * try {\n * const result = await resolver.get('com.example.unknown')\n * } catch (error) {\n * if (error instanceof LexResolverError) {\n * console.error(`Failed to resolve ${error.nsid}: ${error.description}`)\n * }\n * }\n * ```\n */\nexport class LexResolver {\n protected readonly didResolver: DidResolver<'plc' | 'web'>\n\n constructor(protected readonly options: LexResolverOptions) {\n this.didResolver = createDidResolver(options)\n }\n\n /**\n * Gets a lexicon document by its NSID.\n *\n * This is the primary method for resolving lexicons. It combines\n * {@link resolve} and {@link fetch} into a single operation, handling\n * authority resolution, DID lookup, and record fetching.\n *\n * @param nsidStr - The NSID to resolve, either as a string or NSID object\n * @param options - Optional DID resolution options (e.g., signal for cancellation)\n * @returns The resolved lexicon result containing URI, CID, and lexicon document\n * @throws {LexResolverError} If resolution fails at any stage\n *\n * @example\n * ```typescript\n * // Resolve using string NSID\n * const result = await resolver.get('app.bsky.feed.post')\n *\n * // Resolve using NSID object\n * import { NSID } from '@atproto/syntax'\n * const nsid = NSID.from('app.bsky.feed.post')\n * const result = await resolver.get(nsid)\n *\n * // With abort signal for cancellation\n * const controller = new AbortController()\n * const result = await resolver.get('app.bsky.feed.post', {\n * signal: controller.signal\n * })\n * ```\n */\n async get(\n nsidStr: NSID | string,\n options?: ResolveDidOptions,\n ): Promise<LexResolverResult> {\n const uri = await this.resolve(nsidStr)\n return this.fetch(uri, options)\n }\n\n /**\n * Resolves the authority for an NSID and returns the AT URI for the lexicon.\n *\n * This method performs the first stage of lexicon resolution:\n * 1. Parses the NSID to extract the authority domain\n * 2. Looks up the `_lexicon.<authority>` DNS TXT record\n * 3. Extracts the DID from the TXT record (format: `did=<did>`)\n * 4. Constructs the AT URI for the lexicon record\n *\n * Use this when you need the URI without fetching the actual document,\n * or when you want to implement custom fetching logic.\n *\n * @param nsidStr - The NSID to resolve, either as a string or NSID object\n * @returns The AT URI pointing to the lexicon record\n * @throws {LexResolverError} If authority resolution fails (e.g., DNS lookup fails)\n *\n * @example\n * ```typescript\n * // Resolve to get the AT URI\n * const uri = await resolver.resolve('app.bsky.feed.post')\n * console.log(uri.toString())\n * // Output: 'at://did:plc:z72i7hdynmk6r22z27h6tvur/com.atproto.lexicon.schema/app.bsky.feed.post'\n *\n * // The URI can then be used with fetch() or stored for later use\n * const result = await resolver.fetch(uri)\n * ```\n */\n async resolve(nsidStr: NSID | string): Promise<AtUri> {\n const nsid = NSID.from(nsidStr)\n\n const did =\n (await this.options.hooks?.onResolveAuthority?.({ nsid })) ??\n (await this.resolveLexiconAuthority(nsid).then(\n async (did) => {\n await this.options.hooks?.onResolveAuthorityResult?.({ nsid, did })\n return did\n },\n async (err) => {\n await this.options.hooks?.onResolveAuthorityError?.({ nsid, err })\n throw err\n },\n ))\n\n return AtUri.make(did, 'com.atproto.lexicon.schema', nsid.toString())\n }\n\n // @TODO This class could be made compatible with browsers by making the\n // following method abstract and/or by allowing the caller to inject a DNS\n // resolver implementation (based on DNS-over-HTTPS or similar), instead of\n // using the Node.js built-in resolver.\n protected async resolveLexiconAuthority(nsid: NSID): Promise<Did> {\n try {\n return await getDomainTxtDid(`_lexicon.${nsid.authority}`)\n } catch (cause) {\n throw new LexResolverError(\n nsid,\n `Failed to resolve lexicon DID authority for ${nsid}`,\n { cause },\n )\n }\n }\n\n /**\n * Fetches a lexicon document from a specific AT URI.\n *\n * This method performs the second stage of lexicon resolution:\n * 1. Resolves the DID from the URI to find the PDS endpoint\n * 2. Fetches the record from the PDS using `com.atproto.sync.getRecord`\n * 3. Verifies the cryptographic proof (commit signature)\n * 4. Validates the lexicon document structure\n * 5. Ensures the document ID matches the URI rkey\n *\n * Use this when you already have an AT URI (e.g., from {@link resolve})\n * and want to fetch the lexicon document.\n *\n * @param uriStr - The AT URI to fetch, either as a string or AtUri object\n * @param options - Optional DID resolution options (e.g., signal for cancellation, noCache)\n * @returns The resolved lexicon result containing URI, CID, and lexicon document\n * @throws {LexResolverError} If fetching or validation fails\n *\n * @example\n * ```typescript\n * // Fetch from a known URI\n * const result = await resolver.fetch(\n * 'at://did:plc:xyz/com.atproto.lexicon.schema/app.bsky.feed.post'\n * )\n *\n * // Fetch with no-cache to bypass any upstream caching\n * const result = await resolver.fetch(uri, { noCache: true })\n *\n * // Fetch with abort signal\n * const controller = new AbortController()\n * const result = await resolver.fetch(uri, { signal: controller.signal })\n * ```\n */\n async fetch(\n uriStr: AtUri | string,\n options?: ResolveDidOptions,\n ): Promise<LexResolverResult> {\n const uri = typeof uriStr === 'string' ? new AtUri(uriStr) : uriStr\n\n const { lexicon, cid } =\n (await this.options.hooks?.onFetch?.({ uri })) ??\n (await this.fetchLexiconUri(uri, options).then(\n async (res) => {\n await this.options.hooks?.onFetchResult?.({ uri, ...res })\n return res\n },\n async (err) => {\n await this.options.hooks?.onFetchError?.({ uri, err })\n throw err\n },\n ))\n\n return { uri, cid, lexicon }\n }\n\n protected async fetchLexiconUri(\n uri: AtUri,\n options?: ResolveDidOptions,\n ): Promise<LexResolverFetchResult> {\n const { did, nsid } = parseLexiconUri(uri)\n\n const { pds, key } = await this.didResolver\n .resolve(did, options)\n .then(extractAtprotoData)\n .catch((cause) => {\n throw new LexResolverError(\n nsid,\n `Failed to resolve DID document for ${did}`,\n { cause },\n )\n })\n\n if (!key || !pds || !URL.canParse(pds.serviceEndpoint)) {\n throw new LexResolverError(\n nsid,\n `No atproto PDS service endpoint or signing key found in ${did} DID document`,\n )\n }\n\n const agent = buildAgent({\n service: pds.serviceEndpoint,\n fetch: this.options.fetch,\n })\n\n const collection = 'com.atproto.lexicon.schema'\n const rkey = nsid.toString()\n\n const { cid, record } = await xrpc(agent, com.atproto.sync.getRecord, {\n signal: options?.signal,\n headers: options?.noCache ? { 'Cache-Control': 'no-cache' } : undefined,\n params: { did, collection, rkey },\n }).then(\n ({ body }) => {\n return verifyRecordProof(body, did, key, collection, rkey).catch(\n (cause) => {\n throw new LexResolverError(\n nsid,\n `Failed to verify Lexicon record proof at ${uri}`,\n { cause },\n )\n },\n )\n },\n (cause) => {\n throw new LexResolverError(nsid, `Failed to fetch Record ${uri}`, {\n cause,\n })\n },\n )\n\n const validationResult = lexiconDocumentSchema.safeParse(record)\n if (!validationResult.success) {\n throw new LexResolverError(nsid, `Invalid Lexicon document at ${uri}`, {\n cause: validationResult.reason,\n })\n }\n\n const lexicon = validationResult.value\n if (lexicon.id !== uri.rkey) {\n throw new LexResolverError(\n nsid,\n `Invalid document id \"${lexicon.id}\" at ${uri}`,\n )\n }\n\n return { lexicon, cid }\n }\n}\n\nfunction parseLexiconUri(uri: AtUri): {\n did: Did\n nsid: NSID\n} {\n // Validate input URI\n const nsid = NSID.from(uri.rkey)\n try {\n const did = uri.host\n assertDid(did)\n return { did, nsid }\n } catch (cause) {\n throw new LexResolverError(nsid, `URI host is not a DID ${uri}`, { cause })\n }\n}\n\nasync function getDomainTxtDid(domain: string): Promise<Did> {\n const didLines = (await resolveTxt(domain))\n .map((chunks) => chunks.join(''))\n .filter((i) => i.startsWith('did='))\n\n if (didLines.length === 1) {\n const did = didLines[0].slice(4)\n assertDid(did)\n return did\n }\n\n throw didLines.length > 1\n ? new Error('Multiple DIDs found in DNS TXT records')\n : new Error('No DID found in DNS TXT records')\n}\n\nasync function verifyRecordProof(\n car: Uint8Array,\n did: Did,\n key: AtprotoVerificationMethod,\n collection: NsidString,\n rkey: string,\n) {\n const { root, blocks } = await readCarWithRoot(car)\n const blockstore = new MemoryBlockstore(blocks)\n\n const commit = await blockstore.readObj(root, repoDef.commit)\n if (commit.did !== did) {\n throw new Error(`Invalid repo did: ${commit.did}`)\n }\n\n const signingKey = getDidKeyFromMultibase(key)\n const validSig = await verifyCommitSig(commit, signingKey)\n if (!validSig) {\n throw new Error(`Invalid signature on commit: ${root.toString()}`)\n }\n\n const mst = MST.load(blockstore, commit.data)\n\n const cid = await mst.get(`${collection}/${rkey}`)\n if (!cid) throw new Error('Record not found in proof')\n\n const record = await blockstore.readRecord(cid)\n if (record?.$type !== collection) {\n throw new Error(\n `Invalid record type: expected ${collection}, got ${record?.$type}`,\n )\n }\n\n return { cid, record }\n}\n\nfunction getDidKeyFromMultibase(key: AtprotoVerificationMethod) {\n switch (key.type) {\n case 'EcdsaSecp256r1VerificationKey2019': {\n const keyBytes = crypto.multibaseToBytes(key.publicKeyMultibase)\n return crypto.formatDidKey(crypto.P256_JWT_ALG, keyBytes)\n }\n case 'EcdsaSecp256k1VerificationKey2019': {\n const keyBytes = crypto.multibaseToBytes(key.publicKeyMultibase)\n return crypto.formatDidKey(crypto.SECP256K1_JWT_ALG, keyBytes)\n }\n case 'Multikey': {\n const { jwtAlg, keyBytes } = crypto.parseMultikey(key.publicKeyMultibase)\n return crypto.formatDidKey(jwtAlg, keyBytes)\n }\n default: {\n // Should never happen\n throw new Error(`Unsupported verification method type: ${key.type}`)\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-resolver",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon document resolver utility for AT Lexicons",
|
|
6
6
|
"keywords": [
|
|
@@ -30,24 +30,25 @@
|
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
32
|
"types": "./dist/index.d.ts",
|
|
33
|
+
"browser": "./dist/index.js",
|
|
33
34
|
"import": "./dist/index.js",
|
|
34
|
-
"
|
|
35
|
+
"default": "./dist/index.js"
|
|
35
36
|
}
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
39
|
"tslib": "^2.8.1",
|
|
39
40
|
"@atproto-labs/did-resolver": "^0.2.6",
|
|
40
41
|
"@atproto/crypto": "^0.4.5",
|
|
41
|
-
"@atproto/lex-client": "^0.0.
|
|
42
|
-
"@atproto/lex-data": "^0.0.
|
|
43
|
-
"@atproto/lex-document": "^0.0.
|
|
44
|
-
"@atproto/lex-schema": "^0.0.
|
|
42
|
+
"@atproto/lex-client": "^0.0.12",
|
|
43
|
+
"@atproto/lex-data": "^0.0.11",
|
|
44
|
+
"@atproto/lex-document": "^0.0.13",
|
|
45
|
+
"@atproto/lex-schema": "^0.0.12",
|
|
45
46
|
"@atproto/repo": "^0.8.12",
|
|
46
47
|
"@atproto/syntax": "^0.4.3"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
49
50
|
"vitest": "^4.0.16",
|
|
50
|
-
"@atproto/lex-builder": "^0.0.
|
|
51
|
+
"@atproto/lex-builder": "^0.0.14"
|
|
51
52
|
},
|
|
52
53
|
"scripts": {
|
|
53
54
|
"prebuild": "node ./scripts/lex-build.mjs",
|
|
@@ -1,16 +1,105 @@
|
|
|
1
|
+
import { LexError } from '@atproto/lex-data'
|
|
1
2
|
import { NSID } from '@atproto/syntax'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Error class for lexicon resolution failures.
|
|
6
|
+
*
|
|
7
|
+
* This error is thrown when the {@link LexResolver} encounters issues during
|
|
8
|
+
* the resolution process, such as DNS lookup failures, DID resolution errors,
|
|
9
|
+
* invalid lexicon documents, or network failures.
|
|
10
|
+
*
|
|
11
|
+
* @example Catching resolution errors
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { LexResolver, LexResolverError } from '@atproto/lex-resolver'
|
|
14
|
+
*
|
|
15
|
+
* const resolver = new LexResolver({})
|
|
16
|
+
*
|
|
17
|
+
* try {
|
|
18
|
+
* const result = await resolver.get('com.example.myLexicon')
|
|
19
|
+
* } catch (error) {
|
|
20
|
+
* if (error instanceof LexResolverError) {
|
|
21
|
+
* console.error(`Failed to resolve ${error.nsid}: ${error.description}`)
|
|
22
|
+
* // Access the original cause if available
|
|
23
|
+
* if (error.cause) {
|
|
24
|
+
* console.error('Caused by:', error.cause)
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example Creating errors with the factory method
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
33
|
+
*
|
|
34
|
+
* // Create from string NSID
|
|
35
|
+
* const error = LexResolverError.from(
|
|
36
|
+
* 'com.example.myLexicon',
|
|
37
|
+
* 'Custom error description'
|
|
38
|
+
* )
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class LexResolverError extends LexError {
|
|
4
42
|
name = 'LexResolverError'
|
|
5
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new LexResolverError instance.
|
|
46
|
+
*
|
|
47
|
+
* @param nsid - The NSID that failed to resolve
|
|
48
|
+
* @param description - Human-readable description of the error. Defaults to
|
|
49
|
+
* a generic message if not provided.
|
|
50
|
+
* @param options - Standard error options including `cause` for error chaining
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { NSID } from '@atproto/syntax'
|
|
55
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
56
|
+
*
|
|
57
|
+
* const nsid = NSID.from('com.example.myLexicon')
|
|
58
|
+
* const error = new LexResolverError(
|
|
59
|
+
* nsid,
|
|
60
|
+
* 'DNS lookup failed',
|
|
61
|
+
* { cause: originalError }
|
|
62
|
+
* )
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
6
65
|
constructor(
|
|
66
|
+
/**
|
|
67
|
+
* The NSID that failed to resolve.
|
|
68
|
+
*/
|
|
7
69
|
public readonly nsid: NSID,
|
|
70
|
+
/**
|
|
71
|
+
* Human-readable description of what went wrong during resolution.
|
|
72
|
+
*/
|
|
8
73
|
public readonly description = `Could not resolve Lexicon for NSID`,
|
|
9
74
|
options?: ErrorOptions,
|
|
10
75
|
) {
|
|
11
|
-
super(`${description} (${nsid})`, options)
|
|
76
|
+
super('LexiconResolutionFailure', `${description} (${nsid})`, options)
|
|
12
77
|
}
|
|
13
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Factory method to create a LexResolverError from a string or NSID.
|
|
81
|
+
*
|
|
82
|
+
* This is a convenience method that handles the conversion of string NSIDs
|
|
83
|
+
* to NSID objects automatically.
|
|
84
|
+
*
|
|
85
|
+
* @param nsid - The NSID as a string or NSID object
|
|
86
|
+
* @param description - Optional human-readable description of the error
|
|
87
|
+
* @returns A new LexResolverError instance
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* import { LexResolverError } from '@atproto/lex-resolver'
|
|
92
|
+
*
|
|
93
|
+
* // Create from string
|
|
94
|
+
* const error1 = LexResolverError.from('com.example.myLexicon')
|
|
95
|
+
*
|
|
96
|
+
* // Create with description
|
|
97
|
+
* const error2 = LexResolverError.from(
|
|
98
|
+
* 'com.example.myLexicon',
|
|
99
|
+
* 'Authority not found in DNS'
|
|
100
|
+
* )
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
14
103
|
static from(nsid: NSID | string, description?: string) {
|
|
15
104
|
return new LexResolverError(
|
|
16
105
|
typeof nsid === 'string' ? NSID.from(nsid) : nsid,
|