@jongleberry/api-server 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/application.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { createRouteBuilder } from "./router.mjs";
|
|
|
4
4
|
import Router from "find-my-way";
|
|
5
5
|
import { ServerTiming } from "./server-timing.mjs";
|
|
6
6
|
import { Logger } from "./logger.mjs";
|
|
7
|
-
import { getFallbackBody, getFallbackStatus, sendFallback } from "./fallback-response.mjs";
|
|
7
|
+
import { ensureFallbackHeaders, getFallbackBody, getFallbackStatus, sendFallback, } from "./fallback-response.mjs";
|
|
8
8
|
export class Application extends EventEmitter {
|
|
9
9
|
router = Router();
|
|
10
10
|
errorHandlerFn = null;
|
|
@@ -57,6 +57,7 @@ export class Application extends EventEmitter {
|
|
|
57
57
|
// Swallow listener throws so the 500 response still goes out.
|
|
58
58
|
}
|
|
59
59
|
if (!res.headersSent) {
|
|
60
|
+
ensureFallbackHeaders(res);
|
|
60
61
|
res.writeHead(500);
|
|
61
62
|
res.end("Internal Server Error");
|
|
62
63
|
}
|
|
@@ -100,6 +101,7 @@ export class Application extends EventEmitter {
|
|
|
100
101
|
await this.notFoundHandlerFn(ctx);
|
|
101
102
|
}
|
|
102
103
|
else {
|
|
104
|
+
ensureFallbackHeaders(res);
|
|
103
105
|
res.writeHead(404);
|
|
104
106
|
res.end("Not Found");
|
|
105
107
|
}
|
|
@@ -129,6 +131,7 @@ export class Application extends EventEmitter {
|
|
|
129
131
|
}
|
|
130
132
|
else if (!res.headersSent) {
|
|
131
133
|
const status = getFallbackStatus(error);
|
|
134
|
+
ensureFallbackHeaders(res);
|
|
132
135
|
res.writeHead(status);
|
|
133
136
|
res.end(getFallbackBody(error, status));
|
|
134
137
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ServerResponse } from "node:http";
|
|
2
|
+
export declare function ensureFallbackHeaders(res: ServerResponse): void;
|
|
2
3
|
export declare function sendFallback(res: ServerResponse): void;
|
|
3
4
|
export declare function getFallbackStatus(error: unknown): number;
|
|
4
5
|
export declare function getFallbackBody(error: unknown, status: number): string;
|
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
const FALLBACK_BODY = "Not Found";
|
|
2
2
|
const ERROR_STATUS = 500;
|
|
3
3
|
const ERROR_BODY = "Internal Server Error";
|
|
4
|
+
const TEXT_PLAIN_CONTENT_TYPE = "text/plain; charset=utf-8";
|
|
5
|
+
const FALLBACK_HEADERS = {
|
|
6
|
+
"Content-Type": TEXT_PLAIN_CONTENT_TYPE,
|
|
7
|
+
"X-XSS-Protection": "0",
|
|
8
|
+
"X-Frame-Options": "SAMEORIGIN",
|
|
9
|
+
"X-Content-Type-Options": "nosniff",
|
|
10
|
+
};
|
|
11
|
+
export function ensureFallbackHeaders(res) {
|
|
12
|
+
for (const [name, value] of Object.entries(FALLBACK_HEADERS)) {
|
|
13
|
+
try {
|
|
14
|
+
if (typeof res.hasHeader !== "function" || !res.hasHeader(name)) {
|
|
15
|
+
res.setHeader(name, value);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Header mutation can fail on destroyed sockets or non-standard responses.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
4
23
|
export function sendFallback(res) {
|
|
5
24
|
try {
|
|
25
|
+
ensureFallbackHeaders(res);
|
|
6
26
|
res.writeHead(ERROR_STATUS);
|
|
7
27
|
res.end(ERROR_BODY);
|
|
8
28
|
}
|