-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (232 loc) · 7.97 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"use strict";
// Require Node.js Dependencies
const stream = require("stream");
const { promisify } = require("util");
const { join, sep } = require("path");
const { createGunzip } = require("zlib");
const { spawn, execFile } = require("child_process");
const {
createReadStream,
promises: { mkdir, rmdir, rename }
} = require("fs");
// Require Third-party Dependencies
const tar = require("tar-fs");
const github = require("@slimio/github");
const gitlab = require("@slimio/gitlab");
const Manifest = require("@slimio/manifest");
const registry = require("@slimio/registry-sdk");
// Require Internal Dependencies
const { hasPackageLock } = require("./src/utils.js");
// CONSTANTS
const AGENT_ARCHIVE = join(__dirname, "archive", "Agent-master.tar.gz");
const AGENT_REMOTE_NAME = "SlimIO.Agent";
const EXEC_SUFFIX = process.platform === "win32";
const BUILT_IN_ADDONS = Object.freeze(["Events", "Socket", "Gate", "Alerting", "Aggregator"]);
const DEFAULT_ORG_NAME = "SlimIO";
const SUPPORTED_HOSTS = new Set(["github.com", "gitlab.com"]);
// ASYNC
const pipeline = promisify(stream.pipeline);
const execFileAsync = promisify(execFile);
/**
* @async
* @function initAgent
* @param {!string} location
* @param {object} [options]
* @param {string} [options.token]
* @param {string} [options.name="agent"]
* @param {string} [options.forceClean=true]
* @returns {Promise<string>}
*/
async function initAgent(location, options = Object.create(null)) {
const { token = null, name = "agent", forceClean = true } = options;
if (forceClean) {
await rmdir(join(location, name), { recursive: true });
}
const agentDir = await extractAgent(location, { name, token });
const addonsDir = join(agentDir, "addons");
await mkdir(addonsDir, { recursive: true });
await Promise.all([
installDependencies(agentDir, true),
...BUILT_IN_ADDONS.map((addonName) => installAddon(addonName, addonsDir, { token }))
]);
return agentDir;
}
/**
* @async
* @function extractAgent
* @param {!string} dest
* @param {object} options
* @param {boolean} [options.downloadFromRemote=false] download from remote github repository (or from a local archive)
* @param {string} [options.token] github token
* @param {string} [options.name] repository name (on the local system).
* @returns {Promise<string>}
*
* @throws {TypeError}
*/
async function extractAgent(dest, options = Object.create(null)) {
if (typeof dest !== "string") {
throw new TypeError("dest must be a string");
}
const { downloadFromRemote = false, token, name } = options;
await mkdir(dest, { recursive: true });
let currentName = "";
if (downloadFromRemote) {
currentName = await github(AGENT_REMOTE_NAME, { dest, extract: true, token });
}
else {
await pipeline(
createReadStream(AGENT_ARCHIVE),
createGunzip(),
tar.extract(dest)
);
currentName = join(dest, "Agent-master");
}
if (typeof name === "string" && name.trim() !== "") {
const finalPath = join(dest, name);
await rename(currentName, finalPath);
return finalPath;
}
return currentName;
}
/**
* @async
* @function runAgent
* @param {!string} location
* @param {boolean} [silent=true]
* @param {object} [options]
* @returns {Promise<NodeJS.ReadStream>}
*/
async function runAgent(location, silent = true, options = { stdio: "inherit" }) {
const cpArgs = ["--experimental-modules", join(location, "index.js")];
if (silent) {
cpArgs.push("--silent");
}
const cp = spawn(process.argv[0], cpArgs, options);
/* istanbul ignore next */
cp.on("error", (err) => console.error(err));
// Wait a little bit (else the agent will not be yet started).
await new Promise((resolve) => setTimeout(resolve, 1000));
return cp;
}
/**
* @async
* @function installDependencies
* @param {!string} cwd working dir where we need to run the npm install cmd
* @param {boolean} [lock=false] install with package.lock (npm ci)
* @returns {Promise<void>}
*/
async function installDependencies(cwd = process.cwd(), lock = false) {
const cmdArgs = lock ? ["ci", "--only=production"] : ["install", "--production"];
await execFileAsync(`npm${EXEC_SUFFIX ? ".cmd" : ""}`, cmdArgs, { cwd });
}
/**
* @async
* @function renameDirFromManifest
* @description Rename cloned addon repository by retrieving the real name in the SlimIO manifest.
* @param {!string} dir location of the directory to rename
* @param {!string} fileName manifest file name
* @returns {Promise<string>}
*/
async function renameDirFromManifest(dir = process.cwd(), fileName = "slimio.toml") {
let name;
try {
({ name } = Manifest.open(join(dir, fileName)));
}
catch (error) {
([name] = dir.split(sep).pop().split("-"));
}
const addonName = join(dir, "..", name);
await rename(dir, addonName);
return addonName;
}
/**
* @function parseAddonExpr
* @param {!string} addonExpr
* @returns {[string, string]}
*
* @throws {Error}
*
* @example
* parseAddonExpr("https://github.com/SlimIO/Socket"); // { repoUserOrg: "SlimIO", repoName: "Socket", host: "github.com" }
* parseAddonExpr("Socket"); // { repoUserOrg: "SlimIO", repoName: "Socket", host: null }
* parseAddonExpr("Foo/Socket"); // { repoUserOrg: "Foo", repoName: "Socket", host: null }
*/
function parseAddonExpr(addonExpr) {
try {
const gitURL = addonExpr instanceof URL ? addonExpr : new URL(addonExpr);
const { host } = gitURL;
if (!SUPPORTED_HOSTS.has(host)) {
throw new Error(`Unsupported host '${host}'`);
}
const [repoUserOrg, repoName] = gitURL.pathname.split("/", 3).slice(1);
return {
repoUserOrg, repoName, host
};
}
catch (error) {
if (error.code !== "ERR_INVALID_URL") {
throw error;
}
const newAddonExpr = addonExpr.replace(/[.]/g, "/");
if (newAddonExpr.indexOf("/") === -1) {
return { repoUserOrg: DEFAULT_ORG_NAME, repoName: newAddonExpr, host: null };
}
const [repoUserOrg, repoName] = newAddonExpr.split("/", 2);
return { repoUserOrg, repoName, host: null };
}
}
/**
* @function setRegistryURL
* @description set a new SlimIO Registry URL
* @param {!URL} url WHATWG URL
* @returns {void}
*
* @throws {TypeError}
*/
function setRegistryURL(url) {
if (!(url instanceof URL)) {
throw new TypeError("url must be an instanceof WHATWG URL");
}
registry.constants.registry_url = url;
}
/**
* @async
* @function installAddon
* @param {!string} addonExpr
* @param {!string} dest
* @param {object} [options]
* @param {boolean} [options.installDependencies=true]
* @param {boolean} [options.searchInRegistry=false]
* @param {string} [options.token]
* @returns {Promise<string>}
*/
async function installAddon(addonExpr, dest, options = Object.create(null)) {
const { installDependencies: iDep = true, searchInRegistry = false, token } = options;
await mkdir(dest, { recursive: true });
if (searchInRegistry) {
const addonInfos = await registry.getOneAddon(addonExpr);
// eslint-disable-next-line no-param-reassign
addonExpr = addonInfos.git;
}
const { repoUserOrg, repoName, host } = parseAddonExpr(addonExpr);
const config = Object.assign({ dest, extract: true }, typeof token === "string" ? { auth: token } : {});
const hostMethod = host === "github.com" || host === null ? github : gitlab;
const dirName = await hostMethod(`${repoUserOrg}.${repoName}`, config);
const addonDir = await renameDirFromManifest(dirName);
if (iDep) {
const pkgLock = await hasPackageLock(addonDir);
await installDependencies(addonDir, pkgLock);
}
return addonDir;
}
module.exports = {
initAgent,
setRegistryURL,
parseAddonExpr,
extractAgent,
runAgent,
renameDirFromManifest,
installDependencies,
installAddon,
CONSTANTS: Object.freeze({ BUILT_IN_ADDONS })
};