deno/ext/fetch/22_http_client.js
Luca Casonato 3b6c70e5b2
feat(ext/fetch): add support for fetch on unix sockets (#29154)
This commit adds support for using Unix socket proxies in `fetch` API.

This is facilitated by passing an appropriate `Deno.HttpClient` instance
to the `fetch` API:
```
const client = Deno.createHttpClient({
  proxy: {
    transport: "unix",
    path: "/path/to/unix.sock",
  },
});

await fetch("http://localhost/ping", { client });
```

Closes https://github.com/denoland/deno/issues/8821

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2025-05-13 18:06:17 +02:00

127 lines
3.4 KiB
JavaScript

// Copyright 2018-2025 the Deno authors. MIT license.
// @ts-check
/// <reference path="../webidl/internal.d.ts" />
/// <reference path="../web/internal.d.ts" />
/// <reference path="../url/internal.d.ts" />
/// <reference path="../../cli/tsc/dts/lib.deno_web.d.ts" />
/// <reference path="./internal.d.ts" />
/// <reference path="../web/06_streams_types.d.ts" />
/// <reference path="../../cli/tsc/dts/lib.deno_fetch.d.ts" />
/// <reference lib="esnext" />
import { core, primordials } from "ext:core/mod.js";
import { SymbolDispose } from "ext:deno_web/00_infra.js";
import { op_fetch_custom_client } from "ext:core/ops";
import { loadTlsKeyPair } from "ext:deno_net/02_tls.js";
const { internalRidSymbol } = core;
const {
JSONStringify,
ObjectDefineProperty,
ObjectHasOwn,
StringPrototypeStartsWith,
TypeError,
} = primordials;
/**
* @param {Deno.CreateHttpClientOptions} options
* @returns {HttpClient}
*/
function createHttpClient(options) {
options.caCerts ??= [];
if (options.proxy) {
if (ObjectHasOwn(options.proxy, "transport")) {
switch (options.proxy.transport) {
case "http": {
const url = options.proxy.url;
if (
StringPrototypeStartsWith(url, "https:") ||
StringPrototypeStartsWith(url, "socks5:") ||
StringPrototypeStartsWith(url, "socks5h:")
) {
throw new TypeError(
`The url passed into 'proxy.url' has an invalid scheme for this transport.`,
);
}
options.proxy.transport = "http";
break;
}
case "https": {
const url = options.proxy.url;
if (
StringPrototypeStartsWith(url, "http:") ||
StringPrototypeStartsWith(url, "socks5:") ||
StringPrototypeStartsWith(url, "socks5h:")
) {
throw new TypeError(
`The url passed into 'proxy.url' has an invalid scheme for this transport.`,
);
}
options.proxy.transport = "http";
break;
}
case "socks5": {
const url = options.proxy.url;
if (
!StringPrototypeStartsWith(url, "socks5:") ||
!StringPrototypeStartsWith(url, "socks5h:")
) {
throw new TypeError(
`The url passed into 'proxy.url' has an invalid scheme for this transport.`,
);
}
options.proxy.transport = "http";
break;
}
case "unix": {
break;
}
default: {
throw new TypeError(
`Invalid value for 'proxy.transport' option: ${
JSONStringify(options.proxy.transport)
}`,
);
}
}
} else {
options.proxy.transport = "http";
}
}
const keyPair = loadTlsKeyPair("Deno.createHttpClient", options);
return new HttpClient(
op_fetch_custom_client(
options,
keyPair,
),
);
}
class HttpClient {
#rid;
/**
* @param {number} rid
*/
constructor(rid) {
ObjectDefineProperty(this, internalRidSymbol, {
__proto__: null,
enumerable: false,
value: rid,
});
this.#rid = rid;
}
close() {
core.close(this.#rid);
}
[SymbolDispose]() {
core.tryClose(this.#rid);
}
}
const HttpClientPrototype = HttpClient.prototype;
export { createHttpClient, HttpClient, HttpClientPrototype };