@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 82 lines 2.64 KB
Raw
1 /**
2 * Bun implementation of Effect's `Dns` service.
3 *
4 * Address lookups use `Bun.dns.lookup` with the `system` backend, which calls
5 * the operating system resolver (`getaddrinfo`) and therefore also reads the
6 * hosts file. Bun's `node:dns` lookup uses c-ares instead, which bypasses the
7 * system's name service configuration. Record queries and reverse lookups use
8 * `NodeDns.resolver` without the UTF-8 correction of `NodeDns.make`,
9 * because Bun already decodes TXT and CAA character strings as UTF-8.
10 *
11 * **Gotchas**
12 *
13 * Bun returns each character string of a TXT record as a separate record, so
14 * a record made of several strings arrives as several TXT records whose chunks
15 * cannot be reassembled (https://github.com/oven-sh/bun/issues/44692).
16 *
17 * Bun's resolver cannot query TLSA records, so TLSA queries fail with
18 * `Unsupported`.
19 *
20 * @stability experimental
21 * @since 4.0.0
22 */
23 import * as NodeDns from "@effect/platform-node-shared/NodeDns"
24 import * as Config from "effect/Config"
25 import * as Effect from "effect/Effect"
26 import * as Layer from "effect/Layer"
27 import * as Dns from "effect/net/Dns"
28 import type * as NetAddress from "effect/net/NetAddress"
29
30 export type {
31 /**
32 * Options for the Bun `Dns` service: name servers, timeout, and tries for
33 * record queries and reverse lookups.
34 *
35 * @stability experimental
36 * @category re-exports
37 * @since 4.0.0
38 */
39 Options
40 } from "@effect/platform-node-shared/NodeDns"
41
42 /**
43 * Creates a Bun `Dns` service whose resolver lives as long as the scope.
44 *
45 * @stability experimental
46 * @category constructors
47 * @since 4.0.0
48 */
49 export const make = Effect.fnUntraced(function*(options?: NodeDns.Options) {
50 const resolve = yield* NodeDns.resolver(options)
51 return Dns.make({
52 lookup: (host, family) =>
53 Effect.tryPromise({
54 try: () => Bun.dns.lookup(host, { family: NodeDns.toFamily(family), backend: "system" }),
55 catch: (cause) => NodeDns.dnsErrorFromCause(cause, "lookup", host)
56 }).pipe(Effect.map(NodeDns.addressesFromLookup)),
57 resolve
58 })
59 })
60
61 /**
62 * Layer that provides the Bun `Dns` service using the system resolver
63 * configuration.
64 *
65 * @stability experimental
66 * @category layers
67 * @since 4.0.0
68 */
69 export const layer: Layer.Layer<Dns.Dns> = Layer.effect(Dns.Dns, Effect.orDie(make()))
70
71 /**
72 * Creates a layer that provides the Bun `Dns` service with options read
73 * from configuration.
74 *
75 * @stability experimental
76 * @category layers
77 * @since 4.0.0
78 */
79 export const layerConfig = (
80 options: Config.Wrap<NodeDns.Options>
81 ): Layer.Layer<Dns.Dns, Config.ConfigError | NetAddress.NetAddressError> =>
82 Layer.effect(Dns.Dns, Effect.flatMap(Config.unwrap(options), make))