Skip to content

Commit 09bef73

Browse files
authored
feat: make node deployable (#92)
* chore: port main.ts from id proxy * build: add server script * chore: use variables to determine host and port * test: test createVariables
1 parent 069866e commit 09bef73

File tree

6 files changed

+284
-65
lines changed

6 files changed

+284
-65
lines changed

packages/semcom-node/bin/server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
const { launch, createVariables } = require('../dist/main.js');
4+
5+
const variabes = createVariables(process.argv);
6+
7+
launch(variabes);

packages/semcom-node/config/presets/server.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,35 @@
44
"https://linkedsoftwaredependencies.org/bundles/npm/@digita-ai/handlersjs-http/^0.0.0/components/context.jsonld"
55
],
66
"@graph": [
7+
{
8+
"@id": "urn:semcom-node:variables:customConfigPath",
9+
"@type": "Variable"
10+
},
11+
{
12+
"@id": "urn:semcom-node:variables:mainModulePath",
13+
"@type": "Variable"
14+
},
15+
{
16+
"@id": "urn:semcom-node:variables:schema",
17+
"@type": "Variable"
18+
},
19+
{
20+
"@id": "urn:semcom-node:variables:host",
21+
"@type": "Variable"
22+
},
23+
{
24+
"@id": "urn:semcom-node:variables:port",
25+
"@type": "Variable"
26+
},
727
{
828
"@id": "urn:semcom-node:default:NodeHttpServer",
929
"@type": "NodeHttpServer",
10-
"NodeHttpServer:_port": "3000",
11-
"NodeHttpServer:_host": "localhost",
30+
"NodeHttpServer:_port": {
31+
"@id": "urn:semcom-node:variables:port"
32+
},
33+
"NodeHttpServer:_host": {
34+
"@id": "urn:semcom-node:variables:host"
35+
},
1236
"NodeHttpServer:_nodeHttpStreamsHandler": {
1337
"@id": "urn:semcom-node:default:NodeHttpRequestResponseHandler",
1438
"@type": "NodeHttpRequestResponseHandler",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ComponentsManager } from 'componentsjs';
2+
import { createVariables, launch } from './main';
3+
4+
describe('launch', () => {
5+
6+
afterEach(() => {
7+
8+
jest.clearAllMocks();
9+
10+
});
11+
12+
// it('should call ComponentManager.build', async () => {
13+
14+
// const buildSpy = jest.spyOn(ComponentsManager, 'build');
15+
16+
// await launch(createVariables([ ]));
17+
18+
// expect(buildSpy).toHaveBeenCalled();
19+
20+
// });
21+
22+
});
23+
24+
describe('createVariables', () => {
25+
26+
it('should create default variables when no arguments are given', (() => {
27+
28+
const variables = createVariables([ ]);
29+
30+
expect(variables).toEqual({
31+
'urn:semcom-node:variables:customConfigPath': undefined,
32+
'urn:semcom-node:variables:mainModulePath': undefined,
33+
'urn:semcom-node:variables:schema': undefined,
34+
'urn:semcom-node:variables:host': 'localhost',
35+
'urn:semcom-node:variables:port': '3000',
36+
});
37+
38+
}));
39+
40+
it('should create variables based on given arguments', (() => {
41+
42+
const variables = createVariables([
43+
'bin',
44+
'bin',
45+
'-c',
46+
'bla',
47+
]);
48+
49+
expect(variables).toEqual({
50+
'urn:semcom-node:variables:customConfigPath': 'bla',
51+
'urn:semcom-node:variables:mainModulePath': undefined,
52+
'urn:semcom-node:variables:schema': undefined,
53+
'urn:semcom-node:variables:host': 'localhost',
54+
'urn:semcom-node:variables:port': '3000',
55+
});
56+
57+
}));
58+
59+
});

packages/semcom-node/lib/main.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
11
import * as path from 'path';
22
import { ComponentsManager } from 'componentsjs';
33
import { NodeHttpServer } from '@digita-ai/handlersjs-http';
4+
import yargs from 'yargs';
5+
import { hideBin } from 'yargs/helpers';
46

5-
const launch = async () => {
7+
/**
8+
* Instantiates a server from the passed configuration and starts it.
9+
*
10+
* @param {Record<string, any>} variables - a record of values for the variables left open in the configuration.
11+
*/
12+
export const launch: (variables: Record<string, any>) => Promise<void> = async (variables: Record<string, any>) => {
613

7-
const mainModulePath = path.join(__dirname, '../');
8-
const configPath = path.join(__dirname, '../config/config-default.json');
14+
const mainModulePath = variables['urn:semcom-node:variables:mainModulePath']
15+
? path.join(process.cwd(), variables['urn:semcom-node:variables:mainModulePath'])
16+
: path.join(__dirname, '../');
17+
18+
const configPath = variables['urn:semcom-node:variables:customConfigPath']
19+
? path.join(process.cwd(), variables['urn:semcom-node:variables:customConfigPath'])
20+
: path.join(__dirname, '../config/config-default.json');
921

1022
const manager = await ComponentsManager.build({
1123
mainModulePath,
24+
logLevel: 'silly',
1225
});
1326

1427
await manager.configRegistry.register(configPath);
1528

16-
const server: NodeHttpServer = await manager.instantiate(
17-
'urn:semcom-node:default:NodeHttpServer',
18-
);
19-
29+
const server: NodeHttpServer = await manager.instantiate('urn:semcom-node:default:NodeHttpServer', { variables });
2030
server.start();
2131

2232
};
2333

24-
launch();
34+
export const createVariables = (args: string[]): Record<string, any> => {
35+
36+
const { argv: params } = yargs(hideBin(args))
37+
.usage('node ./dist/main.js [args]')
38+
.options({
39+
config: { type: 'string', alias: 'c' },
40+
port: { type: 'number', alias: 'p' },
41+
host: { type: 'string', alias: 'h' },
42+
schema: { type: 'string', alias: 's' },
43+
mainModulePath: { type: 'string', alias: 'm' },
44+
})
45+
.help();
46+
47+
return {
48+
'urn:semcom-node:variables:customConfigPath': params.config,
49+
'urn:semcom-node:variables:mainModulePath': params.mainModulePath,
50+
'urn:semcom-node:variables:schema': params.schema,
51+
'urn:semcom-node:variables:host': params.host ?? 'localhost',
52+
'urn:semcom-node:variables:port': params.port ?? '3000',
53+
};
54+
55+
};

0 commit comments

Comments
 (0)