@server/next 0.20.1 → 0.20.2

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.20.1",
3
+ "version": "0.20.2",
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": "^26.0.1",
35
- "prettier": "^2.7.0"
35
+ "jest": "^29.7.0"
36
36
  },
37
37
  "jest": {
38
38
  "testEnvironment": "jest-environment-node",
@@ -8,8 +8,8 @@ export default function define(obj, key, cb) {
8
8
  get() {
9
9
  const value = cb(obj);
10
10
  Object.defineProperty(obj, key, {
11
- configurable: false,
12
- writable: false,
11
+ configurable: true,
12
+ writable: true,
13
13
  value,
14
14
  });
15
15
  return obj[key];
@@ -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;
@@ -22,6 +22,9 @@ export default async function handleRequest(handlers, ctx) {
22
22
  return new Response(error.message, { status: error.status || 500 });
23
23
  }
24
24
  }
25
+
26
+ // When it's an HTTP method, break free after it's done (which will 404)
27
+ if (method !== "*") break;
25
28
  }
26
29
 
27
30
  return new Response("Not Found", { status: 404 });
package/src/index.js CHANGED
@@ -19,7 +19,16 @@ export default function server(options = {}) {
19
19
 
20
20
  this.platform = getMachine();
21
21
 
22
- this.handlers = {};
22
+ this.handlers = {
23
+ socket: [],
24
+ get: [],
25
+ head: [],
26
+ post: [],
27
+ put: [],
28
+ patch: [],
29
+ delete: [],
30
+ options: [],
31
+ };
23
32
 
24
33
  options.port = options.port || process.env.PORT || 3000;
25
34
 
@@ -59,7 +68,7 @@ export default function server(options = {}) {
59
68
  }
60
69
  response.end();
61
70
  })
62
- .listen(options.port || 3000);
71
+ .listen(options.port);
63
72
  })();
64
73
  }
65
74
 
@@ -74,62 +83,55 @@ export default function server(options = {}) {
74
83
  }
75
84
 
76
85
  // INTERNAL
77
- server.prototype.handle = function (name, ...middleware) {
78
- if (!this.handlers[name]) {
79
- this.handlers[name] = [];
86
+ server.prototype.handle = function (method, path, ...middleware) {
87
+ if (method === "*") {
88
+ for (let m in this.handlers) {
89
+ this.handlers[m].push(["*", path, ...middleware]);
90
+ }
91
+ } else {
92
+ this.handlers[method].push([method, path, ...middleware]);
80
93
  }
81
- this.handlers[name].push(...middleware);
94
+
82
95
  return this;
83
96
  };
84
97
 
85
98
  server.prototype.socket = function (path, ...middleware) {
86
- return this.handle("socket", [path, ...middleware]);
99
+ return this.handle("socket", path, ...middleware);
87
100
  };
88
101
 
89
102
  server.prototype.get = function (path, ...middleware) {
90
- return this.handle("get", [path, ...middleware]);
103
+ return this.handle("get", path, ...middleware);
91
104
  };
92
105
 
93
106
  server.prototype.head = function (path, ...middleware) {
94
- return this.handle("head", [path, ...middleware]);
107
+ return this.handle("head", path, ...middleware);
95
108
  };
96
109
 
97
110
  server.prototype.post = function (path, ...middleware) {
98
- return this.handle("post", [path, ...middleware]);
111
+ return this.handle("post", path, ...middleware);
99
112
  };
100
113
 
101
114
  server.prototype.put = function (path, ...middleware) {
102
- return this.handle("put", [path, ...middleware]);
115
+ return this.handle("put", path, ...middleware);
103
116
  };
104
117
 
105
118
  server.prototype.patch = function (path, ...middleware) {
106
- return this.handle("patch", [path, ...middleware]);
119
+ return this.handle("patch", path, ...middleware);
107
120
  };
108
121
 
109
122
  server.prototype.del = function (path, ...middleware) {
110
- return this.handle("del", [path, ...middleware]);
123
+ return this.handle("del", path, ...middleware);
111
124
  };
112
125
 
113
126
  server.prototype.options = function (path, ...middleware) {
114
- return this.handle("options", [path, ...middleware]);
127
+ return this.handle("options", path, ...middleware);
115
128
  };
116
129
 
117
130
  server.prototype.use = function (...middleware) {
118
- let path = "*";
119
- if (typeof middleware[0] === "string" || middleware[0] instanceof RegExp) {
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) {
@@ -1,4 +1,5 @@
1
1
  export default function pathPattern(pattern, path) {
2
+ pattern = "/" + pattern.replace(/^\//, "");
2
3
  pattern = pattern.replace(/\/$/, "") || "/";
3
4
  path = path.replace(/\/$/, "") || "/";
4
5
 
@@ -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
+ });