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.
Files changed (160) hide show
  1. package/README.md +86 -173
  2. package/cjs/{core/config.js → config/index.js} +3 -5
  3. package/cjs/core/context.js +67 -91
  4. package/cjs/core/request.js +2 -3
  5. package/cjs/core/router.js +45 -27
  6. package/cjs/core/server.js +13 -13
  7. package/cjs/{utils → helper}/formData.js +7 -11
  8. package/cjs/{utils → helper}/generateID.js +2 -17
  9. package/cjs/helper/index.js +4 -47
  10. package/cjs/index.js +3 -6
  11. package/cjs/jwt/{web.js → index.js} +12 -65
  12. package/cjs/middleware/basic-auth.js +19 -10
  13. package/cjs/middleware/bearer-auth.js +6 -6
  14. package/cjs/middleware/cache-control.js +3 -4
  15. package/cjs/middleware/detect-bot.js +12 -23
  16. package/cjs/middleware/etag.js +34 -0
  17. package/cjs/{bun → middleware}/getConnInfo.js +2 -1
  18. package/cjs/middleware/i18n.js +3 -2
  19. package/cjs/middleware/index.js +2 -1
  20. package/cjs/middleware/logger.js +3 -3
  21. package/cjs/middleware/pagination.js +2 -2
  22. package/cjs/middleware/rate-limiter.js +3 -4
  23. package/cjs/registry/RadixRouter.js +3 -4
  24. package/cjs/{node/serveStatic.js → static/index.js} +6 -6
  25. package/cjs/utils/crypto.js +8 -0
  26. package/cjs/utils/index.js +19 -0
  27. package/cjs/utils/response.js +21 -51
  28. package/cjs/utils/url.js +71 -38
  29. package/cjs/utils/utils.js +45 -0
  30. package/cjs/{bun/ws.js → ws/index.js} +5 -7
  31. package/{core/config.d.ts → config/index.d.ts} +1 -3
  32. package/{core/config.js → config/index.js} +1 -3
  33. package/core/context.d.ts +90 -91
  34. package/core/context.js +67 -91
  35. package/core/request.js +2 -3
  36. package/core/router.d.ts +2 -9
  37. package/core/router.js +43 -25
  38. package/core/server.d.ts +15 -47
  39. package/core/server.js +13 -13
  40. package/{utils → helper}/formData.d.ts +1 -1
  41. package/{utils → helper}/formData.js +6 -10
  42. package/helper/generateID.d.ts +31 -0
  43. package/helper/generateID.js +17 -0
  44. package/helper/index.d.ts +4 -40
  45. package/helper/index.js +4 -30
  46. package/index.d.ts +3 -5
  47. package/index.js +3 -5
  48. package/jwt/{web.d.ts → index.d.ts} +1 -1
  49. package/jwt/{web.js → index.js} +12 -65
  50. package/middleware/basic-auth.d.ts +1 -2
  51. package/middleware/basic-auth.js +19 -10
  52. package/middleware/bearer-auth.d.ts +1 -2
  53. package/middleware/bearer-auth.js +6 -6
  54. package/middleware/cache-control.d.ts +2 -3
  55. package/middleware/cache-control.js +3 -4
  56. package/middleware/detect-bot.d.ts +8 -1
  57. package/middleware/detect-bot.js +13 -24
  58. package/middleware/etag.d.ts +13 -0
  59. package/middleware/etag.js +31 -0
  60. package/{bun → middleware}/getConnInfo.d.ts +2 -1
  61. package/{bun → middleware}/getConnInfo.js +2 -1
  62. package/middleware/i18n.d.ts +1 -1
  63. package/middleware/i18n.js +3 -2
  64. package/middleware/index.d.ts +2 -1
  65. package/middleware/index.js +2 -1
  66. package/middleware/logger.d.ts +2 -2
  67. package/middleware/logger.js +3 -3
  68. package/middleware/pagination.d.ts +1 -1
  69. package/middleware/pagination.js +1 -1
  70. package/middleware/powered-by.d.ts +1 -1
  71. package/middleware/rate-limiter.d.ts +5 -10
  72. package/middleware/rate-limiter.js +4 -5
  73. package/middleware/request-id.d.ts +1 -1
  74. package/middleware/sanitize-headers.d.ts +1 -1
  75. package/middleware/xss-protection.d.ts +2 -2
  76. package/package.json +40 -34
  77. package/registry/RadixRouter.js +2 -3
  78. package/{node/serveStatic.d.ts → static/index.d.ts} +2 -10
  79. package/{node/serveStatic.js → static/index.js} +5 -4
  80. package/types/index.d.ts +55 -19
  81. package/utils/crypto.d.ts +9 -0
  82. package/utils/crypto.js +5 -0
  83. package/utils/index.d.ts +14 -0
  84. package/utils/index.js +11 -0
  85. package/utils/response.d.ts +3 -16
  86. package/utils/response.js +20 -49
  87. package/utils/url.d.ts +24 -0
  88. package/utils/url.js +69 -38
  89. package/utils/utils.d.ts +22 -0
  90. package/utils/utils.js +41 -0
  91. package/{bun/ws.d.ts → ws/index.d.ts} +2 -1
  92. package/{bun/ws.js → ws/index.js} +5 -7
  93. package/bun/index.d.ts +0 -15
  94. package/bun/index.js +0 -12
  95. package/cjs/bun/index.js +0 -52
  96. package/cjs/core/error.js +0 -49
  97. package/cjs/deno/env.js +0 -55
  98. package/cjs/deno/getConnInfo.js +0 -18
  99. package/cjs/deno/index.js +0 -17
  100. package/cjs/deno/serveStatic.js +0 -53
  101. package/cjs/deno/ws.js +0 -39
  102. package/cjs/jwt/node.js +0 -94
  103. package/cjs/middleware/secure-headers copy.js +0 -143
  104. package/cjs/middleware/secure-headers.js +0 -157
  105. package/cjs/node/env.js +0 -50
  106. package/cjs/node/getConnInfo.js +0 -16
  107. package/cjs/node/index.js +0 -23
  108. package/cjs/node/mount-node.js +0 -59
  109. package/cjs/node/toWebRequest.js +0 -25
  110. package/cjs/node/ws.js +0 -82
  111. package/cjs/utils/buffer.js +0 -17
  112. package/cjs/utils/file.js +0 -136
  113. package/cjs/utils/httpStatusMap.js +0 -68
  114. package/cjs/utils/low-level.js +0 -115
  115. package/cjs/utils/runtime.js +0 -16
  116. package/core/error.d.ts +0 -96
  117. package/core/error.js +0 -44
  118. package/deno/env.d.ts +0 -5
  119. package/deno/env.js +0 -52
  120. package/deno/getConnInfo.d.ts +0 -21
  121. package/deno/getConnInfo.js +0 -15
  122. package/deno/index.d.ts +0 -14
  123. package/deno/index.js +0 -11
  124. package/deno/serveStatic.d.ts +0 -28
  125. package/deno/serveStatic.js +0 -49
  126. package/deno/ws.d.ts +0 -42
  127. package/deno/ws.js +0 -36
  128. package/jwt/node.d.ts +0 -39
  129. package/jwt/node.js +0 -87
  130. package/middleware/secure-headers copy.d.ts +0 -15
  131. package/middleware/secure-headers copy.js +0 -136
  132. package/middleware/secure-headers.d.ts +0 -132
  133. package/middleware/secure-headers.js +0 -153
  134. package/node/env.d.ts +0 -5
  135. package/node/env.js +0 -47
  136. package/node/getConnInfo.d.ts +0 -21
  137. package/node/getConnInfo.js +0 -13
  138. package/node/index.d.ts +0 -18
  139. package/node/index.js +0 -15
  140. package/node/mount-node.d.ts +0 -11
  141. package/node/mount-node.js +0 -56
  142. package/node/toWebRequest.d.ts +0 -11
  143. package/node/toWebRequest.js +0 -22
  144. package/node/ws.d.ts +0 -56
  145. package/node/ws.js +0 -46
  146. package/utils/buffer.d.ts +0 -1
  147. package/utils/buffer.js +0 -14
  148. package/utils/file.d.ts +0 -38
  149. package/utils/file.js +0 -96
  150. package/utils/generateID.d.ts +0 -42
  151. package/utils/generateID.js +0 -32
  152. package/utils/httpStatusMap.d.ts +0 -15
  153. package/utils/httpStatusMap.js +0 -65
  154. package/utils/low-level.d.ts +0 -58
  155. package/utils/low-level.js +0 -108
  156. package/utils/runtime.d.ts +0 -4
  157. package/utils/runtime.js +0 -12
  158. /package/cjs/{utils/cookie.js → cookie/index.js} +0 -0
  159. /package/{utils/cookie.d.ts → cookie/index.d.ts} +0 -0
  160. /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, high-performance, and lightweight JavaScript framework designed for speed, scalability, and cross-environment compatibility. It offers an intuitive API for routing, middleware management, and static file serving—making it ideal for building web applications with **Node.js**, **Deno**, and **Bun**.
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
  [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tezxjs/TezX)
6
7
 
7
8
  ---
8
9
 
9
- ## 🚀 Features at a Glance
10
+ ## 🚀 Why TezX (Built for Bun)?
10
11
 
11
- - ⚡ **Ultra-Fast Performance** Built for speed and concurrency.
12
- - 🧩 **Minimal & Intuitive API** Clean and easy to use.
13
- - 🗂 **Static File Serving** Serve files with a single command.
14
- - 🔌 **Middleware Support** Stack and compose custom logic.
15
- - 🧭 **Flexible Routing** Dynamic and pattern-based routing.
16
- - 🔐 **Security-First Design** Secure by default.
17
- - 📡 **Efficient HTTP Engine** High-concurrency request handling.
18
- - 🌍 **Cross-Environment** Works with Node.js, Deno, and Bun.
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/logger";
53
- import { createServer } from "node:http";
54
- import { mountTezXOnNode } from "tezx/node";
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>A modern, high-performance, cross-runtime JavaScript framework.</p>
53
+ <p>High-performance JavaScript framework optimized for Bun.</p>
64
54
  `)
65
55
  );
66
56
 
67
- const server = createServer();
68
- mountTezXOnNode(app, server);
57
+ // Server
58
+ const port = Number(process.env.PORT) || 3001;
69
59
 
70
- const PORT = process.env.PORT || 3000;
71
- server.listen(PORT, () => {
72
- console.log(`🚀 TezX is running at http://localhost:${PORT}`);
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
- ### Bun
90
-
91
- ```bash
92
- bun run server.js
93
- ```
74
+ ---
94
75
 
