Skip to content

Commit 6765f3b

Browse files
committed
feat: add option to enable unauthorized TLS
1 parent 73168fd commit 6765f3b

File tree

7 files changed

+54
-37
lines changed

7 files changed

+54
-37
lines changed

examples/terminal/unsecure.mts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HttpRequest, send } from "schwi";
2+
3+
const request = new HttpRequest.Builder("https://signatures.unilim.fr")
4+
.setRedirection(HttpRequest.Redirection.MANUAL)
5+
.enableUnauthorizedTLS()
6+
.build();
7+
8+
const response = await send(request);
9+
console.log(response.headers.getSetCookie());
10+
11+
const body = await response.toString();
12+
console.log(body);

package/src/fetchers/adapters/fetch.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@ import type { HttpRequest } from "~/request";
22
import { HeaderMap } from "~/headers";
33
import { HttpResponse } from "~/response";
44

5-
export const fetchAdapter = async (fetch: (url: string, init: RequestInit) => Promise<Response>, req: HttpRequest): Promise<HttpResponse> => {
6-
const response = await fetch(req.url.href, {
7-
body: req.body,
8-
// We don't want to send cookies, only the ones we set manually.
9-
credentials: "omit",
10-
headers: req.headers.toNativeHeaders(),
11-
method: req.method,
12-
redirect: req.redirection
13-
});
5+
export const httpRequestToFetchInit = (req: HttpRequest): RequestInit => ({
6+
body: req.body,
7+
// We don't want to send cookies, only the ones we set manually.
8+
credentials: "omit",
9+
headers: req.headers.toNativeHeaders(),
10+
method: req.method,
11+
redirect: req.redirection
12+
});
1413

15-
return new HttpResponse(
16-
response.url,
17-
response.status,
18-
new HeaderMap(response.headers),
19-
async () => {
20-
return response.text();
21-
},
22-
async () => {
23-
const buffer = await response.arrayBuffer();
24-
return buffer;
25-
}
26-
);
27-
};
14+
export const fetchResponseToHttpResponse = (res: Response): HttpResponse => new HttpResponse(
15+
res.url,
16+
res.status,
17+
new HeaderMap(res.headers),
18+
async () => res.text(),
19+
async () => res.arrayBuffer()
20+
);

package/src/fetchers/native.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

package/src/fetchers/tauri.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Fetcher } from "../factory";
22

33
import { fetch as tauriFetch } from "@tauri-apps/plugin-http";
4-
import { fetchAdapter } from "./adapters/fetch";
4+
import { fetchResponseToHttpResponse, httpRequestToFetchInit } from "./adapters/fetch";
55

66
export const fetcher: Fetcher = async (req) => {
7-
return fetchAdapter(tauriFetch, req);
7+
const res = await tauriFetch(req.url.href, httpRequestToFetchInit(req));
8+
return fetchResponseToHttpResponse(res);
89
};

package/src/fetchers/undici.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
// @ts-expect-error: undici is not typed with this type of import.
1+
// @ts-expect-error : not typed
2+
import Agent from "undici/lib/dispatcher/agent.js";
3+
// @ts-expect-error : not typed
24
import { fetch as _fetch } from "undici/lib/web/fetch/index.js";
3-
// ^ import from path to avoid Bun overriding it.
45

56
// We have to type the function since no types are provided here.
67
const undiciFetch = _fetch as typeof globalThis.fetch;
78

89
import type { Fetcher } from "../factory";
9-
import { fetchAdapter } from "./adapters/fetch";
10+
import { fetchResponseToHttpResponse, httpRequestToFetchInit } from "./adapters/fetch";
1011

1112
export const fetcher: Fetcher = async (req) => {
12-
return fetchAdapter(undiciFetch, req);
13+
const agent = new Agent({ connect: { rejectUnauthorized: !req.unauthorizedTLS } });
14+
15+
const res = await undiciFetch(req.url.href, {
16+
...httpRequestToFetchInit(req),
17+
// @ts-expect-error : not typed
18+
dispatcher: agent
19+
});
20+
21+
return fetchResponseToHttpResponse(res);
1322
};

package/src/index.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { factory } from "./factory";
2-
import { fetcher } from "./fetchers/native";
2+
import { fetcher } from "./fetchers/undici";
33

44
export * from "./form";
55
export { HeaderKeys, HeaderMap } from "./headers";

package/src/request.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class HttpRequestBuilder {
2121
private headers = new HeaderMap();
2222
private method = HttpRequestMethod.GET;
2323
private redirection = HttpRequestRedirection.MANUAL;
24+
private unauthorizedTLS = false;
2425
private url: URL;
2526

2627
constructor(url: string | URL) {
@@ -49,7 +50,8 @@ class HttpRequestBuilder {
4950
this.method,
5051
this.body,
5152
this.headers,
52-
this.redirection
53+
this.redirection,
54+
this.unauthorizedTLS
5355
);
5456
}
5557

@@ -68,6 +70,11 @@ class HttpRequestBuilder {
6870
return this;
6971
}
7072

73+
public enableUnauthorizedTLS(): this {
74+
this.unauthorizedTLS = true;
75+
return this;
76+
}
77+
7178
/**
7279
* @example
7380
* .setAllCookies([["name", "value"], ["name2", "value2"]])
@@ -146,6 +153,7 @@ export class HttpRequest {
146153
public readonly method: HttpRequestMethod,
147154
public readonly body: ArrayBuffer | Blob | FormData | string | undefined,
148155
public readonly headers: HeaderMap,
149-
public readonly redirection: HttpRequestRedirection
156+
public readonly redirection: HttpRequestRedirection,
157+
public readonly unauthorizedTLS: boolean
150158
) {}
151159
}

0 commit comments

Comments
 (0)