diff options
Diffstat (limited to 'util/src')
-rw-r--r-- | util/src/entities/BaseClass.ts | 1 | ||||
-rw-r--r-- | util/src/entities/Channel.ts | 2 | ||||
-rw-r--r-- | util/src/entities/Encryption.ts | 2 | ||||
-rw-r--r-- | util/src/entities/Member.ts | 2 | ||||
-rw-r--r-- | util/src/fetch.js | 1 | ||||
-rw-r--r-- | util/src/index.ts | 1 | ||||
-rw-r--r-- | util/src/util/AutoUpdate.ts | 3 | ||||
-rw-r--r-- | util/src/util/Config.ts | 1 | ||||
-rw-r--r-- | util/src/util/Event.ts | 4 | ||||
-rw-r--r-- | util/src/util/FieldError.ts | 2 | ||||
-rw-r--r-- | util/src/util/Permissions.ts | 13 | ||||
-rw-r--r-- | util/src/util/Rights.ts | 13 | ||||
-rw-r--r-- | util/src/util/cdn.ts | 4 | ||||
-rw-r--r-- | util/src/util/imports/Checks.ts | 125 | ||||
-rw-r--r-- | util/src/util/imports/HTTPError.ts | 5 | ||||
-rw-r--r-- | util/src/util/imports/index.ts | 2 | ||||
-rw-r--r-- | util/src/util/index.ts | 4 |
17 files changed, 147 insertions, 38 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 7ee27e30..770824ba 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -1,7 +1,6 @@ import "reflect-metadata"; import { BaseEntity, EntityMetadata, ObjectIdColumn, PrimaryColumn, FindOptionsWhere } from "typeorm"; import { Snowflake } from "../util/Snowflake"; -import "missing-native-js-functions"; export class BaseClassWithoutId extends BaseEntity { constructor(props?: any) { diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts index 1a20153c..2a1e38a4 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts @@ -2,7 +2,7 @@ import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "ty import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; import { PublicUserProjection, User } from "./User"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from ".."; import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial, InvisibleCharacters } from "../util"; import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; import { Recipient } from "./Recipient"; diff --git a/util/src/entities/Encryption.ts b/util/src/entities/Encryption.ts index 3b82ff84..6b578d15 100644 --- a/util/src/entities/Encryption.ts +++ b/util/src/entities/Encryption.ts @@ -2,7 +2,7 @@ import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "ty import { BaseClass } from "./BaseClass"; import { Guild } from "./Guild"; import { PublicUserProjection, User } from "./User"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from ".."; import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial, InvisibleCharacters } from "../util"; import { BitField, BitFieldResolvable, BitFlag } from "../util/BitField"; import { Recipient } from "./Recipient"; diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index 0194c9a9..f6d99125 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -20,7 +20,7 @@ import { GuildMemberRemoveEvent, GuildMemberUpdateEvent, } from "../interfaces"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from ".."; import { Role } from "./Role"; import { BaseClassWithoutId } from "./BaseClass"; import { Ban, PublicGuildRelations } from "."; diff --git a/util/src/fetch.js b/util/src/fetch.js deleted file mode 100644 index 709bc440..00000000 --- a/util/src/fetch.js +++ /dev/null @@ -1 +0,0 @@ -export default fetch; diff --git a/util/src/index.ts b/util/src/index.ts index 0de1e19a..ae0f7e54 100644 --- a/util/src/index.ts +++ b/util/src/index.ts @@ -4,4 +4,3 @@ export * from "./util/index"; export * from "./interfaces/index"; export * from "./entities/index"; export * from "./dtos/index"; -export * from "./fetch.js"; diff --git a/util/src/util/AutoUpdate.ts b/util/src/util/AutoUpdate.ts index 7a4af4ff..7d020106 100644 --- a/util/src/util/AutoUpdate.ts +++ b/util/src/util/AutoUpdate.ts @@ -1,5 +1,4 @@ -import "missing-native-js-functions"; -import fetch from "../fetch"; +import fetch from "node-fetch"; import ProxyAgent from 'proxy-agent'; import readline from "readline"; import fs from "fs/promises"; diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index 31b0b35f..c358444f 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -1,4 +1,3 @@ -import "missing-native-js-functions"; import { ConfigValue, ConfigEntity, DefaultConfigOptions } from "../entities/Config"; import path from "path"; import fs from "fs"; diff --git a/util/src/util/Event.ts b/util/src/util/Event.ts index bb624051..90c24347 100644 --- a/util/src/util/Event.ts +++ b/util/src/util/Event.ts @@ -58,8 +58,8 @@ export async function listenEvent(event: string, callback: (event: EventOpts) => process.setMaxListeners(process.getMaxListeners() - 1); }; - const listener = (msg: ProcessEvent) => { - msg.type === "event" && msg.id === event && callback({ ...msg.event, cancel }); + const listener = (message: any) => { + message.type === "event" && message.id === event && callback({ ...message.event, cancel }); }; process.addListener("message", listener); diff --git a/util/src/util/FieldError.ts b/util/src/util/FieldError.ts index 406b33e8..49968e1a 100644 --- a/util/src/util/FieldError.ts +++ b/util/src/util/FieldError.ts @@ -1,5 +1,3 @@ -import "missing-native-js-functions"; - export function FieldErrors(fields: Record<string, { code?: string; message: string }>) { return new FieldError( 50035, diff --git a/util/src/util/Permissions.ts b/util/src/util/Permissions.ts index e003bf05..d3efb70c 100644 --- a/util/src/util/Permissions.ts +++ b/util/src/util/Permissions.ts @@ -1,17 +1,8 @@ // https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities"; -import { BitField } from "./BitField"; -import "missing-native-js-functions"; -import { BitFieldResolvable, BitFlag } from "./BitField"; - -let HTTPError: any; - -try { - HTTPError = require("lambert-server").HTTPError; -} catch (e) { - HTTPError = Error; -} +import { BitField, BitFieldResolvable, BitFlag } from "./BitField"; +import { HTTPError } from ".."; export type PermissionResolvable = bigint | number | Permissions | PermissionResolvable[] | PermissionString; diff --git a/util/src/util/Rights.ts b/util/src/util/Rights.ts index b7f45836..1c3906fb 100644 --- a/util/src/util/Rights.ts +++ b/util/src/util/Rights.ts @@ -1,15 +1,6 @@ -import { BitField } from "./BitField"; -import "missing-native-js-functions"; -import { BitFieldResolvable, BitFlag } from "./BitField"; +import { BitField, BitFieldResolvable, BitFlag } from "./BitField"; import { User } from "../entities"; - -let HTTPError: any; - -try { - HTTPError = require("lambert-server").HTTPError; -} catch (e) { - HTTPError = Error; -} +import { HTTPError } from ".."; export type RightResolvable = bigint | number | Rights | RightResolvable[] | RightString; diff --git a/util/src/util/cdn.ts b/util/src/util/cdn.ts index ef4ac47d..9cfe4896 100644 --- a/util/src/util/cdn.ts +++ b/util/src/util/cdn.ts @@ -1,8 +1,8 @@ import FormData from "form-data"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from ".."; import { Config } from "./Config"; import multer from "multer"; -import fetch from "../fetch" +import fetch from "node-fetch" import { nodeModuleNameResolver } from "typescript"; export async function uploadFile(path: string, file?: Express.Multer.File) { diff --git a/util/src/util/imports/Checks.ts b/util/src/util/imports/Checks.ts new file mode 100644 index 00000000..19a84171 --- /dev/null +++ b/util/src/util/imports/Checks.ts @@ -0,0 +1,125 @@ +//source: https://github.com/Flam3rboy/-server/blob/master/src/check.ts +import { NextFunction, Request, Response } from "express"; +import { HTTPError } from "."; + +const OPTIONAL_PREFIX = "$"; +const EMAIL_REGEX = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + +export function check(schema: any) { + return (req: Request, res: Response, next: NextFunction) => { + try { + const result = instanceOf(schema, req.body, { path: "body" }); + if (result === true) return next(); + throw result; + } catch (error) { + next(new HTTPError((error as any).toString(), 400)); + } + }; +} +export class Tuple { + public types: any[]; + constructor(...types: any[]) { + this.types = types; + } +} + +export class Email { + constructor(public email: string) {} + check() { + return !!this.email.match(EMAIL_REGEX); + } +} +export function instanceOf( + type: any, + value: any, + { path = "", optional = false }: { path?: string; optional?: boolean } = {} +): boolean { + if (!type) return true; // no type was specified + + if (value == null) { + if (optional) return true; + throw `${path} is required`; + } + + switch (type) { + case String: + if (typeof value === "string") return true; + throw `${path} must be a string`; + case Number: + value = Number(value); + if (typeof value === "number" && !isNaN(value)) return true; + throw `${path} must be a number`; + case BigInt: + try { + value = BigInt(value); + if (typeof value === "bigint") return true; + } catch (error) {} + throw `${path} must be a bigint`; + case Boolean: + if (value == "true") value = true; + if (value == "false") value = false; + if (typeof value === "boolean") return true; + throw `${path} must be a boolean`; + case Object: + if (typeof value === "object" && value !== null) return true; + throw `${path} must be a object`; + } + + if (typeof type === "object") { + if (Array.isArray(type)) { + if (!Array.isArray(value)) throw `${path} must be an array`; + if (!type.length) return true; // type array didn't specify any type + + return value.every((val, i) => instanceOf(type[0], val, { path: `${path}[${i}]`, optional })); + } + if (type?.constructor?.name != "Object") { + if (type instanceof Tuple) { + if ( + (<Tuple>type).types.some((x) => { + try { + return instanceOf(x, value, { path, optional }); + } catch (error) { + return false; + } + }) + ) { + return true; + } + throw `${path} must be one of ${type.types}`; + } + if (type instanceof Email) { + if ((<Email>type).check()) return true; + throw `${path} is not a valid E-Mail`; + } + if (value instanceof type) return true; + throw `${path} must be an instance of ${type}`; + } + if (typeof value !== "object") throw `${path} must be a object`; + + const diff = Object.keys(value).missing( + Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x)) + ); + + if (diff.length) throw `Unkown key ${diff}`; + + return Object.keys(type).every((key) => { + let newKey = key; + const OPTIONAL = key.startsWith(OPTIONAL_PREFIX); + if (OPTIONAL) newKey = newKey.slice(OPTIONAL_PREFIX.length); + + return instanceOf(type[key], value[newKey], { + path: `${path}.${newKey}`, + optional: OPTIONAL, + }); + }); + } else if (typeof type === "number" || typeof type === "string" || typeof type === "boolean") { + if (value === type) return true; + throw `${path} must be ${value}`; + } else if (typeof type === "bigint") { + if (BigInt(value) === type) return true; + throw `${path} must be ${value}`; + } + + return type == value; +} diff --git a/util/src/util/imports/HTTPError.ts b/util/src/util/imports/HTTPError.ts new file mode 100644 index 00000000..56a7dd55 --- /dev/null +++ b/util/src/util/imports/HTTPError.ts @@ -0,0 +1,5 @@ +export class HTTPError extends Error { + constructor(message: string, public code: number = 400) { + super(message); + } +} \ No newline at end of file diff --git a/util/src/util/imports/index.ts b/util/src/util/imports/index.ts new file mode 100644 index 00000000..4a4448ba --- /dev/null +++ b/util/src/util/imports/index.ts @@ -0,0 +1,2 @@ +export * from './Checks'; +export * from './HTTPError'; diff --git a/util/src/util/index.ts b/util/src/util/index.ts index f7a273cb..cfad53f3 100644 --- a/util/src/util/index.ts +++ b/util/src/util/index.ts @@ -19,4 +19,6 @@ export * from "./Snowflake"; export * from "./String"; export * from "./Array"; export * from "./TraverseDirectory"; -export * from "./InvisibleCharacters"; \ No newline at end of file +export * from "./InvisibleCharacters"; + +export * from "./imports/index" |