summary refs log tree commit diff
path: root/bundle/scripts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-15 00:44:19 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-15 00:44:19 +0200
commit288eeab6bbb793473788dffa1cae68790ead6d0e (patch)
tree3b60a63f8a7c6317cb43fda1f7ab29993814b587 /bundle/scripts
parentfeat: implement a database check on the healthz and readyz probes, remove /-/... (diff)
parent:art: do not automatically create default guild (diff)
downloadserver-288eeab6bbb793473788dffa1cae68790ead6d0e.tar.xz
Merge branch 'master' into pr/hbjydev/454
Diffstat (limited to 'bundle/scripts')
-rw-r--r--bundle/scripts/benchmark/connections.js58
-rw-r--r--bundle/scripts/benchmark/index.js4
-rw-r--r--bundle/scripts/benchmark/messages.js1
-rw-r--r--bundle/scripts/build.js130
-rw-r--r--bundle/scripts/install.js14
5 files changed, 115 insertions, 92 deletions
diff --git a/bundle/scripts/benchmark/connections.js b/bundle/scripts/benchmark/connections.js
new file mode 100644
index 00000000..efc1bcb6
--- /dev/null
+++ b/bundle/scripts/benchmark/connections.js
@@ -0,0 +1,58 @@
+const cluster = require("cluster");
+const WebSocket = require("ws");
+const endpoint = process.env.GATEWAY || "ws://localhost:3001";
+const connections = Number(process.env.CONNECTIONS) || 50;
+const threads = Number(process.env.THREADS) || require("os").cpus().length || 1;
+const token = process.env.TOKEN;
+
+if (!token) {
+	console.error("TOKEN env var missing");
+	process.exit();
+}
+
+if (cluster.isMaster) {
+	for (let i = 0; i < threads; i++) {
+		cluster.fork();
+	}
+
+	cluster.on("exit", (worker, code, signal) => {
+		console.log(`worker ${worker.process.pid} died`);
+	});
+} else {
+	for (let i = 0; i < connections; i++) {
+		connect();
+	}
+}
+
+function connect() {
+	const client = new WebSocket(endpoint);
+	client.on("message", (data) => {
+		data = JSON.parse(data);
+
+		switch (data.op) {
+			case 10:
+				client.interval = setInterval(() => {
+					client.send(JSON.stringify({ op: 1 }));
+				}, data.d.heartbeat_interval);
+
+				client.send(
+					JSON.stringify({
+						op: 2,
+						d: {
+							token,
+							properties: {},
+						},
+					})
+				);
+
+				break;
+		}
+	});
+	client.once("close", (code, reason) => {
+		clearInterval(client.interval);
+		connect();
+	});
+	client.on("error", (err) => {
+		// console.log(err);
+	});
+}
diff --git a/bundle/scripts/benchmark/index.js b/bundle/scripts/benchmark/index.js
new file mode 100644
index 00000000..37ac5633
--- /dev/null
+++ b/bundle/scripts/benchmark/index.js
@@ -0,0 +1,4 @@
+require("dotenv").config();
+
+require("./connections");
+require("./messages");
diff --git a/bundle/scripts/benchmark/messages.js b/bundle/scripts/benchmark/messages.js
new file mode 100644
index 00000000..70b786d1
--- /dev/null
+++ b/bundle/scripts/benchmark/messages.js
@@ -0,0 +1 @@
+// TODO
diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js
index d7bd23d9..dbc305a9 100644
--- a/bundle/scripts/build.js
+++ b/bundle/scripts/build.js
@@ -1,103 +1,49 @@
-const { spawn } = require("child_process");
+const { execSync } = require("child_process");
 const path = require("path");
-const fs = require("fs");
-const { performance } = require("perf_hooks");
+const fse = require("fs-extra");
+const { getSystemErrorMap } = require("util");
+const { argv } = require("process");
 
-let parts = "api,cdn,gateway,bundle".split(",");
-const tscBin = path.join(__dirname, "..", "..", "util", "node_modules", "typescript", "bin", "tsc");
-const swcBin = path.join(__dirname, "..", "..", "util", "node_modules", "@swc", "cli", "bin", "swc");
+const dirs = ["api", "util", "cdn", "gateway", "bundle"];
 
-// because npm run is slow we directly get the build script of the package.json script
+const verbose = argv.includes("verbose") || argv.includes("v");
 
