@cyberismo/backend 0.0.5 → 0.0.7

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.
Files changed (42) hide show
  1. package/README.md +1 -0
  2. package/dist/app.js +65 -0
  3. package/dist/app.js.map +1 -0
  4. package/dist/export.js +228 -0
  5. package/dist/export.js.map +1 -0
  6. package/dist/index.js +41 -76
  7. package/dist/index.js.map +1 -1
  8. package/dist/main.js +19 -1
  9. package/dist/main.js.map +1 -1
  10. package/dist/public/THIRD-PARTY.txt +47 -47
  11. package/dist/public/assets/index-BngW8o1w.css +1 -0
  12. package/dist/public/assets/index-D5kiRHuF.js +111171 -0
  13. package/dist/public/config.json +3 -0
  14. package/dist/public/index.html +11 -2
  15. package/dist/routes/cards.js +48 -20
  16. package/dist/routes/cards.js.map +1 -1
  17. package/dist/routes/fieldTypes.js +3 -1
  18. package/dist/routes/fieldTypes.js.map +1 -1
  19. package/dist/routes/linkTypes.js +3 -1
  20. package/dist/routes/linkTypes.js.map +1 -1
  21. package/dist/routes/templates.js +8 -1
  22. package/dist/routes/templates.js.map +1 -1
  23. package/dist/routes/tree.js +3 -1
  24. package/dist/routes/tree.js.map +1 -1
  25. package/dist/utils.js +87 -0
  26. package/dist/utils.js.map +1 -0
  27. package/package.json +7 -3
  28. package/src/app.ts +79 -0
  29. package/src/export.ts +288 -0
  30. package/src/index.ts +47 -91
  31. package/src/main.ts +18 -1
  32. package/src/routes/cards.ts +121 -69
  33. package/src/routes/fieldTypes.ts +4 -2
  34. package/src/routes/linkTypes.ts +6 -1
  35. package/src/routes/templates.ts +11 -1
  36. package/src/routes/tree.ts +6 -1
  37. package/src/utils.ts +100 -0
  38. package/dist/public/assets/index-DqBrVRB4.js +0 -605
  39. package/dist/public/assets/index-ImgtZ9pq.css +0 -1
  40. package/dist/routes/cardTypes.js +0 -49
  41. package/dist/routes/cardTypes.js.map +0 -1
  42. package/src/routes/cardTypes.ts +0 -54
