|
| 1 | +import { Effect, Layer, PlatformError } from "effect" |
| 2 | +import { HttpClient, HttpClientError, type HttpClientRequest } from "effect/unstable/http" |
| 3 | +import { current } from "./context" |
| 4 | +import type { Profile } from "./profile" |
| 5 | + |
| 6 | +const proxies = new Set([ |
| 7 | + "HTTP_PROXY", |
| 8 | + "HTTPS_PROXY", |
| 9 | + "ALL_PROXY", |
| 10 | + "NO_PROXY", |
| 11 | + "http_proxy", |
| 12 | + "https_proxy", |
| 13 | + "all_proxy", |
| 14 | + "no_proxy", |
| 15 | +]) |
| 16 | + |
| 17 | +function target(value: string) { |
| 18 | + if (!URL.canParse(value)) return value |
| 19 | + const url = new URL(value) |
| 20 | + return url.origin |
| 21 | +} |
| 22 | + |
| 23 | +function denied(value: string, method: string) { |
| 24 | + return PlatformError.systemError({ |
| 25 | + _tag: "PermissionDenied", |
| 26 | + module: "Sandbox", |
| 27 | + method, |
| 28 | + pathOrDescriptor: target(value), |
| 29 | + description: "Sandbox denied outbound network access", |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +function unsupported(value: string, method: string) { |
| 34 | + return PlatformError.systemError({ |
| 35 | + _tag: "BadResource", |
| 36 | + module: "Sandbox", |
| 37 | + method, |
| 38 | + pathOrDescriptor: target(value), |
| 39 | + description: "Sandbox proxy network mode and allowedHosts are not supported", |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +function unsupportedProfile(profile: Profile) { |
| 44 | + return profile.network.mode === "proxy" || profile.network.allowedHosts.length > 0 |
| 45 | +} |
| 46 | + |
| 47 | +export function networkEnvironment(profile: Profile, environment: Record<string, string>) { |
| 48 | + if (profile.network.mode === "allow" && profile.network.allowedHosts.length === 0) return environment |
| 49 | + return Object.fromEntries(Object.entries(environment).filter(([key]) => !proxies.has(key))) |
| 50 | +} |
| 51 | + |
| 52 | +export function assertProcessNetwork(profile: Profile, command: string) { |
| 53 | + if (!unsupportedProfile(profile)) return Effect.void |
| 54 | + return Effect.fail(unsupported(command, "prepareNetwork")) |
| 55 | +} |
| 56 | + |
| 57 | +export function assertNetwork(value: string, method = "network") { |
| 58 | + return Effect.gen(function* () { |
| 59 | + const profile = yield* current |
| 60 | + if (!profile) return |
| 61 | + if (unsupportedProfile(profile)) yield* Effect.fail(unsupported(value, method)) |
| 62 | + if (profile.network.mode === "allow") return |
| 63 | + yield* Effect.fail(denied(value, method)) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +function requestError(request: HttpClientRequest.HttpClientRequest, description: string) { |
| 68 | + return new HttpClientError.HttpClientError({ |
| 69 | + reason: new HttpClientError.TransportError({ request, description }), |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +function assertRequest(request: HttpClientRequest.HttpClientRequest) { |
| 74 | + return Effect.gen(function* () { |
| 75 | + const profile = yield* current |
| 76 | + if (!profile) return request |
| 77 | + if (profile.network.mode === "allow" && profile.network.allowedHosts.length === 0) return request |
| 78 | + const description = unsupportedProfile(profile) |
| 79 | + ? "Sandbox proxy network mode and allowedHosts are not supported" |
| 80 | + : "Sandbox denied outbound network access" |
| 81 | + return yield* Effect.fail(requestError(request, description)) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +export function decorateHttpClient(http: HttpClient.HttpClient): HttpClient.HttpClient { |
| 86 | + return HttpClient.mapRequestEffect(http, assertRequest) |
| 87 | +} |
| 88 | + |
| 89 | +export const httpLayer = Layer.effect(HttpClient.HttpClient, Effect.map(HttpClient.HttpClient, decorateHttpClient)) |
0 commit comments