-function buildPackage(dir) {
-	const element = path.basename(dir);
-
-	return require("esbuild").build({
-		entryPoints: walk(path.join(dir, "src")),
-		bundle: false,
-		outdir: path.join(dir, "dist"),
-		target: "es2021",
-		// plugins don't really work because bundle is false
-		keepNames: false,
-		tsconfig: path.join(dir, "tsconfig.json"),
-	});
-}
-
-const importPart = /import (\* as )?(({[^}]+})|(\w+)) from ("[.\w-/@q]+")/g;
-const importMod = /import ("[\w-/@q.]+")/g;
-const exportDefault = /export default/g;
-const exportAllAs = /export \* from (".+")/g;
-const exportMod = /export ({[\w, ]+})/g;
-const exportConst = /export (const|var|let) (\w+)/g;
-const exportPart = /export ((async )?\w+) (\w+)/g;
-
-// resolves tsconfig paths + rewrites es6 imports/exports to require (because esbuild/swc doesn't work properly)
-function transpileFiles() {
-	for (const part of ["gateway", "api", "cdn", "bundle"]) {
-		const files = walk(path.join(__dirname, "..", "..", part, "dist"));
-		for (const file of files) {
-			let content = fs.readFileSync(file, { encoding: "utf8" });
-			content = content
-				.replace(
-					new RegExp(`@fosscord/${part}`),
-					path.relative(file, path.join(__dirname, "..", "..", part, "dist")).slice(3)
-				)
-				.replace(importPart, `const $2 = require($5)`)
-				.replace(importMod, `require($1)`)
-				.replace(exportDefault, `module.exports =`)
-				.replace(exportAllAs, `module.exports = {...(module.exports)||{}, ...require($1)}`)
-				.replace(exportMod, "module.exports = $1")
-				.replace(exportConst, `let $2 = {};\nmodule.exports.$2 = $2`)
-				.replace(exportPart, `module.exports.$3 = $1 $3`);
-			fs.writeFileSync(file, content);
+if (argv.includes("clean")) {
+	dirs.forEach((a) => {
+		var d = "../" + a + "/dist";
+		if (fse.existsSync(d)) {
+			fse.rmSync(d, { recursive: true });
+			if (verbose) console.log(`Deleted ${d}!`);
 		}
-	}
-}
-
-function util() {
-	// const child = spawn("node", `${swcBin}  src --out-dir dist --sync`.split(" "), {
-	const child = spawn("node", `\"${tscBin}\" -b .`.split(" "), {
-		cwd: path.join(__dirname, "..", "..", "util"),
-		env: process.env,
-		shell: true,
 	});
-	function log(data) {
-		console.log(`[util] ` + data.toString().slice(0, -1));
-	}
-	child.stdout.on("data", log);
-	child.stderr.on("data", log);
-	child.on("error", (err) => console.error("util", err));
-	return child;
 }
 
-const start = performance.now();
-
-async function main() {
-	console.log("[Build] starting ...");
-	util();
-	await Promise.all(parts.map((part) => buildPackage(path.join(__dirname, "..", "..", part))));
-	transpileFiles();
-}
-
-main();
-
-process.on("exit", () => {
-	console.log("[Build] took " + Math.round(performance.now() - start) + "ms");
+fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets"));
+fse.copySync(
+	path.join(__dirname, "..", "..", "api", "client_test"),
+	path.join(__dirname, "..", "dist", "api", "client_test")
+);
+fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales"));
+dirs.forEach((a) => {
+	fse.copySync("../" + a + "/src", "dist/" + a + "/src");
+	if (verbose) console.log(`Copied ${"../" + a + "/dist"} -> ${"dist/" + a + "/src"}!`);
 });
 
-function walk(dir) {
-	var results = [];
-	var list = fs.readdirSync(dir);
-	list.forEach(function (file) {
-		file = dir + "/" + file;
-		var stat = fs.statSync(file);
-		if (stat && stat.isDirectory()) {
-			/* Recurse into a subdirectory */
-			results = results.concat(walk(file));
-		} else if (file.endsWith(".ts") || file.endsWith(".js")) {
-			/* Is a file */
-			results.push(file);
+console.log("Copying src files done");
+console.log("Compiling src files ...");
+
+console.log(
+	execSync(
+		'node "' +
+			path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
+			'" -p "' +
+			path.join(__dirname, "..") +
+			'"',
+		{
+			cwd: path.join(__dirname, ".."),
+			shell: true,
+			env: process.env,
+			encoding: "utf8",
 		}
-	});
-	return results;
-}
+	)
+);
diff --git a/bundle/scripts/install.js b/bundle/scripts/install.js
new file mode 100644
index 00000000..3008b4c5
--- /dev/null
+++ b/bundle/scripts/install.js
@@ -0,0 +1,14 @@
+const path = require("path");
+const fs = require("fs");
+const parts = ["api", "util", "cdn", "gateway"];
+
+const bundle = require("../package.json");
+
+for (const part of parts) {
+	const { devDependencies, dependencies } = require(path.join("..", "..", part, "package.json"));
+	bundle.devDependencies = { ...bundle.devDependencies, ...devDependencies };
+	bundle.dependencies = { ...bundle.dependencies, ...dependencies };
+	delete bundle.dependencies["@fosscord/util"];
+}
+
+fs.writeFileSync(path.join(__dirname, "..", "package.json"), JSON.stringify(bundle, null, "\t"), { encoding: "utf8" });