-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.ts
More file actions
101 lines (89 loc) · 3.11 KB
/
Server.ts
File metadata and controls
101 lines (89 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import http from "node:http";
import packageJson from "../package.json" with {type: "json"};
import {Request} from "./Request.js";
import {Response} from "./response/Response.js";
import {RouteRegistry} from "./routing/RouteRegistry.js";
import {ServerErrorRegistry} from "./ServerErrorRegistry.js";
class Server {
/**
* Headers sent with every response.
*/
public readonly globalHeaders: Headers;
/**
* This server's route registry.
*/
public readonly routes = new RouteRegistry();
private readonly server: http.Server;
private readonly copyOrigin: boolean;
private readonly errors = new ServerErrorRegistry();
/**
* Create a new HTTP server.
* @param options Server options.
*/
public constructor(options: Server.Options) {
this.server = http.createServer({
joinDuplicateHeaders: true,
}, this.listener.bind(this));
this.globalHeaders = new Headers(options.globalHeaders);
if (!this.globalHeaders.has("server"))
this.globalHeaders.set("Server", `cldn/${packageJson.version}`);
this.copyOrigin = options.copyOrigin ?? false;
this.server.listen(options.port);
}
private async listener(req: http.IncomingMessage, res: http.ServerResponse) {
res.setHeaders(this.globalHeaders);
let apiRequest: Request;
try {
apiRequest = Request.incomingMessage(req);
}
catch (e) {
if (e instanceof Request.BadUrlError) {
this.errors._get(ServerErrorRegistry.ErrorCodes.BAD_URL)._send(res, this);
return;
}
throw e;
}
if (this.copyOrigin) {
res.appendHeader("Access-Control-Allow-Origin", apiRequest.headers.get("Origin") ?? "*");
res.appendHeader("Vary", "Origin");
}
res.setHeaders(this.globalHeaders);
let response: Response;
try {
response = await this.routes.handle(apiRequest);
}
catch (e) {
if (e instanceof RouteRegistry.NoRouteError)
response = this.errors._get(ServerErrorRegistry.ErrorCodes.NO_ROUTE);
else {
console.error("Internal Server Error:", e);
response = this.errors._get(ServerErrorRegistry.ErrorCodes.INTERNAL);
}
}
response._send(res, this, apiRequest);
};
}
namespace Server {
/**
* Server options
*/
export interface Options {
/**
* The HTTP listener port. From 1 to 65535. Ports 1–1023 require
* privileges.
*/
readonly port: number;
/**
* Headers to send with every response.
*/
readonly globalHeaders?: HeadersInit;
/**
* Whether to set the `Access-Control-Allow-Origin` response header to copy the `Origin` request header.
* If enabled and the client does not set `Origin`, the header will be set to `*`.
* Will also enable setting `Vary: Origin`.
* @default false
*/
readonly copyOrigin?: boolean;
}
}
export {Server};