melonykit 0.3.3 → 0.4.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.
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { dirname, join, resolve } from 'node:path';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
|
+
import { globSync } from 'glob';
|
|
5
|
+
import compression from 'compression';
|
|
6
|
+
import express from 'express';
|
|
7
|
+
import Mustache from 'mustache';
|
|
8
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const srcFolder = resolve(`${__dirname}/../../src`);
|
|
11
|
+
const emitter = new EventEmitter();
|
|
12
|
+
const clients = new Set();
|
|
13
|
+
const handlers = new Map();
|
|
14
|
+
|
|
15
|
+
emitter.setMaxListeners(50);
|
|
16
|
+
|
|
17
|
+
export async function initModules(app) {
|
|
18
|
+
await _importEachModule('**/_database.*.js');
|
|
19
|
+
await _importEachModule('**/_service.*.js');
|
|
20
|
+
await _importEachModule('**/_endpoints.*.js', module => {
|
|
21
|
+
const endpoints = Array.isArray(module.default)
|
|
22
|
+
? module.default
|
|
23
|
+
: [module.default];
|
|
24
|
+
|
|
25
|
+
for (const endpoint of endpoints) {
|
|
26
|
+
app[endpoint.method](
|
|
27
|
+
endpoint.path,
|
|
28
|
+
_createEndpointHandler(endpoint.handler)
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function connectClientEvents (app) {
|
|
35
|
+
app.get('/api/events', (req, res) => {
|
|
36
|
+
res.setHeader('Content-Type', 'text/event-stream');
|
|
37
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
38
|
+
res.setHeader('Connection', 'keep-alive');
|
|
39
|
+
|
|
40
|
+
res.flushHeaders();
|
|
41
|
+
|
|
42
|
+
clients.add(res);
|
|
43
|
+
|
|
44
|
+
// Immediately establish the stream.
|
|
45
|
+
res.write(': connected\n\n');
|
|
46
|
+
|
|
47
|
+
req.on('close', () => clients.delete(res));
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export async function start(app, port = 3000) {
|
|
52
|
+
app.use(express.json());
|
|
53
|
+
|
|
54
|
+
if (process.env.NODE_ENV === 'production') {
|
|
55
|
+
// Enable gzip for prod
|
|
56
|
+
app.use(compression());
|
|
57
|
+
|
|
58
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
59
|
+
const buildPath = join(__dirname, '../../dist');
|
|
60
|
+
|
|
61
|
+
// Define path to static web files
|
|
62
|
+
app.use(express.static(buildPath));
|
|
63
|
+
|
|
64
|
+
// Single page application fallback for client routing
|
|
65
|
+
app.get(/.*/, (_, res) => {
|
|
66
|
+
res.sendFile(join(buildPath, 'index.html'));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return app.listen(port,
|
|
71
|
+
() => console.log(`Node Server 🚀 http://localhost:${port}`)
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export function sendClientEvent(type, data = null) {
|
|
76
|
+
const message =
|
|
77
|
+
`event: ${type}\n` +
|
|
78
|
+
`data: ${JSON.stringify(data)}\n\n`;
|
|
79
|
+
|
|
80
|
+
for (const client of clients) {
|
|
81
|
+
client.write(message);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function renderTemplate (template, data) {
|
|
86
|
+
const html = Mustache.render(template, JSON.parse(data));
|
|
87
|
+
|
|
88
|
+
return `<!-- Generiertes HTML -->${html}`;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export function subscribe(eventName, listener) {
|
|
92
|
+
emitter.on(eventName, listener);
|
|
93
|
+
|
|
94
|
+
return () => emitter.off(eventName, listener);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export async function publish(eventName, data) {
|
|
98
|
+
const listeners = emitter.listeners(eventName);
|
|
99
|
+
|
|
100
|
+
if (listeners.length === 0) return undefined;
|
|
101
|
+
|
|
102
|
+
if (listeners.length === 1) {
|
|
103
|
+
return Promise.resolve().then(() => listeners[0](data));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return Promise.all(listeners.map(
|
|
107
|
+
listener => Promise.resolve().then(() => listener(data))
|
|
108
|
+
));
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export function handle(invocationName, handler) {
|
|
112
|
+
if (handlers.has(invocationName)) {
|
|
113
|
+
throw new Error(`Handle - Handler already registered for "${invocationName}".`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
handlers.set(invocationName, handler);
|
|
117
|
+
|
|
118
|
+
return () => {
|
|
119
|
+
if (handlers.get(invocationName) === handler) {
|
|
120
|
+
handlers.delete(invocationName);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export async function invoke(invocationName, data) {
|
|
126
|
+
const handler = handlers.get(invocationName);
|
|
127
|
+
|
|
128
|
+
if (!handler) {
|
|
129
|
+
throw new Error(`Invoke - No handler registered for "${invocationName}".`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return Promise.resolve().then(() => handler(data));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export function debug(error) {
|
|
136
|
+
if (!process.env.DEBUG === 'true') return;
|
|
137
|
+
|
|
138
|
+
console.log('[DEBUG]: ', error);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ----------------------------------------------------------------------------- Private
|
|
142
|
+
async function _importEachModule(filePathPattern, callback = async () => {}) {
|
|
143
|
+
const filePaths = globSync(filePathPattern, {
|
|
144
|
+
cwd: srcFolder,
|
|
145
|
+
absolute: true
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
for (const filePath of filePaths) {
|
|
149
|
+
const module = await import(pathToFileURL(filePath).href);
|
|
150
|
+
|
|
151
|
+
await callback(module);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
function _createEndpointHandler(expressHandler) {
|
|
156
|
+
return async function handler(req, res) {
|
|
157
|
+
const context = {
|
|
158
|
+
req,
|
|
159
|
+
res,
|
|
160
|
+
respondSuccess(data = {}, statusCode = 200) {
|
|
161
|
+
return res.status(statusCode).json(data);
|
|
162
|
+
},
|
|
163
|
+
respondError(error, statusCode = 500) {
|
|
164
|
+
debug(error);
|
|
165
|
+
return res.status(statusCode).json(null);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
try { return await expressHandler(context); }
|
|
170
|
+
catch (error) { return context.respondError(error); }
|
|
171
|
+
};
|
|
172
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Vadim Hermann",
|
|
3
3
|
"name": "melonykit",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Preact based ui and utility functions toolkit",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"module": "./dist/index.js",
|
|
9
9
|
"scripts": {
|
|
10
|
-
"build": "NODE_ENV=production vite build",
|
|
10
|
+
"build": "NODE_ENV=production vite build && cp -R src/server dist/server",
|
|
11
11
|
"dev": "vite",
|
|
12
12
|
"test": "playwright test",
|
|
13
13
|
"prepack": "npm run build",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"exports": {
|
|
17
17
|
".": "./dist/index.js",
|
|
18
18
|
"./functions": "./dist/functions.js",
|
|
19
|
+
"./server": "./dist/server/server.js",
|
|
19
20
|
"./assets/*": "./dist/assets/*"
|
|
20
21
|
},
|
|
21
22
|
"files": [
|
|
@@ -29,7 +30,8 @@
|
|
|
29
30
|
"preact": "10.29.8"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@emotion/css": "11.13.5"
|
|
33
|
+
"@emotion/css": "11.13.5",
|
|
34
|
+
"glob": "13.0.6"
|
|
33
35
|
},
|
|
34
36
|
"devDependencies": {
|
|
35
37
|
"@pivanov/vite-plugin-svg-sprite": "3.1.8",
|