@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.21.0",
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": "/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 data;
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
- data = await res.json();
239
+ body = await res.json();
234
240
  } else {
235
- data = await res.text();
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,
@@ -1,6 +1,8 @@
1
1
  import { type } from "../reply.js";
2
2
 
3
3
  export default async function assets(ctx) {
4
+ if (!ctx.options.public) return;
5
+ if (!ctx.method !== "get") return;
4
6
  try {
5
7
  // TODO: streaming
6
8
  // Read it as buffer (null)
@@ -1,12 +1,4 @@
1
1
  import auth from "../auth/index.js";
2
2
  import assets from "./assets.js";
3
3
 
4
- export default async function middle(ctx) {
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];
@@ -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 () => {