mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-10 21:27:01 +00:00
Clean up some type guards
This commit is contained in:
@@ -56,7 +56,7 @@ router.get(
|
||||
if (parsedLimit < 1 || parsedLimit > 100) throw new HTTPError("limit must be between 1 and 100", 422);
|
||||
|
||||
if (sort_order) {
|
||||
if (typeof sort_order != "string" || ["desc", "asc"].indexOf(sort_order) == -1)
|
||||
if (["desc", "asc"].indexOf(sort_order) == -1)
|
||||
throw FieldErrors({
|
||||
sort_order: {
|
||||
message: "Value must be one of ('desc', 'asc').",
|
||||
|
||||
@@ -40,11 +40,11 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
|
||||
let data: Payload;
|
||||
|
||||
if (
|
||||
(buffer instanceof Buffer && buffer[0] === 123) || // ASCII 123 = `{`. Bad check for JSON
|
||||
(Buffer.isBuffer(buffer) && buffer[0] === 123) || // ASCII 123 = `{`. Bad check for JSON
|
||||
typeof buffer === "string"
|
||||
) {
|
||||
data = bigIntJson.parse(buffer.toString());
|
||||
} else if (this.encoding === "json" && buffer instanceof Buffer) {
|
||||
} else if (this.encoding === "json" && Buffer.isBuffer(buffer)) {
|
||||
if (this.compress === "zlib-stream") {
|
||||
try {
|
||||
buffer = this.inflate!.process(buffer);
|
||||
@@ -59,7 +59,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
|
||||
}
|
||||
}
|
||||
data = bigIntJson.parse(buffer as string);
|
||||
} else if (this.encoding === "etf" && buffer instanceof Buffer && erlpack) {
|
||||
} else if (this.encoding === "etf" && Buffer.isBuffer(buffer) && erlpack) {
|
||||
try {
|
||||
data = erlpack.unpack(buffer);
|
||||
} catch {
|
||||
|
||||
@@ -199,7 +199,7 @@ export class User extends BaseClass {
|
||||
validate() {
|
||||
if (this.discriminator) {
|
||||
const discrim = Number(this.discriminator);
|
||||
if (isNaN(discrim) || !(typeof discrim == "number") || !Number.isInteger(discrim) || discrim <= 0 || discrim >= 10000)
|
||||
if (isNaN(discrim) || !Number.isInteger(discrim) || discrim <= 0 || discrim >= 10000)
|
||||
throw FieldErrors({
|
||||
discriminator: {
|
||||
message: "Discriminator must be a number.",
|
||||
|
||||
@@ -19,10 +19,7 @@
|
||||
export class DateBuilder {
|
||||
private date: Date;
|
||||
// constructors
|
||||
constructor(date = new Date()) {
|
||||
if (!(date instanceof Date)) {
|
||||
throw new Error("Invalid date object.");
|
||||
}
|
||||
constructor(date: Date = new Date()) {
|
||||
this.date = new Date(date.getTime()); // Create a copy to avoid mutating the original date
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user