@atcute/identity-resolver 1.2.0 → 1.2.2
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/README.md
CHANGED
|
@@ -1,66 +1,239 @@
|
|
|
1
1
|
# @atcute/identity-resolver
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
handle and DID document resolution for AT Protocol.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @atcute/identity-resolver
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
in AT Protocol, handles (like `alice.bsky.social`) need to be resolved to DIDs, and DIDs need to be
|
|
10
|
+
resolved to DID documents (which contain the user's PDS location and keys). this package provides
|
|
11
|
+
resolvers for both.
|
|
12
|
+
|
|
13
|
+
## usage
|
|
14
|
+
|
|
15
|
+
### resolving handles
|
|
16
|
+
|
|
17
|
+
handles can be resolved via DNS TXT records or HTTP well-known endpoints. use the composite resolver
|
|
18
|
+
to try both:
|
|
4
19
|
|
|
5
20
|
```ts
|
|
6
|
-
|
|
21
|
+
import {
|
|
22
|
+
CompositeHandleResolver,
|
|
23
|
+
DohJsonHandleResolver,
|
|
24
|
+
WellKnownHandleResolver,
|
|
25
|
+
} from '@atcute/identity-resolver';
|
|
26
|
+
|
|
7
27
|
const handleResolver = new CompositeHandleResolver({
|
|
8
|
-
strategy: 'race',
|
|
9
28
|
methods: {
|
|
10
29
|
dns: new DohJsonHandleResolver({ dohUrl: 'https://mozilla.cloudflare-dns.com/dns-query' }),
|
|
11
30
|
http: new WellKnownHandleResolver(),
|
|
12
31
|
},
|
|
13
32
|
});
|
|
14
33
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
} catch (err) {
|
|
19
|
-
if (err instanceof DidNotFoundError) {
|
|
20
|
-
// handle returned no did
|
|
21
|
-
}
|
|
22
|
-
if (err instanceof InvalidResolvedHandleError) {
|
|
23
|
-
// handle returned a did, but isn't a valid atproto did
|
|
24
|
-
}
|
|
25
|
-
if (err instanceof AmbiguousHandleError) {
|
|
26
|
-
// handle returned multiple did values
|
|
27
|
-
}
|
|
28
|
-
if (err instanceof FailedHandleResolutionError) {
|
|
29
|
-
// handle resolution had thrown something unexpected (fetch error)
|
|
30
|
-
}
|
|
34
|
+
const did = await handleResolver.resolve('bsky.app');
|
|
35
|
+
// -> "did:plc:z72i7hdynmk6r22z27h6tvur"
|
|
36
|
+
```
|
|
31
37
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
### resolution strategies
|
|
39
|
+
|
|
40
|
+
the composite resolver supports different strategies for combining DNS and HTTP resolution:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const handleResolver = new CompositeHandleResolver({
|
|
44
|
+
strategy: 'race', // default - first successful response wins
|
|
45
|
+
methods: { dns: dnsResolver, http: httpResolver },
|
|
46
|
+
});
|
|
47
|
+
```
|
|
36
48
|
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
available strategies:
|
|
50
|
+
|
|
51
|
+
- `race` - returns whichever method succeeds first (default)
|
|
52
|
+
- `dns-first` - try DNS first, fall back to HTTP if it fails
|
|
53
|
+
- `http-first` - try HTTP first, fall back to DNS if it fails
|
|
54
|
+
- `both` - require both methods to agree (throws `AmbiguousHandleError` if they differ)
|
|
55
|
+
|
|
56
|
+
### resolving DID documents
|
|
57
|
+
|
|
58
|
+
DID documents can be resolved for did:plc and did:web methods:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import {
|
|
62
|
+
CompositeDidDocumentResolver,
|
|
63
|
+
PlcDidDocumentResolver,
|
|
64
|
+
WebDidDocumentResolver,
|
|
65
|
+
} from '@atcute/identity-resolver';
|
|
66
|
+
|
|
67
|
+
const didResolver = new CompositeDidDocumentResolver({
|
|
39
68
|
methods: {
|
|
40
69
|
plc: new PlcDidDocumentResolver(),
|
|
41
70
|
web: new WebDidDocumentResolver(),
|
|
42
71
|
},
|
|
43
72
|
});
|
|
44
73
|
|
|
74
|
+
const doc = await didResolver.resolve('did:plc:z72i7hdynmk6r22z27h6tvur');
|
|
75
|
+
// -> { '@context': [...], id: 'did:plc:...', service: [...], ... }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### resolving actors
|
|
79
|
+
|
|
80
|
+
the `ActorResolver` interface provides a way to resolve an actor identifier (handle or DID) to the
|
|
81
|
+
essential info needed to interact with them: their DID, verified handle, and PDS endpoint.
|
|
82
|
+
|
|
83
|
+
`LocalActorResolver` implements this by combining handle and DID document resolution locally:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { LocalActorResolver } from '@atcute/identity-resolver';
|
|
87
|
+
|
|
88
|
+
const actorResolver = new LocalActorResolver({
|
|
89
|
+
handleResolver,
|
|
90
|
+
didDocumentResolver: didResolver,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// resolve from handle
|
|
94
|
+
const actor = await actorResolver.resolve('bsky.app');
|
|
95
|
+
// -> { did: "did:plc:...", handle: "bsky.app", pds: "https://..." }
|
|
96
|
+
|
|
97
|
+
// resolve from DID
|
|
98
|
+
const actor2 = await actorResolver.resolve('did:plc:z72i7hdynmk6r22z27h6tvur');
|
|
99
|
+
// -> { did: "did:plc:...", handle: "bsky.app", pds: "https://..." }
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
the local resolver performs bidirectional verification: it checks that the handle in the DID
|
|
103
|
+
document resolves back to the same DID.
|
|
104
|
+
|
|
105
|
+
other implementations of `ActorResolver` can get this info from dedicated identity services (like
|
|
106
|
+
Slingshot) without needing to fetch and parse full DID documents.
|
|
107
|
+
|
|
108
|
+
### handling errors
|
|
109
|
+
|
|
110
|
+
each resolver throws specific error types for different failure cases:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import {
|
|
114
|
+
DidNotFoundError,
|
|
115
|
+
InvalidResolvedHandleError,
|
|
116
|
+
AmbiguousHandleError,
|
|
117
|
+
FailedHandleResolutionError,
|
|
118
|
+
HandleResolutionError,
|
|
119
|
+
} from '@atcute/identity-resolver';
|
|
120
|
+
|
|
45
121
|
try {
|
|
46
|
-
const
|
|
47
|
-
// ^? { '@context': [...], id: 'did:plc:z72i7hdynmk6r22z27h6tvur', ... }
|
|
122
|
+
const did = await handleResolver.resolve('nonexistent.invalid');
|
|
48
123
|
} catch (err) {
|
|
49
|
-
if (err instanceof
|
|
50
|
-
//
|
|
51
|
-
|
|
52
|
-
if (err instanceof
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
if (err instanceof
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
if (err instanceof
|
|
59
|
-
//
|
|
124
|
+
if (err instanceof DidNotFoundError) {
|
|
125
|
+
// handle has no DID record
|
|
126
|
+
console.log('handle not found');
|
|
127
|
+
} else if (err instanceof InvalidResolvedHandleError) {
|
|
128
|
+
// handle returned an invalid DID format
|
|
129
|
+
console.log('invalid DID:', err.did);
|
|
130
|
+
} else if (err instanceof AmbiguousHandleError) {
|
|
131
|
+
// multiple different DIDs found (with 'both' strategy)
|
|
132
|
+
console.log('ambiguous handle');
|
|
133
|
+
} else if (err instanceof FailedHandleResolutionError) {
|
|
134
|
+
// network or other unexpected error
|
|
135
|
+
console.log('resolution failed:', err.cause);
|
|
136
|
+
} else if (err instanceof HandleResolutionError) {
|
|
137
|
+
// catch-all for any handle resolution error
|
|
60
138
|
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
DID document resolution errors:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import {
|
|
146
|
+
DocumentNotFoundError,
|
|
147
|
+
UnsupportedDidMethodError,
|
|
148
|
+
ImproperDidError,
|
|
149
|
+
FailedDocumentResolutionError,
|
|
150
|
+
DidDocumentResolutionError,
|
|
151
|
+
} from '@atcute/identity-resolver';
|
|
61
152
|
|
|
62
|
-
|
|
63
|
-
|
|
153
|
+
try {
|
|
154
|
+
const doc = await didResolver.resolve('did:example:123');
|
|
155
|
+
} catch (err) {
|
|
156
|
+
if (err instanceof DocumentNotFoundError) {
|
|
157
|
+
// DID document doesn't exist
|
|
158
|
+
} else if (err instanceof UnsupportedDidMethodError) {
|
|
159
|
+
// resolver doesn't support this DID method
|
|
160
|
+
} else if (err instanceof ImproperDidError) {
|
|
161
|
+
// DID format is invalid for this method
|
|
162
|
+
} else if (err instanceof FailedDocumentResolutionError) {
|
|
163
|
+
// network or other unexpected error
|
|
164
|
+
} else if (err instanceof DidDocumentResolutionError) {
|
|
165
|
+
// catch-all for any DID resolution error
|
|
64
166
|
}
|
|
65
167
|
}
|
|
66
168
|
```
|
|
169
|
+
|
|
170
|
+
### caching and abort signals
|
|
171
|
+
|
|
172
|
+
all resolvers accept options for cache control and cancellation:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// skip cache
|
|
176
|
+
const did = await handleResolver.resolve('bsky.app', { noCache: true });
|
|
177
|
+
|
|
178
|
+
// with abort signal
|
|
179
|
+
const controller = new AbortController();
|
|
180
|
+
const did = await handleResolver.resolve('bsky.app', { signal: controller.signal });
|
|
181
|
+
|
|
182
|
+
// cancel the request
|
|
183
|
+
controller.abort();
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### custom fetch function
|
|
187
|
+
|
|
188
|
+
all resolvers accept a custom fetch implementation:
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
const dnsResolver = new DohJsonHandleResolver({
|
|
192
|
+
dohUrl: 'https://mozilla.cloudflare-dns.com/dns-query',
|
|
193
|
+
fetch: customFetch,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const httpResolver = new WellKnownHandleResolver({
|
|
197
|
+
fetch: customFetch,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
const plcResolver = new PlcDidDocumentResolver({
|
|
201
|
+
fetch: customFetch,
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### custom PLC directory
|
|
206
|
+
|
|
207
|
+
by default, did:plc resolution uses `https://plc.directory`. you can specify a different directory:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
const plcResolver = new PlcDidDocumentResolver({
|
|
211
|
+
apiUrl: 'https://plc.wtf', // mirror of plc.directory
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## resolver classes
|
|
216
|
+
|
|
217
|
+
### handle resolvers
|
|
218
|
+
|
|
219
|
+
| class | description |
|
|
220
|
+
| ------------------------- | --------------------------------------------------------------- |
|
|
221
|
+
| `DohJsonHandleResolver` | resolves via DNS-over-HTTPS (TXT record at `_atproto.{handle}`) |
|
|
222
|
+
| `WellKnownHandleResolver` | resolves via HTTP (`https://{handle}/.well-known/atproto-did`) |
|
|
223
|
+
| `XrpcHandleResolver` | resolves via XRPC (`com.atproto.identity.resolveHandle`) |
|
|
224
|
+
| `CompositeHandleResolver` | combines DNS and HTTP resolvers |
|
|
225
|
+
|
|
226
|
+
### DID document resolvers
|
|
227
|
+
|
|
228
|
+
| class | description |
|
|
229
|
+
| ------------------------------ | ----------------------------------------------------- |
|
|
230
|
+
| `PlcDidDocumentResolver` | resolves did:plc from PLC directory |
|
|
231
|
+
| `WebDidDocumentResolver` | resolves did:web from domain |
|
|
232
|
+
| `XrpcDidDocumentResolver` | resolves via XRPC (`com.atproto.identity.resolveDid`) |
|
|
233
|
+
| `CompositeDidDocumentResolver` | routes to resolver by DID method |
|
|
234
|
+
|
|
235
|
+
### actor resolvers
|
|
236
|
+
|
|
237
|
+
| class | description |
|
|
238
|
+
| -------------------- | ------------------------------------------------------------------ |
|
|
239
|
+
| `LocalActorResolver` | combines handle and DID resolution with bidirectional verification |
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doh-json.d.ts","sourceRoot":"","sources":["../../../lib/handle/methods/doh-json.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"doh-json.d.ts","sourceRoot":"","sources":["../../../lib/handle/methods/doh-json.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAIlE,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAK3E,MAAM,WAAW,4BAA4B;IAC5C,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACrB;AAED,qBAAa,qBAAsB,YAAW,cAAc;;IAC3D,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAGxB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAiB,EAAE,EAAE,4BAA4B,EAG7E;IAEK,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,CA2DjF;CACD"}
|
|
@@ -1,51 +1,8 @@
|
|
|
1
|
-
import * as v from '@badrap/valita';
|
|
2
1
|
import { isAtprotoDid } from '@atcute/identity';
|
|
3
|
-
import {
|
|
2
|
+
import { fetchDohJsonTxt } from '@atcute/util-fetch';
|
|
4
3
|
import * as err from '../../errors.js';
|
|
5
|
-
const uint32 = v.number().assert((input) => Number.isInteger(input) && input >= 0 && input <= 2 ** 32 - 1);
|
|
6
|
-
const question = v.object({
|
|
7
|
-
name: v.string(),
|
|
8
|
-
type: v.literal(16), // TXT
|
|
9
|
-
});
|
|
10
|
-
const answer = v.object({
|
|
11
|
-
name: v.string(),
|
|
12
|
-
type: v.literal(16), // TXT
|
|
13
|
-
TTL: uint32,
|
|
14
|
-
data: v.string().chain((input) => {
|
|
15
|
-
return v.ok(input.replace(/^"|"$/g, '').replace(/\\"/g, '"'));
|
|
16
|
-
}),
|
|
17
|
-
});
|
|
18
|
-
const authority = v.object({
|
|
19
|
-
name: v.string(),
|
|
20
|
-
type: uint32,
|
|
21
|
-
TTL: uint32,
|
|
22
|
-
data: v.string(),
|
|
23
|
-
});
|
|
24
|
-
const result = v.object({
|
|
25
|
-
/** DNS response code */
|
|
26
|
-
Status: uint32,
|
|
27
|
-
/** Whether response is truncated */
|
|
28
|
-
TC: v.boolean(),
|
|
29
|
-
/** Whether recursive desired bit is set, always true for Google and Cloudflare DoH */
|
|
30
|
-
RD: v.boolean(),
|
|
31
|
-
/** Whether recursive available bit is set, always true for Google and Cloudflare DoH */
|
|
32
|
-
RA: v.boolean(),
|
|
33
|
-
/** Whether response data was validated with DNSSEC */
|
|
34
|
-
AD: v.boolean(),
|
|
35
|
-
/** Whether client asked to disable DNSSEC validation */
|
|
36
|
-
CD: v.boolean(),
|
|
37
|
-
/** Requested records */
|
|
38
|
-
Question: v.tuple([question]),
|
|
39
|
-
/** Answers */
|
|
40
|
-
Answer: v.array(answer).optional(() => []),
|
|
41
|
-
/** Authority */
|
|
42
|
-
Authority: v.array(authority).optional(),
|
|
43
|
-
/** Comment from the DNS server */
|
|
44
|
-
Comment: v.string().optional(),
|
|
45
|
-
});
|
|
46
4
|
const SUBDOMAIN = '_atproto';
|
|
47
5
|
const PREFIX = 'did=';
|
|
48
|
-
const fetchDohJsonHandler = pipe(isResponseOk, parseResponseAsJson(/^application\/(dns-)?json$/, 16 * 1024), validateJsonWith(result, { mode: 'passthrough' }));
|
|
49
6
|
export class DohJsonHandleResolver {
|
|
50
7
|
dohUrl;
|
|
51
8
|
#fetch;
|
|
@@ -64,7 +21,7 @@ export class DohJsonHandleResolver {
|
|
|
64
21
|
cache: options?.noCache ? 'no-cache' : undefined,
|
|
65
22
|
headers: { accept: 'application/dns-json' },
|
|
66
23
|
});
|
|
67
|
-
const handled = await
|
|
24
|
+
const handled = await fetchDohJsonTxt(response);
|
|
68
25
|
json = handled.json;
|
|
69
26
|
}
|
|
70
27
|
catch (cause) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doh-json.js","sourceRoot":"","sources":["../../../lib/handle/methods/doh-json.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"doh-json.js","sourceRoot":"","sources":["../../../lib/handle/methods/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C,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACtC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,MAAM,IAAI,GAAG,CAAC,0BAA0B,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YACvD,CAAC;YAED,OAAO,GAAG,CAAC;QACZ,CAAC;QAED,sEAAsE;QACtE,MAAM,IAAI,GAAG,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAAA,CACvC;CACD"}
|
|
@@ -1,67 +1,13 @@
|
|
|
1
|
-
import * as v from '@badrap/valita';
|
|
2
|
-
|
|
3
1
|
import { isAtprotoDid } from '@atcute/identity';
|
|
4
2
|
import type { AtprotoDid, Handle } from '@atcute/lexicons/syntax';
|
|
5
|
-
import {
|
|
3
|
+
import { type DohJsonTxtResult, fetchDohJsonTxt } from '@atcute/util-fetch';
|
|
6
4
|
|
|
7
5
|
import * as err from '../../errors.js';
|
|
8
6
|
import type { HandleResolver, ResolveHandleOptions } from '../../types.js';
|
|
9
7
|
|
|
10
|
-
const uint32 = v.number().assert((input) => Number.isInteger(input) && input >= 0 && input <= 2 ** 32 - 1);
|
|
11
|
-
|
|
12
|
-
const question = v.object({
|
|
13
|
-
name: v.string(),
|
|
14
|
-
type: v.literal(16), // TXT
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const answer = v.object({
|
|
18
|
-
name: v.string(),
|
|
19
|
-
type: v.literal(16), // TXT
|
|
20
|
-
TTL: uint32,
|
|
21
|
-
data: v.string().chain((input) => {
|
|
22
|
-
return v.ok(input.replace(/^"|"$/g, '').replace(/\\"/g, '"'));
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const authority = v.object({
|
|
27
|
-
name: v.string(),
|
|
28
|
-
type: uint32,
|
|
29
|
-
TTL: uint32,
|
|
30
|
-
data: v.string(),
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const result = v.object({
|
|
34
|
-
/** DNS response code */
|
|
35
|
-
Status: uint32,
|
|
36
|
-
/** Whether response is truncated */
|
|
37
|
-
TC: v.boolean(),
|
|
38
|
-
/** Whether recursive desired bit is set, always true for Google and Cloudflare DoH */
|
|
39
|
-
RD: v.boolean(),
|
|
40
|
-
/** Whether recursive available bit is set, always true for Google and Cloudflare DoH */
|
|
41
|
-
RA: v.boolean(),
|
|
42
|
-
/** Whether response data was validated with DNSSEC */
|
|
43
|
-
AD: v.boolean(),
|
|
44
|
-
/** Whether client asked to disable DNSSEC validation */
|
|
45
|
-
CD: v.boolean(),
|
|
46
|
-
/** Requested records */
|
|
47
|
-
Question: v.tuple([question]),
|
|
48
|
-
/** Answers */
|
|
49
|
-
Answer: v.array(answer).optional(() => []),
|
|
50
|
-
/** Authority */
|
|
51
|
-
Authority: v.array(authority).optional(),
|
|
52
|
-
/** Comment from the DNS server */
|
|
53
|
-
Comment: v.string().optional(),
|
|
54
|
-
});
|
|
55
|
-
|
|
56
8
|
const SUBDOMAIN = '_atproto';
|
|
57
9
|
const PREFIX = 'did=';
|
|
58
10
|
|
|
59
|
-
const fetchDohJsonHandler = pipe(
|
|
60
|
-
isResponseOk,
|
|
61
|
-
parseResponseAsJson(/^application\/(dns-)?json$/, 16 * 1024),
|
|
62
|
-
validateJsonWith(result, { mode: 'passthrough' }),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
11
|
export interface DohJsonHandleResolverOptions {
|
|
66
12
|
dohUrl: string;
|
|
67
13
|
fetch?: typeof fetch;
|
|
@@ -77,7 +23,7 @@ export class DohJsonHandleResolver implements HandleResolver {
|
|
|
77
23
|
}
|
|
78
24
|
|
|
79
25
|
async resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid> {
|
|
80
|
-
let json:
|
|
26
|
+
let json: DohJsonTxtResult;
|
|
81
27
|
|
|
82
28
|
try {
|
|
83
29
|
const url = new URL(this.dohUrl);
|
|
@@ -90,7 +36,7 @@ export class DohJsonHandleResolver implements HandleResolver {
|
|
|
90
36
|
headers: { accept: 'application/dns-json' },
|
|
91
37
|
});
|
|
92
38
|
|
|
93
|
-
const handled = await
|
|
39
|
+
const handled = await fetchDohJsonTxt(response);
|
|
94
40
|
|
|
95
41
|
json = handled.json;
|
|
96
42
|
} catch (cause) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@atcute/identity-resolver",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.2",
|
|
5
5
|
"description": "atproto handle and DID document resolution",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"atproto",
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"url": "https://github.com/mary-ext/atcute",
|
|
13
13
|
"directory": "packages/identity/identity-resolver"
|
|
14
14
|
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
15
18
|
"files": [
|
|
16
19
|
"dist/",
|
|
17
20
|
"lib/",
|
|
@@ -27,12 +30,12 @@
|
|
|
27
30
|
},
|
|
28
31
|
"dependencies": {
|
|
29
32
|
"@badrap/valita": "^0.4.6",
|
|
30
|
-
"@atcute/lexicons": "^1.2.
|
|
31
|
-
"@atcute/util-fetch": "^1.0.
|
|
33
|
+
"@atcute/lexicons": "^1.2.6",
|
|
34
|
+
"@atcute/util-fetch": "^1.0.5"
|
|
32
35
|
},
|
|
33
36
|
"devDependencies": {
|
|
34
|
-
"@vitest/coverage-v8": "^4.0.
|
|
35
|
-
"vitest": "^4.0.
|
|
37
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
38
|
+
"vitest": "^4.0.16",
|
|
36
39
|
"@atcute/identity": "^1.1.3"
|
|
37
40
|
},
|
|
38
41
|
"scripts": {
|