package/src/export.ts ADDED
@@ -0,0 +1,288 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
13
+
14
+ import path from 'node:path';
15
+
16
+ import { mkdir, readFile } from 'node:fs/promises';
17
+
18
+ import { CommandManager } from '@cyberismo/data-handler';
19
+ import { createApp } from './app.js';
20
+ import { cp, writeFile } from 'node:fs/promises';
21
+ import {
22
+ runCbSafely,
23
+ runInParallel,
24
+ staticFrontendDirRelative,
25
+ } from './utils.js';
26
+ import { QueryResult } from '@cyberismo/data-handler/types/queries';
27
+ import { Context, Hono, MiddlewareHandler } from 'hono';
28
+ import mime from 'mime-types';
29
+
30
+ let _cardQueryPromise: Promise<QueryResult<'card'>[]> | null = null;
31
+
32
+ /**
33
+ * DO NO USE DIRECTLY. This resets the callOnce map, allowing you to redo the export.
34
+ * Also resets the card query promise.
35
+ */
36
+ export function reset() {
37
+ _cardQueryPromise = null;
38
+ }
39
+
40
+ /**
41
+ * Get the card query result for a given card key. Should only be called during
42
+ * static site generation
43
+ * @param projectPath - Path to the project.
44
+ * @param cardKey - Key of the card to get the query result for.
45
+ * @returns The card query result for the given card key.
46
+ */
47
+ export async function getCardQueryResult(
48
+ projectPath: string,
49
+ cardKey?: string,
50
+ ): Promise<QueryResult<'card'>[]> {
51
+ if (!_cardQueryPromise) {
52
+ const commands = await CommandManager.getInstance(projectPath);
53
+ // fetch all cards
54
+ _cardQueryPromise = commands.calculateCmd.runQuery('card', {});
55
+ }
56
+ return _cardQueryPromise.then((results) => {
57
+ if (!cardKey) {
58
+ return results;
59
+ }
60
+ const card = results.find((r) => r.key === cardKey);
61
+ if (!card) {
62
+ throw new Error(`Card ${cardKey} not found`);
63
+ }
64
+ return [card];
65
+ });
66
+ }
67
+
68
+ /**
69
+ * Export the site to a given directory.
70
+ * Note: Do not call this function in parallel.
71
+ * @param projectPath - Path to the project.
72
+ * @param exportDir - Directory to export to.
73
+ * @param level - Log level for the operation.
74
+ * @param onProgress - Optional progress callback function.
75
+ */
76
+ export async function exportSite(
77
+ projectPath: string,
78
+ exportDir?: string,
79
+ level?: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal',
80
+ onProgress?: (current?: number, total?: number) => void,
81
+ ) {
82
+ exportDir = exportDir || 'static';
83
+
84
+ const app = createApp(projectPath);
85
+
86
+ // copy whole frontend to the same directory
87
+ await cp(staticFrontendDirRelative, exportDir, { recursive: true });
88
+ // read config file and change export to true
89
+ const config = await readFile(path.join(exportDir, 'config.json'), 'utf-8');
90
+ const configJson = JSON.parse(config);
91
+ configJson.staticMode = true;
92
+ await writeFile(
93
+ path.join(exportDir, 'config.json'),
94
+ JSON.stringify(configJson),
95
+ );
96
+
97
+ const commands = await CommandManager.getInstance(projectPath, {
98
+ logLevel: level,
99
+ });
100
+ await toSsg(app, commands, exportDir, onProgress);
101
+ }
102
+
103
+ async function getRoutes(app: Hono) {
104
+ const routes = new Set<string>();
105
+ for (const route of app.routes) {
106
+ if (route.method === 'GET') routes.add(route.path);
107
+ }
108
+
109
+ // handles both routes with and without dynamic parameters
110
+ const filteredRoutes = [];
111
+ for (const route of routes) {
112
+ if (!route.includes(':')) {
113
+ filteredRoutes.push(route);
114
+ continue;
115
+ }
116
+ const response = await createSsgRequest(app, route, true);
117
+ if (response.ok) {
118
+ const params = await response.json();
119
+ if (Array.isArray(params) && params.length > 0) {
120
+ for (const param of params) {
121
+ let newRoute = route;
122
+ for (const [key, value] of Object.entries(param)) {
123
+ newRoute = newRoute.replace(`:${key}`, `${value}`);
124
+ }
125
+ filteredRoutes.push(newRoute);
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ return filteredRoutes;
132
+ }
133
+
134
+ /**
135
+ * This is similar to hono's ssg function, but it only calls middlewares once
136
+ * @param app
137
+ * @param onProgress
138
+ */
139
+ async function toSsg(
140
+ app: Hono,
141
+ commands: CommandManager,
142
+ dir: string,
143
+ onProgress?: (current?: number, total?: number) => void,
144
+ ) {
145
+ reset();
146
+ await commands.calculateCmd.generate();
147
+
148
+ const promises = [];
149
+
150
+ const routes = await getRoutes(app);
151
+ await runCbSafely(() => onProgress?.(0, routes.length));
152
+
153
+ let processedFiles = 0;
154
+ let failed = false;
155
+ let errors: Error[] = [];
156
+ const done = async (error?: Error) => {
157
+ if (error) {
158
+ failed = true;
159
+ errors.push(error);
160
+ }
161
+ processedFiles++;
162
+ await runCbSafely(() => onProgress?.(processedFiles, routes.length));
163
+ };
164
+ for (const route of routes) {
165
+ promises.push(async () => {
166
+ try {
167
+ const response = await createSsgRequest(app, route, false);
168
+ if (!response.ok) {
169
+ const error = await response.json();
170
+ if (typeof error === 'object' && error !== null && 'error' in error) {
171
+ await done(
172
+ new Error(`Failed to export route ${route}: ${error.error}`),
173
+ );
174
+ } else {
175
+ await done(new Error(`Failed to export route ${route}`));
176
+ }
177
+ return;
178
+ }
179
+ await writeFileToDir(dir, response, route);
180
+ await done();
181
+ } catch (error) {
182
+ await done(error instanceof Error ? error : new Error(String(error)));
183
+ }
184
+ });
185
+ }
186
+
187
+ await runInParallel(promises, 5);
188
+ if (failed) {
189
+ const message = `Errors:\n${errors.map((e) => e.message).join('\n')}`;
190
+ throw new Error(message);
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Get the file content and file ending for a given response and route.
196
+ * @param response - The response to get the file content and file ending for.
197
+ * @param route - The route to get the file content and file ending for.
198
+ * @returns The file content and file ending for the given response and route.
199
+ * If the route already has a file ending, it will be returned as an empty string.
200
+ */
201
+ async function getFileContent(
202
+ response: Response,
203
+ route: string,
204
+ ): Promise<{
205
+ content: ArrayBuffer;
206
+ fileEnding: string;
207
+ }> {
208
+ // Check if route already has an extension
209
+ const routeExtension = path.extname(route);
210
+ if (routeExtension) {
211
+ // Trust the existing extension in the route
212
+ const content = await response.arrayBuffer();
213
+ return {
214
+ content,
215
+ fileEnding: '',
216
+ };
217
+ }
218
+
219
+ // No extension in route, fall back to content type detection
220
+ const contentType = response.headers.get('content-type');
221
+ if (!contentType) {
222
+ throw new Error('No content type');
223
+ }
224
+ const extension = mime.extension(contentType);
225
+ if (!extension) {
226
+ throw new Error('Unsupported content type');
227
+ }
228
+
229
+ // Use ArrayBuffer for all content types
230
+ const content = await response.arrayBuffer();
231
+ return {
232
+ content,
233
+ fileEnding: `.${extension}`,
234
+ };
235
+ }
236
+
237
+ async function writeFileToDir(dir: string, response: Response, route: string) {
238
+ const { content, fileEnding } = await getFileContent(response, route);
239
+
240
+ let filePath = path.join(dir, route);
241
+
242
+ // if route does not have a file ending, add it based on the content type
243
+ if (!route.endsWith(fileEnding)) {
244
+ filePath += fileEnding;
245
+ }
246
+
247
+ await mkdir(path.dirname(filePath), { recursive: true });
248
+ await writeFile(filePath, Buffer.from(content));
249
+ }
250
+
251
+ // findroutes = if this request is used to find the routes in the app
252
+ function createSsgRequest(
253
+ app: Hono,
254
+ route: string,
255
+ findRoutes: boolean = true,
256
+ ) {
257
+ return app.request(route, {
258
+ headers: new Headers({
259
+ 'x-ssg': 'true',
260
+ 'x-ssg-find': findRoutes ? 'true' : 'false',
261
+ }),
262
+ });
263
+ }
264
+
265
+ /**
266
+ * Check if the request is a static site generation request.
267
+ * @param c - The context of the request.
268
+ * @returns True if the request is a static site generation request.
269
+ */
270
+ export function isSSGContext(c: Context) {
271
+ return c.req.header('x-ssg') === 'true';
272
+ }
273
+
274
+ /**
275
+ * This middleware is used to find the routes in the app.
276
+ * @param fn - The function to call to get the parameters for the route.
277
+ * @returns The middleware handler.
278
+ */
279
+ export function ssgParams(
280
+ fn?: (c: Context) => Promise<unknown[]>,
281
+ ): MiddlewareHandler {
282
+ return async (c, next) => {
283
+ if (c.req.header('x-ssg-find') === 'true') {
284
+ return fn ? c.json(await fn(c)) : c.json([]);
285
+ }
286
+ return next();
287
+ };
288
+ }
package/src/index.ts CHANGED
@@ -1,82 +1,73 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
1
13
  import { serve } from '@hono/node-server';
2
14
  import { Hono } from 'hono';
3
- import { cors } from 'hono/cors';
4
15
  import { serveStatic } from '@hono/node-server/serve-static';
5
16
  import path from 'node:path';
6
- import { createServer } from 'node:net';
7
-
8
- import { attachCommandManager } from './middleware/commandManager.js';
9
-
10
- // Import routes
11
- import cardsRouter from './routes/cards.js';
12
- import cardTypesRouter from './routes/cardTypes.js';
13
- import fieldTypesRouter from './routes/fieldTypes.js';
14
- import linkTypesRouter from './routes/linkTypes.js';
15
- import templatesRouter from './routes/templates.js';
16
- import treeRouter from './routes/tree.js';
17
-
18
- import { fileURLToPath } from 'node:url';
19
17
  import { readFile } from 'node:fs/promises';
20
-
21
- const filename = fileURLToPath(import.meta.url);
22
- const dirname = path.dirname(filename);
23
-
24
- export function createApp(projectPath?: string) {
18
+ import { findFreePort } from './utils.js';
19
+ import { createApp } from './app.js';
20
+ export { exportSite } from './export.js';
21
+
22
+ const DEFAULT_PORT = 3000;
23
+ const DEFAULT_MAX_PORT = DEFAULT_PORT + 100;
24
+
25
+ /**
26
+ * Preview the exported site
27
+ * @param dir - Directory to preview
28
+ * @param findPort - If true, find a free port
29
+ */
30
+ export async function previewSite(dir: string, findPort: boolean = true) {
25
31
  const app = new Hono();
26
-
27
- app.use('/api', cors());
28
-
29
- app.use(
30
- '*',
31
- serveStatic({
32
- root: path.relative(process.cwd(), path.resolve(dirname, 'public')),
33
- }),
32
+ app.use(serveStatic({ root: dir }));
33
+ app.get('*', (c) =>
34
+ c.html(
35
+ readFile(path.join(dir, 'index.html')).then((file) => file.toString()),
36
+ ),
34
37
  );
35
38
 
36
- // Attach CommandManager to all requests
37
- app.use(attachCommandManager(projectPath));
38
-
39
- // Wire up routes
40
- app.route('/api/cards', cardsRouter);
41
- app.route('/api/cardTypes', cardTypesRouter);
42
- app.route('/api/fieldTypes', fieldTypesRouter);
43
- app.route('/api/linkTypes', linkTypesRouter);
44
- app.route('/api/templates', templatesRouter);
45
- app.route('/api/tree', treeRouter);
39
+ let port = parseInt(process.env.PORT || DEFAULT_PORT.toString(), 10);
46
40
 
47
- // serve index.html for all other routes
48
- app.notFound(async (c) => {
49
- if (c.req.path.startsWith('/api')) {
50
- return c.text('Not Found', 400);
51
- }
52
- const file = await readFile(path.join(dirname, 'public', 'index.html'));
53
- return c.html(file.toString());
54
- });
55
- // Error handling
56
- app.onError((err, c) => {
57
- console.error(err.stack);
58
- return c.text('Internal Server Error', 500);
59
- });
60
-
61
- return app;
41
+ if (findPort) {
42
+ port = await findFreePort(port, DEFAULT_MAX_PORT);
43
+ }
44
+ await startApp(app, port);
62
45
  }
63
46
 
47
+ /**
48
+ * Start the server
49
+ * @param projectPath - Path to the project
50
+ * @param findPort - If true, find a free port
51
+ */
64
52
  export async function startServer(
65
53
  projectPath?: string,
66
54
  findPort: boolean = true,
67
55
  ) {
68
- let port = parseInt(process.env.PORT || '3000', 10);
56
+ let port = parseInt(process.env.PORT || DEFAULT_PORT.toString(), 10);
69
57
 
70
58
  if (findPort) {
71
- port = await findFreePort(port);
59
+ port = await findFreePort(port, DEFAULT_MAX_PORT);
72
60
  }
73
-
74
61
  const app = createApp(projectPath);
62
+ await startApp(app, port);
63
+ }
64
+
65
+ async function startApp(app: Hono, port: number) {
75
66
  // Start server
76
67
  serve(
77
68
  {
78
69
  fetch: app.fetch,
79
- port: Number(port),
70
+ port: port,
80
71
  },
81
72
  (info) => {
82
73
  console.log(`Running Cyberismo app on http://localhost:${info.port}`);
@@ -84,38 +75,3 @@ export async function startServer(
84
75
  },
85
76
  );
86
77
  }
87
-
88
- async function findFreePort(
89
- port: number,
90
- maxAttempts: number = 100,
91
- ): Promise<number> {
92
- for (let i = port; i < port + maxAttempts; i++) {
93
- try {
94
- await testPort(i);
95
- return i;
96
- } catch (err) {
97
- if (err instanceof Error && err.message.includes('EADDRINUSE')) {
98
- console.log(`Port ${i} is already in use, trying next port...`);
99
- } else {
100
- throw err;
101
- }
102
- }
103
- }
104
- throw new Error('Failed to find free port');
105
- }
106
-
107
- function testPort(port: number) {
108
- return new Promise((resolve, reject) => {
109
- const server = createServer();
110
- server.listen(port, () => {
111
- server.close();
112
- resolve(true);
113
- });
114
- server.on('error', (err) => {
115
- reject(err);
116
- });
117
- setTimeout(() => {
118
- reject(new Error('Timed out waiting for port to be free'));
119
- }, 2000);
120
- });
121
- }
package/src/main.ts CHANGED
@@ -1,7 +1,24 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
1
13
  import { startServer } from './index.js';
14
+ import { exportSite } from './export.js';
2
15
  import dotenv from 'dotenv';
3
16
 
4
17
  // Load environment variables from .env file
5
18
  dotenv.config();
6
19
 
7
- startServer(process.env.npm_config_project_path);
20
+ if (process.argv.includes('--export')) {
21
+ exportSite(process.env.npm_config_project_path || '');
22
+ } else {
23
+ startServer(process.env.npm_config_project_path || '');
24
+ }