summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-06-11 18:07:50 +0200
committerRory& <root@rory.gay>2026-06-11 18:07:50 +0200
commitfe1c390973221703bfdeefeebd59c5fdee3f5f2d (patch)
treed81333f441614ae1d8000bd00150c461ac0fb9fc /src/util
parentAllow subpath imports from module aliases (diff)
downloadserver-ts-fe1c390973221703bfdeefeebd59c5fdee3f5f2d.tar.xz
Allow subpath imports, move extensions to separate module
Diffstat (limited to 'src/util')
-rw-r--r--src/util/entities/Guild.ts2
-rw-r--r--src/util/util/extensions/Array.ts82
-rw-r--r--src/util/util/extensions/Math.ts22
-rw-r--r--src/util/util/extensions/index.ts25
-rw-r--r--src/util/util/index.ts2
-rw-r--r--src/util/util/ipc/listener/RabbitMqSingleListener.ts9
-rw-r--r--src/util/util/ipc/listener/UnixSocketListener.ts5
-rw-r--r--src/util/util/ipc/writer/RabbitMqSingleWriter.ts5
8 files changed, 12 insertions, 140 deletions
diff --git a/src/util/entities/Guild.ts b/src/util/entities/Guild.ts

index aea911eb5..0cea637e1 100644 --- a/src/util/entities/Guild.ts +++ b/src/util/entities/Guild.ts
@@ -17,6 +17,7 @@ */ import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "typeorm"; +import { arrayRemove } from "@spacebar/extensions"; import { Config, GuildWelcomeScreen, Snowflake, handleFile } from ".."; import { Ban } from "./Ban"; import { BaseClass } from "./BaseClass"; @@ -30,7 +31,6 @@ import { Template } from "./Template"; import { User } from "./User"; import { VoiceState } from "./VoiceState"; import { Webhook } from "./Webhook"; -import { arrayRemove } from "@spacebar/util"; // TODO: application_command_count, application_command_counts: {1: 0, 2: 0, 3: 0} // TODO: guild_scheduled_events // TODO: stage_instances diff --git a/src/util/util/extensions/Array.ts b/src/util/util/extensions/Array.ts deleted file mode 100644
index c95328b5a..000000000 --- a/src/util/util/extensions/Array.ts +++ /dev/null
@@ -1,82 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2025 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ - -/* https://stackoverflow.com/a/50636286 */ -export function arrayPartition<T>(array: T[], filter: (elem: T) => boolean): [T[], T[]] { - const pass: T[] = [], - fail: T[] = []; - array.forEach((e) => (filter(e) ? pass : fail).push(e)); - return [pass, fail]; -} - -export function arrayRemove<T>(array: T[], item: T): void { - const index = array.indexOf(item); - if (index > -1) { - array.splice(index, 1); - } -} - -export function arrayDistinctBy<T, M>(array: T[], selector: (elem: T) => M): T[] { - const mapped = new Set<M>(); - - return array.filter((item) => { - const mappedValue = selector(item); - if (mapped.has(mappedValue)) return false; - - mapped.add(mappedValue); - return true; - }); -} - -export function arrayGroupBy<T, M>(array: T[], selector: (elem: T) => M): Map<M, T[]> { - const map = new Map<M, T[]>(); - - array.forEach((item) => { - const mappedValue = selector(item); - const existing = map.get(mappedValue); - if (existing) existing.push(item); - else map.set(mappedValue, [item]); - }); - - return map; -} - -// https://github.com/TheArcaneBrony/ArcaneLibs/blob/ca7ce2bb57a5dffd891b0b86ec5f358df02735c0/ArcaneLibs/Extensions/CollectionExtensions.cs#L103 -export function arrayDistributeSequentially<T>(array: T[], count: number): T[][] { - if (count <= 0) throw new Error("Count must be greater than 0"); - if (count == 1) return [array]; - - const list = array; - const groups: T[][] = []; - for (let i = 0; i < count; i++) groups.push([]); - - // [1,2,3],[4,5,6],[7,8,9]... - for (let i = 0; i < list.length; i++) { - const idx: number = Math.floor((i / list.length) * count); - // console.log(`${i}/${list.length} -> ${idx}:${groups[idx].length}/{count}`); - groups[idx].push(list[i]); - } - - return groups; -} - -//region Numerics -export function arraySum(array: number[]) { - return array.reduce((prev, curr) => prev + curr, 0); -} -//endregion diff --git a/src/util/util/extensions/Math.ts b/src/util/util/extensions/Math.ts deleted file mode 100644
index 0a34aaabf..000000000 --- a/src/util/util/extensions/Math.ts +++ /dev/null
@@ -1,22 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2026 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ - -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log -export function mathLogBase(num: number, base: number) { - return Math.log(num) / Math.log(base); -} diff --git a/src/util/util/extensions/index.ts b/src/util/util/extensions/index.ts deleted file mode 100644
index 53bd443c8..000000000 --- a/src/util/util/extensions/index.ts +++ /dev/null
@@ -1,25 +0,0 @@ -/* - Spacebar: A FOSS re-implementation and extension of the Discord.com backend. - Copyright (C) 2026 Spacebar and Spacebar Contributors - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ - -export * from "./Array"; -export * from "./Math"; - -// TODO: move to a separate file -export async function sleep(ms: number) { - return new Promise((resolve) => void setTimeout(resolve, ms)); -} diff --git a/src/util/util/index.ts b/src/util/util/index.ts
index f6de6a955..2c028e1d4 100644 --- a/src/util/util/index.ts +++ b/src/util/util/index.ts
@@ -17,7 +17,6 @@ */ export * from "./ApiError"; -export * from "./extensions/Array"; export * from "./BitField"; //export * from "./Categories"; export * from "./cdn"; @@ -52,7 +51,6 @@ export * from "./Gifs"; export * from "./Application"; export * from "./NameValidation"; export * from "../../schemas/HelperTypes"; -export * from "./extensions"; export * from "./Random"; export * from "./Url"; export * from "./Version"; diff --git a/src/util/util/ipc/listener/RabbitMqSingleListener.ts b/src/util/util/ipc/listener/RabbitMqSingleListener.ts
index 7e5867acc..d221cbb8c 100644 --- a/src/util/util/ipc/listener/RabbitMqSingleListener.ts +++ b/src/util/util/ipc/listener/RabbitMqSingleListener.ts
@@ -18,12 +18,13 @@ import EventEmitter from "node:events"; import { randomUUID } from "node:crypto"; -import { BaseEventListener } from "./BaseEventListener"; -import { arraySum, EVENT, Event, EventOpts, sleep } from "@spacebar/util"; import amqp, { Channel, ChannelModel } from "amqplib"; -import { ProcessLifecycle } from "../../ProcessLifecycle"; -import { Monitoring } from "../../../monitoring/Monitoring"; import { Gauge } from "prom-client"; +import { sleep, arraySum } from "@spacebar/extensions"; +import { EVENT, Event, EventOpts } from "@spacebar/util"; +import { Monitoring } from "../../../monitoring/Monitoring"; +import { ProcessLifecycle } from "../../ProcessLifecycle"; +import { BaseEventListener } from "./BaseEventListener"; export class RabbitMqSingleListener extends BaseEventListener { static openListenersMetric: Gauge; diff --git a/src/util/util/ipc/listener/UnixSocketListener.ts b/src/util/util/ipc/listener/UnixSocketListener.ts
index 50ffe2b09..cb9ea96b7 100644 --- a/src/util/util/ipc/listener/UnixSocketListener.ts +++ b/src/util/util/ipc/listener/UnixSocketListener.ts
@@ -19,10 +19,11 @@ import EventEmitter from "node:events"; import fs from "node:fs"; import net, { Server } from "node:net"; +import { Gauge } from "prom-client"; +import { arraySum } from "@spacebar/extensions"; +import { EVENT, Event, EventOpts } from "@spacebar/util"; import { BaseEventListener } from "./BaseEventListener"; -import { arraySum, EVENT, Event, EventOpts } from "@spacebar/util"; import { ProcessLifecycle } from "../../ProcessLifecycle"; -import { Gauge } from "prom-client"; import { Monitoring } from "../../../monitoring/Monitoring"; export class UnixSocketListener extends BaseEventListener { diff --git a/src/util/util/ipc/writer/RabbitMqSingleWriter.ts b/src/util/util/ipc/writer/RabbitMqSingleWriter.ts
index ad00aa2b5..96e820367 100644 --- a/src/util/util/ipc/writer/RabbitMqSingleWriter.ts +++ b/src/util/util/ipc/writer/RabbitMqSingleWriter.ts
@@ -16,10 +16,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { BaseEventWriter } from "./BaseEventWriter"; import amqp, { Channel, ChannelModel } from "amqplib"; -import { Event, sleep } from "@spacebar/util"; +import { sleep } from "@spacebar/extensions"; +import { Event } from "@spacebar/util"; import { ProcessLifecycle } from "../../ProcessLifecycle"; +import { BaseEventWriter } from "./BaseEventWriter"; export class RabbitMqSingleWriter extends BaseEventWriter { private readonly host: string;