melonykit 0.10.0 → 0.10.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.
|
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events';
|
|
|
2
2
|
|
|
3
3
|
const emitter = new EventEmitter();
|
|
4
4
|
const handlers = new Map();
|
|
5
|
+
const clients = new Set();
|
|
5
6
|
|
|
6
7
|
emitter.setMaxListeners(50);
|
|
7
8
|
|
|
@@ -29,7 +30,6 @@ export async function invoke(invocationName, data) {
|
|
|
29
30
|
return Promise.resolve().then(() => handler(data));
|
|
30
31
|
};
|
|
31
32
|
|
|
32
|
-
|
|
33
33
|
export function subscribe(eventName, listener) {
|
|
34
34
|
emitter.on(eventName, listener);
|
|
35
35
|
|
|
@@ -49,3 +49,30 @@ export async function publish(eventName, data) {
|
|
|
49
49
|
listener => Promise.resolve().then(() => listener(data))
|
|
50
50
|
));
|
|
51
51
|
};
|
|
52
|
+
|
|
53
|
+
export function connectClientEvents(app) {
|
|
54
|
+
app.get('/api/client-events', (req, res) => {
|
|
55
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
56
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
57
|
+
res.setHeader('Connection', 'keep-alive');
|
|
58
|
+
|
|
59
|
+
res.flushHeaders();
|
|
60
|
+
|
|
61
|
+
clients.add(res);
|
|
62
|
+
|
|
63
|
+
// Immediately establish the stream.
|
|
64
|
+
res.write(': connected\n\n');
|
|
65
|
+
|
|
66
|
+
req.on('close', () => clients.delete(res));
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export function sendClientEvent(type, data = null) {
|
|
71
|
+
const message =
|
|
72
|
+
`event: ${type}\n` +
|
|
73
|
+
`data: ${JSON.stringify(data)}\n\n`;
|
|
74
|
+
|
|
75
|
+
for (const client of clients) {
|
|
76
|
+
client.write(message);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
@@ -2,9 +2,7 @@ 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 './
|
|
6
|
-
|
|
7
|
-
const clients = new Set();
|
|
5
|
+
import { handle, invoke, connectClientEvents, sendClientEvent } from './_events.js';
|
|
8
6
|
|
|
9
7
|
export async function importModules(
|
|
10
8
|
srcFolder,
|
|
@@ -39,7 +37,7 @@ export async function importRestEndpoints(
|
|
|
39
37
|
}
|
|
40
38
|
});
|
|
41
39
|
|
|
42
|
-
|
|
40
|
+
connectClientEvents(app);
|
|
43
41
|
};
|
|
44
42
|
|
|
45
43
|
// --------------------------------------------------------------------- Private
|
|
@@ -48,9 +46,9 @@ function _createEndpointHandler(expressHandler) {
|
|
|
48
46
|
const context = {
|
|
49
47
|
req,
|
|
50
48
|
res,
|
|
51
|
-
sendClientEvent: _sendClientEvent,
|
|
52
49
|
handle,
|
|
53
50
|
invoke,
|
|
51
|
+
sendClientEvent,
|
|
54
52
|
respondSuccess(data = {}, statusCode = 200) {
|
|
55
53
|
return res.status(statusCode).json(data);
|
|
56
54
|
},
|
|
@@ -70,30 +68,3 @@ function _createEndpointHandler(expressHandler) {
|
|
|
70
68
|
}
|
|
71
69
|
};
|
|
72
70
|
};
|
|
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
|
-
};
|
package/dist/server/server.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default as debug } from './_debug.js';
|
|
2
|
-
export { importModules, importRestEndpoints } from './
|
|
3
|
-
export { handle, invoke, subscribe, publish } from './
|
|
2
|
+
export { importModules, importRestEndpoints } from './_imports.js';
|
|
3
|
+
export { handle, invoke, subscribe, publish, sendClientEvent } from './_events.js';
|