diesel-core 0.0.21 → 0.0.22
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/CONTRIBUTING.md +2 -2
- package/LICENSE +8 -8
- package/README.md +276 -275
- package/build.js +32 -32
- package/dist/ctx.d.ts +1 -0
- package/dist/ctx.js +1 -1
- package/dist/handleRequest.d.ts +1 -0
- package/dist/handleRequest.js +1 -1
- package/dist/main.d.ts +1 -0
- package/dist/main.js +1 -1
- package/dist/route.d.ts +0 -11
- package/dist/types.d.ts +1 -3
- package/dist/utils.js +1 -1
- package/example/bun.lockb +0 -0
- package/example/index.html +13 -13
- package/example/main.ts +86 -75
- package/example/package-lock.json +177 -177
- package/example/package.json +21 -21
- package/example/route.ts +39 -39
- package/example/test.js +27 -27
- package/example/tester.js +88 -88
- package/example/tsconfig.json +16 -16
- package/index.d.ts +5 -5
- package/index.js +8 -9
- package/jsconfig.json +28 -28
- package/package.json +1 -1
- package/src/ctx.ts +251 -252
- package/src/handleRequest.ts +144 -144
- package/src/main.ts +361 -358
- package/src/trie.ts +148 -148
- package/src/types.ts +157 -157
- package/src/utils.ts +70 -70
- package/test/README.md +15 -15
- package/test/bun.lockb +0 -0
- package/test/index.ts +4 -4
- package/test/package.json +13 -13
- package/test/tsconfig.json +27 -27
- package/tsconfig.json +16 -16
- package/.prettierignore +0 -7
- package/.prettierrc +0 -7
- package/src/route.ts +0 -35
package/CONTRIBUTING.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
only i know folder structure and codebase , if u dont understand it dont worry , just ping me.
|
|
2
|
-
|
|
1
|
+
only i know folder structure and codebase , if u dont understand it dont worry , just ping me.
|
|
2
|
+
|
|
3
3
|
if you changes code OR fixing bugs so after doing make sure to run tsc and bun build.js
|
package/LICENSE
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
Diesel.js License
|
|
2
|
-
Copyright (c) 2024 Pradeep Sahu
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
-
|
|
6
|
-
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
-
|
|
8
|
-
2. The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the Software or the use or other dealings in the Software.
|
|
1
|
+
Diesel.js License
|
|
2
|
+
Copyright (c) 2024 Pradeep Sahu
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
2. The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non-infringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the Software or the use or other dealings in the Software.
|
package/README.md
CHANGED
|
@@ -1,275 +1,276 @@
|
|
|
1
|
-
# DieselJS
|
|
2
|
-
|
|
3
|
-
**made only for bun***
|
|
4
|
-
|
|
5
|
-
**Diesel** is a simple and lightweight HTTP server library for Bun.js that provides you with complete control over your API routes and middleware. It is designed to be intuitive and efficient, allowing you to quickly set up a server, define routes, and optimize important paths for faster response times.
|
|
6
|
-
|
|
7
|
-
With built-in support for TypeScript, DieselJS ensures type safety and improved developer experience, making it easier to catch errors during development. Whether you are building a small application or a large API, DieselJS helps you manage your routes and middleware seamlessly.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## Installation
|
|
11
|
-
Install diesel-core via bun | npm | yarn | pnpm
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm install diesel-core
|
|
15
|
-
```
|
|
16
|
-
```bash
|
|
17
|
-
bun add diesel-core
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
### Code Example
|
|
21
|
-
```javascript
|
|
22
|
-
import {Diesel} from "diesel-core"
|
|
23
|
-
|
|
24
|
-
const app = new Diesel()
|
|
25
|
-
const port = 3000
|
|
26
|
-
|
|
27
|
-
app.get("/", async (ctx:ContextType) => {
|
|
28
|
-
return ctx.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
import
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
.
|
|
86
|
-
.
|
|
87
|
-
.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
.setCookie("
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
1
|
+
# DieselJS
|
|
2
|
+
|
|
3
|
+
**made only for bun***
|
|
4
|
+
|
|
5
|
+
**Diesel** is a simple and lightweight HTTP server library for Bun.js that provides you with complete control over your API routes and middleware. It is designed to be intuitive and efficient, allowing you to quickly set up a server, define routes, and optimize important paths for faster response times.
|
|
6
|
+
|
|
7
|
+
With built-in support for TypeScript, DieselJS ensures type safety and improved developer experience, making it easier to catch errors during development. Whether you are building a small application or a large API, DieselJS helps you manage your routes and middleware seamlessly.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
Install diesel-core via bun | npm | yarn | pnpm
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install diesel-core
|
|
15
|
+
```
|
|
16
|
+
```bash
|
|
17
|
+
bun add diesel-core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Code Example
|
|
21
|
+
```javascript
|
|
22
|
+
import {Diesel} from "diesel-core"
|
|
23
|
+
|
|
24
|
+
const app = new Diesel()
|
|
25
|
+
const port = 3000
|
|
26
|
+
|
|
27
|
+
app.get("/", async (ctx:ContextType) => {
|
|
28
|
+
return ctx.text("Hello world...!",200)
|
|
29
|
+
// Note :- passing statusCode is optional
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Start the server
|
|
33
|
+
app.listen(port, () => {
|
|
34
|
+
console.log(`diesel is running on port ${port}`)
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
# CORS
|
|
39
|
+
|
|
40
|
+
### Diesel supports cors out of the box
|
|
41
|
+
|
|
42
|
+
``` javascript
|
|
43
|
+
app.cors({
|
|
44
|
+
origin: ['http://localhost:5173','*'],
|
|
45
|
+
methods: 'GET,POST,PUT,DELETE',
|
|
46
|
+
allowedHeaders: 'Content-Type,Authorization'
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
# Filter and Route Security
|
|
51
|
+
**Diesel** provides a simple way to manage public and protected routes by using a filter() method. You can define specific routes to be publicly accessible, while others will require authentication or custom middleware functions.
|
|
52
|
+
|
|
53
|
+
### How to Use the Filter
|
|
54
|
+
The **filter()** method allows you to secure certain endpoints while keeping others public. You can specify routes that should be publicly accessible using permitAll(), and apply authentication or other middleware to the remaining routes with require().
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### Example Usage
|
|
58
|
+
```javascript
|
|
59
|
+
import {Diesel} from "diesel-core";
|
|
60
|
+
import jwt from 'jsonwebtoken';
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
const app = new Diesel();
|
|
64
|
+
|
|
65
|
+
async function authJwt (ctx:ContextType, server?:Server): Promise<void | Response> {
|
|
66
|
+
const token = await ctx.getCookie("accessToken"); // Retrieve the JWT token from cookies
|
|
67
|
+
if (!token) {
|
|
68
|
+
return ctx.json({ message: "Authentication token missing" },401);
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
// Verify the JWT token using a secret key
|
|
72
|
+
const user = jwt.verify(token, secret); // Replace with your JWT secret
|
|
73
|
+
// Set the user data in context
|
|
74
|
+
ctx.setUser(user);
|
|
75
|
+
|
|
76
|
+
// Proceed to the next middleware/route handler
|
|
77
|
+
return ctx.next();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return ctx.json({ message: "Invalid token" },403);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Define routes and apply filter
|
|
84
|
+
app
|
|
85
|
+
.filter()
|
|
86
|
+
.routeMatcher('/api/user/register', '/api/user/login', '/test/:id', '/cookie') // Define public routes
|
|
87
|
+
.permitAll() // Mark these routes as public (no auth required)
|
|
88
|
+
.require(authJwt); // Apply the authJwt middleware to all other routes
|
|
89
|
+
|
|
90
|
+
// Example public route (no auth required)
|
|
91
|
+
app.get("/api/user/register", async (ctx:ContextType) => {
|
|
92
|
+
return ctx.json({ msg: "This is a public route. No authentication needed." });
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Example protected route (requires auth)
|
|
96
|
+
app.get("/api/user/profile", async (ctx:ContextType) => {
|
|
97
|
+
// This route is protected, so the auth middleware will run before this handler
|
|
98
|
+
const user = ctx.getUser()
|
|
99
|
+
return ctx.json({
|
|
100
|
+
msg: "You are authenticated!" ,
|
|
101
|
+
user
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Start the server
|
|
106
|
+
const port = 3000;
|
|
107
|
+
app.listen(port, () => {
|
|
108
|
+
console.log(`Diesel is running on port ${port}`);
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
# Filter Methods
|
|
113
|
+
1. **routeMatcher(...routes: string[])** : Passed endpoints in this routeMatcher will be ***Public*** means they don't need authentication, including those with dynamic parameters (e.g., /test/:id).
|
|
114
|
+
|
|
115
|
+
```javascript
|
|
116
|
+
.routeMatcher('/api/user/register', '/api/user/login', '/test/:id')
|
|
117
|
+
```
|
|
118
|
+
1. **permitAll()** : Marks the routes specified in routeMatcher() as publicly accessible, meaning no middleware (like authentication) will be required for these routes.
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
.permitAll()
|
|
122
|
+
```
|
|
123
|
+
1. **require(fnc?: middlewareFunc)** :Means that defined routes in ***routeMatcher*** is public & All endpoints needs authentication.
|
|
124
|
+
|
|
125
|
+
*Note* : If you don't pass a middleware function to require(), DieselJS will throw an "Unauthorized" error by default. Ensure that you implement and pass a valid authentication function
|
|
126
|
+
```javascript
|
|
127
|
+
.require(authJwt)
|
|
128
|
+
```
|
|
129
|
+
## Use Case
|
|
130
|
+
* **Public Routes** : Some routes ***(like /api/user/register or /api/user/login)*** are often open to all users without authentication. These routes can be specified with permitAll().
|
|
131
|
+
|
|
132
|
+
* **Protected Routes** : For other routes ***(like /api/user/profile)***, you'll want to require authentication or custom middleware. Use require(authJwt) to ensure that the user is authenticated before accessing these routes.
|
|
133
|
+
|
|
134
|
+
# Using Hooks in DieselJS
|
|
135
|
+
|
|
136
|
+
DieselJS allows you to enhance your request handling by utilizing hooks at various stages of the request lifecycle. This gives you the flexibility to execute custom logic for logging, authentication, data manipulation, and more.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
### Available Hooks
|
|
140
|
+
|
|
141
|
+
1. **onRequest**: Triggered when a request is received.
|
|
142
|
+
2. **preHandler**: Invoked just before the request handler executes.
|
|
143
|
+
3. **postHandler**: Executed after the request handler completes but before sending the response.
|
|
144
|
+
4. **onSend**: Called just before the response is sent to the client.
|
|
145
|
+
5. **onError** : Executes if any error occurs
|
|
146
|
+
|
|
147
|
+
### How to Define Hooks
|
|
148
|
+
|
|
149
|
+
To define hooks in your DieselJS application, you can add them directly to your `Diesel` instance. Here's how to set up and use each hook:
|
|
150
|
+
|
|
151
|
+
### Example Usage
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
|
|
155
|
+
// define and onError hook
|
|
156
|
+
|
|
157
|
+
app.addHooks("onError",(error,req,url,server) => {
|
|
158
|
+
console.log(`error occured ${error.message}`)
|
|
159
|
+
// retunr new Response(......)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Define an onRequest hook
|
|
163
|
+
app.addHooks("onRequest",(req,url,server) =>{
|
|
164
|
+
console.log(`Request received: ${req.method} ${url}`);
|
|
165
|
+
})
|
|
166
|
+
// you get req,url & server instance in onReq
|
|
167
|
+
|
|
168
|
+
// Define a preHandler hook
|
|
169
|
+
app.addHooks("preHandler",(ctx:ContextType) =>{
|
|
170
|
+
// Check for authentication token
|
|
171
|
+
const authToken = ctx.req.headers.get("Authorization");
|
|
172
|
+
if (!authToken) {
|
|
173
|
+
return new Response("Unauthorized", { status: 401 });
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
// Define a postHandler hook
|
|
178
|
+
app.addHooks('postHandler', async (ctx:ContextType) => {
|
|
179
|
+
console.log(`Response sent for: ${ctx.req.url}`);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Define an onSend hook
|
|
183
|
+
app.addHooks('onSend',async (ctx, result) => {
|
|
184
|
+
console.log(`Sending response with status: ${result.status}`);
|
|
185
|
+
return result; // You can modify the response here if needed
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
# Middleware example
|
|
190
|
+
|
|
191
|
+
**No Need to call NonSense *next()* in Middleware**
|
|
192
|
+
|
|
193
|
+
**just dont return , if evrything goes right**
|
|
194
|
+
|
|
195
|
+
```javascript
|
|
196
|
+
async function authJwt (ctx:ContextType, server?:Server): Promise<void | Response> {
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
const token = ctx?.getCookie("accessToken");
|
|
200
|
+
if (!token) {
|
|
201
|
+
return ctx.json({ message: "Authentication token missing" },401);
|
|
202
|
+
}
|
|
203
|
+
// Verify the JWT token using a secret key
|
|
204
|
+
const user = jwt.verify(token, secret);
|
|
205
|
+
ctx.set('user',user);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
return ctx.json({ message: "Invalid token" },403);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// this is a global middleware
|
|
212
|
+
app.use(authJwt)
|
|
213
|
+
|
|
214
|
+
// path middleware example
|
|
215
|
+
app.use("/user",authJWT)
|
|
216
|
+
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
# set cookies
|
|
220
|
+
|
|
221
|
+
```javascript
|
|
222
|
+
app.get("/set-cookie", async(ctx:ContextType) => {
|
|
223
|
+
const user = {
|
|
224
|
+
name: "pk",
|
|
225
|
+
age: 22,
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const accessToken = jwt.sign(user, secret, { expiresIn: "1d" })
|
|
229
|
+
|
|
230
|
+
const refreshToken = jwt.sign(user, secret ,{ expiresIn: "10d" })
|
|
231
|
+
|
|
232
|
+
const options = {
|
|
233
|
+
httpOnly: true,
|
|
234
|
+
secure: true,
|
|
235
|
+
maxAge: 24 * 60 * 60 * 1000,
|
|
236
|
+
sameSite: "Strict",
|
|
237
|
+
path: "/",
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
ctx
|
|
241
|
+
.setCookie("accessToken", accessToken, options)
|
|
242
|
+
.setCookie("refreshToken", refreshToken, options)
|
|
243
|
+
|
|
244
|
+
return ctx.json({msg:"setting cookies"})
|
|
245
|
+
})
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
# Render a HTML page
|
|
249
|
+
```javascript
|
|
250
|
+
app.get("/render",async (ctx) => {
|
|
251
|
+
return ctx.file(`${import.meta.dir}/index.html`)
|
|
252
|
+
})
|
|
253
|
+
```
|
|
254
|
+
# redirect
|
|
255
|
+
```javascript
|
|
256
|
+
app.get("/redirect",(ctx:ContextType) => {
|
|
257
|
+
return ctx.redirect("/");
|
|
258
|
+
})
|
|
259
|
+
```
|
|
260
|
+
# get params
|
|
261
|
+
|
|
262
|
+
**You can use set ***Multiparams***** , ***like this***
|
|
263
|
+
|
|
264
|
+
```javascript
|
|
265
|
+
app.get("/product/:productId/:productName)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```javascript
|
|
269
|
+
app.get("/hello/:id/",(ctx:ContextType) => {
|
|
270
|
+
const id = ctx.getParams("id")
|
|
271
|
+
const query = ctx.getQuery() // you can pass query name also , you wanna get
|
|
272
|
+
return ctx.json({ msg: "Hello", id });
|
|
273
|
+
})
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
|
package/build.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
const entryPoints = [
|
|
3
|
-
'./src/main.ts',
|
|
4
|
-
'./src/ctx.ts',
|
|
5
|
-
'./src/handleRequest.ts',
|
|
6
|
-
'./src/trie.ts',
|
|
7
|
-
'./src/router.ts',
|
|
8
|
-
'./src/utils.ts'
|
|
9
|
-
];
|
|
10
|
-
|
|
11
|
-
entryPoints.forEach(entry => {
|
|
12
|
-
console.log(`Building: ${entry}`);
|
|
13
|
-
try {
|
|
14
|
-
Bun.build({
|
|
15
|
-
entrypoints: [path.resolve(entry)],
|
|
16
|
-
outdir: './dist',
|
|
17
|
-
minify: true,
|
|
18
|
-
});
|
|
19
|
-
} catch (error) {
|
|
20
|
-
console.error(`Failed to build ${entry}:`, error);
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
// Bun.spawn(['rm','-rf','./main'])
|
|
25
|
-
|
|
26
|
-
// oha test
|
|
27
|
-
// oha -c 500 -n 100000 -H "Cookie: accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicGsiLCJhZ2UiOjIyLCJpYXQiOjE3Mjk5MjQxOTQsImV4cCI6MTczMDAxMDU5NH0._dEjx5iUuOLoq15-xTPgXOemfzIPrg06Qmruiv-I5cc" http://localhost:3000/
|
|
28
|
-
|
|
29
|
-
// bombardier
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// bombardier -c 500 -n 100000 -H "Cookie: accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicGsiLCJhZ2UiOjIyLCJpYXQiOjE3Mjk5MjQxOTQsImV4cCI6MTczMDAxMDU5NH0._dEjx5iUuOLoq15-xTPgXOemfzIPrg06Qmruiv-I5cc" http://localhost:3000/
|
|
1
|
+
import path from 'path';
|
|
2
|
+
const entryPoints = [
|
|
3
|
+
'./src/main.ts',
|
|
4
|
+
'./src/ctx.ts',
|
|
5
|
+
'./src/handleRequest.ts',
|
|
6
|
+
'./src/trie.ts',
|
|
7
|
+
// './src/router.ts',
|
|
8
|
+
'./src/utils.ts'
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
entryPoints.forEach(entry => {
|
|
12
|
+
console.log(`Building: ${entry}`);
|
|
13
|
+
try {
|
|
14
|
+
Bun.build({
|
|
15
|
+
entrypoints: [path.resolve(entry)],
|
|
16
|
+
outdir: './dist',
|
|
17
|
+
minify: true,
|
|
18
|
+
});
|
|
19
|
+
} catch (error) {
|
|
20
|
+
console.error(`Failed to build ${entry}:`, error);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Bun.spawn(['rm','-rf','./main'])
|
|
25
|
+
|
|
26
|
+
// oha test
|
|
27
|
+
// oha -c 500 -n 100000 -H "Cookie: accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicGsiLCJhZ2UiOjIyLCJpYXQiOjE3Mjk5MjQxOTQsImV4cCI6MTczMDAxMDU5NH0._dEjx5iUuOLoq15-xTPgXOemfzIPrg06Qmruiv-I5cc" http://localhost:3000/
|
|
28
|
+
|
|
29
|
+
// bombardier
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// bombardier -c 500 -n 100000 -H "Cookie: accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoicGsiLCJhZ2UiOjIyLCJpYXQiOjE3Mjk5MjQxOTQsImV4cCI6MTczMDAxMDU5NH0._dEjx5iUuOLoq15-xTPgXOemfzIPrg06Qmruiv-I5cc" http://localhost:3000/
|