melonykit 0.9.3 → 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,6 +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, connectClientEvents, sendClientEvent } from './_events.js';
|
|
5
6
|
|
|
6
7
|
export async function importModules(
|
|
7
8
|
srcFolder,
|
|
@@ -35,27 +36,34 @@ export async function importRestEndpoints(
|
|
|
35
36
|
);
|
|
36
37
|
}
|
|
37
38
|
});
|
|
39
|
+
|
|
40
|
+
connectClientEvents(app);
|
|
38
41
|
};
|
|
39
42
|
|
|
40
|
-
//
|
|
43
|
+
// --------------------------------------------------------------------- Private
|
|
41
44
|
function _createEndpointHandler(expressHandler) {
|
|
42
45
|
return async function handler(req, res) {
|
|
43
46
|
const context = {
|
|
44
47
|
req,
|
|
45
48
|
res,
|
|
49
|
+
handle,
|
|
50
|
+
invoke,
|
|
51
|
+
sendClientEvent,
|
|
46
52
|
respondSuccess(data = {}, statusCode = 200) {
|
|
47
53
|
return res.status(statusCode).json(data);
|
|
48
54
|
},
|
|
49
55
|
respondError(error, statusCode = 500) {
|
|
50
56
|
debug(error);
|
|
51
57
|
return res.status(statusCode).json(null);
|
|
52
|
-
}
|
|
58
|
+
},
|
|
59
|
+
debug
|
|
53
60
|
};
|
|
54
61
|
|
|
55
|
-
try {
|
|
62
|
+
try {
|
|
63
|
+
return await expressHandler(context);
|
|
64
|
+
}
|
|
56
65
|
catch (error) {
|
|
57
66
|
debug(error);
|
|
58
|
-
|
|
59
67
|
return context.respondError(error);
|
|
60
68
|
}
|
|
61
69
|
};
|
package/dist/server/server.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export { default as debug } from './_debug.js';
|
|
2
|
-
export { importModules, importRestEndpoints } from './
|
|
3
|
-
export {
|
|
4
|
-
export { handle, invoke, subscribe, publish } from './_server-events.js';
|
|
2
|
+
export { importModules, importRestEndpoints } from './_imports.js';
|
|
3
|
+
export { handle, invoke, subscribe, publish, sendClientEvent } from './_events.js';
|
package/package.json
CHANGED
|
@@ -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
|
-
};
|