melonykit 0.4.2 → 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,9 +1,10 @@
1
1
  import { resolve } from 'node:path';
2
2
  import { pathToFileURL } from 'node:url';
3
3
  import { globSync } from 'glob';
4
+ import debug from './_debug.js';
4
5
 
5
- export default async function importModules(
6
- srcFolder = 'src',
6
+ export async function importModules(
7
+ srcFolder,
7
8
  filePathPatterns,
8
9
  callback
9
10
  ) {
@@ -16,3 +17,42 @@ export default async function importModules(
16
17
  await callback?.(module);
17
18
  }
18
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
+ };
@@ -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,7 +1,7 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "0.4.2",
4
+ "version": "0.4.3",
5
5
  "type": "module",
6
6
  "description": "Preact based ui and utility functions toolkit",
7
7
  "main": "./dist/index.js",
@@ -1,40 +0,0 @@
1
- import importModules from './_import-modules.js';
2
-
3
- export default async function importRestEndpoints(
4
- srcFolder,
5
- filePathPatterns,
6
- app
7
- ) {
8
- await importModules(srcFolder, filePathPatterns, module => {
9
- const endpoints = Array.isArray(module.default)
10
- ? module.default
11
- : [module.default];
12
-
13
- for (const endpoint of endpoints) {
14
- app[endpoint.method](
15
- endpoint.path,
16
- _createEndpointHandler(endpoint.handler)
17
- );
18
- }
19
- });
20
- };
21
-
22
- // ----------------------------------------------------------------------------- Private
23
- function _createEndpointHandler(expressHandler) {
24
- return async function handler(req, res) {
25
- const context = {
26
- req,
27
- res,
28
- respondSuccess(data = {}, statusCode = 200) {
29
- return res.status(statusCode).json(data);
30
- },
31
- respondError(error, statusCode = 500) {
32
- debug(error);
33
- return res.status(statusCode).json(null);
34
- }
35
- };
36
-
37
- try { return await expressHandler(context); }
38
- catch (error) { return context.respondError(error); }
39
- };
40
- };