The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 6 | import type { Providers } from "../Providers.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Configuration for the singleton WARP default device profile. |
| 10 | * |
| 11 | * Every Cloudflare Zero Trust account has exactly one default device |
| 12 | * profile that applies to all WARP devices not matched by a custom |
| 13 | * profile. This shape mirrors the fields supported by |
| 14 | * `PATCH /accounts/{accountId}/devices/policy` plus the three list |
| 15 | * endpoints for the split-tunnel include/exclude and fallback domains. |
| 16 | */ |
| 17 | export interface DeviceDefaultProfileProps { |
| 18 | /** |
| 19 | * Split-tunnel mode. `"include"` routes only listed CIDRs/hostnames |
| 20 | * through WARP; `"exclude"` routes everything except listed entries. |
| 21 | * |
| 22 | * The Cloudflare backend stores the include and exclude lists |
| 23 | * independently — only the list matching the active mode is enforced |
| 24 | * at runtime. We always push both lists when supplied so toggling |
| 25 | * `mode` is non-destructive. |
| 26 | * |
| 27 | * @default "exclude" |
| 28 | */ |
| 29 | mode?: "include" | "exclude"; |
| 30 | /** |
| 31 | * Routes that WARP will tunnel. Only effective when {@link mode} is |
| 32 | * `"include"`. Pushed to `PUT /devices/policy/include`. |
| 33 | */ |
| 34 | splitTunnelInclude?: DeviceDefaultProfile.SplitTunnelEntry[]; |
| 35 | /** |
| 36 | * Routes that WARP will bypass. Only effective when {@link mode} is |
| 37 | * `"exclude"`. Pushed to `PUT /devices/policy/exclude`. |
| 38 | */ |
| 39 | splitTunnelExclude?: DeviceDefaultProfile.SplitTunnelEntry[]; |
| 40 | /** |
| 41 | * Local-fallback DNS suffixes. For each suffix, WARP will resolve |
| 42 | * matching names via the given DNS servers (or the system resolver |
| 43 | * when omitted and {@link disableAutoFallback} is `false`). |
| 44 | */ |
| 45 | fallbackDomains?: DeviceDefaultProfile.FallbackDomain[]; |
| 46 | /** |
| 47 | * Seconds to wait before activating the captive-portal bypass. A |
| 48 | * value of `0` disables the bypass. |
| 49 | */ |
| 50 | captivePortal?: number; |
| 51 | /** |
| 52 | * Seconds to wait before WARP reconnects after the user disables it. |
| 53 | * `0` keeps WARP disabled until the user re-enables it. |
| 54 | */ |
| 55 | autoConnect?: number; |
| 56 | /** |
| 57 | * Whether the user is allowed to remove the device from the team. |
| 58 | */ |
| 59 | allowedToLeave?: boolean; |
| 60 | /** |
| 61 | * Whether the user is allowed to switch WARP between Gateway-with-WARP |
| 62 | * and other modes. |
| 63 | */ |
| 64 | allowModeSwitch?: boolean; |
| 65 | /** |
| 66 | * Whether WARP should surface available client updates to the user. |
| 67 | */ |
| 68 | allowUpdates?: boolean; |
| 69 | /** |
| 70 | * When `true`, fallback domains without an explicit `dnsServer` are |
| 71 | * NOT resolved via the system resolver — WARP refuses to resolve them. |
| 72 | */ |
| 73 | disableAutoFallback?: boolean; |
| 74 | /** |
| 75 | * When `true`, Microsoft 365 service IP ranges are added to the |
| 76 | * split-tunnel exclude list automatically. |
| 77 | */ |
| 78 | excludeOfficeIps?: boolean; |
| 79 | /** |
| 80 | * When `true`, the user cannot turn the WARP switch off. |
| 81 | */ |
| 82 | switchLocked?: boolean; |
| 83 | /** |
| 84 | * Selects WARP's tunneling behavior: |
| 85 | * - `"warp"` — full WireGuard tunnel |
| 86 | * - `"proxy"` — local SOCKS proxy on the chosen `port` |
| 87 | */ |
| 88 | serviceModeV2?: DeviceDefaultProfile.ServiceModeV2; |
| 89 | /** |
| 90 | * Minutes of LAN access permitted after a WARP reconnect. `0` means |
| 91 | * unrestricted until the next WARP cycle. |
| 92 | */ |
| 93 | lanAllowMinutes?: number; |
| 94 | /** |
| 95 | * Prefix length of the LAN subnet that {@link lanAllowMinutes} |
| 96 | * applies to (e.g. `24` for a `/24`). |
| 97 | */ |
| 98 | lanAllowSubnetSize?: number; |
| 99 | /** |
| 100 | * When `true`, the OS registers WARP's local interface IP with the |
| 101 | * on-premises DNS server. |
| 102 | */ |
| 103 | registerInterfaceIpWithDns?: boolean; |
| 104 | /** |
| 105 | * When `true`, WARP signals SCCM that the device is inside a VPN |
| 106 | * boundary. Windows only. |
| 107 | */ |
| 108 | sccmVpnBoundarySupport?: boolean; |
| 109 | /** |
| 110 | * URL launched when the user clicks "Send Feedback" in the client. |
| 111 | */ |
| 112 | supportUrl?: string; |
| 113 | /** |
| 114 | * Which underlying tunnel protocol WARP should use (e.g. `"wireguard"`, |
| 115 | * `"masque"`). |
| 116 | */ |
| 117 | tunnelProtocol?: string; |
| 118 | } |
| 119 | |
| 120 | export declare namespace DeviceDefaultProfile { |
| 121 | /** |
| 122 | * One entry in either the split-tunnel include or exclude list. |
| 123 | * Either an `address` (CIDR) or a `host` (DNS name) is required. |
| 124 | */ |
| 125 | export interface SplitTunnelEntry { |
| 126 | /** CIDR-form route, e.g. `"10.0.0.0/8"` or `"1.2.3.4/32"`. */ |
| 127 | address: string; |
| 128 | /** Hostname-form route, e.g. `"example.com"`. */ |
| 129 | host?: string; |
| 130 | /** Free-form description shown in the dashboard. */ |
| 131 | description?: string; |
| 132 | } |
| 133 | /** |
| 134 | * Local-fallback DNS configuration for a given DNS suffix. |
| 135 | */ |
| 136 | export interface FallbackDomain { |
| 137 | /** DNS suffix, e.g. `"corp.example.com"`. */ |
| 138 | suffix: string; |
| 139 | /** Optional free-form description. */ |
| 140 | description?: string; |
| 141 | /** DNS resolvers (IPv4/IPv6) used for names matching `suffix`. */ |
| 142 | dnsServer?: string[]; |
| 143 | } |
| 144 | /** |
| 145 | * Tunneling-mode configuration. |
| 146 | */ |
| 147 | export interface ServiceModeV2 { |
| 148 | /** `"warp"` for the full WireGuard tunnel, `"proxy"` for SOCKS. */ |
| 149 | mode: "warp" | "proxy"; |
| 150 | /** Local SOCKS port when `mode === "proxy"`. Ignored otherwise. */ |
| 151 | port?: number; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | export type DeviceDefaultProfile = Resource< |
| 156 | "Cloudflare.Devices.DefaultProfile", |
| 157 | DeviceDefaultProfileProps, |
| 158 | { |
| 159 | /** Account that owns the default profile. */ |
| 160 | accountId: string; |
| 161 | /** Observed split-tunnel mode. */ |
| 162 | mode: "include" | "exclude"; |
| 163 | /** Observed include list. */ |
| 164 | splitTunnelInclude: DeviceDefaultProfile.SplitTunnelEntry[]; |
| 165 | /** Observed exclude list. */ |
| 166 | splitTunnelExclude: DeviceDefaultProfile.SplitTunnelEntry[]; |
| 167 | /** Observed fallback domains. */ |
| 168 | fallbackDomains: DeviceDefaultProfile.FallbackDomain[]; |
| 169 | /** Observed captive-portal timeout (seconds). */ |
| 170 | captivePortal: number | undefined; |
| 171 | /** Observed auto-connect timeout (seconds). */ |
| 172 | autoConnect: number | undefined; |
| 173 | /** Whether devices are allowed to leave the organization. */ |
| 174 | allowedToLeave: boolean | undefined; |
| 175 | /** Whether the user may switch WARP modes. */ |
| 176 | allowModeSwitch: boolean | undefined; |
| 177 | /** Whether update notifications are shown. */ |
| 178 | allowUpdates: boolean | undefined; |
| 179 | /** Whether auto-fallback to system DNS is disabled. */ |
| 180 | disableAutoFallback: boolean | undefined; |
| 181 | /** Whether Microsoft IPs are auto-excluded. */ |
| 182 | excludeOfficeIps: boolean | undefined; |
| 183 | /** Whether the WARP switch is locked on. */ |
| 184 | switchLocked: boolean | undefined; |
| 185 | /** Tunneling mode configuration. */ |
| 186 | serviceModeV2: DeviceDefaultProfile.ServiceModeV2 | undefined; |
| 187 | /** LAN-access allowance (minutes). */ |
| 188 | lanAllowMinutes: number | undefined; |
| 189 | /** LAN subnet prefix length. */ |
| 190 | lanAllowSubnetSize: number | undefined; |
| 191 | /** Whether the WARP IP is registered with the on-prem DNS server. */ |
| 192 | registerInterfaceIpWithDns: boolean | undefined; |
| 193 | /** Whether SCCM VPN-boundary support is enabled. */ |
| 194 | sccmVpnBoundarySupport: boolean | undefined; |
| 195 | /** Send-Feedback URL. */ |
| 196 | supportUrl: string | undefined; |
| 197 | /** Tunnel protocol selection. */ |
| 198 | tunnelProtocol: string | undefined; |
| 199 | }, |
| 200 | never, |
| 201 | Providers |
| 202 | >; |
| 203 | |
| 204 | /** |
| 205 | * Manages the **singleton** Cloudflare WARP **default device profile** for |
| 206 | * an account. The default profile applies to every WARP device not |
| 207 | * matched by a custom profile. |
| 208 | * @remarks |
| 209 | * There is exactly one default profile per account; it cannot be created |
| 210 | * or deleted. Reconciling this resource patches the existing profile in |
| 211 | * place and synchronizes the four sibling list endpoints (include, |
| 212 | * exclude, fallback domains). The `delete` lifecycle is a deliberate |
| 213 | * no-op — destroying the Alchemy resource only removes our local state, |
| 214 | * the cloud profile remains intact. |
| 215 | * |
| 216 | * Custom (non-default) profiles are a separate resource. |
| 217 | * |
| 218 | * ### Configuring split tunneling |
| 219 | * **Example:** Exclude-mode (default): tunnel everything except listed routes |
| 220 | * ```typescript |
| 221 | * yield* Cloudflare.Devices.DeviceDefaultProfile("Default", { |
| 222 | * mode: "exclude", |
| 223 | * splitTunnelExclude: [ |
| 224 | * { address: "10.0.0.0/8", description: "RFC1918" }, |
| 225 | * { address: "192.168.0.0/16", description: "RFC1918" }, |
| 226 | * ], |
| 227 | * excludeOfficeIps: true, |
| 228 | * }); |
| 229 | * ``` |
| 230 | * |
| 231 | * **Example:** Include-mode: only listed routes go through WARP |
| 232 | * ```typescript |
| 233 | * yield* Cloudflare.Devices.DeviceDefaultProfile("Default", { |
| 234 | * mode: "include", |
| 235 | * splitTunnelInclude: [ |
| 236 | * { address: "10.42.0.0/16", description: "Prod VPC" }, |
| 237 | * ], |
| 238 | * }); |
| 239 | * ``` |
| 240 | * |
| 241 | * ### Configuring fallback domains |
| 242 | * **Example:** Resolve a private suffix via an on-prem DNS server |
| 243 | * ```typescript |
| 244 | * yield* Cloudflare.Devices.DeviceDefaultProfile("Default", { |
| 245 | * fallbackDomains: [ |
| 246 | * { |
| 247 | * suffix: "corp.example.com", |
| 248 | * dnsServer: ["10.0.0.53"], |
| 249 | * description: "Corp AD", |
| 250 | * }, |
| 251 | * ], |
| 252 | * disableAutoFallback: true, |
| 253 | * }); |
| 254 | * ``` |
| 255 | * |
| 256 | * @resource |
| 257 | * @product Devices |
| 258 | * @category Cloudflare One (Zero Trust) |
| 259 | */ |
| 260 | export const DeviceDefaultProfile = Resource<DeviceDefaultProfile>( |
| 261 | "Cloudflare.Devices.DefaultProfile", |
| 262 | ); |
| 263 | |
| 264 | /** |
| 265 | * Live `Provider` for {@link DeviceDefaultProfile}. Wire into a Cloudflare |
| 266 | * provider Layer with `Provider.collection([DeviceDefaultProfile])` plus |
| 267 | * `DeviceDefaultProfileProvider()`. |
| 268 | */ |
| 269 | export const DeviceDefaultProfileProvider = () => |
| 270 | Provider.succeed(DeviceDefaultProfile, { |
| 271 | nuke: { singleton: true }, |
| 272 | stables: ["accountId"], |
| 273 | reconcile: Effect.fn(function* ({ news = {} }) { |
| 274 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 275 | // 1. Observe — the default profile always exists. |
| 276 | let obs = yield* observe(); |
| 277 | |
| 278 | // 2. Sync — four independent idempotent diffs. |
| 279 | |
| 280 | // (a) General profile patch. |
| 281 | const desiredMode = news.mode ?? inferMode(obs.profile); |
| 282 | const observedSm = obs.profile.serviceModeV2; |
| 283 | const desiredSm = news.serviceModeV2; |
| 284 | const patchBody: zeroTrust.PatchDevicePolicyDefaultRequest = { |
| 285 | accountId, |
| 286 | }; |
| 287 | let needsPatch = false; |
| 288 | const setIf = <K extends keyof zeroTrust.PatchDevicePolicyDefaultRequest>( |
| 289 | key: K, |
| 290 | desired: zeroTrust.PatchDevicePolicyDefaultRequest[K], |
| 291 | observed: unknown, |
| 292 | ) => { |
| 293 | if (desired === undefined) return; |
| 294 | if (desired !== observed) { |
| 295 | patchBody[key] = desired; |
| 296 | needsPatch = true; |
| 297 | } |
| 298 | }; |
| 299 | setIf("captivePortal", news.captivePortal, denull(obs.profile.captivePortal)); |
| 300 | setIf("autoConnect", news.autoConnect, denull(obs.profile.autoConnect)); |
| 301 | setIf("allowedToLeave", news.allowedToLeave, denull(obs.profile.allowedToLeave)); |
| 302 | setIf("allowModeSwitch", news.allowModeSwitch, denull(obs.profile.allowModeSwitch)); |
| 303 | setIf("allowUpdates", news.allowUpdates, denull(obs.profile.allowUpdates)); |
| 304 | setIf( |
| 305 | "disableAutoFallback", |
| 306 | news.disableAutoFallback, |
| 307 | denull(obs.profile.disableAutoFallback), |
| 308 | ); |
| 309 | setIf("excludeOfficeIps", news.excludeOfficeIps, denull(obs.profile.excludeOfficeIps)); |
| 310 | setIf("switchLocked", news.switchLocked, denull(obs.profile.switchLocked)); |
| 311 | // lanAllowMinutes / lanAllowSubnetSize are accepted by PATCH but |
| 312 | // not surfaced on GET, so we cannot diff them. Push them every |
| 313 | // time the user sets them — the API is idempotent. |
| 314 | if (news.lanAllowMinutes !== undefined) { |
| 315 | patchBody.lanAllowMinutes = news.lanAllowMinutes; |
| 316 | needsPatch = true; |
| 317 | } |
| 318 | if (news.lanAllowSubnetSize !== undefined) { |
| 319 | patchBody.lanAllowSubnetSize = news.lanAllowSubnetSize; |
| 320 | needsPatch = true; |
| 321 | } |
| 322 | setIf( |
| 323 | "registerInterfaceIpWithDns", |
| 324 | news.registerInterfaceIpWithDns, |
| 325 | denull(obs.profile.registerInterfaceIpWithDns), |
| 326 | ); |
| 327 | setIf( |
| 328 | "sccmVpnBoundarySupport", |
| 329 | news.sccmVpnBoundarySupport, |
| 330 | denull(obs.profile.sccmVpnBoundarySupport), |
| 331 | ); |
| 332 | setIf("supportUrl", news.supportUrl, denull(obs.profile.supportUrl)); |
| 333 | setIf("tunnelProtocol", news.tunnelProtocol, denull(obs.profile.tunnelProtocol)); |
| 334 | |
| 335 | if ( |
| 336 | desiredSm !== undefined && |
| 337 | !sameJSON( |
| 338 | desiredSm, |
| 339 | observedSm |
| 340 | ? { |
| 341 | mode: denull(observedSm.mode), |
| 342 | port: denull(observedSm.port), |
| 343 | } |
| 344 | : undefined, |
| 345 | ) |
| 346 | ) { |
| 347 | patchBody.serviceModeV2 = desiredSm; |
| 348 | needsPatch = true; |
| 349 | } |
| 350 | |
| 351 | // `mode` is a derived view of which list is enforced; the CF |
| 352 | // PATCH endpoint surfaces it as a SIDE-EFFECT of which array |
| 353 | // (`include`/`exclude`) is non-empty. We let the include/exclude |
| 354 | // sync blocks below handle persistence. |
| 355 | void desiredMode; |
| 356 | |
| 357 | if (needsPatch) { |
| 358 | yield* zeroTrust.patchDevicePolicyDefault(patchBody); |
| 359 | } |
| 360 | |
| 361 | // (b) Include list. |
| 362 | if (news.splitTunnelInclude !== undefined) { |
| 363 | const desired = news.splitTunnelInclude; |
| 364 | if (!sameJSON(desired, obs.splitTunnelInclude)) { |
| 365 | yield* zeroTrust.putDevicePolicyDefaultInclude({ |
| 366 | accountId, |
| 367 | body: desired.map(encodeSplit), |
| 368 | }); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // (c) Exclude list. |
| 373 | if (news.splitTunnelExclude !== undefined) { |
| 374 | const desired = news.splitTunnelExclude; |
| 375 | if (!sameJSON(desired, obs.splitTunnelExclude)) { |
| 376 | yield* zeroTrust.putDevicePolicyDefaultExclude({ |
| 377 | accountId, |
| 378 | body: desired.map(encodeSplit), |
| 379 | }); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // (d) Fallback domains. |
| 384 | if (news.fallbackDomains !== undefined) { |
| 385 | const desired = news.fallbackDomains; |
| 386 | if (!sameJSON(desired, obs.fallbackDomains)) { |
| 387 | yield* zeroTrust.putDevicePolicyDefaultFallbackDomain({ |
| 388 | accountId, |
| 389 | domains: desired.map(encodeFallback), |
| 390 | }); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // 3. Re-observe so attrs reflect post-sync truth. |
| 395 | obs = yield* observe(); |
| 396 | return buildAttrs(accountId, obs); |
| 397 | }), |
| 398 | delete: Effect.fn(function* () { |
| 399 | // The default device profile cannot be deleted. Drop our state |
| 400 | // and leave the cloud profile in place. |
| 401 | yield* Effect.logWarning( |
| 402 | "Cloudflare.Devices.DefaultProfile: delete is a no-op; the default device profile cannot be deleted.", |
| 403 | ); |
| 404 | return Effect.void; |
| 405 | }), |
| 406 | read: Effect.fn(function* () { |
| 407 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 408 | const obs = yield* observe(); |
| 409 | return buildAttrs(accountId, obs); |
| 410 | }), |
| 411 | // Account-scoped singleton: there is exactly one default device profile |
| 412 | // per account and no enumeration API. Mirror `read` and return the single |
| 413 | // profile as a one-element array. |
| 414 | list: Effect.fn(function* () { |
| 415 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 416 | const obs = yield* observe(); |
| 417 | return [buildAttrs(accountId, obs)]; |
| 418 | }), |
| 419 | }); |
| 420 | |
| 421 | // Cloudflare returns `{result: null, success: true}` (NOT `[]`) when |
| 422 | // a default-policy list endpoint is empty, and distilled's schema |
| 423 | // rejects that as a transport error. Swallow into `undefined` so the |
| 424 | // reconciler treats an empty list as "no entries" rather than failing. |
| 425 | const listOrEmpty = <A, Err, Req>(op: Effect.Effect<{ result?: readonly A[] | null }, Err, Req>) => |
| 426 | op.pipe( |
| 427 | Effect.orElseSucceed(() => ({ |
| 428 | result: [] as readonly A[], |
| 429 | })), |
| 430 | ); |
| 431 | |
| 432 | const observe = Effect.fn(function* () { |
| 433 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 434 | const [profile, include, exclude, fallback] = yield* Effect.all( |
| 435 | [ |
| 436 | zeroTrust.getDevicePolicyDefault({ accountId }), |
| 437 | listOrEmpty(zeroTrust.getDevicePolicyDefaultInclude({ accountId })), |
| 438 | listOrEmpty(zeroTrust.getDevicePolicyDefaultExclude({ accountId })), |
| 439 | listOrEmpty(zeroTrust.getDevicePolicyDefaultFallbackDomain({ accountId })), |
| 440 | ], |
| 441 | { concurrency: "unbounded" }, |
| 442 | ); |
| 443 | const inc = (include.result ?? []).map(normalizeSplit); |
| 444 | const exc = (exclude.result ?? []).map(normalizeSplit); |
| 445 | const fb = (fallback.result ?? []).map(normalizeFallback); |
| 446 | return { |
| 447 | profile, |
| 448 | splitTunnelInclude: inc, |
| 449 | splitTunnelExclude: exc, |
| 450 | fallbackDomains: fb, |
| 451 | }; |
| 452 | }); |
| 453 | |
| 454 | const buildAttrs = ( |
| 455 | accountId: string, |
| 456 | obs: Effect.Success<ReturnType<typeof observe>>, |
| 457 | ): DeviceDefaultProfile["Attributes"] => { |
| 458 | const { profile } = obs; |
| 459 | const sm = profile.serviceModeV2; |
| 460 | const serviceModeV2: DeviceDefaultProfile.ServiceModeV2 | undefined = |
| 461 | sm && sm.mode != null |
| 462 | ? { |
| 463 | mode: (sm.mode === "proxy" ? "proxy" : "warp") as "warp" | "proxy", |
| 464 | port: denull(sm.port), |
| 465 | } |
| 466 | : undefined; |
| 467 | return { |
| 468 | accountId, |
| 469 | mode: inferMode(profile), |
| 470 | splitTunnelInclude: obs.splitTunnelInclude, |
| 471 | splitTunnelExclude: obs.splitTunnelExclude, |
| 472 | fallbackDomains: obs.fallbackDomains, |
| 473 | captivePortal: denull(profile.captivePortal), |
| 474 | autoConnect: denull(profile.autoConnect), |
| 475 | allowedToLeave: denull(profile.allowedToLeave), |
| 476 | allowModeSwitch: denull(profile.allowModeSwitch), |
| 477 | allowUpdates: denull(profile.allowUpdates), |
| 478 | disableAutoFallback: denull(profile.disableAutoFallback), |
| 479 | excludeOfficeIps: denull(profile.excludeOfficeIps), |
| 480 | switchLocked: denull(profile.switchLocked), |
| 481 | serviceModeV2, |
| 482 | // lanAllow* are PATCH-only — GET does not echo them back. |
| 483 | lanAllowMinutes: undefined, |
| 484 | lanAllowSubnetSize: undefined, |
| 485 | registerInterfaceIpWithDns: denull(profile.registerInterfaceIpWithDns), |
| 486 | sccmVpnBoundarySupport: denull(profile.sccmVpnBoundarySupport), |
| 487 | supportUrl: denull(profile.supportUrl), |
| 488 | tunnelProtocol: denull(profile.tunnelProtocol), |
| 489 | }; |
| 490 | }; |
| 491 | |
| 492 | /** |
| 493 | * Strip Cloudflare's `null` echoes to `undefined` so structural equality |
| 494 | * (`JSON.stringify`) works. |
| 495 | */ |
| 496 | const denull = <T>(v: T | null | undefined): T | undefined => (v == null ? undefined : v); |
| 497 | |
| 498 | const normalizeSplit = ( |
| 499 | entry: |
| 500 | | { address: string; description?: string | null } |
| 501 | | { host: string; description?: string | null }, |
| 502 | ): DeviceDefaultProfile.SplitTunnelEntry => ({ |
| 503 | address: "address" in entry ? entry.address : "", |
| 504 | host: "host" in entry ? entry.host : undefined, |
| 505 | description: denull(entry.description), |
| 506 | }); |
| 507 | |
| 508 | const normalizeFallback = (entry: { |
| 509 | suffix: string; |
| 510 | description?: string | null; |
| 511 | dnsServer?: string[] | null; |
| 512 | }): DeviceDefaultProfile.FallbackDomain => ({ |
| 513 | suffix: entry.suffix, |
| 514 | description: denull(entry.description), |
| 515 | dnsServer: denull(entry.dnsServer), |
| 516 | }); |
| 517 | |
| 518 | /** |
| 519 | * Encode a SplitTunnelEntry for the API's union shape (`address` OR `host`). |
| 520 | * Prefers `host` when set since the address would otherwise be empty. |
| 521 | */ |
| 522 | const encodeSplit = ( |
| 523 | e: DeviceDefaultProfile.SplitTunnelEntry, |
| 524 | ): { address: string; description?: string } | { host: string; description?: string } => { |
| 525 | if (e.host && !e.address) { |
| 526 | return e.description !== undefined |
| 527 | ? { host: e.host, description: e.description } |
| 528 | : { host: e.host }; |
| 529 | } |
| 530 | return e.description !== undefined |
| 531 | ? { address: e.address, description: e.description } |
| 532 | : { address: e.address }; |
| 533 | }; |
| 534 | |
| 535 | const encodeFallback = ( |
| 536 | d: DeviceDefaultProfile.FallbackDomain, |
| 537 | ): { suffix: string; description?: string; dnsServer?: string[] } => { |
| 538 | const out: { suffix: string; description?: string; dnsServer?: string[] } = { |
| 539 | suffix: d.suffix, |
| 540 | }; |
| 541 | if (d.description !== undefined) out.description = d.description; |
| 542 | if (d.dnsServer !== undefined) out.dnsServer = d.dnsServer; |
| 543 | return out; |
| 544 | }; |
| 545 | |
| 546 | /** Structural deep-equality via canonical JSON. */ |
| 547 | const sameJSON = (a: unknown, b: unknown): boolean => JSON.stringify(a) === JSON.stringify(b); |
| 548 | |
| 549 | const inferMode = (observed: { |
| 550 | include?: readonly unknown[] | null | undefined; |
| 551 | }): "include" | "exclude" => |
| 552 | observed.include && observed.include.length > 0 ? "include" : "exclude"; |