jitar 0.1.1 → 0.1.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.
Files changed (183) hide show
  1. package/dist/client.d.ts +1 -0
  2. package/dist/client.js +12 -0
  3. package/dist/core/Implementation.js +7 -0
  4. package/dist/core/errors/UnknownParameter.d.ts +3 -0
  5. package/dist/core/errors/UnknownParameter.js +6 -0
  6. package/dist/runtime/LocalRepository.d.ts +1 -1
  7. package/dist/runtime/LocalRepository.js +13 -4
  8. package/dist/server/configuration/RepositoryConfiguration.d.ts +1 -0
  9. package/dist/server/configuration/RepositoryConfiguration.js +7 -1
  10. package/dist/server/configuration/StandaloneConfiguration.d.ts +1 -0
  11. package/dist/server/configuration/StandaloneConfiguration.js +6 -0
  12. package/dist/server/controllers/RPCController.js +20 -3
  13. package/dist/server/utils/LocalFileManager.d.ts +1 -0
  14. package/dist/server/utils/LocalFileManager.js +19 -11
  15. package/dist/server/utils/RuntimeConfigurator.js +9 -4
  16. package/package.json +6 -2
  17. package/.eslintignore +0 -3
  18. package/.eslintrc +0 -16
  19. package/jest.config.js +0 -20
  20. package/src/client.ts +0 -18
  21. package/src/core/ErrorManager.ts +0 -18
  22. package/src/core/Implementation.ts +0 -53
  23. package/src/core/Procedure.ts +0 -98
  24. package/src/core/Segment.ts +0 -62
  25. package/src/core/SegmentBuilder.ts +0 -55
  26. package/src/core/Version.ts +0 -68
  27. package/src/core/definitions/AccessLevel.ts +0 -3
  28. package/src/core/errors/ImplementationNotFound.ts +0 -12
  29. package/src/core/errors/InvalidVersionNumber.ts +0 -12
  30. package/src/core/errors/MissingParameterValue.ts +0 -12
  31. package/src/core/errors/NotImplemented.ts +0 -12
  32. package/src/core/errors/ProcedureNotFound.ts +0 -12
  33. package/src/core/interfaces/Runner.ts +0 -7
  34. package/src/core/reflection/ParameterParser.ts +0 -74
  35. package/src/core/reflection/ReflectionHelper.ts +0 -118
  36. package/src/core/reflection/models/ReflectionField.ts +0 -22
  37. package/src/core/reflection/models/ReflectionParameter.ts +0 -18
  38. package/src/core/types/Component.ts +0 -8
  39. package/src/core/types/FlexObject.ts +0 -4
  40. package/src/core/types/Module.ts +0 -6
  41. package/src/core/types/SegmentImplementation.ts +0 -9
  42. package/src/core/types/SegmentModule.ts +0 -9
  43. package/src/core/types/SegmentProcedure.ts +0 -11
  44. package/src/core/utils/FqnBuilder.ts +0 -11
  45. package/src/hooks.ts +0 -2
  46. package/src/lib.ts +0 -7
  47. package/src/runtime/ClientId.ts +0 -18
  48. package/src/runtime/Gateway.ts +0 -18
  49. package/src/runtime/LocalGateway.ts +0 -93
  50. package/src/runtime/LocalNode.ts +0 -130
  51. package/src/runtime/LocalRepository.ts +0 -154
  52. package/src/runtime/Node.ts +0 -15
  53. package/src/runtime/NodeBalancer.ts +0 -61
  54. package/src/runtime/NodeMonitor.ts +0 -63
  55. package/src/runtime/Proxy.ts +0 -60
  56. package/src/runtime/Remote.ts +0 -157
  57. package/src/runtime/RemoteGateway.ts +0 -40
  58. package/src/runtime/RemoteNode.ts +0 -49
  59. package/src/runtime/RemoteRepository.ts +0 -60
  60. package/src/runtime/Repository.ts +0 -20
  61. package/src/runtime/Runtime.ts +0 -47
  62. package/src/runtime/caching/CacheBuilder.ts +0 -424
  63. package/src/runtime/caching/ImportRewriter.ts +0 -76
  64. package/src/runtime/caching/RemoteBuilder.ts +0 -51
  65. package/src/runtime/caching/SourceAppender.ts +0 -57
  66. package/src/runtime/caching/definitions/Keywords.ts +0 -3
  67. package/src/runtime/caching/errors/InvalidSegmentFilename.ts +0 -8
  68. package/src/runtime/caching/errors/MissingModuleExport.ts +0 -8
  69. package/src/runtime/caching/errors/SegmentFileNotLoaded.ts +0 -8
  70. package/src/runtime/caching/errors/SegmentModuleNotLoaded.ts +0 -8
  71. package/src/runtime/caching/models/ApplicationModule.ts +0 -30
  72. package/src/runtime/caching/models/Implementation.ts +0 -41
  73. package/src/runtime/caching/models/Procedure.ts +0 -26
  74. package/src/runtime/caching/models/Segment.ts +0 -28
  75. package/src/runtime/caching/models/SegmentModule.ts +0 -26
  76. package/src/runtime/caching/types/SegmentFile.ts +0 -6
  77. package/src/runtime/caching/types/SegmentImportProperties.ts +0 -9
  78. package/src/runtime/caching/types/SegmentImports.ts +0 -6
  79. package/src/runtime/errors/ClientNotFound.ts +0 -12
  80. package/src/runtime/errors/FileNotFound.ts +0 -12
  81. package/src/runtime/errors/InvalidClientId.ts +0 -12
  82. package/src/runtime/errors/InvalidJitarHooks.ts +0 -12
  83. package/src/runtime/errors/InvalidSegmentFile.ts +0 -12
  84. package/src/runtime/errors/ModuleNotLoaded.ts +0 -12
  85. package/src/runtime/errors/NoNodeAvailable.ts +0 -12
  86. package/src/runtime/errors/RepositoryNotAvaiable.ts +0 -12
  87. package/src/runtime/errors/RuntimeNotAvailable.ts +0 -12
  88. package/src/runtime/errors/SegmentNotFound.ts +0 -12
  89. package/src/runtime/hooks/dependencies.ts +0 -19
  90. package/src/runtime/hooks/runtime.ts +0 -25
  91. package/src/runtime/interfaces/FileManager.ts +0 -19
  92. package/src/runtime/interfaces/HealthCheck.ts +0 -5
  93. package/src/runtime/interfaces/ProcedureContainer.ts +0 -9
  94. package/src/runtime/models/File.ts +0 -22
  95. package/src/runtime/serialization/ArraySerializer.ts +0 -27
  96. package/src/runtime/serialization/ClassSerializer.ts +0 -133
  97. package/src/runtime/serialization/MapSerializer.ts +0 -60
  98. package/src/runtime/serialization/ObjectSerializer.ts +0 -42
  99. package/src/runtime/serialization/SetSerializer.ts +0 -42
  100. package/src/runtime/serialization/ValueSerializer.ts +0 -81
  101. package/src/runtime/serialization/errors/ClassNotFound.ts +0 -12
  102. package/src/runtime/serialization/errors/InvalidClass.ts +0 -12
  103. package/src/runtime/serialization/errors/InvalidPropertyType.ts +0 -12
  104. package/src/runtime/serialization/interfaces/Serializer.ts +0 -10
  105. package/src/runtime/serialization/types/SerializableObject.ts +0 -6
  106. package/src/runtime/serialization/types/Serialized.ts +0 -8
  107. package/src/runtime/serialization/types/SerializedClass.ts +0 -13
  108. package/src/runtime/serialization/types/SerializedMap.ts +0 -13
  109. package/src/runtime/serialization/types/SerializedObject.ts +0 -4
  110. package/src/runtime/serialization/types/SerializedSet.ts +0 -9
  111. package/src/runtime/types/JitarHooks.ts +0 -10
  112. package/src/runtime/types/ModuleImporter.ts +0 -6
  113. package/src/runtime/utils/ModuleAnalyser.ts +0 -60
  114. package/src/runtime/utils/ModuleLoader.ts +0 -51
  115. package/src/runtime/utils/UrlRewriter.ts +0 -59
  116. package/src/server/JitarServer.ts +0 -161
  117. package/src/server/configuration/GatewayConfiguration.ts +0 -9
  118. package/src/server/configuration/NodeConfiguration.ts +0 -15
  119. package/src/server/configuration/ProxyConfiguration.ts +0 -16
  120. package/src/server/configuration/RepositoryConfiguration.ts +0 -17
  121. package/src/server/configuration/RuntimeConfiguration.ts +0 -30
  122. package/src/server/configuration/ServerOptions.ts +0 -14
  123. package/src/server/configuration/StandaloneConfiguration.ts +0 -21
  124. package/src/server/controllers/AssetsController.ts +0 -59
  125. package/src/server/controllers/HealthController.ts +0 -41
  126. package/src/server/controllers/JitarController.ts +0 -18
  127. package/src/server/controllers/ModulesController.ts +0 -77
  128. package/src/server/controllers/NodesController.ts +0 -58
  129. package/src/server/controllers/ProceduresController.ts +0 -31
  130. package/src/server/controllers/ProxyController.ts +0 -38
  131. package/src/server/controllers/RPCController.ts +0 -173
  132. package/src/server/definitions/RuntimeDefaults.ts +0 -10
  133. package/src/server/errors/MissingConfigurationValue.ts +0 -8
  134. package/src/server/errors/RuntimeNotAvaiable.ts +0 -8
  135. package/src/server/errors/UnknownRuntimeMode.ts +0 -8
  136. package/src/server/models/HealthDto.ts +0 -11
  137. package/src/server/models/NodeDto.ts +0 -11
  138. package/src/server/utils/DataConverter.ts +0 -15
  139. package/src/server/utils/LocalFileManager.ts +0 -122
  140. package/src/server/utils/RuntimeConfigurationLoader.ts +0 -19
  141. package/src/server/utils/RuntimeConfigurator.ts +0 -207
  142. package/src/server/utils/ServerOptionsReader.ts +0 -17
  143. package/src/server.ts +0 -16
  144. package/test/_fixtures/core/Implementation.fixture.ts +0 -33
  145. package/test/_fixtures/core/Procedure.fixture.ts +0 -33
  146. package/test/_fixtures/core/Segment.fixture.ts +0 -36
  147. package/test/_fixtures/core/SegmentBuilder.fixture.ts +0 -45
  148. package/test/_fixtures/core/Version.fixture.ts +0 -18
  149. package/test/_fixtures/core/reflection/ReflectionHelper.fixture.ts +0 -56
  150. package/test/_fixtures/runtime/LocalGateway.fixture.ts +0 -24
  151. package/test/_fixtures/runtime/LocalNode.fixture.ts +0 -16
  152. package/test/_fixtures/runtime/LocalRepository.fixture.ts +0 -73
  153. package/test/_fixtures/runtime/NodeBalancer.fixture.ts +0 -19
  154. package/test/_fixtures/runtime/NodeMonitor.fixture.ts +0 -34
  155. package/test/_fixtures/runtime/RemoteNode.fixture.ts +0 -8
  156. package/test/_fixtures/runtime/caching/ImportRewriter.fixture.ts +0 -81
  157. package/test/_fixtures/runtime/caching/RemoteBuilder.fixture.ts +0 -50
  158. package/test/_fixtures/runtime/caching/SourceAppender.fixture.ts +0 -35
  159. package/test/_fixtures/runtime/segments.ts +0 -78
  160. package/test/_fixtures/runtime/serialization/ValueSerializer.fixture.ts +0 -134
  161. package/test/_fixtures/runtime/utils/ModuleAnaliser.fixture.ts +0 -26
  162. package/test/core/Implementation.spec.ts +0 -89
  163. package/test/core/Procedure.spec.ts +0 -108
  164. package/test/core/Segment.spec.ts +0 -74
  165. package/test/core/SegmentBuilder.spec.ts +0 -39
  166. package/test/core/Version.spec.ts +0 -103
  167. package/test/core/errors/ErrorManager.spec.ts +0 -32
  168. package/test/core/reflection/ReflectionHelper.spec.ts +0 -128
  169. package/test/core/utils/FqnBuilder.spec.ts +0 -22
  170. package/test/runtime/ClientId.spec.ts +0 -43
  171. package/test/runtime/LocalGateway.spec.ts +0 -40
  172. package/test/runtime/LocalNode.spec.ts +0 -87
  173. package/test/runtime/LocalRepository.spec.ts +0 -50
  174. package/test/runtime/NodeBalancer.spec.ts +0 -39
  175. package/test/runtime/NodeMonitor.spec.ts +0 -31
  176. package/test/runtime/RemoteNode.spec.ts +0 -43
  177. package/test/runtime/caching/ImportRewriter.spec.ts +0 -68
  178. package/test/runtime/caching/RemoteBuilder.spec.ts +0 -21
  179. package/test/runtime/caching/SourceAppender.spec.ts +0 -31
  180. package/test/runtime/serialization/ValueSerializer.spec.ts +0 -203
  181. package/test/runtime/utils/ModuleAnalyser.spec.ts +0 -54
  182. package/test/runtime/utils/UrlRewriter.spec.ts +0 -57
  183. package/tsconfig.json +0 -23
