@wxn0brp/falcon-frame 0.9.0 → 0.9.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.
@@ -5,10 +5,12 @@ export function parseLimit(limit) {
5
5
  return limit;
6
6
  if (typeof limit !== "string")
7
7
  return 0;
8
- limit = limit.toLowerCase().replace("b", "");
9
- const match = limit.match(/^(\d+)([kmg])?$/i);
8
+ const match = limit
9
+ .toLowerCase()
10
+ .replace(/b$/, "")
11
+ .match(/^(\d+)([kmg])?$/);
10
12
  if (!match)
11
- return 0;
13
+ throw new Error(`Invalid body limit: ${limit}`);
12
14
  const num = parseInt(match[1], 10);
13
15
  const unit = match[2]?.toLowerCase();
14
16
  switch (unit) {
@@ -27,19 +29,21 @@ export function getContentType(req) {
27
29
  }
28
30
  export function getRawBody(req, res, limit) {
29
31
  return new Promise((resolve, reject) => {
30
- let body = "";
32
+ const chunks = [];
33
+ let total = 0;
31
34
  req.on("data", chunk => {
32
- body += chunk.toString();
33
- if (limit && body.length > limit) {
34
- const error = new Error("Payload Too Large");
35
+ const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
36
+ total += buf.length;
37
+ if (limit && total > limit) {
35
38
  res.status(413);
36
39
  res.FF._413(req, res);
37
40
  req.destroy();
38
- return reject(error);
41
+ return reject(new Error("Payload Too Large"));
39
42
  }
43
+ chunks.push(buf);
40
44
  });
41
45
  req.on("end", () => {
42
- resolve(body);
46
+ resolve(Buffer.concat(chunks).toString());
43
47
  });
44
48
  req.on("error", err => {
45
49
  reject(err);
package/dist/debug.d.ts CHANGED
@@ -3,6 +3,7 @@ import { RouteHandler } from "./types.js";
3
3
  export interface DebugOptions {
4
4
  url?: string;
5
5
  hosts?: string[];
6
+ /** @deprecated */
6
7
  logs?: boolean;
7
8
  }
8
9
  export declare function createDebug(opts?: DebugOptions): Router | RouteHandler;
package/dist/debug.js CHANGED
@@ -5,7 +5,7 @@ export function createDebug(opts = {}) {
5
5
  "localhost",
6
6
  "127.0.0.1",
7
7
  "::1",
8
- ], logs = true, } = opts;
8
+ ], } = opts;
9
9
  if (process.env.NODE_ENV === "production" &&
10
10
  !process.env.FF_DEBUG_PRODUCTION_ENABLED)
11
11
  return (req, res, next) => {
@@ -54,7 +54,5 @@ export function createDebug(opts = {}) {
54
54
  };
55
55
  next();
56
56
  });
57
- if (logs)
58
- router.post("/logs", () => "ok");
59
57
  return router;
60
58
  }
package/dist/req.js CHANGED
@@ -19,17 +19,17 @@ export function handleRequest(req, res, FF, reRoute = false) {
19
19
  const { logger } = FF;
20
20
  try {
21
21
  const [path, params] = (req.url || "").split("?");
22
- const normalizedPath = path.replace(/\/{2,}/g, "/");
22
+ const normalizedPath = decodeURIComponent(path).replace(/\/{2,}/g, "/");
23
23
  const parsedUrl = new URL(normalizedPath + (params ? `?${params}` : ""), "http://localhost");
24
- req.path = decodeURIComponent(parsedUrl.pathname) || "/";
24
+ req.path = parsedUrl.pathname || "/";
25
25
  req.query = Object.fromEntries(parsedUrl.searchParams);
26
+ req.cookies = parseCookies(req.headers.cookie || "");
26
27
  }
27
28
  catch (e) {
28
- logger.error(`Error parsing URL (${req.url}): ${e}`);
29
+ logger.error(`Error parsing request (${req.url}): ${e}`);
29
30
  res.status(400).end("400: Bad request");
30
31
  return;
31
32
  }
32
- req.cookies = parseCookies(req.headers.cookie || "");
33
33
  req.ip = getIP(req);
34
34
  req.params = {};
35
35
  req.valid = (schema, regexRules) => validate({
@@ -152,8 +152,9 @@ export function handleRequest(req, res, FF, reRoute = false) {
152
152
  next();
153
153
  })
154
154
  .catch(() => {
155
- if (!res._ended)
155
+ if (!res._ended) {
156
156
  req.body = {};
157
- next();
157
+ next();
158
+ }
158
159
  });
159
160
  }
package/dist/res.js CHANGED
@@ -54,7 +54,7 @@ export class FFResponse extends http.ServerResponse {
54
54
  cookie += `; Secure`;
55
55
  if (options.sameSite)
56
56
  cookie += `; SameSite=${options.sameSite}`;
57
- this.setHeader("Set-Cookie", cookie);
57
+ this.appendHeader("Set-Cookie", cookie);
58
58
  return this;
59
59
  }
60
60
  clearCookie(name, options = {}) {
package/dist/router.js CHANGED
@@ -63,6 +63,8 @@ export class Router {
63
63
  dirPath = apiPath;
64
64
  apiPath = "/";
65
65
  }
66
+ if (dirPath === "/")
67
+ throw new Error("Cannot serve root directory");
66
68
  this.use(apiPath, handleStaticFiles(dirPath, opts));
67
69
  return this;
68
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",