Skip to content

Proxied HTTP client

ProxiedHttpClient hands out an HTTP client for a proxy route, so the Scraper decides where its requests leave from and the provider decides how.

ProxiedHttpClient is the Capability for sites that must be reached from a particular place.

It splits one decision in two:

  • The Scraper states the proxy route: where its requests must leave from. That is a fact about the site.
  • The provider decides how to satisfy the route: which proxy vendor, which credentials, which session.

The port ​

ts
type Code = typeof CountryCode.Type;

export interface ProxyRoute<Country extends Code = Code> {
  readonly country?: Country;
  readonly session?: "run" | "shared";
}

export class ProxiedHttpClient<out Covered extends Code = Code> extends Context.Service<
  ProxiedHttpClient,
  { readonly route: RouteTo<Code> }
>()(key) {
  declare readonly countries: Covered;
}

route takes a ProxyRoute and returns an ordinary HttpClient. A route has an optional country, such as { country: "BR" }. An empty route, {}, asks for a proxy without a country. session says whether the route's address belongs to this Run ("run", the default) or is shared by every Run ("shared"); Apify Proxy describes how a provider applies it.

The returned client needs a Scope. Whatever the provider holds for it is released when that scope closes.

Countries are part of the type ​

CountryCode is a Schema of the ISO 3166-1 alpha-2 codes, so { country: "XX" } does not compile. ProxiedHttpClient<Covered> names the countries a requirement needs or a provider covers:

  • ProxiedHttpClient.route({ country: "BR" }) requires ProxiedHttpClient<"BR">. An Implementation that routes to Brazil and the United States requires ProxiedHttpClient<"BR"> | ProxiedHttpClient<"US">.
  • ProxiedHttpClient.layer<"BR" | "US">(route) provides ProxiedHttpClient<"BR" | "US">, and its route function receives only those countries.
  • A plain ProxiedHttpClient covers every country. apifyProxy is one.

A provider satisfies a requirement only when it covers the country:

ts
const brazilAndUs = Effect.scoped(
  Effect.gen(function* brazilAndUs() {
    yield* ProxiedHttpClient.route({ country: "BR" });
    yield* ProxiedHttpClient.route({ country: "US" });
  })
);

const onlyUs = ProxiedHttpClient.layer<"US">(() => Effect.succeed(upstream));

const uncovered = brazilAndUs.pipe(Effect.provide(onlyUs));
// Effect<void, never, ProxiedHttpClient<"BR">>

A Release that provides a provider without Brazil to a Scraper that routes to Brazil fails to compile, in the same way as a Release that provides no proxy at all.

Two ways to use it ​

Route the whole Implementation ​

When an Implementation already works with an HttpClient, ProxiedHttpClient.layerHttpClient(route) provides one from a route. The Implementation's requirement changes from HttpClient to ProxiedHttpClient<"US">, and its code does not change.

ts
export const QuotesProxiedLive = QuotesApiLive.pipe(
  Layer.provide(ProxiedHttpClient.layerHttpClient({ country: "US" }))
);

Adding a proxy walks through this.

Ask for a route inside the Implementation ​

When the Scraper needs a client per Run, for example with its own cookies, call ProxiedHttpClient.route where the Run starts.

ts
const client = (yield* ProxiedHttpClient.route({ country: "BR" })).pipe(
  HttpClient.withCookiesRef(cookies)
);

The Implementation then requires ProxiedHttpClient<"BR">, and the client and its session belong to the Run's Scope.

Providers ​

ProviderWhere it runsRoute support
apifyProxy from @scwrap/platform-apifyA Cloudflare Worker, such as a Worker releaseEvery country, as ProxiedHttpClient, for http: only (Known gaps).
apifyProxy from @scwrap/platform-apify/nodeNodeEvery country, as ProxiedHttpClient.
A fake in a testAnywhereWhatever the test checks.

Write a fake for tests ​

A provider is a Layer with one function. A test's provider names the countries it covers, records the routes it was asked for and returns a client that answers from memory.

ts
const proxy = ProxiedHttpClient.layer<"BR">((route) =>
  Ref.update(routes, (asked) => [...asked, route]).pipe(Effect.as(upstream))
);

Assert on the recorded routes. A test that only checks the items passes even if the Scraper bypasses the proxy.

Browsers use the same countries ​

A browser Implementation does not use ProxiedHttpClient. It opens its page in a country with Browser.openIn, and the requirement names the country in the same way, Browser<"FR">:

ts
const page = yield* Browser.openIn({ location: "FR" });

See The Browser Capability.

Source ​