From e9a1b8e83afe336d9a0cfdb085e2b5496d71feed Mon Sep 17 00:00:00 2001 From: siaeyy Date: Wed, 15 Jan 2025 05:26:21 +0300 Subject: [PATCH] feat(node/dns): Tts for A and AAAA record queries --- .../polyfills/internal_binding/cares_wrap.ts | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/ext/node/polyfills/internal_binding/cares_wrap.ts b/ext/node/polyfills/internal_binding/cares_wrap.ts index 6a864e5855..41f239d00c 100644 --- a/ext/node/polyfills/internal_binding/cares_wrap.ts +++ b/ext/node/polyfills/internal_binding/cares_wrap.ts @@ -186,20 +186,22 @@ export class ChannelWrap extends AsyncWrap implements ChannelWrapQuery { this.#tries = tries; } - async #query(query: string, recordType: Deno.RecordType) { - // TODO(@bartlomieju): TTL logic. - + async #query(query: string, recordType: Deno.RecordType, ttl?: boolean) { let code: number; let ret: Awaited>; + const resolveOptions: Deno.ResolveDnsOptions = { + ...ttl !== undefined ? { ttl } : {}, + }; + if (this.#servers.length) { for (const [ipAddr, port] of this.#servers) { - const resolveOptions = { + Object.assign(resolveOptions, { nameServer: { ipAddr, port, }, - }; + }); ({ code, ret } = await this.#resolve( query, @@ -212,7 +214,7 @@ export class ChannelWrap extends AsyncWrap implements ChannelWrapQuery { } } } else { - ({ code, ret } = await this.#resolve(query, recordType)); + ({ code, ret } = await this.#resolve(query, recordType, resolveOptions)); } return { code: code!, ret: ret! }; @@ -351,18 +353,34 @@ export class ChannelWrap extends AsyncWrap implements ChannelWrapQuery { } queryA(req: QueryReqWrap, name: string): number { - this.#query(name, "A").then(({ code, ret }) => { - req.oncomplete(code, ret); + this.#query(name, "A", req.ttl).then(({ code, ret }) => { + let recordsWithTtl; + if (req.ttl) { + recordsWithTtl = (ret as Deno.RecordWithTtl[]).map((val) => ({ + address: val?.data, + ttl: val?.ttl, + })); + } + + req.oncomplete(code, recordsWithTtl ?? ret); }); return 0; } queryAaaa(req: QueryReqWrap, name: string): number { - this.#query(name, "AAAA").then(({ code, ret }) => { - const records = (ret as string[]).map((record) => compressIPv6(record)); + this.#query(name, "AAAA", req.ttl).then(({ code, ret }) => { + let recordsWithTtl; + if (req.ttl) { + recordsWithTtl = (ret as Deno.RecordWithTtl[]).map((val) => ({ + address: compressIPv6(val?.data as string), + ttl: val?.ttl, + })); + } else { + ret = (ret as string[]).map((record) => compressIPv6(record)); + } - req.oncomplete(code, records); + req.oncomplete(code, recordsWithTtl ?? ret); }); return 0;