@ublitzjs/core 1.0.0 → 1.0.1
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/package.json +7 -1
- package/USAGE.md +0 -330
- package/babel.config.json +0 -8
- package/bun.lock +0 -627
- package/logo.png +0 -0
- package/src/http-codes.ts +0 -94
- package/src/http-headers.ts +0 -1136
- package/src/index.ts +0 -360
- package/tmp/cjs.cjs +0 -75
- package/tmp/esm.mjs +0 -75
- package/tsconfig.esm.json +0 -9
- package/tsconfig.json +0 -30
- package/tsconfig.types.json +0 -11
package/package.json
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ublitzjs/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"types": "./dist/types/index.d.ts",
|
|
5
|
+
"files": ["dist"],
|
|
6
|
+
"typesVersions": {
|
|
7
|
+
"*": {
|
|
8
|
+
".": ["./dist/types/index.d.ts"]
|
|
9
|
+
}
|
|
10
|
+
},
|
|
5
11
|
"exports": {
|
|
6
12
|
"types": "./dist/types/index.d.ts",
|
|
7
13
|
"require": "./dist/cjs/index.js",
|
package/USAGE.md
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
> some examples will be shown using typescript, all examples are esm (but can also be cjs)
|
|
2
|
-
|
|
3
|
-
# Setup main file
|
|
4
|
-
|
|
5
|
-
In this section each code sample will show more features, so be ready to see them one by one.
|
|
6
|
-
|
|
7
|
-
## no routers or other imports
|
|
8
|
-
|
|
9
|
-
### without package
|
|
10
|
-
|
|
11
|
-
```typescript
|
|
12
|
-
import uWS from "uWebSockets.js";
|
|
13
|
-
|
|
14
|
-
const server = uWS.App();
|
|
15
|
-
|
|
16
|
-
server.any("/*", (res, req) => {
|
|
17
|
-
// manually handle this
|
|
18
|
-
res.writeStatus("404").end("Nothing to see here!");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
// though I don't use "run", it is needed for example
|
|
22
|
-
function run() {
|
|
23
|
-
server.listen(9001, (token) =>
|
|
24
|
-
token ? console.info("Start success") : console.error("Start failure")
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
run();
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### with core package (low deps)
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import uWS from "uWebSockets.js";
|
|
35
|
-
|
|
36
|
-
// c404 is an ArrayBuffer (not all codes are converted, don't worry), toAB - conversion helper
|
|
37
|
-
import { c404, toAB } from "@ublitzjs/core";
|
|
38
|
-
|
|
39
|
-
const server = uWS.App();
|
|
40
|
-
|
|
41
|
-
var noFoundMessage = toAB("Nothing to see here!");
|
|
42
|
-
server.any("/*", (res, req) => {
|
|
43
|
-
res.writeStatus(c404).end(notFoundMessage);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
function run() {
|
|
47
|
-
server.listen(9001, (token) =>
|
|
48
|
-
token ? console.info("Start success") : console.error("Start failure")
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
run();
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### with core package (better version)
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
import uWS from "uWebSockets.js";
|
|
59
|
-
|
|
60
|
-
// extendApp - lets you use or write extensions, notFoundConstructor - send 404 with a message
|
|
61
|
-
import { extendApp, notFoundConstructor } from "@ublitzjs/core";
|
|
62
|
-
|
|
63
|
-
// all extensions will be typed in typescript, so server.run call is also typed.
|
|
64
|
-
const server = extendApp(
|
|
65
|
-
uWS.App(),
|
|
66
|
-
/* your extensions as a rest parameter. They will be bound to "server" */ {
|
|
67
|
-
run(/*for typescript*/ this: Server) {
|
|
68
|
-
this.listen(9001, (token) =>
|
|
69
|
-
token ? console.info("Start success") : console.error("Start failure")
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
} /*, you can put here as many as you want. */
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
server.any("/*", notFoundConstructor("Nothing to see here!"));
|
|
76
|
-
|
|
77
|
-
// use your extension
|
|
78
|
-
server.run();
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
And last example, just to show real-world use-case for extendApp
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
import uWS from "uWebSockets.js";
|
|
85
|
-
import { extendApp, notFoundConstructor } from "@ublitzjs/core";
|
|
86
|
-
|
|
87
|
-
import { serverExtension as openapiExt } from "@ublitzjs/openapi";
|
|
88
|
-
|
|
89
|
-
const server = extendApp(
|
|
90
|
-
uWS.App(),
|
|
91
|
-
// this is very basic, so for better examples - go find source repo
|
|
92
|
-
openapiExt({
|
|
93
|
-
openapi: "3.0.0",
|
|
94
|
-
info: {
|
|
95
|
-
title: "Some ublitzjs server",
|
|
96
|
-
version: "0.0.1",
|
|
97
|
-
},
|
|
98
|
-
}),
|
|
99
|
-
// your extension can also be here
|
|
100
|
-
{
|
|
101
|
-
run(this: Server) {
|
|
102
|
-
this.listen(9001, (token) =>
|
|
103
|
-
token ? console.info("Start success") : console.error("Start failure")
|
|
104
|
-
);
|
|
105
|
-
},
|
|
106
|
-
}
|
|
107
|
-
);
|
|
108
|
-
// this function is given by the extension
|
|
109
|
-
await server.serveOpenApi("/docs", { build: false, path: "openapi.json" });
|
|
110
|
-
|
|
111
|
-
server.any("/*", notFoundConstructor("Nothing to see here!"));
|
|
112
|
-
|
|
113
|
-
server.run();
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
So in extendApp you put "extensions", as I call them.
|
|
117
|
-
|
|
118
|
-
## With routers (mainly not @ublitzjs/router) and other imports
|
|
119
|
-
|
|
120
|
-
### two modules with core package
|
|
121
|
-
|
|
122
|
-
_main.js_
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
import uWS from "uWebSockets.js";
|
|
126
|
-
import { extendApp } from "@ublitzjs/core";
|
|
127
|
-
import router from "./router.js";
|
|
128
|
-
const server = extendApp(uWS.App());
|
|
129
|
-
// register "plugins", not "extensions"
|
|
130
|
-
server.register(router);
|
|
131
|
-
server.listen(9001, () => {});
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
here you have imported your router and registered it as a plugin. So "extension" gives you NEW functionality, while "plugin" should use your current functionality
|
|
135
|
-
|
|
136
|
-
_router.js_
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
import type { Server } from "@ublitzjs/core";
|
|
140
|
-
// for readability and
|
|
141
|
-
export default (server: Server) => {
|
|
142
|
-
server.get("/simple", (res) => {
|
|
143
|
-
res.end("hello");
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### example with @ublitzjs/router
|
|
149
|
-
|
|
150
|
-
_main.js_ - the same. Nothing changed at all <br>
|
|
151
|
-
_router.js_
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
import type { Server } from "@ublitzjs/core";
|
|
155
|
-
import { Router } from "@ublitzjs/router";
|
|
156
|
-
// again - better explanations in source repo.
|
|
157
|
-
// Generally, router looks like OpenAPI
|
|
158
|
-
const router = new Router({
|
|
159
|
-
// route
|
|
160
|
-
"/simple": {
|
|
161
|
-
// method
|
|
162
|
-
get(res) {
|
|
163
|
-
res.end("hello");
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
});
|
|
167
|
-
export default (server: Server) => {
|
|
168
|
-
router.bind(server).define("/simple", "get");
|
|
169
|
-
};
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
So here I showed you how to setup a main file and split your code into modules
|
|
173
|
-
|
|
174
|
-
# Headers
|
|
175
|
-
|
|
176
|
-
## HeadersMap
|
|
177
|
-
|
|
178
|
-
This class is used to convert all headers to ArrayBuffers and quickly set them on each request.
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
import { HeadersMap, definePlugin, toAB, type Server } from "@ublitzjs/core";
|
|
182
|
-
// init headers
|
|
183
|
-
const htmlHeaders = new HeadersMap({
|
|
184
|
-
"Content-Type": "text/html",
|
|
185
|
-
});
|
|
186
|
-
// convert strings to ArrayBuffers -> it returns a function
|
|
187
|
-
const setHeaders = htmlHeaders.prepare();
|
|
188
|
-
|
|
189
|
-
export default (server: Server) => {
|
|
190
|
-
const html = toAB("<h1>Hello</h1>");
|
|
191
|
-
server.get("/", (res) => {
|
|
192
|
-
// and this function returns "res" object
|
|
193
|
-
setHeaders(res).end(html);
|
|
194
|
-
});
|
|
195
|
-
};
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
Also there is HeadersMap.default (see in the code), HeadersMap.baseObj (also in the code), and HeadersMap.remove (you remove unwanted headers)
|
|
199
|
-
|
|
200
|
-
## setCSP
|
|
201
|
-
|
|
202
|
-
Creates a string of CSP, using array parameters.
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
// in response (not recommended)
|
|
206
|
-
res.writeHeader(
|
|
207
|
-
"Content-Security-Policy",
|
|
208
|
-
setCSP({ "default-src": ["'self'"] })
|
|
209
|
-
);
|
|
210
|
-
// in HeadersMap
|
|
211
|
-
import { setCSP, HeadersMap, /*basic settings*/ CSPDirs } from "@ublitzjs";
|
|
212
|
-
new HeadersMap({
|
|
213
|
-
"Content-Security-Policy": setCSP(
|
|
214
|
-
{
|
|
215
|
-
...CSPDirs,
|
|
216
|
-
"connect-src": ["'self'", "https://example.com"],
|
|
217
|
-
},
|
|
218
|
-
/*remove directives in a rest param*/
|
|
219
|
-
"img-src",
|
|
220
|
-
"object-src"
|
|
221
|
-
),
|
|
222
|
-
}).prepare();
|
|
223
|
-
```
|
|
224
|
-
|
|
225
|
-
## lowHeaders
|
|
226
|
-
|
|
227
|
-
Greatly fits when you need to use request.getHeader("sec-websocket-extensions"), but miss a letter or two. <br>
|
|
228
|
-
See example below in "uWS types / DocumentedWS and DocumentedWSBehavior"
|
|
229
|
-
|
|
230
|
-
# registerAbort
|
|
231
|
-
|
|
232
|
-
this utility lets you extend your handlers in a very asynchronous manner and nothing will ever brake.<br>
|
|
233
|
-
Emitter used here is "tseep", which is faster than node:events.
|
|
234
|
-
|
|
235
|
-
```typescript
|
|
236
|
-
import type { HttpResponse } from "@ublitzjs/core";
|
|
237
|
-
function myOnAbort(res: HttpResponse) {
|
|
238
|
-
res.emitter.once("abort", () => {
|
|
239
|
-
/*do something*/
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
server.get("/", (res) => {
|
|
243
|
-
// use it
|
|
244
|
-
registerAbort(res);
|
|
245
|
-
console.log(/*added on the response*/ res.aborted, res.emitter);
|
|
246
|
-
|
|
247
|
-
res.emitter.once("abort", () => {
|
|
248
|
-
console.log("ABORTED");
|
|
249
|
-
// as an example
|
|
250
|
-
stopDatabaseTransaction();
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
myOnAbort();
|
|
254
|
-
/*here you something*/
|
|
255
|
-
});
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
It is heavily used in @ublitzjs/req-body and @ublitzjs/static to stop file streams
|
|
259
|
-
If you want a more interesting use-case, <a href="./examples/AsyncFunction.mts">Go here</a>
|
|
260
|
-
|
|
261
|
-
# uWS types
|
|
262
|
-
|
|
263
|
-
Another benefit of using this package are additional typescript docs for uWS, that they haven't added yet (I mean index.d.ts has no description yet).
|
|
264
|
-
|
|
265
|
-
## DeclarativeResponse
|
|
266
|
-
|
|
267
|
-
This class helps you define faster and more optimized controllers, because they are prebuilt before execution.<br>
|
|
268
|
-
|
|
269
|
-
Instead of sending response dynamically each time with <code>res.end()</code> you generate the whole response with all headers and body right on the start of the application.<br>
|
|
270
|
-
|
|
271
|
-
It is not something, that ublitzjs created (only <code>.writeHeaders</code> method), but rather just description it gives you with the class
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
import { DeclarativeResponse } from "@ublitzjs";
|
|
275
|
-
server.get(
|
|
276
|
-
"/fast-response",
|
|
277
|
-
new Declarative()
|
|
278
|
-
.writeHeader("Content-Type", "text/plain")
|
|
279
|
-
/*spec method*/ .writeHeaders({ Allow: "GET" })
|
|
280
|
-
.end("HI")
|
|
281
|
-
);
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
## DocumentedWS and DocumentedWSBehavior
|
|
285
|
-
|
|
286
|
-
These types add 3 methods to websocket object: sendFirstFragment, sendFragment, sendLastFragment.<br>
|
|
287
|
-
More detailed description <a href="./types/uws-types.d.ts">here</a>
|
|
288
|
-
|
|
289
|
-
```typescript
|
|
290
|
-
import type { extendApp, DocumentedWSBehavior, lowHeaders } from "@ublitzjs/core";
|
|
291
|
-
import uWS from "uWebSockets.js";
|
|
292
|
-
const server = extendApp(uWS.App()) // better for ts
|
|
293
|
-
server.ws("/*", {
|
|
294
|
-
upgrade(res, req, context) {
|
|
295
|
-
res.upgrade(
|
|
296
|
-
{ url: req.getUrl() },
|
|
297
|
-
req.getHeader</*adds typescript support*/ lowHeaders>(
|
|
298
|
-
"sec-websocket-key"
|
|
299
|
-
),
|
|
300
|
-
req.getHeader<lowHeaders>("sec-websocket-protocol"),
|
|
301
|
-
req.getHeader<lowHeaders>("sec-websocket-extensions"),
|
|
302
|
-
context
|
|
303
|
-
);
|
|
304
|
-
},
|
|
305
|
-
async open(ws) {
|
|
306
|
-
ws.sendFirstFragment("hello1\n");
|
|
307
|
-
ws.sendFragment("hello2\n");
|
|
308
|
-
ws.sendLastFragment("end hello");
|
|
309
|
-
},
|
|
310
|
-
close(ws){
|
|
311
|
-
//typed safety flag
|
|
312
|
-
ws.closed = true;
|
|
313
|
-
}
|
|
314
|
-
message(ws){
|
|
315
|
-
setTimeout(()=>{
|
|
316
|
-
//acts like res.aborted
|
|
317
|
-
if(ws.closed) return;
|
|
318
|
-
ws.send("hello")
|
|
319
|
-
}, 100);
|
|
320
|
-
},
|
|
321
|
-
});
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
# Bundling
|
|
325
|
-
|
|
326
|
-
Best efficiency and start time can be achieved if the code is bundled and minified.<br>
|
|
327
|
-
For this purpose you can use "esbuild" (at least it was tested and worked for both cjs and esm formats).<br>
|
|
328
|
-
The only thing to remember: when you use it for bundling, don't forget to put "uWebSockets.js" to "external" array.<br>
|
|
329
|
-
<a href="./examples/esbuild.mjs">Example 1</a><br>
|
|
330
|
-
<a href="./tests/esbuild.test.ts">Dynamic example 2</a>
|