95
- ### Deno
76
+ ## ▶ Run the Server
96
77
 
97
78
  ```bash
98
- deno run --allow-all server.ts
79
+ bun run server.ts
99
80
  ```
100
81
 
101
82
  ---
102
83
 
103
- ## 🔌 Middleware Usage
84
+ ## 🔌 Middleware Example
104
85
 
105
86
  ```ts
106
87
  app.use((ctx, next) => {
107
- console.log(`Incoming request: ${ctx.req.url}`);
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
- app.static("/public", "./public");
98
+ import { serveStatic } from "tezx/static";
99
+
100
+ app.static(serveStatic("/assets", "./assets"));
118
101
  ```
119
102
 
120
- Accessible via: `http://localhost:3000/public/filename.ext`
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 Us</h1>"));
114
+ app.get("/about", (ctx) => ctx.html("<h1>About TezX</h1>"));
128
115
 
129
- app.post("/submit", (ctx) =>
130
- ctx.json({ message: "Form submitted successfully" })
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({ error: "Internal Server Error" });
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
- ### Run Dev Server
149
-
150
- ```bash
151
- npm run dev
152
- # or
153
- bun run dev
154
- ```
139
+ ### `dev` script for Bun
155
140
 
156
- Access: [http://localhost:3000](http://localhost:3000)
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
- #### Example: `src/index.ts`
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 all contributions! Here's how to get started:
169
+ We welcome contributions!
265
170
 
266
- - Fork the repository
267
- - Create a new branch
268
- - Submit a pull request
269
- - Open issues for bugs or ideas
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 is open-source and developed with love. If you find it helpful:
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
- <!-- - [Buy Me a Coffee](https://www.buymeacoffee.com/srakib17) -->
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 and maintain TezX for everyone.
187
+ Your support helps improve the framework.
285
188
 
286
189
  ---
287
190
 
288
191
  ## 🙌 Sponsor
289
192
 
290
- [![papernxt](https://papernxt.com/favicon.ico)](https://papernxt.com)
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
- This project is licensed under the [MIT License](./LICENSE).
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.GlobalConfig = void 0;
3
+ exports.Config = void 0;
4
4
  const debugging_js_1 = require("../utils/debugging.js");
5
- const runtime_js_1 = require("../utils/runtime.js");
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.GlobalConfig = GlobalConfig;
25
+ exports.Config = Config;
@@ -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 error_js_1 = require("./error.js");
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
- #params = {};
12
+ params = {};
15
13
  rawRequest;
16
- #args;
14
+ #server;
17
15
  #body;
18
16
  url;
19
17
  res;
20
- env = {};
21
18
  pathname;
22
19
  method;
23
- constructor(req, pathname, method, env, args) {
24
- this.#args = args;
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.#params));
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 = (status) => {
53
+ status(status) {
67
54
  this.#status = status;
68
55
  return this;
69
- };
56
+ }
70
57
  #newResponse(body, type, init = {}) {
71
- const headers = new Headers(this.#headers);
72
- headers.set("Content-Type", type);
73
- if (init.headers) {
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
- const headers = new Headers(this.#headers);
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, ...args) {
118
- return this.#newResponse((0, response_js_1.toString)(strings, args), "text/html; charset=utf-8", args?.[0]);
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 { body: _body, type } = (0, response_js_1.determineContentTypeBody)(body);
128
- const contentType = init?.headers?.["Content-Type"] ??
129
- init?.headers?.["content-type"] ??
130
- type;
131
- return this.#newResponse(_body, contentType, init);
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
- if (!(await (0, file_js_1.fileExists)(filePath)))
153
- throw error_js_1.TezXError.notFound("File not found");
154
- let { size, mtime } = await (0, file_js_1.fileSize)(filePath);
155
- const ext = (0, low_level_js_1.extensionExtract)(filePath);
156
- const mimeType = mimeTypes_js_1.mimeTypes[ext] ?? mimeTypes_js_1.defaultMimeType;
157
- let fileStream = await (0, file_js_1.readStream)(filePath);
158
- let headers = {
159
- "Content-Type": mimeType,
160
- "Content-Length": size.toString(),
161
- ...init?.headers,
162
- };
163
- let filename = init?.filename;
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
- return this.newResponse(fileStream, {
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 args() {
174
- return this.#args;
149
+ get server() {
150
+ return this.#server;
175
151
  }
176
152
  }
177
153
  exports.Context = Context;
@@ -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 error_js_1.TezXError("Missing Content-Type");
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 error_js_1.TezXError("Multipart body already consumed elsewhere");
90
+ throw new Error("Multipart body already consumed elsewhere");
92
91
  }
93
92
  this.#cachedFormObject = (await this.#rawRequest.formData());
94
93
  this.#bodyConsumed = true;