Skip to content

Commit 8847c59

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/multipart-ts-1.3.2
2 parents a040a6e + 2200f65 commit 8847c59

File tree

4 files changed

+64
-40
lines changed

4 files changed

+64
-40
lines changed

src/Request.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import {Multipart} from "multipart-ts";
12
import http, {OutgoingHttpHeader} from "node:http";
23
import stream from "node:stream";
3-
import {Multipart} from "multipart-ts";
44

55
/**
66
* An incoming HTTP request from a connected client.
@@ -33,7 +33,12 @@ export class Request {
3333
* @param headers See {@link Request#headers}.
3434
* @param bodyStream See {@link Request#bodyStream}.
3535
*/
36-
protected constructor(method: Request["method"], url: Request["url"], headers: Request["headers"], bodyStream: Request["bodyStream"]) {
36+
protected constructor(
37+
method: Request["method"],
38+
url: Request["url"],
39+
headers: Request["headers"],
40+
bodyStream: Request["bodyStream"],
41+
) {
3742
this.method = method;
3843
this.url = url;
3944
this.headers = headers;
@@ -45,8 +50,14 @@ export class Request {
4550
* @throws {@link Request.BadUrlError} If the request URL is invalid.
4651
*/
4752
public static incomingMessage(incomingMessage: http.IncomingMessage) {
48-
const auth = incomingMessage.headers.authorization?.toLowerCase().startsWith("basic ")
49-
? Buffer.from(incomingMessage.headers.authorization.substring("basic ".length), "base64").toString()
53+
const auth =
54+
incomingMessage.headers.authorization
55+
?.toLowerCase()
56+
.startsWith("basic ")
57+
? Buffer.from(
58+
incomingMessage.headers.authorization
59+
.substring("basic ".length), "base64"
60+
).toString()
5061
: null;
5162

5263
const url = `http://${auth ? `${auth}@` : ""}${process.env.HOST ?? "localhost"}${incomingMessage.url ?? "/"}`;
@@ -55,12 +66,21 @@ export class Request {
5566

5667
const headers = Request.headersFromNodeDict(incomingMessage.headers);
5768

58-
return new Request(
59-
incomingMessage.method as Request.Method,
60-
new URL(url),
61-
headers,
62-
incomingMessage
63-
)
69+
return new Request(incomingMessage.method as Request.Method, new URL(url), headers, incomingMessage);
70+
}
71+
72+
/**
73+
* @internal
74+
*/
75+
public static headersFromNodeDict(headers: Record<string, OutgoingHttpHeader | undefined>): Headers {
76+
return new Headers(Object.entries(headers)
77+
.filter((e) => e[1] !== undefined)
78+
.flatMap<[string, string]>(([key, value]) =>
79+
value instanceof Array
80+
? value.map<[string, string]>(v => [key, v])
81+
: [[key, String(value)]]
82+
)
83+
);
6484
}
6585

6686
/**
@@ -135,19 +155,6 @@ export class Request {
135155
public async text(): Promise<string> {
136156
return (await this.blob()).text();
137157
}
138-
139-
/**
140-
* @internal
141-
*/
142-
public static headersFromNodeDict(headers: Record<string, OutgoingHttpHeader | undefined>): Headers {
143-
return new Headers(
144-
(Object.entries(headers)
145-
.filter(([, v]) => v !== undefined) as [string, Exclude<typeof headers[string], undefined>][])
146-
.flatMap<[string, string]>(([key, value]) =>value instanceof Array
147-
? value.map<[string, string]>(v => [key, v])
148-
: [[key, String(value)]])
149-
);
150-
}
151158
}
152159

153160
export namespace Request {

src/ServerErrorRegistry.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,41 @@ import {TextResponse} from "./response/TextResponse.js";
77
class ServerErrorRegistry {
88
private readonly responses: Record<ServerErrorRegistry.ErrorCodes, Response>;
99

10+
/**
11+
* Create a new server error registry initialised with default responses.
12+
*/
13+
public constructor() {
14+
this.responses = {
15+
[ServerErrorRegistry.ErrorCodes.BAD_URL]:
16+
new TextResponse("Bad request URL.", 400),
17+
18+
[ServerErrorRegistry.ErrorCodes.NO_ROUTE]:
19+
new TextResponse("No route in this registry matches the request.", 404),
20+
21+
[ServerErrorRegistry.ErrorCodes.INTERNAL]:
22+
new TextResponse("An internal error occurred.", 500),
23+
};
24+
}
25+
26+
/**
27+
* Replace server error response by registering a new custom response.
28+
* @param code The server error code.
29+
* @param response The response to send.
30+
*/
1031
public register(code: ServerErrorRegistry.ErrorCodes, response: Response) {
1132
this.responses[code] = response;
1233
}
1334

35+
/** @internal */
1436
public _get(code: ServerErrorRegistry.ErrorCodes): Response {
1537
return this.responses[code];
1638
}
17-
18-
public constructor() {
19-
this.responses = {
20-
[ServerErrorRegistry.ErrorCodes.BAD_URL]: new TextResponse("Bad request URL.", 400),
21-
[ServerErrorRegistry.ErrorCodes.NO_ROUTE]: new TextResponse("No route in this registry matches the request.", 404),
22-
[ServerErrorRegistry.ErrorCodes.INTERNAL]: new TextResponse("An internal error occurred.", 500),
23-
};
24-
}
2539
}
2640

2741
namespace ServerErrorRegistry {
42+
/**
43+
* Server error codes
44+
*/
2845
export const enum ErrorCodes {
2946
BAD_URL,
3047
NO_ROUTE,

src/response/JsonResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export class JsonResponse<T> extends TextResponse {
1717
return v.toISOString();
1818
if (typeof v === "bigint")
1919
return v <= BigInt(Number.MAX_SAFE_INTEGER)
20-
? Number(v)
21-
: v.toString();
20+
? Number(v)
21+
: v.toString();
2222
return v;
2323
}));
2424
}

src/response/Response.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export abstract class Response {
2626
this.headers = new Headers(headers);
2727
}
2828

29+
/**
30+
* @internal
31+
*/
32+
public _send(...args: Parameters<Response["send"]>): ReturnType<Response["send"]> {
33+
return this.send(...args);
34+
}
35+
2936
/**
3037
* Set the HTTP response status code and headers.
3138
*/
@@ -38,11 +45,4 @@ export abstract class Response {
3845
* Called once by the server to send the response.
3946
*/
4047
protected abstract send(res: http.ServerResponse, server: Server, req?: Request): void | Promise<void>;
41-
42-
/**
43-
* @internal
44-
*/
45-
public _send(...args: Parameters<Response["send"]>): ReturnType<Response["send"]> {
46-
return this.send(...args);
47-
}
4848
}

0 commit comments

Comments
 (0)