@atcute/identity-resolver-node 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 +36 -0
- package/dist/did/methods/node.d.ts +11 -0
- package/dist/did/methods/node.js +53 -0
- package/dist/did/methods/node.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/lib/did/methods/node.ts +77 -0
- package/lib/index.ts +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
2
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
3
|
+
in the Software without restriction, including without limitation the rights
|
|
4
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
5
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
6
|
+
furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all
|
|
9
|
+
copies or substantial portions of the Software.
|
|
10
|
+
|
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
17
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @atcute/identity-resolver-node
|
|
2
|
+
|
|
3
|
+
additional atproto identity resolvers for Node.js
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
// handle resolution
|
|
7
|
+
const handleResolver = new CompositeHandleResolver({
|
|
8
|
+
strategy: 'race',
|
|
9
|
+
methods: {
|
|
10
|
+
dns: new NodeDnsHandleResolver(),
|
|
11
|
+
http: new WellKnownHandleResolver(),
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const handle = await didResolver.resolve('bsky.app');
|
|
17
|
+
// ^? 'did:plc:z72i7hdynmk6r22z27h6tvur'
|
|
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
|
+
}
|
|
31
|
+
|
|
32
|
+
if (err instanceof HandleResolutionError) {
|
|
33
|
+
// the errors above extend this class, so you can do a catch-all.
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type AtprotoDid, type Handle } from '@atcute/identity';
|
|
2
|
+
import { type HandleResolver, type ResolveHandleOptions } from '@atcute/identity-resolver';
|
|
3
|
+
export interface NodeDnsHandleResolverOptions {
|
|
4
|
+
nameservers?: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare class NodeDnsHandleResolver implements HandleResolver {
|
|
7
|
+
#private;
|
|
8
|
+
get nameservers(): string[] | undefined;
|
|
9
|
+
constructor({ nameservers }?: NodeDnsHandleResolverOptions);
|
|
10
|
+
resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import dns from 'node:dns/promises';
|
|
2
|
+
import { isAtprotoDid } from '@atcute/identity';
|
|
3
|
+
import { AmbiguousHandleError, DidNotFoundError, FailedHandleResolutionError, InvalidResolvedHandleError, } from '@atcute/identity-resolver';
|
|
4
|
+
const SUBDOMAIN = '_atproto';
|
|
5
|
+
const PREFIX = 'did=';
|
|
6
|
+
export class NodeDnsHandleResolver {
|
|
7
|
+
#resolver = null;
|
|
8
|
+
get nameservers() {
|
|
9
|
+
return this.#resolver?.getServers();
|
|
10
|
+
}
|
|
11
|
+
constructor({ nameservers } = {}) {
|
|
12
|
+
if (nameservers) {
|
|
13
|
+
this.#resolver = new dns.Resolver();
|
|
14
|
+
this.#resolver.setServers(nameservers);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async resolve(handle, options) {
|
|
18
|
+
let results;
|
|
19
|
+
try {
|
|
20
|
+
const signal = options?.signal;
|
|
21
|
+
const resolver = this.#resolver ?? dns;
|
|
22
|
+
results = await resolver.resolveTxt(`${SUBDOMAIN}.${handle}`);
|
|
23
|
+
signal?.throwIfAborted();
|
|
24
|
+
}
|
|
25
|
+
catch (cause) {
|
|
26
|
+
if (cause instanceof Error && 'code' in cause && cause.code === 'ENOTFOUND') {
|
|
27
|
+
throw new DidNotFoundError(handle);
|
|
28
|
+
}
|
|
29
|
+
throw new FailedHandleResolutionError(handle, { cause });
|
|
30
|
+
}
|
|
31
|
+
const records = results.map((record) => record.join('').replace(/^"|"$/g, '').replace(/\\"/g, '"'));
|
|
32
|
+
for (let i = 0, il = records.length; i < il; i++) {
|
|
33
|
+
const data = records[i];
|
|
34
|
+
if (!data.startsWith(PREFIX)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (let j = i + 1; j < il; j++) {
|
|
38
|
+
const data = records[j];
|
|
39
|
+
if (data.startsWith(PREFIX)) {
|
|
40
|
+
throw new AmbiguousHandleError(handle);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const did = data.slice(PREFIX.length);
|
|
44
|
+
if (!isAtprotoDid(did)) {
|
|
45
|
+
throw new InvalidResolvedHandleError(handle, did);
|
|
46
|
+
}
|
|
47
|
+
return did;
|
|
48
|
+
}
|
|
49
|
+
// theoretically this shouldn't happen, it should've returned ENOTFOUND
|
|
50
|
+
throw new DidNotFoundError(handle);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../../lib/did/methods/node.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,mBAAmB,CAAC;AAEpC,OAAO,EAAE,YAAY,EAAgC,MAAM,kBAAkB,CAAC;AAC9E,OAAO,EACN,oBAAoB,EACpB,gBAAgB,EAChB,2BAA2B,EAC3B,0BAA0B,GAG1B,MAAM,2BAA2B,CAAC;AAEnC,MAAM,SAAS,GAAG,UAAU,CAAC;AAC7B,MAAM,MAAM,GAAG,MAAM,CAAC;AAMtB,MAAM,OAAO,qBAAqB;IACjC,SAAS,GAAwB,IAAI,CAAC;IAEtC,IAAI,WAAW;QACd,OAAO,IAAI,CAAC,SAAS,EAAE,UAAU,EAAE,CAAC;IACrC,CAAC;IAED,YAAY,EAAE,WAAW,KAAmC,EAAE;QAC7D,IAAI,WAAW,EAAE,CAAC;YACjB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;YACpC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;QACxC,CAAC;IACF,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,MAAc,EAAE,OAA8B;QAC3D,IAAI,OAAmB,CAAC;QAExB,IAAI,CAAC;YACJ,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAAC;YAC/B,MAAM,QAAQ,GAA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YACF,CAAC;YAED,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YACtC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;gBACxB,MAAM,IAAI,0BAA0B,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YACnD,CAAC;YAED,OAAO,GAAG,CAAC;QACZ,CAAC;QAED,uEAAuE;QACvE,MAAM,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;CACD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './did/methods/node.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import dns from 'node:dns/promises';
|
|
2
|
+
|
|
3
|
+
import { isAtprotoDid, type AtprotoDid, type Handle } from '@atcute/identity';
|
|
4
|
+
import {
|
|
5
|
+
AmbiguousHandleError,
|
|
6
|
+
DidNotFoundError,
|
|
7
|
+
FailedHandleResolutionError,
|
|
8
|
+
InvalidResolvedHandleError,
|
|
9
|
+
type HandleResolver,
|
|
10
|
+
type ResolveHandleOptions,
|
|
11
|
+
} from '@atcute/identity-resolver';
|
|
12
|
+
|
|
13
|
+
const SUBDOMAIN = '_atproto';
|
|
14
|
+
const PREFIX = 'did=';
|
|
15
|
+
|
|
16
|
+
export interface NodeDnsHandleResolverOptions {
|
|
17
|
+
nameservers?: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class NodeDnsHandleResolver implements HandleResolver {
|
|
21
|
+
#resolver: dns.Resolver | null = null;
|
|
22
|
+
|
|
23
|
+
get nameservers(): string[] | undefined {
|
|
24
|
+
return this.#resolver?.getServers();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
constructor({ nameservers }: NodeDnsHandleResolverOptions = {}) {
|
|
28
|
+
if (nameservers) {
|
|
29
|
+
this.#resolver = new dns.Resolver();
|
|
30
|
+
this.#resolver.setServers(nameservers);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async resolve(handle: Handle, options?: ResolveHandleOptions): Promise<AtprotoDid> {
|
|
35
|
+
let results: string[][];
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const signal = options?.signal;
|
|
39
|
+
const resolver = this.#resolver ?? dns;
|
|
40
|
+
|
|
41
|
+
results = await resolver.resolveTxt(`${SUBDOMAIN}.${handle}`);
|
|
42
|
+
signal?.throwIfAborted();
|
|
43
|
+
} catch (cause) {
|
|
44
|
+
if (cause instanceof Error && 'code' in cause && cause.code === 'ENOTFOUND') {
|
|
45
|
+
throw new DidNotFoundError(handle);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
throw new FailedHandleResolutionError(handle, { cause });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const records = results.map((record) => record.join('').replace(/^"|"$/g, '').replace(/\\"/g, '"'));
|
|
52
|
+
for (let i = 0, il = records.length; i < il; i++) {
|
|
53
|
+
const data = records[i];
|
|
54
|
+
|
|
55
|
+
if (!data.startsWith(PREFIX)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (let j = i + 1; j < il; j++) {
|
|
60
|
+
const data = records[j];
|
|
61
|
+
if (data.startsWith(PREFIX)) {
|
|
62
|
+
throw new AmbiguousHandleError(handle);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const did = data.slice(PREFIX.length);
|
|
67
|
+
if (!isAtprotoDid(did)) {
|
|
68
|
+
throw new InvalidResolvedHandleError(handle, did);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return did;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// theoretically this shouldn't happen, it should've returned ENOTFOUND
|
|
75
|
+
throw new DidNotFoundError(handle);
|
|
76
|
+
}
|
|
77
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './did/methods/node.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "@atcute/identity-resolver-node",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "additional atproto identity resolvers for Node.js",
|
|
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-node"
|
|
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
|
+
"@atcute/identity-resolver": "^0.1.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/bun": "^1.2.1",
|
|
31
|
+
"@atcute/identity": "^0.1.0",
|
|
32
|
+
"@atcute/identity-resolver": "^0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc --project tsconfig.build.json",
|
|
36
|
+
"test": "bun test --coverage",
|
|
37
|
+
"prepublish": "rm -rf dist; pnpm run build"
|
|
38
|
+
}
|
|
39
|
+
}
|