@twin.org/modules 0.0.1-next.16
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.
- package/LICENSE +201 -0
- package/README.md +21 -0
- package/dist/cjs/index.cjs +158 -0
- package/dist/esm/index.mjs +156 -0
- package/dist/types/helpers/moduleHelper.d.ts +43 -0
- package/dist/types/index.d.ts +1 -0
- package/docs/changelog.md +5 -0
- package/docs/examples.md +1 -0
- package/docs/reference/classes/ModuleHelper.md +157 -0
- package/docs/reference/index.md +5 -0
- package/locales/en.json +11 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 IOTA Stiftung.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# TWIN Framework Modules
|
|
2
|
+
|
|
3
|
+
Helper classes for loading and executing from modules.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @twin.org/modules
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
|
|
18
|
+
|
|
19
|
+
## Changelog
|
|
20
|
+
|
|
21
|
+
The changes between each version can be found in [docs/changelog.md](docs/changelog.md)
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_worker_threads = require('node:worker_threads');
|
|
4
|
+
var core = require('@twin.org/core');
|
|
5
|
+
|
|
6
|
+
// Copyright 2024 IOTA Stiftung.
|
|
7
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
8
|
+
/* eslint-disable promise/prefer-await-to-then */
|
|
9
|
+
/**
|
|
10
|
+
* Helper functions for modules.
|
|
11
|
+
*/
|
|
12
|
+
class ModuleHelper {
|
|
13
|
+
/**
|
|
14
|
+
* Runtime name for the class.
|
|
15
|
+
*/
|
|
16
|
+
static CLASS_NAME = "ModuleHelper";
|
|
17
|
+
/**
|
|
18
|
+
* Get the module entry.
|
|
19
|
+
* @param module The module.
|
|
20
|
+
* @param entry The entry to get from the module.
|
|
21
|
+
* @returns The entry from the module.
|
|
22
|
+
* @throws GeneralError if getting the module entry failed.
|
|
23
|
+
*/
|
|
24
|
+
static async getModuleEntry(module, entry) {
|
|
25
|
+
let moduleInstance;
|
|
26
|
+
try {
|
|
27
|
+
moduleInstance = await import(module);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
|
|
31
|
+
module,
|
|
32
|
+
entry
|
|
33
|
+
}, core.BaseError.fromError(err));
|
|
34
|
+
}
|
|
35
|
+
const moduleEntry = moduleInstance[entry];
|
|
36
|
+
if (core.Is.empty(moduleEntry)) {
|
|
37
|
+
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
|
|
38
|
+
module,
|
|
39
|
+
entry
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return moduleEntry;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the method from a module.
|
|
46
|
+
* @param module The module.
|
|
47
|
+
* @param method The method to execute from the module, use dot notation to get a static class method.
|
|
48
|
+
* @returns The result of the method execution.
|
|
49
|
+
* @throws GeneralError if executing the module entry failed.
|
|
50
|
+
*/
|
|
51
|
+
static async getModuleMethod(module, method) {
|
|
52
|
+
const methodParts = method.split(".");
|
|
53
|
+
if (methodParts.length === 2) {
|
|
54
|
+
const moduleEntry = await ModuleHelper.getModuleEntry(module, methodParts[0]);
|
|
55
|
+
if (core.Is.function(moduleEntry[methodParts[1]])) {
|
|
56
|
+
return moduleEntry[methodParts[1]];
|
|
57
|
+
}
|
|
58
|
+
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "notFunction", {
|
|
59
|
+
module,
|
|
60
|
+
entry: method
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
const moduleEntry = await ModuleHelper.getModuleEntry(module, methodParts[0]);
|
|
64
|
+
if (core.Is.function(moduleEntry)) {
|
|
65
|
+
return moduleEntry;
|
|
66
|
+
}
|
|
67
|
+
throw new core.GeneralError(ModuleHelper.CLASS_NAME, "notFunction", {
|
|
68
|
+
module,
|
|
69
|
+
entry: method
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Execute the method in the module.
|
|
74
|
+
* @param module The module.
|
|
75
|
+
* @param method The method to execute from the module.
|
|
76
|
+
* @param args The arguments to pass to the method.
|
|
77
|
+
* @returns The result of the method execution.
|
|
78
|
+
* @throws GeneralError if executing the module entry failed.
|
|
79
|
+
*/
|
|
80
|
+
static async execModuleMethod(module, method, args) {
|
|
81
|
+
const moduleMethod = await ModuleHelper.getModuleMethod(module, method);
|
|
82
|
+
return moduleMethod(...(args ?? []));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Execute the method in the module in a thread.
|
|
86
|
+
* @param module The module.
|
|
87
|
+
* @param method The method to execute from the module.
|
|
88
|
+
* @param args The arguments to pass to the method.
|
|
89
|
+
* @returns The result of the method execution.
|
|
90
|
+
* @throws GeneralError if executing the module entry failed.
|
|
91
|
+
*/
|
|
92
|
+
static async execModuleMethodThread(module, method, args) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const worker = new node_worker_threads.Worker(`
|
|
95
|
+
const { workerData, parentPort } = require('worker_threads');
|
|
96
|
+
|
|
97
|
+
function rejectError(type, innerError) {
|
|
98
|
+
parentPort.postMessage({ errorType: type, innerError });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function resolveResult(result) {
|
|
102
|
+
Promise.resolve(result).then(res => parentPort.postMessage({ result: res }));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const { module, method, args } = workerData;
|
|
106
|
+
|
|
107
|
+
import(module)
|
|
108
|
+
.then(moduleInstance => {
|
|
109
|
+
const methodParts = method.split(".");
|
|
110
|
+
const moduleEntry = moduleInstance[methodParts[0]];
|
|
111
|
+
|
|
112
|
+
if (moduleEntry === undefined) {
|
|
113
|
+
rejectError("entryNotFound");
|
|
114
|
+
} else if (methodParts.length === 2) {
|
|
115
|
+
const moduleMethod = moduleEntry[methodParts[1]];
|
|
116
|
+
if (typeof moduleMethod === "function") {
|
|
117
|
+
resolveResult(moduleMethod(...(args ?? [])));
|
|
118
|
+
} else {
|
|
119
|
+
rejectError("notFunction");
|
|
120
|
+
}
|
|
121
|
+
} else if (typeof moduleEntry === "function") {
|
|
122
|
+
resolveResult(moduleEntry(...(args ?? [])));
|
|
123
|
+
} else {
|
|
124
|
+
rejectError("notFunction");
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
.catch(err => {
|
|
128
|
+
rejectError("moduleNotFound", err);
|
|
129
|
+
});
|
|
130
|
+
`, { eval: true, workerData: { module, method, args: args ?? [] } });
|
|
131
|
+
worker.on("message", msg => {
|
|
132
|
+
if (core.Is.stringValue(msg.errorType)) {
|
|
133
|
+
reject(new core.GeneralError(ModuleHelper.CLASS_NAME, msg.errorType, { module, entry: method }, msg.innerError));
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
resolve(msg.result);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
worker.on("error", err => {
|
|
140
|
+
reject(new core.GeneralError(ModuleHelper.CLASS_NAME, "workerException", {
|
|
141
|
+
module,
|
|
142
|
+
entry: method
|
|
143
|
+
}, err));
|
|
144
|
+
});
|
|
145
|
+
worker.on("exit", code => {
|
|
146
|
+
if (code === 1) {
|
|
147
|
+
reject(new core.GeneralError(ModuleHelper.CLASS_NAME, "workerFailed", {
|
|
148
|
+
module,
|
|
149
|
+
entry: method,
|
|
150
|
+
exitCode: code
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
exports.ModuleHelper = ModuleHelper;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Worker } from 'node:worker_threads';
|
|
2
|
+
import { GeneralError, BaseError, Is } from '@twin.org/core';
|
|
3
|
+
|
|
4
|
+
// Copyright 2024 IOTA Stiftung.
|
|
5
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
6
|
+
/* eslint-disable promise/prefer-await-to-then */
|
|
7
|
+
/**
|
|
8
|
+
* Helper functions for modules.
|
|
9
|
+
*/
|
|
10
|
+
class ModuleHelper {
|
|
11
|
+
/**
|
|
12
|
+
* Runtime name for the class.
|
|
13
|
+
*/
|
|
14
|
+
static CLASS_NAME = "ModuleHelper";
|
|
15
|
+
/**
|
|
16
|
+
* Get the module entry.
|
|
17
|
+
* @param module The module.
|
|
18
|
+
* @param entry The entry to get from the module.
|
|
19
|
+
* @returns The entry from the module.
|
|
20
|
+
* @throws GeneralError if getting the module entry failed.
|
|
21
|
+
*/
|
|
22
|
+
static async getModuleEntry(module, entry) {
|
|
23
|
+
let moduleInstance;
|
|
24
|
+
try {
|
|
25
|
+
moduleInstance = await import(module);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new GeneralError(ModuleHelper.CLASS_NAME, "moduleNotFound", {
|
|
29
|
+
module,
|
|
30
|
+
entry
|
|
31
|
+
}, BaseError.fromError(err));
|
|
32
|
+
}
|
|
33
|
+
const moduleEntry = moduleInstance[entry];
|
|
34
|
+
if (Is.empty(moduleEntry)) {
|
|
35
|
+
throw new GeneralError(ModuleHelper.CLASS_NAME, "entryNotFound", {
|
|
36
|
+
module,
|
|
37
|
+
entry
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return moduleEntry;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the method from a module.
|
|
44
|
+
* @param module The module.
|
|
45
|
+
* @param method The method to execute from the module, use dot notation to get a static class method.
|
|
46
|
+
* @returns The result of the method execution.
|
|
47
|
+
* @throws GeneralError if executing the module entry failed.
|
|
48
|
+
*/
|
|
49
|
+
static async getModuleMethod(module, method) {
|
|
50
|
+
const methodParts = method.split(".");
|
|
51
|
+
if (methodParts.length === 2) {
|
|
52
|
+
const moduleEntry = await ModuleHelper.getModuleEntry(module, methodParts[0]);
|
|
53
|
+
if (Is.function(moduleEntry[methodParts[1]])) {
|
|
54
|
+
return moduleEntry[methodParts[1]];
|
|
55
|
+
}
|
|
56
|
+
throw new GeneralError(ModuleHelper.CLASS_NAME, "notFunction", {
|
|
57
|
+
module,
|
|
58
|
+
entry: method
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const moduleEntry = await ModuleHelper.getModuleEntry(module, methodParts[0]);
|
|
62
|
+
if (Is.function(moduleEntry)) {
|
|
63
|
+
return moduleEntry;
|
|
64
|
+
}
|
|
65
|
+
throw new GeneralError(ModuleHelper.CLASS_NAME, "notFunction", {
|
|
66
|
+
module,
|
|
67
|
+
entry: method
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Execute the method in the module.
|
|
72
|
+
* @param module The module.
|
|
73
|
+
* @param method The method to execute from the module.
|
|
74
|
+
* @param args The arguments to pass to the method.
|
|
75
|
+
* @returns The result of the method execution.
|
|
76
|
+
* @throws GeneralError if executing the module entry failed.
|
|
77
|
+
*/
|
|
78
|
+
static async execModuleMethod(module, method, args) {
|
|
79
|
+
const moduleMethod = await ModuleHelper.getModuleMethod(module, method);
|
|
80
|
+
return moduleMethod(...(args ?? []));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Execute the method in the module in a thread.
|
|
84
|
+
* @param module The module.
|
|
85
|
+
* @param method The method to execute from the module.
|
|
86
|
+
* @param args The arguments to pass to the method.
|
|
87
|
+
* @returns The result of the method execution.
|
|
88
|
+
* @throws GeneralError if executing the module entry failed.
|
|
89
|
+
*/
|
|
90
|
+
static async execModuleMethodThread(module, method, args) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const worker = new Worker(`
|
|
93
|
+
const { workerData, parentPort } = require('worker_threads');
|
|
94
|
+
|
|
95
|
+
function rejectError(type, innerError) {
|
|
96
|
+
parentPort.postMessage({ errorType: type, innerError });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function resolveResult(result) {
|
|
100
|
+
Promise.resolve(result).then(res => parentPort.postMessage({ result: res }));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const { module, method, args } = workerData;
|
|
104
|
+
|
|
105
|
+
import(module)
|
|
106
|
+
.then(moduleInstance => {
|
|
107
|
+
const methodParts = method.split(".");
|
|
108
|
+
const moduleEntry = moduleInstance[methodParts[0]];
|
|
109
|
+
|
|
110
|
+
if (moduleEntry === undefined) {
|
|
111
|
+
rejectError("entryNotFound");
|
|
112
|
+
} else if (methodParts.length === 2) {
|
|
113
|
+
const moduleMethod = moduleEntry[methodParts[1]];
|
|
114
|
+
if (typeof moduleMethod === "function") {
|
|
115
|
+
resolveResult(moduleMethod(...(args ?? [])));
|
|
116
|
+
} else {
|
|
117
|
+
rejectError("notFunction");
|
|
118
|
+
}
|
|
119
|
+
} else if (typeof moduleEntry === "function") {
|
|
120
|
+
resolveResult(moduleEntry(...(args ?? [])));
|
|
121
|
+
} else {
|
|
122
|
+
rejectError("notFunction");
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch(err => {
|
|
126
|
+
rejectError("moduleNotFound", err);
|
|
127
|
+
});
|
|
128
|
+
`, { eval: true, workerData: { module, method, args: args ?? [] } });
|
|
129
|
+
worker.on("message", msg => {
|
|
130
|
+
if (Is.stringValue(msg.errorType)) {
|
|
131
|
+
reject(new GeneralError(ModuleHelper.CLASS_NAME, msg.errorType, { module, entry: method }, msg.innerError));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
resolve(msg.result);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
worker.on("error", err => {
|
|
138
|
+
reject(new GeneralError(ModuleHelper.CLASS_NAME, "workerException", {
|
|
139
|
+
module,
|
|
140
|
+
entry: method
|
|
141
|
+
}, err));
|
|
142
|
+
});
|
|
143
|
+
worker.on("exit", code => {
|
|
144
|
+
if (code === 1) {
|
|
145
|
+
reject(new GeneralError(ModuleHelper.CLASS_NAME, "workerFailed", {
|
|
146
|
+
module,
|
|
147
|
+
entry: method,
|
|
148
|
+
exitCode: code
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export { ModuleHelper };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper functions for modules.
|
|
3
|
+
*/
|
|
4
|
+
export declare class ModuleHelper {
|
|
5
|
+
/**
|
|
6
|
+
* Runtime name for the class.
|
|
7
|
+
*/
|
|
8
|
+
static readonly CLASS_NAME: string;
|
|
9
|
+
/**
|
|
10
|
+
* Get the module entry.
|
|
11
|
+
* @param module The module.
|
|
12
|
+
* @param entry The entry to get from the module.
|
|
13
|
+
* @returns The entry from the module.
|
|
14
|
+
* @throws GeneralError if getting the module entry failed.
|
|
15
|
+
*/
|
|
16
|
+
static getModuleEntry<T>(module: string, entry: string): Promise<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Get the method from a module.
|
|
19
|
+
* @param module The module.
|
|
20
|
+
* @param method The method to execute from the module, use dot notation to get a static class method.
|
|
21
|
+
* @returns The result of the method execution.
|
|
22
|
+
* @throws GeneralError if executing the module entry failed.
|
|
23
|
+
*/
|
|
24
|
+
static getModuleMethod<T>(module: string, method: string): Promise<(...args: unknown[]) => T>;
|
|
25
|
+
/**
|
|
26
|
+
* Execute the method in the module.
|
|
27
|
+
* @param module The module.
|
|
28
|
+
* @param method The method to execute from the module.
|
|
29
|
+
* @param args The arguments to pass to the method.
|
|
30
|
+
* @returns The result of the method execution.
|
|
31
|
+
* @throws GeneralError if executing the module entry failed.
|
|
32
|
+
*/
|
|
33
|
+
static execModuleMethod<T>(module: string, method: string, args?: unknown[]): Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Execute the method in the module in a thread.
|
|
36
|
+
* @param module The module.
|
|
37
|
+
* @param method The method to execute from the module.
|
|
38
|
+
* @param args The arguments to pass to the method.
|
|
39
|
+
* @returns The result of the method execution.
|
|
40
|
+
* @throws GeneralError if executing the module entry failed.
|
|
41
|
+
*/
|
|
42
|
+
static execModuleMethodThread<T>(module: string, method: string, args?: unknown[]): Promise<T>;
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./helpers/moduleHelper";
|
package/docs/examples.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @twin.org/modules - Examples
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# Class: ModuleHelper
|
|
2
|
+
|
|
3
|
+
Helper functions for modules.
|
|
4
|
+
|
|
5
|
+
## Constructors
|
|
6
|
+
|
|
7
|
+
### new ModuleHelper()
|
|
8
|
+
|
|
9
|
+
> **new ModuleHelper**(): [`ModuleHelper`](ModuleHelper.md)
|
|
10
|
+
|
|
11
|
+
#### Returns
|
|
12
|
+
|
|
13
|
+
[`ModuleHelper`](ModuleHelper.md)
|
|
14
|
+
|
|
15
|
+
## Properties
|
|
16
|
+
|
|
17
|
+
### CLASS\_NAME
|
|
18
|
+
|
|
19
|
+
> `readonly` `static` **CLASS\_NAME**: `string`
|
|
20
|
+
|
|
21
|
+
Runtime name for the class.
|
|
22
|
+
|
|
23
|
+
## Methods
|
|
24
|
+
|
|
25
|
+
### getModuleEntry()
|
|
26
|
+
|
|
27
|
+
> `static` **getModuleEntry**\<`T`\>(`module`, `entry`): `Promise`\<`T`\>
|
|
28
|
+
|
|
29
|
+
Get the module entry.
|
|
30
|
+
|
|
31
|
+
#### Type Parameters
|
|
32
|
+
|
|
33
|
+
• **T**
|
|
34
|
+
|
|
35
|
+
#### Parameters
|
|
36
|
+
|
|
37
|
+
• **module**: `string`
|
|
38
|
+
|
|
39
|
+
The module.
|
|
40
|
+
|
|
41
|
+
• **entry**: `string`
|
|
42
|
+
|
|
43
|
+
The entry to get from the module.
|
|
44
|
+
|
|
45
|
+
#### Returns
|
|
46
|
+
|
|
47
|
+
`Promise`\<`T`\>
|
|
48
|
+
|
|
49
|
+
The entry from the module.
|
|
50
|
+
|
|
51
|
+
#### Throws
|
|
52
|
+
|
|
53
|
+
GeneralError if getting the module entry failed.
|
|
54
|
+
|
|
55
|
+
***
|
|
56
|
+
|
|
57
|
+
### getModuleMethod()
|
|
58
|
+
|
|
59
|
+
> `static` **getModuleMethod**\<`T`\>(`module`, `method`): `Promise`\<(...`args`) => `T`\>
|
|
60
|
+
|
|
61
|
+
Get the method from a module.
|
|
62
|
+
|
|
63
|
+
#### Type Parameters
|
|
64
|
+
|
|
65
|
+
• **T**
|
|
66
|
+
|
|
67
|
+
#### Parameters
|
|
68
|
+
|
|
69
|
+
• **module**: `string`
|
|
70
|
+
|
|
71
|
+
The module.
|
|
72
|
+
|
|
73
|
+
• **method**: `string`
|
|
74
|
+
|
|
75
|
+
The method to execute from the module, use dot notation to get a static class method.
|
|
76
|
+
|
|
77
|
+
#### Returns
|
|
78
|
+
|
|
79
|
+
`Promise`\<(...`args`) => `T`\>
|
|
80
|
+
|
|
81
|
+
The result of the method execution.
|
|
82
|
+
|
|
83
|
+
#### Throws
|
|
84
|
+
|
|
85
|
+
GeneralError if executing the module entry failed.
|
|
86
|
+
|
|
87
|
+
***
|
|
88
|
+
|
|
89
|
+
### execModuleMethod()
|
|
90
|
+
|
|
91
|
+
> `static` **execModuleMethod**\<`T`\>(`module`, `method`, `args`?): `Promise`\<`T`\>
|
|
92
|
+
|
|
93
|
+
Execute the method in the module.
|
|
94
|
+
|
|
95
|
+
#### Type Parameters
|
|
96
|
+
|
|
97
|
+
• **T**
|
|
98
|
+
|
|
99
|
+
#### Parameters
|
|
100
|
+
|
|
101
|
+
• **module**: `string`
|
|
102
|
+
|
|
103
|
+
The module.
|
|
104
|
+
|
|
105
|
+
• **method**: `string`
|
|
106
|
+
|
|
107
|
+
The method to execute from the module.
|
|
108
|
+
|
|
109
|
+
• **args?**: `unknown`[]
|
|
110
|
+
|
|
111
|
+
The arguments to pass to the method.
|
|
112
|
+
|
|
113
|
+
#### Returns
|
|
114
|
+
|
|
115
|
+
`Promise`\<`T`\>
|
|
116
|
+
|
|
117
|
+
The result of the method execution.
|
|
118
|
+
|
|
119
|
+
#### Throws
|
|
120
|
+
|
|
121
|
+
GeneralError if executing the module entry failed.
|
|
122
|
+
|
|
123
|
+
***
|
|
124
|
+
|
|
125
|
+
### execModuleMethodThread()
|
|
126
|
+
|
|
127
|
+
> `static` **execModuleMethodThread**\<`T`\>(`module`, `method`, `args`?): `Promise`\<`T`\>
|
|
128
|
+
|
|
129
|
+
Execute the method in the module in a thread.
|
|
130
|
+
|
|
131
|
+
#### Type Parameters
|
|
132
|
+
|
|
133
|
+
• **T**
|
|
134
|
+
|
|
135
|
+
#### Parameters
|
|
136
|
+
|
|
137
|
+
• **module**: `string`
|
|
138
|
+
|
|
139
|
+
The module.
|
|
140
|
+
|
|
141
|
+
• **method**: `string`
|
|
142
|
+
|
|
143
|
+
The method to execute from the module.
|
|
144
|
+
|
|
145
|
+
• **args?**: `unknown`[]
|
|
146
|
+
|
|
147
|
+
The arguments to pass to the method.
|
|
148
|
+
|
|
149
|
+
#### Returns
|
|
150
|
+
|
|
151
|
+
`Promise`\<`T`\>
|
|
152
|
+
|
|
153
|
+
The result of the method execution.
|
|
154
|
+
|
|
155
|
+
#### Throws
|
|
156
|
+
|
|
157
|
+
GeneralError if executing the module entry failed.
|
package/locales/en.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"error": {
|
|
3
|
+
"moduleHelper": {
|
|
4
|
+
"moduleNotFound": "Failed to load module \"{module}\"",
|
|
5
|
+
"entryNotFound": "Failed to load entry \"{entry}\" from module \"{module}\"",
|
|
6
|
+
"notFunction": "The entry \"{entry}\" in module \"{module}\" is not a function",
|
|
7
|
+
"workerFailed": "The thread worker failed for \"{entry}\" in module \"{module}\" with exit code \"{exitCode}\"",
|
|
8
|
+
"workerException": "The thread worker failed with an exception \"{entry}\" in module \"{module}\""
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@twin.org/modules",
|
|
3
|
+
"version": "0.0.1-next.16",
|
|
4
|
+
"description": "Helper classes for loading and executing from modules",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/twinfoundation/framework.git",
|
|
8
|
+
"directory": "packages/modules"
|
|
9
|
+
},
|
|
10
|
+
"author": "martyn.janes@iota.org",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20.0.0"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@twin.org/core": "next",
|
|
18
|
+
"@twin.org/nameof": "next"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/cjs/index.cjs",
|
|
21
|
+
"module": "./dist/esm/index.mjs",
|
|
22
|
+
"types": "./dist/types/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"require": "./dist/cjs/index.cjs",
|
|
26
|
+
"import": "./dist/esm/index.mjs",
|
|
27
|
+
"types": "./dist/types/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./locales/*.json": "./locales/*.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist/cjs",
|
|
33
|
+
"dist/esm",
|
|
34
|
+
"dist/types",
|
|
35
|
+
"locales",
|
|
36
|
+
"docs"
|
|
37
|
+
]
|
|
38
|
+
}
|