@server/next 0.20.2 → 0.20.4
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/helpers/handleRequest.js +2 -1
- package/src/index.js +17 -19
- package/src/parseResponse.js +1 -3
- package/src/reply.js +5 -3
- package/src/router.js +20 -14
package/package.json
CHANGED
|
@@ -15,7 +15,8 @@ export default async function handleRequest(handlers, ctx) {
|
|
|
15
15
|
try {
|
|
16
16
|
validate(ctx, cb);
|
|
17
17
|
if (typeof cb === "function") {
|
|
18
|
-
const
|
|
18
|
+
const res = await cb(ctx);
|
|
19
|
+
const out = await parseResponse(res, ctx);
|
|
19
20
|
if (out) return out;
|
|
20
21
|
}
|
|
21
22
|
} catch (error) {
|
package/src/index.js
CHANGED
|
@@ -17,8 +17,7 @@ export default function server(options = {}) {
|
|
|
17
17
|
return new server(options);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
// Skip "forbidden methods" https://fetch.spec.whatwg.org/#concept-method
|
|
22
21
|
this.handlers = {
|
|
23
22
|
socket: [],
|
|
24
23
|
get: [],
|
|
@@ -30,33 +29,33 @@ export default function server(options = {}) {
|
|
|
30
29
|
options: [],
|
|
31
30
|
};
|
|
32
31
|
|
|
32
|
+
const platform = getMachine();
|
|
33
33
|
options.port = options.port || process.env.PORT || 3000;
|
|
34
34
|
|
|
35
35
|
options.views = options.views ? Bucket(options.views) : null;
|
|
36
36
|
options.public = options.public ? Bucket(options.public) : null;
|
|
37
37
|
options.uploads = options.uploads ? Bucket(options.uploads) : null;
|
|
38
38
|
|
|
39
|
-
this.options = options;
|
|
40
|
-
|
|
41
39
|
// WEBSOCKETS stuff
|
|
42
|
-
|
|
40
|
+
const sockets = [];
|
|
43
41
|
this.websocket = {
|
|
44
|
-
message: async (
|
|
42
|
+
message: async (socket, body) => {
|
|
45
43
|
this.handlers.socket
|
|
46
44
|
?.filter((s) => s[0] === "message")
|
|
47
|
-
?.map((s) => s[1]({ socket
|
|
45
|
+
?.map((s) => s[1]({ socket, sockets, body }));
|
|
48
46
|
},
|
|
49
|
-
open: (ws) =>
|
|
50
|
-
close: (ws) =>
|
|
47
|
+
open: (ws) => sockets.push(ws),
|
|
48
|
+
close: (ws) => sockets.splice(sockets.indexOf(ws), 1),
|
|
51
49
|
};
|
|
52
50
|
|
|
53
|
-
if (
|
|
51
|
+
if (platform.runtime === "node") {
|
|
54
52
|
(async () => {
|
|
55
53
|
const http = await import("http");
|
|
56
54
|
http
|
|
57
55
|
.createServer(async (request, response) => {
|
|
58
56
|
const ctx = await createNodeContext(request, options);
|
|
59
|
-
ctx.
|
|
57
|
+
ctx.app = this;
|
|
58
|
+
ctx.platform = platform;
|
|
60
59
|
|
|
61
60
|
const out = await handleRequest(this.handlers, ctx);
|
|
62
61
|
|
|
@@ -75,8 +74,9 @@ export default function server(options = {}) {
|
|
|
75
74
|
this.fetch = async (request, env, fetchCtx) => {
|
|
76
75
|
if (env?.upgrade(request)) return;
|
|
77
76
|
|
|
78
|
-
const ctx = await createWinterContext(request, options
|
|
79
|
-
ctx.
|
|
77
|
+
const ctx = await createWinterContext(request, options);
|
|
78
|
+
ctx.app = this;
|
|
79
|
+
ctx.platform = platform;
|
|
80
80
|
|
|
81
81
|
return await handleRequest(this.handlers, ctx);
|
|
82
82
|
};
|
|
@@ -86,7 +86,7 @@ export default function server(options = {}) {
|
|
|
86
86
|
server.prototype.handle = function (method, path, ...middleware) {
|
|
87
87
|
if (method === "*") {
|
|
88
88
|
for (let m in this.handlers) {
|
|
89
|
-
this.handlers[m].push([
|
|
89
|
+
this.handlers[m].push([method, path, ...middleware]);
|
|
90
90
|
}
|
|
91
91
|
} else {
|
|
92
92
|
this.handlers[method].push([method, path, ...middleware]);
|
|
@@ -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/parseResponse.js
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { json } from "./reply.js";
|
|
4
4
|
|
|
5
|
-
export default async function parseResponse(
|
|
6
|
-
let out = await handler(ctx);
|
|
7
|
-
|
|
5
|
+
export default async function parseResponse(out, ctx) {
|
|
8
6
|
// undefined || null || 0 || false || ~""~ -> empty string is still 200
|
|
9
7
|
if (!out && typeof out !== "string") return null;
|
|
10
8
|
|
package/src/reply.js
CHANGED
|
@@ -57,9 +57,11 @@ Reply.prototype.json = function (body) {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
Reply.prototype.file = async function (path) {
|
|
60
|
-
const data = await fs.readFile(path
|
|
61
|
-
if (data) return
|
|
62
|
-
|
|
60
|
+
const data = await fs.readFile(path);
|
|
61
|
+
if (!data) return status(404).send();
|
|
62
|
+
|
|
63
|
+
this.type(path.split(".").pop());
|
|
64
|
+
return new Response(data, { status: 200, headers: this.res.headers });
|
|
63
65
|
};
|
|
64
66
|
|
|
65
67
|
Reply.prototype.view = async function (path) {
|
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
|
};
|