owebjs 1.5.8-dev → 1.5.9-dev

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 CHANGED
@@ -99,6 +99,7 @@ import Oweb from 'owebjs';
99
99
  const app = await new Oweb({
100
100
  uWebSocketsEnabled: true,
101
101
  poweredByHeader: false,
102
+ autoPreflight: true,
102
103
  staticResponseHeaders: {
103
104
  // CORS (set your real origin in production)
104
105
  'access-control-allow-origin': 'https://yourdomain.com',
@@ -126,6 +127,28 @@ await app.loadRoutes({
126
127
  await app.start({ port: 3000, host: '0.0.0.0' });
127
128
  ```
128
129
 
130
+ ## CORS Configuration
131
+
132
+ Use `autoPreflight` to return `204` for preflight requests and set CORS headers through `staticResponseHeaders`.
133
+
134
+ ```js
135
+ import Oweb from 'owebjs';
136
+
137
+ const app = await new Oweb({
138
+ uWebSocketsEnabled: true,
139
+ autoPreflight: true,
140
+ poweredByHeader: false,
141
+ staticResponseHeaders: {
142
+ 'access-control-allow-origin': 'https://yourdomain.com',
143
+ 'access-control-allow-methods': 'GET,POST,PUT,PATCH,DELETE,OPTIONS',
144
+ 'access-control-allow-headers': 'Content-Type, Authorization',
145
+ vary: 'Origin',
146
+ },
147
+ }).setup();
148
+ ```
149
+
150
+ If you need credentials, do not use `*` for `access-control-allow-origin`; set an explicit origin.
151
+
129
152
  ## First App (2 Minutes)
130
153
 
131
154
  Start with a minimal app, then we will add route conventions step by step.
@@ -167,7 +190,7 @@ routes/
167
190
  [id].js
168
191
  auth/
169
192
  login.post.js
170
- matcher/
193
+ posts/
171
194
  [id=integer].js
172
195
  events/
173
196
  sse.js
@@ -236,7 +259,7 @@ export default class LoginPostRoute extends Route {
236
259
 
237
260
  Matcher params add filename-level validation.
238
261
 
239
- `routes/matcher/[id=integer].js` + `matchers/integer.js`
262
+ `routes/posts/[id=integer].js` + `matchers/integer.js`
240
263
 
241
264
  ```js
242
265
  // matchers/integer.js
package/dist/index.d.ts CHANGED
@@ -21,6 +21,7 @@ export { HTTPMethods } from 'fastify/types/utils';
21
21
  interface OwebOptions extends FastifyServerOptions {
22
22
  uWebSocketsEnabled?: boolean;
23
23
  poweredByHeader?: boolean;
24
+ autoPreflight?: boolean;
24
25
  staticResponseHeaders?: Record<string, string>;
25
26
  OWEB_INTERNAL_ERROR_HANDLER?: Function;
26
27
  }
@@ -29,6 +29,7 @@ class Oweb extends _FastifyInstance {
29
29
  this._options = options ?? {};
30
30
  this._options.uWebSocketsEnabled ??= false;
31
31
  this._options.poweredByHeader ??= true;
32
+ this._options.autoPreflight ??= false;
32
33
  this._options.OWEB_INTERNAL_ERROR_HANDLER ??= (_, res, err) => {
33
34
  return res.status(500).send({
34
35
  error: err.message
@@ -62,7 +63,9 @@ class Oweb extends _FastifyInstance {
62
63
  if (this._options.uWebSocketsEnabled) {
63
64
  const serverimp = (await import("../uwebsocket/server.js")).default;
64
65
  const server = await serverimp({
65
- staticResponseHeaders: this._options.staticResponseHeaders
66
+ staticResponseHeaders: this._options.staticResponseHeaders,
67
+ autoPreflight: this._options.autoPreflight,
68
+ poweredByHeader: this._options.poweredByHeader
66
69
  });
67
70
  this.uServer = server;
68
71
  this._options.serverFactory = (handler) => {
@@ -94,6 +97,11 @@ class Oweb extends _FastifyInstance {
94
97
  done();
95
98
  });
96
99
  }
100
+ if (this._options.autoPreflight && !this._options.uWebSocketsEnabled) {
101
+ fastify.options("/*", (_req, res) => {
102
+ return res.status(204).send();
103
+ });
104
+ }
97
105
  const internalKV = this._internalKV;
98
106
  fastify.addHook("onClose", async () => {
99
107
  const watchers = internalKV.get(HMR_WATCHERS_KEY);
@@ -6,7 +6,7 @@ const REQUEST_EVENT = "request";
6
6
  import HttpRequest from './request.js';
7
7
  import HttpResponse from './response.js';
8
8
  import http from "node:http";
9
- async function server_default({ cert_file_name, key_file_name, staticResponseHeaders }) {
9
+ async function server_default({ cert_file_name, key_file_name, staticResponseHeaders, autoPreflight, poweredByHeader }) {
10
10
  let uWS;
11
11
  uWS = (await import("uWebSockets.js")).default;
12
12
  let appType = "App";
@@ -37,6 +37,27 @@ async function server_default({ cert_file_name, key_file_name, staticResponseHea
37
37
  const query = req.getQuery();
38
38
  const url = req.getUrl();
39
39
  const requiresBody = method !== "HEAD" && method !== "GET";
40
+ if (autoPreflight && method === "OPTIONS") {
41
+ res.writeStatus("204 No Content");
42
+ let hasPoweredByHeader = false;
43
+ if (normalizedStaticHeaders?.length) {
44
+ for (let i = 0; i < normalizedStaticHeaders.length; i++) {
45
+ const [key, value] = normalizedStaticHeaders[i];
46
+ if (key === "content-length" || key === "transfer-encoding") {
47
+ continue;
48
+ }
49
+ if (key === "x-powered-by") {
50
+ hasPoweredByHeader = true;
51
+ }
52
+ res.writeHeader(key, value);
53
+ }
54
+ }
55
+ if (poweredByHeader && !hasPoweredByHeader) {
56
+ res.writeHeader("x-powered-by", "Oweb");
57
+ }
58
+ res.end();
59
+ return;
60
+ }
40
61
  res.finished = false;
41
62
  res.aborted = false;
42
63
  if (requiresBody) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "owebjs",
3
- "version": "1.5.8-dev",
3
+ "version": "1.5.9-dev",
4
4
  "description": "A flexible and modern web framework built on top of Fastify",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/0http.js DELETED
@@ -1,8 +0,0 @@
1
- import cero from '0http';
2
- const { router, server } = cero();
3
-
4
- router.get('/', (req, res) => {
5
- res.end('Hello, World!');
6
- });
7
-
8
- server.listen(3000);
package/allahbeler2.png DELETED
Binary file
package/allahbeyler.png DELETED
Binary file
package/avatar.png DELETED
Binary file
package/benchmark.txt DELETED
@@ -1,191 +0,0 @@
1
- OWEB UWS:
2
-
3
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
4
- Running 40s test @ http://localhost:3000
5
- 100 connections with 10 pipelining factor
6
-
7
- running [=================== ] 95%
8
- ┌─────────┬──────┬───────┬───────┬───────┬──────────┬──────────┬────────┐
9
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
10
- ├─────────┼──────┼───────┼───────┼───────┼──────────┼──────────┼────────┤
11
- │ Latency │ 5 ms │ 12 ms │ 73 ms │ 98 ms │ 15.81 ms │ 15.73 ms │ 158 ms │
12
- └─────────┴──────┴───────┴───────┴───────┴──────────┴──────────┴────────┘
13
- ┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬─────────┐
14
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
15
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
16
- │ Req/Sec │ 57.087 │ 57.087 │ 64.991 │ 72.191 │ 64.785,27 │ 3.607,71 │ 57.060 │
17
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
18
- │ Bytes/Sec │ 9.42 MB │ 9.42 MB │ 10.7 MB │ 11.9 MB │ 10.7 MB │ 596 kB │ 9.41 MB │
19
- └───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴─────────┘
20
-
21
- Req/Bytes counts sampled once per second.
22
- # of samples: 38
23
-
24
- 2463k requests in 40.19s, 406 MB read
25
- ╭─ pwsh      40s 459ms⠀     6,13:14 
26
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
27
- Running 40s test @ http://localhost:3000
28
- 100 connections with 10 pipelining factor
29
-
30
-
31
- ┌─────────┬──────┬───────┬───────┬───────┬─────────┬──────────┬────────┐
32
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
33
- ├─────────┼──────┼───────┼───────┼───────┼─────────┼──────────┼────────┤
34
- │ Latency │ 5 ms │ 13 ms │ 72 ms │ 98 ms │ 15.9 ms │ 15.27 ms │ 262 ms │
35
- └─────────┴──────┴───────┴───────┴───────┴─────────┴──────────┴────────┘
36
- ┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬──────────┬─────────┐
37
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
38
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
39
- │ Req/Sec │ 48.959 │ 48.959 │ 64.447 │ 70.783 │ 63.949,54 │ 4.776,67 │ 48.930 │
40
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼──────────┼─────────┤
41
- │ Bytes/Sec │ 8.08 MB │ 8.08 MB │ 10.6 MB │ 11.7 MB │ 10.6 MB │ 789 kB │ 8.07 MB │
42
- └───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴──────────┴─────────┘
43
-
44
- Req/Bytes counts sampled once per second.
45
- # of samples: 39
46
-
47
- 2495k requests in 40.96s, 412 MB read
48
-
49
-
50
- OWEB FASTIFY:
51
-
52
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
53
- Running 40s test @ http://localhost:3000
54
- 100 connections with 10 pipelining factor
55
-
56
-
57
- ┌─────────┬──────┬───────┬───────┬────────┬──────────┬──────────┬────────┐
58
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
59
- ├─────────┼──────┼───────┼───────┼────────┼──────────┼──────────┼────────┤
60
- │ Latency │ 3 ms │ 21 ms │ 84 ms │ 110 ms │ 26.12 ms │ 20.65 ms │ 200 ms │
61
- └─────────┴──────┴───────┴───────┴────────┴──────────┴──────────┴────────┘
62
- ┌───────────┬─────────┬─────────┬─────────┬────────┬──────────┬──────────┬─────────┐
63
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
64
- ├───────────┼─────────┼─────────┼─────────┼────────┼──────────┼──────────┼─────────┤
65
- │ Req/Sec │ 26.751 │ 26.751 │ 36.287 │ 45.503 │ 37.570,6 │ 4.606,09 │ 26.741 │
66
- ├───────────┼─────────┼─────────┼─────────┼────────┼──────────┼──────────┼─────────┤
67
- │ Bytes/Sec │ 4.76 MB │ 4.76 MB │ 6.46 MB │ 8.1 MB │ 6.69 MB │ 820 kB │ 4.76 MB │
68
- └───────────┴─────────┴─────────┴─────────┴────────┴──────────┴──────────┴─────────┘
69
-
70
- Req/Bytes counts sampled once per second.
71
- # of samples: 40
72
-
73
- 1504k requests in 40.05s, 268 MB read
74
- ╭─ pwsh      40s 334ms⠀     6,13:16 
75
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
76
- Running 40s test @ http://localhost:3000
77
- 100 connections with 10 pipelining factor
78
-
79
-
80
- ┌─────────┬──────┬───────┬───────┬────────┬──────────┬──────────┬────────┐
81
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
82
- ├─────────┼──────┼───────┼───────┼────────┼──────────┼──────────┼────────┤
83
- │ Latency │ 4 ms │ 20 ms │ 93 ms │ 122 ms │ 24.03 ms │ 21.64 ms │ 209 ms │
84
- └─────────┴──────┴───────┴───────┴────────┴──────────┴──────────┴────────┘
85
- ┌───────────┬────────┬────────┬─────────┬─────────┬───────────┬──────────┬────────┐
86
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
87
- ├───────────┼────────┼────────┼─────────┼─────────┼───────────┼──────────┼────────┤
88
- │ Req/Sec │ 29.759 │ 29.759 │ 41.343 │ 44.895 │ 40.765,81 │ 3.004,68 │ 29.750 │
89
- ├───────────┼────────┼────────┼─────────┼─────────┼───────────┼──────────┼────────┤
90
- │ Bytes/Sec │ 5.3 MB │ 5.3 MB │ 7.36 MB │ 7.99 MB │ 7.26 MB │ 535 kB │ 5.3 MB │
91
- └───────────┴────────┴────────┴─────────┴─────────┴───────────┴──────────┴────────┘
92
-
93
- Req/Bytes counts sampled once per second.
94
- # of samples: 40
95
-
96
- 1632k requests in 40.05s, 290 MB read
97
-
98
- DÜZ FASTIFY:
99
-
100
- Running 40s test @ http://localhost:3000
101
- 100 connections with 10 pipelining factor
102
-
103
-
104
- ┌─────────┬──────┬───────┬───────┬───────┬──────────┬──────────┬────────┐
105
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
106
- ├─────────┼──────┼───────┼───────┼───────┼──────────┼──────────┼────────┤
107
- │ Latency │ 3 ms │ 18 ms │ 76 ms │ 97 ms │ 21.09 ms │ 17.82 ms │ 257 ms │
108
- └─────────┴──────┴───────┴───────┴───────┴──────────┴──────────┴────────┘
109
- ┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬──────────┬─────────┐
110
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
111
- ├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
112
- │ Req/Sec │ 32.543 │ 32.543 │ 46.783 │ 49.695 │ 46.349,8 │ 2.964,89 │ 32.530 │
113
- ├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
114
- │ Bytes/Sec │ 5.79 MB │ 5.79 MB │ 8.33 MB │ 8.85 MB │ 8.25 MB │ 528 kB │ 5.79 MB │
115
- └───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴──────────┴─────────┘
116
-
117
- Req/Bytes counts sampled once per second.
118
- # of samples: 40
119
-
120
- 1855k requests in 40.06s, 330 MB read
121
- ╭─ pwsh      40s 362ms⠀     6,13:18 
122
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
123
- Running 40s test @ http://localhost:3000
124
- 100 connections with 10 pipelining factor
125
-
126
-
127
- ┌─────────┬──────┬───────┬───────┬────────┬──────────┬─────────┬────────┐
128
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
129
- ├─────────┼──────┼───────┼───────┼────────┼──────────┼─────────┼────────┤
130
- │ Latency │ 3 ms │ 18 ms │ 80 ms │ 102 ms │ 21.48 ms │ 18.3 ms │ 188 ms │
131
- └─────────┴──────┴───────┴───────┴────────┴──────────┴─────────┴────────┘
132
- ┌───────────┬─────────┬─────────┬─────────┬────────┬──────────┬──────────┬─────────┐
133
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
134
- ├───────────┼─────────┼─────────┼─────────┼────────┼──────────┼──────────┼─────────┤
135
- │ Req/Sec │ 37.567 │ 37.567 │ 45.535 │ 48.863 │ 45.501,6 │ 2.214,24 │ 37.550 │
136
- ├───────────┼─────────┼─────────┼─────────┼────────┼──────────┼──────────┼─────────┤
137
- │ Bytes/Sec │ 6.68 MB │ 6.68 MB │ 8.11 MB │ 8.7 MB │ 8.1 MB │ 394 kB │ 6.68 MB │
138
- └───────────┴─────────┴─────────┴─────────┴────────┴──────────┴──────────┴─────────┘
139
-
140
- Req/Bytes counts sampled once per second.
141
- # of samples: 40
142
-
143
- 1821k requests in 40.05s, 324 MB read
144
-
145
- DÜZ UWS:
146
-
147
- Running 40s test @ http://localhost:3000
148
- 100 connections with 10 pipelining factor
149
-
150
- running [=================== ] 95%
151
- ┌─────────┬───────┬───────┬───────┬───────┬──────────┬─────────┬────────┐
152
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
153
- ├─────────┼───────┼───────┼───────┼───────┼──────────┼─────────┼────────┤
154
- │ Latency │ 11 ms │ 12 ms │ 19 ms │ 22 ms │ 12.93 ms │ 3.39 ms │ 167 ms │
155
- └─────────┴───────┴───────┴───────┴───────┴──────────┴─────────┴────────┘
156
- ┌───────────┬─────────┬─────────┬─────────┬─────────┬───────────┬─────────┬─────────┐
157
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
158
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼─────────┼─────────┤
159
- │ Req/Sec │ 71.743 │ 71.743 │ 80.831 │ 80.959 │ 79.149,48 │ 3.427,1 │ 71.730 │
160
- ├───────────┼─────────┼─────────┼─────────┼─────────┼───────────┼─────────┼─────────┤
161
- │ Bytes/Sec │ 10.6 MB │ 10.6 MB │ 11.9 MB │ 11.9 MB │ 11.6 MB │ 503 kB │ 10.5 MB │
162
- └───────────┴─────────┴─────────┴─────────┴─────────┴───────────┴─────────┴─────────┘
163
-
164
- Req/Bytes counts sampled once per second.
165
- # of samples: 38
166
-
167
- 3009k requests in 40.43s, 442 MB read
168
- ╭─ pwsh      40s 739ms⠀     6,13:20 
169
- ╰─ autocannon -c 100 -d 40 -p 10 localhost:3000
170
- Running 40s test @ http://localhost:3000
171
- 100 connections with 10 pipelining factor
172
-
173
-
174
- ┌─────────┬───────┬───────┬───────┬───────┬──────────┬─────────┬────────┐
175
- │ Stat │ 2.5% │ 50% │ 97.5% │ 99% │ Avg │ Stdev │ Max │
176
- ├─────────┼───────┼───────┼───────┼───────┼──────────┼─────────┼────────┤
177
- │ Latency │ 10 ms │ 12 ms │ 20 ms │ 22 ms │ 12.44 ms │ 3.35 ms │ 181 ms │
178
- └─────────┴───────┴───────┴───────┴───────┴──────────┴─────────┴────────┘
179
- ┌───────────┬────────┬────────┬─────────┬─────────┬───────────┬──────────┬────────┐
180
- │ Stat │ 1% │ 2.5% │ 50% │ 97.5% │ Avg │ Stdev │ Min │
181
- ├───────────┼────────┼────────┼─────────┼─────────┼───────────┼──────────┼────────┤
182
- │ Req/Sec │ 68.287 │ 68.287 │ 80.831 │ 89.855 │ 81.377,65 │ 4.922,97 │ 68.284 │
183
- ├───────────┼────────┼────────┼─────────┼─────────┼───────────┼──────────┼────────┤
184
- │ Bytes/Sec │ 10 MB │ 10 MB │ 11.9 MB │ 13.2 MB │ 12 MB │ 724 kB │ 10 MB │
185
- └───────────┴────────┴────────┴─────────┴─────────┴───────────┴──────────┴────────┘
186
-
187
- Req/Bytes counts sampled once per second.
188
- # of samples: 39
189
-
190
- 3175k requests in 41.09s, 467 MB read
191
-
package/express.js DELETED
@@ -1,14 +0,0 @@
1
- import express from 'express';
2
-
3
- const app = express();
4
- const port = 3000;
5
-
6
- // Define a route for the root URL
7
- app.get('/', (req, res) => {
8
- res.send('Hello World from Express!');
9
- });
10
-
11
- // Start the server
12
- app.listen(port, () => {
13
- console.log(`Express app listening at http://localhost:${port}`);
14
- });
package/fasti.js DELETED
@@ -1,14 +0,0 @@
1
- import fastify from 'fastify';
2
-
3
- const app = fastify();
4
-
5
- app.get('/', async (request, reply) => {
6
- return 'Hello, World!';
7
- });
8
-
9
- app.listen({ port: 3000, host: '0.0.0.0' }, (err) => {
10
- if (err) {
11
- console.error(err);
12
- process.exit(1);
13
- }
14
- });
package/purehttp.js DELETED
@@ -1,19 +0,0 @@
1
- import http from 'node:http';
2
-
3
- const hostname = '127.0.0.1';
4
- const port = 3000;
5
-
6
- // Create the server instance
7
- const server = http.createServer((req, res) => {
8
- // Set the response header with HTTP status and Content-Type
9
- res.statusCode = 200;
10
- res.setHeader('Content-Type', 'text/plain');
11
-
12
- // Send the response body
13
- res.end('Hello, World!\n');
14
- });
15
-
16
- // Start listening for requests
17
- server.listen(port, hostname, () => {
18
- console.log(`Server running at http://${hostname}:${port}/`);
19
- });
package/uws.js DELETED
@@ -1,16 +0,0 @@
1
- import uWS from 'uwebsockets.js';
2
-
3
- uWS.App()
4
- .get('/', (res, req) => {
5
- res.writeStatus('200 OK')
6
- .writeHeader('Content-Type', 'text/plain; charset=utf-8')
7
- .end('Hello, World!');
8
- })
9
- .listen('0.0.0.0', 3000, (listenSocket) => {
10
- if (listenSocket) {
11
- console.log('uWebSockets.js listening on http://localhost:3000');
12
- } else {
13
- console.error('Failed to listen on port 3000');
14
- process.exit(1);
15
- }
16
- });