@@ -1,130 +0,0 @@
1
-
2
- import ProcedureNotFound from '../core/errors/ProcedureNotFound.js';
3
- import Segment from '../core/Segment.js';
4
- import SegmentBuilder from '../core/SegmentBuilder.js';
5
- import Version from '../core/Version.js';
6
- import Module from '../core/types/Module.js';
7
- import SegmentModule from '../core/types/SegmentModule.js';
8
-
9
- import RepositoryNotAvailable from './errors/RepositoryNotAvaiable.js';
10
- import Gateway from './Gateway.js';
11
- import LocalGateway from './LocalGateway.js';
12
- import Node from './Node.js';
13
- import Repository from './Repository.js';
14
- import ModuleLoader from './utils/ModuleLoader.js';
15
- import UrlRewriter from './utils/UrlRewriter.js';
16
-
17
- export default class LocalNode extends Node
18
- {
19
- #segments: Map<string, Segment> = new Map();
20
- #gateway?: Gateway;
21
- #repository?: Repository;
22
- #clientId = '';
23
-
24
- getProcedureNames(): string[]
25
- {
26
- const names: Set<string> = new Set();
27
-
28
- for (const segment of this.#segments.values())
29
- {
30
- // We only expose the public procedures
31
- // to protect access to private procedures
32
-
33
- const procedures = segment.getPublicProcedures();
34
-
35
- procedures.forEach(procedure => names.add(procedure.fqn));
36
- }
37
-
38
- return [...names.values()];
39
- }
40
-
41
- async loadSegment(name: string): Promise<void>
42
- {
43
- const segmentFile = `${name}.segment.local.js`;
44
- const module = await this.import(segmentFile) as SegmentModule;
45
- const segment = SegmentBuilder.build(name, module);
46
-
47
- this.addSegment(segment);
48
- }
49
-
50
- addSegment(segment: Segment): void
51
- {
52
- this.#segments.set(segment.id, segment);
53
- }
54
-
55
- getSegment(id: string): Segment | undefined
56
- {
57
- return this.#segments.get(id);
58
- }
59
-
60
- hasProcedure(fqn: string): boolean
61
- {
62
- const procedureNames = this.getProcedureNames();
63
-
64
- return procedureNames.includes(fqn);
65
- }
66
-
67
- #getProcedureSegment(fqn: string): Segment | undefined
68
- {
69
- for (const segment of this.#segments.values())
70
- {
71
- if (segment.hasProcedure(fqn))
72
- {
73
- return segment;
74
- }
75
- }
76
-
77
- return undefined;
78
- }
79
-
80
- async setGateway(gateway: Gateway): Promise<void>
81
- {
82
- if (gateway instanceof LocalGateway || this.url !== undefined)
83
- {
84
- await gateway.addNode(this);
85
- }
86
-
87
- this.#gateway = gateway;
88
- }
89
-
90
- async setRepository(repository: Repository, segmentNames: string[]): Promise<void>
91
- {
92
- this.#clientId = await repository.registerClient(segmentNames);
93
-
94
- await repository.setRuntime(this);
95
-
96
- const moduleLocation = await repository.getModuleLocation(this.#clientId);
97
-
98
- ModuleLoader.setBaseUrl(moduleLocation);
99
-
100
- this.#repository = repository;
101
- }
102
-
103
- async import(url: string, base?: string): Promise<Module>
104
- {
105
- if (this.#repository === undefined)
106
- {
107
- throw new RepositoryNotAvailable();
108
- }
109
-
110
- if (base !== undefined)
111
- {
112
- url = UrlRewriter.addBase(url, base);
113
- }
114
-
115
- return this.#repository.importModule(this.#clientId, url);
116
- }
117
-
118
- async run(fqn: string, version: Version, args: Map<string, unknown>): Promise<unknown>
119
- {
120
- const segment = this.#getProcedureSegment(fqn);
121
- const runner = segment ?? this.#gateway;
122
-
123
- if (runner === undefined)
124
- {
125
- throw new ProcedureNotFound(fqn);
126
- }
127
-
128
- return runner.run(fqn, version, args);
129
- }
130
- }
@@ -1,154 +0,0 @@
1
-
2
- import Module from '../core/types/Module.js';
3
-
4
- import ClientId from './ClientId.js';
5
- import LocalNode from './LocalNode.js';
6
- import Repository from './Repository.js';
7
- import FileManager from './interfaces/FileManager.js';
8
- import File from './models/File.js';
9
- import ModuleLoader from './utils/ModuleLoader.js';
10
-
11
- import ClientNotFound from './errors/ClientNotFound.js';
12
- import InvalidClientId from './errors/InvalidClientId.js';
13
- import InvalidSegmentFile from './errors/InvalidSegmentFile.js';
14
-
15
- import { setRuntime } from '../hooks.js';
16
-
17
- export default class LocalRepository extends Repository
18
- {
19
- #fileManager: FileManager;
20
- #segments: Map<string, string> = new Map();
21
- #clients: Map<string, string[]> = new Map();
22
-
23
- constructor(fileManager: FileManager, url?: string)
24
- {
25
- super(url);
26
-
27
- this.#fileManager = fileManager;
28
- }
29
-
30
- async loadSegment(name: string): Promise<void>
31
- {
32
- const filename = `./${name}.segment.repository.js`;
33
- const location = this.#fileManager.getAbsoluteLocation(filename);
34
- const module = await ModuleLoader.load(location);
35
- const files = module.files as string[];
36
-
37
- if (files === undefined)
38
- {
39
- throw new InvalidSegmentFile(location);
40
- }
41
-
42
- this.registerSegment(name, files);
43
- }
44
-
45
- async registerSegment(name: string, files: string[]): Promise<void>
46
- {
47
- files.forEach((file: string) => this.#segments.set(file, name));
48
- }
49
-
50
- async registerClient(segmentFilenames: string[]): Promise<string>
51
- {
52
- const clientId = ClientId.generate();
53
-
54
- this.#clients.set(clientId, segmentFilenames);
55
-
56
- return clientId;
57
- }
58
-
59
- async setRuntime(runtime: LocalNode): Promise<void>
60
- {
61
- setRuntime(runtime);
62
- }
63
-
64
- async loadAsset(filename: string): Promise<File>
65
- {
66
- return this.#fileManager.load(filename);
67
- }
68
-
69
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
70
- async getModuleLocation(clientId: string): Promise<string>
71
- {
72
- return this.#fileManager.getRootLocation();
73
- }
74
-
75
- async loadModule(clientId: string, filename: string): Promise<File>
76
- {
77
- this.#validateClientId(clientId);
78
-
79
- const segmentFilename = this.#segments.get(filename);
80
-
81
- if (segmentFilename === undefined)
82
- {
83
- return this.#getNodeModule(filename, false);
84
- }
85
-
86
- return this.#hasClientSegmentFile(clientId, segmentFilename)
87
- ? this.#getNodeModule(filename, true)
88
- : this.#getRemoteModule(filename);
89
- }
90
-
91
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
92
- async importModule(clientId: string, filename: string): Promise<Module>
93
- {
94
- // This function loads the original module file containing the
95
- // original imports to prevent import issues while loading the
96
- // module in the local repository.
97
-
98
- const location = this.#fileManager.getAbsoluteLocation(filename);
99
-
100
- return ModuleLoader.import(location);
101
- }
102
-
103
- #validateClientId(clientId: string): void
104
- {
105
- if (ClientId.validate(clientId) === false)
106
- {
107
- throw new InvalidClientId(clientId);
108
- }
109
-
110
- if (this.#clients.has(clientId) === false)
111
- {
112
- throw new ClientNotFound(clientId);
113
- }
114
- }
115
-
116
- #hasClientSegmentFile(clientId: string, segmentFilename: string): boolean
117
- {
118
- const clientSegmentFiles = this.#clients.get(clientId);
119
-
120
- if (clientSegmentFiles === undefined)
121
- {
122
- throw new ClientNotFound(clientId);
123
- }
124
-
125
- return clientSegmentFiles.some(clientSegmentFilename => segmentFilename.endsWith(clientSegmentFilename));
126
- }
127
-
128
- async #getNodeModule(filename: string, isSegmented: boolean): Promise<File>
129
- {
130
- // This function loads the node module file containing the rewritten
131
- // imports to prevent import issues while loading the module from
132
- // a remote repository.
133
-
134
- if (isSegmented)
135
- {
136
- filename = filename.replace('.js', '.local.js');
137
- }
138
-
139
- const file = await this.loadAsset(filename);
140
- const code = file.content.toString();
141
-
142
- return new File(filename, 'application/javascript', code);
143
- }
144
-
145
- async #getRemoteModule(filename: string): Promise<File>
146
- {
147
- // This function loads the remote module file containing the rewritten
148
- // implementation for each function to execute them on another node.
149
-
150
- const remoteFilename = filename.replace('.js', '.remote.js');
151
-
152
- return this.loadAsset(remoteFilename);
153
- }
154
- }
@@ -1,15 +0,0 @@
1
-
2
- import Version from '../core/Version.js';
3
-
4
- import ProcedureContainer from './interfaces/ProcedureContainer.js';
5
-
6
- import Runtime from './Runtime.js';
7
-
8
- export default abstract class Node extends Runtime implements ProcedureContainer
9
- {
10
- abstract getProcedureNames(): string[];
11
-
12
- abstract hasProcedure(name: string): boolean;
13
-
14
- abstract run(name: string, version: Version, args: Map<string, unknown>): Promise<unknown>;
15
- }
@@ -1,61 +0,0 @@
1
-
2
- import Runner from '../core/interfaces/Runner.js';
3
- import Version from '../core/Version.js';
4
-
5
- import NoNodeAvailable from './errors/NoNodeAvailable.js';
6
- import Node from './Node.js';
7
-
8
- export default class NodeBalancer implements Runner
9
- {
10
- #nodes: Node[] = [];
11
- #currentIndex = 0;
12
-
13
- addNode(node: Node): void
14
- {
15
- if (this.#nodes.includes(node))
16
- {
17
- return;
18
- }
19
-
20
- this.#nodes.push(node);
21
- }
22
-
23
- removeNode(node: Node): void
24
- {
25
- const index = this.#nodes.indexOf(node);
26
-
27
- if (index === -1)
28
- {
29
- return;
30
- }
31
-
32
- this.#nodes.splice(index, 1);
33
- }
34
-
35
- getNextNode(): Node | undefined
36
- {
37
- if (this.#nodes.length === 0)
38
- {
39
- return;
40
- }
41
-
42
- if (this.#currentIndex >= this.#nodes.length)
43
- {
44
- this.#currentIndex = 0;
45
- }
46
-
47
- return this.#nodes[this.#currentIndex++];
48
- }
49
-
50
- async run(fqn: string, version: Version, args: Map<string, unknown>): Promise<unknown>
51
- {
52
- const node = this.getNextNode();
53
-
54
- if (node === undefined)
55
- {
56
- throw new NoNodeAvailable(fqn);
57
- }
58
-
59
- return node.run(fqn, version, args);
60
- }
61
- }
@@ -1,63 +0,0 @@
1
-
2
- import LocalGateway from './LocalGateway.js';
3
- import Node from './Node.js';
4
-
5
- const DEFAULT_FREQUENCY = 5000;
6
-
7
- export default class NodeMonitor
8
- {
9
- #gateway: LocalGateway;
10
- #frequency: number;
11
- #interval: ReturnType<typeof setInterval> | null = null;
12
-
13
- constructor(gateway: LocalGateway, frequency: number = DEFAULT_FREQUENCY)
14
- {
15
- this.#gateway = gateway;
16
- this.#frequency = frequency;
17
- }
18
-
19
- start(): void
20
- {
21
- this.#interval = setInterval(async () => await this.#monitor(), this.#frequency);
22
- }
23
-
24
- stop(): void
25
- {
26
- if (this.#interval === null)
27
- {
28
- return;
29
- }
30
-
31
- clearInterval(this.#interval);
32
- }
33
-
34
- async #monitor(): Promise<void>
35
- {
36
- const nodes = this.#gateway.nodes;
37
- const promises = nodes.map(async (node: Node) => await this.#monitorNode(node));
38
-
39
- await Promise.all(promises);
40
- }
41
-
42
- async #monitorNode(node: Node): Promise<void>
43
- {
44
- const available = await this.#checkNodeAvailable(node);
45
-
46
- if (available === false)
47
- {
48
- this.#gateway.removeNode(node);
49
- }
50
- }
51
-
52
- async #checkNodeAvailable(node: Node): Promise<boolean>
53
- {
54
- try
55
- {
56
- return await node.isHealthy();
57
- }
58
- catch (error)
59
- {
60
- return false;
61
- }
62
- }
63
- }
@@ -1,60 +0,0 @@
1
-
2
- import Version from '../core/Version.js';
3
-
4
- import ProcedureContainer from './interfaces/ProcedureContainer.js';
5
-
6
- import Gateway from './Gateway.js';
7
- import File from './models/File.js';
8
- import Node from './Node.js';
9
- import Repository from './Repository.js';
10
- import Runtime from './Runtime.js';
11
-
12
- export default class Proxy extends Runtime implements ProcedureContainer
13
- {
14
- #repository: Repository;
15
- #runner: Gateway | Node;
16
-
17
- constructor(repository: Repository, runner: Gateway | Node, url?: string)
18
- {
19
- super(url);
20
-
21
- this.#repository = repository;
22
- this.#runner = runner;
23
- }
24
-
25
- get repository() { return this.#repository; }
26
-
27
- get runner() { return this.#runner; }
28
-
29
- getProcedureNames(): string[]
30
- {
31
- return this.#runner.getProcedureNames();
32
- }
33
-
34
- hasProcedure(fqn: string): boolean
35
- {
36
- const procedureNames = this.getProcedureNames();
37
-
38
- return procedureNames.includes(fqn);
39
- }
40
-
41
- async loadAsset(filename: string): Promise<File>
42
- {
43
- return this.#repository.loadAsset(filename);
44
- }
45
-
46
- async registerClient(segmentFiles: string[]): Promise<string>
47
- {
48
- return this.#repository.registerClient(segmentFiles);
49
- }
50
-
51
- async loadModule(clientId: string, filename: string): Promise<File>
52
- {
53
- return this.#repository.loadModule(clientId, filename);
54
- }
55
-
56
- async run(name: string, version: Version, args: Map<string, unknown>): Promise<unknown>
57
- {
58
- return this.#runner.run(name, version, args);
59
- }
60
- }
@@ -1,157 +0,0 @@
1
-
2
- import Module from '../core/types/Module.js';
3
- import Version from '../core/Version.js';
4
-
5
- import File from './models/File.js';
6
- import Node from './Node.js';
7
- import ValueSerializer from './serialization/ValueSerializer.js';
8
- import ModuleLoader from './utils/ModuleLoader.js';
9
-
10
- export default class Remote
11
- {
12
- #url: string;
13
- #useSerializer: boolean;
14
-
15
- constructor(url: string, useSerializer: boolean)
16
- {
17
- this.#url = url;
18
- this.#useSerializer = useSerializer;
19
- }
20
-
21
- async registerClient(segmentFiles: string[]): Promise<string>
22
- {
23
- const url = `${this.#url}/modules`;
24
- const options =
25
- {
26
- method: 'POST',
27
- headers: { 'Content-Type': 'application/json' },
28
- body: JSON.stringify(segmentFiles)
29
- };
30
-
31
- const response = await this.#callRemote(url, options, 200);
32
- const clientId = await response.text();
33
-
34
- return clientId;
35
- }
36
-
37
- async loadFile(filename: string): Promise<File>
38
- {
39
- const url = `${this.#url}/${filename}`;
40
- const options = { method: 'GET' };
41
-
42
- const response = await this.#callRemote(url, options, 200);
43
- const type = response.headers.get('Content-Type') || 'application/octet-stream';
44
- const content = await response.text();
45
-
46
- return new File(filename, type, content);
47
- }
48
-
49
- async importFile(filename: string): Promise<Module>
50
- {
51
- const url = `${this.#url}/${filename}`;
52
-
53
- return ModuleLoader.load(url);
54
- }
55
-
56
- async isHealthy(): Promise<boolean>
57
- {
58
- const url = `${this.#url}/health/status`;
59
- const options = { method: 'GET' };
60
-
61
- const response = await this.#callRemote(url, options, 200);
62
- const healthy = await response.text();
63
-
64
- return Boolean(healthy);
65
- }
66
-
67
- async getHealth(): Promise<Map<string, boolean>>
68
- {
69
- const url = `${this.#url}/health`;
70
- const options = { method: 'GET' };
71
-
72
- const response = await this.#callRemote(url, options, 200);
73
- const health = await response.json();
74
-
75
- return new Map(Object.entries(health));
76
- }
77
-
78
- async addNode(node: Node): Promise<void>
79
- {
80
- const url = `${this.#url}/nodes`;
81
- const body =
82
- {
83
- url: node.url,
84
- procedureNames: node.getProcedureNames()
85
- }
86
- const options =
87
- {
88
- method: 'POST',
89
- headers: { 'Content-Type': 'application/json' },
90
- body: JSON.stringify(body)
91
- }
92
-
93
- await this.#callRemote(url, options, 201);
94
- }
95
-
96
- async run(fqn: string, version: Version, args: Map<string, unknown>): Promise<unknown>
97
- {
98
- const versionString = version.toString();
99
- const argsObject = Object.fromEntries(args);
100
-
101
- const url = `${this.#url}/rpc/${fqn}?version=${versionString}&serialize=true`;
102
- const options =
103
- {
104
- method: 'POST',
105
- headers: { 'Content-Type': 'application/json' },
106
- body: this.#createRequestBody(argsObject, this.#useSerializer)
107
- };
108
-
109
- const response = await this.#callRemote(url, options, 200);
110
-
111
- return this.#createResponseResult(response, this.#useSerializer);
112
- }
113
-
114
- async #callRemote(url: string, options: object, expectedStatus: number): Promise<Response>
115
- {
116
- const response = await fetch(url, options);
117
-
118
- if (response.status !== expectedStatus)
119
- {
120
- const error = await this.#createResponseResult(response, true);
121
-
122
- throw error;
123
- }
124
-
125
- return response;
126
- }
127
-
128
- #createRequestBody(body: unknown, serialize: boolean): string
129
- {
130
- const data = serialize
131
- ? ValueSerializer.serialize(body)
132
- : body;
133
-
134
- return JSON.stringify(data);
135
- }
136
-
137
- async #createResponseResult(response: Response, serialize: boolean): Promise<unknown>
138
- {
139
- const result = await this.#getResponseResult(response);
140
-
141
- return serialize
142
- ? ValueSerializer.deserialize(result)
143
- : result;
144
- }
145
-
146
- async #getResponseResult(response: Response): Promise<unknown>
147
- {
148
- const contentType = response.headers.get('Content-Type');
149
-
150
- if (contentType !== null && contentType.includes('json'))
151
- {
152
- return response.json();
153
- }
154
-
155
- return response.text();
156
- }
157
- }
@@ -1,40 +0,0 @@
1
-
2
- import NotImplemented from '../core/errors/NotImplemented.js';
3
- import Version from '../core/Version.js';
4
-
5
- import Gateway from './Gateway.js';
6
- import Node from './Node.js';
7
- import Remote from './Remote.js';
8
-
9
- export default class RemoteGateway extends Gateway
10
- {
11
- #remote: Remote;
12
-
13
- constructor(url: string)
14
- {
15
- super(url);
16
-
17
- this.#remote = new Remote(url, true);
18
- }
19
-
20
- getProcedureNames(): string[]
21
- {
22
- throw new NotImplemented();
23
- }
24
-
25
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
26
- hasProcedure(name: string): boolean
27
- {
28
- throw new NotImplemented();
29
- }
30
-
31
- async addNode(node: Node): Promise<void>
32
- {
33
- return this.#remote.addNode(node);
34
- }
35
-
36
- async run(fqn: string, version: Version, args: Map<string, unknown>): Promise<unknown>
37
- {
38
- return this.#remote.run(fqn, version, args);
39
- }
40
- }