melonykit 1.18.1 → 1.19.0

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.
@@ -1,27 +1,28 @@
1
1
  import { EventEmitter } from 'node:events';
2
2
 
3
- const emitter = new EventEmitter();
4
- const handlers = new Map();
5
- const clients = new Set();
3
+ const _emitter = new EventEmitter();
4
+ const _handlers = new Map();
5
+ const _clients = new Set();
6
+ const _connectedApps = new WeakSet();
6
7
 
7
- emitter.setMaxListeners(50);
8
+ _emitter.setMaxListeners(50);
8
9
 
9
10
  export function handle(invocationName, handler) {
10
- if (handlers.has(invocationName)) {
11
+ if (_handlers.has(invocationName)) {
11
12
  throw new Error(`Handle - Handler already registered for "${invocationName}".`);
12
13
  }
13
14
 
14
- handlers.set(invocationName, handler);
15
+ _handlers.set(invocationName, handler);
15
16
 
16
17
  return () => {
17
- if (handlers.get(invocationName) === handler) {
18
- handlers.delete(invocationName);
18
+ if (_handlers.get(invocationName) === handler) {
19
+ _handlers.delete(invocationName);
19
20
  }
20
21
  };
21
22
  };
22
23
 
23
24
  export async function invoke(invocationName, data) {
24
- const handler = handlers.get(invocationName);
25
+ const handler = _handlers.get(invocationName);
25
26
 
26
27
  if (!handler) {
27
28
  throw new Error(`Invoke - No handler registered for "${invocationName}".`);
@@ -36,18 +37,18 @@ export function subscribe(eventNames, listener) {
36
37
  )];
37
38
 
38
39
  for (const eventName of names) {
39
- emitter.on(eventName, listener);
40
+ _emitter.on(eventName, listener);
40
41
  }
41
42
 
42
43
  return () => {
43
44
  for (const eventName of names) {
44
- emitter.off(eventName, listener);
45
+ _emitter.off(eventName, listener);
45
46
  }
46
47
  };
47
48
  };
48
49
 
49
50
  export async function publish(eventName, data) {
50
- const listeners = emitter.listeners(eventName);
51
+ const listeners = _emitter.listeners(eventName);
51
52
 
52
53
  if (listeners.length === 0) return undefined;
53
54
 
@@ -61,6 +62,8 @@ export async function publish(eventName, data) {
61
62
  };
62
63
 
63
64
  export function connectClientEvents(app) {
65
+ if (_connectedApps.has(app)) return;
66
+
64
67
  app.get('/api/client-events', (req, res) => {
65
68
  res.setHeader('Content-Type', 'text/event-stream');
66
69
  res.setHeader('Cache-Control', 'no-cache');
@@ -68,13 +71,15 @@ export function connectClientEvents(app) {
68
71
 
69
72
  res.flushHeaders();
70
73
 
71
- clients.add(res);
74
+ _clients.add(res);
72
75
 
73
76
  // Immediately establish the stream.
74
77
  res.write(': connected\n\n');
75
78
 
76
- req.on('close', () => clients.delete(res));
79
+ req.on('close', () => _clients.delete(res));
77
80
  });
81
+
82
+ _connectedApps.add(app);
78
83
  };
79
84
 
80
85
  export function sendClientEvent(type, data = null) {
@@ -82,7 +87,7 @@ export function sendClientEvent(type, data = null) {
82
87
  `event: ${type}\n` +
83
88
  `data: ${JSON.stringify(data)}\n\n`;
84
89
 
85
- for (const client of clients) {
90
+ for (const client of _clients) {
86
91
  client.write(message);
87
92
  }
88
93
  };
@@ -10,12 +10,11 @@ export async function importModules(srcFolder, filePathPatterns, callback) {
10
10
 
11
11
  for (const filePath of filePaths) {
12
12
  const module = await import(pathToFileURL(filePath).href);
13
-
14
13
  await callback?.(module);
15
14
  }
16
15
  };
17
16
 
18
- export async function importRestEndpoints(srcFolder, filePathPatterns, app) {
17
+ export async function initRestEndpoints(srcFolder, filePathPatterns, app) {
19
18
  await importModules(srcFolder, filePathPatterns, module => {
20
19
  const endpoints = Array.isArray(module.default)
21
20
  ? module.default
@@ -32,11 +31,56 @@ export async function importRestEndpoints(srcFolder, filePathPatterns, app) {
32
31
  connectClientEvents(app);
33
32
  };
34
33
 
34
+ /** Registers a GraphQL route from schema modules and enables client events. */
35
+ export async function initGraphqlEndpoints(srcFolder, filePathPatterns, app) {
36
+ const { assertValidSchema, buildSchema, graphql } = await import('graphql');
37
+ const typeDefs = ['type Query'];
38
+ const rootValue = Object.create(null);
39
+
40
+ await importModules(srcFolder, filePathPatterns, module => {
41
+ typeDefs.push(module.typeDefs);
42
+
43
+ for (const [name, resolver] of Object.entries(module.resolvers)) {
44
+ if (Object.hasOwn(rootValue, name))
45
+ throw new Error(`Duplicate GraphQL resolver: ${name}`);
46
+
47
+ rootValue[name] = resolver;
48
+ }
49
+ });
50
+
51
+ const schema = buildSchema(typeDefs.join('\n'));
52
+ assertValidSchema(schema);
53
+
54
+ app.post('/api/graphql', _createEndpointHandler(async ({
55
+ req, respondSuccess, invoke
56
+ }) => {
57
+ const { query, variables, operationName } = req.body ?? {};
58
+
59
+ if (typeof query !== 'string' || !query.trim()) {
60
+ return respondSuccess({
61
+ errors: [{ message: 'Expected a GraphQL query.' }]
62
+ }, 400);
63
+ }
64
+
65
+ const result = await graphql({
66
+ schema,
67
+ source: query,
68
+ variableValues: variables,
69
+ operationName,
70
+ rootValue,
71
+ contextValue: { req, invoke }
72
+ });
73
+
74
+ return respondSuccess(result);
75
+ }));
76
+
77
+ connectClientEvents(app);
78
+ };
79
+
35
80
 
36
81
 
37
82
  //
38
83
  // Helpers
39
-
40
84
  function _createEndpointHandler(expressHandler) {
41
85
  return async function handler(req, res) {
42
86
  const context = {
@@ -1,3 +1,3 @@
1
1
  export { default as debug } from './_debug.js';
2
- export { importModules, importRestEndpoints } from './_imports.js';
2
+ export { importModules, initRestEndpoints, initGraphqlEndpoints } from './_imports.js';
3
3
  export { handle, invoke, subscribe, publish, sendClientEvent } from './_events.js';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "author": "Vadim Hermann",
3
3
  "name": "melonykit",
4
- "version": "1.18.1",
4
+ "version": "1.19.0",
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 && cp -R src/server dist/server",
10
+ "build": "vite build && cp -R src/server dist/server",
11
11
  "dev": "vite",
12
12
  "test": "playwright test",
13
13
  "prepack": "npm run build"
@@ -32,13 +32,14 @@
32
32
  "dependencies": {
33
33
  "@codemirror/commands": "6.11.2",
34
34
  "@codemirror/lang-html": "6.4.12",
35
- "@codemirror/lang-markdown": "6.5.2",
35
+ "@codemirror/lang-markdown": "6.5.3",
36
36
  "@codemirror/language": "6.13.1",
37
37
  "@codemirror/state": "6.7.6",
38
38
  "@codemirror/view": "6.44.2",
39
39
  "@emotion/css": "11.13.5",
40
40
  "dompurify": "3.5.0",
41
- "glob": "14.0.6"
41
+ "glob": "14.0.6",
42
+ "graphql": "17.0.2"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@pivanov/vite-plugin-svg-sprite": "3.1.8",