melonykit 0.9.3 → 0.10.0

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.
@@ -2,6 +2,9 @@ import { resolve } from 'node:path';
2
2
  import { pathToFileURL } from 'node:url';
3
3
  import { globSync } from 'glob';
4
4
  import debug from './_debug.js';
5
+ import { handle, invoke } from './_server-events.js';
6
+
7
+ const clients = new Set();
5
8
 
6
9
  export async function importModules(
7
10
  srcFolder,
@@ -35,28 +38,62 @@ export async function importRestEndpoints(
35
38
  );
36
39
  }
37
40
  });
41
+
42
+ _connectClientEvents(app);
38
43
  };
39
44
 
40
- // ----------------------------------------------------------------------------- Private
45
+ // --------------------------------------------------------------------- Private
41
46
  function _createEndpointHandler(expressHandler) {
42
47
  return async function handler(req, res) {
43
48
  const context = {
44
49
  req,
45
50
  res,
51
+ sendClientEvent: _sendClientEvent,
52
+ handle,
53
+ invoke,
46
54
  respondSuccess(data = {}, statusCode = 200) {
47
55
  return res.status(statusCode).json(data);
48
56
  },
49
57
  respondError(error, statusCode = 500) {
50
58
  debug(error);
51
59
  return res.status(statusCode).json(null);
52
- }
60
+ },
61
+ debug
53
62
  };
54
63
 
55
- try { return await expressHandler(context); }
64
+ try {
65
+ return await expressHandler(context);
66
+ }
56
67
  catch (error) {
57
68
  debug(error);
58
-
59
69
  return context.respondError(error);
60
70
  }
61
71
  };
62
72
  };
73
+
74
+ function _connectClientEvents(app) {
75
+ app.get('/api/client-events', (req, res) => {
76
+ res.setHeader('Content-Type', 'text/event-stream');
77
+ res.setHeader('Cache-Control', 'no-cache');
78
+ res.setHeader('Connection', 'keep-alive');
79
+
80
+ res.flushHeaders();
81
+
82
+ clients.add(res);
83
+
84
+ // Immediately establish the stream.
85
+ res.write(': connected\n\n');
86
+
87
+ req.on('close', () => clients.delete(res));
88
+ });
89
+ };
90
+
91
+ function _sendClientEvent(type, data = null) {
92
+ const message =
93
+ `event: ${type}\n` +
94
+ `data: ${JSON.stringify(data)}\n\n`;
95
+
96
+ for (const client of clients) {
97
+ client.write(message);
98
+ }
99
+ };
@@ -1,4 +1,3 @@
1
1
  export { default as debug } from './_debug.js';
2
2
  export { importModules, importRestEndpoints } from './_import-modules.js';
3
- export { connectClientEvents, sendClientEvent } from './_client-events.js';
4
3
  export { handle, invoke, subscribe, publish } from './_server-events.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "0.9.3",
4
+ "version": "0.10.0",
5
5
  "type": "module",
6
6
  "description": "Preact based ui and utility functions toolkit",
7
7
  "main": "./dist/index.js",
@@ -1,28 +0,0 @@
1
- const clients = new Set();
2
-
3
- export function connectClientEvents(app) {
4
- app.get('/api/events', (req, res) => {
5
- res.setHeader('Content-Type', 'text/event-stream');
6
- res.setHeader('Cache-Control', 'no-cache');
7
- res.setHeader('Connection', 'keep-alive');
8
-
9
- res.flushHeaders();
10
-
11
- clients.add(res);
12
-
13
- // Immediately establish the stream.
14
- res.write(': connected\n\n');
15
-
16
- req.on('close', () => clients.delete(res));
17
- });
18
- };
19
-
20
- export function sendClientEvent(type, data = null) {
21
- const message =
22
- `event: ${type}\n` +
23
- `data: ${JSON.stringify(data)}\n\n`;
24
-
25
- for (const client of clients) {
26
- client.write(message);
27
- }
28
- };