diff --git a/api/.swcrc b/api/.swcrc new file mode 100644 index 00000000..794efbd7 --- /dev/null +++ b/api/.swcrc @@ -0,0 +1,17 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021", + "baseUrl": "./", + "paths": { + "@fosscord/api": ["src/index.ts"], + "@fosscord/api/*": ["src/*"] + } + } +} diff --git a/api/jsconfig.json b/api/jsconfig.json deleted file mode 100644 index 41c4cb9a..00000000 --- a/api/jsconfig.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "compilerOptions": { - "baseUrl": ".", - "paths": { - "@util/*": ["./src/util/*"], - "@middlewares/*": ["./src/middlewares/*"], - "@schema/*": ["./src/schema/*"] - } - }, - "include": ["src"] -} diff --git a/api/package-lock.json b/api/package-lock.json index 84d3ffd3..2af8c7b9 100644 Binary files a/api/package-lock.json and b/api/package-lock.json differ diff --git a/api/package.json b/api/package.json index c2a75a8f..3f0315ae 100644 --- a/api/package.json +++ b/api/package.json @@ -2,8 +2,8 @@ "name": "@fosscord/api", "version": "1.0.0", "description": "This repository contains the HTTP API Server", - "main": "dist/Server.js", - "types": "dist/Server.d.ts", + "main": "dist/index.js", + "types": "src/index.ts", "scripts": { "test:only": "jest --coverage --verbose --forceExit ./tests", "test:routes": "jest --coverage --verbose --forceExit ./routes.test.ts", @@ -40,6 +40,8 @@ "@babel/core": "^7.15.5", "@babel/preset-env": "^7.15.6", "@babel/preset-typescript": "^7.15.0", + "@swc/cli": "^0.1.51", + "@swc/core": "^1.2.93", "@types/amqplib": "^0.8.1", "@types/bcrypt": "^5.0.0", "@types/express": "^4.17.9", @@ -67,6 +69,7 @@ "ts-node": "^9.1.1", "ts-node-dev": "^1.1.6", "ts-patch": "^1.4.4", + "tsup": "^5.4.0", "typescript": "^4.4.2", "typescript-json-schema": "0.50.1" }, @@ -84,6 +87,7 @@ "dot-prop": "^6.0.1", "dotenv": "^8.2.0", "env-paths": "^2.2.1", + "esbuild": "^0.13.4", "express": "^4.17.1", "express-validator": "^6.9.2", "form-data": "^3.0.0", @@ -102,7 +106,8 @@ "patch-package": "^6.4.7", "supertest": "^6.1.6", "tsconfig-paths": "^3.11.0", - "typeorm": "^0.2.37" + "typeorm": "^0.2.37", + "wsc": "^0.3.0" }, "jest": { "setupFiles": [ diff --git a/api/tsconfig-docker.json b/api/tsconfig-docker.json index 5d1f0a3c..1713dd64 100644 --- a/api/tsconfig-docker.json +++ b/api/tsconfig-docker.json @@ -7,7 +7,7 @@ // "incremental": true, /* Enable incremental compilation */ "target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": ["ES2020"] /* Specify library files to be included in the compilation. */, + "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, "allowJs": true /* Allow javascript files to be compiled. */, "checkJs": true /* Report errors in .js files. */, // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ diff --git a/api/tsconfig.json b/api/tsconfig.json index 0169642d..0bbd615a 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -7,7 +7,7 @@ // "incremental": true, /* Enable incremental compilation */ "target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": ["ES2020"] /* Specify library files to be included in the compilation. */, + "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, "allowJs": true /* Allow javascript files to be compiled. */, "checkJs": true /* Report errors in .js files. */, // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ diff --git a/bundle/.swcrc b/bundle/.swcrc new file mode 100644 index 00000000..9a89b49d --- /dev/null +++ b/bundle/.swcrc @@ -0,0 +1,12 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021" + } +} diff --git a/bundle/package-lock.json b/bundle/package-lock.json index ea4fb9e2..dd80d787 100644 Binary files a/bundle/package-lock.json and b/bundle/package-lock.json differ diff --git a/bundle/package.json b/bundle/package.json index cd2b9f44..a7f5358b 100644 --- a/bundle/package.json +++ b/bundle/package.json @@ -5,15 +5,10 @@ "main": "src/start.js", "scripts": { "setup": "cd ../util && npm --production=false i && cd ../api && npm --production=false i && cd ../cdn && npm --production=false i && cd ../gateway && npm --production=false i && cd ../bundle/ && npm --production=false i && npm run build", - "build": "npm run build:util && npm run build:api && npm run build:cdn && npm run build:gateway && npm run build:bundle", - "postinstall": "ts-patch install -s", + "build": "node scripts/build.js", "build:bundle": "npx tsc -b .", - "build:util": "cd ../util/ && npm run build", - "build:api": "cd ../api/ && npm run build", - "build:cdn": "cd ../cdn/ && npm run build", - "build:gateway": "cd ../gateway/ && npm run build", - "start": "npm run build && npm run start:bundle", - "start:bundle": "node dist/start.js", + "start": "node scripts/build.js && node -r tsconfig-paths/register dist/start.js", + "start:bundle": "node -r tsconfig-paths/register dist/start.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -28,6 +23,8 @@ }, "homepage": "https://fosscord.com", "devDependencies": { + "@swc/cli": "^0.1.51", + "@swc/core": "^1.2.93", "@types/amqplib": "^0.8.1", "@types/async-exit-hook": "^2.0.0", "@types/bcrypt": "^5.0.0", @@ -44,9 +41,12 @@ "@types/uuid": "^8.3.0", "@types/ws": "^7.4.0", "@zerollup/ts-transform-paths": "^1.7.18", + "esbuild": "^0.13.4", + "esbuild-plugin-tsc": "^0.3.0", "ts-node": "^10.2.1", "ts-patch": "^1.4.4", - "typescript": "^4.3.5" + "tsconfig-paths": "^3.11.0", + "typescript": "^4.4.3" }, "dependencies": { "@fosscord/api": "file:../api", @@ -59,6 +59,6 @@ "missing-native-js-functions": "^1.2.17", "nanocolors": "^0.2.12", "node-os-utils": "^1.3.5", - "tsconfig-paths": "^3.11.0" + "reflect-metadata": "^0.1.13" } } diff --git a/bundle/scripts/build.js b/bundle/scripts/build.js new file mode 100644 index 00000000..ba559ecf --- /dev/null +++ b/bundle/scripts/build.js @@ -0,0 +1,70 @@ +const { spawn } = require("child_process"); +const path = require("path"); +const { performance } = require("perf_hooks"); +const fs = require("fs"); +const esbuildPluginTsc = require("esbuild-plugin-tsc"); + +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"); + +// because npm run is slow we directly get the build script of the package.json script + +function buildPackage(dir) { + const element = path.basename(dir); + + require("esbuild").build({ + entryPoints: walk(path.join(dir, "src")), + bundle: false, + outdir: path.join(dir, "dist"), + target: "es2021", + format: "cjs", + plugins: [esbuildPluginTsc({})], + keepNames: true, + }); +} + +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(); +console.log("[Build] starting ..."); + +util(); +for (const part of parts) { + buildPackage(path.join(__dirname, "..", "..", part)); +} + +process.on("exit", () => { + console.log("[Build] took " + Math.round(performance.now() - start) + "ms"); +}); + +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")) { + /* Is a file */ + results.push(file); + } + }); + return results; +} diff --git a/bundle/src/start.ts b/bundle/src/start.ts index 353ebfc6..872f324e 100644 --- a/bundle/src/start.ts +++ b/bundle/src/start.ts @@ -1,4 +1,20 @@ // process.env.MONGOMS_DEBUG = "true"; +const tsConfigPaths = require("tsconfig-paths"); +const path = require("path"); +const baseUrl = path.join(__dirname, ".."); // Either absolute or relative path. If relative it's resolved to current working directory. +const cleanup = tsConfigPaths.register({ + baseUrl, + paths: { + "@fosscord/api": ["../api/dist/index.js"], + "@fosscord/api/*": ["../api/dist/*"], + "@fosscord/gateway": ["../gateway/dist/index.js"], + "@fosscord/gateway/*": ["../gateway/dist/*"], + "@fosscord/cdn": ["../cdn/dist/index.js"], + "@fosscord/cdn/*": ["../cdn/dist/*"], + }, +}); + +import "reflect-metadata"; import cluster from "cluster"; import os from "os"; import { red, bold, yellow, cyan } from "nanocolors"; diff --git a/bundle/tsconfig.json b/bundle/tsconfig.json index 6bf2e6b7..b300a426 100644 --- a/bundle/tsconfig.json +++ b/bundle/tsconfig.json @@ -4,10 +4,10 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ + "incremental": true /* Enable incremental compilation */, "target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": ["ES2020"] /* Specify library files to be included in the compilation. */, + "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, "allowJs": true /* Allow javascript files to be compiled. */, "checkJs": true /* Report errors in .js files. */, // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ @@ -63,6 +63,8 @@ /* Advanced Options */ "skipLibCheck": true /* Skip type checking of declaration files. */, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "emitDecoratorMetadata": true, + "experimentalDecorators": true } } diff --git a/cdn/.swcrc b/cdn/.swcrc new file mode 100644 index 00000000..ab531194 --- /dev/null +++ b/cdn/.swcrc @@ -0,0 +1,17 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021", + "baseUrl": ".", + "paths": { + "@fosscord/cdn/": ["src/index"], + "@fosscord/cdn/*": ["src/*"] + } + } +} diff --git a/cdn/package-lock.json b/cdn/package-lock.json index a71d100d..f0cce0af 100644 Binary files a/cdn/package-lock.json and b/cdn/package-lock.json differ diff --git a/cdn/package.json b/cdn/package.json index 24870318..5d3f1589 100644 --- a/cdn/package.json +++ b/cdn/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "cdn for fosscord", "main": "dist/index.js", - "types": "dist/index.d.ts", + "types": "src/index.ts", "scripts": { "postinstall": "ts-patch install -s", "test": "npm run build && jest --coverage ./tests", @@ -22,6 +22,8 @@ }, "homepage": "https://github.com/fosscord/fosscord-server#readme", "devDependencies": { + "@swc/cli": "^0.1.51", + "@swc/core": "^1.2.93", "@types/amqplib": "^0.8.1", "@types/body-parser": "^1.19.0", "@types/btoa": "^1.2.3", diff --git a/cdn/tsconfig.json b/cdn/tsconfig.json index f2f378e1..c80d16fd 100644 --- a/cdn/tsconfig.json +++ b/cdn/tsconfig.json @@ -4,7 +4,7 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ + "incremental": true, /* Enable incremental compilation */ "target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, "lib": ["ES2015", "dom"] /* Specify library files to be included in the compilation. */, diff --git a/gateway/.swcrc b/gateway/.swcrc new file mode 100644 index 00000000..3079a89f --- /dev/null +++ b/gateway/.swcrc @@ -0,0 +1,17 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021", + "baseUrl": ".", + "paths": { + "@fosscord/gateway": ["src/index"], + "@fosscord/gateway/*": ["src/*"] + } + } +} diff --git a/gateway/package-lock.json b/gateway/package-lock.json index 65117a07..38962a2a 100644 Binary files a/gateway/package-lock.json and b/gateway/package-lock.json differ diff --git a/gateway/package.json b/gateway/package.json index b0119477..ddbddeff 100644 --- a/gateway/package.json +++ b/gateway/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "", "main": "dist/index.js", + "types": "src/index.ts", "scripts": { "postinstall": "npx ts-patch install -s", "test": "echo \"Error: no test specified\" && exit 1", @@ -14,6 +15,8 @@ "author": "Fosscord", "license": "ISC", "devDependencies": { + "@swc/cli": "^0.1.51", + "@swc/core": "^1.2.93", "@types/amqplib": "^0.8.1", "@types/jsonwebtoken": "^8.5.0", "@types/mongodb": "^3.6.9", diff --git a/gateway/tsconfig.json b/gateway/tsconfig.json index 7143c8a3..e2ac5b0b 100644 --- a/gateway/tsconfig.json +++ b/gateway/tsconfig.json @@ -5,7 +5,7 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ + "incremental": true, /* Enable incremental compilation */ "target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, "lib": ["ES2015", "ES2020.BigInt", "DOM"] /* Specify library files to be included in the compilation. */, diff --git a/util/.swcrc b/util/.swcrc new file mode 100644 index 00000000..a723d37f --- /dev/null +++ b/util/.swcrc @@ -0,0 +1,18 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021", + "transform": { + "legacyDecorator": true, + "decoratorMetadata": true + }, + "keepClassNames": true, + "loose": true + } +} diff --git a/util/package-lock.json b/util/package-lock.json index 7249b87d..0ce50636 100644 Binary files a/util/package-lock.json and b/util/package-lock.json differ diff --git a/util/package.json b/util/package.json index 33abf483..32204081 100644 --- a/util/package.json +++ b/util/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "description": "Utility functions for the all server repositories", "main": "dist/index.js", - "types": "dist/index.d.ts", + "types": "src/index.ts", "scripts": { "start": "npm run build && node dist/", "test": "npm run build && jest", @@ -28,6 +28,8 @@ }, "homepage": "https://docs.fosscord.com/", "devDependencies": { + "@swc/cli": "^0.1.51", + "@swc/core": "^1.2.93", "@types/amqplib": "^0.8.1", "@types/jsonwebtoken": "^8.5.0", "@types/mongoose-autopopulate": "^0.10.1", diff --git a/util/src/util/Snowflake.ts b/util/src/util/Snowflake.ts index 1d725710..f7a13388 100644 --- a/util/src/util/Snowflake.ts +++ b/util/src/util/Snowflake.ts @@ -1,5 +1,5 @@ // @ts-nocheck -import cluster from "cluster"; +import * as cluster from "cluster"; // https://github.com/discordjs/discord.js/blob/master/src/util/Snowflake.js // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah diff --git a/util/tsconfig.json b/util/tsconfig.json index 7fbe3bac..cbd5db32 100644 --- a/util/tsconfig.json +++ b/util/tsconfig.json @@ -4,10 +4,10 @@ /* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ + "incremental": true, /* Enable incremental compilation */ "target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": ["ES2020"] /* Specify library files to be included in the compilation. */, + "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, "allowJs": true /* Allow javascript files to be compiled. */, "checkJs": true /* Report errors in .js files. */, // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ diff --git a/webrtc/.swcrc b/webrtc/.swcrc new file mode 100644 index 00000000..9a89b49d --- /dev/null +++ b/webrtc/.swcrc @@ -0,0 +1,12 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "decorators": true + }, + "target": "es2021" + } +} diff --git a/webrtc/tsconfig.json b/webrtc/tsconfig.json index 6bf2e6b7..70631fca 100644 --- a/webrtc/tsconfig.json +++ b/webrtc/tsconfig.json @@ -7,7 +7,7 @@ // "incremental": true, /* Enable incremental compilation */ "target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */, "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, - "lib": ["ES2020"] /* Specify library files to be included in the compilation. */, + "lib": ["ES2021"] /* Specify library files to be included in the compilation. */, "allowJs": true /* Allow javascript files to be compiled. */, "checkJs": true /* Report errors in .js files. */, // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */