princejs 1.2.1 → 1.2.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/Readme.md +3 -3
- package/dist/prince.js +36 -26
- package/package.json +1 -1
package/Readme.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# PrinceJS
|
|
2
2
|
|
|
3
3
|
**The fastest backend framework ever made.** <br>
|
|
4
|
-
**Under
|
|
4
|
+
**Under 5 kB • Beats Hono and Express • Built in 3 days** <br>
|
|
5
5
|
|
|
6
6
|
---
|
|
7
7
|
|
|
@@ -57,7 +57,7 @@ app.error(handler) — global error handling
|
|
|
57
57
|
|
|
58
58
|
### Benchmarks (real Windows laptop)
|
|
59
59
|
```bash
|
|
60
|
-
PrinceJS: 24k–
|
|
60
|
+
PrinceJS: 24k–47k requests in 10s
|
|
61
61
|
Hono: 20k–40k requests in 10s
|
|
62
62
|
Express: ~7k requests in 10s
|
|
63
63
|
```
|
|
@@ -65,7 +65,7 @@ Express: ~7k requests in 10s
|
|
|
65
65
|
Same code. Same features. Same machine.
|
|
66
66
|
|
|
67
67
|
### Author
|
|
68
|
-
Matthew Micheal —
|
|
68
|
+
Matthew Micheal — Nigeria
|
|
69
69
|
|
|
70
70
|
Made in PowerShell on a school laptop.
|
|
71
71
|
|
package/dist/prince.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// prince.ts
|
|
3
3
|
class Prince {
|
|
4
|
-
routes =
|
|
4
|
+
routes = Object.create(null);
|
|
5
5
|
middlewares = [];
|
|
6
6
|
use(middleware) {
|
|
7
7
|
this.middlewares.push(middleware);
|
|
8
|
+
return this;
|
|
8
9
|
}
|
|
9
10
|
error(handler) {
|
|
10
11
|
this.use(async (req, next) => {
|
|
@@ -14,6 +15,7 @@ class Prince {
|
|
|
14
15
|
return handler(err, req);
|
|
15
16
|
}
|
|
16
17
|
});
|
|
18
|
+
return this;
|
|
17
19
|
}
|
|
18
20
|
json(data, status = 200) {
|
|
19
21
|
return new Response(JSON.stringify(data), {
|
|
@@ -22,59 +24,67 @@ class Prince {
|
|
|
22
24
|
});
|
|
23
25
|
}
|
|
24
26
|
get(path, handler) {
|
|
25
|
-
this.add("GET", path, handler);
|
|
27
|
+
return this.add("GET", path, handler);
|
|
26
28
|
}
|
|
27
29
|
post(path, handler) {
|
|
28
|
-
this.add("POST", path, handler);
|
|
30
|
+
return this.add("POST", path, handler);
|
|
29
31
|
}
|
|
30
32
|
put(path, handler) {
|
|
31
|
-
this.add("PUT", path, handler);
|
|
33
|
+
return this.add("PUT", path, handler);
|
|
32
34
|
}
|
|
33
35
|
delete(path, handler) {
|
|
34
|
-
this.add("DELETE", path, handler);
|
|
36
|
+
return this.add("DELETE", path, handler);
|
|
35
37
|
}
|
|
36
38
|
patch(path, handler) {
|
|
37
|
-
this.add("PATCH", path, handler);
|
|
39
|
+
return this.add("PATCH", path, handler);
|
|
38
40
|
}
|
|
39
41
|
options(path, handler) {
|
|
40
|
-
this.add("OPTIONS", path, handler);
|
|
42
|
+
return this.add("OPTIONS", path, handler);
|
|
41
43
|
}
|
|
42
44
|
head(path, handler) {
|
|
43
|
-
this.add("HEAD", path, handler);
|
|
45
|
+
return this.add("HEAD", path, handler);
|
|
44
46
|
}
|
|
45
47
|
add(method, path, handler) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this
|
|
48
|
+
this.routes[path] ??= Object.create(null);
|
|
49
|
+
this.routes[path][method] = handler;
|
|
50
|
+
return this;
|
|
49
51
|
}
|
|
50
52
|
listen(port = 3000) {
|
|
51
|
-
|
|
53
|
+
const mw = this.middlewares;
|
|
54
|
+
const mwLen = mw.length;
|
|
55
|
+
const routes = this.routes;
|
|
56
|
+
return Bun.serve({
|
|
52
57
|
port,
|
|
53
58
|
fetch: async (req) => {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
const route = this.routes.get(url.pathname);
|
|
61
|
-
const handler = route?.get(req.method) || route?.get("GET");
|
|
59
|
+
try {
|
|
60
|
+
const url = req.url;
|
|
61
|
+
const path = url.slice(url.indexOf("/", 8));
|
|
62
|
+
const route = routes[path];
|
|
63
|
+
const handler = route?.[req.method] ?? route?.["GET"];
|
|
62
64
|
if (!handler) {
|
|
63
65
|
return this.json({ error: "Route not found" }, 404);
|
|
64
66
|
}
|
|
65
|
-
|
|
67
|
+
if (!mwLen) {
|
|
66
68
|
const result = await handler(req);
|
|
67
69
|
return result instanceof Response ? result : this.json(result);
|
|
68
|
-
} catch (e) {
|
|
69
|
-
throw e;
|
|
70
70
|
}
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
let idx = 0;
|
|
72
|
+
const next = async () => {
|
|
73
|
+
if (idx < mwLen)
|
|
74
|
+
return mw[idx++](req, next);
|
|
75
|
+
const result = await handler(req);
|
|
76
|
+
return result instanceof Response ? result : this.json(result);
|
|
77
|
+
};
|
|
78
|
+
return next();
|
|
79
|
+
} catch (err) {
|
|
80
|
+
return this.json({ error: String(err) }, 500);
|
|
81
|
+
}
|
|
73
82
|
}
|
|
74
83
|
});
|
|
75
|
-
console.log(`PrinceJS v1.0.0 running at http://localhost:${port}`);
|
|
76
84
|
}
|
|
77
85
|
}
|
|
86
|
+
var prince = () => new Prince;
|
|
78
87
|
export {
|
|
88
|
+
prince,
|
|
79
89
|
Prince
|
|
80
90
|
};
|