@ndiinginc/router 4.16.0 → 4.16.2
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 +1 -1
- package/src/response.js +46 -25
- package/src/router.js +3 -3
package/package.json
CHANGED
package/src/response.js
CHANGED
|
@@ -3,39 +3,60 @@ const fs = require("fs");
|
|
|
3
3
|
const path = require("path");
|
|
4
4
|
|
|
5
5
|
class Response extends http.ServerResponse {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @param {number} statusCode
|
|
8
|
+
* @returns {this}
|
|
9
|
+
*/
|
|
10
|
+
status(statusCode = 200) {
|
|
11
|
+
this.statusCode = statusCode;
|
|
8
12
|
return this;
|
|
9
13
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {string} data
|
|
17
|
+
*/
|
|
18
|
+
send(data) {
|
|
19
|
+
this.setHeader("Content-Type", "text/html");
|
|
20
|
+
this.setHeader("Content-Length", Buffer.byteLength(data));
|
|
21
|
+
this.end(data);
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
/**
|
|
25
|
+
* @param {object} data
|
|
26
|
+
*/
|
|
27
|
+
json(data) {
|
|
28
|
+
const body = JSON.stringify(data);
|
|
29
|
+
this.setHeader("Content-Type", "application/json");
|
|
30
|
+
this.setHeader("Content-Length", Buffer.byteLength(body));
|
|
31
|
+
this.end(body);
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
/**
|
|
35
|
+
* @param {string} filePath
|
|
36
|
+
* @param {string} filename
|
|
37
|
+
*/
|
|
38
|
+
download(filePath, filename) {
|
|
39
|
+
if (!fs.existsSync(filePath)) {
|
|
40
|
+
this.status(404);
|
|
41
|
+
return this.end();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const stat = fs.statSync(filePath);
|
|
45
|
+
const basename = filename || path.basename(filePath);
|
|
46
|
+
|
|
47
|
+
this.setHeader("Content-Type", "application/octet-stream");
|
|
48
|
+
this.setHeader("Content-Length", stat.size);
|
|
49
|
+
this.setHeader("Content-Disposition", `attachment; filename="${basename}"`);
|
|
50
|
+
|
|
51
|
+
const stream = fs.createReadStream(filePath);
|
|
52
|
+
stream.on("error", (err) => {
|
|
53
|
+
if (!this.headersSent) {
|
|
54
|
+
this.status(500);
|
|
25
55
|
}
|
|
26
|
-
|
|
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);
|
|
56
|
+
this.end();
|
|
38
57
|
});
|
|
58
|
+
|
|
59
|
+
stream.pipe(this);
|
|
39
60
|
}
|
|
40
61
|
}
|
|
41
62
|
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
|
|
248
|
-
const value =
|
|
247
|
+
const slashIdx = path.indexOf("/");
|
|
248
|
+
const value = slashIdx === -1 ? path : path.slice(0, slashIdx);
|
|
249
249
|
if (value.length) {
|
|
250
|
-
const rest =
|
|
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;
|