@server/next 0.20.1 → 0.20.3
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 +4 -4
- package/src/helpers/define.js +2 -2
- package/src/helpers/handleRequest.js +6 -2
- package/src/index.js +43 -41
- package/src/parseResponse.js +1 -3
- package/src/pathPattern.js +1 -0
- package/src/url.test.js +24 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@server/next",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.3",
|
|
4
4
|
"description": "An experimental reimplementation of server.js focused on the DX",
|
|
5
5
|
"homepage": "https://node-server.com/",
|
|
6
6
|
"repository": "https://github.com/franciscop/server-next.git",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"demo": "nodemon ./demo/app.js",
|
|
13
13
|
"start": "bun test --watch",
|
|
14
|
-
"test": "bun test"
|
|
14
|
+
"test": "bun test",
|
|
15
|
+
"test:jest": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [
|
|
17
18
|
"server",
|
|
@@ -31,8 +32,7 @@
|
|
|
31
32
|
"engineStrict": true,
|
|
32
33
|
"dependencies": {},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"jest": "^
|
|
35
|
-
"prettier": "^2.7.0"
|
|
35
|
+
"jest": "^29.7.0"
|
|
36
36
|
},
|
|
37
37
|
"jest": {
|
|
38
38
|
"testEnvironment": "jest-environment-node",
|
package/src/helpers/define.js
CHANGED
|
@@ -4,7 +4,7 @@ import define from "./define.js";
|
|
|
4
4
|
import validate from "./validate.js";
|
|
5
5
|
|
|
6
6
|
export default async function handleRequest(handlers, ctx) {
|
|
7
|
-
for (let [matcher, ...cbs] of handlers[ctx.method]) {
|
|
7
|
+
for (let [method, matcher, ...cbs] of handlers[ctx.method]) {
|
|
8
8
|
const match = pathPattern(matcher, ctx.url.pathname || "/");
|
|
9
9
|
// Skip this whole middleware if there was no match
|
|
10
10
|
if (!match) continue;
|
|
@@ -15,13 +15,17 @@ 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) {
|
|
22
23
|
return new Response(error.message, { status: error.status || 500 });
|
|
23
24
|
}
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
// When it's an HTTP method, break free after it's done (which will 404)
|
|
28
|
+
if (method !== "*") break;
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
return new Response("Not Found", { status: 404 });
|
package/src/index.js
CHANGED
|
@@ -17,37 +17,45 @@ export default function server(options = {}) {
|
|
|
17
17
|
return new server(options);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// Skip "forbidden methods" https://fetch.spec.whatwg.org/#concept-method
|
|
21
|
+
this.handlers = {
|
|
22
|
+
socket: [],
|
|
23
|
+
get: [],
|
|
24
|
+
head: [],
|
|
25
|
+
post: [],
|
|
26
|
+
put: [],
|
|
27
|
+
patch: [],
|
|
28
|
+
delete: [],
|
|
29
|
+
options: [],
|
|
30
|
+
};
|
|
23
31
|
|
|
32
|
+
const platform = getMachine();
|
|
24
33
|
options.port = options.port || process.env.PORT || 3000;
|
|
25
34
|
|
|
26
35
|
options.views = options.views ? Bucket(options.views) : null;
|
|
27
36
|
options.public = options.public ? Bucket(options.public) : null;
|
|
28
37
|
options.uploads = options.uploads ? Bucket(options.uploads) : null;
|
|
29
38
|
|
|
30
|
-
this.options = options;
|
|
31
|
-
|
|
32
39
|
// WEBSOCKETS stuff
|
|
33
|
-
|
|
40
|
+
const sockets = [];
|
|
34
41
|
this.websocket = {
|
|
35
|
-
message: async (
|
|
42
|
+
message: async (socket, body) => {
|
|
36
43
|
this.handlers.socket
|
|
37
44
|
?.filter((s) => s[0] === "message")
|
|
38
|
-
?.map((s) => s[1]({ socket
|
|
45
|
+
?.map((s) => s[1]({ socket, sockets, body }));
|
|
39
46
|
},
|
|
40
|
-
open: (ws) =>
|
|
41
|
-
close: (ws) =>
|
|
47
|
+
open: (ws) => sockets.push(ws),
|
|
48
|
+
close: (ws) => sockets.splice(sockets.indexOf(ws), 1),
|
|
42
49
|
};
|
|
43
50
|
|
|
44
|
-
if (
|
|
51
|
+
if (platform.runtime === "node") {
|
|
45
52
|
(async () => {
|
|
46
53
|
const http = await import("http");
|
|
47
54
|
http
|
|
48
55
|
.createServer(async (request, response) => {
|
|
49
56
|
const ctx = await createNodeContext(request, options);
|
|
50
|
-
ctx.
|
|
57
|
+
ctx.app = this;
|
|
58
|
+
ctx.platform = platform;
|
|
51
59
|
|
|
52
60
|
const out = await handleRequest(this.handlers, ctx);
|
|
53
61
|
|
|
@@ -59,77 +67,71 @@ export default function server(options = {}) {
|
|
|
59
67
|
}
|
|
60
68
|
response.end();
|
|
61
69
|
})
|
|
62
|
-
.listen(options.port
|
|
70
|
+
.listen(options.port);
|
|
63
71
|
})();
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
this.fetch = async (request, env, fetchCtx) => {
|
|
67
75
|
if (env?.upgrade(request)) return;
|
|
68
76
|
|
|
69
|
-
const ctx = await createWinterContext(request, options
|
|
70
|
-
ctx.
|
|
77
|
+
const ctx = await createWinterContext(request, options);
|
|
78
|
+
ctx.app = this;
|
|
79
|
+
ctx.platform = platform;
|
|
71
80
|
|
|
72
81
|
return await handleRequest(this.handlers, ctx);
|
|
73
82
|
};
|
|
74
83
|
}
|
|
75
84
|
|
|
76
85
|
// INTERNAL
|
|
77
|
-
server.prototype.handle = function (
|
|
78
|
-
if (
|
|
79
|
-
this.handlers
|
|
86
|
+
server.prototype.handle = function (method, path, ...middleware) {
|
|
87
|
+
if (method === "*") {
|
|
88
|
+
for (let m in this.handlers) {
|
|
89
|
+
this.handlers[m].push([method, path, ...middleware]);
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
this.handlers[method].push([method, path, ...middleware]);
|
|
80
93
|
}
|
|
81
|
-
|
|
94
|
+
|
|
82
95
|
return this;
|
|
83
96
|
};
|
|
84
97
|
|
|
85
98
|
server.prototype.socket = function (path, ...middleware) {
|
|
86
|
-
return this.handle("socket",
|
|
99
|
+
return this.handle("socket", path, ...middleware);
|
|
87
100
|
};
|
|
88
101
|
|
|
89
102
|
server.prototype.get = function (path, ...middleware) {
|
|
90
|
-
return this.handle("get",
|
|
103
|
+
return this.handle("get", path, ...middleware);
|
|
91
104
|
};
|
|
92
105
|
|
|
93
106
|
server.prototype.head = function (path, ...middleware) {
|
|
94
|
-
return this.handle("head",
|
|
107
|
+
return this.handle("head", path, ...middleware);
|
|
95
108
|
};
|
|
96
109
|
|
|
97
110
|
server.prototype.post = function (path, ...middleware) {
|
|
98
|
-
return this.handle("post",
|
|
111
|
+
return this.handle("post", path, ...middleware);
|
|
99
112
|
};
|
|
100
113
|
|
|
101
114
|
server.prototype.put = function (path, ...middleware) {
|
|
102
|
-
return this.handle("put",
|
|
115
|
+
return this.handle("put", path, ...middleware);
|
|
103
116
|
};
|
|
104
117
|
|
|
105
118
|
server.prototype.patch = function (path, ...middleware) {
|
|
106
|
-
return this.handle("patch",
|
|
119
|
+
return this.handle("patch", path, ...middleware);
|
|
107
120
|
};
|
|
108
121
|
|
|
109
122
|
server.prototype.del = function (path, ...middleware) {
|
|
110
|
-
return this.handle("del",
|
|
123
|
+
return this.handle("del", path, ...middleware);
|
|
111
124
|
};
|
|
112
125
|
|
|
113
126
|
server.prototype.options = function (path, ...middleware) {
|
|
114
|
-
return this.handle("options",
|
|
127
|
+
return this.handle("options", path, ...middleware);
|
|
115
128
|
};
|
|
116
129
|
|
|
117
130
|
server.prototype.use = function (...middleware) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
path = middleware.shift();
|
|
131
|
+
if (typeof middleware[0] === "string") {
|
|
132
|
+
return this.handle("*", ...middleware);
|
|
121
133
|
}
|
|
122
|
-
|
|
123
|
-
this.handle("socket", [path, ...middleware]);
|
|
124
|
-
this.handle("get", [path, ...middleware]);
|
|
125
|
-
this.handle("head", [path, ...middleware]);
|
|
126
|
-
this.handle("post", [path, ...middleware]);
|
|
127
|
-
this.handle("put", [path, ...middleware]);
|
|
128
|
-
this.handle("patch", [path, ...middleware]);
|
|
129
|
-
this.handle("del", [path, ...middleware]);
|
|
130
|
-
this.handle("options", [path, ...middleware]);
|
|
131
|
-
|
|
132
|
-
return this;
|
|
134
|
+
return this.handle("*", "*", ...middleware);
|
|
133
135
|
};
|
|
134
136
|
|
|
135
137
|
server.prototype.router = function (basePath, router) {
|
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/pathPattern.js
CHANGED
package/src/url.test.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import server from "./index.js";
|
|
2
|
+
|
|
3
|
+
describe("can match the url", () => {
|
|
4
|
+
it("stops at the first matching route", async () => {
|
|
5
|
+
const app = server()
|
|
6
|
+
.get("/:id", (ctx) => ctx.url.params)
|
|
7
|
+
.get("/*", (ctx) => ctx.url.params);
|
|
8
|
+
|
|
9
|
+
const res = await app.fetch(new Request("http://localhost:3000/hello"));
|
|
10
|
+
expect(await res.json()).toEqual({ id: "hello" });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("but it doesn't if it's a use", async () => {
|
|
14
|
+
const app = server()
|
|
15
|
+
.use(() => {
|
|
16
|
+
// No-op
|
|
17
|
+
})
|
|
18
|
+
.get("/:id", (ctx) => ctx.url.params)
|
|
19
|
+
.get("/*", (ctx) => ctx.url.params);
|
|
20
|
+
|
|
21
|
+
const res = await app.fetch(new Request("http://localhost:3000/hello"));
|
|
22
|
+
expect(await res.json()).toEqual({ id: "hello" });
|
|
23
|
+
});
|
|
24
|
+
});
|