Skip to content

Commit 3f21a1f

Browse files
authored
Merge branch 'main' into 20-real-client-ip-based-on-header-trusted-proxies
2 parents a7b3805 + d47b291 commit 3f21a1f

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServerErrorRegistry.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ class ServerErrorRegistry<A> {
1414
public constructor() {
1515
this.responses = {
1616
[ServerErrorRegistry.ErrorCodes.BAD_URL]:
17-
new TextResponse("Bad request URL.", 400),
17+
new TextResponse("Invalid request URL.", 400),
1818

1919
[ServerErrorRegistry.ErrorCodes.NO_ROUTE]:
20-
new TextResponse("No route in this registry matches the request.", 404),
20+
new TextResponse("No route matched the request.", 404),
2121

2222
[ServerErrorRegistry.ErrorCodes.INTERNAL]:
23-
new TextResponse("An internal error occurred.", 500),
23+
new TextResponse("An unexpected internal server error occurred.", 500),
2424

2525
[ServerErrorRegistry.ErrorCodes.PRECONDITION_FAILED]:
26-
new TextResponse("Precondition failed.", 412),
26+
new TextResponse("One or more preconditions were not met.", 412),
2727

2828
[ServerErrorRegistry.ErrorCodes.NO_PERMISSION]:
29-
new TextResponse("You do not have permission to perform this action.", 403),
29+
new TextResponse("You do not have the necessary permissions to perform this action.", 403),
3030

3131
[ServerErrorRegistry.ErrorCodes.UNAUTHORISED]:
3232
new TextResponse("Authentication information was either absent or invalid.", 401),

src/response/EmptyResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class EmptyResponse<A> extends Response<A> {
1616
}
1717

1818
protected override async send(res: http.ServerResponse, req?: Request<A>): Promise<void> {
19-
if (req !== undefined)
20-
req._responseHeaders.set("content-length", "0");
19+
if (!this.headers.has("content-length"))
20+
this.headers.set("content-length", "0");
2121
this.writeHead(res, req);
2222
res.end();
2323
}

src/response/StreamedResponse.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import http from "node:http";
2+
import stream from "node:stream/promises";
3+
import {Request} from "../Request.js";
4+
import {Response} from "./Response.js";
5+
6+
/**
7+
* A response that streams data from a readable stream.
8+
*/
9+
export class StreamedResponse<A> extends Response<A> {
10+
private readonly stream: NodeJS.ReadableStream;
11+
12+
/**
13+
* Construct a StreamedResponse.
14+
* @param stream The readable stream to send in the response body.
15+
* @param [statusCode=200] The HTTP response status code to send.
16+
* @param [headers] The HTTP response headers to send.
17+
*/
18+
public constructor(stream: NodeJS.ReadableStream, statusCode = 200, headers?: HeadersInit) {
19+
super(statusCode, headers);
20+
this.stream = stream;
21+
if (!this.headers.has("transfer-encoding"))
22+
this.headers.set("transfer-encoding", "chunked");
23+
}
24+
25+
protected override async send(res: http.ServerResponse, req?: Request<A>): Promise<void> {
26+
this.writeHead(res, req);
27+
await stream.pipeline(this.stream, res);
28+
}
29+
}

src/response/TextResponse.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export class TextResponse<A> extends BufferResponse<A> {
1919
public constructor(text: string, statusCode = 200, headers?: HeadersInit) {
2020
super(statusCode, headers);
2121
this.text = text;
22+
if (!this.headers.has("content-type"))
23+
this.headers.set("content-type", "text/plain");
2224
}
2325

2426
public override readBuffer() {

src/response/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from "./BufferResponse.js";
33
export * from "./EmptyResponse.js";
44
export * from "./JsonResponse.js";
55
export * from "./Response.js";
6+
export * from "./StreamedResponse.js";
67
export * from "./TextResponse.js";
78
export * from "./ThrowableResponse.js";

0 commit comments

Comments
 (0)