@wxn0brp/falcon-frame 0.9.9 → 0.9.11
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/README.md +2 -1
- package/dist/body-utils.js +3 -3
- package/dist/chain.d.ts +9 -0
- package/dist/chain.js +65 -0
- package/dist/compress.js +3 -3
- package/dist/debug.js +3 -6
- package/dist/engine.js +24 -28
- package/dist/error-handlers.d.ts +21 -0
- package/dist/error-handlers.js +72 -0
- package/dist/helpers.d.ts +4 -1
- package/dist/helpers.js +68 -5
- package/dist/https.d.ts +6 -0
- package/dist/https.js +35 -0
- package/dist/index.d.ts +23 -14
- package/dist/index.js +55 -52
- package/dist/middleware.js +11 -1
- package/dist/req.js +37 -47
- package/dist/res.d.ts +0 -1
- package/dist/res.js +0 -2
- package/dist/router.d.ts +6 -2
- package/dist/router.js +16 -4
- package/dist/safe.d.ts +9 -0
- package/dist/safe.js +79 -0
- package/dist/static.js +51 -11
- package/dist/types.d.ts +13 -1
- package/dist/types.js +4 -0
- package/dist/valid.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# FalconFrame
|
|
2
2
|
|
|
3
|
-
A minimal, modular TypeScript
|
|
3
|
+
A minimal, modular TypeScript http server library.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -12,6 +12,7 @@ A minimal, modular TypeScript web library.
|
|
|
12
12
|
- Debuggable logger
|
|
13
13
|
- Debug panel
|
|
14
14
|
- Proxy-aware client IP resolution
|
|
15
|
+
- HTTPS via environment variables, both servers with `app.l()`
|
|
15
16
|
- Body limits and read timeouts
|
|
16
17
|
- Compression middleware
|
|
17
18
|
|
package/dist/body-utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { safe408, safe413 } from "./safe.js";
|
|
1
2
|
export function parseLimit(limit) {
|
|
2
3
|
if (!limit)
|
|
3
4
|
return 0;
|
|
@@ -38,7 +39,7 @@ export function getRawBody(req, res, limit, timeout = 60_000) {
|
|
|
38
39
|
settled = true;
|
|
39
40
|
res.status(408);
|
|
40
41
|
res.setHeader("Connection", "close");
|
|
41
|
-
|
|
42
|
+
safe408(req, res);
|
|
42
43
|
if (!res.writableEnded)
|
|
43
44
|
res.end();
|
|
44
45
|
res.once("finish", () => req.destroy());
|
|
@@ -59,8 +60,7 @@ export function getRawBody(req, res, limit, timeout = 60_000) {
|
|
|
59
60
|
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
60
61
|
total += buf.length;
|
|
61
62
|
if (limit && total > limit) {
|
|
62
|
-
res
|
|
63
|
-
res.FF._413(req, res);
|
|
63
|
+
safe413(req, res);
|
|
64
64
|
finish(() => reject(new Error("Payload Too Large")));
|
|
65
65
|
req.destroy();
|
|
66
66
|
return;
|
package/dist/chain.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FFResponse } from "./res.js";
|
|
2
|
+
import type { FFRequest, Middleware } from "./types.js";
|
|
3
|
+
export interface ChainHooks {
|
|
4
|
+
start?: (middleware: Middleware) => void;
|
|
5
|
+
result?: (result: any) => void;
|
|
6
|
+
error?: (err: any, middleware: Middleware) => void;
|
|
7
|
+
exhausted?: () => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function createChain(req: FFRequest, res: FFResponse, middlewares: Middleware[], hooks: ChainHooks): () => Promise<void>;
|
package/dist/chain.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { isEnded } from "./safe.js";
|
|
2
|
+
export function createChain(req, res, middlewares, hooks) {
|
|
3
|
+
let index = 0;
|
|
4
|
+
let seqCounter = 0;
|
|
5
|
+
const running = new Set();
|
|
6
|
+
const waiters = [];
|
|
7
|
+
function settle() {
|
|
8
|
+
for (let i = waiters.length - 1; i >= 0; i--) {
|
|
9
|
+
const waiter = waiters[i];
|
|
10
|
+
let blocked = false;
|
|
11
|
+
for (const seq of running) {
|
|
12
|
+
if (seq > waiter.from) {
|
|
13
|
+
blocked = true;
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
if (blocked)
|
|
18
|
+
continue;
|
|
19
|
+
waiters.splice(i, 1);
|
|
20
|
+
waiter.resolve();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async function invoke(middleware, seq) {
|
|
24
|
+
running.add(seq);
|
|
25
|
+
try {
|
|
26
|
+
hooks.start?.(middleware);
|
|
27
|
+
const result = await middleware.middleware(req, res, nextFor(seq));
|
|
28
|
+
if (result && !isEnded(res))
|
|
29
|
+
hooks.result?.(result);
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
hooks.error?.(err, middleware);
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
running.delete(seq);
|
|
36
|
+
settle();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function nextFor(from) {
|
|
40
|
+
let called = false;
|
|
41
|
+
return function next() {
|
|
42
|
+
if (called)
|
|
43
|
+
throw new Error("next() called multiple times");
|
|
44
|
+
called = true;
|
|
45
|
+
if (isEnded(res))
|
|
46
|
+
return Promise.resolve();
|
|
47
|
+
if (index >= middlewares.length) {
|
|
48
|
+
hooks.exhausted?.();
|
|
49
|
+
return Promise.resolve();
|
|
50
|
+
}
|
|
51
|
+
const middleware = middlewares[index++];
|
|
52
|
+
const seq = ++seqCounter;
|
|
53
|
+
const wait = new Promise(resolve => {
|
|
54
|
+
waiters.push({
|
|
55
|
+
from,
|
|
56
|
+
resolve,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
void invoke(middleware, seq);
|
|
60
|
+
settle();
|
|
61
|
+
return wait;
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return nextFor(-1);
|
|
65
|
+
}
|
package/dist/compress.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBrotliCompress, createDeflate, createGzip, constants as zlibConstants, } from "zlib";
|
|
2
|
+
import { safe500 } from "./safe.js";
|
|
2
3
|
function isCompressible(contentType) {
|
|
3
4
|
if (!contentType)
|
|
4
5
|
return true;
|
|
@@ -148,8 +149,7 @@ export function compression(req, res, opts = {}) {
|
|
|
148
149
|
res.destroy(err);
|
|
149
150
|
return;
|
|
150
151
|
}
|
|
151
|
-
res
|
|
152
|
-
res.FF._500(err, req, res);
|
|
152
|
+
safe500(err, req, res);
|
|
153
153
|
if (!res.writableEnded)
|
|
154
154
|
res.end();
|
|
155
155
|
});
|
package/dist/debug.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createHash, randomBytes, timingSafeEqual } from "crypto";
|
|
|
2
2
|
import { join } from "path";
|
|
3
3
|
import { isDebugEnabled } from "./config.js";
|
|
4
4
|
import { isLoopback, normalizeIp } from "./helpers.js";
|
|
5
|
+
import { safe404 } from "./safe.js";
|
|
5
6
|
import { Router } from "./router.js";
|
|
6
7
|
const COOKIE_NAME = "ff_debug";
|
|
7
8
|
const SESSION_TTL = 8 * 60 * 60 * 1000;
|
|
@@ -111,10 +112,6 @@ function hostAllowed(hosts, ip) {
|
|
|
111
112
|
return normalizeIp(host) === target;
|
|
112
113
|
});
|
|
113
114
|
}
|
|
114
|
-
function notFound(req, res) {
|
|
115
|
-
res.status(404);
|
|
116
|
-
req.FF._404(req, res);
|
|
117
|
-
}
|
|
118
115
|
function attempt(req, config) {
|
|
119
116
|
if (!config.enabled)
|
|
120
117
|
return "disabled";
|
|
@@ -170,7 +167,7 @@ export function createDebug(opts = {}, preset = {}) {
|
|
|
170
167
|
if (result === "disabled")
|
|
171
168
|
return next();
|
|
172
169
|
if (result === "denied")
|
|
173
|
-
return
|
|
170
|
+
return safe404(req, res);
|
|
174
171
|
if (result === "token") {
|
|
175
172
|
grantSession(req, res, settings);
|
|
176
173
|
if (tokenSource(req) === "query")
|
|
@@ -185,7 +182,7 @@ export function createDebug(opts = {}, preset = {}) {
|
|
|
185
182
|
if (result === "disabled")
|
|
186
183
|
return next();
|
|
187
184
|
if (result === "denied")
|
|
188
|
-
return
|
|
185
|
+
return safe404(req, res);
|
|
189
186
|
if (result === "token")
|
|
190
187
|
grantSession(req, res, settings);
|
|
191
188
|
next();
|
package/dist/engine.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { getContentType } from "./body-utils.js";
|
|
2
|
+
import { createChain } from "./chain.js";
|
|
2
3
|
import { parseCookies } from "./helpers.js";
|
|
4
|
+
import { safe404, safe500 } from "./safe.js";
|
|
3
5
|
import { extractParams, getMiddlewares, matchMiddleware } from "./middleware.js";
|
|
4
6
|
import { validate } from "./valid.js";
|
|
5
7
|
export class OfflineEngine {
|
|
@@ -24,20 +26,20 @@ export class OfflineEngine {
|
|
|
24
26
|
statusCode: 200,
|
|
25
27
|
headers: {},
|
|
26
28
|
body: "",
|
|
27
|
-
|
|
29
|
+
writableEnded: false,
|
|
28
30
|
FF: this.ff,
|
|
29
31
|
json: (data) => {
|
|
30
32
|
res.headers["content-type"] = "application/json";
|
|
31
33
|
res.body = data;
|
|
32
|
-
res.
|
|
34
|
+
res.writableEnded = true;
|
|
33
35
|
},
|
|
34
36
|
send: (data) => {
|
|
35
37
|
res.body = data;
|
|
36
|
-
res.
|
|
38
|
+
res.writableEnded = true;
|
|
37
39
|
},
|
|
38
40
|
end: (data) => {
|
|
39
41
|
res.body = data;
|
|
40
|
-
res.
|
|
42
|
+
res.writableEnded = true;
|
|
41
43
|
},
|
|
42
44
|
status: (code) => {
|
|
43
45
|
res.statusCode = code;
|
|
@@ -104,32 +106,26 @@ export class OfflineEngine {
|
|
|
104
106
|
const middlewares = getMiddlewares(this.ff.middlewares, path + "/");
|
|
105
107
|
const matched = matchMiddleware(path, middlewares.filter(m => m.method === request.method.toLowerCase() || m.method === "all"));
|
|
106
108
|
if (matched.length === 0) {
|
|
107
|
-
res
|
|
108
|
-
this.ff._404(req, res);
|
|
109
|
+
safe404(req, res);
|
|
109
110
|
return this.buildResponse(res);
|
|
110
111
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
res.statusCode = 500;
|
|
129
|
-
this.ff._500(err, req, res);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
112
|
+
const next = createChain(req, res, matched, {
|
|
113
|
+
start(middleware) {
|
|
114
|
+
req.params = extractParams(middleware.path, path);
|
|
115
|
+
},
|
|
116
|
+
result(result) {
|
|
117
|
+
if (typeof result === "string")
|
|
118
|
+
res.send(result);
|
|
119
|
+
else
|
|
120
|
+
res.json(result);
|
|
121
|
+
},
|
|
122
|
+
error: (err, middleware) => {
|
|
123
|
+
safe500(err, req, res, middleware);
|
|
124
|
+
},
|
|
125
|
+
exhausted: () => {
|
|
126
|
+
safe404(req, res);
|
|
127
|
+
},
|
|
128
|
+
});
|
|
133
129
|
await next();
|
|
134
130
|
return this.buildResponse(res);
|
|
135
131
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ErrorHandler, FinalHandler, ValidationErrorFormatter } from "./types.js";
|
|
2
|
+
export declare class ErrorHandlers {
|
|
3
|
+
_400_formatter: ValidationErrorFormatter;
|
|
4
|
+
_404: FinalHandler;
|
|
5
|
+
_413: FinalHandler;
|
|
6
|
+
_408: FinalHandler;
|
|
7
|
+
_500: ErrorHandler;
|
|
8
|
+
_500Api: ErrorHandler;
|
|
9
|
+
_404Api: FinalHandler;
|
|
10
|
+
_413Api: FinalHandler;
|
|
11
|
+
_408Api: FinalHandler;
|
|
12
|
+
set400Formatter(formatter: ValidationErrorFormatter): void;
|
|
13
|
+
set404(handler: FinalHandler): void;
|
|
14
|
+
set413(handler: FinalHandler): void;
|
|
15
|
+
set408(handler: FinalHandler): void;
|
|
16
|
+
set500(handler: ErrorHandler): void;
|
|
17
|
+
set500Api(handler: ErrorHandler): void;
|
|
18
|
+
set404Api(handler: FinalHandler): void;
|
|
19
|
+
set413Api(handler: FinalHandler): void;
|
|
20
|
+
set408Api(handler: FinalHandler): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export class ErrorHandlers {
|
|
2
|
+
_400_formatter = err => {
|
|
3
|
+
return {
|
|
4
|
+
err: true,
|
|
5
|
+
msg: "Bad request",
|
|
6
|
+
errors: err,
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
_404 = (req, res) => {
|
|
10
|
+
res.end("404: File had second thoughts");
|
|
11
|
+
};
|
|
12
|
+
_413 = (req, res) => {
|
|
13
|
+
res.end("413: Cat is too fat");
|
|
14
|
+
};
|
|
15
|
+
_408 = (req, res) => {
|
|
16
|
+
res.end("408: The request took a nap");
|
|
17
|
+
};
|
|
18
|
+
_500 = (err, req, res) => {
|
|
19
|
+
res.end("500: The code had an existential crisis");
|
|
20
|
+
};
|
|
21
|
+
_500Api = (err, req, res) => {
|
|
22
|
+
res.json({
|
|
23
|
+
err: true,
|
|
24
|
+
msg: "Internal Server Error",
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
_404Api = (req, res) => {
|
|
28
|
+
res.json({
|
|
29
|
+
err: true,
|
|
30
|
+
msg: "Not Found",
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
_413Api = (req, res) => {
|
|
34
|
+
res.json({
|
|
35
|
+
err: true,
|
|
36
|
+
msg: "Payload Too Large",
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
_408Api = (req, res) => {
|
|
40
|
+
res.json({
|
|
41
|
+
err: true,
|
|
42
|
+
msg: "Request Timeout",
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
set400Formatter(formatter) {
|
|
46
|
+
this._400_formatter = formatter;
|
|
47
|
+
}
|
|
48
|
+
set404(handler) {
|
|
49
|
+
this._404 = handler;
|
|
50
|
+
}
|
|
51
|
+
set413(handler) {
|
|
52
|
+
this._413 = handler;
|
|
53
|
+
}
|
|
54
|
+
set408(handler) {
|
|
55
|
+
this._408 = handler;
|
|
56
|
+
}
|
|
57
|
+
set500(handler) {
|
|
58
|
+
this._500 = handler;
|
|
59
|
+
}
|
|
60
|
+
set500Api(handler) {
|
|
61
|
+
this._500Api = handler;
|
|
62
|
+
}
|
|
63
|
+
set404Api(handler) {
|
|
64
|
+
this._404Api = handler;
|
|
65
|
+
}
|
|
66
|
+
set413Api(handler) {
|
|
67
|
+
this._413Api = handler;
|
|
68
|
+
}
|
|
69
|
+
set408Api(handler) {
|
|
70
|
+
this._408Api = handler;
|
|
71
|
+
}
|
|
72
|
+
}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { FFResponse } from "./res.js";
|
|
2
|
-
import { Cookies, FFRequest, ProxyMode } from "./types.js";
|
|
2
|
+
import { Cookies, FFRequest, ListenAddress, ListenTarget, ProxyMode } from "./types.js";
|
|
3
3
|
export { getContentType } from "./encoding.js";
|
|
4
4
|
export declare function parseCookies(cookieHeader: string): Cookies;
|
|
5
5
|
export declare function normalizeIp(ip: string): string;
|
|
6
|
+
export declare function parseListenTarget(target: ListenAddress): ListenTarget;
|
|
7
|
+
export declare function formatListenHost(host: string): string;
|
|
6
8
|
export declare function isLoopback(ip: string): boolean;
|
|
7
9
|
export declare function resolveRequestInfo(req: FFRequest, proxy?: ProxyMode): {
|
|
8
10
|
socketIp: string;
|
|
9
11
|
ip: string;
|
|
10
12
|
isProxied: boolean;
|
|
13
|
+
protocol: "http" | "https";
|
|
11
14
|
};
|
|
12
15
|
export declare function getIP(req: FFRequest, proxy?: ProxyMode): string;
|
|
13
16
|
export declare function streamFile(res: FFResponse, filePath: string): void;
|
package/dist/helpers.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createReadStream } from "fs";
|
|
2
|
+
import { isIP } from "net";
|
|
3
|
+
import { safe404, safe500 } from "./safe.js";
|
|
2
4
|
export { getContentType } from "./encoding.js";
|
|
3
5
|
export function parseCookies(cookieHeader) {
|
|
4
6
|
const cookies = Object.create(null);
|
|
@@ -13,6 +15,57 @@ export function normalizeIp(ip) {
|
|
|
13
15
|
const trimmed = ip.trim();
|
|
14
16
|
return trimmed.startsWith("::ffff:") ? trimmed.slice(7) : trimmed;
|
|
15
17
|
}
|
|
18
|
+
export function parseListenTarget(target) {
|
|
19
|
+
if (typeof target === "object") {
|
|
20
|
+
return {
|
|
21
|
+
host: target.host,
|
|
22
|
+
port: target.port,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
if (typeof target === "number")
|
|
26
|
+
return {
|
|
27
|
+
port: target,
|
|
28
|
+
};
|
|
29
|
+
const value = target.trim();
|
|
30
|
+
const bracketed = /^\[([^\]]+)\](?::(\d+))?$/.exec(value);
|
|
31
|
+
if (bracketed) {
|
|
32
|
+
return {
|
|
33
|
+
host: bracketed[1],
|
|
34
|
+
port: bracketed[2] ? +bracketed[2] : 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (isIP(value))
|
|
38
|
+
return {
|
|
39
|
+
host: value,
|
|
40
|
+
port: 0,
|
|
41
|
+
};
|
|
42
|
+
const colon = value.lastIndexOf(":");
|
|
43
|
+
if (colon !== -1) {
|
|
44
|
+
const portPart = value.slice(colon + 1);
|
|
45
|
+
if (/^\d+$/.test(portPart)) {
|
|
46
|
+
return {
|
|
47
|
+
host: value.slice(0, colon),
|
|
48
|
+
port: +portPart,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
host: value,
|
|
53
|
+
port: 0,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const port = +value;
|
|
57
|
+
return Number.isNaN(port)
|
|
58
|
+
? {
|
|
59
|
+
host: value,
|
|
60
|
+
port: 0,
|
|
61
|
+
}
|
|
62
|
+
: {
|
|
63
|
+
port,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function formatListenHost(host) {
|
|
67
|
+
return host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
|
|
68
|
+
}
|
|
16
69
|
export function isLoopback(ip) {
|
|
17
70
|
const value = normalizeIp(ip);
|
|
18
71
|
return value === "::1" || value.startsWith("127.");
|
|
@@ -41,14 +94,26 @@ function trustsProxy(proxy, socketIp) {
|
|
|
41
94
|
}
|
|
42
95
|
return isLoopback(socketIp);
|
|
43
96
|
}
|
|
97
|
+
function forwardedProto(req) {
|
|
98
|
+
const header = req.headers["x-forwarded-proto"];
|
|
99
|
+
const value = Array.isArray(header) ? header[0] : header;
|
|
100
|
+
if (!value)
|
|
101
|
+
return undefined;
|
|
102
|
+
const proto = value.split(",")[0]?.trim().toLowerCase();
|
|
103
|
+
return proto === "http" || proto === "https" ? proto : undefined;
|
|
104
|
+
}
|
|
44
105
|
export function resolveRequestInfo(req, proxy = "auto") {
|
|
45
106
|
const socketIp = normalizeIp(req.socket.remoteAddress || "");
|
|
46
107
|
const forwarded = forwardedIp(req);
|
|
47
|
-
const
|
|
108
|
+
const trusted = trustsProxy(proxy, socketIp);
|
|
109
|
+
const isProxied = trusted && !!forwarded;
|
|
110
|
+
const protocol = (trusted ? forwardedProto(req) : undefined) ??
|
|
111
|
+
(req.socket?.encrypted ? "https" : "http");
|
|
48
112
|
return {
|
|
49
113
|
socketIp,
|
|
50
114
|
ip: isProxied ? normalizeIp(forwarded) : socketIp,
|
|
51
115
|
isProxied,
|
|
116
|
+
protocol,
|
|
52
117
|
};
|
|
53
118
|
}
|
|
54
119
|
export function getIP(req, proxy = "auto") {
|
|
@@ -66,12 +131,10 @@ export function streamFile(res, filePath) {
|
|
|
66
131
|
res.removeHeader("content-length");
|
|
67
132
|
res.removeHeader("content-encoding");
|
|
68
133
|
if (err.code === "ENOENT") {
|
|
69
|
-
res.
|
|
70
|
-
res.FF._404(res.req, res);
|
|
134
|
+
safe404(res.req, res);
|
|
71
135
|
return;
|
|
72
136
|
}
|
|
73
|
-
res.
|
|
74
|
-
res.FF._500(err, res.req, res);
|
|
137
|
+
safe500(err, res.req, res);
|
|
75
138
|
if (!res.writableEnded)
|
|
76
139
|
res.end();
|
|
77
140
|
});
|
package/dist/https.d.ts
ADDED
package/dist/https.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
function readFile(path) {
|
|
3
|
+
try {
|
|
4
|
+
return readFileSync(path);
|
|
5
|
+
}
|
|
6
|
+
catch (err) {
|
|
7
|
+
throw new Error(`[FF] Cannot read TLS file "${path}": ${err?.message ?? err}`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function parsePort(value) {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
return 443;
|
|
13
|
+
return +value;
|
|
14
|
+
}
|
|
15
|
+
export function resolveHTTPSConfig() {
|
|
16
|
+
const key = process.env.HTTPS_KEY;
|
|
17
|
+
const cert = process.env.HTTPS_CERT;
|
|
18
|
+
if (!key && !cert)
|
|
19
|
+
return undefined;
|
|
20
|
+
if (!key || !cert) {
|
|
21
|
+
throw new Error("[FF] HTTPS_KEY and HTTPS_CERT must be set together");
|
|
22
|
+
}
|
|
23
|
+
const options = {
|
|
24
|
+
key: readFile(key),
|
|
25
|
+
cert: readFile(cert),
|
|
26
|
+
};
|
|
27
|
+
if (process.env.HTTPS_CA)
|
|
28
|
+
options.ca = readFile(process.env.HTTPS_CA);
|
|
29
|
+
if (process.env.HTTPS_PASSPHRASE)
|
|
30
|
+
options.passphrase = process.env.HTTPS_PASSPHRASE;
|
|
31
|
+
return {
|
|
32
|
+
port: parsePort(process.env.HTTPS_PORT),
|
|
33
|
+
options,
|
|
34
|
+
};
|
|
35
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Logger } from "@wxn0brp/lucerna-log";
|
|
2
2
|
import http from "http";
|
|
3
|
+
import https from "https";
|
|
3
4
|
import { type ResolvedConfig } from "./config.js";
|
|
5
|
+
import { ErrorHandlers } from "./error-handlers.js";
|
|
4
6
|
import { renderHTML } from "./render.js";
|
|
5
7
|
import { FFResponse } from "./res.js";
|
|
6
8
|
import { Router } from "./router.js";
|
|
7
|
-
import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, DebugOptions, EngineCallback, ErrorHandler, FFOpts, FFRequest, FinalHandler, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
|
|
9
|
+
import type { BeforeHandleRequest, BodyParserEntry, CombinedVars, DebugOptions, EngineCallback, ErrorHandler, FFOpts, FFRequest, FinalHandler, ListenAddress, ParseBodyFunction, RouteHandler, StandardBodyParserOptions, ValidationErrorFormatter } from "./types.js";
|
|
8
10
|
export declare class FalconFrame<Vars extends Record<string, any> = {}> extends Router {
|
|
9
11
|
logger: Logger;
|
|
12
|
+
httpsServer?: https.Server;
|
|
10
13
|
bodyParsers: Record<string, BodyParserEntry>;
|
|
11
14
|
vars: CombinedVars<Vars>;
|
|
12
15
|
opts: FFOpts;
|
|
@@ -18,14 +21,11 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
|
|
|
18
21
|
generated: boolean;
|
|
19
22
|
enabled?: boolean;
|
|
20
23
|
};
|
|
21
|
-
|
|
22
|
-
_404: FinalHandler;
|
|
23
|
-
_413: FinalHandler;
|
|
24
|
-
_408: FinalHandler;
|
|
25
|
-
_500: ErrorHandler;
|
|
24
|
+
errorHandlers: ErrorHandlers;
|
|
26
25
|
constructor(opts?: Partial<FFOpts>);
|
|
27
26
|
addBodyParser(contentType: string, parser: ParseBodyFunction, opts?: StandardBodyParserOptions): this;
|
|
28
|
-
listen(port:
|
|
27
|
+
listen(port: ListenAddress, callback?: (() => void) | boolean, beforeHandleRequest?: BeforeHandleRequest): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
28
|
+
private printDebugUrl;
|
|
29
29
|
/**
|
|
30
30
|
* Mount the debug panel. Without an explicit `pass` a random token is
|
|
31
31
|
* generated and printed together with the URL on `listen()`.
|
|
@@ -47,23 +47,32 @@ export declare class FalconFrame<Vars extends Record<string, any> = {}> extends
|
|
|
47
47
|
setOrigin(origin?: string[] | string): void;
|
|
48
48
|
/**
|
|
49
49
|
* Listens to the specified port, or the environment variable PORT if available.
|
|
50
|
-
*
|
|
51
|
-
*
|
|
50
|
+
* HTTPS starts as an additional server when `HTTPS_KEY` and `HTTPS_CERT` are set,
|
|
51
|
+
* on `HTTPS_PORT` (default 443).
|
|
52
|
+
* @param port - Port, `host:port`, `[::1]:port`, `[::]:port`, or `{ host, port }`.
|
|
53
|
+
* @returns The HTTP server object. The HTTPS server is available as `httpsServer`.
|
|
52
54
|
*/
|
|
53
|
-
l(port:
|
|
55
|
+
l(port: ListenAddress): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
56
|
+
/** @deprecated use `app.errorHandlers.set400Formatter` */
|
|
54
57
|
set400Formatter(formatter: ValidationErrorFormatter): void;
|
|
58
|
+
/** @deprecated use `app.errorHandlers.set404` */
|
|
55
59
|
set404(handler: FinalHandler): void;
|
|
60
|
+
/** @deprecated use `app.errorHandlers.set413` */
|
|
56
61
|
set413(handler: FinalHandler): void;
|
|
62
|
+
/** @deprecated use `app.errorHandlers.set408` */
|
|
57
63
|
set408(handler: FinalHandler): void;
|
|
64
|
+
/** @deprecated use `app.errorHandlers.set500` */
|
|
58
65
|
set500(handler: ErrorHandler): void;
|
|
59
66
|
}
|
|
60
67
|
export default FalconFrame;
|
|
61
|
-
export { OfflineEngine } from "./engine.js";
|
|
62
|
-
export type { OfflineRequest, OfflineResponse } from "./engine.js";
|
|
63
|
-
export * as Helpers from "./helpers.js";
|
|
64
68
|
export { compression, compressionMiddleware } from "./compress.js";
|
|
65
69
|
export { createDebug, generateDebugToken } from "./debug.js";
|
|
66
70
|
export type { DebugOptions } from "./debug.js";
|
|
67
|
-
export
|
|
71
|
+
export { OfflineEngine } from "./engine.js";
|
|
72
|
+
export type { OfflineRequest, OfflineResponse } from "./engine.js";
|
|
73
|
+
export { ErrorHandlers } from "./error-handlers.js";
|
|
74
|
+
export * as Helpers from "./helpers.js";
|
|
75
|
+
export { resolveHTTPSConfig } from "./https.js";
|
|
76
|
+
export type { CompressionOptions, ListenAddress, FFOpts as Opts, ProxyMode, } from "./types.js";
|
|
68
77
|
export { validateBody } from "./valid.js";
|
|
69
78
|
export { FFRequest, FFResponse, FinalHandler, renderHTML, RouteHandler, Router, };
|