-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (161 loc) · 4.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Require Node.js Dependencies
import { promises as fs } from "fs";
import { fileURLToPath } from "url";
import { join, extname, dirname } from "path";
const { readdir, readFile, access } = fs;
// Require Third-party Dependencies
import Addon from "@slimio/addon";
// Node.js CJS CONSTANTS
const __dirname = dirname(fileURLToPath(import.meta.url));
// CONSTANTS
const CORE = global.slimio_core;
const DUMP_DIR = join(__dirname, "..", "..", "debug");
// SYMBOLS
const SYM_PARALLEL = Symbol.for("ParallelAddon");
const gate = new Addon("gate");
/**
* @async
* @function globalInfo
* @returns {Promise<any>}
*/
async function globalInfo() {
const { root, silent } = CORE;
const coreVersion = global.coreVersion || "0.0.0";
const versions = process.versions;
return { root, silent, coreVersion, versions };
}
/**
* @async
* @function listAddons
* @returns {Promise<string[]>}
*/
async function listAddons() {
return [...CORE.addons.keys()].map((addonName) => addonName.toLowerCase());
}
/**
* @async
* @function getRoutingTable
* @returns {Promise<string[]>}
*/
async function getRoutingTable() {
return [...CORE.routingTable.keys()];
}
/**
* @async
* @function getConfig
* @param {!Addon.CallbackHeader} header callback header
* @param {!string} path key path in the configuration file
* @returns {Promise<any>}
*/
async function getConfig(header, path) {
if (typeof path === "string") {
return CORE.config.get(path);
}
return CORE.config.payload;
}
/**
* @async
* @function setConfig
* @param {!Addon.CallbackHeader} header callback header
* @param {!string} path key path in the configuration file
* @param {!string} value key value
* @returns {Promise<void>}
*
* @throws {TypeError}
*/
async function setConfig(header, path, value) {
if (typeof path !== "string" || typeof value !== "string") {
throw new TypeError("path and value must be typeof string");
}
CORE.config.set(path, value);
CORE.config.lazyWriteOnDisk();
}
/**
* @async
* @function dumpList
* @returns {Promise<string[]>}
*/
async function dumpList() {
const dirents = await readdir(DUMP_DIR, { withFileTypes: true });
const jsonFiles = dirents
.filter((dirent) => dirent.isFile() && extname(dirent.name) === ".json")
.map((dirent) => dirent.name);
return jsonFiles;
}
/**
* @async
* @function getDump
* @param {!Addon.CallbackHeader} header callback header
* @param {!string} name dump name
* @returns {Promise<object | null>}
*
* @throws {TypeError}
*/
async function getDump(header, name) {
if (typeof name !== "string") {
throw new TypeError("name must be a string");
}
const completeName = extname(name) === ".json" ? name : `${name}.json`;
try {
const filePath = join(DUMP_DIR, completeName);
await access(filePath);
const str = await readFile(filePath, "utf-8");
return JSON.parse(str);
}
catch (err) {
return null;
}
}
/**
* @async
* @function startAddon
* @param {!Addon.CallbackHeader} header callback header
* @param {!string} addonName addon to start!
* @returns {Promise<void>}
*/
async function startAddon(header, addonName) {
if (!CORE.addons.has(addonName)) {
return;
}
const addon = CORE.addons.get(addonName);
if (addon.started) {
return;
}
await CORE.setupAddonConfiguration(addonName, { active: true, standalone: false });
}
/**
* @async
* @function getLockState
* @param {!Addon.CallbackHeader} header callback header
* @returns {Promise<object>}
*/
async function getLockState(header) {
const currentAddon = CORE.addons.get(header.from);
const result = {};
for (const addonName of currentAddon.locks.keys()) {
const localAddon = CORE.addons.get(addonName);
const payload = { ready: false, started: false, awake: false };
// eslint-disable-next-line
if (Boolean(localAddon[SYM_PARALLEL])) {
// TODO: how do we handle parallel addon ?
}
else {
payload.ready = localAddon.isReady;
payload.started = localAddon.isStarted;
payload.awake = localAddon.isAwake;
}
result[addonName] = payload;
}
return result;
}
// Register all callbacks
gate.registerCallback(globalInfo)
.registerCallback(listAddons)
.registerCallback(getRoutingTable)
.registerCallback(getConfig)
.registerCallback(setConfig)
.registerCallback(dumpList)
.registerCallback(getDump)
.registerCallback(startAddon)
.registerCallback(getLockState);
export default gate;