diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/package-lock.json | 197 | ||||
-rw-r--r-- | util/package.json | 2 | ||||
-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 | ||||
-rw-r--r-- | util/tsconfig.json | 2 |
20 files changed, 262 insertions, 124 deletions
diff --git a/util/package-lock.json b/util/package-lock.json index e56a5f1b..472f1b95 100644 --- a/util/package-lock.json +++ b/util/package-lock.json @@ -10,12 +10,14 @@ "hasInstallScript": true, "license": "AGPL-3.0-only", "dependencies": { + "@types/node-fetch": "^2.6.2", "amqplib": "^0.10.0", "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.2.12", "missing-native-js-functions": "^1.2.18", "multer": "^1.4.5-lts.1", + "node-fetch": "^2.6.7", "picocolors": "^1.0.0", "proxy-agent": "^5.0.0", "reflect-metadata": "^0.1.13", @@ -1106,27 +1108,6 @@ "node": ">=8" } }, - "node_modules/@mapbox/node-pre-gyp/node_modules/node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", - "optional": true, - "peer": true, - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, "node_modules/@mapbox/node-pre-gyp/node_modules/npmlog": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz", @@ -1196,31 +1177,6 @@ "node": ">=8" } }, - "node_modules/@mapbox/node-pre-gyp/node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "optional": true, - "peer": true - }, - "node_modules/@mapbox/node-pre-gyp/node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "optional": true, - "peer": true - }, - "node_modules/@mapbox/node-pre-gyp/node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "optional": true, - "peer": true, - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - }, "node_modules/@npmcli/fs": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@npmcli/fs/-/fs-1.1.1.tgz", @@ -1495,8 +1451,29 @@ "node_modules/@types/node": { "version": "18.0.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.6.tgz", - "integrity": "sha512-/xUq6H2aQm261exT6iZTMifUySEt4GR5KX8eYyY+C4MSNPqSh9oNIP7tz2GLKTlFaiBbgZNxffoR3CVRG+cljw==", - "devOptional": true + "integrity": "sha512-/xUq6H2aQm261exT6iZTMifUySEt4GR5KX8eYyY+C4MSNPqSh9oNIP7tz2GLKTlFaiBbgZNxffoR3CVRG+cljw==" + }, + "node_modules/@types/node-fetch": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.2.tgz", + "integrity": "sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A==", + "dependencies": { + "@types/node": "*", + "form-data": "^3.0.0" + } + }, + "node_modules/@types/node-fetch/node_modules/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } }, "node_modules/@types/prettier": { "version": "2.6.3", @@ -5110,6 +5087,25 @@ "optional": true, "peer": true }, + "node_modules/node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/node-gyp": { "version": "8.4.1", "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.1.tgz", @@ -6772,6 +6768,11 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/ts-node": { "version": "10.9.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", @@ -7289,6 +7290,20 @@ "makeerror": "1.0.12" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -8407,16 +8422,6 @@ "optional": true, "peer": true }, - "node-fetch": { - "version": "2.6.7", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", - "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", - "optional": true, - "peer": true, - "requires": { - "whatwg-url": "^5.0.0" - } - }, "npmlog": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/npmlog/-/npmlog-5.0.1.tgz", @@ -8473,31 +8478,6 @@ "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } - }, - "tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "optional": true, - "peer": true - }, - "webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "optional": true, - "peer": true - }, - "whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "optional": true, - "peer": true, - "requires": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } } } }, @@ -8761,8 +8741,28 @@ "@types/node": { "version": "18.0.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.6.tgz", - "integrity": "sha512-/xUq6H2aQm261exT6iZTMifUySEt4GR5KX8eYyY+C4MSNPqSh9oNIP7tz2GLKTlFaiBbgZNxffoR3CVRG+cljw==", - "devOptional": true + "integrity": "sha512-/xUq6H2aQm261exT6iZTMifUySEt4GR5KX8eYyY+C4MSNPqSh9oNIP7tz2GLKTlFaiBbgZNxffoR3CVRG+cljw==" + }, + "@types/node-fetch": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.2.tgz", + "integrity": "sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A==", + "requires": { + "@types/node": "*", + "form-data": "^3.0.0" + }, + "dependencies": { + "form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + } + } }, "@types/prettier": { "version": "2.6.3", @@ -11589,6 +11589,14 @@ "optional": true, "peer": true }, + "node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "requires": { + "whatwg-url": "^5.0.0" + } + }, "node-gyp": { "version": "8.4.1", "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-8.4.1.tgz", @@ -12862,6 +12870,11 @@ "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" }, + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "ts-node": { "version": "10.9.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", @@ -13168,6 +13181,20 @@ "makeerror": "1.0.12" } }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/util/package.json b/util/package.json index f9440500..01c3d9f2 100644 --- a/util/package.json +++ b/util/package.json @@ -37,12 +37,14 @@ "ts-node": "^10.2.1" }, "dependencies": { + "@types/node-fetch": "^2.6.2", "amqplib": "^0.10.0", "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.2.12", "missing-native-js-functions": "^1.2.18", "multer": "^1.4.5-lts.1", + "node-fetch": "^2.6.7", "picocolors": "^1.0.0", "proxy-agent": "^5.0.0", "reflect-metadata": "^0.1.13", 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" diff --git a/util/tsconfig.json b/util/tsconfig.json index 0398ce9a..a5d5ae05 100644 --- a/util/tsconfig.json +++ b/util/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": false /* 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": ["ES2021"] /* Specify library files to be included in the compilation. */, |