Allow subpath imports, move extensions to separate module

This commit is contained in:
Rory&
2026-06-11 18:07:50 +02:00
parent 758389d1f5
commit fe1c390973
18 changed files with 54 additions and 23 deletions
+1 -1
View File
@@ -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
-82
View File
@@ -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
-22
View File
@@ -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);
}
-25
View File
@@ -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));
}
-2
View File
@@ -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";
@@ -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;
@@ -19,10 +19,11 @@
import EventEmitter from "node:events";
import fs from "node:fs";
import net, { Server } from "node:net";
import { BaseEventListener } from "./BaseEventListener";
import { arraySum, EVENT, Event, EventOpts } from "@spacebar/util";
import { ProcessLifecycle } from "../../ProcessLifecycle";
import { Gauge } from "prom-client";
import { arraySum } from "@spacebar/extensions";
import { EVENT, Event, EventOpts } from "@spacebar/util";
import { BaseEventListener } from "./BaseEventListener";
import { ProcessLifecycle } from "../../ProcessLifecycle";
import { Monitoring } from "../../../monitoring/Monitoring";
export class UnixSocketListener extends BaseEventListener {
@@ -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;