@server/next 0.21.0 → 0.21.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 +3 -2
- package/src/helpers/handleRequest.js +0 -12
- package/src/index.js +10 -4
- package/src/middle/assets.js +2 -0
- package/src/middle/index.js +1 -9
- package/src/router.test.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@server/next",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
|
|
5
5
|
"homepage": "https://server-js.com/",
|
|
6
6
|
"repository": "https://github.com/franciscop/server-next.git",
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
"license": "UNLICENSED",
|
|
11
11
|
"documentation": {
|
|
12
12
|
"title": "Server JS - A modern web server for Bun and Node.js",
|
|
13
|
+
"home": "./docs/index.html",
|
|
13
14
|
"menu": {
|
|
14
|
-
"About": "/
|
|
15
|
+
"About": "/",
|
|
15
16
|
"Documentation": "/documentation",
|
|
16
17
|
"Github": "https://github.com/franciscop/server-next"
|
|
17
18
|
}
|
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
import middle from "../middle/index.js";
|
|
2
1
|
import parseResponse from "../parseResponse.js";
|
|
3
2
|
import pathPattern from "../pathPattern.js";
|
|
4
3
|
import define from "./define.js";
|
|
5
4
|
import validate from "./validate.js";
|
|
6
5
|
|
|
7
|
-
const extendWithDefaults = (ctx) => {
|
|
8
|
-
// TODO: find a better way of doing this FFS
|
|
9
|
-
// Only want to execute it once; it needs to happen on a per-request
|
|
10
|
-
// basis since we only have full access to the options there
|
|
11
|
-
if (ctx.app.extended) return;
|
|
12
|
-
middle(ctx);
|
|
13
|
-
ctx.app.extended = true;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
6
|
export default async function handleRequest(handlers, ctx) {
|
|
17
|
-
extendWithDefaults(ctx);
|
|
18
|
-
|
|
19
7
|
for (let [method, matcher, ...cbs] of handlers[ctx.method]) {
|
|
20
8
|
const match = pathPattern(matcher, ctx.url.pathname || "/");
|
|
21
9
|
// Skip this whole middleware if there was no match
|
package/src/index.js
CHANGED
|
@@ -11,6 +11,8 @@ import {
|
|
|
11
11
|
parseHeaders,
|
|
12
12
|
} from "./helpers/index.js";
|
|
13
13
|
|
|
14
|
+
import middle from "./middle/index.js";
|
|
15
|
+
|
|
14
16
|
// Export the reply helpers
|
|
15
17
|
export * from "./reply.js";
|
|
16
18
|
|
|
@@ -71,6 +73,8 @@ export default function server(options = {}) {
|
|
|
71
73
|
if (this.platform.runtime === "node") {
|
|
72
74
|
this.node();
|
|
73
75
|
}
|
|
76
|
+
|
|
77
|
+
this.use(...middle);
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
server.prototype.self = function () {
|
|
@@ -146,6 +150,8 @@ server.prototype.fetch = async function (request, env) {
|
|
|
146
150
|
// #region HTTP methods
|
|
147
151
|
// INTERNAL
|
|
148
152
|
server.prototype.handle = function (method, path, ...middleware) {
|
|
153
|
+
// Do not try to optimize, we NEED the method to remain '*' here so that
|
|
154
|
+
// it doesn't auto-finish
|
|
149
155
|
if (method === "*") {
|
|
150
156
|
for (let m in this.handlers) {
|
|
151
157
|
this.handlers[m].push([method, path, ...middleware]);
|
|
@@ -224,17 +230,17 @@ server.prototype.test = function () {
|
|
|
224
230
|
);
|
|
225
231
|
|
|
226
232
|
const headers = parseHeaders(res.headers);
|
|
227
|
-
let
|
|
233
|
+
let body;
|
|
228
234
|
if (headers["set-cookie"]) {
|
|
229
235
|
// TODO: this should really be a smart merge of the 2
|
|
230
236
|
cookie = headers["set-cookie"];
|
|
231
237
|
}
|
|
232
238
|
if (headers["content-type"]?.includes("application/json")) {
|
|
233
|
-
|
|
239
|
+
body = await res.json();
|
|
234
240
|
} else {
|
|
235
|
-
|
|
241
|
+
body = await res.text();
|
|
236
242
|
}
|
|
237
|
-
return { status: res.status, headers, data };
|
|
243
|
+
return { status: res.status, headers, body, data: body };
|
|
238
244
|
};
|
|
239
245
|
return {
|
|
240
246
|
app: this,
|
package/src/middle/assets.js
CHANGED
package/src/middle/index.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
import auth from "../auth/index.js";
|
|
2
2
|
import assets from "./assets.js";
|
|
3
3
|
|
|
4
|
-
export default
|
|
5
|
-
// Serve assets
|
|
6
|
-
if (ctx.options.public) {
|
|
7
|
-
// We need these before other endpoints
|
|
8
|
-
ctx.app.handlers.get.unshift(["*", "*", assets]);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
await auth.middle(ctx);
|
|
12
|
-
}
|
|
4
|
+
export default [assets, auth.middle];
|
package/src/router.test.js
CHANGED
|
@@ -15,7 +15,7 @@ describe("can route properly", () => {
|
|
|
15
15
|
// INTERNAL - so this might change in the future
|
|
16
16
|
it("has the correct structure", () => {
|
|
17
17
|
const registeredPaths = app.handlers.get.map((h) => h[1]);
|
|
18
|
-
expect(registeredPaths).toEqual(["/hello", "/api/hello", "/"]);
|
|
18
|
+
expect(registeredPaths).toEqual(["*", "/hello", "/api/hello", "/"]);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it("can get fallback when nothing matches", async () => {
|