From 4528a96ded3f2023d193957a95ffbd070e4cfc30 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 22 Apr 2021 23:29:06 +0200 Subject: [PATCH] prepare npm publish --- .npmignore | 1 + README.md | 32 +++++++---- package-lock.json | Bin 775569 -> 758715 bytes package.json | 18 ++++-- src/Server.ts | 10 ++-- src/index.ts | 52 +++++++----------- src/middlewares/index.ts | 10 ++-- src/start.ts | 33 +++++++++++ ...think_test.ts => rethink_test.ts.disabled} | 0 src/util/Captcha.ts | 1 + src/util/Config.ts | 6 +- tsconfig.json | 2 +- 12 files changed, 104 insertions(+), 61 deletions(-) create mode 100644 .npmignore create mode 100644 src/start.ts rename src/test/{rethink_test.ts => rethink_test.ts.disabled} (100%) diff --git a/.npmignore b/.npmignore new file mode 100644 index 00000000..05a9d0cf --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +!dist/ \ No newline at end of file diff --git a/README.md b/README.md index bf8eed09..d83120e2 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,51 @@ # Fosscord API Server -This repository contains the HTTP API Server + +This repository contains the Fosscord HTTP API Server ## Bug Tracker + [Project Board](https://github.com/fosscord/fosscord-api/projects/2) ## API -We use [express](https://expressjs.com/) for the HTTP Server and +We use [express](https://expressjs.com/) for the HTTP Server and [lambert-server](https://www.npmjs.com/package/lambert-server) for route handling and body validation (customized). ## Contribution -You should be familiar with: -- [Git](https://git-scm.com/) -- [NodeJS](https://nodejs.org/) -- [TypeScript](https://www.typescriptlang.org/) -- [MongoDB/mongoose](http://mongoosejs.com/) -and the other technologies we use +You should be familiar with: + +- [Git](https://git-scm.com/) +- [NodeJS](https://nodejs.org/) +- [TypeScript](https://www.typescriptlang.org/) +- [MongoDB/mongoose](http://mongoosejs.com/) + +and the other technologies we use ### Getting Started + Clone the Repository: + ```bash git clone https://github.com/fosscord/fosscord-api cd discord-server ``` + #### Install (dev)dependencies: + ```bash npm install npm install --only=dev ``` + #### Starting: + ``` npm start ``` + #### Debugging: + **Vscode:** -The Launch file configuration is in ``./vscode/launch.json``, -so you can just debug the server by pressing ``F5`` or the ``> Launch Server`` button +The Launch file configuration is in `./vscode/launch.json`, +so you can just debug the server by pressing `F5` or the `> Launch Server` button diff --git a/package-lock.json b/package-lock.json index bd7cb447397fa6459b05e1165541310f676df729..455aa3c4d930611124713abb98c3956760181ebd 100644 GIT binary patch delta 1247 zcma)*TTEP46o#|U>~q=Yz%Vi}WwZQq#tSJBH(B{S7@nn)MV#d>uT&(wwz3RjlJ4o&9~*{*mHC8 zYwNOXS@vpMwk^|&58Ju3gcNxlV(?rCSFf3hD?2_sHTj^xh{F=OSO_OA#DdkuexyJF4w&K!B+S(q%*n4#1z#Xyms(_&sM zxBLXm*KrIUKCTIa`2ErV6iiS9%#ZPU(B*Q$_)87*g`09vs7&dNsuXVL>yCHrzNx)-0+}jG4^mAJ+=$zqxV6YfQw)8bz;jrO~W?-j! zHpL9VVYD1RF;a>n;WUwfwp-AuQ@_j#hJ%&7cZO*VgL7+~!7o6`OhYI-lIfO2_&G|9gh``(U9DJ*!GTCQ zo2ahmqu^jYRnS>U*DL*fKHDdU!`N{y0dybJSonOFF2kc5Vg`4kyb14qM5BnhF`SCt z>KEZ~vx52GJs;-3raW%CO_zM-rvlsTJ9L8L>-T7`tOgI6Vd6tsi{Jf7_wU8BBHo$- zrGN5?IO^ng5cunmCg6mLE&TLLc+<_Vi&%M|zxjj%JW^%MToBYuGxpTcLwQJD*zMwxek|y%# zP7yT*REovu9A7j>s46BN)OCpMDcE#EEY-q+VabSBKM)%?qx+6{R}cG$Xu|6&V(WG| zQ{tV@(=LRfm%nfAVBN_cfwjw;AoNla?3YIOglM)1V3nmO&W(EQ!PqGLYw@6yF-DhSl;N7^w zLJ2t6I3DB%F&I22gfJX>z`8W}$11Ch!?%ob!mfbU%K;@YTP>S>$^Q#>p%z2Yni_#d(AC`H1u16jvu)6q4 on62_d1|J*|cxGI=q=%p>s`LJDQ4epH^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B=z9!huH+JzVxX7M~V=i#zUyC0wutmOjJeRtzE9 zOeaQp-_ZT>FcBcX;Y>`d;Y9t^ca2`b`!rL)U3EX}f>mq;3^p>;KXCIdbx&JB+kKeRE#6LJ0*)F`?x)TrGr(oP0=FAQvhekRCEUGV;)QFvXTQF||~ ziSvxY_d|@L>q3l(zOyYBdC$%WR#DHN8W=kLTmIC{%6A3*V08{I^j;ma`v%?&|JrMS zM_&Vc$u`(y5pIU>-^<_ef5my2)oq8sPM&DMdFYq!<=@GJU;dWAc>sBUfjnDazlZ5& zn9t8Zq2P_uzxpbFCpz?b4DH!I>}Ia`@RFVLV;DQi9i^)e@vmmRH{wIB5Ak74YbCfa zwKnLdk*!BwWMSY0K2B>-@F%AK=)fnqF2D!+dt^Ot8Mq93n9OR8>t{mhIiL8GXFhO< z;BG&1hQs}T@FUm|SS>#WRmvX&{78ZD@OGXfiR2XX%(pTXIPoAyv;t|kpu2-}=_;vV zF?%}wLYw7cx?M=8yvijvkQE?_;Psoa%>E4yHPL~T_66^57x)I0tNinex41m zPYSjiImI7=+wI6!@O(nRgALbncEGV7hr5z!e;YRN1k)hDgCkp^D+9fR)&;M$fqa@X z4Zo^j+u-U0LOw{Z=Zw&@KrwU#V7+(j-O^~#BaR781YyCl6UC}UwJdk+qGVNrEjz&!_scx zzf=n^^GY!5z2oMc6(CL)H*6^JOakW88T-^`C;L;i=tBaqHAD|G#kN zH-Z$saIx@v9C(()_@?vVmC(B2m0@AB%vX+ng1=|^nn>^+y~hqs!_!YNf_v}ckVBb& zd>3ascy1Do(7)Lu{A)RVY`^eZTZX3g$-3}^)9(?kOq?ICzzMH#63SoZz7M!E3O~A8 H`22qZ;sM?W diff --git a/package.json b/package.json index 2047cb37..b8e591df 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { - "name": "fosscord-api", + "name": "@fosscord/api", "version": "1.0.0", "description": "This repository contains the HTTP API Server", - "main": "index.js", + "main": "dist/Server.js", + "types": "dist/Server.d.ts", "scripts": { "test": "jest", "test:watch": "jest --watch", - "start": "npm run build:util && npm run build && node dist/", + "start": "npm run build:util && npm run build && node dist/start", "build": "tsc -b .", "build:util": "tsc -b ./node_modules/fosscord-server-util/", "postinstall": "npm i github:fosscord/fosscord-server-util && patch-package" @@ -15,14 +16,21 @@ "type": "git", "url": "git+https://github.com/fosscord/fosscord-api.git" }, - "keywords": [], - "author": "", + "keywords": [ + "discord", + "fosscord", + "fosscord-api", + "discord open source", + "discord-open-source" + ], + "author": "Fosscord", "license": "ISC", "bugs": { "url": "https://github.com/fosscord/fosscord-api/issues" }, "homepage": "https://github.com/fosscord/fosscord-api#readme", "dependencies": { + "@fosscord/fosscord-server-util": "github:fosscord/fosscord-server-util", "@types/jest": "^26.0.22", "bcrypt": "^5.0.0", "body-parser": "^1.19.0", diff --git a/src/Server.ts b/src/Server.ts index 19a0fc5f..03222d4a 100644 --- a/src/Server.ts +++ b/src/Server.ts @@ -13,21 +13,21 @@ import { BodyParser } from "./middlewares/BodyParser"; import { Router } from "express"; import fetch from "node-fetch"; -export interface DiscordServerOptions extends ServerOptions {} +export interface FosscordServerOptions extends ServerOptions {} declare global { namespace Express { interface Request { // @ts-ignore - server: DiscordServer; + server: FosscordServer; } } } -export class DiscordServer extends Server { - public options: DiscordServerOptions; +export class FosscordServer extends Server { + public options: FosscordServerOptions; - constructor(opts?: Partial) { + constructor(opts?: Partial) { // @ts-ignore super({ ...opts, errorHandler: false, jsonBody: false }); } diff --git a/src/index.ts b/src/index.ts index 471bf90c..c6417126 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,33 +1,19 @@ -process.on("uncaughtException", console.error); -process.on("unhandledRejection", console.error); - -import "missing-native-js-functions"; -import { config } from "dotenv"; -config(); -import { DiscordServer } from "./Server"; -import cluster from "cluster"; -import os from "os"; -const cores = os.cpus().length; - -if (cluster.isMaster && process.env.production == "true") { - console.log(`Primary ${process.pid} is running`); - - // Fork workers. - for (let i = 0; i < cores; i++) { - cluster.fork(); - } - - cluster.on("exit", (worker, code, signal) => { - console.log(`worker ${worker.process.pid} died, restart worker`); - cluster.fork(); - }); -} else { - var port = Number(process.env.PORT); - if (isNaN(port)) port = 1000; - - const server = new DiscordServer({ port }); - server.start().catch(console.error); - - // @ts-ignore - global.server = server; -} +export * from "./Server"; +export * from "./middlewares/"; +export * from "./schema/Ban"; +export * from "./schema/Channel"; +export * from "./schema/Guild"; +export * from "./schema/Invite"; +export * from "./schema/Message"; +export * from "./util/Captcha"; +export * from "./util/Config"; +export * from "./util/Constants"; +export * from "./util/Event"; +export * from "./util/instanceOf"; +export * from "./util/Event"; +export * from "./util/instanceOf"; +export * from "./util/Member"; +export * from "./util/RandomInviteID"; +export * from "./util/String"; +export * from "./util/User"; +export { check as checkPassword } from "./util/passwordStrength"; diff --git a/src/middlewares/index.ts b/src/middlewares/index.ts index e3332f07..6a2993a7 100644 --- a/src/middlewares/index.ts +++ b/src/middlewares/index.ts @@ -1,4 +1,6 @@ -import { Authentication } from "./Authentication"; -import { GlobalRateLimit } from "./GlobalRateLimit"; - -export { Authentication, GlobalRateLimit }; +export * from "./GlobalRateLimit"; +export * from "./Authentication"; +export * from "./BodyParser"; +export * from "./CORS"; +export * from "./ErrorHandler"; +export * from "./RateLimit"; diff --git a/src/start.ts b/src/start.ts new file mode 100644 index 00000000..11e15941 --- /dev/null +++ b/src/start.ts @@ -0,0 +1,33 @@ +process.on("uncaughtException", console.error); +process.on("unhandledRejection", console.error); + +import "missing-native-js-functions"; +import { config } from "dotenv"; +config(); +import { FosscordServer } from "./Server"; +import cluster from "cluster"; +import os from "os"; +const cores = os.cpus().length; + +if (cluster.isMaster && process.env.production == "true") { + console.log(`Primary ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < cores; i++) { + cluster.fork(); + } + + cluster.on("exit", (worker, code, signal) => { + console.log(`worker ${worker.process.pid} died, restart worker`); + cluster.fork(); + }); +} else { + var port = Number(process.env.PORT); + if (isNaN(port)) port = 1000; + + const server = new FosscordServer({ port }); + server.start().catch(console.error); + + // @ts-ignore + global.server = server; +} diff --git a/src/test/rethink_test.ts b/src/test/rethink_test.ts.disabled similarity index 100% rename from src/test/rethink_test.ts rename to src/test/rethink_test.ts.disabled diff --git a/src/util/Captcha.ts b/src/util/Captcha.ts index e69de29b..cb0ff5c3 100644 --- a/src/util/Captcha.ts +++ b/src/util/Captcha.ts @@ -0,0 +1 @@ +export {}; diff --git a/src/util/Config.ts b/src/util/Config.ts index 60d83e1a..1a038b1d 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -16,7 +16,7 @@ export default { setAll: Config.setAll, }; -export interface RateLimit { +export interface RateLimitOptions { count: number; timespan: number; } @@ -62,8 +62,8 @@ export interface DefaultOptions { }; routes: { auth?: { - login?: RateLimit; - register?: RateLimit; + login?: RateLimitOptions; + register?: RateLimitOptions; }; channel?: {}; // TODO: rate limit configuration for all routes diff --git a/tsconfig.json b/tsconfig.json index 0976d3f7..1d4f4886 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,5 @@ { - "include": ["src/**/*.ts"], + "include": ["src/**/*.ts", "src/test/rethink_test.ts.disabled"], "compilerOptions": { /* Visit https://aka.ms/tsconfig.json to read more about this file */