-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d7d8b8
commit 40c6b80
Showing
3 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ipv4, ipv6 } from "./ip.ts"; | ||
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts"; | ||
|
||
Deno.test("ip.ts", async (t) => { | ||
await t.step(`ipv4`, async () => | ||
assertEquals(await ipv4("localhost"), ["127.0.0.1"]) | ||
); | ||
await t.step(`ipv6`, async () => | ||
assertEquals(await ipv6("localhost"), ["::1"]) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const resolveHostname = | ||
(recordType: "A" | "AAAA") => | ||
async (hostname: string): Promise<string[]> => { | ||
const aRecords = await Deno.resolveDns(hostname, recordType); | ||
if (aRecords.length > 0) return aRecords; | ||
const cname = await Deno.resolveDns(hostname, "CNAME"); | ||
const cnameIps = ( | ||
await Promise.all( | ||
cname.map((cname) => { | ||
try { | ||
return resolveHostname(recordType)(cname); | ||
} catch { | ||
return null; | ||
} | ||
}) | ||
) | ||
) | ||
.flat() | ||
.filter((ip): ip is string => ip !== null); | ||
|
||
if (cnameIps.length > 0) return cnameIps; | ||
throw new Error(`Could not determine ${recordType} for ${hostname}!`); | ||
}; | ||
|
||
export const ipv4 = resolveHostname("A"); | ||
export const ipv6 = resolveHostname("AAAA"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters