@wxn0brp/falcon-frame 0.9.3 → 0.9.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/dist/res.d.ts CHANGED
@@ -83,4 +83,11 @@ export declare class FFResponse extends http.ServerResponse {
83
83
  * @returns The response object
84
84
  */
85
85
  sseSend(data: string | object): this;
86
+ /**
87
+ * Sends a Server-Sent Event with an event type to the client.
88
+ * @param event The event type
89
+ * @param data The data to be sent. If an object, it will be JSON.stringified.
90
+ * @returns The response object
91
+ */
92
+ sseEventSend(event: string, data: string | object): this;
86
93
  }
package/dist/res.js CHANGED
@@ -180,4 +180,16 @@ export class FFResponse extends http.ServerResponse {
180
180
  this.write(`data: ${data}\n\n`);
181
181
  return this;
182
182
  }
183
+ /**
184
+ * Sends a Server-Sent Event with an event type to the client.
185
+ * @param event The event type
186
+ * @param data The data to be sent. If an object, it will be JSON.stringified.
187
+ * @returns The response object
188
+ */
189
+ sseEventSend(event, data) {
190
+ if (typeof data === "object")
191
+ data = JSON.stringify(data);
192
+ this.write(`event: ${event}\ndata: ${data}\n\n`);
193
+ return this;
194
+ }
183
195
  }
package/dist/router.d.ts CHANGED
@@ -16,5 +16,5 @@ export declare class Router {
16
16
  static(apiPath: string, dirPath?: string, opts?: StaticServeOptions): this;
17
17
  sse(path: string, ...handlers: RouteHandler[]): SSEManager;
18
18
  customParser(path: string, handler: RouteHandler, method?: Method): this;
19
- router(path: string): Router;
19
+ router(path?: string): Router;
20
20
  }
package/dist/router.js CHANGED
@@ -80,7 +80,7 @@ export class Router {
80
80
  this.middlewares[index - 1].customParser = true;
81
81
  return this;
82
82
  }
83
- router(path) {
83
+ router(path = "/") {
84
84
  const router = new Router();
85
85
  this.use(path, router);
86
86
  return router;
package/dist/sse.d.ts CHANGED
@@ -12,5 +12,7 @@ export declare class SSEManager {
12
12
  }>;
13
13
  sendAll(data: any): void;
14
14
  sendTo(id: string, data: any): void;
15
+ sendEventAll(event: string, data: any): void;
16
+ sendEventTo(id: string, event: string, data: any): void;
15
17
  disconnect(id: string): void;
16
18
  }
package/dist/sse.js CHANGED
@@ -20,21 +20,30 @@ export class SSEManager {
20
20
  return this._clients;
21
21
  }
22
22
  sendAll(data) {
23
- for (const { res } of this._clients.values()) {
23
+ for (const { res } of this._clients.values())
24
24
  res.sseSend(data);
25
- }
26
25
  }
27
26
  sendTo(id, data) {
28
27
  const client = this._clients.get(id);
29
- if (client) {
30
- client.res.sseSend(data);
31
- }
28
+ if (!client)
29
+ return;
30
+ client.res.sseSend(data);
31
+ }
32
+ sendEventAll(event, data) {
33
+ for (const { res } of this._clients.values())
34
+ res.sseEventSend(event, data);
35
+ }
36
+ sendEventTo(id, event, data) {
37
+ const client = this._clients.get(id);
38
+ if (!client)
39
+ return;
40
+ client.res.sseEventSend(event, data);
32
41
  }
33
42
  disconnect(id) {
34
43
  const client = this._clients.get(id);
35
- if (client) {
36
- client.res.end();
37
- this._clients.delete(id);
38
- }
44
+ if (!client)
45
+ return;
46
+ client.res.end();
47
+ this._clients.delete(id);
39
48
  }
40
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/falcon-frame",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",