tspace-spear 1.2.2 → 1.2.4
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 +304 -23
- package/dist/lib/core/const/index.d.ts +153 -0
- package/dist/lib/core/const/index.js +105 -0
- package/dist/lib/core/const/index.js.map +1 -0
- package/dist/lib/core/decorators/context.d.ts +16 -9
- package/dist/lib/core/decorators/context.js +85 -59
- package/dist/lib/core/decorators/context.js.map +1 -1
- package/dist/lib/core/decorators/headers.d.ts +2 -2
- package/dist/lib/core/decorators/headers.js +1 -1
- package/dist/lib/core/decorators/headers.js.map +1 -1
- package/dist/lib/core/decorators/methods.d.ts +7 -7
- package/dist/lib/core/decorators/methods.js.map +1 -1
- package/dist/lib/core/decorators/middleware.d.ts +3 -3
- package/dist/lib/core/decorators/middleware.js +2 -2
- package/dist/lib/core/decorators/middleware.js.map +1 -1
- package/dist/lib/core/decorators/statusCode.d.ts +1 -1
- package/dist/lib/core/decorators/statusCode.js.map +1 -1
- package/dist/lib/core/decorators/swagger.d.ts +1 -1
- package/dist/lib/core/decorators/swagger.js.map +1 -1
- package/dist/lib/core/server/express.d.ts +295 -0
- package/dist/lib/core/server/express.js +1356 -0
- package/dist/lib/core/server/express.js.map +1 -0
- package/dist/lib/core/server/fast-router.d.ts +133 -0
- package/dist/lib/core/server/fast-router.js +277 -0
- package/dist/lib/core/server/fast-router.js.map +1 -0
- package/dist/lib/core/server/index.d.ts +43 -36
- package/dist/lib/core/server/index.js +300 -426
- package/dist/lib/core/server/index.js.map +1 -1
- package/dist/lib/core/server/net/index.d.ts +20 -0
- package/dist/lib/core/server/net/index.js +393 -0
- package/dist/lib/core/server/net/index.js.map +1 -0
- package/dist/lib/core/server/parser-factory.d.ts +20 -12
- package/dist/lib/core/server/parser-factory.js +283 -196
- package/dist/lib/core/server/parser-factory.js.map +1 -1
- package/dist/lib/core/server/request.d.ts +2 -0
- package/dist/lib/core/server/request.js +7 -0
- package/dist/lib/core/server/request.js.map +1 -0
- package/dist/lib/core/server/response.d.ts +6 -0
- package/dist/lib/core/server/response.js +168 -0
- package/dist/lib/core/server/response.js.map +1 -0
- package/dist/lib/core/server/router.d.ts +2 -2
- package/dist/lib/core/server/router.js +2 -1
- package/dist/lib/core/server/router.js.map +1 -1
- package/dist/lib/core/server/uWS/index.d.ts +30 -0
- package/dist/lib/core/server/uWS/index.js +357 -0
- package/dist/lib/core/server/uWS/index.js.map +1 -0
- package/dist/lib/core/types/index.d.ts +159 -38
- package/dist/lib/core/utils/index.d.ts +12 -0
- package/dist/lib/core/utils/index.js +137 -0
- package/dist/lib/core/utils/index.js.map +1 -0
- package/dist/lib/index.d.ts +3 -3
- package/dist/lib/index.js +3 -2
- package/dist/lib/index.js.map +1 -1
- package/package.json +27 -11
- package/dist/tests/benchmark.test.d.ts +0 -1
- package/dist/tests/benchmark.test.js +0 -145
- package/dist/tests/benchmark.test.js.map +0 -1
package/README.md
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
[](https://www.npmjs.com)
|
|
4
4
|
[](https://www.npmjs.com)
|
|
5
5
|
|
|
6
|
-
tspace-spear is a lightweight API framework for Node.js
|
|
7
|
-
|
|
6
|
+
tspace-spear is a lightweight and high-performance API framework for Node.js,
|
|
7
|
+
built on the native HTTP server with optional support for uWebSockets.js (C++) to achieve maximum speed and efficiency.
|
|
8
|
+
|
|
9
|
+
It is designed with a strong focus on delivering an excellent developer experience.
|
|
8
10
|
|
|
9
11
|
## Install
|
|
10
12
|
|
|
@@ -14,8 +16,14 @@ Install with [npm](https://www.npmjs.com/):
|
|
|
14
16
|
npm install tspace-spear --save
|
|
15
17
|
|
|
16
18
|
```
|
|
19
|
+
|
|
20
|
+
## Documentation
|
|
21
|
+
|
|
22
|
+
See the [`docs`](https://thanathip41.github.io/tspace-spear) directory for full documentation.
|
|
23
|
+
|
|
17
24
|
## Basic Usage
|
|
18
25
|
- [Start Server](#start-server)
|
|
26
|
+
- [Adapter](#adapter)
|
|
19
27
|
- [Cluster](#cluster)
|
|
20
28
|
- [Global Prefix](#global-prefix)
|
|
21
29
|
- [Logger](#logger)
|
|
@@ -29,9 +37,10 @@ npm install tspace-spear --save
|
|
|
29
37
|
- [Cookie](#cookie)
|
|
30
38
|
- [Middleware](#middleware)
|
|
31
39
|
- [Controller](#controller)
|
|
40
|
+
- [Dto](#dto)
|
|
32
41
|
- [Router](#router)
|
|
33
42
|
- [Swagger](#swagger)
|
|
34
|
-
- [WebSocket](#
|
|
43
|
+
- [WebSocket](#websocket)
|
|
35
44
|
- [Example CRUD](#example-crud)
|
|
36
45
|
|
|
37
46
|
## Start Server
|
|
@@ -49,7 +58,37 @@ new Spear()
|
|
|
49
58
|
|
|
50
59
|
```
|
|
51
60
|
|
|
61
|
+
## Adapter
|
|
62
|
+
tspace-spear supports multiple server adapters,
|
|
63
|
+
including the native Node.js HTTP server and uWebSockets.js for high performance.
|
|
64
|
+
|
|
65
|
+
⚠️ Requirements for uWebSockets.js
|
|
66
|
+
Node.js 18 or higher is required
|
|
67
|
+
Installation is done via GitHub (no official npm release)
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
import { Spear } from "tspace-spear";
|
|
71
|
+
import uWS from "uWebSockets.js";
|
|
72
|
+
|
|
73
|
+
// Install via package.json
|
|
74
|
+
// "dependencies": {
|
|
75
|
+
// "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.45.0"
|
|
76
|
+
// }
|
|
77
|
+
|
|
78
|
+
new Spear({ adapter: uWS })
|
|
79
|
+
.get("/", () => "Hello world!")
|
|
80
|
+
.get("/json", () => {
|
|
81
|
+
return {
|
|
82
|
+
message: "Hello world!",
|
|
83
|
+
};
|
|
84
|
+
})
|
|
85
|
+
.listen(3000, () =>
|
|
86
|
+
console.log("uWS server is running at http://localhost:3000")
|
|
87
|
+
);
|
|
88
|
+
```
|
|
89
|
+
|
|
52
90
|
## Cluster
|
|
91
|
+
Cluster mode allows tspace-spear to run multiple worker processes to fully utilize multi-core CPU performance.
|
|
53
92
|
```js
|
|
54
93
|
import { Spear } from "tspace-spear";
|
|
55
94
|
new Spear({
|
|
@@ -66,6 +105,9 @@ new Spear({
|
|
|
66
105
|
```
|
|
67
106
|
|
|
68
107
|
## Global Prefix
|
|
108
|
+
Global Prefix allows you to define a base path for all routes in your application.
|
|
109
|
+
|
|
110
|
+
It helps keep your API structured and consistent (e.g. /api, /v1, /app).
|
|
69
111
|
```js
|
|
70
112
|
const app = new Spear({
|
|
71
113
|
globalPrefix : '/api' // prefix all routes
|
|
@@ -77,6 +119,16 @@ const app = new Spear({
|
|
|
77
119
|
```
|
|
78
120
|
|
|
79
121
|
## Logger
|
|
122
|
+
The built-in Logger provides request logging for incoming HTTP requests.
|
|
123
|
+
|
|
124
|
+
It helps you monitor:
|
|
125
|
+
|
|
126
|
+
Request method
|
|
127
|
+
Request path
|
|
128
|
+
Performance tracking
|
|
129
|
+
Debugging and observability
|
|
130
|
+
|
|
131
|
+
You can enable a simple logger or configure advanced logging behavior.
|
|
80
132
|
```js
|
|
81
133
|
const app = new Spear({
|
|
82
134
|
logger : true
|
|
@@ -92,8 +144,17 @@ const app = new Spear({
|
|
|
92
144
|
```
|
|
93
145
|
|
|
94
146
|
## Format Response
|
|
147
|
+
Provides a consistent response structure system to standardize how responses are handled across your application.
|
|
148
|
+
|
|
149
|
+
It ensures that:
|
|
150
|
+
|
|
151
|
+
* All responses are predictable
|
|
152
|
+
* Errors are properly structured
|
|
153
|
+
* Missing routes are handled cleanly
|
|
154
|
+
* Global error catching is supported
|
|
95
155
|
|
|
96
156
|
### Notfound
|
|
157
|
+
The NotFound handler is triggered when no route matches the incoming request.
|
|
97
158
|
```js
|
|
98
159
|
const app = new Spear()
|
|
99
160
|
.get('/' , () => {
|
|
@@ -110,6 +171,13 @@ const app = new Spear()
|
|
|
110
171
|
```
|
|
111
172
|
|
|
112
173
|
### Response
|
|
174
|
+
The response system ensures that all returned values are automatically formatted and sent to the client.
|
|
175
|
+
|
|
176
|
+
You can return:
|
|
177
|
+
|
|
178
|
+
* String
|
|
179
|
+
* Object (JSON)
|
|
180
|
+
* Custom response via ctx.res
|
|
113
181
|
```js
|
|
114
182
|
import { Spear } from "tspace-spear";
|
|
115
183
|
|
|
@@ -151,6 +219,10 @@ const app = new Spear()
|
|
|
151
219
|
```
|
|
152
220
|
|
|
153
221
|
### Catch
|
|
222
|
+
The Catch handler is used to handle unexpected runtime errors globally.
|
|
223
|
+
|
|
224
|
+
It acts as a safety layer to prevent server crashes and standardize error responses.
|
|
225
|
+
|
|
154
226
|
```js
|
|
155
227
|
import { Spear } from "tspace-spear";
|
|
156
228
|
import { z } from "zod";
|
|
@@ -185,7 +257,9 @@ const app = new Spear()
|
|
|
185
257
|
```
|
|
186
258
|
|
|
187
259
|
## Cors
|
|
260
|
+
CORS (Cross-Origin Resource Sharing) controls which origins are allowed to access your API.
|
|
188
261
|
|
|
262
|
+
It helps secure your server by restricting or allowing cross-domain requests.
|
|
189
263
|
```js
|
|
190
264
|
const app = new Spear()
|
|
191
265
|
.cors({
|
|
@@ -200,6 +274,9 @@ const app = new Spear()
|
|
|
200
274
|
```
|
|
201
275
|
|
|
202
276
|
## Body
|
|
277
|
+
Body parsing allows your server to read incoming request payloads (JSON) and access them via ctx.body.
|
|
278
|
+
|
|
279
|
+
It enables handling requests with structured data.
|
|
203
280
|
```js
|
|
204
281
|
|
|
205
282
|
new Spear()
|
|
@@ -215,7 +292,14 @@ new Spear()
|
|
|
215
292
|
```
|
|
216
293
|
|
|
217
294
|
## File Upload
|
|
295
|
+
File upload support allows handling multipart/form-data requests and working with uploaded files via ctx.files.
|
|
218
296
|
|
|
297
|
+
It provides:
|
|
298
|
+
|
|
299
|
+
* Temporary file handling
|
|
300
|
+
* File size limits
|
|
301
|
+
* Manual file movement
|
|
302
|
+
* Auto cleanup option
|
|
219
303
|
```js
|
|
220
304
|
|
|
221
305
|
import { Spear, type T } from 'tspace-spear';
|
|
@@ -252,6 +336,14 @@ new Spear()
|
|
|
252
336
|
```
|
|
253
337
|
|
|
254
338
|
## Cookie
|
|
339
|
+
Cookie support allows you to read and manage HTTP cookies from incoming requests via ctx.cookies.
|
|
340
|
+
|
|
341
|
+
It is useful for:
|
|
342
|
+
|
|
343
|
+
* Session handling
|
|
344
|
+
* Authentication
|
|
345
|
+
* User preferences
|
|
346
|
+
* Stateful requests
|
|
255
347
|
```js
|
|
256
348
|
|
|
257
349
|
new Spear()
|
|
@@ -265,6 +357,12 @@ new Spear()
|
|
|
265
357
|
```
|
|
266
358
|
|
|
267
359
|
## Middleware
|
|
360
|
+
Middleware is a function that runs before the controller handler and is used to:
|
|
361
|
+
|
|
362
|
+
* Intercept requests
|
|
363
|
+
* Modify ctx
|
|
364
|
+
* Validate or block execution
|
|
365
|
+
* Handle authentication / logging / transformations
|
|
268
366
|
```js
|
|
269
367
|
import { type T } from "tspace-spear"
|
|
270
368
|
// file cat-middleware.ts
|
|
@@ -316,6 +414,9 @@ import CatMiddleware from './cat-middleware.ts'
|
|
|
316
414
|
```
|
|
317
415
|
|
|
318
416
|
## Controller
|
|
417
|
+
A Controller is used to group related routes and define request handlers in a structured way.
|
|
418
|
+
|
|
419
|
+
It helps organize your application into modules (similar to NestJS / Express routers), while keeping a clean and readable API design.
|
|
319
420
|
```js
|
|
320
421
|
import {
|
|
321
422
|
Controller ,
|
|
@@ -430,8 +531,131 @@ import CatController from './cat-controller.ts'
|
|
|
430
531
|
})()
|
|
431
532
|
```
|
|
432
533
|
|
|
534
|
+
|
|
535
|
+
## Dto
|
|
536
|
+
DTO (Data Transfer Object) is used to validate and transform incoming request data before it reaches your controller logic.
|
|
537
|
+
```js
|
|
538
|
+
import {
|
|
539
|
+
Controller ,
|
|
540
|
+
Post,
|
|
541
|
+
createDtoDecorator
|
|
542
|
+
type T
|
|
543
|
+
} from 'tspace-spear';
|
|
544
|
+
|
|
545
|
+
import z from "zod";
|
|
546
|
+
|
|
547
|
+
const ValidateDtoBody = (keys: string[]) => {
|
|
548
|
+
return createDtoDecorator((ctx) => {
|
|
549
|
+
const body = ctx.body ?? {};
|
|
550
|
+
const issues: Array<{ path: string; message: string }> = [];
|
|
551
|
+
|
|
552
|
+
for (let i = 0; i < keys.length; i++) {
|
|
553
|
+
const key = keys[i];
|
|
554
|
+
|
|
555
|
+
if (body[key] == null) {
|
|
556
|
+
issues.push({
|
|
557
|
+
path: key,
|
|
558
|
+
message: "Missing field",
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (issues.length > 0) {
|
|
564
|
+
throw {
|
|
565
|
+
message : "Validation failed",
|
|
566
|
+
issues
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
const ValidateDtoZodBody = (schema: z.ZodTypeAny) => {
|
|
573
|
+
return createDtoDecorator((ctx) => {
|
|
574
|
+
const result = schema.parse(ctx.body);
|
|
575
|
+
ctx.body = result as T.Body;
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const ValidateDtoPromiseBody = (keys: string[]) => {
|
|
580
|
+
return createDtoDecorator(async (ctx) => {
|
|
581
|
+
await new Promise(resolve => setTimeout(resolve,500));
|
|
582
|
+
// check in DB or other async operation
|
|
583
|
+
throw new Error('Validation failed in promise!');
|
|
584
|
+
}, (ctx, error) => {
|
|
585
|
+
// you implement your custom error handling for async validation here
|
|
586
|
+
return ctx.res.status(400).json({
|
|
587
|
+
message: error.message || "Validation failed",
|
|
588
|
+
issues: error.issues || [],
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const catSchema = z.object({
|
|
594
|
+
name: z.string(),
|
|
595
|
+
age: z.number(),
|
|
596
|
+
})
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
// file cat-controller.ts
|
|
600
|
+
@Controller('/cats')
|
|
601
|
+
export class CatController {
|
|
602
|
+
@Post('/')
|
|
603
|
+
@ValidateDtoBody(["name", "age"])
|
|
604
|
+
public async basic(ctx : T.Context) {
|
|
605
|
+
const body = ctx.body;
|
|
606
|
+
return {
|
|
607
|
+
body
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
@Post('/zod')
|
|
612
|
+
@ValidateDtoZodBody(catSchema)
|
|
613
|
+
public async zod(ctx : T.Context) {
|
|
614
|
+
const body = ctx.body as z.infer<typeof catSchema>;
|
|
615
|
+
return {
|
|
616
|
+
body
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
@Post('/promise')
|
|
621
|
+
@ValidateDtoPromiseBody(['name', 'age'])
|
|
622
|
+
public async promise(ctx : T.Context) {
|
|
623
|
+
const body = ctx.body;
|
|
624
|
+
|
|
625
|
+
return {
|
|
626
|
+
body
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
import { Spear } , { Router, type T } from "tspace-spear";
|
|
632
|
+
|
|
633
|
+
import CatController from './cat-controller.ts'
|
|
634
|
+
|
|
635
|
+
(async () => {
|
|
636
|
+
|
|
637
|
+
new Spear({
|
|
638
|
+
controllers: [ CatController ]
|
|
639
|
+
})
|
|
640
|
+
.useBodyParser()
|
|
641
|
+
.listen(3000 , () => console.log(`Server is now listening http://localhost:3000`))
|
|
642
|
+
|
|
643
|
+
// localhost:3000/cats // basic implete
|
|
644
|
+
// localhost:3000/cats/zod // zod implete
|
|
645
|
+
// localhost:3000/cats/promise // promise implete
|
|
646
|
+
|
|
647
|
+
})()
|
|
648
|
+
```
|
|
649
|
+
|
|
433
650
|
## Router
|
|
651
|
+
The Router allows you to organize routes into modular groups, making your application more scalable and maintainable.
|
|
434
652
|
|
|
653
|
+
It supports:
|
|
654
|
+
|
|
655
|
+
* Grouped routes
|
|
656
|
+
* Nested route prefixes
|
|
657
|
+
* Reusable router modules
|
|
658
|
+
* Separation of concerns
|
|
435
659
|
```js
|
|
436
660
|
import { Spear, Router, type T } from "tspace-spear";
|
|
437
661
|
|
|
@@ -475,6 +699,14 @@ app.listen(port , () => console.log(`Server is now listening http://localhost:30
|
|
|
475
699
|
```
|
|
476
700
|
|
|
477
701
|
## Swagger
|
|
702
|
+
Provides built-in Swagger support to document your API endpoints.
|
|
703
|
+
|
|
704
|
+
It allows you to:
|
|
705
|
+
|
|
706
|
+
* Describe request parameters (query, body, params)
|
|
707
|
+
* Generate API documentation
|
|
708
|
+
* Improve developer experience
|
|
709
|
+
* Standardize API contracts
|
|
478
710
|
```js
|
|
479
711
|
|
|
480
712
|
// file cat-controller.ts
|
|
@@ -542,7 +774,22 @@ class CatController {
|
|
|
542
774
|
name : {
|
|
543
775
|
type : 'string',
|
|
544
776
|
example : "xxxxx"
|
|
545
|
-
}
|
|
777
|
+
},
|
|
778
|
+
status : {
|
|
779
|
+
type : 'string',
|
|
780
|
+
example: "active",
|
|
781
|
+
enum: ['active', 'inactive'],
|
|
782
|
+
description: "User status (active = enabled, inactive = disabled)",
|
|
783
|
+
required : true
|
|
784
|
+
},
|
|
785
|
+
roles: {
|
|
786
|
+
type: 'array',
|
|
787
|
+
items : {
|
|
788
|
+
type: 'string',
|
|
789
|
+
example: "roleA",
|
|
790
|
+
enum: ['roleA', 'roleB']
|
|
791
|
+
}
|
|
792
|
+
},
|
|
546
793
|
}
|
|
547
794
|
}
|
|
548
795
|
})
|
|
@@ -619,7 +866,7 @@ class CatController {
|
|
|
619
866
|
file : {
|
|
620
867
|
type : 'array',
|
|
621
868
|
items: {
|
|
622
|
-
type:"
|
|
869
|
+
type :"string",
|
|
623
870
|
format:"binary"
|
|
624
871
|
}
|
|
625
872
|
},
|
|
@@ -667,44 +914,78 @@ class CatController {
|
|
|
667
914
|
```
|
|
668
915
|
|
|
669
916
|
## WebSocket
|
|
917
|
+
Provides built-in WebSocket support for real-time communication.
|
|
918
|
+
|
|
919
|
+
It allows you to:
|
|
920
|
+
|
|
921
|
+
* Handle client connections
|
|
922
|
+
* Send/receive messages
|
|
923
|
+
* Build chat systems
|
|
924
|
+
* Manage real-time events
|
|
670
925
|
```js
|
|
671
926
|
import { Spear } from "tspace-spear";
|
|
927
|
+
import fs from 'fs';
|
|
928
|
+
import path from 'path';
|
|
672
929
|
|
|
673
930
|
new Spear()
|
|
674
|
-
.get('/',
|
|
675
|
-
|
|
676
|
-
|
|
931
|
+
.get('/',(ctx) => {
|
|
932
|
+
// you can serve a HTML file for testing WebSocket connection
|
|
933
|
+
const htmlWs = fs.readFileSync(path.join(path.resolve(), 'public', 'ws.html'), 'utf-8');
|
|
934
|
+
return ctx.res.html(htmlWs);
|
|
677
935
|
})
|
|
678
936
|
.ws(() => {
|
|
937
|
+
const clients = new Map<string, any>();
|
|
679
938
|
return {
|
|
680
939
|
connection: (ws) => {
|
|
681
|
-
|
|
940
|
+
console.log("connected");
|
|
682
941
|
},
|
|
942
|
+
|
|
683
943
|
message: (ws, msg) => {
|
|
684
944
|
const data = JSON.parse(msg.toString());
|
|
685
945
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
946
|
+
if (data.type === "register") {
|
|
947
|
+
ws.userId = data.userId;
|
|
948
|
+
clients.set(data.userId, ws);
|
|
949
|
+
|
|
950
|
+
ws.send(JSON.stringify({
|
|
951
|
+
type: "system",
|
|
952
|
+
message: `registered as ${data.userId}`
|
|
953
|
+
}));
|
|
954
|
+
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
if (data.type === "chat") {
|
|
959
|
+
const targetWs = clients.get(data.to);
|
|
960
|
+
|
|
961
|
+
if (!targetWs) {
|
|
962
|
+
ws.send(JSON.stringify({
|
|
963
|
+
type: "error",
|
|
964
|
+
message: "User not online"
|
|
965
|
+
}));
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
691
968
|
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
ws.
|
|
695
|
-
|
|
969
|
+
targetWs.send(JSON.stringify({
|
|
970
|
+
type: "chat",
|
|
971
|
+
from: ws.userId,
|
|
972
|
+
text: data.text
|
|
973
|
+
}));
|
|
696
974
|
|
|
697
|
-
|
|
698
|
-
ws.send(JSON.stringify({ type: 'error', message: 'Unknown message type' }));
|
|
975
|
+
return;
|
|
699
976
|
}
|
|
700
977
|
},
|
|
701
|
-
|
|
702
|
-
|
|
978
|
+
|
|
979
|
+
close: (ws) => {
|
|
980
|
+
if (ws.userId) {
|
|
981
|
+
clients.delete(ws.userId);
|
|
982
|
+
}
|
|
703
983
|
},
|
|
984
|
+
|
|
704
985
|
error: (ws, error) => {
|
|
705
986
|
console.error('WebSocket error:', error);
|
|
706
987
|
}
|
|
707
|
-
}
|
|
988
|
+
};
|
|
708
989
|
})
|
|
709
990
|
.listen(3000 , ({ port , server }) => {
|
|
710
991
|
console.log(`server listening on : http://localhost:${port}`)
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export declare const HTTP_STATUS_MESSAGES: {
|
|
2
|
+
readonly 100: "Continue";
|
|
3
|
+
readonly 101: "Switching Protocols";
|
|
4
|
+
readonly 102: "Processing";
|
|
5
|
+
readonly 200: "OK";
|
|
6
|
+
readonly 201: "Created";
|
|
7
|
+
readonly 202: "Accepted";
|
|
8
|
+
readonly 203: "Non-Authoritative Information";
|
|
9
|
+
readonly 204: "No Content";
|
|
10
|
+
readonly 205: "Reset Content";
|
|
11
|
+
readonly 206: "Partial Content";
|
|
12
|
+
readonly 207: "Multi-Status";
|
|
13
|
+
readonly 208: "Already Reported";
|
|
14
|
+
readonly 226: "IM Used";
|
|
15
|
+
readonly 300: "Multiple Choices";
|
|
16
|
+
readonly 301: "Moved Permanently";
|
|
17
|
+
readonly 302: "Found";
|
|
18
|
+
readonly 303: "See Other";
|
|
19
|
+
readonly 304: "Not Modified";
|
|
20
|
+
readonly 305: "Use Proxy";
|
|
21
|
+
readonly 306: "(Unused)";
|
|
22
|
+
readonly 307: "Temporary Redirect";
|
|
23
|
+
readonly 308: "Permanent Redirect";
|
|
24
|
+
readonly 400: "Bad Request";
|
|
25
|
+
readonly 401: "Unauthorized";
|
|
26
|
+
readonly 402: "Payment Required";
|
|
27
|
+
readonly 403: "Forbidden";
|
|
28
|
+
readonly 404: "Not Found";
|
|
29
|
+
readonly 405: "Method Not Allowed";
|
|
30
|
+
readonly 406: "Not Acceptable";
|
|
31
|
+
readonly 407: "Proxy Authentication Required";
|
|
32
|
+
readonly 408: "Request Timeout";
|
|
33
|
+
readonly 409: "Conflict";
|
|
34
|
+
readonly 410: "Gone";
|
|
35
|
+
readonly 411: "Length Required";
|
|
36
|
+
readonly 412: "Precondition Failed";
|
|
37
|
+
readonly 413: "Payload Too Large";
|
|
38
|
+
readonly 414: "URI Too Long";
|
|
39
|
+
readonly 415: "Unsupported Media Type";
|
|
40
|
+
readonly 416: "Range Not Satisfiable";
|
|
41
|
+
readonly 417: "Expectation Failed";
|
|
42
|
+
readonly 418: "I'm a teapot";
|
|
43
|
+
readonly 421: "Misdirected Request";
|
|
44
|
+
readonly 422: "Unprocessable Entity";
|
|
45
|
+
readonly 423: "Locked";
|
|
46
|
+
readonly 424: "Failed Dependency";
|
|
47
|
+
readonly 425: "Too Early";
|
|
48
|
+
readonly 426: "Upgrade Required";
|
|
49
|
+
readonly 428: "Precondition Required";
|
|
50
|
+
readonly 429: "Too Many Requests";
|
|
51
|
+
readonly 431: "Request Header Fields Too Large";
|
|
52
|
+
readonly 451: "Unavailable For Legal Reasons";
|
|
53
|
+
readonly 500: "Internal Server Error";
|
|
54
|
+
readonly 501: "Not Implemented";
|
|
55
|
+
readonly 502: "Bad Gateway";
|
|
56
|
+
readonly 503: "Service Unavailable";
|
|
57
|
+
readonly 504: "Gateway Timeout";
|
|
58
|
+
readonly 505: "HTTP Version Not Supported";
|
|
59
|
+
readonly 506: "Variant Also Negotiates";
|
|
60
|
+
readonly 507: "Insufficient Storage";
|
|
61
|
+
readonly 508: "Loop Detected";
|
|
62
|
+
readonly 510: "Not Extended";
|
|
63
|
+
readonly 511: "Network Authentication Required";
|
|
64
|
+
};
|
|
65
|
+
export declare const HEADER_CONTENT_TYPES: {
|
|
66
|
+
readonly text: {
|
|
67
|
+
readonly 'Content-Type': "text/plain";
|
|
68
|
+
};
|
|
69
|
+
readonly html: {
|
|
70
|
+
readonly 'Content-Type': "text/html";
|
|
71
|
+
};
|
|
72
|
+
readonly css: {
|
|
73
|
+
readonly 'Content-Type': "text/css";
|
|
74
|
+
};
|
|
75
|
+
readonly js: {
|
|
76
|
+
readonly 'Content-Type': "application/javascript";
|
|
77
|
+
};
|
|
78
|
+
readonly json: {
|
|
79
|
+
readonly 'Content-Type': "application/json";
|
|
80
|
+
};
|
|
81
|
+
readonly form: {
|
|
82
|
+
readonly 'Content-Type': "application/x-www-form-urlencoded";
|
|
83
|
+
};
|
|
84
|
+
readonly multipart: {
|
|
85
|
+
readonly 'Content-Type': "multipart/form-data";
|
|
86
|
+
};
|
|
87
|
+
readonly png: {
|
|
88
|
+
readonly 'Content-Type': "image/png";
|
|
89
|
+
};
|
|
90
|
+
readonly jpg: {
|
|
91
|
+
readonly 'Content-Type': "image/jpeg";
|
|
92
|
+
};
|
|
93
|
+
readonly jpeg: {
|
|
94
|
+
readonly 'Content-Type': "image/jpeg";
|
|
95
|
+
};
|
|
96
|
+
readonly gif: {
|
|
97
|
+
readonly 'Content-Type': "image/gif";
|
|
98
|
+
};
|
|
99
|
+
readonly webp: {
|
|
100
|
+
readonly 'Content-Type': "image/webp";
|
|
101
|
+
};
|
|
102
|
+
readonly svg: {
|
|
103
|
+
readonly 'Content-Type': "image/svg+xml";
|
|
104
|
+
};
|
|
105
|
+
readonly ico: {
|
|
106
|
+
readonly 'Content-Type': "image/x-icon";
|
|
107
|
+
};
|
|
108
|
+
readonly pdf: {
|
|
109
|
+
readonly 'Content-Type': "application/pdf";
|
|
110
|
+
};
|
|
111
|
+
readonly zip: {
|
|
112
|
+
readonly 'Content-Type': "application/zip";
|
|
113
|
+
};
|
|
114
|
+
readonly gzip: {
|
|
115
|
+
readonly 'Content-Type': "application/gzip";
|
|
116
|
+
};
|
|
117
|
+
readonly mp4: {
|
|
118
|
+
readonly 'Content-Type': "video/mp4";
|
|
119
|
+
};
|
|
120
|
+
readonly webm: {
|
|
121
|
+
readonly 'Content-Type': "video/webm";
|
|
122
|
+
};
|
|
123
|
+
readonly ogg: {
|
|
124
|
+
readonly 'Content-Type': "video/ogg";
|
|
125
|
+
};
|
|
126
|
+
readonly mp3: {
|
|
127
|
+
readonly 'Content-Type': "audio/mpeg";
|
|
128
|
+
};
|
|
129
|
+
readonly wav: {
|
|
130
|
+
readonly 'Content-Type': "audio/wav";
|
|
131
|
+
};
|
|
132
|
+
readonly aac: {
|
|
133
|
+
readonly 'Content-Type': "audio/aac";
|
|
134
|
+
};
|
|
135
|
+
readonly oga: {
|
|
136
|
+
readonly 'Content-Type': "audio/ogg";
|
|
137
|
+
};
|
|
138
|
+
readonly woff: {
|
|
139
|
+
readonly 'Content-Type': "font/woff";
|
|
140
|
+
};
|
|
141
|
+
readonly woff2: {
|
|
142
|
+
readonly 'Content-Type': "font/woff2";
|
|
143
|
+
};
|
|
144
|
+
readonly ttf: {
|
|
145
|
+
readonly 'Content-Type': "font/ttf";
|
|
146
|
+
};
|
|
147
|
+
readonly otf: {
|
|
148
|
+
readonly 'Content-Type': "font/otf";
|
|
149
|
+
};
|
|
150
|
+
readonly octet: {
|
|
151
|
+
readonly 'Content-Type': "application/octet-stream";
|
|
152
|
+
};
|
|
153
|
+
};
|