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
| 1 | /** |
| 2 | * Deno-backed implementation of Effect's `Dns` service. |
| 3 | * |
| 4 | * Address lookups use the operating system resolver through Deno's `node:dns` |
| 5 | * compatibility layer, so they also read the hosts file. Record queries and |
| 6 | * reverse lookups use `Deno.resolveDns` and can be cancelled by interruption. |
| 7 | * Records whose data cannot be represented, such as names that are not valid |
| 8 | * `Host.DomainName` values, are skipped, and a query fails with |
| 9 | * `InvalidResponse` when every record is skipped. |
| 10 | * |
| 11 | * **Gotchas** |
| 12 | * |
| 13 | * `Deno.resolveDns` reports every error response from the name server with the |
| 14 | * same error as a missing name, so refused queries and server failures |
| 15 | * (`REFUSED`, `SERVFAIL`, `FORMERR`, `NOTIMP`) fail with `NotFound` instead of |
| 16 | * `Refused`, `ServerFailure`, `InvalidResponse`, or `Unsupported`. Server |
| 17 | * failures are therefore not reported as temporary. `Deno.resolveDns` cannot |
| 18 | * query TLSA records, so TLSA queries fail with `Unsupported`. Queries require |
| 19 | * the `--allow-net` permission. |
| 20 | * |
| 21 | * @stability experimental |
| 22 | * @since 4.0.0 |
| 23 | */ |
| 24 | import * as NodeDns from "@effect/platform-node-shared/NodeDns" |
| 25 | import type * as Arr from "effect/Array" |
| 26 | import * as Config from "effect/Config" |
| 27 | import * as Duration from "effect/Duration" |
| 28 | import * as Effect from "effect/Effect" |
| 29 | import * as Layer from "effect/Layer" |
| 30 | import * as Dns from "effect/net/Dns" |
| 31 | import * as NetAddress from "effect/net/NetAddress" |
| 32 | import * as Result from "effect/Result" |
| 33 | |
| 34 | /** |
| 35 | * Options for the Deno `Dns` service. |
| 36 | * |
| 37 | * **Details** |
| 38 | * |
| 39 | * `nameServer` replaces the system name server for record queries and reverse |
| 40 | * lookups and is converted like `Dns.nameServerFromInput`. Address |
| 41 | * lookups always use the operating system resolver. |
| 42 | * |
| 43 | * **Gotchas** |
| 44 | * |
| 45 | * Invalid name servers fail with a `NetAddress.NetAddressError` when the |
| 46 | * service is created, and so do IPv6 name servers with a scope ID, such as |
| 47 | * link-local addresses. |
| 48 | * |
| 49 | * @stability experimental |
| 50 | * @category models |
| 51 | * @since 4.0.0 |
| 52 | */ |
| 53 | export interface Options { |
| 54 | readonly nameServer?: NetAddress.IpAddressInput | NetAddress.InetAddressInput | undefined |
| 55 | } |
| 56 | |
| 57 | const reasons: Record<string, Dns.DnsErrorReason> = { |
| 58 | NotFound: "NotFound", |
| 59 | TimedOut: "Timeout", |
| 60 | ConnectionRefused: "Refused", |
| 61 | InvalidData: "InvalidResponse", |
| 62 | NotSupported: "Unsupported" |
| 63 | } |
| 64 | |
| 65 | const toDnsError = (cause: unknown, name: string, type: Dns.RecordType): Dns.DnsError => |
| 66 | new Dns.DnsError({ |
| 67 | reason: (cause instanceof Error ? reasons[cause.name] : undefined) ?? "Unknown", |
| 68 | method: "resolve", |
| 69 | hostname: name, |
| 70 | recordType: type, |
| 71 | cause |
| 72 | }) |
| 73 | |
| 74 | const queries: { |
| 75 | readonly [K in Dns.RecordType]: ( |
| 76 | name: string, |
| 77 | options: Deno.ResolveDnsOptions |
| 78 | ) => Promise<Result.Result<Array<Dns.DnsRecord>, Dns.DnsError>> |
| 79 | } = { |
| 80 | A: async (name, options) => |
| 81 | NodeDns.recordsFromResolver(name, "A", await Deno.resolveDns(name, "A", options), (address) => ({ |
| 82 | address: NetAddress.ipFromStringUnsafe(address) as NetAddress.Ipv4Address |
| 83 | })), |
| 84 | AAAA: async (name, options) => |
| 85 | NodeDns.recordsFromResolver(name, "AAAA", await Deno.resolveDns(name, "AAAA", options), (address) => ({ |
| 86 | address: NetAddress.ipFromStringUnsafe(address) as NetAddress.Ipv6Address |
| 87 | })), |
| 88 | CAA: async (name, options) => |
| 89 | NodeDns.recordsFromResolver(name, "CAA", await Deno.resolveDns(name, "CAA", options), (caa) => ({ |
| 90 | critical: caa.critical, |
| 91 | tag: caa.tag, |
| 92 | value: caa.value |
| 93 | })), |
| 94 | CNAME: async (name, options) => |
| 95 | NodeDns.recordsFromResolver(name, "CNAME", await Deno.resolveDns(name, "CNAME", options), (target) => ({ |
| 96 | target: NodeDns.domainNameFromResolverUnsafe(target) |
| 97 | })), |
| 98 | MX: async (name, options) => |
| 99 | NodeDns.recordsFromResolver(name, "MX", await Deno.resolveDns(name, "MX", options), (mx) => ({ |
| 100 | exchange: NodeDns.domainNameFromResolverUnsafe(mx.exchange), |
| 101 | priority: mx.preference |
| 102 | })), |
| 103 | NAPTR: async (name, options) => |
| 104 | NodeDns.recordsFromResolver(name, "NAPTR", await Deno.resolveDns(name, "NAPTR", options), (naptr) => ({ |
| 105 | order: naptr.order, |
| 106 | preference: naptr.preference, |
| 107 | flags: naptr.flags, |
| 108 | service: naptr.services, |
| 109 | regexp: naptr.regexp, |
| 110 | replacement: NodeDns.domainNameFromResolverUnsafe(naptr.replacement) |
| 111 | })), |
| 112 | NS: async (name, options) => |
| 113 | NodeDns.recordsFromResolver(name, "NS", await Deno.resolveDns(name, "NS", options), (host) => ({ |
| 114 | host: NodeDns.domainNameFromResolverUnsafe(host) |
| 115 | })), |
| 116 | PTR: async (name, options) => |
| 117 | NodeDns.recordsFromResolver(name, "PTR", await Deno.resolveDns(name, "PTR", options), (host) => ({ |
| 118 | host: NodeDns.nameTextFromResolver(host, 8) |
| 119 | })), |
| 120 | SOA: async (name, options) => |
| 121 | NodeDns.recordsFromResolver(name, "SOA", await Deno.resolveDns(name, "SOA", options), (soa) => ({ |
| 122 | primary: NodeDns.domainNameFromResolverUnsafe(soa.mname), |
| 123 | admin: NodeDns.nameTextFromResolver(soa.rname, 8), |
| 124 | serial: soa.serial, |
| 125 | refresh: NodeDns.secondsFromInt32(soa.refresh), |
| 126 | retry: NodeDns.secondsFromInt32(soa.retry), |
| 127 | expire: NodeDns.secondsFromInt32(soa.expire), |
| 128 | minimum: Duration.seconds(soa.minimum) |
| 129 | })), |
| 130 | SRV: async (name, options) => |
| 131 | NodeDns.recordsFromResolver(name, "SRV", await Deno.resolveDns(name, "SRV", options), (srv) => ({ |
| 132 | target: NodeDns.domainNameFromResolverUnsafe(srv.target), |
| 133 | port: srv.port, |
| 134 | priority: srv.priority, |
| 135 | weight: srv.weight |
| 136 | })), |
| 137 | TLSA: async (name) => |
| 138 | Result.fail(new Dns.DnsError({ reason: "Unsupported", method: "resolve", hostname: name, recordType: "TLSA" })), |
| 139 | TXT: async (name, options) => |
| 140 | NodeDns.recordsFromResolver(name, "TXT", await Deno.resolveDns(name, "TXT", options), (chunks) => ({ |
| 141 | chunks: chunks.map(NodeDns.utf8FromLatin1) as unknown as Arr.NonEmptyReadonlyArray<string> |
| 142 | })) |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Creates a Deno `Dns` service. |
| 147 | * |
| 148 | * @stability experimental |
| 149 | * @category constructors |
| 150 | * @since 4.0.0 |
| 151 | */ |
| 152 | export const make = Effect.fnUntraced(function*(options?: Options) { |
| 153 | const input = options?.nameServer |
| 154 | const server = input === undefined ? undefined : yield* Effect.fromResult(Dns.nameServerFromInput(input)) |
| 155 | if (server !== undefined && NetAddress.isInetAddressV6(server) && server.scopeId !== 0) { |
| 156 | return yield* new NetAddress.NetAddressError({ |
| 157 | input: input!, |
| 158 | message: "IPv6 name servers with a scope ID are not supported" |
| 159 | }) |
| 160 | } |
| 161 | const nameServer: Deno.ResolveDnsOptions["nameServer"] = server === undefined |
| 162 | ? undefined |
| 163 | : { ipAddr: NetAddress.formatHost(server), port: server.port } |
| 164 | |
| 165 | return Dns.make({ |
| 166 | lookup: NodeDns.lookup, |
| 167 | resolve: (name, type) => |
| 168 | Effect.tryPromise({ |
| 169 | try: (signal) => queries[type](name, nameServer === undefined ? { signal } : { nameServer, signal }), |
| 170 | catch: (cause) => toDnsError(cause, name, type) |
| 171 | }).pipe(Effect.flatMap(Effect.fromResult)) |
| 172 | }) |
| 173 | }) |
| 174 | |
| 175 | /** |
| 176 | * Layer that provides the Deno `Dns` service using the system resolver |
| 177 | * configuration. |
| 178 | * |
| 179 | * @stability experimental |
| 180 | * @category layers |
| 181 | * @since 4.0.0 |
| 182 | */ |
| 183 | export const layer: Layer.Layer<Dns.Dns> = Layer.effect(Dns.Dns, Effect.orDie(make())) |
| 184 | |
| 185 | /** |
| 186 | * Creates a layer that provides the Deno `Dns` service with options read |
| 187 | * from configuration. |
| 188 | * |
| 189 | * @stability experimental |
| 190 | * @category layers |
| 191 | * @since 4.0.0 |
| 192 | */ |
| 193 | export const layerConfig = ( |
| 194 | options: Config.Wrap<Options> |
| 195 | ): Layer.Layer<Dns.Dns, Config.ConfigError | NetAddress.NetAddressError> => |
| 196 | Layer.effect(Dns.Dns, Effect.flatMap(Config.unwrap(options), make)) |