@ndiinginc/router 4.16.0 → 4.16.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndiinginc/router",
3
- "version": "4.16.0",
3
+ "version": "4.16.1",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/response.js CHANGED
@@ -3,39 +3,66 @@ const fs = require("fs");
3
3
  const path = require("path");
4
4
 
5
5
  class Response extends http.ServerResponse {
6
- status(code) {
7
- this.statusCode = code;
6
+ /**
7
+ * @param {number} statusCode
8
+ * @returns {this}
9
+ */
10
+ status(statusCode = 200) {
11
+ this.statusCode = statusCode;
8
12
  return this;
9
13
  }
10
- send(chunk) {
11
- this.setHeader("Content-Type", "text/plain; charset=utf-8");
12
- this.end(chunk);
14
+
15
+ /**
16
+ * @param {string} data
17
+ */
18
+ send(data) {
19
+ this.writeHead(this.statusCode, {
20
+ "Content-Type": "text/html",
21
+ "Content-Length": Buffer.byteLength(data),
22
+ });
23
+ this.end(data);
13
24
  }
14
25
 
15
- json(value) {
16
- this.setHeader("Content-Type", "application/json; charset=utf-8");
17
- this.end(JSON.stringify(value));
26
+ /**
27
+ * @param {object} data
28
+ */
29
+ json(data) {
30
+ const body = JSON.stringify(data);
31
+ this.writeHead(this.statusCode, {
32
+ "Content-Type": "application/json",
33
+ "Content-Length": Buffer.byteLength(body),
34
+ });
35
+ this.end(body);
18
36
  }
19
37
 
20
- download(filepath, filename) {
21
- fs.stat(filepath, (err, stats) => {
22
- if (err || !stats.isFile()) {
23
- this.statusCode = 404;
24
- return this.end();
38
+ /**
39
+ * @param {string} filePath
40
+ * @param {string} filename
41
+ */
42
+ download(filePath, filename) {
43
+ if (!fs.existsSync(filePath)) {
44
+ this.writeHead(404, { "Content-Type": "text/plain" });
45
+ return this.end();
46
+ }
47
+
48
+ const stat = fs.statSync(filePath);
49
+ const basename = filename || path.basename(filePath);
50
+
51
+ this.writeHead(200, {
52
+ "Content-Type": "application/octet-stream",
53
+ "Content-Length": stat.size,
54
+ "Content-Disposition": `attachment; filename="${basename}"`,
55
+ });
56
+
57
+ const stream = fs.createReadStream(filePath);
58
+ stream.on("error", (err) => {
59
+ if (!this.headersSent) {
60
+ this.writeHead(500, { "Content-Type": "text/plain" });
25
61
  }
26
- filename=filename||path.basename(filepath)
27
- this.setHeader("Content-Type", "application/octet-stream");
28
- this.setHeader("Content-Disposition", `attachment; filename="${filename}"`);
29
- this.setHeader("Content-Length", stats.size);
30
- const stream = fs.createReadStream(filepath);
31
- stream.on("error", (err) => {
32
- if (!this.headersSent) {
33
- this.statusCode = 500;
34
- this.end();
35
- }
36
- });
37
- stream.pipe(this);
62
+ this.end();
38
63
  });
64
+
65
+ stream.pipe(this);
39
66
  }
40
67
  }
41
68
  exports.Response = Response;
package/src/router.js CHANGED
@@ -244,10 +244,10 @@ class Router {
244
244
  }
245
245
  if (node.params.size) {
246
246
  for (const [, child] of node.params) {
247
- const index = path.indexOf("/");
248
- const value = index === -1 ? path : path.slice(0, index);
247
+ const slashIdx = path.indexOf("/");
248
+ const value = slashIdx === -1 ? path : path.slice(0, slashIdx);
249
249
  if (value.length) {
250
- const rest = index === -1 ? "" : path.slice(index);
250
+ const rest = slashIdx === -1 ? "" : path.slice(slashIdx);
251
251
  const next = { ...params, [child.name]: value };
252
252
  const result = this.search(method, rest, child, nodes, next, state);
253
253
  if (result.matched) return result;