prepare npm publish

This commit is contained in:
Flam3rboy 2021-04-22 23:29:06 +02:00
parent 9705b01fdc
commit 4528a96ded
12 changed files with 251 additions and 654 deletions

1
.npmignore Normal file
View File

@ -0,0 +1 @@
!dist/

View File

@ -1,39 +1,51 @@
# Fosscord API Server # Fosscord API Server
This repository contains the HTTP API Server
This repository contains the Fosscord HTTP API Server
## Bug Tracker ## Bug Tracker
[Project Board](https://github.com/fosscord/fosscord-api/projects/2) [Project Board](https://github.com/fosscord/fosscord-api/projects/2)
## API ## 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). [lambert-server](https://www.npmjs.com/package/lambert-server) for route handling and body validation (customized).
## Contribution ## 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 ### Getting Started
Clone the Repository: Clone the Repository:
```bash ```bash
git clone https://github.com/fosscord/fosscord-api git clone https://github.com/fosscord/fosscord-api
cd discord-server cd discord-server
``` ```
#### Install (dev)dependencies: #### Install (dev)dependencies:
```bash ```bash
npm install npm install
npm install --only=dev npm install --only=dev
``` ```
#### Starting: #### Starting:
``` ```
npm start npm start
``` ```
#### Debugging: #### Debugging:
**Vscode:** **Vscode:**
The Launch file configuration is in ``./vscode/launch.json``, The Launch file configuration is in `./vscode/launch.json`,
so you can just debug the server by pressing ``F5`` or the ``> Launch Server`` button so you can just debug the server by pressing `F5` or the `> Launch Server` button

740
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,13 @@
{ {
"name": "fosscord-api", "name": "@fosscord/api",
"version": "1.0.0", "version": "1.0.0",
"description": "This repository contains the HTTP API Server", "description": "This repository contains the HTTP API Server",
"main": "index.js", "main": "dist/Server.js",
"types": "dist/Server.d.ts",
"scripts": { "scripts": {
"test": "jest", "test": "jest",
"test:watch": "jest --watch", "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": "tsc -b .",
"build:util": "tsc -b ./node_modules/fosscord-server-util/", "build:util": "tsc -b ./node_modules/fosscord-server-util/",
"postinstall": "npm i github:fosscord/fosscord-server-util && patch-package" "postinstall": "npm i github:fosscord/fosscord-server-util && patch-package"
@ -15,14 +16,21 @@
"type": "git", "type": "git",
"url": "git+https://github.com/fosscord/fosscord-api.git" "url": "git+https://github.com/fosscord/fosscord-api.git"
}, },
"keywords": [], "keywords": [
"author": "", "discord",
"fosscord",
"fosscord-api",
"discord open source",
"discord-open-source"
],
"author": "Fosscord",
"license": "ISC", "license": "ISC",
"bugs": { "bugs": {
"url": "https://github.com/fosscord/fosscord-api/issues" "url": "https://github.com/fosscord/fosscord-api/issues"
}, },
"homepage": "https://github.com/fosscord/fosscord-api#readme", "homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": { "dependencies": {
"@fosscord/fosscord-server-util": "github:fosscord/fosscord-server-util",
"@types/jest": "^26.0.22", "@types/jest": "^26.0.22",
"bcrypt": "^5.0.0", "bcrypt": "^5.0.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",

View File

@ -13,21 +13,21 @@ import { BodyParser } from "./middlewares/BodyParser";
import { Router } from "express"; import { Router } from "express";
import fetch from "node-fetch"; import fetch from "node-fetch";
export interface DiscordServerOptions extends ServerOptions {} export interface FosscordServerOptions extends ServerOptions {}
declare global { declare global {
namespace Express { namespace Express {
interface Request { interface Request {
// @ts-ignore // @ts-ignore
server: DiscordServer; server: FosscordServer;
} }
} }
} }
export class DiscordServer extends Server { export class FosscordServer extends Server {
public options: DiscordServerOptions; public options: FosscordServerOptions;
constructor(opts?: Partial<DiscordServerOptions>) { constructor(opts?: Partial<FosscordServerOptions>) {
// @ts-ignore // @ts-ignore
super({ ...opts, errorHandler: false, jsonBody: false }); super({ ...opts, errorHandler: false, jsonBody: false });
} }

View File

@ -1,33 +1,19 @@
process.on("uncaughtException", console.error); export * from "./Server";
process.on("unhandledRejection", console.error); export * from "./middlewares/";
export * from "./schema/Ban";
import "missing-native-js-functions"; export * from "./schema/Channel";
import { config } from "dotenv"; export * from "./schema/Guild";
config(); export * from "./schema/Invite";
import { DiscordServer } from "./Server"; export * from "./schema/Message";
import cluster from "cluster"; export * from "./util/Captcha";
import os from "os"; export * from "./util/Config";
const cores = os.cpus().length; export * from "./util/Constants";
export * from "./util/Event";
if (cluster.isMaster && process.env.production == "true") { export * from "./util/instanceOf";
console.log(`Primary ${process.pid} is running`); export * from "./util/Event";
export * from "./util/instanceOf";
// Fork workers. export * from "./util/Member";
for (let i = 0; i < cores; i++) { export * from "./util/RandomInviteID";
cluster.fork(); export * from "./util/String";
} export * from "./util/User";
export { check as checkPassword } from "./util/passwordStrength";
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;
}

View File

@ -1,4 +1,6 @@
import { Authentication } from "./Authentication"; export * from "./GlobalRateLimit";
import { GlobalRateLimit } from "./GlobalRateLimit"; export * from "./Authentication";
export * from "./BodyParser";
export { Authentication, GlobalRateLimit }; export * from "./CORS";
export * from "./ErrorHandler";
export * from "./RateLimit";

33
src/start.ts Normal file
View File

@ -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;
}

View File

@ -0,0 +1 @@
export {};

View File

@ -16,7 +16,7 @@ export default {
setAll: Config.setAll, setAll: Config.setAll,
}; };
export interface RateLimit { export interface RateLimitOptions {
count: number; count: number;
timespan: number; timespan: number;
} }
@ -62,8 +62,8 @@ export interface DefaultOptions {
}; };
routes: { routes: {
auth?: { auth?: {
login?: RateLimit; login?: RateLimitOptions;
register?: RateLimit; register?: RateLimitOptions;
}; };
channel?: {}; channel?: {};
// TODO: rate limit configuration for all routes // TODO: rate limit configuration for all routes

View File

@ -1,5 +1,5 @@
{ {
"include": ["src/**/*.ts"], "include": ["src/**/*.ts", "src/test/rethink_test.ts.disabled"],
"compilerOptions": { "compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Visit https://aka.ms/tsconfig.json to read more about this file */