@wxn0brp/falcon-frame 0.6.3 → 0.7.1
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/body-utils.d.ts +2 -2
- package/dist/body-utils.js +3 -5
- package/dist/body.d.ts +2 -3
- package/dist/body.js +4 -4
- package/dist/cors.d.ts +1 -1
- package/dist/cors.js +53 -17
- package/dist/index.js +2 -2
- package/dist/render.d.ts +1 -1
- package/dist/req.d.ts +1 -1
- package/dist/req.js +1 -0
- package/dist/res.d.ts +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.js +1 -0
- package/package.json +2 -2
package/dist/body-utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { FFResponse } from "./index.js";
|
|
2
2
|
import type { FFRequest, ParseBodyFunction, RouteHandler, StandardBodyParserOptions } from "./types.js";
|
|
3
3
|
export declare function parseLimit(limit: string | number): number;
|
|
4
4
|
export declare function getContentType(req: FFRequest): string | undefined;
|
|
5
5
|
export declare function getRawBody(req: FFRequest, res: FFResponse, limit: number): Promise<string>;
|
|
6
|
-
export declare function getStandardBodyParser(type: string, parser: ParseBodyFunction,
|
|
6
|
+
export declare function getStandardBodyParser(type: string, parser: ParseBodyFunction, opts: StandardBodyParserOptions): RouteHandler;
|
package/dist/body-utils.js
CHANGED
|
@@ -35,8 +35,6 @@ export function getRawBody(req, res, limit) {
|
|
|
35
35
|
res.status(413);
|
|
36
36
|
res.FF._413(req, res);
|
|
37
37
|
req.destroy();
|
|
38
|
-
// @ts-ignore
|
|
39
|
-
error.cancel = true;
|
|
40
38
|
return reject(error);
|
|
41
39
|
}
|
|
42
40
|
});
|
|
@@ -48,7 +46,7 @@ export function getRawBody(req, res, limit) {
|
|
|
48
46
|
});
|
|
49
47
|
});
|
|
50
48
|
}
|
|
51
|
-
export function getStandardBodyParser(type, parser,
|
|
49
|
+
export function getStandardBodyParser(type, parser, opts) {
|
|
52
50
|
const limit = parseLimit(opts.limit || "100k");
|
|
53
51
|
return async (req, res, next) => {
|
|
54
52
|
if ((typeof req.body == "object" && Object.keys(req.body).length) || getContentType(req) !== type) {
|
|
@@ -56,11 +54,11 @@ export function getStandardBodyParser(type, parser, FF, opts) {
|
|
|
56
54
|
}
|
|
57
55
|
try {
|
|
58
56
|
const body = await getRawBody(req, res, limit);
|
|
59
|
-
req.body = await parser(body, req,
|
|
57
|
+
req.body = await parser(body, req, res);
|
|
60
58
|
next();
|
|
61
59
|
}
|
|
62
60
|
catch (err) {
|
|
63
|
-
if (
|
|
61
|
+
if (res._ended)
|
|
64
62
|
return;
|
|
65
63
|
req.body = {};
|
|
66
64
|
next();
|
package/dist/body.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import FalconFrame from "./index.js";
|
|
2
1
|
import { RouteHandler, StandardBodyParserOptions } from "./types.js";
|
|
3
|
-
export declare function json(
|
|
4
|
-
export declare function urlencoded(
|
|
2
|
+
export declare function json(opts?: StandardBodyParserOptions): RouteHandler;
|
|
3
|
+
export declare function urlencoded(opts?: StandardBodyParserOptions): RouteHandler;
|
package/dist/body.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import querystring from "querystring";
|
|
2
2
|
import { getStandardBodyParser } from "./body-utils.js";
|
|
3
|
-
export function json(
|
|
4
|
-
return getStandardBodyParser("application/json", (body) => JSON.parse(body),
|
|
3
|
+
export function json(opts = {}) {
|
|
4
|
+
return getStandardBodyParser("application/json", (body) => JSON.parse(body), opts);
|
|
5
5
|
}
|
|
6
|
-
export function urlencoded(
|
|
7
|
-
return getStandardBodyParser("application/x-www-form-urlencoded", (body) => querystring.parse(body),
|
|
6
|
+
export function urlencoded(opts = {}) {
|
|
7
|
+
return getStandardBodyParser("application/x-www-form-urlencoded", (body) => querystring.parse(body), opts);
|
|
8
8
|
}
|
package/dist/cors.d.ts
CHANGED
|
@@ -4,4 +4,4 @@ export interface Opts {
|
|
|
4
4
|
accessControlAllowHeaders?: boolean;
|
|
5
5
|
headers?: Record<string, string>;
|
|
6
6
|
}
|
|
7
|
-
export declare function createCORS(allowedOrigins: string[], opts?: Opts): RouteHandler;
|
|
7
|
+
export declare function createCORS(allowedOrigins: (string | RegExp)[], opts?: Opts): RouteHandler;
|
package/dist/cors.js
CHANGED
|
@@ -4,6 +4,54 @@ function setHeader(res, opts) {
|
|
|
4
4
|
if (opts.accessControlAllowHeaders)
|
|
5
5
|
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
|
6
6
|
}
|
|
7
|
+
function parseOrigin(origin) {
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(origin);
|
|
10
|
+
return {
|
|
11
|
+
host: url.hostname,
|
|
12
|
+
port: url.port ||
|
|
13
|
+
(url.protocol === "https:" ? "443" : "80")
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function matches(origin, pattern) {
|
|
21
|
+
if (pattern === "*")
|
|
22
|
+
return true;
|
|
23
|
+
if (pattern instanceof RegExp)
|
|
24
|
+
return pattern.test(origin);
|
|
25
|
+
if (pattern.toLowerCase() === origin.toLowerCase())
|
|
26
|
+
return true;
|
|
27
|
+
const o = parseOrigin(origin);
|
|
28
|
+
if (!o)
|
|
29
|
+
return false;
|
|
30
|
+
// *.example.com[:port]
|
|
31
|
+
if (pattern.startsWith("*.")) {
|
|
32
|
+
const [pHost, pPort] = pattern.split(":");
|
|
33
|
+
const base = pHost.slice(2).toLowerCase();
|
|
34
|
+
if (o.host !== base && o.host.endsWith("." + base)) {
|
|
35
|
+
return !pPort || pPort === "*" || pPort === o.port;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// localhost:port
|
|
40
|
+
if (pattern.includes(":")) {
|
|
41
|
+
const [pHost, pPort] = pattern.split(":");
|
|
42
|
+
const base = pHost.toLowerCase();
|
|
43
|
+
return o.host === base && (pPort === "*" || o.port === pPort);
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
function handleEnd(req, res, next, origin) {
|
|
48
|
+
res.setHeader("Access-Control-Allow-Origin", origin);
|
|
49
|
+
if (req.method === "OPTIONS") {
|
|
50
|
+
res.statusCode = 204;
|
|
51
|
+
return res.end();
|
|
52
|
+
}
|
|
53
|
+
next();
|
|
54
|
+
}
|
|
7
55
|
export function createCORS(allowedOrigins, opts = {}) {
|
|
8
56
|
opts = {
|
|
9
57
|
accessControlAllowMethods: true,
|
|
@@ -16,24 +64,12 @@ export function createCORS(allowedOrigins, opts = {}) {
|
|
|
16
64
|
res.setHeader(key, value);
|
|
17
65
|
}
|
|
18
66
|
}
|
|
19
|
-
|
|
20
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
21
|
-
setHeader(res, opts);
|
|
22
|
-
if (req.method === "OPTIONS") {
|
|
23
|
-
res.statusCode = 204;
|
|
24
|
-
return res.end();
|
|
25
|
-
}
|
|
26
|
-
return next();
|
|
27
|
-
}
|
|
67
|
+
setHeader(res, opts);
|
|
28
68
|
const origin = req.headers.origin;
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
res.statusCode = 204;
|
|
34
|
-
return res.end();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
69
|
+
if (allowedOrigins.includes("*"))
|
|
70
|
+
return handleEnd(req, res, next, origin || "*");
|
|
71
|
+
if (origin && allowedOrigins.some((pattern) => matches(origin, pattern)))
|
|
72
|
+
return handleEnd(req, res, next, origin);
|
|
37
73
|
next();
|
|
38
74
|
};
|
|
39
75
|
}
|
package/dist/index.js
CHANGED
|
@@ -40,9 +40,9 @@ export class FalconFrame extends Router {
|
|
|
40
40
|
...opts,
|
|
41
41
|
};
|
|
42
42
|
if (!this.opts.disableJsonParser)
|
|
43
|
-
this.addBodyParser(json(
|
|
43
|
+
this.addBodyParser(json({ limit: this.opts.bodyLimit }));
|
|
44
44
|
if (!this.opts.disableUrlencodedParser)
|
|
45
|
-
this.addBodyParser(urlencoded(
|
|
45
|
+
this.addBodyParser(urlencoded({ limit: this.opts.bodyLimit }));
|
|
46
46
|
this.engine(".html", (path, options, callback, FF) => {
|
|
47
47
|
try {
|
|
48
48
|
const content = renderHTML(path, options, [], FF);
|
package/dist/render.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import FalconFrame from "./index.js";
|
|
|
2
2
|
export interface RenderOptions {
|
|
3
3
|
noLayout?: boolean;
|
|
4
4
|
}
|
|
5
|
-
export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame
|
|
5
|
+
export declare function renderHTML(templatePath: string, data?: Record<string, any>, renderedPaths?: string[], FF?: FalconFrame<any>, opts?: RenderOptions): string;
|
package/dist/req.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import FalconFrame from "./index.js";
|
|
2
2
|
import { FFResponse } from "./res.js";
|
|
3
3
|
import { FFRequest } from "./types.js";
|
|
4
|
-
export declare function handleRequest(req: FFRequest, res: FFResponse, FF: FalconFrame): void;
|
|
4
|
+
export declare function handleRequest(req: FFRequest, res: FFResponse, FF: FalconFrame<any>): void;
|
package/dist/req.js
CHANGED
|
@@ -5,6 +5,7 @@ import { FFResponse } from "./res.js";
|
|
|
5
5
|
import { validate } from "./valid.js";
|
|
6
6
|
export function handleRequest(req, res, FF) {
|
|
7
7
|
Object.setPrototypeOf(res, FFResponse.prototype);
|
|
8
|
+
req.FF = FF;
|
|
8
9
|
res.FF = FF;
|
|
9
10
|
const originalEnd = res.end;
|
|
10
11
|
res.end = function (...any) {
|
package/dist/res.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -17,11 +17,12 @@ export interface Query {
|
|
|
17
17
|
export interface Body {
|
|
18
18
|
[key: string]: any;
|
|
19
19
|
}
|
|
20
|
-
export type ParseBodyFunction = (body: string, req: FFRequest,
|
|
20
|
+
export type ParseBodyFunction = (body: string, req: FFRequest, res: FFResponse) => Promise<Record<string, any>>;
|
|
21
21
|
export interface StandardBodyParserOptions {
|
|
22
22
|
limit?: string | number;
|
|
23
23
|
}
|
|
24
24
|
export declare class FFRequest extends http.IncomingMessage {
|
|
25
|
+
FF: FalconFrame<any>;
|
|
25
26
|
path: string;
|
|
26
27
|
query: Query;
|
|
27
28
|
params: Params;
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/falcon-frame",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"typescript": "*"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@wxn0brp/lucerna-log": "^0.2.
|
|
32
|
+
"@wxn0brp/lucerna-log": "^0.2.2"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|