@zero-server/sdk 0.9.1 → 0.9.3

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 (128) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -443
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +465 -465
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +137 -137
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +255 -255
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/body.d.ts +14 -0
  113. package/types/cli.d.ts +2 -0
  114. package/types/cluster.d.ts +75 -75
  115. package/types/env.d.ts +80 -80
  116. package/types/errors.d.ts +316 -316
  117. package/types/fetch.d.ts +43 -43
  118. package/types/grpc.d.ts +432 -432
  119. package/types/index.d.ts +384 -384
  120. package/types/lifecycle.d.ts +60 -60
  121. package/types/middleware.d.ts +320 -320
  122. package/types/observe.d.ts +304 -304
  123. package/types/orm.d.ts +1887 -1887
  124. package/types/request.d.ts +109 -109
  125. package/types/response.d.ts +157 -157
  126. package/types/router.d.ts +78 -78
  127. package/types/sse.d.ts +78 -78
  128. package/types/websocket.d.ts +126 -126
package/types/router.d.ts CHANGED
@@ -1,78 +1,78 @@
1
- import { Request } from './request';
2
- import { Response } from './response';
3
- import { MiddlewareFunction, NextFunction } from './middleware';
4
-
5
- export interface RouteOptions {
6
- /** When `true`, route matches only HTTPS; when `false`, only HTTP. */
7
- secure?: boolean;
8
- }
9
-
10
- export type RouteHandler = (req: Request, res: Response, next?: NextFunction) => void | Promise<void>;
11
-
12
- export interface RouteEntry {
13
- method: string;
14
- path: string;
15
- regex: RegExp;
16
- keys: string[];
17
- handlers: RouteHandler[];
18
- secure?: boolean;
19
- }
20
-
21
- export interface RouteInfo {
22
- method: string;
23
- path: string;
24
- secure?: boolean;
25
- maxPayload?: number;
26
- pingInterval?: number;
27
- }
28
-
29
- export interface RouteChain {
30
- get(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
31
- post(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
32
- put(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
33
- delete(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
34
- patch(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
35
- options(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
36
- head(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
37
- all(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
38
- }
39
-
40
- export interface RouterInstance {
41
- /** Registered routes. */
42
- routes: RouteEntry[];
43
-
44
- /**
45
- * Register a route.
46
- */
47
- add(method: string, path: string, handlers: RouteHandler[], options?: RouteOptions): void;
48
-
49
- /**
50
- * Mount a child router under a path prefix.
51
- */
52
- use(prefix: string, router: RouterInstance): void;
53
-
54
- /**
55
- * Match and handle request.
56
- */
57
- handle(req: Request, res: Response): void;
58
-
59
- /**
60
- * Chainable route builder.
61
- */
62
- route(path: string): RouteChain;
63
-
64
- /**
65
- * Return a flat list of all registered routes.
66
- */
67
- inspect(prefix?: string): RouteInfo[];
68
-
69
- // HTTP method shortcuts
70
- get(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
71
- post(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
72
- put(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
73
- delete(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
74
- patch(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
75
- options(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
76
- head(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
77
- all(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
78
- }
1
+ import { Request } from './request';
2
+ import { Response } from './response';
3
+ import { MiddlewareFunction, NextFunction } from './middleware';
4
+
5
+ export interface RouteOptions {
6
+ /** When `true`, route matches only HTTPS; when `false`, only HTTP. */
7
+ secure?: boolean;
8
+ }
9
+
10
+ export type RouteHandler = (req: Request, res: Response, next?: NextFunction) => void | Promise<void>;
11
+
12
+ export interface RouteEntry {
13
+ method: string;
14
+ path: string;
15
+ regex: RegExp;
16
+ keys: string[];
17
+ handlers: RouteHandler[];
18
+ secure?: boolean;
19
+ }
20
+
21
+ export interface RouteInfo {
22
+ method: string;
23
+ path: string;
24
+ secure?: boolean;
25
+ maxPayload?: number;
26
+ pingInterval?: number;
27
+ }
28
+
29
+ export interface RouteChain {
30
+ get(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
31
+ post(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
32
+ put(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
33
+ delete(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
34
+ patch(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
35
+ options(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
36
+ head(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
37
+ all(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
38
+ }
39
+
40
+ export interface RouterInstance {
41
+ /** Registered routes. */
42
+ routes: RouteEntry[];
43
+
44
+ /**
45
+ * Register a route.
46
+ */
47
+ add(method: string, path: string, handlers: RouteHandler[], options?: RouteOptions): void;
48
+
49
+ /**
50
+ * Mount a child router under a path prefix.
51
+ */
52
+ use(prefix: string, router: RouterInstance): void;
53
+
54
+ /**
55
+ * Match and handle request.
56
+ */
57
+ handle(req: Request, res: Response): void;
58
+
59
+ /**
60
+ * Chainable route builder.
61
+ */
62
+ route(path: string): RouteChain;
63
+
64
+ /**
65
+ * Return a flat list of all registered routes.
66
+ */
67
+ inspect(prefix?: string): RouteInfo[];
68
+
69
+ // HTTP method shortcuts
70
+ get(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
71
+ post(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
72
+ put(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
73
+ delete(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
74
+ patch(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
75
+ options(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
76
+ head(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
77
+ all(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
78
+ }
package/types/sse.d.ts CHANGED
@@ -1,78 +1,78 @@
1
- export interface SSEOptions {
2
- retry?: number;
3
- headers?: Record<string, string>;
4
- keepAlive?: number;
5
- keepAliveComment?: string;
6
- autoId?: boolean;
7
- startId?: number;
8
- pad?: number;
9
- status?: number;
10
- }
11
-
12
- export interface SSEStream {
13
- /** Whether the stream is still open. */
14
- readonly connected: boolean;
15
- /** The Last-Event-ID from client reconnection. */
16
- lastEventId: string | null;
17
- /** Total events sent. */
18
- eventCount: number;
19
- /** Total bytes written. */
20
- bytesSent: number;
21
- /** Timestamp when stream was opened. */
22
- connectedAt: number;
23
- /** Milliseconds since opened. */
24
- readonly uptime: number;
25
- /** Whether connection is over TLS. */
26
- secure: boolean;
27
- /** Arbitrary user-data store. */
28
- data: Record<string, any>;
29
-
30
- /**
31
- * Send an unnamed data event.
32
- */
33
- send(data: string | object, id?: string | number): SSEStream;
34
-
35
- /**
36
- * Send a JSON event (alias for send).
37
- */
38
- sendJSON(obj: any, id?: string | number): SSEStream;
39
-
40
- /**
41
- * Send a named event.
42
- */
43
- event(eventName: string, data: string | object, id?: string | number): SSEStream;
44
-
45
- /**
46
- * Send a comment line.
47
- */
48
- comment(text: string): SSEStream;
49
-
50
- /**
51
- * Send or update the retry interval.
52
- */
53
- retry(ms: number): SSEStream;
54
-
55
- /**
56
- * Start/restart keep-alive timer.
57
- */
58
- keepAlive(intervalMs: number, comment?: string): SSEStream;
59
-
60
- /**
61
- * Flush buffered data.
62
- */
63
- flush(): SSEStream;
64
-
65
- /**
66
- * Close the SSE connection.
67
- */
68
- close(): void;
69
-
70
- // Event emitter
71
- on(event: 'close', fn: () => void): SSEStream;
72
- on(event: 'error', fn: (err: Error) => void): SSEStream;
73
- once(event: 'close', fn: () => void): SSEStream;
74
- once(event: 'error', fn: (err: Error) => void): SSEStream;
75
- off(event: string, fn: Function): SSEStream;
76
- removeAllListeners(event?: string): SSEStream;
77
- listenerCount(event: string): number;
78
- }
1
+ export interface SSEOptions {
2
+ retry?: number;
3
+ headers?: Record<string, string>;
4
+ keepAlive?: number;
5
+ keepAliveComment?: string;
6
+ autoId?: boolean;
7
+ startId?: number;
8
+ pad?: number;
9
+ status?: number;
10
+ }
11
+
12
+ export interface SSEStream {
13
+ /** Whether the stream is still open. */
14
+ readonly connected: boolean;
15
+ /** The Last-Event-ID from client reconnection. */
16
+ lastEventId: string | null;
17
+ /** Total events sent. */
18
+ eventCount: number;
19
+ /** Total bytes written. */
20
+ bytesSent: number;
21
+ /** Timestamp when stream was opened. */
22
+ connectedAt: number;
23
+ /** Milliseconds since opened. */
24
+ readonly uptime: number;
25
+ /** Whether connection is over TLS. */
26
+ secure: boolean;
27
+ /** Arbitrary user-data store. */
28
+ data: Record<string, any>;
29
+
30
+ /**
31
+ * Send an unnamed data event.
32
+ */
33
+ send(data: string | object, id?: string | number): SSEStream;
34
+
35
+ /**
36
+ * Send a JSON event (alias for send).
37
+ */
38
+ sendJSON(obj: any, id?: string | number): SSEStream;
39
+
40
+ /**
41
+ * Send a named event.
42
+ */
43
+ event(eventName: string, data: string | object, id?: string | number): SSEStream;
44
+
45
+ /**
46
+ * Send a comment line.
47
+ */
48
+ comment(text: string): SSEStream;
49
+
50
+ /**
51
+ * Send or update the retry interval.
52
+ */
53
+ retry(ms: number): SSEStream;
54
+
55
+ /**
56
+ * Start/restart keep-alive timer.
57
+ */
58
+ keepAlive(intervalMs: number, comment?: string): SSEStream;
59
+
60
+ /**
61
+ * Flush buffered data.
62
+ */
63
+ flush(): SSEStream;
64
+
65
+ /**
66
+ * Close the SSE connection.
67
+ */
68
+ close(): void;
69
+
70
+ // Event emitter
71
+ on(event: 'close', fn: () => void): SSEStream;
72
+ on(event: 'error', fn: (err: Error) => void): SSEStream;
73
+ once(event: 'close', fn: () => void): SSEStream;
74
+ once(event: 'error', fn: (err: Error) => void): SSEStream;
75
+ off(event: string, fn: Function): SSEStream;
76
+ removeAllListeners(event?: string): SSEStream;
77
+ listenerCount(event: string): number;
78
+ }
@@ -1,126 +1,126 @@
1
- import { IncomingMessage, IncomingHttpHeaders } from 'http';
2
-
3
- export interface WebSocketOptions {
4
- /** Maximum incoming frame size in bytes (default 1 MB). */
5
- maxPayload?: number;
6
- /** Auto-ping interval in ms. Set `0` to disable. */
7
- pingInterval?: number;
8
- /** Return false to reject the upgrade. */
9
- verifyClient?: (req: IncomingMessage) => boolean;
10
- }
11
-
12
- export type WebSocketHandler = (ws: WebSocketConnection, req: IncomingMessage) => void;
13
-
14
- export declare const WebSocketReadyState: {
15
- readonly CONNECTING: 0;
16
- readonly OPEN: 1;
17
- readonly CLOSING: 2;
18
- readonly CLOSED: 3;
19
- };
20
-
21
- export interface WebSocketConnection {
22
- /** Unique connection identifier. */
23
- id: string;
24
- /** Current ready state (0-3). */
25
- readyState: number;
26
- /** Negotiated sub-protocol. */
27
- protocol: string;
28
- /** Requested extensions. */
29
- extensions: string;
30
- /** Request headers from the upgrade. */
31
- headers: IncomingHttpHeaders;
32
- /** Remote IP address. */
33
- ip: string | null;
34
- /** Parsed query params from the upgrade URL. */
35
- query: Record<string, string>;
36
- /** Full upgrade URL. */
37
- url: string;
38
- /** Whether connection is over TLS. */
39
- secure: boolean;
40
- /** Maximum incoming payload bytes. */
41
- maxPayload: number;
42
- /** Timestamp when connected. */
43
- connectedAt: number;
44
- /** Arbitrary user-data store. */
45
- data: Record<string, any>;
46
- /** Bytes waiting in send buffer. */
47
- readonly bufferedAmount: number;
48
- /** Milliseconds since connected. */
49
- readonly uptime: number;
50
-
51
- /**
52
- * Send a text or binary message.
53
- */
54
- send(data: string | Buffer, opts?: { binary?: boolean; callback?: Function }): boolean;
55
-
56
- /**
57
- * Send a JSON-serialised message.
58
- */
59
- sendJSON(obj: any, cb?: Function): boolean;
60
-
61
- /**
62
- * Send a ping frame.
63
- */
64
- ping(payload?: string | Buffer, cb?: Function): boolean;
65
-
66
- /**
67
- * Send a pong frame.
68
- */
69
- pong(payload?: string | Buffer, cb?: Function): boolean;
70
-
71
- /**
72
- * Close the connection.
73
- */
74
- close(code?: number, reason?: string): void;
75
-
76
- /**
77
- * Forcefully destroy the socket.
78
- */
79
- terminate(): void;
80
-
81
- // Event emitter
82
- on(event: 'message', fn: (data: string | Buffer) => void): WebSocketConnection;
83
- on(event: 'close', fn: (code: number, reason: string) => void): WebSocketConnection;
84
- on(event: 'error', fn: (err: Error) => void): WebSocketConnection;
85
- on(event: 'pong', fn: (payload: Buffer) => void): WebSocketConnection;
86
- on(event: 'ping', fn: (payload: Buffer) => void): WebSocketConnection;
87
- on(event: 'drain', fn: () => void): WebSocketConnection;
88
- once(event: string, fn: Function): WebSocketConnection;
89
- off(event: string, fn: Function): WebSocketConnection;
90
- removeAllListeners(event?: string): WebSocketConnection;
91
- listenerCount(event: string): number;
92
- }
93
-
94
- export interface WebSocketPool {
95
- /** Total active connections. */
96
- readonly size: number;
97
- /** All active room names. */
98
- readonly rooms: string[];
99
- /** All active connections. */
100
- readonly clients: WebSocketConnection[];
101
-
102
- /** Add a connection to the pool. */
103
- add(ws: WebSocketConnection): WebSocketPool;
104
- /** Remove a connection from the pool. */
105
- remove(ws: WebSocketConnection): WebSocketPool;
106
- /** Join a connection to a room. */
107
- join(ws: WebSocketConnection, room: string): WebSocketPool;
108
- /** Remove a connection from a room. */
109
- leave(ws: WebSocketConnection, room: string): WebSocketPool;
110
- /** Get all rooms a connection belongs to. */
111
- roomsOf(ws: WebSocketConnection): string[];
112
- /** Broadcast to ALL connections. */
113
- broadcast(data: string | Buffer, exclude?: WebSocketConnection): void;
114
- /** Broadcast JSON to ALL connections. */
115
- broadcastJSON(obj: any, exclude?: WebSocketConnection): void;
116
- /** Send to all connections in a room. */
117
- toRoom(room: string, data: string | Buffer, exclude?: WebSocketConnection): void;
118
- /** Send JSON to all connections in a room. */
119
- toRoomJSON(room: string, obj: any, exclude?: WebSocketConnection): void;
120
- /** Get all connections in a room. */
121
- in(room: string): WebSocketConnection[];
122
- /** Number of connections in a room. */
123
- roomSize(room: string): number;
124
- /** Close all connections. */
125
- closeAll(code?: number, reason?: string): void;
126
- }
1
+ import { IncomingMessage, IncomingHttpHeaders } from 'http';
2
+
3
+ export interface WebSocketOptions {
4
+ /** Maximum incoming frame size in bytes (default 1 MB). */
5
+ maxPayload?: number;
6
+ /** Auto-ping interval in ms. Set `0` to disable. */
7
+ pingInterval?: number;
8
+ /** Return false to reject the upgrade. */
9
+ verifyClient?: (req: IncomingMessage) => boolean;
10
+ }
11
+
12
+ export type WebSocketHandler = (ws: WebSocketConnection, req: IncomingMessage) => void;
13
+
14
+ export declare const WebSocketReadyState: {
15
+ readonly CONNECTING: 0;
16
+ readonly OPEN: 1;
17
+ readonly CLOSING: 2;
18
+ readonly CLOSED: 3;
19
+ };
20
+
21
+ export interface WebSocketConnection {
22
+ /** Unique connection identifier. */
23
+ id: string;
24
+ /** Current ready state (0-3). */
25
+ readyState: number;
26
+ /** Negotiated sub-protocol. */
27
+ protocol: string;
28
+ /** Requested extensions. */
29
+ extensions: string;
30
+ /** Request headers from the upgrade. */
31
+ headers: IncomingHttpHeaders;
32
+ /** Remote IP address. */
33
+ ip: string | null;
34
+ /** Parsed query params from the upgrade URL. */
35
+ query: Record<string, string>;
36
+ /** Full upgrade URL. */
37
+ url: string;
38
+ /** Whether connection is over TLS. */
39
+ secure: boolean;
40
+ /** Maximum incoming payload bytes. */
41
+ maxPayload: number;
42
+ /** Timestamp when connected. */
43
+ connectedAt: number;
44
+ /** Arbitrary user-data store. */
45
+ data: Record<string, any>;
46
+ /** Bytes waiting in send buffer. */
47
+ readonly bufferedAmount: number;
48
+ /** Milliseconds since connected. */
49
+ readonly uptime: number;
50
+
51
+ /**
52
+ * Send a text or binary message.
53
+ */
54
+ send(data: string | Buffer, opts?: { binary?: boolean; callback?: Function }): boolean;
55
+
56
+ /**
57
+ * Send a JSON-serialised message.
58
+ */
59
+ sendJSON(obj: any, cb?: Function): boolean;
60
+
61
+ /**
62
+ * Send a ping frame.
63
+ */
64
+ ping(payload?: string | Buffer, cb?: Function): boolean;
65
+
66
+ /**
67
+ * Send a pong frame.
68
+ */
69
+ pong(payload?: string | Buffer, cb?: Function): boolean;
70
+
71
+ /**
72
+ * Close the connection.
73
+ */
74
+ close(code?: number, reason?: string): void;
75
+
76
+ /**
77
+ * Forcefully destroy the socket.
78
+ */
79
+ terminate(): void;
80
+
81
+ // Event emitter
82
+ on(event: 'message', fn: (data: string | Buffer) => void): WebSocketConnection;
83
+ on(event: 'close', fn: (code: number, reason: string) => void): WebSocketConnection;
84
+ on(event: 'error', fn: (err: Error) => void): WebSocketConnection;
85
+ on(event: 'pong', fn: (payload: Buffer) => void): WebSocketConnection;
86
+ on(event: 'ping', fn: (payload: Buffer) => void): WebSocketConnection;
87
+ on(event: 'drain', fn: () => void): WebSocketConnection;
88
+ once(event: string, fn: Function): WebSocketConnection;
89
+ off(event: string, fn: Function): WebSocketConnection;
90
+ removeAllListeners(event?: string): WebSocketConnection;
91
+ listenerCount(event: string): number;
92
+ }
93
+
94
+ export interface WebSocketPool {
95
+ /** Total active connections. */
96
+ readonly size: number;
97
+ /** All active room names. */
98
+ readonly rooms: string[];
99
+ /** All active connections. */
100
+ readonly clients: WebSocketConnection[];
101
+
102
+ /** Add a connection to the pool. */
103
+ add(ws: WebSocketConnection): WebSocketPool;
104
+ /** Remove a connection from the pool. */
105
+ remove(ws: WebSocketConnection): WebSocketPool;
106
+ /** Join a connection to a room. */
107
+ join(ws: WebSocketConnection, room: string): WebSocketPool;
108
+ /** Remove a connection from a room. */
109
+ leave(ws: WebSocketConnection, room: string): WebSocketPool;
110
+ /** Get all rooms a connection belongs to. */
111
+ roomsOf(ws: WebSocketConnection): string[];
112
+ /** Broadcast to ALL connections. */
113
+ broadcast(data: string | Buffer, exclude?: WebSocketConnection): void;
114
+ /** Broadcast JSON to ALL connections. */
115
+ broadcastJSON(obj: any, exclude?: WebSocketConnection): void;
116
+ /** Send to all connections in a room. */
117
+ toRoom(room: string, data: string | Buffer, exclude?: WebSocketConnection): void;
118
+ /** Send JSON to all connections in a room. */
119
+ toRoomJSON(room: string, obj: any, exclude?: WebSocketConnection): void;
120
+ /** Get all connections in a room. */
121
+ in(room: string): WebSocketConnection[];
122
+ /** Number of connections in a room. */
123
+ roomSize(room: string): number;
124
+ /** Close all connections. */
125
+ closeAll(code?: number, reason?: string): void;
126
+ }