melonykit 0.4.1 → 0.4.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.
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
};
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
2
3
|
import { globSync } from 'glob';
|
|
4
|
+
import debug from './_debug.js';
|
|
3
5
|
|
|
4
|
-
export
|
|
6
|
+
export async function importModules(
|
|
5
7
|
srcFolder,
|
|
6
8
|
filePathPatterns,
|
|
7
9
|
callback
|
|
8
10
|
) {
|
|
9
|
-
const cwd =
|
|
11
|
+
const cwd = resolve(process.cwd(), srcFolder);
|
|
10
12
|
const filePaths = globSync(filePathPatterns, { cwd, absolute: true });
|
|
11
13
|
|
|
12
14
|
for (const filePath of filePaths) {
|
|
@@ -15,3 +17,42 @@ export default async function importModules(
|
|
|
15
17
|
await callback?.(module);
|
|
16
18
|
}
|
|
17
19
|
};
|
|
20
|
+
|
|
21
|
+
export async function importRestEndpoints(
|
|
22
|
+
srcFolder,
|
|
23
|
+
filePathPatterns,
|
|
24
|
+
app
|
|
25
|
+
) {
|
|
26
|
+
await importModules(srcFolder, filePathPatterns, module => {
|
|
27
|
+
const endpoints = Array.isArray(module.default)
|
|
28
|
+
? module.default
|
|
29
|
+
: [module.default];
|
|
30
|
+
|
|
31
|
+
for (const endpoint of endpoints) {
|
|
32
|
+
app[endpoint.method](
|
|
33
|
+
endpoint.path,
|
|
34
|
+
_createEndpointHandler(endpoint.handler)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// ----------------------------------------------------------------------------- Private
|
|
41
|
+
function _createEndpointHandler(expressHandler) {
|
|
42
|
+
return async function handler(req, res) {
|
|
43
|
+
const context = {
|
|
44
|
+
req,
|
|
45
|
+
res,
|
|
46
|
+
respondSuccess(data = {}, statusCode = 200) {
|
|
47
|
+
return res.status(statusCode).json(data);
|
|
48
|
+
},
|
|
49
|
+
respondError(error, statusCode = 500) {
|
|
50
|
+
debug(error);
|
|
51
|
+
return res.status(statusCode).json(null);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
try { return await expressHandler(context); }
|
|
56
|
+
catch (error) { return context.respondError(error); }
|
|
57
|
+
};
|
|
58
|
+
};
|
package/dist/server/server.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { default as importModules } from './_import-modules.js';
|
|
2
|
-
export { default as importRestEndpoints } from './_import-rest-endpoints.js';
|
|
3
1
|
export { default as debug } from './_debug.js';
|
|
2
|
+
export { importModules, importRestEndpoints } from './_import-modules.js';
|
|
3
|
+
export { connectClientEvents, sendClientEvent } from './_client-events.js';
|
package/package.json
CHANGED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
2
|
-
import { globSync } from 'glob';
|
|
3
|
-
|
|
4
|
-
export default async function importModules(
|
|
5
|
-
srcFolder,
|
|
6
|
-
filePathPatterns,
|
|
7
|
-
callback
|
|
8
|
-
) {
|
|
9
|
-
const cwd = fileURLToPath(new URL(srcFolder, import.meta.url));
|
|
10
|
-
const filePaths = globSync(filePathPatterns, { cwd, absolute: true });
|
|
11
|
-
|
|
12
|
-
for (const filePath of filePaths) {
|
|
13
|
-
const module = await import(pathToFileURL(filePath).href);
|
|
14
|
-
|
|
15
|
-
await callback?.(module);
|
|
16
|
-
}
|
|
17
|
-
};
|