tezx 3.0.14-beta.3 → 4.0.0-beta
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 +86 -173
- package/cjs/{core/config.js → config/index.js} +3 -5
- package/cjs/core/context.js +67 -91
- package/cjs/core/request.js +2 -3
- package/cjs/core/router.js +45 -27
- package/cjs/core/server.js +13 -13
- package/cjs/{utils → helper}/formData.js +7 -11
- package/cjs/{utils → helper}/generateID.js +2 -17
- package/cjs/helper/index.js +4 -47
- package/cjs/index.js +3 -6
- package/cjs/jwt/{web.js → index.js} +12 -65
- package/cjs/middleware/basic-auth.js +19 -10
- package/cjs/middleware/bearer-auth.js +6 -6
- package/cjs/middleware/cache-control.js +3 -4
- package/cjs/middleware/detect-bot.js +12 -23
- package/cjs/middleware/etag.js +34 -0
- package/cjs/{bun → middleware}/getConnInfo.js +2 -1
- package/cjs/middleware/i18n.js +3 -2
- package/cjs/middleware/index.js +2 -1
- package/cjs/middleware/logger.js +3 -3
- package/cjs/middleware/pagination.js +2 -2
- package/cjs/middleware/rate-limiter.js +3 -4
- package/cjs/registry/RadixRouter.js +3 -4
- package/cjs/{node/serveStatic.js → static/index.js} +6 -6
- package/cjs/utils/crypto.js +8 -0
- package/cjs/utils/index.js +19 -0
- package/cjs/utils/response.js +21 -51
- package/cjs/utils/url.js +71 -38
- package/cjs/utils/utils.js +45 -0
- package/cjs/{bun/ws.js → ws/index.js} +5 -7
- package/{core/config.d.ts → config/index.d.ts} +1 -3
- package/{core/config.js → config/index.js} +1 -3
- package/core/context.d.ts +90 -91
- package/core/context.js +67 -91
- package/core/request.js +2 -3
- package/core/router.d.ts +2 -9
- package/core/router.js +43 -25
- package/core/server.d.ts +15 -47
- package/core/server.js +13 -13
- package/{utils → helper}/formData.d.ts +1 -1
- package/{utils → helper}/formData.js +6 -10
- package/helper/generateID.d.ts +31 -0
- package/helper/generateID.js +17 -0
- package/helper/index.d.ts +4 -40
- package/helper/index.js +4 -30
- package/index.d.ts +3 -5
- package/index.js +3 -5
- package/jwt/{web.d.ts → index.d.ts} +1 -1
- package/jwt/{web.js → index.js} +12 -65
- package/middleware/basic-auth.d.ts +1 -2
- package/middleware/basic-auth.js +19 -10
- package/middleware/bearer-auth.d.ts +1 -2
- package/middleware/bearer-auth.js +6 -6
- package/middleware/cache-control.d.ts +2 -3
- package/middleware/cache-control.js +3 -4
- package/middleware/detect-bot.d.ts +8 -1
- package/middleware/detect-bot.js +13 -24
- package/middleware/etag.d.ts +13 -0
- package/middleware/etag.js +31 -0
- package/{bun → middleware}/getConnInfo.d.ts +2 -1
- package/{bun → middleware}/getConnInfo.js +2 -1
- package/middleware/i18n.d.ts +1 -1
- package/middleware/i18n.js +3 -2
- package/middleware/index.d.ts +2 -1
- package/middleware/index.js +2 -1
- package/middleware/logger.d.ts +2 -2
- package/middleware/logger.js +3 -3
- package/middleware/pagination.d.ts +1 -1
- package/middleware/pagination.js +1 -1
- package/middleware/powered-by.d.ts +1 -1
- package/middleware/rate-limiter.d.ts +5 -10
- package/middleware/rate-limiter.js +4 -5
- package/middleware/request-id.d.ts +1 -1
- package/middleware/sanitize-headers.d.ts +1 -1
- package/middleware/xss-protection.d.ts +2 -2
- package/package.json +40 -34
- package/registry/RadixRouter.js +2 -3
- package/{node/serveStatic.d.ts → static/index.d.ts} +2 -10
- package/{node/serveStatic.js → static/index.js} +5 -4
- package/types/index.d.ts +55 -19
- package/utils/crypto.d.ts +9 -0
- package/utils/crypto.js +5 -0
- package/utils/index.d.ts +14 -0
- package/utils/index.js +11 -0
- package/utils/response.d.ts +3 -16
- package/utils/response.js +20 -49
- package/utils/url.d.ts +24 -0
- package/utils/url.js +69 -38
- package/utils/utils.d.ts +22 -0
- package/utils/utils.js +41 -0
- package/{bun/ws.d.ts → ws/index.d.ts} +2 -1
- package/{bun/ws.js → ws/index.js} +5 -7
- package/bun/index.d.ts +0 -15
- package/bun/index.js +0 -12
- package/cjs/bun/index.js +0 -52
- package/cjs/core/error.js +0 -49
- package/cjs/deno/env.js +0 -55
- package/cjs/deno/getConnInfo.js +0 -18
- package/cjs/deno/index.js +0 -17
- package/cjs/deno/serveStatic.js +0 -53
- package/cjs/deno/ws.js +0 -39
- package/cjs/jwt/node.js +0 -94
- package/cjs/middleware/secure-headers copy.js +0 -143
- package/cjs/middleware/secure-headers.js +0 -157
- package/cjs/node/env.js +0 -50
- package/cjs/node/getConnInfo.js +0 -16
- package/cjs/node/index.js +0 -23
- package/cjs/node/mount-node.js +0 -59
- package/cjs/node/toWebRequest.js +0 -25
- package/cjs/node/ws.js +0 -82
- package/cjs/utils/buffer.js +0 -17
- package/cjs/utils/file.js +0 -136
- package/cjs/utils/httpStatusMap.js +0 -68
- package/cjs/utils/low-level.js +0 -115
- package/cjs/utils/runtime.js +0 -16
- package/core/error.d.ts +0 -96
- package/core/error.js +0 -44
- package/deno/env.d.ts +0 -5
- package/deno/env.js +0 -52
- package/deno/getConnInfo.d.ts +0 -21
- package/deno/getConnInfo.js +0 -15
- package/deno/index.d.ts +0 -14
- package/deno/index.js +0 -11
- package/deno/serveStatic.d.ts +0 -28
- package/deno/serveStatic.js +0 -49
- package/deno/ws.d.ts +0 -42
- package/deno/ws.js +0 -36
- package/jwt/node.d.ts +0 -39
- package/jwt/node.js +0 -87
- package/middleware/secure-headers copy.d.ts +0 -15
- package/middleware/secure-headers copy.js +0 -136
- package/middleware/secure-headers.d.ts +0 -132
- package/middleware/secure-headers.js +0 -153
- package/node/env.d.ts +0 -5
- package/node/env.js +0 -47
- package/node/getConnInfo.d.ts +0 -21
- package/node/getConnInfo.js +0 -13
- package/node/index.d.ts +0 -18
- package/node/index.js +0 -15
- package/node/mount-node.d.ts +0 -11
- package/node/mount-node.js +0 -56
- package/node/toWebRequest.d.ts +0 -11
- package/node/toWebRequest.js +0 -22
- package/node/ws.d.ts +0 -56
- package/node/ws.js +0 -46
- package/utils/buffer.d.ts +0 -1
- package/utils/buffer.js +0 -14
- package/utils/file.d.ts +0 -38
- package/utils/file.js +0 -96
- package/utils/generateID.d.ts +0 -42
- package/utils/generateID.js +0 -32
- package/utils/httpStatusMap.d.ts +0 -15
- package/utils/httpStatusMap.js +0 -65
- package/utils/low-level.d.ts +0 -58
- package/utils/low-level.js +0 -108
- package/utils/runtime.d.ts +0 -4
- package/utils/runtime.js +0 -12
- /package/cjs/{utils/cookie.js → cookie/index.js} +0 -0
- /package/{utils/cookie.d.ts → cookie/index.d.ts} +0 -0
- /package/{utils/cookie.js → cookie/index.js} +0 -0
package/README.md
CHANGED
|
@@ -1,110 +1,91 @@
|
|
|
1
|
-
# ⚡ TezX – High-Performance JavaScript Framework
|
|
1
|
+
# ⚡ TezX – High-Performance JavaScript Framework for **Bun**
|
|
2
2
|
|
|
3
|
-
**TezX** is a modern,
|
|
3
|
+
**TezX** is a modern, ultra-fast, and lightweight JavaScript framework built specifically for **Bun**.
|
|
4
|
+
With a clean API, powerful routing, WebSocket support, middleware stacking, and native static file serving — TezX helps you build scalable applications with unmatched speed.
|
|
4
5
|
|
|
5
6
|
[](https://deepwiki.com/tezxjs/TezX)
|
|
6
7
|
|
|
7
8
|
---
|
|
8
9
|
|
|
9
|
-
## 🚀
|
|
10
|
+
## 🚀 Why TezX (Built for Bun)?
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
* ⚡ **Blazing Fast** — Fully optimized for Bun’s event loop & native performance.
|
|
13
|
+
* 🧩 **Minimal, Clean API** — Developer-friendly and intuitive.
|
|
14
|
+
* 🗂 **Native Static Serving** — No external dependencies needed.
|
|
15
|
+
* 🔌 **Powerful Middleware Engine** — Compose any logic effortlessly.
|
|
16
|
+
* 🧭 **Advanced Routing** — Dynamic, nested, and pattern-based.
|
|
17
|
+
* 🔐 **Secure by Default** — Built-in safe context handling.
|
|
18
|
+
* 📡 **WebSocket Support** — Real-time apps made easy with `wsHandlers`.
|
|
19
|
+
* ♻️ **Multi-Process Ready** — Via Bun’s `reusePort`.
|
|
19
20
|
|
|
20
21
|
---
|
|
21
22
|
|
|
22
23
|
## 📦 Installation
|
|
23
24
|
|
|
24
|
-
### Node.js
|
|
25
|
-
|
|
26
|
-
```bash
|
|
27
|
-
npm install tezx
|
|
28
|
-
# or
|
|
29
|
-
yarn add tezx
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
### Bun
|
|
33
|
-
|
|
34
25
|
```bash
|
|
35
26
|
bun add tezx
|
|
27
|
+
# or
|
|
28
|
+
npm install tezx
|
|
36
29
|
```
|
|
37
30
|
|
|
38
|
-
<!--
|
|
39
|
-
### Deno
|
|
40
|
-
|
|
41
|
-
```ts
|
|
42
|
-
import { TezX } from "https://deno.land/x/tezx/mod.ts";
|
|
43
|
-
```
|
|
44
|
-
-->
|
|
45
|
-
|
|
46
31
|
---
|
|
47
32
|
|
|
48
|
-
## ⚡ Quick Start
|
|
33
|
+
## ⚡ Quick Start (Bun)
|
|
49
34
|
|
|
50
35
|
```ts
|
|
51
36
|
import { TezX } from "tezx";
|
|
52
|
-
import { logger } from "tezx/
|
|
53
|
-
import {
|
|
54
|
-
import {
|
|
37
|
+
import { logger } from "tezx/middleware";
|
|
38
|
+
import { serveStatic } from "tezx/static";
|
|
39
|
+
import { wsHandlers } from "tezx/ws";
|
|
55
40
|
|
|
56
41
|
const app = new TezX();
|
|
42
|
+
|
|
43
|
+
// Middlewares
|
|
57
44
|
app.use(logger());
|
|
58
|
-
app.static("/", "./static");
|
|
59
45
|
|
|
46
|
+
// Static files
|
|
47
|
+
app.static(serveStatic("/", "./static"));
|
|
48
|
+
|
|
49
|
+
// Route
|
|
60
50
|
app.get("/", (ctx) =>
|
|
61
51
|
ctx.html(`
|
|
62
52
|
<h1>Welcome to TezX</h1>
|
|
63
|
-
<p>
|
|
53
|
+
<p>High-performance JavaScript framework optimized for Bun.</p>
|
|
64
54
|
`)
|
|
65
55
|
);
|
|
66
56
|
|
|
67
|
-
|
|
68
|
-
|
|
57
|
+
// Server
|
|
58
|
+
const port = Number(process.env.PORT) || 3001;
|
|
69
59
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
60
|
+
Bun.serve({
|
|
61
|
+
port,
|
|
62
|
+
reusePort: true,
|
|
63
|
+
fetch(req, server) {
|
|
64
|
+
return app.serve(req, server);
|
|
65
|
+
},
|
|
66
|
+
websocket: wsHandlers({
|
|
67
|
+
// Optional WebSocket config
|
|
68
|
+
}),
|
|
73
69
|
});
|
|
74
|
-
```
|
|
75
70
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
## ▶ Starting the Server
|
|
79
|
-
|
|
80
|
-
### Node.js
|
|
81
|
-
|
|
82
|
-
```bash
|
|
83
|
-
node server.js
|
|
84
|
-
# or
|
|
85
|
-
npm install -g nodemon
|
|
86
|
-
nodemon server.js
|
|
71
|
+
console.log(`🚀 TezX server running at http://localhost:${port}`);
|
|
87
72
|
```
|
|
88
73
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
```bash
|
|
92
|
-
bun run server.js
|
|
93
|
-
```
|
|
74
|
+
---
|
|
94
75
|
|
|
95
|
-
|
|
76
|
+
## ▶ Run the Server
|
|
96
77
|
|
|
97
78
|
```bash
|
|
98
|
-
|
|
79
|
+
bun run server.ts
|
|
99
80
|
```
|
|
100
81
|
|
|
101
82
|
---
|
|
102
83
|
|
|
103
|
-
## 🔌 Middleware
|
|
84
|
+
## 🔌 Middleware Example
|
|
104
85
|
|
|
105
86
|
```ts
|
|
106
87
|
app.use((ctx, next) => {
|
|
107
|
-
console.log(
|
|
88
|
+
console.log("➡ Request:", ctx.req.url);
|
|
108
89
|
return next();
|
|
109
90
|
});
|
|
110
91
|
```
|
|
@@ -114,21 +95,28 @@ app.use((ctx, next) => {
|
|
|
114
95
|
## 🗂 Static File Serving
|
|
115
96
|
|
|
116
97
|
```ts
|
|
117
|
-
|
|
98
|
+
import { serveStatic } from "tezx/static";
|
|
99
|
+
|
|
100
|
+
app.static(serveStatic("/assets", "./assets"));
|
|
118
101
|
```
|
|
119
102
|
|
|
120
|
-
|
|
103
|
+
Access via:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
http://localhost:3001/assets/your-file.ext
|
|
107
|
+
```
|
|
121
108
|
|
|
122
109
|
---
|
|
123
110
|
|
|
124
111
|
## 🧭 Routing
|
|
125
112
|
|
|
126
113
|
```ts
|
|
127
|
-
app.get("/about", (ctx) => ctx.html("<h1>About
|
|
114
|
+
app.get("/about", (ctx) => ctx.html("<h1>About TezX</h1>"));
|
|
128
115
|
|
|
129
|
-
app.post("/
|
|
130
|
-
ctx.json(
|
|
131
|
-
);
|
|
116
|
+
app.post("/contact", async (ctx) => {
|
|
117
|
+
const body = await ctx.json();
|
|
118
|
+
return ctx.json({ received: body });
|
|
119
|
+
});
|
|
132
120
|
```
|
|
133
121
|
|
|
134
122
|
---
|
|
@@ -137,7 +125,10 @@ app.post("/submit", (ctx) =>
|
|
|
137
125
|
|
|
138
126
|
```ts
|
|
139
127
|
app.onError((err, ctx) => {
|
|
140
|
-
return ctx.status(500).json({
|
|
128
|
+
return ctx.status(500).json({
|
|
129
|
+
error: "Internal Server Error",
|
|
130
|
+
message: err.message,
|
|
131
|
+
});
|
|
141
132
|
});
|
|
142
133
|
```
|
|
143
134
|
|
|
@@ -145,38 +136,9 @@ app.onError((err, ctx) => {
|
|
|
145
136
|
|
|
146
137
|
## 🧪 Development Setup
|
|
147
138
|
|
|
148
|
-
###
|
|
149
|
-
|
|
150
|
-
```bash
|
|
151
|
-
npm run dev
|
|
152
|
-
# or
|
|
153
|
-
bun run dev
|
|
154
|
-
```
|
|
139
|
+
### `dev` script for Bun
|
|
155
140
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
---
|
|
159
|
-
|
|
160
|
-
## ⚙️ Platform-Specific Scripts
|
|
161
|
-
|
|
162
|
-
### Node.js – `package.json`
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
{
|
|
166
|
-
"scripts": {
|
|
167
|
-
"build:esm": "tsc --outDir dist/mjs --removeComments",
|
|
168
|
-
"build:dts": "tsc --outDir dist/types --declaration --emitDeclarationOnly",
|
|
169
|
-
"build": "npm run build:esm && npm run build:dts",
|
|
170
|
-
"start": "node dist/index.js",
|
|
171
|
-
"nodemon": "nodemon src/index.ts",
|
|
172
|
-
"dev": "tsx watch src/index.ts"
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
---
|
|
178
|
-
|
|
179
|
-
### Bun – `package.json`
|
|
141
|
+
**package.json**
|
|
180
142
|
|
|
181
143
|
```json
|
|
182
144
|
{
|
|
@@ -186,111 +148,62 @@ Access: [http://localhost:3000](http://localhost:3000)
|
|
|
186
148
|
}
|
|
187
149
|
```
|
|
188
150
|
|
|
189
|
-
|
|
151
|
+
### Example: `src/index.ts`
|
|
190
152
|
|
|
191
153
|
```ts
|
|
154
|
+
import app from "./app";
|
|
155
|
+
|
|
192
156
|
Bun.serve({
|
|
193
157
|
port: 3001,
|
|
194
158
|
reusePort: true,
|
|
195
159
|
fetch(req, server) {
|
|
196
160
|
return app.serve(req, server);
|
|
197
161
|
},
|
|
198
|
-
websocket: {
|
|
199
|
-
open(ws) {
|
|
200
|
-
console.log("WebSocket connected");
|
|
201
|
-
return ws.data?.open?.(ws);
|
|
202
|
-
},
|
|
203
|
-
message(ws, msg) {
|
|
204
|
-
return ws.data?.message?.(ws, msg);
|
|
205
|
-
},
|
|
206
|
-
close(ws, code, reason) {
|
|
207
|
-
return ws.data?.close?.(ws, { code, reason });
|
|
208
|
-
},
|
|
209
|
-
ping(ws, data) {
|
|
210
|
-
return ws.data?.ping?.(ws, data);
|
|
211
|
-
},
|
|
212
|
-
pong(ws, data) {
|
|
213
|
-
return ws.data?.pong?.(ws, data);
|
|
214
|
-
},
|
|
215
|
-
drain(ws) {
|
|
216
|
-
return ws.data?.drain?.(ws);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
console.log(`🚀 Server running at http://localhost:${process.env.PORT}`);
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
---
|
|
225
|
-
|
|
226
|
-
### Deno – `package.json`
|
|
227
|
-
|
|
228
|
-
```json
|
|
229
|
-
{
|
|
230
|
-
"scripts": {
|
|
231
|
-
"dev": "deno run --watch --allow-all --unstable-sloppy-imports src/index.ts"
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
#### Example: `src/index.ts`
|
|
237
|
-
|
|
238
|
-
```ts
|
|
239
|
-
Deno.serve({ port: Number(Deno.env.get("PORT") || 5000) }, (req, connInfo) => {
|
|
240
|
-
return app.serve(req, connInfo);
|
|
241
162
|
});
|
|
242
163
|
```
|
|
243
164
|
|
|
244
165
|
---
|
|
245
166
|
|
|
246
|
-
## 🏗 Build & Deployment
|
|
247
|
-
|
|
248
|
-
### Compile TypeScript
|
|
249
|
-
|
|
250
|
-
```bash
|
|
251
|
-
npm run build
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
Outputs:
|
|
255
|
-
|
|
256
|
-
- CommonJS (`dist/cjs`)
|
|
257
|
-
- ESM (`dist/mjs`)
|
|
258
|
-
- Type Declarations (`dist/types`)
|
|
259
|
-
|
|
260
|
-
---
|
|
261
|
-
|
|
262
167
|
## 🤝 Contributing
|
|
263
168
|
|
|
264
|
-
We welcome
|
|
169
|
+
We welcome contributions!
|
|
265
170
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
171
|
+
1. Fork the repo
|
|
172
|
+
2. Create a new branch
|
|
173
|
+
3. Commit your changes
|
|
174
|
+
4. Open a pull request
|
|
270
175
|
|
|
271
|
-
👉 GitHub: [https://github.com/tezxjs](https://github.com/tezxjs)
|
|
176
|
+
👉 GitHub: **[https://github.com/tezxjs](https://github.com/tezxjs)**
|
|
272
177
|
|
|
273
178
|
---
|
|
274
179
|
|
|
275
180
|
## 💖 Support TezX
|
|
276
181
|
|
|
277
|
-
TezX
|
|
278
|
-
|
|
279
|
-
- 🌟 Star the project on [GitHub](https://github.com/tezxjs/TezX)
|
|
280
|
-
- 💸 [Sponsor on GitHub](https://github.com/sponsors/srakib17)
|
|
182
|
+
If TezX helps you, consider supporting:
|
|
281
183
|
|
|
282
|
-
|
|
184
|
+
* ⭐ Star on GitHub
|
|
185
|
+
* 💸 Sponsor on GitHub: [https://github.com/sponsors/srakib17](https://github.com/sponsors/srakib17)
|
|
283
186
|
|
|
284
|
-
Your support helps improve
|
|
187
|
+
Your support helps improve the framework.
|
|
285
188
|
|
|
286
189
|
---
|
|
287
190
|
|
|
288
191
|
## 🙌 Sponsor
|
|
289
192
|
|
|
290
|
-
|
|
193
|
+
<a href="http://papernxt.com" target="_blank">
|
|
194
|
+
<img
|
|
195
|
+
src="https://papernxt.com/favicon.ico"
|
|
196
|
+
width="48"
|
|
197
|
+
hight="48"
|
|
198
|
+
alt="papernxt.com"
|
|
199
|
+
title="papernxt.com"
|
|
200
|
+
/>
|
|
201
|
+
</a>
|
|
291
202
|
|
|
292
203
|
---
|
|
293
204
|
|
|
294
205
|
## 📜 License
|
|
295
206
|
|
|
296
|
-
|
|
207
|
+
Licensed under the **MIT License**.
|
|
208
|
+
|
|
209
|
+
---
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Config = void 0;
|
|
4
4
|
const debugging_js_1 = require("../utils/debugging.js");
|
|
5
|
-
|
|
6
|
-
let GlobalConfig = class {
|
|
5
|
+
let Config = class {
|
|
7
6
|
static debugMode = false;
|
|
8
|
-
static adapter = runtime_js_1.runtime;
|
|
9
7
|
static get debugging() {
|
|
10
8
|
return this.debugMode
|
|
11
9
|
? {
|
|
@@ -24,4 +22,4 @@ let GlobalConfig = class {
|
|
|
24
22
|
};
|
|
25
23
|
}
|
|
26
24
|
};
|
|
27
|
-
exports.
|
|
25
|
+
exports.Config = Config;
|
package/cjs/core/context.js
CHANGED
|
@@ -1,39 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Context = void 0;
|
|
4
|
-
const file_js_1 = require("../utils/file.js");
|
|
5
|
-
const low_level_js_1 = require("../utils/low-level.js");
|
|
6
4
|
const mimeTypes_js_1 = require("../utils/mimeTypes.js");
|
|
7
5
|
const response_js_1 = require("../utils/response.js");
|
|
8
|
-
const
|
|
6
|
+
const utils_js_1 = require("../utils/utils.js");
|
|
9
7
|
const request_js_1 = require("./request.js");
|
|
10
8
|
class Context {
|
|
11
9
|
#status = 200;
|
|
12
10
|
#headers;
|
|
13
11
|
#req = null;
|
|
14
|
-
|
|
12
|
+
params = {};
|
|
15
13
|
rawRequest;
|
|
16
|
-
#
|
|
14
|
+
#server;
|
|
17
15
|
#body;
|
|
18
16
|
url;
|
|
19
17
|
res;
|
|
20
|
-
env = {};
|
|
21
18
|
pathname;
|
|
22
19
|
method;
|
|
23
|
-
constructor(req, pathname, method,
|
|
24
|
-
this.#
|
|
20
|
+
constructor(req, pathname, method, server) {
|
|
21
|
+
this.#server = server;
|
|
25
22
|
this.rawRequest = req;
|
|
26
23
|
this.url = req.url;
|
|
27
24
|
this.pathname = pathname;
|
|
28
|
-
this.env = env ?? {};
|
|
29
25
|
this.method = method;
|
|
30
26
|
}
|
|
31
|
-
get getStatus() {
|
|
32
|
-
return this.#status;
|
|
33
|
-
}
|
|
34
|
-
set setStatus(code) {
|
|
35
|
-
this.#status = code;
|
|
36
|
-
}
|
|
37
27
|
get headers() {
|
|
38
28
|
return this.res?.headers ?? (this.#headers ??= new Headers());
|
|
39
29
|
}
|
|
@@ -51,11 +41,8 @@ class Context {
|
|
|
51
41
|
}
|
|
52
42
|
return this;
|
|
53
43
|
}
|
|
54
|
-
set params(params) {
|
|
55
|
-
this.#params = params;
|
|
56
|
-
}
|
|
57
44
|
get req() {
|
|
58
|
-
return (this.#req ??= new request_js_1.TezXRequest(this.rawRequest, this.method, this.pathname, this
|
|
45
|
+
return (this.#req ??= new request_js_1.TezXRequest(this.rawRequest, this.method, this.pathname, this.params));
|
|
59
46
|
}
|
|
60
47
|
get body() {
|
|
61
48
|
return this.#body;
|
|
@@ -63,26 +50,14 @@ class Context {
|
|
|
63
50
|
set body(value) {
|
|
64
51
|
this.#body = value;
|
|
65
52
|
}
|
|
66
|
-
status
|
|
53
|
+
status(status) {
|
|
67
54
|
this.#status = status;
|
|
68
55
|
return this;
|
|
69
|
-
}
|
|
56
|
+
}
|
|
70
57
|
#newResponse(body, type, init = {}) {
|
|
71
|
-
|
|
72
|
-
headers.
|
|
73
|
-
|
|
74
|
-
for (const key in init.headers) {
|
|
75
|
-
const value = init.headers[key];
|
|
76
|
-
if (!value)
|
|
77
|
-
continue;
|
|
78
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
79
|
-
headers.append(key, value);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
headers.set(key, value);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
58
|
+
let headers = (0, response_js_1.mergeHeaders)(this.#headers, init.headers);
|
|
59
|
+
if (!headers.has("Content-Type"))
|
|
60
|
+
headers.set("Content-Type", type);
|
|
86
61
|
return new Response(body, {
|
|
87
62
|
status: init.status ?? this.#status,
|
|
88
63
|
statusText: init.statusText,
|
|
@@ -90,21 +65,7 @@ class Context {
|
|
|
90
65
|
});
|
|
91
66
|
}
|
|
92
67
|
newResponse(body, init = {}) {
|
|
93
|
-
|
|
94
|
-
if (init.headers) {
|
|
95
|
-
for (const key in init.headers) {
|
|
96
|
-
const value = init.headers[key];
|
|
97
|
-
if (!value) {
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
101
|
-
headers.append(key, value);
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
headers.set(key, value);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
68
|
+
let headers = (0, response_js_1.mergeHeaders)(this.#headers, init.headers);
|
|
108
69
|
return new Response(body, {
|
|
109
70
|
status: init.status ?? this.#status,
|
|
110
71
|
statusText: init.statusText,
|
|
@@ -114,64 +75,79 @@ class Context {
|
|
|
114
75
|
text(content, init) {
|
|
115
76
|
return this.#newResponse(content, "text/plain; charset=utf-8", init);
|
|
116
77
|
}
|
|
117
|
-
html(strings,
|
|
118
|
-
return this.#newResponse(
|
|
119
|
-
}
|
|
120
|
-
xml(strings, ...args) {
|
|
121
|
-
return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
|
|
78
|
+
html(strings, init) {
|
|
79
|
+
return this.#newResponse(strings, "text/html; charset=utf-8", init);
|
|
122
80
|
}
|
|
123
81
|
json(json, init) {
|
|
124
82
|
return this.#newResponse(JSON.stringify(json), "application/json; charset=utf-8", init);
|
|
125
83
|
}
|
|
126
84
|
send(body, init) {
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
85
|
+
let _body;
|
|
86
|
+
let type;
|
|
87
|
+
if (body === null || body === undefined) {
|
|
88
|
+
_body = "";
|
|
89
|
+
type = "text/plain";
|
|
90
|
+
}
|
|
91
|
+
else if (typeof body === "string" || typeof body === "number") {
|
|
92
|
+
_body = body;
|
|
93
|
+
type = "text/plain";
|
|
94
|
+
}
|
|
95
|
+
else if (body instanceof Uint8Array || body instanceof ArrayBuffer || (typeof ReadableStream !== "undefined" && body instanceof ReadableStream)) {
|
|
96
|
+
_body = body;
|
|
97
|
+
type = "application/octet-stream";
|
|
98
|
+
}
|
|
99
|
+
else if (body instanceof Blob || (typeof File !== "undefined" && body instanceof File)) {
|
|
100
|
+
_body = body;
|
|
101
|
+
type = body.type || "application/octet-stream";
|
|
102
|
+
}
|
|
103
|
+
else if (typeof body === "object") {
|
|
104
|
+
_body = JSON.stringify(body);
|
|
105
|
+
type = "application/json";
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
_body = String(body);
|
|
109
|
+
type = "text/plain";
|
|
110
|
+
}
|
|
111
|
+
return this.#newResponse(_body, type, init);
|
|
132
112
|
}
|
|
133
113
|
redirect(url, status = 302) {
|
|
134
114
|
const headers = new Headers(this.#headers);
|
|
135
115
|
headers.set("Location", url);
|
|
136
116
|
return new Response(null, { status, headers });
|
|
137
117
|
}
|
|
138
|
-
async download(filePath, filename) {
|
|
139
|
-
if (!(await (0, file_js_1.fileExists)(filePath)))
|
|
140
|
-
throw error_js_1.TezXError.notFound("File not found");
|
|
141
|
-
const buf = await (0, file_js_1.getFileBuffer)(filePath);
|
|
142
|
-
const headers = {
|
|
143
|
-
"Content-Disposition": `attachment; filename="${filename}"`,
|
|
144
|
-
"Content-Length": buf.byteLength.toString(),
|
|
145
|
-
};
|
|
146
|
-
return this.#newResponse(buf, "application/octet-stream", {
|
|
147
|
-
status: this.#status,
|
|
148
|
-
headers,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
118
|
async sendFile(filePath, init) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
let
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (filename) {
|
|
165
|
-
headers["Content-Disposition"] = `attachment; filename="${filename}"`;
|
|
119
|
+
let file = Bun.file(filePath);
|
|
120
|
+
if (!await file.exists()) {
|
|
121
|
+
this.#status = 404;
|
|
122
|
+
throw Error("File not found");
|
|
123
|
+
}
|
|
124
|
+
;
|
|
125
|
+
let size = file.size;
|
|
126
|
+
let stream = file.stream();
|
|
127
|
+
let headers = init?.headers ?? {};
|
|
128
|
+
headers['Content-Length'] = size.toString();
|
|
129
|
+
if (init?.filename) {
|
|
130
|
+
headers["Content-Disposition"] = `attachment; filename="${init?.filename}"`;
|
|
166
131
|
}
|
|
167
|
-
|
|
132
|
+
if (init?.download || init?.filename) {
|
|
133
|
+
headers["Content-Type"] = "application/octet-stream";
|
|
134
|
+
return this.newResponse(stream, {
|
|
135
|
+
status: init?.status ?? this.#status,
|
|
136
|
+
statusText: init?.statusText,
|
|
137
|
+
headers,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const ext = (0, utils_js_1.extensionExtract)(filePath);
|
|
141
|
+
const mimeType = mimeTypes_js_1.mimeTypes[ext] ?? mimeTypes_js_1.defaultMimeType;
|
|
142
|
+
headers["Content-Type"] = mimeType;
|
|
143
|
+
return this.newResponse(stream, {
|
|
168
144
|
status: init?.status ?? this.#status,
|
|
169
145
|
statusText: init?.statusText,
|
|
170
146
|
headers,
|
|
171
147
|
});
|
|
172
148
|
}
|
|
173
|
-
get
|
|
174
|
-
return this.#
|
|
149
|
+
get server() {
|
|
150
|
+
return this.#server;
|
|
175
151
|
}
|
|
176
152
|
}
|
|
177
153
|
exports.Context = Context;
|
package/cjs/core/request.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TezXRequest = void 0;
|
|
4
4
|
const url_js_1 = require("../utils/url.js");
|
|
5
|
-
const error_js_1 = require("./error.js");
|
|
6
5
|
class TezXRequest {
|
|
7
6
|
url;
|
|
8
7
|
method;
|
|
@@ -84,11 +83,11 @@ class TezXRequest {
|
|
|
84
83
|
return this.#cachedFormObject;
|
|
85
84
|
const ct = this.#contentType;
|
|
86
85
|
if (!ct)
|
|
87
|
-
throw new
|
|
86
|
+
throw new Error("Missing Content-Type");
|
|
88
87
|
if (ct === "application/x-www-form-urlencoded" ||
|
|
89
88
|
ct === "multipart/form-data") {
|
|
90
89
|
if (this.#bodyConsumed) {
|
|
91
|
-
throw new
|
|
90
|
+
throw new Error("Multipart body already consumed elsewhere");
|
|
92
91
|
}
|
|
93
92
|
this.#cachedFormObject = (await this.#rawRequest.formData());
|
|
94
93
|
this.#bodyConsumed = true;
|