@helia/dnslink 1.0.1 → 1.1.0-eb91b810
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/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/constants.d.ts +3 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +3 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/dnslink.d.ts +8 -6
- package/dist/src/dnslink.d.ts.map +1 -1
- package/dist/src/dnslink.js +47 -2
- package/dist/src/dnslink.js.map +1 -1
- package/dist/src/index.d.ts +33 -12
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/namespaces/ipfs.d.ts +2 -2
- package/dist/src/namespaces/ipfs.d.ts.map +1 -1
- package/dist/src/namespaces/ipfs.js +11 -13
- package/dist/src/namespaces/ipfs.js.map +1 -1
- package/dist/src/namespaces/ipns.d.ts +2 -2
- package/dist/src/namespaces/ipns.d.ts.map +1 -1
- package/dist/src/namespaces/ipns.js +11 -13
- package/dist/src/namespaces/ipns.js.map +1 -1
- package/package.json +4 -2
- package/src/constants.ts +3 -0
- package/src/dnslink.ts +66 -10
- package/src/index.ts +38 -13
- package/src/namespaces/ipfs.ts +12 -14
- package/src/namespaces/ipns.ts +12 -14
- package/dist/typedoc-urls.json +0 -22
package/dist/src/constants.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,KAAK,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,UAAU,OAAQ,CAAA;AAC/B,eAAO,MAAM,aAAa,QAAS,CAAA;AACnC,eAAO,MAAM,iBAAiB,KAAK,CAAA"}
|
package/dist/src/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAA"}
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAA;AACrC,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,CAAA;AAC/B,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAA;AACnC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAA"}
|
package/dist/src/dnslink.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type { DNSLink as DNSLinkInterface, ResolveDNSLinkOptions, DNSLinkOptions, DNSLinkComponents,
|
|
2
|
-
export declare class DNSLink implements DNSLinkInterface {
|
|
1
|
+
import type { DNSLink as DNSLinkInterface, ResolveDNSLinkOptions, DNSLinkOptions, DNSLinkComponents, DNSLinkParser, DNSLinkResolveResult } from './index.js';
|
|
2
|
+
export declare class DNSLink<Namespaces extends Record<string, DNSLinkParser<DNSLinkResolveResult>>> implements DNSLinkInterface<ReturnType<Namespaces[keyof Namespaces]>> {
|
|
3
3
|
private readonly dns;
|
|
4
4
|
private readonly log;
|
|
5
5
|
private readonly namespaces;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
private readonly cache;
|
|
7
|
+
private readonly cacheMaxAnswers;
|
|
8
|
+
constructor(components: DNSLinkComponents, init?: DNSLinkOptions<Namespaces>);
|
|
9
|
+
resolve(domain: string, options?: ResolveDNSLinkOptions): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>>;
|
|
10
|
+
recursiveResolveDomain(domain: string, depth: number, options?: ResolveDNSLinkOptions): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>>;
|
|
11
|
+
recursiveResolveDnslink(domain: string, depth: number, options?: ResolveDNSLinkOptions): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>>;
|
|
10
12
|
}
|
|
11
13
|
//# sourceMappingURL=dnslink.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dnslink.d.ts","sourceRoot":"","sources":["../../src/dnslink.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dnslink.d.ts","sourceRoot":"","sources":["../../src/dnslink.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,OAAO,IAAI,gBAAgB,EAAE,qBAAqB,EAAE,cAAc,EAAE,iBAAiB,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAI5J,qBAAa,OAAO,CAAE,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,oBAAoB,CAAC,CAAC,CAAE,YAAW,gBAAgB,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC;IACjK,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAK;IACzB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAQ;IAC5B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoC;IAC/D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8E;IACpG,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAQ;gBAE3B,UAAU,EAAE,iBAAiB,EAAE,IAAI,GAAE,cAAc,CAAC,UAAU,CAAM;IAe3E,OAAO,CAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC;IAmDvH,sBAAsB,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC;IAkCrJ,uBAAuB,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC;CA0F7J"}
|
package/dist/src/dnslink.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { MAX_RECURSIVE_DEPTH, RecordType } from '@multiformats/dns';
|
|
2
|
+
import QuickLRU from 'quick-lru';
|
|
3
|
+
import { CACHE_MAX_AGE, CACHE_MAX_ANSWERS, CACHE_SIZE } from "./constants.js";
|
|
2
4
|
import { DNSLinkNotFoundError } from './errors.js';
|
|
3
5
|
import { ipfs } from "./namespaces/ipfs.js";
|
|
4
6
|
import { ipns } from "./namespaces/ipns.js";
|
|
@@ -6,6 +8,8 @@ export class DNSLink {
|
|
|
6
8
|
dns;
|
|
7
9
|
log;
|
|
8
10
|
namespaces;
|
|
11
|
+
cache;
|
|
12
|
+
cacheMaxAnswers;
|
|
9
13
|
constructor(components, init = {}) {
|
|
10
14
|
this.dns = components.dns;
|
|
11
15
|
this.log = components.logger.forComponent('helia:dnslink');
|
|
@@ -14,9 +18,50 @@ export class DNSLink {
|
|
|
14
18
|
ipns,
|
|
15
19
|
...init.namespaces
|
|
16
20
|
};
|
|
21
|
+
this.cache = new QuickLRU({
|
|
22
|
+
maxSize: init.cacheSize ?? CACHE_SIZE,
|
|
23
|
+
maxAge: init.cacheMaxAge ?? CACHE_MAX_AGE
|
|
24
|
+
});
|
|
25
|
+
this.cacheMaxAnswers = init.cacheMaxAnswers ?? CACHE_MAX_ANSWERS;
|
|
17
26
|
}
|
|
18
27
|
async resolve(domain, options = {}) {
|
|
19
|
-
|
|
28
|
+
if (options.nocache !== true) {
|
|
29
|
+
// check the cache if allowed
|
|
30
|
+
const cached = this.cache.get(domain);
|
|
31
|
+
if (cached != null) {
|
|
32
|
+
const answers = [...cached.values()];
|
|
33
|
+
if (answers.length > 0) {
|
|
34
|
+
return answers;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const result = await this.recursiveResolveDomain(domain, options.maxRecursiveDepth ?? MAX_RECURSIVE_DEPTH, options);
|
|
39
|
+
// cache answers according to individual TTLs
|
|
40
|
+
const cache = new QuickLRU({
|
|
41
|
+
maxSize: this.cacheMaxAnswers
|
|
42
|
+
});
|
|
43
|
+
result.forEach((result, index) => {
|
|
44
|
+
cache.set(index, result, {
|
|
45
|
+
maxAge: (result.answer.TTL * 1000)
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
// find longest answer TTL
|
|
49
|
+
let maxTTL = result.reduce((acc, curr) => {
|
|
50
|
+
const ttl = (curr.answer.TTL * 1000);
|
|
51
|
+
if (ttl > acc) {
|
|
52
|
+
return ttl;
|
|
53
|
+
}
|
|
54
|
+
return acc;
|
|
55
|
+
}, 0);
|
|
56
|
+
// if the configured max age is less than the longest answer TTL, use that
|
|
57
|
+
// instead
|
|
58
|
+
if (this.cache.maxAge < maxTTL) {
|
|
59
|
+
maxTTL = this.cache.maxAge;
|
|
60
|
+
}
|
|
61
|
+
this.cache.set(domain, cache, {
|
|
62
|
+
maxAge: maxTTL
|
|
63
|
+
});
|
|
64
|
+
return result;
|
|
20
65
|
}
|
|
21
66
|
async recursiveResolveDomain(domain, depth, options = {}) {
|
|
22
67
|
if (depth === 0) {
|
|
@@ -89,7 +134,7 @@ export class DNSLink {
|
|
|
89
134
|
this.log('unknown protocol "%s" in DNSLink record for domain: %s', protocol, domain);
|
|
90
135
|
continue;
|
|
91
136
|
}
|
|
92
|
-
output.push(parser
|
|
137
|
+
output.push(parser(result, answer));
|
|
93
138
|
}
|
|
94
139
|
catch (err) {
|
|
95
140
|
this.log.error('could not parse DNS link record for domain %s, %s', domain, answer.data, err);
|
package/dist/src/dnslink.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dnslink.js","sourceRoot":"","sources":["../../src/dnslink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAK3C,MAAM,OAAO,OAAO;IACD,GAAG,CAAK;IACR,GAAG,CAAQ;IACX,UAAU,
|
|
1
|
+
{"version":3,"file":"dnslink.js","sourceRoot":"","sources":["../../src/dnslink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,QAAQ,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAK3C,MAAM,OAAO,OAAO;IACD,GAAG,CAAK;IACR,GAAG,CAAQ;IACX,UAAU,CAAoC;IAC9C,KAAK,CAA8E;IACnF,eAAe,CAAQ;IAExC,YAAa,UAA6B,EAAE,OAAmC,EAAE;QAC/E,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,CAAA;QACzB,IAAI,CAAC,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC,YAAY,CAAC,eAAe,CAAC,CAAA;QAC1D,IAAI,CAAC,UAAU,GAAG;YAChB,IAAI;YACJ,IAAI;YACJ,GAAG,IAAI,CAAC,UAAU;SACnB,CAAA;QACD,IAAI,CAAC,KAAK,GAAG,IA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ger, PeerId } from '@libp2p/interface';
|
|
108
|
-
import type { Answer, DNS, ResolveDnsProgressEvents } from '@multiformats/dns';
|
|
108
|
+
import type { Answer, DNS, ResolveDnsProgressEvents as ResolveProgressEvents } from '@multiformats/dns';
|
|
109
109
|
import type { CID } from 'multiformats/cid';
|
|
110
110
|
import type { ProgressOptions } from 'progress-events';
|
|
111
|
-
export
|
|
111
|
+
export type { ResolveProgressEvents };
|
|
112
|
+
export interface ResolveDNSLinkOptions extends AbortOptions, ProgressOptions<ResolveProgressEvents> {
|
|
112
113
|
/**
|
|
113
114
|
* Do not query the network for the IPNS record
|
|
114
115
|
*
|
|
@@ -153,7 +154,7 @@ export interface DNSLinkIPNSResult {
|
|
|
153
154
|
*/
|
|
154
155
|
answer: Answer;
|
|
155
156
|
/**
|
|
156
|
-
* The
|
|
157
|
+
* The IPNS namespace
|
|
157
158
|
*/
|
|
158
159
|
namespace: 'ipns';
|
|
159
160
|
/**
|
|
@@ -165,24 +166,23 @@ export interface DNSLinkIPNSResult {
|
|
|
165
166
|
*/
|
|
166
167
|
path: string;
|
|
167
168
|
}
|
|
168
|
-
export interface
|
|
169
|
+
export interface DNSLinkResolveResult {
|
|
169
170
|
/**
|
|
170
171
|
* The resolved record
|
|
171
172
|
*/
|
|
172
173
|
answer: Answer;
|
|
173
174
|
/**
|
|
174
|
-
* The
|
|
175
|
+
* The namespace
|
|
175
176
|
*/
|
|
176
177
|
namespace: string;
|
|
177
178
|
}
|
|
178
|
-
export
|
|
179
|
-
export interface DNSLinkNamespace {
|
|
179
|
+
export interface DNSLinkParser<T extends DNSLinkResolveResult> {
|
|
180
180
|
/**
|
|
181
181
|
* Return a result parsed from a DNSLink value
|
|
182
182
|
*/
|
|
183
|
-
|
|
183
|
+
(value: string, answer: Answer): T;
|
|
184
184
|
}
|
|
185
|
-
export interface DNSLink {
|
|
185
|
+
export interface DNSLink<DNSLinkResolveResults extends DNSLinkResolveResult = DNSLinkIPFSResult | DNSLinkIPNSResult> {
|
|
186
186
|
/**
|
|
187
187
|
* Resolve a CID from a dns-link style IPNS record
|
|
188
188
|
*
|
|
@@ -202,18 +202,39 @@ export interface DNSLink {
|
|
|
202
202
|
* console.info(result) // { answer: ..., value: ... }
|
|
203
203
|
* ```
|
|
204
204
|
*/
|
|
205
|
-
resolve(domain: string, options?: ResolveDNSLinkOptions): Promise<
|
|
205
|
+
resolve(domain: string, options?: ResolveDNSLinkOptions): Promise<Array<DNSLinkResolveResults | DNSLinkIPFSResult | DNSLinkIPNSResult>>;
|
|
206
206
|
}
|
|
207
207
|
export interface DNSLinkComponents {
|
|
208
208
|
dns: DNS;
|
|
209
209
|
logger: ComponentLogger;
|
|
210
210
|
}
|
|
211
|
-
export interface DNSLinkOptions {
|
|
211
|
+
export interface DNSLinkOptions<Namespaces extends Record<string, DNSLinkParser<any>> = Record<string, DNSLinkParser<any>>> {
|
|
212
212
|
/**
|
|
213
213
|
* By default `/ipfs/...`, `/ipns/...` and `/dnslink/...` record values are
|
|
214
214
|
* supported - to support other prefixes pass other value parsers here
|
|
215
215
|
*/
|
|
216
|
-
namespaces?:
|
|
216
|
+
namespaces?: Namespaces;
|
|
217
|
+
/**
|
|
218
|
+
* How many DNSLink lookups to cache in memory
|
|
219
|
+
*
|
|
220
|
+
* @default 1_000
|
|
221
|
+
*/
|
|
222
|
+
cacheSize?: number;
|
|
223
|
+
/**
|
|
224
|
+
* The maximum number of Answers to cache for each domain
|
|
225
|
+
*
|
|
226
|
+
* @default 10
|
|
227
|
+
*/
|
|
228
|
+
cacheMaxAnswers?: number;
|
|
229
|
+
/**
|
|
230
|
+
* By default the DNS TTL will be used to calculate when a DNSLink lookup
|
|
231
|
+
* should be evicted from the cache, unless one is not available in which case
|
|
232
|
+
* this number of ms will be used.
|
|
233
|
+
*
|
|
234
|
+
* @default 30_000
|
|
235
|
+
*/
|
|
236
|
+
cacheMaxAge?: number;
|
|
217
237
|
}
|
|
238
|
+
export declare function dnsLink<R extends DNSLinkResolveResult, T extends Record<string, DNSLinkParser<R>>>(components: DNSLinkComponents, options: DNSLinkOptions<T>): DNSLink<ReturnType<T[keyof T]>>;
|
|
218
239
|
export declare function dnsLink(components: DNSLinkComponents, options?: DNSLinkOptions): DNSLink;
|
|
219
240
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyGG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyGG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC9E,OAAO,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,wBAAwB,IAAI,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AACvG,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEtD,YAAY,EAAE,qBAAqB,EAAE,CAAA;AAErC,MAAM,WAAW,qBAAsB,SAAQ,YAAY,EAAE,eAAe,CAAC,qBAAqB,CAAC;IACjG;;;;OAIG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,aAAa,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;IACxH;;;OAGG;IACH,UAAU,CAAC,EAAE,UAAU,CAAA;IAEvB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAA;IAExB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,wBAAgB,OAAO,CAAE,CAAC,SAAS,oBAAoB,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC,EAAG,UAAU,EAAE,iBAAiB,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;AACjM,wBAAgB,OAAO,CAAE,UAAU,EAAE,iBAAiB,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAA"}
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyGG;AAEH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyGG;AAEH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,cAAc,CAAA;AA4JtD,MAAM,UAAU,OAAO,CAAE,UAA6B,EAAE,UAA0B,EAAE;IAClF,OAAO,IAAI,YAAY,CAAC,UAAU,EAAE,OAAO,CAAC,CAAA;AAC9C,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const ipfs:
|
|
1
|
+
import type { DNSLinkParser, DNSLinkIPFSResult } from '../index.js';
|
|
2
|
+
export declare const ipfs: DNSLinkParser<DNSLinkIPFSResult>;
|
|
3
3
|
//# sourceMappingURL=ipfs.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipfs.d.ts","sourceRoot":"","sources":["../../../src/namespaces/ipfs.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"ipfs.d.ts","sourceRoot":"","sources":["../../../src/namespaces/ipfs.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAGnE,eAAO,MAAM,IAAI,EAAE,aAAa,CAAC,iBAAiB,CAcjD,CAAA"}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { CID } from 'multiformats/cid';
|
|
2
2
|
import { InvalidNamespaceError } from "../errors.js";
|
|
3
|
-
export const ipfs = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
throw new InvalidNamespaceError(`Namespace ${protocol} was not "ipfs"`);
|
|
8
|
-
}
|
|
9
|
-
// if the result is a CID, we've reached the end of the recursion
|
|
10
|
-
return {
|
|
11
|
-
namespace: 'ipfs',
|
|
12
|
-
cid: CID.parse(cid),
|
|
13
|
-
path: rest.length > 0 ? `/${rest.join('/')}` : '',
|
|
14
|
-
answer
|
|
15
|
-
};
|
|
3
|
+
export const ipfs = (value, answer) => {
|
|
4
|
+
const [, protocol, cid, ...rest] = value.split('/');
|
|
5
|
+
if (protocol !== 'ipfs') {
|
|
6
|
+
throw new InvalidNamespaceError(`Namespace ${protocol} was not "ipfs"`);
|
|
16
7
|
}
|
|
8
|
+
// if the result is a CID, we've reached the end of the recursion
|
|
9
|
+
return {
|
|
10
|
+
namespace: 'ipfs',
|
|
11
|
+
cid: CID.parse(cid),
|
|
12
|
+
path: rest.length > 0 ? `/${rest.join('/')}` : '',
|
|
13
|
+
answer
|
|
14
|
+
};
|
|
17
15
|
};
|
|
18
16
|
//# sourceMappingURL=ipfs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipfs.js","sourceRoot":"","sources":["../../../src/namespaces/ipfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAIpD,MAAM,CAAC,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"ipfs.js","sourceRoot":"","sources":["../../../src/namespaces/ipfs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAIpD,MAAM,CAAC,MAAM,IAAI,GAAqC,CAAC,KAAa,EAAE,MAAc,EAAqB,EAAE;IACzG,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEnD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,MAAM,IAAI,qBAAqB,CAAC,aAAa,QAAQ,iBAAiB,CAAC,CAAA;IACzE,CAAC;IAED,iEAAiE;IACjE,OAAO;QACL,SAAS,EAAE,MAAM;QACjB,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC;QACnB,IAAI,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE;QACjD,MAAM;KACP,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const ipns:
|
|
1
|
+
import type { DNSLinkParser, DNSLinkIPNSResult } from '../index.js';
|
|
2
|
+
export declare const ipns: DNSLinkParser<DNSLinkIPNSResult>;
|
|
3
3
|
//# sourceMappingURL=ipns.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipns.d.ts","sourceRoot":"","sources":["../../../src/namespaces/ipns.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"ipns.d.ts","sourceRoot":"","sources":["../../../src/namespaces/ipns.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAGnE,eAAO,MAAM,IAAI,EAAE,aAAa,CAAC,iBAAiB,CAcjD,CAAA"}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
2
|
import { InvalidNamespaceError } from "../errors.js";
|
|
3
|
-
export const ipns = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
throw new InvalidNamespaceError(`Namespace ${protocol} was not "ipns"`);
|
|
8
|
-
}
|
|
9
|
-
// if the result is a CID, we've reached the end of the recursion
|
|
10
|
-
return {
|
|
11
|
-
namespace: 'ipns',
|
|
12
|
-
peerId: peerIdFromString(peerId),
|
|
13
|
-
path: rest.length > 0 ? `/${rest.join('/')}` : '',
|
|
14
|
-
answer
|
|
15
|
-
};
|
|
3
|
+
export const ipns = (value, answer) => {
|
|
4
|
+
const [, protocol, peerId, ...rest] = value.split('/');
|
|
5
|
+
if (protocol !== 'ipns') {
|
|
6
|
+
throw new InvalidNamespaceError(`Namespace ${protocol} was not "ipns"`);
|
|
16
7
|
}
|
|
8
|
+
// if the result is a CID, we've reached the end of the recursion
|
|
9
|
+
return {
|
|
10
|
+
namespace: 'ipns',
|
|
11
|
+
peerId: peerIdFromString(peerId),
|
|
12
|
+
path: rest.length > 0 ? `/${rest.join('/')}` : '',
|
|
13
|
+
answer
|
|
14
|
+
};
|
|
17
15
|
};
|
|
18
16
|
//# sourceMappingURL=ipns.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ipns.js","sourceRoot":"","sources":["../../../src/namespaces/ipns.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAIpD,MAAM,CAAC,MAAM,IAAI,
|
|
1
|
+
{"version":3,"file":"ipns.js","sourceRoot":"","sources":["../../../src/namespaces/ipns.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAIpD,MAAM,CAAC,MAAM,IAAI,GAAqC,CAAC,KAAa,EAAE,MAAc,EAAqB,EAAE;IACzG,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEtD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;QACxB,MAAM,IAAI,qBAAqB,CAAC,aAAa,QAAQ,iBAAiB,CAAC,CAAA;IACzE,CAAC;IAED,iEAAiE;IACjE,OAAO;QACL,SAAS,EAAE,MAAM;QACjB,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC;QAChC,IAAI,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE;QACjD,MAAM;KACP,CAAA;AACH,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/dnslink",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0-eb91b810",
|
|
4
4
|
"description": "DNSLink operations using Helia",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/ipfs/helia/tree/main/packages/dnslink#readme",
|
|
@@ -68,12 +68,14 @@
|
|
|
68
68
|
"@libp2p/peer-id": "^6.0.3",
|
|
69
69
|
"@multiformats/dns": "^1.0.9",
|
|
70
70
|
"multiformats": "^13.4.1",
|
|
71
|
-
"progress-events": "^1.0.1"
|
|
71
|
+
"progress-events": "^1.0.1",
|
|
72
|
+
"quick-lru": "^7.3.0"
|
|
72
73
|
},
|
|
73
74
|
"devDependencies": {
|
|
74
75
|
"@libp2p/logger": "^6.0.5",
|
|
75
76
|
"@types/dns-packet": "^5.6.5",
|
|
76
77
|
"aegir": "^47.0.22",
|
|
78
|
+
"delay": "^6.0.0",
|
|
77
79
|
"sinon-ts": "^2.0.0"
|
|
78
80
|
},
|
|
79
81
|
"browser": {
|
package/src/constants.ts
CHANGED
package/src/dnslink.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
import { MAX_RECURSIVE_DEPTH, RecordType } from '@multiformats/dns'
|
|
2
|
+
import QuickLRU from 'quick-lru'
|
|
3
|
+
import { CACHE_MAX_AGE, CACHE_MAX_ANSWERS, CACHE_SIZE } from './constants.ts'
|
|
2
4
|
import { DNSLinkNotFoundError } from './errors.js'
|
|
3
5
|
import { ipfs } from './namespaces/ipfs.ts'
|
|
4
6
|
import { ipns } from './namespaces/ipns.ts'
|
|
5
|
-
import type { DNSLink as DNSLinkInterface, ResolveDNSLinkOptions, DNSLinkOptions, DNSLinkComponents,
|
|
7
|
+
import type { DNSLink as DNSLinkInterface, ResolveDNSLinkOptions, DNSLinkOptions, DNSLinkComponents, DNSLinkParser, DNSLinkResolveResult } from './index.js'
|
|
6
8
|
import type { Logger } from '@libp2p/interface'
|
|
7
9
|
import type { DNS } from '@multiformats/dns'
|
|
8
10
|
|
|
9
|
-
export class DNSLink implements DNSLinkInterface {
|
|
11
|
+
export class DNSLink <Namespaces extends Record<string, DNSLinkParser<DNSLinkResolveResult>>> implements DNSLinkInterface<ReturnType<Namespaces[keyof Namespaces]>> {
|
|
10
12
|
private readonly dns: DNS
|
|
11
13
|
private readonly log: Logger
|
|
12
|
-
private readonly namespaces: Record<string,
|
|
14
|
+
private readonly namespaces: Record<string, DNSLinkParser<any>>
|
|
15
|
+
private readonly cache: QuickLRU<string, QuickLRU<number, ReturnType<Namespaces[keyof Namespaces]>>>
|
|
16
|
+
private readonly cacheMaxAnswers: number
|
|
13
17
|
|
|
14
|
-
constructor (components: DNSLinkComponents, init: DNSLinkOptions = {}) {
|
|
18
|
+
constructor (components: DNSLinkComponents, init: DNSLinkOptions<Namespaces> = {}) {
|
|
15
19
|
this.dns = components.dns
|
|
16
20
|
this.log = components.logger.forComponent('helia:dnslink')
|
|
17
21
|
this.namespaces = {
|
|
@@ -19,13 +23,65 @@ export class DNSLink implements DNSLinkInterface {
|
|
|
19
23
|
ipns,
|
|
20
24
|
...init.namespaces
|
|
21
25
|
}
|
|
26
|
+
this.cache = new QuickLRU({
|
|
27
|
+
maxSize: init.cacheSize ?? CACHE_SIZE,
|
|
28
|
+
maxAge: init.cacheMaxAge ?? CACHE_MAX_AGE
|
|
29
|
+
})
|
|
30
|
+
this.cacheMaxAnswers = init.cacheMaxAnswers ?? CACHE_MAX_ANSWERS
|
|
22
31
|
}
|
|
23
32
|
|
|
24
|
-
async resolve (domain: string, options: ResolveDNSLinkOptions = {}): Promise<
|
|
25
|
-
|
|
33
|
+
async resolve (domain: string, options: ResolveDNSLinkOptions = {}): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>> {
|
|
34
|
+
if (options.nocache !== true) {
|
|
35
|
+
// check the cache if allowed
|
|
36
|
+
const cached = this.cache.get(domain)
|
|
37
|
+
|
|
38
|
+
if (cached != null) {
|
|
39
|
+
const answers = [...cached.values()]
|
|
40
|
+
|
|
41
|
+
if (answers.length > 0) {
|
|
42
|
+
return answers
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const result = await this.recursiveResolveDomain(domain, options.maxRecursiveDepth ?? MAX_RECURSIVE_DEPTH, options)
|
|
48
|
+
|
|
49
|
+
// cache answers according to individual TTLs
|
|
50
|
+
const cache = new QuickLRU<number, ReturnType<Namespaces[keyof Namespaces]>>({
|
|
51
|
+
maxSize: this.cacheMaxAnswers
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
result.forEach((result, index) => {
|
|
55
|
+
cache.set(index, result, {
|
|
56
|
+
maxAge: (result.answer.TTL * 1000)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
// find longest answer TTL
|
|
61
|
+
let maxTTL = result.reduce((acc, curr) => {
|
|
62
|
+
const ttl = (curr.answer.TTL * 1000)
|
|
63
|
+
|
|
64
|
+
if (ttl > acc) {
|
|
65
|
+
return ttl
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return acc
|
|
69
|
+
}, 0)
|
|
70
|
+
|
|
71
|
+
// if the configured max age is less than the longest answer TTL, use that
|
|
72
|
+
// instead
|
|
73
|
+
if (this.cache.maxAge < maxTTL) {
|
|
74
|
+
maxTTL = this.cache.maxAge
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this.cache.set(domain, cache, {
|
|
78
|
+
maxAge: maxTTL
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
return result
|
|
26
82
|
}
|
|
27
83
|
|
|
28
|
-
async recursiveResolveDomain (domain: string, depth: number, options: ResolveDNSLinkOptions = {}): Promise<
|
|
84
|
+
async recursiveResolveDomain (domain: string, depth: number, options: ResolveDNSLinkOptions = {}): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>> {
|
|
29
85
|
if (depth === 0) {
|
|
30
86
|
throw new Error('recursion limit exceeded')
|
|
31
87
|
}
|
|
@@ -59,7 +115,7 @@ export class DNSLink implements DNSLinkInterface {
|
|
|
59
115
|
}
|
|
60
116
|
}
|
|
61
117
|
|
|
62
|
-
async recursiveResolveDnslink (domain: string, depth: number, options: ResolveDNSLinkOptions = {}): Promise<
|
|
118
|
+
async recursiveResolveDnslink (domain: string, depth: number, options: ResolveDNSLinkOptions = {}): Promise<Array<ReturnType<Namespaces[keyof Namespaces]>>> {
|
|
63
119
|
if (depth === 0) {
|
|
64
120
|
throw new Error('recursion limit exceeded')
|
|
65
121
|
}
|
|
@@ -78,7 +134,7 @@ export class DNSLink implements DNSLinkInterface {
|
|
|
78
134
|
|
|
79
135
|
this.log('found %d TXT records for %s', txtRecords.length, domain)
|
|
80
136
|
|
|
81
|
-
const output:
|
|
137
|
+
const output: Array<ReturnType<Namespaces[keyof Namespaces]>> = []
|
|
82
138
|
|
|
83
139
|
for (const answer of txtRecords) {
|
|
84
140
|
try {
|
|
@@ -113,7 +169,7 @@ export class DNSLink implements DNSLinkInterface {
|
|
|
113
169
|
continue
|
|
114
170
|
}
|
|
115
171
|
|
|
116
|
-
output.push(parser
|
|
172
|
+
output.push(parser(result, answer))
|
|
117
173
|
} catch (err: any) {
|
|
118
174
|
this.log.error('could not parse DNS link record for domain %s, %s', domain, answer.data, err)
|
|
119
175
|
}
|
package/src/index.ts
CHANGED
|
@@ -107,11 +107,13 @@
|
|
|
107
107
|
|
|
108
108
|
import { DNSLink as DNSLinkClass } from './dnslink.js'
|
|
109
109
|
import type { AbortOptions, ComponentLogger, PeerId } from '@libp2p/interface'
|
|
110
|
-
import type { Answer, DNS, ResolveDnsProgressEvents } from '@multiformats/dns'
|
|
110
|
+
import type { Answer, DNS, ResolveDnsProgressEvents as ResolveProgressEvents } from '@multiformats/dns'
|
|
111
111
|
import type { CID } from 'multiformats/cid'
|
|
112
112
|
import type { ProgressOptions } from 'progress-events'
|
|
113
113
|
|
|
114
|
-
export
|
|
114
|
+
export type { ResolveProgressEvents }
|
|
115
|
+
|
|
116
|
+
export interface ResolveDNSLinkOptions extends AbortOptions, ProgressOptions<ResolveProgressEvents> {
|
|
115
117
|
/**
|
|
116
118
|
* Do not query the network for the IPNS record
|
|
117
119
|
*
|
|
@@ -164,7 +166,7 @@ export interface DNSLinkIPNSResult {
|
|
|
164
166
|
answer: Answer
|
|
165
167
|
|
|
166
168
|
/**
|
|
167
|
-
* The
|
|
169
|
+
* The IPNS namespace
|
|
168
170
|
*/
|
|
169
171
|
namespace: 'ipns'
|
|
170
172
|
|
|
@@ -179,28 +181,26 @@ export interface DNSLinkIPNSResult {
|
|
|
179
181
|
path: string
|
|
180
182
|
}
|
|
181
183
|
|
|
182
|
-
export interface
|
|
184
|
+
export interface DNSLinkResolveResult {
|
|
183
185
|
/**
|
|
184
186
|
* The resolved record
|
|
185
187
|
*/
|
|
186
188
|
answer: Answer
|
|
187
189
|
|
|
188
190
|
/**
|
|
189
|
-
* The
|
|
191
|
+
* The namespace
|
|
190
192
|
*/
|
|
191
193
|
namespace: string
|
|
192
194
|
}
|
|
193
195
|
|
|
194
|
-
export
|
|
195
|
-
|
|
196
|
-
export interface DNSLinkNamespace {
|
|
196
|
+
export interface DNSLinkParser<T extends DNSLinkResolveResult> {
|
|
197
197
|
/**
|
|
198
198
|
* Return a result parsed from a DNSLink value
|
|
199
199
|
*/
|
|
200
|
-
|
|
200
|
+
(value: string, answer: Answer): T
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
export interface DNSLink {
|
|
203
|
+
export interface DNSLink<DNSLinkResolveResults extends DNSLinkResolveResult = DNSLinkIPFSResult | DNSLinkIPNSResult> {
|
|
204
204
|
/**
|
|
205
205
|
* Resolve a CID from a dns-link style IPNS record
|
|
206
206
|
*
|
|
@@ -220,7 +220,7 @@ export interface DNSLink {
|
|
|
220
220
|
* console.info(result) // { answer: ..., value: ... }
|
|
221
221
|
* ```
|
|
222
222
|
*/
|
|
223
|
-
resolve(domain: string, options?: ResolveDNSLinkOptions): Promise<
|
|
223
|
+
resolve(domain: string, options?: ResolveDNSLinkOptions): Promise<Array<DNSLinkResolveResults | DNSLinkIPFSResult | DNSLinkIPNSResult>>
|
|
224
224
|
}
|
|
225
225
|
|
|
226
226
|
export interface DNSLinkComponents {
|
|
@@ -228,14 +228,39 @@ export interface DNSLinkComponents {
|
|
|
228
228
|
logger: ComponentLogger
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
export interface DNSLinkOptions {
|
|
231
|
+
export interface DNSLinkOptions<Namespaces extends Record<string, DNSLinkParser<any>> = Record<string, DNSLinkParser<any>>> {
|
|
232
232
|
/**
|
|
233
233
|
* By default `/ipfs/...`, `/ipns/...` and `/dnslink/...` record values are
|
|
234
234
|
* supported - to support other prefixes pass other value parsers here
|
|
235
235
|
*/
|
|
236
|
-
namespaces?:
|
|
236
|
+
namespaces?: Namespaces
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* How many DNSLink lookups to cache in memory
|
|
240
|
+
*
|
|
241
|
+
* @default 1_000
|
|
242
|
+
*/
|
|
243
|
+
cacheSize?: number
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The maximum number of Answers to cache for each domain
|
|
247
|
+
*
|
|
248
|
+
* @default 10
|
|
249
|
+
*/
|
|
250
|
+
cacheMaxAnswers?: number
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* By default the DNS TTL will be used to calculate when a DNSLink lookup
|
|
254
|
+
* should be evicted from the cache, unless one is not available in which case
|
|
255
|
+
* this number of ms will be used.
|
|
256
|
+
*
|
|
257
|
+
* @default 30_000
|
|
258
|
+
*/
|
|
259
|
+
cacheMaxAge?: number
|
|
237
260
|
}
|
|
238
261
|
|
|
262
|
+
export function dnsLink <R extends DNSLinkResolveResult, T extends Record<string, DNSLinkParser<R>>> (components: DNSLinkComponents, options: DNSLinkOptions<T>): DNSLink<ReturnType<T[keyof T]>>
|
|
263
|
+
export function dnsLink (components: DNSLinkComponents, options?: DNSLinkOptions): DNSLink
|
|
239
264
|
export function dnsLink (components: DNSLinkComponents, options: DNSLinkOptions = {}): DNSLink {
|
|
240
265
|
return new DNSLinkClass(components, options)
|
|
241
266
|
}
|