tezx 2.0.11 → 3.0.0
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 +122 -89
- package/bun/getConnInfo.d.ts +21 -0
- package/bun/getConnInfo.js +9 -0
- package/bun/index.d.ts +10 -4
- package/bun/index.js +8 -4
- package/bun/ws.d.ts +48 -0
- package/bun/ws.js +58 -0
- package/cjs/bun/getConnInfo.js +12 -0
- package/cjs/bun/index.js +35 -7
- package/cjs/bun/ws.js +63 -0
- package/cjs/core/config.js +2 -12
- package/cjs/core/context.js +131 -379
- package/cjs/core/error.js +49 -0
- package/cjs/core/request.js +79 -131
- package/cjs/core/router.js +54 -387
- package/cjs/core/server.js +83 -202
- package/cjs/deno/env.js +4 -4
- package/cjs/deno/getConnInfo.js +18 -0
- package/cjs/deno/index.js +11 -18
- package/cjs/deno/serveStatic.js +53 -0
- package/cjs/deno/ws.js +39 -0
- package/cjs/helper/index.js +46 -10
- package/cjs/index.js +5 -7
- package/cjs/jwt/node.js +94 -0
- package/cjs/jwt/web.js +178 -0
- package/cjs/middleware/basic-auth.js +42 -0
- package/cjs/middleware/bearer-auth.js +34 -0
- package/cjs/middleware/cache-control.js +44 -0
- package/cjs/middleware/cors.js +11 -21
- package/cjs/middleware/detect-bot.js +57 -0
- package/cjs/middleware/i18n.js +73 -60
- package/cjs/middleware/index.js +8 -46
- package/cjs/middleware/logger.js +9 -4
- package/cjs/middleware/pagination.js +3 -2
- package/cjs/middleware/powered-by.js +3 -2
- package/cjs/middleware/rate-limiter.js +38 -0
- package/cjs/middleware/request-id.js +4 -5
- package/cjs/middleware/sanitize-headers.js +22 -0
- package/cjs/middleware/secure-headers copy.js +143 -0
- package/cjs/middleware/secure-headers.js +157 -0
- package/cjs/middleware/{xssProtection.js → xss-protection.js} +5 -8
- package/cjs/node/env.js +7 -7
- package/cjs/node/getConnInfo.js +16 -0
- package/cjs/node/index.js +17 -18
- package/cjs/node/mount-node.js +59 -0
- package/cjs/node/serveStatic.js +56 -0
- package/cjs/node/toWebRequest.js +25 -0
- package/cjs/node/ws.js +82 -0
- package/cjs/registry/RadixRouter.js +148 -0
- package/cjs/registry/index.js +17 -0
- package/cjs/types/headers.js +2 -0
- package/cjs/types/index.js +13 -0
- package/cjs/utils/buffer.js +17 -0
- package/cjs/utils/colors.js +2 -0
- package/cjs/utils/cookie.js +59 -0
- package/cjs/utils/file.js +136 -0
- package/cjs/utils/formData.js +60 -10
- package/cjs/utils/generateID.js +37 -0
- package/cjs/utils/low-level.js +115 -0
- package/cjs/utils/{staticFile.js → mimeTypes.js} +0 -87
- package/cjs/utils/rateLimit.js +41 -0
- package/cjs/utils/response.js +65 -0
- package/cjs/{core/environment.js → utils/runtime.js} +2 -1
- package/cjs/utils/url.js +65 -30
- package/core/config.d.ts +2 -7
- package/core/config.js +2 -12
- package/core/context.d.ts +209 -164
- package/core/context.js +131 -346
- package/core/error.d.ts +96 -0
- package/core/error.js +44 -0
- package/core/request.d.ts +67 -107
- package/core/request.js +78 -130
- package/core/router.d.ts +138 -133
- package/core/router.js +53 -352
- package/core/server.d.ts +99 -38
- package/core/server.js +83 -202
- package/deno/env.js +3 -3
- package/deno/getConnInfo.d.ts +21 -0
- package/deno/getConnInfo.js +15 -0
- package/deno/index.d.ts +9 -4
- package/deno/index.js +7 -4
- package/deno/serveStatic.d.ts +28 -0
- package/deno/serveStatic.js +49 -0
- package/deno/ws.d.ts +42 -0
- package/deno/ws.js +36 -0
- package/helper/index.d.ts +29 -15
- package/helper/index.js +27 -7
- package/index.d.ts +10 -8
- package/index.js +4 -5
- package/jwt/node.d.ts +39 -0
- package/jwt/node.js +87 -0
- package/jwt/web.d.ts +14 -0
- package/jwt/web.js +174 -0
- package/middleware/basic-auth.d.ts +56 -0
- package/middleware/basic-auth.js +38 -0
- package/middleware/bearer-auth.d.ts +53 -0
- package/middleware/bearer-auth.js +30 -0
- package/middleware/cache-control.d.ts +30 -0
- package/middleware/cache-control.js +40 -0
- package/middleware/cors.d.ts +30 -3
- package/middleware/cors.js +12 -22
- package/middleware/detect-bot.d.ts +113 -0
- package/middleware/detect-bot.js +53 -0
- package/middleware/i18n.d.ts +166 -73
- package/middleware/i18n.js +73 -60
- package/middleware/index.d.ts +8 -32
- package/middleware/index.js +8 -44
- package/middleware/logger.d.ts +5 -2
- package/middleware/logger.js +9 -4
- package/middleware/pagination.d.ts +9 -6
- package/middleware/pagination.js +3 -2
- package/middleware/powered-by.d.ts +2 -1
- package/middleware/powered-by.js +3 -2
- package/middleware/{rateLimiter.d.ts → rate-limiter.d.ts} +15 -9
- package/middleware/rate-limiter.js +34 -0
- package/middleware/request-id.d.ts +2 -1
- package/middleware/request-id.js +5 -6
- package/middleware/{sanitizeHeader.d.ts → sanitize-headers.d.ts} +5 -19
- package/middleware/sanitize-headers.js +18 -0
- package/middleware/secure-headers copy.d.ts +15 -0
- package/middleware/secure-headers copy.js +136 -0
- package/middleware/secure-headers.d.ts +132 -0
- package/middleware/secure-headers.js +153 -0
- package/middleware/{xssProtection.d.ts → xss-protection.d.ts} +2 -1
- package/middleware/xss-protection.js +19 -0
- package/node/env.js +4 -4
- package/node/getConnInfo.d.ts +21 -0
- package/node/getConnInfo.js +13 -0
- package/node/index.d.ts +13 -4
- package/node/index.js +11 -4
- package/node/mount-node.d.ts +11 -0
- package/node/mount-node.js +56 -0
- package/node/serveStatic.d.ts +36 -0
- package/node/serveStatic.js +52 -0
- package/node/toWebRequest.js +22 -0
- package/node/ws.d.ts +56 -0
- package/node/ws.js +46 -0
- package/package.json +39 -30
- package/registry/RadixRouter.d.ts +40 -0
- package/registry/RadixRouter.js +144 -0
- package/registry/index.d.ts +2 -0
- package/registry/index.js +1 -0
- package/types/headers.d.ts +2 -0
- package/types/headers.js +1 -0
- package/types/index.d.ts +318 -18
- package/types/index.js +12 -1
- package/utils/buffer.d.ts +1 -0
- package/utils/buffer.js +14 -0
- package/utils/colors.d.ts +24 -0
- package/utils/colors.js +2 -0
- package/utils/cookie.d.ts +55 -0
- package/utils/cookie.js +53 -0
- package/utils/file.d.ts +38 -0
- package/utils/file.js +96 -0
- package/utils/formData.d.ts +41 -1
- package/utils/formData.js +58 -9
- package/utils/generateID.d.ts +42 -0
- package/utils/generateID.js +32 -0
- package/utils/httpStatusMap.d.ts +14 -0
- package/utils/low-level.d.ts +58 -0
- package/utils/low-level.js +108 -0
- package/utils/mimeTypes.d.ts +4 -0
- package/utils/{staticFile.js → mimeTypes.js} +0 -53
- package/utils/rateLimit.d.ts +18 -0
- package/utils/rateLimit.js +37 -0
- package/utils/response.d.ts +18 -0
- package/utils/response.js +58 -0
- package/{core/environment.d.ts → utils/runtime.d.ts} +1 -0
- package/{core/environment.js → utils/runtime.js} +1 -0
- package/utils/url.d.ts +42 -14
- package/utils/url.js +61 -27
- package/bun/adapter.d.ts +0 -127
- package/bun/adapter.js +0 -97
- package/cjs/bun/adapter.js +0 -100
- package/cjs/core/MiddlewareConfigure.js +0 -68
- package/cjs/core/common.js +0 -15
- package/cjs/deno/adpater.js +0 -67
- package/cjs/helper/common.js +0 -17
- package/cjs/middleware/basicAuth.js +0 -71
- package/cjs/middleware/cacheControl.js +0 -90
- package/cjs/middleware/detectBot.js +0 -104
- package/cjs/middleware/detectLocale.js +0 -43
- package/cjs/middleware/lazyLoadModules.js +0 -73
- package/cjs/middleware/rateLimiter.js +0 -24
- package/cjs/middleware/requestTimeout.js +0 -42
- package/cjs/middleware/sanitizeHeader.js +0 -51
- package/cjs/middleware/secureHeaders.js +0 -42
- package/cjs/node/adapter.js +0 -138
- package/cjs/utils/regexRouter.js +0 -58
- package/cjs/utils/state.js +0 -34
- package/cjs/utils/toWebRequest.js +0 -35
- package/cjs/ws/deno.js +0 -20
- package/cjs/ws/index.js +0 -53
- package/cjs/ws/node.js +0 -65
- package/core/MiddlewareConfigure.d.ts +0 -15
- package/core/MiddlewareConfigure.js +0 -63
- package/core/common.d.ts +0 -21
- package/core/common.js +0 -11
- package/deno/adpater.d.ts +0 -38
- package/deno/adpater.js +0 -64
- package/helper/common.d.ts +0 -5
- package/helper/common.js +0 -14
- package/middleware/basicAuth.d.ts +0 -81
- package/middleware/basicAuth.js +0 -67
- package/middleware/cacheControl.d.ts +0 -48
- package/middleware/cacheControl.js +0 -53
- package/middleware/detectBot.d.ts +0 -121
- package/middleware/detectBot.js +0 -98
- package/middleware/detectLocale.d.ts +0 -55
- package/middleware/detectLocale.js +0 -39
- package/middleware/lazyLoadModules.d.ts +0 -72
- package/middleware/lazyLoadModules.js +0 -69
- package/middleware/rateLimiter.js +0 -20
- package/middleware/requestTimeout.d.ts +0 -25
- package/middleware/requestTimeout.js +0 -38
- package/middleware/sanitizeHeader.js +0 -47
- package/middleware/secureHeaders.d.ts +0 -78
- package/middleware/secureHeaders.js +0 -38
- package/middleware/xssProtection.js +0 -22
- package/node/adapter.d.ts +0 -46
- package/node/adapter.js +0 -102
- package/utils/regexRouter.d.ts +0 -66
- package/utils/regexRouter.js +0 -53
- package/utils/state.d.ts +0 -50
- package/utils/state.js +0 -30
- package/utils/staticFile.d.ts +0 -10
- package/utils/toWebRequest.js +0 -32
- package/ws/deno.d.ts +0 -6
- package/ws/deno.js +0 -16
- package/ws/index.d.ts +0 -180
- package/ws/index.js +0 -50
- package/ws/node.d.ts +0 -7
- package/ws/node.js +0 -28
- /package/{utils → node}/toWebRequest.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
# TezX
|
|
1
|
+
# ⚡ TezX – High-Performance JavaScript Framework
|
|
2
2
|
|
|
3
|
-
**TezX** is a
|
|
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**.
|
|
4
4
|
|
|
5
5
|
[](https://deepwiki.com/tezxjs/TezX)
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
-
## 🚀
|
|
9
|
+
## 🚀 Features at a Glance
|
|
10
10
|
|
|
11
|
-
- ⚡ **
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
- 🔌 **
|
|
15
|
-
- 🧭 **
|
|
16
|
-
- 🔐 **Security
|
|
17
|
-
- 📡 **Efficient HTTP
|
|
18
|
-
- 🌍 **Cross-Environment
|
|
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.
|
|
19
19
|
|
|
20
20
|
---
|
|
21
21
|
|
|
@@ -25,11 +25,9 @@
|
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
27
|
npm install tezx
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```bash
|
|
28
|
+
# or
|
|
31
29
|
yarn add tezx
|
|
32
|
-
|
|
30
|
+
```
|
|
33
31
|
|
|
34
32
|
### Bun
|
|
35
33
|
|
|
@@ -42,7 +40,7 @@ bun add tezx
|
|
|
42
40
|
|
|
43
41
|
```ts
|
|
44
42
|
import { TezX } from "https://deno.land/x/tezx/mod.ts";
|
|
45
|
-
```
|
|
43
|
+
```
|
|
46
44
|
-->
|
|
47
45
|
|
|
48
46
|
---
|
|
@@ -51,58 +49,58 @@ import { TezX } from "https://deno.land/x/tezx/mod.ts";
|
|
|
51
49
|
|
|
52
50
|
```ts
|
|
53
51
|
import { TezX } from "tezx";
|
|
54
|
-
import { logger } from "tezx/
|
|
55
|
-
import {
|
|
52
|
+
import { logger } from "tezx/logger";
|
|
53
|
+
import { createServer } from "node:http";
|
|
54
|
+
import { mountTezXOnNode } from "tezx/node";
|
|
56
55
|
|
|
57
56
|
const app = new TezX();
|
|
58
57
|
app.use(logger());
|
|
59
|
-
|
|
60
58
|
app.static("/", "./static");
|
|
61
59
|
|
|
62
|
-
app.get("/", (ctx) =>
|
|
63
|
-
|
|
60
|
+
app.get("/", (ctx) =>
|
|
61
|
+
ctx.html(`
|
|
64
62
|
<h1>Welcome to TezX</h1>
|
|
65
|
-
<p>A modern, high-performance cross-
|
|
66
|
-
`)
|
|
67
|
-
|
|
63
|
+
<p>A modern, high-performance, cross-runtime JavaScript framework.</p>
|
|
64
|
+
`)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const server = createServer();
|
|
68
|
+
mountTezXOnNode(app, server);
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
const PORT = process.env.PORT || 3000;
|
|
71
|
+
server.listen(PORT, () => {
|
|
72
|
+
console.log(`🚀 TezX is running at http://localhost:${PORT}`);
|
|
71
73
|
});
|
|
72
74
|
```
|
|
73
75
|
|
|
74
76
|
---
|
|
75
77
|
|
|
76
|
-
## ▶
|
|
78
|
+
## ▶ Starting the Server
|
|
77
79
|
|
|
78
80
|
### Node.js
|
|
79
81
|
|
|
80
82
|
```bash
|
|
81
83
|
node server.js
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
For development:
|
|
85
|
-
|
|
86
|
-
```bash
|
|
84
|
+
# or
|
|
87
85
|
npm install -g nodemon
|
|
88
86
|
nodemon server.js
|
|
89
87
|
```
|
|
90
88
|
|
|
91
|
-
###
|
|
89
|
+
### Bun
|
|
92
90
|
|
|
93
91
|
```bash
|
|
94
|
-
|
|
92
|
+
bun run server.js
|
|
95
93
|
```
|
|
96
94
|
|
|
97
|
-
###
|
|
95
|
+
### Deno
|
|
98
96
|
|
|
99
97
|
```bash
|
|
100
|
-
|
|
98
|
+
deno run --allow-all server.ts
|
|
101
99
|
```
|
|
102
100
|
|
|
103
101
|
---
|
|
104
102
|
|
|
105
|
-
##
|
|
103
|
+
## 🔌 Middleware Usage
|
|
106
104
|
|
|
107
105
|
```ts
|
|
108
106
|
app.use((ctx, next) => {
|
|
@@ -113,22 +111,24 @@ app.use((ctx, next) => {
|
|
|
113
111
|
|
|
114
112
|
---
|
|
115
113
|
|
|
116
|
-
##
|
|
114
|
+
## 🗂 Static File Serving
|
|
117
115
|
|
|
118
116
|
```ts
|
|
119
117
|
app.static("/public", "./public");
|
|
120
118
|
```
|
|
121
119
|
|
|
122
|
-
|
|
120
|
+
Accessible via: `http://localhost:3000/public/filename.ext`
|
|
123
121
|
|
|
124
122
|
---
|
|
125
123
|
|
|
126
|
-
##
|
|
124
|
+
## 🧭 Routing
|
|
127
125
|
|
|
128
126
|
```ts
|
|
129
127
|
app.get("/about", (ctx) => ctx.html("<h1>About Us</h1>"));
|
|
130
128
|
|
|
131
|
-
app.post("/submit", (ctx) =>
|
|
129
|
+
app.post("/submit", (ctx) =>
|
|
130
|
+
ctx.json({ message: "Form submitted successfully" })
|
|
131
|
+
);
|
|
132
132
|
```
|
|
133
133
|
|
|
134
134
|
---
|
|
@@ -145,72 +145,99 @@ app.onError((err, ctx) => {
|
|
|
145
145
|
|
|
146
146
|
## 🧪 Development Setup
|
|
147
147
|
|
|
148
|
-
### Clone and Install
|
|
149
|
-
|
|
150
|
-
```bash
|
|
151
|
-
git clone https://github.com/tezxjs/tezx-app-example
|
|
152
|
-
cd tezx-app-example
|
|
153
|
-
npm install tezx@latest
|
|
154
|
-
```
|
|
155
|
-
|
|
156
148
|
### Run Dev Server
|
|
157
149
|
|
|
158
150
|
```bash
|
|
159
151
|
npm run dev
|
|
152
|
+
# or
|
|
153
|
+
bun run dev
|
|
160
154
|
```
|
|
161
155
|
|
|
162
|
-
|
|
156
|
+
Access: [http://localhost:3000](http://localhost:3000)
|
|
163
157
|
|
|
164
158
|
---
|
|
165
159
|
|
|
166
160
|
## ⚙️ Platform-Specific Scripts
|
|
167
161
|
|
|
168
|
-
### Node.js
|
|
162
|
+
### Node.js – `package.json`
|
|
169
163
|
|
|
170
164
|
```json
|
|
171
|
-
|
|
172
|
-
"
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
+
}
|
|
180
174
|
}
|
|
181
175
|
```
|
|
182
176
|
|
|
183
|
-
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
### Bun – `package.json`
|
|
184
180
|
|
|
185
181
|
```json
|
|
186
|
-
|
|
187
|
-
"
|
|
182
|
+
{
|
|
183
|
+
"scripts": {
|
|
184
|
+
"dev": "bun run --hot --watch src/index.ts"
|
|
185
|
+
}
|
|
188
186
|
}
|
|
189
187
|
```
|
|
190
188
|
|
|
191
|
-
`src/index.ts
|
|
189
|
+
#### Example: `src/index.ts`
|
|
192
190
|
|
|
193
191
|
```ts
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
192
|
+
Bun.serve({
|
|
193
|
+
port: 3001,
|
|
194
|
+
reusePort: true,
|
|
195
|
+
fetch(req, server) {
|
|
196
|
+
return app.serve(req, server);
|
|
197
|
+
},
|
|
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
|
+
}
|
|
197
219
|
});
|
|
220
|
+
|
|
221
|
+
console.log(`🚀 Server running at http://localhost:${process.env.PORT}`);
|
|
198
222
|
```
|
|
199
223
|
|
|
200
|
-
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
### Deno – `package.json`
|
|
201
227
|
|
|
202
228
|
```json
|
|
203
|
-
|
|
204
|
-
"
|
|
229
|
+
{
|
|
230
|
+
"scripts": {
|
|
231
|
+
"dev": "deno run --watch --allow-all --unstable-sloppy-imports src/index.ts"
|
|
232
|
+
}
|
|
205
233
|
}
|
|
206
234
|
```
|
|
207
235
|
|
|
208
|
-
`src/index.ts
|
|
236
|
+
#### Example: `src/index.ts`
|
|
209
237
|
|
|
210
238
|
```ts
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
console.log(message);
|
|
239
|
+
Deno.serve({ port: Number(Deno.env.get("PORT") || 5000) }, (req, connInfo) => {
|
|
240
|
+
return app.serve(req, connInfo);
|
|
214
241
|
});
|
|
215
242
|
```
|
|
216
243
|
|
|
@@ -218,39 +245,47 @@ denoAdapter(app).listen(3000, (message) => {
|
|
|
218
245
|
|
|
219
246
|
## 🏗 Build & Deployment
|
|
220
247
|
|
|
221
|
-
### Compile TypeScript
|
|
222
|
-
|
|
223
|
-
Using `tsc`:
|
|
248
|
+
### Compile TypeScript
|
|
224
249
|
|
|
225
250
|
```bash
|
|
226
251
|
npm run build
|
|
227
252
|
```
|
|
228
253
|
|
|
254
|
+
Outputs:
|
|
255
|
+
|
|
256
|
+
- CommonJS (`dist/cjs`)
|
|
257
|
+
- ESM (`dist/mjs`)
|
|
258
|
+
- Type Declarations (`dist/types`)
|
|
259
|
+
|
|
229
260
|
---
|
|
230
261
|
|
|
231
262
|
## 🤝 Contributing
|
|
232
263
|
|
|
233
|
-
We welcome contributions!
|
|
264
|
+
We welcome all contributions! Here's how to get started:
|
|
234
265
|
|
|
235
266
|
- Fork the repository
|
|
267
|
+
- Create a new branch
|
|
236
268
|
- Submit a pull request
|
|
237
|
-
- Open
|
|
269
|
+
- Open issues for bugs or ideas
|
|
238
270
|
|
|
239
|
-
GitHub: [https://github.com/tezxjs](https://github.com/tezxjs)
|
|
271
|
+
👉 GitHub: [https://github.com/tezxjs](https://github.com/tezxjs)
|
|
240
272
|
|
|
241
273
|
---
|
|
242
274
|
|
|
243
|
-
## 💖
|
|
275
|
+
## 💖 Support TezX
|
|
276
|
+
|
|
277
|
+
TezX is open-source and developed with love. If you find it helpful:
|
|
244
278
|
|
|
245
|
-
|
|
279
|
+
- 🌟 Star the project on [GitHub](https://github.com/tezxjs/TezX)
|
|
280
|
+
- 💸 [Sponsor on GitHub](https://github.com/sponsors/srakib17)
|
|
246
281
|
|
|
247
|
-
- 🌟 [Star on GitHub](https://github.com/tezxjs/TezX)
|
|
248
282
|
<!-- - ☕ [Buy Me a Coffee](https://www.buymeacoffee.com/srakib17) -->
|
|
249
|
-
- 💸 Become a sponsor on [GitHub Sponsors](https://github.com/sponsors/srakib17)
|
|
250
283
|
|
|
251
|
-
Your support helps
|
|
284
|
+
Your support helps improve and maintain TezX for everyone.
|
|
252
285
|
|
|
253
|
-
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## 🙌 Sponsor
|
|
254
289
|
|
|
255
290
|
[](https://papernxt.com)
|
|
256
291
|
|
|
@@ -258,6 +293,4 @@ Your support helps us maintain and improve TezX for developers around the world.
|
|
|
258
293
|
|
|
259
294
|
## 📜 License
|
|
260
295
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
---
|
|
296
|
+
This project is licensed under the [MIT License](./LICENSE).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Middleware } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Middleware to extract and inject connection information into the request context.
|
|
4
|
+
*
|
|
5
|
+
* This middleware reads the socket's remote address information (like IP, port, and family)
|
|
6
|
+
* from the request object and attaches it to `ctx.req.remoteAddress`.
|
|
7
|
+
*
|
|
8
|
+
* @returns {Middleware<any>} The middleware function that sets `ctx.req.remoteAddress`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import { getConnInfo } from "tezx/bun";
|
|
12
|
+
*
|
|
13
|
+
* app.use(getConnInfo());
|
|
14
|
+
*
|
|
15
|
+
* // Access later in route handler:
|
|
16
|
+
* router.get("/", (ctx) => {
|
|
17
|
+
* const ip = ctx.req.remoteAddress?.address;
|
|
18
|
+
* return new Response(`Your IP: ${ip}`);
|
|
19
|
+
* });
|
|
20
|
+
*/
|
|
21
|
+
export declare function getConnInfo<T extends Record<string, any> = {}, Path extends string = any>(): Middleware<T, Path>;
|
package/bun/index.d.ts
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { loadEnv } from "../node/env.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { serveStatic } from "../node/serveStatic.js";
|
|
3
|
+
import { getConnInfo } from "./getConnInfo.js";
|
|
4
|
+
import upgradeWebSocket, { wsHandlers } from "./ws.js";
|
|
5
|
+
export type { ServeStatic, StaticFileArray, StaticServeOption, WebSocketCallback, WebSocketEvent, WebSocketOptions, } from "../types/index.js";
|
|
6
|
+
export type { wsHandlersOptions } from "./ws.js";
|
|
7
|
+
export { getConnInfo, loadEnv, serveStatic, upgradeWebSocket, wsHandlers };
|
|
5
8
|
declare const _default: {
|
|
6
|
-
|
|
9
|
+
getConnInfo: typeof getConnInfo;
|
|
10
|
+
wsHandlers: (options?: import("./ws.js").wsHandlersOptions) => Bun.WebSocketHandler;
|
|
7
11
|
loadEnv: typeof loadEnv;
|
|
12
|
+
upgradeWebSocket: typeof upgradeWebSocket;
|
|
13
|
+
serveStatic: typeof serveStatic;
|
|
8
14
|
};
|
|
9
15
|
export default _default;
|
package/bun/index.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { loadEnv } from "../node/env.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import { serveStatic } from "../node/serveStatic.js";
|
|
3
|
+
import { getConnInfo } from "./getConnInfo.js";
|
|
4
|
+
import upgradeWebSocket, { wsHandlers } from "./ws.js";
|
|
5
|
+
export { getConnInfo, loadEnv, serveStatic, upgradeWebSocket, wsHandlers };
|
|
5
6
|
export default {
|
|
6
|
-
|
|
7
|
+
getConnInfo,
|
|
8
|
+
wsHandlers,
|
|
7
9
|
loadEnv,
|
|
10
|
+
upgradeWebSocket,
|
|
11
|
+
serveStatic,
|
|
8
12
|
};
|
package/bun/ws.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Middleware, WebSocketCallback, WebSocketOptions } from "../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a middleware that upgrades an HTTP request to a WebSocket connection
|
|
4
|
+
* if the request meets the WebSocket upgrade protocol.
|
|
5
|
+
*
|
|
6
|
+
* Supports Bun natively. Node.js requires `req` + `socket` to be passed correctly.
|
|
7
|
+
*/
|
|
8
|
+
export declare function upgradeWebSocket<T extends Record<string, any> & {
|
|
9
|
+
wsProtocol: "wss" | "ws";
|
|
10
|
+
}, Path extends string = any>(callback: WebSocketCallback, options?: WebSocketOptions): Middleware<T, Path>;
|
|
11
|
+
export type wsHandlersOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* Maximum payload size in bytes. Default: 16 MB.
|
|
14
|
+
*/
|
|
15
|
+
maxPayloadLength?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Maximum backpressure limit in bytes. Default: 16 MB.
|
|
18
|
+
*/
|
|
19
|
+
backpressureLimit?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Close connection when backpressure limit reached. Default: false.
|
|
22
|
+
*/
|
|
23
|
+
closeOnBackpressureLimit?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Idle timeout in seconds. Default: 120 (2 minutes).
|
|
26
|
+
*/
|
|
27
|
+
idleTimeout?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Whether ws.publish() sends message to itself if subscribed. Default: false.
|
|
30
|
+
*/
|
|
31
|
+
publishToSelf?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Whether the server sends/responds to pings automatically. Default: true.
|
|
34
|
+
*/
|
|
35
|
+
sendPings?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Compression settings for per-message deflate.
|
|
38
|
+
*/
|
|
39
|
+
perMessageDeflate?: boolean | {
|
|
40
|
+
compress?: Bun.WebSocketCompressor | boolean;
|
|
41
|
+
decompress?: Bun.WebSocketCompressor | boolean;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Returns a Bun-compatible WebSocketHandler with optional custom options.
|
|
46
|
+
*/
|
|
47
|
+
export declare const wsHandlers: (options?: wsHandlersOptions) => Bun.WebSocketHandler;
|
|
48
|
+
export default upgradeWebSocket;
|
package/bun/ws.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { TezXError } from "../core/error.js";
|
|
2
|
+
export function upgradeWebSocket(callback, options = {}) {
|
|
3
|
+
const { onUpgradeError = (error, ctx) => {
|
|
4
|
+
ctx.setStatus = 401;
|
|
5
|
+
return ctx.text(error.message);
|
|
6
|
+
}, } = options;
|
|
7
|
+
return async (ctx, next) => {
|
|
8
|
+
const upgrade = ctx.req.header("upgrade")?.toLowerCase();
|
|
9
|
+
const connection = ctx.req.header("connection")?.toLowerCase();
|
|
10
|
+
const key = ctx.req.header("sec-websocket-key");
|
|
11
|
+
if (upgrade !== "websocket" || !connection?.includes("upgrade") || !key) {
|
|
12
|
+
if (next) {
|
|
13
|
+
ctx.body = { error: "401 Bad Request: Invalid WebSocket headers" };
|
|
14
|
+
return next();
|
|
15
|
+
}
|
|
16
|
+
ctx.setStatus = 401;
|
|
17
|
+
return onUpgradeError(new TezXError("401 Bad Request: Invalid WebSocket headers", 401), ctx);
|
|
18
|
+
}
|
|
19
|
+
ctx.wsProtocol = ctx.url?.startsWith("https") ? "wss" : "ws";
|
|
20
|
+
if (!callback) {
|
|
21
|
+
throw new TezXError("WebSocket callback is missing. Please provide a valid callback function to handle the WebSocket events.");
|
|
22
|
+
}
|
|
23
|
+
let args = ctx.args[0];
|
|
24
|
+
if (!args?.upgrade) {
|
|
25
|
+
return onUpgradeError(new TezXError("Bun server instance missing for WebSocket"), ctx);
|
|
26
|
+
}
|
|
27
|
+
const success = args.upgrade(ctx.rawRequest, {
|
|
28
|
+
data: callback(ctx),
|
|
29
|
+
});
|
|
30
|
+
if (success)
|
|
31
|
+
return undefined;
|
|
32
|
+
return next();
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export const wsHandlers = (options) => {
|
|
36
|
+
return {
|
|
37
|
+
open(ws) {
|
|
38
|
+
return ws.data?.open?.(ws);
|
|
39
|
+
},
|
|
40
|
+
message(ws, msg) {
|
|
41
|
+
return ws.data?.message?.(ws, msg);
|
|
42
|
+
},
|
|
43
|
+
close(ws, code, reason) {
|
|
44
|
+
return ws.data?.close?.(ws, { code, reason });
|
|
45
|
+
},
|
|
46
|
+
ping(ws, data) {
|
|
47
|
+
return ws.data?.ping?.(ws, data);
|
|
48
|
+
},
|
|
49
|
+
pong(ws, data) {
|
|
50
|
+
return ws.data?.pong?.(ws, data);
|
|
51
|
+
},
|
|
52
|
+
drain(ws) {
|
|
53
|
+
return ws.data?.drain?.(ws);
|
|
54
|
+
},
|
|
55
|
+
...options,
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export default upgradeWebSocket;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getConnInfo = getConnInfo;
|
|
4
|
+
function getConnInfo() {
|
|
5
|
+
return (ctx, next) => {
|
|
6
|
+
let server = ctx.args?.[0];
|
|
7
|
+
if (server && server.requestIP) {
|
|
8
|
+
ctx.req.remoteAddress = server.requestIP(ctx.rawRequest);
|
|
9
|
+
}
|
|
10
|
+
return next();
|
|
11
|
+
};
|
|
12
|
+
}
|
package/cjs/bun/index.js
CHANGED
|
@@ -10,15 +10,43 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
}
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
16
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.wsHandlers = exports.upgradeWebSocket = exports.serveStatic = exports.loadEnv = exports.getConnInfo = void 0;
|
|
17
37
|
const env_js_1 = require("../node/env.js");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
38
|
+
Object.defineProperty(exports, "loadEnv", { enumerable: true, get: function () { return env_js_1.loadEnv; } });
|
|
39
|
+
const serveStatic_js_1 = require("../node/serveStatic.js");
|
|
40
|
+
Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serveStatic_js_1.serveStatic; } });
|
|
41
|
+
const getConnInfo_js_1 = require("./getConnInfo.js");
|
|
42
|
+
Object.defineProperty(exports, "getConnInfo", { enumerable: true, get: function () { return getConnInfo_js_1.getConnInfo; } });
|
|
43
|
+
const ws_js_1 = __importStar(require("./ws.js"));
|
|
44
|
+
exports.upgradeWebSocket = ws_js_1.default;
|
|
45
|
+
Object.defineProperty(exports, "wsHandlers", { enumerable: true, get: function () { return ws_js_1.wsHandlers; } });
|
|
21
46
|
exports.default = {
|
|
22
|
-
|
|
47
|
+
getConnInfo: getConnInfo_js_1.getConnInfo,
|
|
48
|
+
wsHandlers: ws_js_1.wsHandlers,
|
|
23
49
|
loadEnv: env_js_1.loadEnv,
|
|
50
|
+
upgradeWebSocket: ws_js_1.default,
|
|
51
|
+
serveStatic: serveStatic_js_1.serveStatic,
|
|
24
52
|
};
|
package/cjs/bun/ws.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wsHandlers = void 0;
|
|
4
|
+
exports.upgradeWebSocket = upgradeWebSocket;
|
|
5
|
+
const error_js_1 = require("../core/error.js");
|
|
6
|
+
function upgradeWebSocket(callback, options = {}) {
|
|
7
|
+
const { onUpgradeError = (error, ctx) => {
|
|
8
|
+
ctx.setStatus = 401;
|
|
9
|
+
return ctx.text(error.message);
|
|
10
|
+
}, } = options;
|
|
11
|
+
return async (ctx, next) => {
|
|
12
|
+
const upgrade = ctx.req.header("upgrade")?.toLowerCase();
|
|
13
|
+
const connection = ctx.req.header("connection")?.toLowerCase();
|
|
14
|
+
const key = ctx.req.header("sec-websocket-key");
|
|
15
|
+
if (upgrade !== "websocket" || !connection?.includes("upgrade") || !key) {
|
|
16
|
+
if (next) {
|
|
17
|
+
ctx.body = { error: "401 Bad Request: Invalid WebSocket headers" };
|
|
18
|
+
return next();
|
|
19
|
+
}
|
|
20
|
+
ctx.setStatus = 401;
|
|
21
|
+
return onUpgradeError(new error_js_1.TezXError("401 Bad Request: Invalid WebSocket headers", 401), ctx);
|
|
22
|
+
}
|
|
23
|
+
ctx.wsProtocol = ctx.url?.startsWith("https") ? "wss" : "ws";
|
|
24
|
+
if (!callback) {
|
|
25
|
+
throw new error_js_1.TezXError("WebSocket callback is missing. Please provide a valid callback function to handle the WebSocket events.");
|
|
26
|
+
}
|
|
27
|
+
let args = ctx.args[0];
|
|
28
|
+
if (!args?.upgrade) {
|
|
29
|
+
return onUpgradeError(new error_js_1.TezXError("Bun server instance missing for WebSocket"), ctx);
|
|
30
|
+
}
|
|
31
|
+
const success = args.upgrade(ctx.rawRequest, {
|
|
32
|
+
data: callback(ctx),
|
|
33
|
+
});
|
|
34
|
+
if (success)
|
|
35
|
+
return undefined;
|
|
36
|
+
return next();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const wsHandlers = (options) => {
|
|
40
|
+
return {
|
|
41
|
+
open(ws) {
|
|
42
|
+
return ws.data?.open?.(ws);
|
|
43
|
+
},
|
|
44
|
+
message(ws, msg) {
|
|
45
|
+
return ws.data?.message?.(ws, msg);
|
|
46
|
+
},
|
|
47
|
+
close(ws, code, reason) {
|
|
48
|
+
return ws.data?.close?.(ws, { code, reason });
|
|
49
|
+
},
|
|
50
|
+
ping(ws, data) {
|
|
51
|
+
return ws.data?.ping?.(ws, data);
|
|
52
|
+
},
|
|
53
|
+
pong(ws, data) {
|
|
54
|
+
return ws.data?.pong?.(ws, data);
|
|
55
|
+
},
|
|
56
|
+
drain(ws) {
|
|
57
|
+
return ws.data?.drain?.(ws);
|
|
58
|
+
},
|
|
59
|
+
...options,
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
exports.wsHandlers = wsHandlers;
|
|
63
|
+
exports.default = upgradeWebSocket;
|