@atcute/lexicon-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 ADDED
@@ -0,0 +1,14 @@
1
+ BSD Zero Clause License
2
+
3
+ Copyright (c) 2025 Mary
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14
+ PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @atcute/lexicon-resolver-node
2
+
3
+ Node.js lexicon authority resolver using native DNS.
4
+
5
+ ```sh
6
+ npm install @atcute/lexicon-resolver-node
7
+ ```
8
+
9
+ provides `NodeDnsLexiconAuthorityResolver` which resolves lexicon authorities via DNS TXT records
10
+ using Node.js's native `dns` module, avoiding the need for HTTP-based DoH resolution.
11
+
12
+ ## usage
13
+
14
+ ```ts
15
+ import { NodeDnsLexiconAuthorityResolver } from '@atcute/lexicon-resolver-node';
16
+
17
+ const authorityResolver = new NodeDnsLexiconAuthorityResolver();
18
+
19
+ const authority = await authorityResolver.resolve('app.bsky.feed.post');
20
+ // -> 'did:plc:4v4y5r3lwsbtmsxhile2ljac'
21
+ ```
22
+
23
+ ### custom nameservers
24
+
25
+ ```ts
26
+ const resolver = new NodeDnsLexiconAuthorityResolver({
27
+ nameservers: ['8.8.8.8', '8.8.4.4'],
28
+ });
29
+ ```
@@ -0,0 +1,12 @@
1
+ import { type LexiconAuthorityResolver, type ResolveLexiconAuthorityOptions } from '@atcute/lexicon-resolver';
2
+ import type { AtprotoDid, Nsid } from '@atcute/lexicons/syntax';
3
+ export interface NodeDnsLexiconAuthorityResolverOptions {
4
+ nameservers?: string[];
5
+ }
6
+ export declare class NodeDnsLexiconAuthorityResolver implements LexiconAuthorityResolver {
7
+ #private;
8
+ get nameservers(): string[] | undefined;
9
+ constructor({ nameservers }?: NodeDnsLexiconAuthorityResolverOptions);
10
+ resolve(nsid: Nsid, options?: ResolveLexiconAuthorityOptions): Promise<AtprotoDid>;
11
+ }
12
+ //# sourceMappingURL=node-dns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-dns.d.ts","sourceRoot":"","sources":["../../lib/authority/node-dns.ts"],"names":[],"mappings":"AAGA,OAAO,EAKN,KAAK,wBAAwB,EAC7B,KAAK,8BAA8B,EAEnC,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAKhE,MAAM,WAAW,sCAAsC;IACtD,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,qBAAa,+BAAgC,YAAW,wBAAwB;;IAG/E,IAAI,WAAW,IAAI,MAAM,EAAE,GAAG,SAAS,CAEtC;IAED,YAAY,EAAE,WAAW,EAAE,GAAE,sCAA2C,EAKvE;IAEK,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,8BAA8B,GAAG,OAAO,CAAC,UAAU,CAAC,CA4CvF;CACD"}
@@ -0,0 +1,54 @@
1
+ import dns from 'node:dns/promises';
2
+ import { isAtprotoDid } from '@atcute/identity';
3
+ import { AmbiguousAuthorityError, AuthorityNotFoundError, FailedAuthorityResolutionError, InvalidResolvedAuthorityError, nsidToLookupDomain, } from '@atcute/lexicon-resolver';
4
+ const SUBDOMAIN = '_lexicon';
5
+ const PREFIX = 'did=';
6
+ export class NodeDnsLexiconAuthorityResolver {
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(nsid, options) {
18
+ const lookupDomain = nsidToLookupDomain(nsid);
19
+ let results;
20
+ try {
21
+ const signal = options?.signal;
22
+ const resolver = this.#resolver ?? dns;
23
+ results = await resolver.resolveTxt(`${SUBDOMAIN}.${lookupDomain}`);
24
+ signal?.throwIfAborted();
25
+ }
26
+ catch (cause) {
27
+ if (cause instanceof Error && 'code' in cause && cause.code === 'ENOTFOUND') {
28
+ throw new AuthorityNotFoundError(nsid);
29
+ }
30
+ throw new FailedAuthorityResolutionError(nsid, { cause });
31
+ }
32
+ const records = results.map((record) => record.join('').replace(/^"|"$/g, '').replace(/\\"/g, '"'));
33
+ for (let i = 0, il = records.length; i < il; i++) {
34
+ const data = records[i];
35
+ if (!data.startsWith(PREFIX)) {
36
+ continue;
37
+ }
38
+ for (let j = i + 1; j < il; j++) {
39
+ const data = records[j];
40
+ if (data.startsWith(PREFIX)) {
41
+ throw new AmbiguousAuthorityError(nsid);
42
+ }
43
+ }
44
+ const did = data.slice(PREFIX.length);
45
+ if (!isAtprotoDid(did)) {
46
+ throw new InvalidResolvedAuthorityError(nsid, did);
47
+ }
48
+ return did;
49
+ }
50
+ // theoretically this shouldn't happen, it should've returned ENOTFOUND
51
+ throw new AuthorityNotFoundError(nsid);
52
+ }
53
+ }
54
+ //# sourceMappingURL=node-dns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-dns.js","sourceRoot":"","sources":["../../lib/authority/node-dns.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,mBAAmB,CAAC;AAEpC,OAAO,EAAE,YAAY,EAAE,M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1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './authority/node-dns.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC"}
@@ -0,0 +1,81 @@
1
+ import dns from 'node:dns/promises';
2
+
3
+ import { isAtprotoDid } from '@atcute/identity';
4
+ import {
5
+ AmbiguousAuthorityError,
6
+ AuthorityNotFoundError,
7
+ FailedAuthorityResolutionError,
8
+ InvalidResolvedAuthorityError,
9
+ type LexiconAuthorityResolver,
10
+ type ResolveLexiconAuthorityOptions,
11
+ nsidToLookupDomain,
12
+ } from '@atcute/lexicon-resolver';
13
+ import type { AtprotoDid, Nsid } from '@atcute/lexicons/syntax';
14
+
15
+ const SUBDOMAIN = '_lexicon';
16
+ const PREFIX = 'did=';
17
+
18
+ export interface NodeDnsLexiconAuthorityResolverOptions {
19
+ nameservers?: string[];
20
+ }
21
+
22
+ export class NodeDnsLexiconAuthorityResolver implements LexiconAuthorityResolver {
23
+ #resolver: dns.Resolver | null = null;
24
+
25
+ get nameservers(): string[] | undefined {
26
+ return this.#resolver?.getServers();
27
+ }
28
+
29
+ constructor({ nameservers }: NodeDnsLexiconAuthorityResolverOptions = {}) {
30
+ if (nameservers) {
31
+ this.#resolver = new dns.Resolver();
32
+ this.#resolver.setServers(nameservers);
33
+ }
34
+ }
35
+
36
+ async resolve(nsid: Nsid, options?: ResolveLexiconAuthorityOptions): Promise<AtprotoDid> {
37
+ const lookupDomain = nsidToLookupDomain(nsid);
38
+
39
+ let results: string[][];
40
+
41
+ try {
42
+ const signal = options?.signal;
43
+ const resolver = this.#resolver ?? dns;
44
+
45
+ results = await resolver.resolveTxt(`${SUBDOMAIN}.${lookupDomain}`);
46
+ signal?.throwIfAborted();
47
+ } catch (cause) {
48
+ if (cause instanceof Error && 'code' in cause && cause.code === 'ENOTFOUND') {
49
+ throw new AuthorityNotFoundError(nsid);
50
+ }
51
+
52
+ throw new FailedAuthorityResolutionError(nsid, { cause });
53
+ }
54
+
55
+ const records = results.map((record) => record.join('').replace(/^"|"$/g, '').replace(/\\"/g, '"'));
56
+ for (let i = 0, il = records.length; i < il; i++) {
57
+ const data = records[i];
58
+
59
+ if (!data.startsWith(PREFIX)) {
60
+ continue;
61
+ }
62
+
63
+ for (let j = i + 1; j < il; j++) {
64
+ const data = records[j];
65
+ if (data.startsWith(PREFIX)) {
66
+ throw new AmbiguousAuthorityError(nsid);
67
+ }
68
+ }
69
+
70
+ const did = data.slice(PREFIX.length);
71
+ if (!isAtprotoDid(did)) {
72
+ throw new InvalidResolvedAuthorityError(nsid, did);
73
+ }
74
+
75
+ return did;
76
+ }
77
+
78
+ // theoretically this shouldn't happen, it should've returned ENOTFOUND
79
+ throw new AuthorityNotFoundError(nsid);
80
+ }
81
+ }
package/lib/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './authority/node-dns.js';
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@atcute/lexicon-resolver-node",
4
+ "version": "0.1.0",
5
+ "description": "additional atproto lexicon resolvers for Node.js",
6
+ "keywords": [
7
+ "atproto",
8
+ "lexicon",
9
+ "did"
10
+ ],
11
+ "license": "0BSD",
12
+ "repository": {
13
+ "url": "https://github.com/mary-ext/atcute",
14
+ "directory": "packages/lexicons/lexicon-resolver-node"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "files": [
20
+ "dist/",
21
+ "lib/",
22
+ "!lib/**/*.bench.ts",
23
+ "!lib/**/*.test.ts"
24
+ ],
25
+ "exports": {
26
+ ".": "./dist/index.js"
27
+ },
28
+ "sideEffects": false,
29
+ "peerDependencies": {
30
+ "@atcute/identity": "^1.0.0",
31
+ "@atcute/lexicon-resolver": "^0.1.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^22.19.3",
35
+ "@atcute/lexicon-resolver": "^0.1.6",
36
+ "@atcute/identity": "^1.1.3"
37
+ },
38
+ "dependencies": {
39
+ "@atcute/lexicons": "^1.2.6"
40
+ },
41
+ "scripts": {
42
+ "build": "tsgo --project tsconfig.build.json",
43
+ "prepublish": "rm -rf dist; pnpm run build"
44
+ }
45
+ }