@atcute/identity-resolver 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.
- package/LICENSE +17 -0
- package/README.md +66 -0
- package/dist/did/composite.d.ts +12 -0
- package/dist/did/composite.js +17 -0
- package/dist/did/composite.js.map +1 -0
- package/dist/did/methods/plc.d.ts +12 -0
- package/dist/did/methods/plc.js +34 -0
- package/dist/did/methods/plc.js.map +1 -0
- package/dist/did/methods/web.d.ts +15 -0
- package/dist/did/methods/web.js +63 -0
- package/dist/did/methods/web.js.map +1 -0
- package/dist/did/utils.d.ts +1 -0
- package/dist/did/utils.js +4 -0
- package/dist/did/utils.js.map +1 -0
- package/dist/errors.d.ts +47 -0
- package/dist/errors.js +75 -0
- package/dist/errors.js.map +1 -0
- package/dist/handle/composite.d.ts +15 -0
- package/dist/handle/composite.js +62 -0
- package/dist/handle/composite.js.map +1 -0
- package/dist/handle/methods/doh-json.d.ts +12 -0
- package/dist/handle/methods/doh-json.js +105 -0
- package/dist/handle/methods/doh-json.js.map +1 -0
- package/dist/handle/methods/well-known.d.ts +10 -0
- package/dist/handle/methods/well-known.js +35 -0
- package/dist/handle/methods/well-known.js.map +1 -0
- package/dist/handle/methods/xrpc.d.ts +12 -0
- package/dist/handle/methods/xrpc.js +38 -0
- package/dist/handle/methods/xrpc.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +15 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/lib/did/composite.ts +27 -0
- package/lib/did/methods/plc.ts +51 -0
- package/lib/did/methods/web.ts +85 -0
- package/lib/did/utils.ts +8 -0
- package/lib/errors.ts +86 -0
- package/lib/handle/composite.ts +94 -0
- package/lib/handle/methods/doh-json.ts +138 -0
- package/lib/handle/methods/well-known.ts +50 -0
- package/lib/handle/methods/xrpc.ts +65 -0
- package/lib/index.ts +11 -0
- package/lib/types.ts +19 -0
- package/package.json +41 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isAtprotoDid, type AtprotoDid, type Handle } from '@atcute/identity';
|
|
2
|
+
import { FailedResponseError, isResponseOk, pipe, readResponseAsText } from '@atcute/util-fetch';
|
|
3
|
+
|
|
4
|
+
import * as err from '../../errors.js';
|
|
5
|
+
import type { HandleResolver, ResolveHandleOptions } from '../../types.js';
|
|
6
|
+
|
|
7
|
+
export interface WellKnownHandleResolverOptions {
|
|
8
|
+
fetch?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const fetchWellKnownHandler = pipe(isResponseOk, readResponseAsText(2048 + 16));
|
|
12
|
+
|
|
13
|
+
export class WellKnownHandleResolver implements HandleResolver {
|
|
14
|
+
#fetch: typeof fetch;
|
|
15
|
+
|
|
16
|
+
constructor({ fetch: fetchThis = fetch }: WellKnownHandleResolverOptions = {}) {
|
|
17
|
+
this.#fetch = fetchThis;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid> {
|
|
21
|
+
let text: string;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const url = new URL('/.well-known/atproto-did', `https://${handle}`);
|
|
25
|
+
|
|
26
|
+
const response = await (0, this.#fetch)(url, {
|
|
27
|
+
signal: options?.signal,
|
|
28
|
+
cache: options?.noCache ? 'no-cache' : 'default',
|
|
29
|
+
redirect: 'error',
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const handled = await fetchWellKnownHandler(response);
|
|
33
|
+
|
|
34
|
+
text = handled.text;
|
|
35
|
+
} catch (cause) {
|
|
36
|
+
if (cause instanceof FailedResponseError && cause.status === 404) {
|
|
37
|
+
throw new err.DidNotFoundError(handle);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
throw new err.FailedHandleResolutionError(handle, { cause });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const did = text.split('\n')[0]!.trim();
|
|
44
|
+
if (!isAtprotoDid(did)) {
|
|
45
|
+
throw new err.InvalidResolvedHandleError(handle, did);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return did;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as v from '@badrap/valita';
|
|
2
|
+
|
|
3
|
+
import { isAtprotoDid, type AtprotoDid, type Handle } from '@atcute/identity';
|
|
4
|
+
import {
|
|
5
|
+
FailedResponseError,
|
|
6
|
+
isResponseOk,
|
|
7
|
+
parseResponseAsJson,
|
|
8
|
+
pipe,
|
|
9
|
+
validateJsonWith,
|
|
10
|
+
} from '@atcute/util-fetch';
|
|
11
|
+
|
|
12
|
+
import * as err from '../../errors.js';
|
|
13
|
+
import type { HandleResolver, ResolveHandleOptions } from '../../types.js';
|
|
14
|
+
|
|
15
|
+
const response = v.object({
|
|
16
|
+
did: v.string().assert((input) => isAtprotoDid(input)),
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const fetchXrpcHandler = pipe(
|
|
20
|
+
isResponseOk,
|
|
21
|
+
parseResponseAsJson(/^application\/json$/, 4 * 1024),
|
|
22
|
+
validateJsonWith(response, { mode: 'passthrough' }),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export interface XrpcHandleResolverOptions {
|
|
26
|
+
serviceUrl: string;
|
|
27
|
+
fetch?: typeof fetch;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class XrpcHandleResolver implements HandleResolver {
|
|
31
|
+
readonly serviceUrl: string;
|
|
32
|
+
#fetch: typeof fetch;
|
|
33
|
+
|
|
34
|
+
constructor({ serviceUrl, fetch: fetchThis = fetch }: XrpcHandleResolverOptions) {
|
|
35
|
+
this.serviceUrl = serviceUrl;
|
|
36
|
+
this.#fetch = fetchThis;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid> {
|
|
40
|
+
let json: v.Infer<typeof response>;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(`/xrpc/com.atproto.identity.resolveHandle`, this.serviceUrl);
|
|
44
|
+
url.searchParams.set('handle', handle);
|
|
45
|
+
|
|
46
|
+
const response = await this.#fetch(url, {
|
|
47
|
+
signal: options?.signal,
|
|
48
|
+
cache: options?.noCache ? 'no-cache' : 'default',
|
|
49
|
+
headers: { accept: 'application/json' },
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const handled = await fetchXrpcHandler(response);
|
|
53
|
+
|
|
54
|
+
json = handled.json;
|
|
55
|
+
} catch (cause) {
|
|
56
|
+
if (cause instanceof FailedResponseError && cause.status === 400) {
|
|
57
|
+
throw new err.DidNotFoundError(handle);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
throw new err.FailedHandleResolutionError(handle, { cause });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return json.did;
|
|
64
|
+
}
|
|
65
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './did/composite.js';
|
|
2
|
+
export * from './did/methods/plc.js';
|
|
3
|
+
export * from './did/methods/web.js';
|
|
4
|
+
|
|
5
|
+
export * from './handle/composite.js';
|
|
6
|
+
export * from './handle/methods/doh-json.js';
|
|
7
|
+
export * from './handle/methods/well-known.js';
|
|
8
|
+
export * from './handle/methods/xrpc.js';
|
|
9
|
+
|
|
10
|
+
export * from './errors.js';
|
|
11
|
+
export * from './types.js';
|
package/lib/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AtprotoDid, Did, DidDocument, Handle } from '@atcute/identity';
|
|
2
|
+
|
|
3
|
+
export interface ResolveDidDocumentOptions {
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
noCache?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface DidDocumentResolver<TMethod extends string> {
|
|
9
|
+
resolve(did: Did<TMethod>, options?: ResolveDidDocumentOptions): Promise<DidDocument>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ResolveHandleOptions {
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
noCache?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface HandleResolver {
|
|
18
|
+
resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid>;
|
|
19
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@atcute/identity-resolver",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "atproto handle and DID document resolution",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"atproto",
|
|
8
|
+
"did"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"url": "https://github.com/mary-ext/atcute",
|
|
13
|
+
"directory": "packages/identity/identity-resolver"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"lib/",
|
|
18
|
+
"!lib/**/*.bench.ts",
|
|
19
|
+
"!lib/**/*.test.ts"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@atcute/identity": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@badrap/valita": "^0.4.2",
|
|
30
|
+
"@atcute/util-fetch": "^1.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/bun": "^1.2.1",
|
|
34
|
+
"@atcute/identity": "^0.1.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc --project tsconfig.build.json",
|
|
38
|
+
"test": "bun test --coverage",
|
|
39
|
+
"prepublish": "rm -rf dist; pnpm run build"
|
|
40
|
+
}
|
|
41
|
+
}
|