@server/next 0.20.3 → 0.20.5
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/index.js +3 -5
- package/src/reply.js +15 -3
- package/src/router.js +20 -14
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -137,11 +137,9 @@ server.prototype.use = function (...middleware) {
|
|
|
137
137
|
server.prototype.router = function (basePath, router) {
|
|
138
138
|
basePath = "/" + basePath.replace(/^\//, "").replace(/\/$/, "") + "/";
|
|
139
139
|
for (const method in router.handlers) {
|
|
140
|
-
|
|
141
|
-
basePath + path.replace(/^\//, ""),
|
|
142
|
-
|
|
143
|
-
]);
|
|
144
|
-
this.handle(method, ...handlers);
|
|
140
|
+
router.handlers[method].forEach(([method, path, ...callbacks]) => {
|
|
141
|
+
this.handle(method, basePath + path.replace(/^\//, ""), ...callbacks);
|
|
142
|
+
});
|
|
145
143
|
}
|
|
146
144
|
return this;
|
|
147
145
|
};
|
package/src/reply.js
CHANGED
|
@@ -57,9 +57,16 @@ Reply.prototype.json = function (body) {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
Reply.prototype.file = async function (path) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
try {
|
|
61
|
+
const data = await fs.readFile(path);
|
|
62
|
+
const ext = path.split(".").pop();
|
|
63
|
+
return this.type(ext).send(data);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error.code === "ENOENT") {
|
|
66
|
+
return status(404).send();
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
63
70
|
};
|
|
64
71
|
|
|
65
72
|
Reply.prototype.view = async function (path) {
|
|
@@ -87,6 +94,11 @@ Reply.prototype.send = function (body = "") {
|
|
|
87
94
|
return new Response(body, { status, headers });
|
|
88
95
|
}
|
|
89
96
|
|
|
97
|
+
if (body?.constructor?.name === "Buffer") {
|
|
98
|
+
const headers = this.generateHeaders();
|
|
99
|
+
return new Response(body, { status, headers });
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
// This is a bit loopy, send({}) => json({}) => send('{}')
|
|
91
103
|
return this.json(body);
|
|
92
104
|
};
|
package/src/router.js
CHANGED
|
@@ -2,46 +2,52 @@ export default function router() {
|
|
|
2
2
|
if (!(this instanceof router)) {
|
|
3
3
|
return new router();
|
|
4
4
|
}
|
|
5
|
-
this.handlers = {
|
|
5
|
+
this.handlers = {
|
|
6
|
+
socket: [],
|
|
7
|
+
get: [],
|
|
8
|
+
head: [],
|
|
9
|
+
post: [],
|
|
10
|
+
put: [],
|
|
11
|
+
patch: [],
|
|
12
|
+
delete: [],
|
|
13
|
+
options: [],
|
|
14
|
+
};
|
|
6
15
|
}
|
|
7
16
|
|
|
8
17
|
// INTERNAL
|
|
9
|
-
router.prototype.handle = function (
|
|
10
|
-
|
|
11
|
-
this.handlers[name] = [];
|
|
12
|
-
}
|
|
13
|
-
this.handlers[name].push(middleware);
|
|
18
|
+
router.prototype.handle = function (method, path, ...middleware) {
|
|
19
|
+
this.handlers[method].push([method, path, ...middleware]);
|
|
14
20
|
return this;
|
|
15
21
|
};
|
|
16
22
|
|
|
17
23
|
router.prototype.socket = function (path, ...middleware) {
|
|
18
|
-
return this.handle("socket",
|
|
24
|
+
return this.handle("socket", path, ...middleware);
|
|
19
25
|
};
|
|
20
26
|
|
|
21
27
|
router.prototype.get = function (path, ...middleware) {
|
|
22
|
-
return this.handle("get",
|
|
28
|
+
return this.handle("get", path, ...middleware);
|
|
23
29
|
};
|
|
24
30
|
|
|
25
31
|
router.prototype.head = function (path, ...middleware) {
|
|
26
|
-
return this.handle("head",
|
|
32
|
+
return this.handle("head", path, ...middleware);
|
|
27
33
|
};
|
|
28
34
|
|
|
29
35
|
router.prototype.post = function (path, ...middleware) {
|
|
30
|
-
return this.handle("post",
|
|
36
|
+
return this.handle("post", path, ...middleware);
|
|
31
37
|
};
|
|
32
38
|
|
|
33
39
|
router.prototype.put = function (path, ...middleware) {
|
|
34
|
-
return this.handle("put",
|
|
40
|
+
return this.handle("put", path, ...middleware);
|
|
35
41
|
};
|
|
36
42
|
|
|
37
43
|
router.prototype.patch = function (path, ...middleware) {
|
|
38
|
-
return this.handle("patch",
|
|
44
|
+
return this.handle("patch", path, ...middleware);
|
|
39
45
|
};
|
|
40
46
|
|
|
41
47
|
router.prototype.del = function (path, ...middleware) {
|
|
42
|
-
return this.handle("del",
|
|
48
|
+
return this.handle("del", path, ...middleware);
|
|
43
49
|
};
|
|
44
50
|
|
|
45
51
|
router.prototype.options = function (path, ...middleware) {
|
|
46
|
-
return this.handle("options",
|
|
52
|
+
return this.handle("options", path, ...middleware);
|
|
47
53
|
};
|