@cyberismo/backend 0.0.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,40 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+ import { Hono } from 'hono';
13
+ const router = new Hono();
14
+ /**
15
+ * @swagger
16
+ * /api/fieldTypes:
17
+ * get:
18
+ * summary: Returns a list of all field types in the defined project.
19
+ * description: List of field types includes all field types in the project with all their details
20
+ * responses:
21
+ * 200:
22
+ * description: Object containing the project field types. See definitions.ts/FieldTypes for the structure.
23
+ * 400:
24
+ * description: Error in reading project details.
25
+ * 500:
26
+ * description: project_path not set or other internal error
27
+ */
28
+ router.get('/', async (c) => {
29
+ const commands = c.get('commands');
30
+ const response = await commands.showCmd.showResources('fieldTypes');
31
+ if (response) {
32
+ const fieldTypes = await Promise.all(response.map((fieldType) => commands.showCmd.showResource(fieldType)));
33
+ return c.json(fieldTypes);
34
+ }
35
+ else {
36
+ return c.text(`No field types found from path ${c.get('projectPath')}`, 500);
37
+ }
38
+ });
39
+ export default router;
40
+ //# sourceMappingURL=fieldTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fieldTypes.js","sourceRoot":"","sources":["../../src/routes/fieldTypes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IAC1B,MAAM,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAEnC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;IACpE,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,UAAU,GAAG,MAAM,OAAO,CAAC,GAAG,CAClC,QAAQ,CAAC,GAAG,CAAC,CAAC,SAAiB,EAAE,EAAE,CACjC,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CACzC,CACF,CAAC;QAEF,OAAO,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5B,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,CAAC,IAAI,CACX,kCAAkC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,EACxD,GAAG,CACJ,CAAC;IACJ,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,eAAe,MAAM,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+ import { Hono } from 'hono';
13
+ const router = new Hono();
14
+ /**
15
+ * @swagger
16
+ * /api/linkTypes:
17
+ * get:
18
+ * summary: Returns a list of all link types in the defined project.
19
+ * description: List of link types includes all link types in the project with all their details
20
+ * responses:
21
+ * 200:
22
+ * description: Object containing the project link types.
23
+ * 400:
24
+ * description: Error in reading project details.
25
+ * 500:
26
+ * description: project_path not set or other internal error
27
+ */
28
+ router.get('/', async (c) => {
29
+ const commands = c.get('commands');
30
+ const response = await commands.showCmd.showResources('linkTypes');
31
+ if (response) {
32
+ const linkTypes = await Promise.all(response.map((linkType) => commands.showCmd.showResource(linkType)));
33
+ return c.json(linkTypes);
34
+ }
35
+ else {
36
+ return c.text(`No link types found from path ${c.get('projectPath')}`, 500);
37
+ }
38
+ });
39
+ export default router;
40
+ //# sourceMappingURL=linkTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linkTypes.js","sourceRoot":"","sources":["../../src/routes/linkTypes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IAC1B,MAAM,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAEnC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;IACnE,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,GAAG,CACjC,QAAQ,CAAC,GAAG,CAAC,CAAC,QAAgB,EAAE,EAAE,CAChC,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,CACxC,CACF,CAAC;QAEF,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC9E,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,eAAe,MAAM,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+ import { Hono } from 'hono';
13
+ const router = new Hono();
14
+ /**
15
+ * @swagger
16
+ * /api/templates:
17
+ * get:
18
+ * summary: Returns a list of all templates in the defined project.
19
+ * description: List of templates includes only the names of the templates in the project.
20
+ * responses:
21
+ * 200:
22
+ * description: Array containing the names of the project templates.
23
+ * 400:
24
+ * description: Error in reading project details.
25
+ * 500:
26
+ * description: project_path not set or other internal error
27
+ */
28
+ router.get('/', async (c) => {
29
+ const commands = c.get('commands');
30
+ const response = await commands.showCmd.showTemplatesWithDetails();
31
+ if (response) {
32
+ return c.json(response);
33
+ }
34
+ else {
35
+ return c.text(`No templates found from path ${c.get('projectPath')}`, 500);
36
+ }
37
+ });
38
+ export default router;
39
+ //# sourceMappingURL=templates.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templates.js","sourceRoot":"","sources":["../../src/routes/templates.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IAC1B,MAAM,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAEnC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,wBAAwB,EAAE,CAAC;IACnE,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC;SAAM,CAAC;QACN,OAAO,CAAC,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;IAC7E,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,eAAe,MAAM,CAAC"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+ import { Hono } from 'hono';
13
+ const router = new Hono();
14
+ /**
15
+ * @swagger
16
+ * /api/tree
17
+ * get:
18
+ * summary: Returns everything required by treeview
19
+ * description: Returns the query result
20
+ * responses:
21
+ * 200:
22
+ * description: Object containing the query result
23
+ * 500:
24
+ * description: project_path not set or other internal error
25
+ */
26
+ router.get('/', async (c) => {
27
+ const commands = c.get('commands');
28
+ try {
29
+ await commands.calculateCmd.generate();
30
+ const tree = await commands.calculateCmd.runQuery('tree');
31
+ return c.json(tree);
32
+ }
33
+ catch (e) {
34
+ return c.text((e instanceof Error && e.message) || '', 500);
35
+ }
36
+ });
37
+ export default router;
38
+ //# sourceMappingURL=tree.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tree.js","sourceRoot":"","sources":["../../src/routes/tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;AAE1B;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE;IAC1B,MAAM,QAAQ,GAAG,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACnC,IAAI,CAAC;QACH,MAAM,QAAQ,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC;QACvC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC1D,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,eAAe,MAAM,CAAC"}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@cyberismo/backend",
3
+ "version": "0.0.3",
4
+ "description": "Express backend for Cyberismo",
5
+ "main": "dist/index.js",
6
+ "keywords": [],
7
+ "author": "",
8
+ "license": "AGPL-3.0",
9
+ "dependencies": {
10
+ "@asciidoctor/core": "^3.0.4",
11
+ "@hono/node-server": "^1.14.1",
12
+ "dotenv": "^16.5.0",
13
+ "hono": "^4.7.5",
14
+ "@cyberismo/data-handler": "0.0.3"
15
+ },
16
+ "devDependencies": {
17
+ "@cyberismo/app": "0.0.2"
18
+ },
19
+ "types": "dist/index.d.ts",
20
+ "exports": {
21
+ ".": "./dist/index.js"
22
+ },
23
+ "type": "module",
24
+ "files": [
25
+ "dist",
26
+ "src"
27
+ ],
28
+ "scripts": {
29
+ "start": "node dist/main.js",
30
+ "dev": "tsx watch src/main.ts",
31
+ "build": "tsc -p tsconfig.build.json && shx rm -rf ./dist/public && shx cp -r ../app/dist ./dist/public",
32
+ "test": "vitest run"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,121 @@
1
+ import { serve } from '@hono/node-server';
2
+ import { Hono } from 'hono';
3
+ import { cors } from 'hono/cors';
4
+ import { serveStatic } from '@hono/node-server/serve-static';
5
+ 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
+ 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) {
25
+ 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
+ }),
34
+ );
35
+
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);
46
+
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;
62
+ }
63
+
64
+ export async function startServer(
65
+ projectPath?: string,
66
+ findPort: boolean = true,
67
+ ) {
68
+ let port = parseInt(process.env.PORT || '3000', 10);
69
+
70
+ if (findPort) {
71
+ port = await findFreePort(port);
72
+ }
73
+
74
+ const app = createApp(projectPath);
75
+ // Start server
76
+ serve(
77
+ {
78
+ fetch: app.fetch,
79
+ port: Number(port),
80
+ },
81
+ (info) => {
82
+ console.log(`Running Cyberismo app on http://localhost:${info.port}`);
83
+ console.log('Press Control+C to stop.');
84
+ },
85
+ );
86
+ }
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 ADDED
@@ -0,0 +1,7 @@
1
+ import { startServer } from './index.js';
2
+ import dotenv from 'dotenv';
3
+
4
+ // Load environment variables from .env file
5
+ dotenv.config();
6
+
7
+ startServer(process.env.npm_config_project_path);
@@ -0,0 +1,43 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+
13
+ import { Context, MiddlewareHandler } from 'hono';
14
+ import { CommandManager } from '@cyberismo/data-handler';
15
+
16
+ // Extend Hono Context type to include our custom properties
17
+ declare module 'hono' {
18
+ interface ContextVariableMap {
19
+ commands: CommandManager;
20
+ projectPath: string;
21
+ }
22
+ }
23
+
24
+ export const attachCommandManager = (
25
+ projectPath?: string,
26
+ ): MiddlewareHandler => {
27
+ return async (c: Context, next) => {
28
+ if (!projectPath) {
29
+ return c.text('project_path environment variable not set.', 500);
30
+ }
31
+
32
+ try {
33
+ c.set('commands', await CommandManager.getInstance(projectPath));
34
+ c.set('projectPath', projectPath);
35
+ await next();
36
+ } catch (error) {
37
+ return c.text(
38
+ `Failed to initialize CommandManager: ${error instanceof Error ? error.message : 'Unknown error'}`,
39
+ 500,
40
+ );
41
+ }
42
+ };
43
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+
5
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
+
7
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
+
9
+ You should have received a copy of the GNU Affero General Public
10
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
+ */
12
+
13
+ import { Hono } from 'hono';
14
+
15
+ const router = new Hono();
16
+
17
+ /**
18
+ * @swagger
19
+ * /api/cardTypes:
20
+ * get:
21
+ * summary: Returns the full content of a specific card type.
22
+ * description: The key parameter is the unique identifier ("cardType") of the card type. The response includes the card type details.
23
+ * parameters:
24
+ * - name: name
25
+ * in: query
26
+ * required: true
27
+ * description: name of card type, including path (such as /project/cardTypes/page)
28
+ * responses:
29
+ * 200:
30
+ * description: Object containing card type details. See definitions.ts/CardType for the structure.
31
+ * 400:
32
+ * description: Error in reading project details.
33
+ * 500:
34
+ * description: project_path not set or other internal error
35
+ */
36
+ router.get('/', async (c) => {
37
+ const commands = c.get('commands');
38
+
39
+ // Card type name delivered in url parameter 'name' because it usually contains a path
40
+ const cardType = c.req.query('name');
41
+ if (!cardType) {
42
+ return c.text('No card type', 400);
43
+ }
44
+
45
+ const detailsResponse = await commands.showCmd.showResource(cardType);
46
+
47
+ if (detailsResponse) {
48
+ return c.json(detailsResponse);
49
+ } else {
50
+ return c.text(`No card type details found for ${cardType}`, 500);
51
+ }
52
+ });
53
+
54
+ export default router;