diff options
Diffstat (limited to 'dist')
-rw-r--r-- | dist/Server.d.ts | 29 | ||||
-rw-r--r-- | dist/Server.js | 92 | ||||
-rw-r--r-- | dist/Snowflake.d.ts | 85 | ||||
-rw-r--r-- | dist/Snowflake.js | 131 | ||||
-rw-r--r-- | dist/Util.d.ts | 8 | ||||
-rw-r--r-- | dist/Util.js | 45 | ||||
-rw-r--r-- | dist/index.d.ts | 1 | ||||
-rw-r--r-- | dist/index.js | 15 | ||||
-rw-r--r-- | dist/routes/attachments.d.ts | 2 | ||||
-rw-r--r-- | dist/routes/attachments.js | 48 | ||||
-rw-r--r-- | dist/routes/external.d.ts | 2 | ||||
-rw-r--r-- | dist/routes/external.js | 75 |
12 files changed, 533 insertions, 0 deletions
diff --git a/dist/Server.d.ts b/dist/Server.d.ts new file mode 100644 index 00000000..f4d12c65 --- /dev/null +++ b/dist/Server.d.ts @@ -0,0 +1,29 @@ +/// <reference types="node" /> +import { Application, Router } from "express"; +import { Database } from "lambert-db"; +import { Server as HTTPServer } from "http"; +import "express-async-errors"; +export declare type ServerOptions = { + db: string; + port: number; + host: string; +}; +declare global { + namespace Express { + interface Request { + server: Server; + } + } +} +export declare class Server { + app: Application; + http: HTTPServer; + db: Database; + routes: Router[]; + options: ServerOptions; + constructor(options?: Partial<ServerOptions>); + init(): Promise<void>; + registerRoutes(root: string): Promise<any[]>; + registerRoute(root: string, file: string): any; + destroy(): Promise<void>; +} diff --git a/dist/Server.js b/dist/Server.js new file mode 100644 index 00000000..72046dfc --- /dev/null +++ b/dist/Server.js @@ -0,0 +1,92 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Server = void 0; +const express_1 = __importDefault(require("express")); +const lambert_db_1 = require("lambert-db"); +const Util_1 = require("./Util"); +require("express-async-errors"); +const log = console.log; +console.log = (content) => { + log(`[${new Date().toTimeString().split(" ")[0]}]`, content); +}; +class Server { + constructor(options = { port: 3000, host: "0.0.0.0" }) { + this.app = express_1.default(); + this.db = new lambert_db_1.MongoDatabase(options === null || options === void 0 ? void 0 : options.db); + this.options = options; + } + init() { + return __awaiter(this, void 0, void 0, function* () { + yield this.db.init(); + console.log("[Database] connected..."); + yield new Promise((res, rej) => { + this.http = this.app.listen(this.options.port, this.options.host, () => res(null)); + }); + this.routes = yield this.registerRoutes(__dirname + "/routes/"); + }); + } + registerRoutes(root) { + return __awaiter(this, void 0, void 0, function* () { + this.app.use((req, res, next) => { + req.server = this; + next(); + }); + const routes = yield Util_1.traverseDirectory({ dirname: root, recursive: true }, this.registerRoute.bind(this, root)); + this.app.use((err, req, res, next) => { + res.status(400).send(err); + next(err); + }); + return routes; + }); + } + registerRoute(root, file) { + var _a, _b; + if (root.endsWith("/") || root.endsWith("\\")) + root = root.slice(0, -1); // removes slash at the end of the root dir + let path = file.replace(root, ""); // remove root from path and + path = path.split(".").slice(0, -1).join("."); // trancate .js/.ts file extension of path + if (path.endsWith("/index")) + path = path.slice(0, -6); // delete index from path + try { + var router = require(file); + if (router.router) + router = router.router; + if (router.default) + router = router.default; + if (!router || ((_b = (_a = router === null || router === void 0 ? void 0 : router.prototype) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) !== "router") + throw `File doesn't export any default router`; + this.app.use(path, router); + console.log(`[Routes] ${path} registerd`); + return router; + } + catch (error) { + console.error(new Error(`[Server] ¯\\_(ツ)_/¯ Failed to register route ${path}: ${error}`)); + } + } + destroy() { + return __awaiter(this, void 0, void 0, function* () { + yield this.db.destroy(); + yield new Promise((res, rej) => { + this.http.close((err) => { + if (err) + return rej(err); + return res(""); + }); + }); + }); + } +} +exports.Server = Server; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU2VydmVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL1NlcnZlci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7QUFBQSxzREFBd0Y7QUFDeEYsMkNBQXFEO0FBRXJELGlDQUEyQztBQUUzQyxnQ0FBOEI7QUFFOUIsTUFBTSxHQUFHLEdBQUcsT0FBTyxDQUFDLEdBQUcsQ0FBQztBQUN4QixPQUFPLENBQUMsR0FBRyxHQUFHLENBQUMsT0FBTyxFQUFFLEVBQUU7SUFDekIsR0FBRyxDQUFDLElBQUksSUFBSSxJQUFJLEVBQUUsQ0FBQyxZQUFZLEVBQUUsQ0FBQyxLQUFLLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQyxDQUFDLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztBQUM5RCxDQUFDLENBQUM7QUFnQkYsTUFBYSxNQUFNO0lBT2xCLFlBQVksVUFBa0MsRUFBRSxJQUFJLEVBQUUsSUFBSSxFQUFFLElBQUksRUFBRSxTQUFTLEVBQUU7UUFDNUUsSUFBSSxDQUFDLEdBQUcsR0FBRyxpQkFBTyxFQUFFLENBQUM7UUFDckIsSUFBSSxDQUFDLEVBQUUsR0FBRyxJQUFJLDBCQUFhLENBQUMsT0FBTyxhQUFQLE9BQU8sdUJBQVAsT0FBTyxDQUFFLEVBQUUsQ0FBQyxDQUFDO1FBQ3pDLElBQUksQ0FBQyxPQUFPLEdBQUcsT0FBd0IsQ0FBQztJQUN6QyxDQUFDO0lBRUssSUFBSTs7WUFDVCxNQUFNLElBQUksQ0FBQyxFQ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QUM7S0FBQTtDQUNEO0FBbEVELHdCQWtFQyJ9 \ No newline at end of file diff --git a/dist/Snowflake.d.ts b/dist/Snowflake.d.ts new file mode 100644 index 00000000..17effce6 --- /dev/null +++ b/dist/Snowflake.d.ts @@ -0,0 +1,85 @@ +export = SnowflakeUtil; +/** + * A container for useful snowflake-related methods. + */ +declare class SnowflakeUtil { + /** + * A Twitter snowflake, except the epoch is 2015-01-01T00:00:00.000Z + * ``` + * If we have a snowflake '266241948824764416' we can represent it as binary: + * + * 64 22 17 12 0 + * 000000111011000111100001101001000101000000 00001 00000 000000000000 + * number of ms since Discord epoch worker pid increment + * ``` + * @typedef {string} Snowflake + */ + /** + * Transforms a snowflake from a decimal string to a bit string. + * @param {Snowflake} num Snowflake to be transformed + * @returns {string} + * @private + */ + private static idToBinary; + /** + * Transforms a snowflake from a bit string to a decimal string. + * @param {string} num Bit string to be transformed + * @returns {Snowflake} + * @private + */ + private static binaryToID; + /** + * Generates a Discord snowflake. + * <info>This hardcodes the worker ID as 1 and the process ID as 0.</info> + * @param {number|Date} [timestamp=Date.now()] Timestamp or date of the snowflake to generate + * @returns {Snowflake} The generated snowflake + */ + static generate(timestamp?: number | Date | undefined): string; + /** + * A deconstructed snowflake. + * @typedef {Object} DeconstructedSnowflake + * @property {number} timestamp Timestamp the snowflake was created + * @property {Date} date Date the snowflake was created + * @property {number} workerID Worker ID in the snowflake + * @property {number} processID Process ID in the snowflake + * @property {number} increment Increment in the snowflake + * @property {string} binary Binary representation of the snowflake + */ + /** + * Deconstructs a Discord snowflake. + * @param {Snowflake} snowflake Snowflake to deconstruct + * @returns {DeconstructedSnowflake} Deconstructed snowflake + */ + static deconstruct(snowflake: string): { + /** + * Timestamp the snowflake was created + */ + timestamp: number; + /** + * Date the snowflake was created + */ + date: Date; + /** + * Worker ID in the snowflake + */ + workerID: number; + /** + * Process ID in the snowflake + */ + processID: number; + /** + * Increment in the snowflake + */ + increment: number; + /** + * Binary representation of the snowflake + */ + binary: string; + }; + /** + * Discord's epoch value (2015-01-01T00:00:00.000Z). + * @type {number} + * @readonly + */ + static readonly get EPOCH(): number; +} diff --git a/dist/Snowflake.js b/dist/Snowflake.js new file mode 100644 index 00000000..4a50845e --- /dev/null +++ b/dist/Snowflake.js @@ -0,0 +1,131 @@ +// @ts-nocheck +// github.com/discordjs/discord.js/blob/master/src/util/Snowflake.js +"use strict"; +// Discord epoch (2015-01-01T00:00:00.000Z) +const EPOCH = 1420070400000; +let INCREMENT = 0; +/** + * A container for useful snowflake-related methods. + */ +class SnowflakeUtil { + constructor() { + throw new Error(`The ${this.constructor.name} class may not be instantiated.`); + } + /** + * A Twitter snowflake, except the epoch is 2015-01-01T00:00:00.000Z + * ``` + * If we have a snowflake '266241948824764416' we can represent it as binary: + * + * 64 22 17 12 0 + * 000000111011000111100001101001000101000000 00001 00000 000000000000 + * number of ms since Discord epoch worker pid increment + * ``` + * @typedef {string} Snowflake + */ + /** + * Transforms a snowflake from a decimal string to a bit string. + * @param {Snowflake} num Snowflake to be transformed + * @returns {string} + * @private + */ + static idToBinary(num) { + let bin = ""; + let high = parseInt(num.slice(0, -10)) || 0; + let low = parseInt(num.slice(-10)); + while (low > 0 || high > 0) { + bin = String(low & 1) + bin; + low = Math.floor(low / 2); + if (high > 0) { + low += 5000000000 * (high % 2); + high = Math.floor(high / 2); + } + } + return bin; + } + /** + * Transforms a snowflake from a bit string to a decimal string. + * @param {string} num Bit string to be transformed + * @returns {Snowflake} + * @private + */ + static binaryToID(num) { + let dec = ""; + while (num.length > 50) { + const high = parseInt(num.slice(0, -32), 2); + const low = parseInt((high % 10).toString(2) + num.slice(-32), 2); + dec = (low % 10).toString() + dec; + num = + Math.floor(high / 10).toString(2) + + Math.floor(low / 10) + .toString(2) + .padStart(32, "0"); + } + num = parseInt(num, 2); + while (num > 0) { + dec = (num % 10).toString() + dec; + num = Math.floor(num / 10); + } + return dec; + } + /** + * Generates a Discord snowflake. + * <info>This hardcodes the worker ID as 1 and the process ID as 0.</info> + * @param {number|Date} [timestamp=Date.now()] Timestamp or date of the snowflake to generate + * @returns {Snowflake} The generated snowflake + */ + static generate(timestamp = Date.now()) { + if (timestamp instanceof Date) + timestamp = timestamp.getTime(); + if (typeof timestamp !== "number" || isNaN(timestamp)) { + throw new TypeError(`"timestamp" argument must be a number (received ${isNaN(timestamp) ? "NaN" : typeof timestamp})`); + } + if (INCREMENT >= 4095) + INCREMENT = 0; + const BINARY = `${(timestamp - EPOCH).toString(2).padStart(42, "0")}0000100000${(INCREMENT++) + .toString(2) + .padStart(12, "0")}`; + return SnowflakeUtil.binaryToID(BINARY); + } + /** + * A deconstructed snowflake. + * @typedef {Object} DeconstructedSnowflake + * @property {number} timestamp Timestamp the snowflake was created + * @property {Date} date Date the snowflake was created + * @property {number} workerID Worker ID in the snowflake + * @property {number} processID Process ID in the snowflake + * @property {number} increment Increment in the snowflake + * @property {string} binary Binary representation of the snowflake + */ + /** + * Deconstructs a Discord snowflake. + * @param {Snowflake} snowflake Snowflake to deconstruct + * @returns {DeconstructedSnowflake} Deconstructed snowflake + */ + static deconstruct(snowflake) { + const BINARY = SnowflakeUtil.idToBinary(snowflake).toString(2).padStart(64, "0"); + const res = { + timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH, + workerID: parseInt(BINARY.substring(42, 47), 2), + processID: parseInt(BINARY.substring(47, 52), 2), + increment: parseInt(BINARY.substring(52, 64), 2), + binary: BINARY, + }; + Object.defineProperty(res, "date", { + get: function get() { + return new Date(this.timestamp); + }, + enumerable: true, + }); + return res; + } + /** + * Discord's epoch value (2015-01-01T00:00:00.000Z). + * @type {number} + * @readonly + */ + static get EPOCH() { + return EPOCH; + } +} +module.exports = SnowflakeUtil; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU25vd2ZsYWtlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL1Nub3dmbGFrZS5qcyJdLCJuYW1lcyI6W10sIm1hcHBp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ILE9BQU8sR0FBRyxDQUFDO0lBQ1osQ0FBQztJQUVEOzs7O09BSUc7SUFDSCxNQUFNLEtBQUssS0FBSztRQUNmLE9BQU8sS0FBSyxDQUFDO0lBQ2QsQ0FBQztDQUNEO0FBRUQsTUFBTSxDQUFDLE9BQU8sR0FBRyxhQUFhLENBQUMifQ== \ No newline at end of file diff --git a/dist/Util.d.ts b/dist/Util.d.ts new file mode 100644 index 00000000..c0a33362 --- /dev/null +++ b/dist/Util.d.ts @@ -0,0 +1,8 @@ +import "missing-native-js-functions"; +export interface traverseDirectoryOptions { + dirname: string; + filter?: RegExp; + excludeDirs?: RegExp; + recursive?: boolean; +} +export declare function traverseDirectory<T>(options: traverseDirectoryOptions, action: (path: string) => T): Promise<T[]>; diff --git a/dist/Util.js b/dist/Util.js new file mode 100644 index 00000000..45421484 --- /dev/null +++ b/dist/Util.js @@ -0,0 +1,45 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.traverseDirectory = void 0; +const promises_1 = __importDefault(require("fs/promises")); +require("missing-native-js-functions"); +const DEFAULT_EXCLUDE_DIR = /^\./; +const DEFAULT_FILTER = /^([^\.].*)\.js$/; +function traverseDirectory(options, action) { + return __awaiter(this, void 0, void 0, function* () { + if (!options.filter) + options.filter = DEFAULT_FILTER; + if (!options.excludeDirs) + options.excludeDirs = DEFAULT_EXCLUDE_DIR; + const routes = yield promises_1.default.readdir(options.dirname); + const promises = routes.map((file) => __awaiter(this, void 0, void 0, function* () { + const path = options.dirname + file; + const stat = yield promises_1.default.lstat(path); + if (path.match(options.excludeDirs)) + return; + if (stat.isFile() && path.match(options.filter)) { + return action(path); + } + else if (options.recursive && stat.isDirectory()) { + return traverseDirectory(Object.assign(Object.assign({}, options), { dirname: path + "/" }), action); + } + })); + const result = yield Promise.all(promises); + const t = result.flat(); + return t.filter((x) => x != undefined); + }); +} +exports.traverseDirectory = traverseDirectory; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVXRpbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9VdGlsLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7OztBQUFBLDJEQUE2QjtBQUM3Qix1Q0FBcUM7QUFTckMsTUFBTSxtQkFBbUIsR0FBRyxLQUFLLENBQUM7QUFDbEMsTUFBTSxjQUFjLEdBQUcsaUJBQWlCLENBQUM7QUFFekMsU0FBc0IsaUJBQWlCLENBQ3RDLE9BQWlDLEVBQ2pDLE1BQTJCOztRQUUzQixJQUFJLENBQUMsT0FBTyxDQUFDLE1BQU07WUFBRSxPQUFPLENBQUMsTUFBTSxHQUFHLGNBQWMsQ0FBQztRQUNyRCxJQUFJLENBQUMsT0FBTyxDQUFDLFdBQVc7WUFBRSxPQUFPLENBQUMsV0FBVyxHQUFHLG1CQUFtQixDQUFDO1FBRXBFLE1BQU0sTUFBTSxHQUFHLE1BQU0sa0JBQUUsQ0FBQyxPQUFPLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ2pELE1BQU0sUUFBUSxHQUFtQyxNQUFNLENBQUMsR0FBRyxDQUFDLENBQU8sSUFBSSxFQUFFLEVBQUU7WUFDMUUsTUFBTSxJQUFJLEdBQUcsT0FBTyxDQUFDLE9BQU8sR0FBRyxJQUFJLENBQUM7WUFDcEMsTUFBTSxJQUFJLEdBQUcsTUFBTSxrQkFBRSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQztZQUNsQyxJQUFJLElBQUksQ0FBQyxLQUFLLENBQVMsT0FBTyxDQUFDLFdBQVcsQ0FBQztnQkFBRSxPQUFPO1lBRXBELElBQUksSUFBSSxDQUFDLE1BQU0sRUFBRSxJQUFJLElBQUksQ0FBQyxLQUFLLENBQVMsT0FBTyxDQUFDLE1BQU0sQ0FBQyxFQUFFO2dCQUN4RCxPQUFPLE1BQU0sQ0FBQyxJQUFJLENBQUMsQ0FBQzthQUNwQjtpQkFBTSxJQUFJLE9BQU8sQ0FBQyxTQUFTLElBQUksSUFBSSxDQUFDLFdBQVcsRUFBRSxFQUFFO2dCQUNuRCxPQUFPLGlCQUFpQixpQ0FBTSxPQUFPLEtBQUUsT0FBTyxFQUFFLElBQUksR0FBRyxHQUFHLEtBQUksTUFBTSxDQUFDLENBQUM7YUFDdEU7UUFDRixDQUFDLENBQUEsQ0FBQyxDQUFDO1FBQ0gsTUFBTSxNQUFNLEdBQUcsTUFBTSxPQUFPLENBQUMsR0FBRyxDQUFDLFFBQVEsQ0FBQyxDQUFDO1FBRTNDLE1BQU0sQ0FBQyxHQUFzQixNQUFNLENBQUMsSUFBSSxFQUFFLENBQUM7UUFFM0MsT0FBWSxDQUFDLENBQUMsTUFBTSxDQUFDLENBQUMsQ0FBQyxFQUFFLEVBQUUsQ0FBQyxDQUFDLElBQUksU0FBUyxDQUFDLENBQUM7SUFDN0MsQ0FBQztDQUFBO0FBeEJELDhDQXdCQyJ9 \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 00000000..792fcefd --- /dev/null +++ b/dist/index.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const Server_1 = require("./Server"); +const server = new Server_1.Server(); +server + .init() + .then(() => { + console.log("[Server] started on :" + server.options.port); +}) + .catch((e) => console.error("[Server] Error starting: ", e)); +//// server +//// .destroy() +//// .then(() => console.log("[Server] closed.")) +//// .catch((e) => console.log("[Server] Error closing: ", e)); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxxQ0FBa0M7QUFFbEMsTUFBTSxNQUFNLEdBQUcsSUFBSSxlQUFNLEVBQUUsQ0FBQztBQUM1QixNQUFNO0tBQ0osSUFBSSxFQUFFO0tBQ04sSUFBSSxDQUFDLEdBQUcsRUFBRTtJQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsdUJBQXVCLEdBQUcsTUFBTSxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztBQUM1RCxDQUFDLENBQUM7S0FDRCxLQUFLLENBQUMsQ0FBQyxDQUFDLEVBQUUsRUFBRSxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsMkJBQTJCLEVBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQztBQUU5RCxXQUFXO0FBQ1gsZ0JBQWdCO0FBQ2hCLGtEQUFrRDtBQUNsRCwrREFBK0QifQ== \ No newline at end of file diff --git a/dist/routes/attachments.d.ts b/dist/routes/attachments.d.ts new file mode 100644 index 00000000..ae2ab413 --- /dev/null +++ b/dist/routes/attachments.d.ts @@ -0,0 +1,2 @@ +declare const router: import("express-serve-static-core").Router; +export default router; diff --git a/dist/routes/attachments.js b/dist/routes/attachments.js new file mode 100644 index 00000000..4b639448 --- /dev/null +++ b/dist/routes/attachments.js @@ -0,0 +1,48 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = require("express"); +const multer_1 = __importDefault(require("multer")); +const Snowflake_1 = __importDefault(require("../Snowflake")); +const multer_ = multer_1.default(); +const router = express_1.Router(); +router.post("/:filename", multer_.single("attachment"), (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { buffer, mimetype } = req.file; + const { filename } = req.params; + const { db } = req.server; + const File = { + filename, + file: buffer.toString("base64"), + id: Snowflake_1.default.generate(), + type: mimetype, + }; + if (!(yield db.data.attachments.push(File))) + throw new Error("Error uploading file"); + return res.status(201).send({ success: true, message: "attachment uploaded", id: File.id, filename }); +})); +router.get("/:hash/:filename", (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { db } = req.server; + const { hash, filename } = req.params; + const File = yield db.data.attachments({ id: hash, filename: filename }).get(); + res.set("Content-Type", File.type); + return res.send(Buffer.from(File.file, "base64")); +})); +router.delete("/:hash/:filename", (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { hash, filename } = req.params; + const { db } = req.server; + yield db.data.attachments({ id: hash, filename: filename }).delete(); + return res.send({ success: true, message: "attachment deleted" }); +})); +exports.default = router; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXR0YWNobWVudHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvcm91dGVzL2F0dGFjaG1lbnRzLnRzIl0sIm5hbWVzIjpbXSwibW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VBQUUsRUFBRSxJQUFJLEVBQUUsUUFBUSxFQUFFLFFBQVEsRUFBRSxDQUFDLENBQUMsTUFBTSxFQUFFLENBQUM7SUFDckUsT0FBTyxHQUFHLENBQUMsSUFBSSxDQUFDLEVBQUUsT0FBTyxFQUFFLElBQUksRUFBRSxPQUFPLEVBQUUsb0JBQW9CLEVBQUUsQ0FBQyxDQUFDO0FBQ25FLENBQUMsQ0FBQSxDQUFDLENBQUM7QUFFSCxrQkFBZSxNQUFNLENBQUMifQ== \ No newline at end of file diff --git a/dist/routes/external.d.ts b/dist/routes/external.d.ts new file mode 100644 index 00000000..ae2ab413 --- /dev/null +++ b/dist/routes/external.d.ts @@ -0,0 +1,2 @@ +declare const router: import("express-serve-static-core").Router; +export default router; diff --git a/dist/routes/external.js b/dist/routes/external.js new file mode 100644 index 00000000..751388f1 --- /dev/null +++ b/dist/routes/external.js @@ -0,0 +1,75 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const body_parser_1 = __importDefault(require("body-parser")); +const express_1 = require("express"); +const node_fetch_1 = __importDefault(require("node-fetch")); +const cheerio_1 = __importDefault(require("cheerio")); +const btoa_1 = __importDefault(require("btoa")); +const url_1 = require("url"); +const router = express_1.Router(); +const DEFAULT_FETCH_OPTIONS = { + redirect: "follow", + follow: 1, + headers: { + "user-agent": "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)", + }, + size: 1024 * 1024 * 8, + compress: true, + method: "GET", +}; +router.post("/", body_parser_1.default.json(), (req, res) => __awaiter(void 0, void 0, void 0, function* () { + if (!req.body) + throw new Error("Invalid Body (url missing) \nExample: url:https://discord.com"); + const { db } = req.server; + const { url } = req.body; + const ID = btoa_1.default(url); + const cache = yield db.data.crawler({ id: ID }).get(); + if (cache) + return res.send(cache); + try { + const request = yield node_fetch_1.default(url, DEFAULT_FETCH_OPTIONS); + const text = yield request.text(); + const ツ = cheerio_1.default.load(text); + const ogTitle = ツ('meta[property="og:title"]').attr("content"); + const ogDescription = ツ('meta[property="og:description"]').attr("content"); + const ogImage = ツ('meta[property="og:image"]').attr("content"); + const ogUrl = ツ('meta[property="og:url"]').attr("content"); + const ogType = ツ('meta[property="og:type"]').attr("content"); + const filename = new url_1.URL(url).host.split(".")[0]; + const ImageResponse = yield node_fetch_1.default(ogImage, DEFAULT_FETCH_OPTIONS); + const ImageType = ImageResponse.headers.get("content-type"); + const ImageExtension = ImageType === null || ImageType === void 0 ? void 0 : ImageType.split("/")[1]; + const ImageResponseBuffer = (yield ImageResponse.buffer()).toString("base64"); + const cachedImage = `/external/${ID}/${filename}.${ImageExtension}`; + yield db.data.externals.push({ image: ImageResponseBuffer, id: ID, type: ImageType }); + const new_cache_entry = { id: ID, ogTitle, ogDescription, cachedImage, ogUrl, ogType }; + yield db.data.crawler.push(new_cache_entry); + res.send(new_cache_entry); + } + catch (error) { + console.log(error); + throw new Error("Couldn't fetch website"); + } +})); +router.get("/:id/:filename", (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const { db } = req.server; + const { id, filename } = req.params; + const { image, type } = yield db.data.externals({ id: id }).get(); + const imageBuffer = Buffer.from(image, "base64"); + res.set("Content-Type", type); + res.send(imageBuffer); +})); +exports.default = router; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZXh0ZXJuYWwuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6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