Skip to content

Commit 5b8fef4

Browse files
committed
chore: switch to bigint handling for permissions
1 parent 00f3ca6 commit 5b8fef4

File tree

14 files changed

+125
-95
lines changed

14 files changed

+125
-95
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "stoat.js",
3-
"version": "7.3.1",
3+
"version": "7.3.2",
44
"type": "module",
55
"exports": {
66
".": "./lib/index.js"
@@ -29,8 +29,9 @@
2929
"@solid-primitives/map": "^0.7.1",
3030
"@solid-primitives/set": "^0.7.1",
3131
"@vladfrangu/async_event_emitter": "^2.4.6",
32-
"stoat-api": "0.8.9-3",
32+
"json-with-bigint": "^3.4.4",
3333
"solid-js": "^1.9.6",
34+
"stoat-api": "0.8.9-4",
3435
"ulid": "^2.4.0"
3536
},
3637
"devDependencies": {

pnpm-lock.yaml

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/classes/Channel.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
Invite,
1313
Override,
1414
} from "stoat-api";
15-
import type { APIRoutes } from "stoat-api/lib/routes";
15+
import type { APIRoutes } from "stoat-api";
1616
import { decodeTime, ulid } from "ulid";
1717

1818
import { ChannelCollection } from "../collections/index.js";
@@ -229,21 +229,21 @@ export class Channel {
229229
/**
230230
* Permissions allowed for users in this group
231231
*/
232-
get permissions(): number | undefined {
232+
get permissions(): bigint | undefined {
233233
return this.#collection.getUnderlyingObject(this.id).permissions;
234234
}
235235

236236
/**
237237
* Default permissions for this server channel
238238
*/
239-
get defaultPermissions(): { a: number; d: number } | undefined {
239+
get defaultPermissions(): { a: bigint; d: bigint } | undefined {
240240
return this.#collection.getUnderlyingObject(this.id).defaultPermissions;
241241
}
242242

243243
/**
244244
* Role permissions for this server channel
245245
*/
246-
get rolePermissions(): Record<string, { a: number; d: number }> | undefined {
246+
get rolePermissions(): Record<string, { a: bigint; d: bigint }> | undefined {
247247
return this.#collection.getUnderlyingObject(this.id).rolePermissions;
248248
}
249249

@@ -349,16 +349,16 @@ export class Channel {
349349
get potentiallyRestrictedChannel(): string | boolean | undefined {
350350
if (!this.serverId) return false;
351351
return (
352-
bitwiseAndEq(this.defaultPermissions?.d ?? 0, Permission.ViewChannel) ||
352+
bitwiseAndEq(this.defaultPermissions?.d ?? 0n, Permission.ViewChannel) ||
353353
!bitwiseAndEq(this.server!.defaultPermissions, Permission.ViewChannel) ||
354354
[...(this.server?.roles.keys() ?? [])].find(
355355
(role) =>
356356
bitwiseAndEq(
357-
this.rolePermissions?.[role]?.d ?? 0,
357+
this.rolePermissions?.[role]?.d ?? 0n,
358358
Permission.ViewChannel,
359359
) ||
360360
bitwiseAndEq(
361-
this.server?.roles.get(role)?.permissions.d ?? 0,
361+
this.server?.roles.get(role)?.permissions.d ?? 0n,
362362
Permission.ViewChannel,
363363
),
364364
)
@@ -368,7 +368,7 @@ export class Channel {
368368
/**
369369
* Permission the currently authenticated user has against this channel
370370
*/
371-
get permission(): number {
371+
get permission(): bigint {
372372
return calculatePermission(this.#collection.client, this);
373373
}
374374

src/classes/Server.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class Server {
167167
/**
168168
* Default permissions
169169
*/
170-
get defaultPermissions(): number {
170+
get defaultPermissions(): bigint {
171171
return this.#collection.getUnderlyingObject(this.id).defaultPermissions;
172172
}
173173

@@ -271,7 +271,7 @@ export class Server {
271271
*/
272272
get orderedRoles(): {
273273
name: string;
274-
permissions: OverrideField;
274+
permissions: { a: bigint, d: bigint };
275275
colour?: string | null;
276276
hoist?: boolean;
277277
rank?: number;
@@ -337,7 +337,7 @@ export class Server {
337337
/**
338338
* Permission the currently authenticated user has against this server
339339
*/
340-
get permission(): number {
340+
get permission(): bigint {
341341
return calculatePermission(this.#collection.client, this);
342342
}
343343

@@ -514,12 +514,11 @@ export class Server {
514514
*/
515515
async kickUser(user: string | User | ServerMember): Promise<void> {
516516
return await this.#collection.client.api.delete(
517-
`/servers/${this.id as ""}/members/${
518-
typeof user === "string"
519-
? user
520-
: user instanceof User
521-
? user.id
522-
: user.id.user
517+
`/servers/${this.id as ""}/members/${typeof user === "string"
518+
? user
519+
: user instanceof User
520+
? user.id
521+
: user.id.user
523522
}`,
524523
);
525524
}
@@ -723,8 +722,7 @@ export class Server {
723722
query: string,
724723
): Promise<{ members: ServerMember[]; users: User[] }> {
725724
const data = (await this.#collection.client.api.get(
726-
`/servers/${
727-
this.id as ""
725+
`/servers/${this.id as ""
728726
}/members_experimental_query?experimental_api=true&query=${encodeURIComponent(
729727
query,
730728
)}` as never,

src/classes/ServerMember.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class ServerMember {
110110
/**
111111
* Ordered list of roles for this member, from lowest to highest priority.
112112
*/
113-
get orderedRoles(): (Partial<Role> & { id: string })[] {
113+
get orderedRoles(): (Partial<Omit<Role, 'permissions'> & { permissions: { a: bigint, d: bigint } }> & { id: string })[] {
114114
const server = this.server!;
115115
return (
116116
this.roles
@@ -125,7 +125,7 @@ export class ServerMember {
125125
/**
126126
* Member's currently hoisted role.
127127
*/
128-
get hoistedRole(): Partial<Role> | null {
128+
get hoistedRole(): Partial<Omit<Role, 'permissions'> & { permissions: { a: bigint, d: bigint } }> | null {
129129
const roles = this.orderedRoles.filter((x) => x.hoist);
130130
if (roles.length > 0) {
131131
return roles[roles.length - 1];
@@ -168,7 +168,7 @@ export class ServerMember {
168168
* @param target Target object to check permissions against
169169
* @returns Permissions that this member has
170170
*/
171-
getPermissions(target: Server | Channel): number {
171+
getPermissions(target: Server | Channel): bigint {
172172
return calculatePermission(this.#collection.client, target, {
173173
member: this,
174174
});

src/classes/ServerRole.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Role as APIRole, OverrideField } from "stoat-api";
1+
import type { Role as APIRole } from "stoat-api";
22

33
import type { Client } from "../Client.js";
44

@@ -11,7 +11,10 @@ export class ServerRole {
1111

1212
readonly id: string;
1313
readonly name: string;
14-
readonly permissions: OverrideField;
14+
readonly permissions: {
15+
a: bigint,
16+
d: bigint
17+
};
1518
readonly colour?: string;
1619
readonly hoist: boolean;
1720
readonly rank: number;
@@ -29,7 +32,10 @@ export class ServerRole {
2932

3033
this.id = id;
3134
this.name = data.name;
32-
this.permissions = data.permissions;
35+
this.permissions = {
36+
a: BigInt(data.permissions.a),
37+
d: BigInt(data.permissions.d)
38+
};
3339
this.colour = data.colour ?? undefined;
3440
this.hoist = data.hoist || false;
3541
this.rank = data.rank ?? 0;

src/events/EventClient.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { Error } from "stoat-api";
66

77
import type { ProtocolV1 } from "./v1.js";
88

9+
import { JSONParse, JSONStringify } from 'json-with-bigint';
10+
911
/**
1012
* Available protocols to connect with
1113
*/
@@ -94,7 +96,7 @@ export class EventClient<
9496
#connectTimeoutReference: number | undefined;
9597

9698
#lastError: // eslint-disable-next-line @typescript-eslint/no-explicit-any
97-
{ type: "socket"; data: any } | { type: "revolt"; data: Error } | undefined;
99+
{ type: "socket"; data: any } | { type: "revolt"; data: Error } | undefined;
98100

99101
/**
100102
* Create a new event client.
@@ -179,7 +181,7 @@ export class EventClient<
179181

180182
if (this.#transportFormat === "json") {
181183
if (typeof event.data === "string") {
182-
this.handle(JSON.parse(event.data));
184+
this.handle(JSONParse(event.data));
183185
}
184186
}
185187
};
@@ -214,7 +216,7 @@ export class EventClient<
214216
send(event: EventProtocol<T>["client"]): void {
215217
if (this.options.debug) console.debug("[C->S]", event);
216218
if (!this.#socket) throw "Socket closed, trying to send.";
217-
this.#socket.send(JSON.stringify(event));
219+
this.#socket.send(JSONStringify(event));
218220
}
219221

220222
/**
@@ -273,14 +275,14 @@ export class EventClient<
273275
*/
274276
get lastError():
275277
| {
276-
type: "socket";
277-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
278-
data: any;
279-
}
278+
type: "socket";
279+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
280+
data: any;
281+
}
280282
| {
281-
type: "revolt";
282-
data: Error;
283-
}
283+
type: "revolt";
284+
data: Error;
285+
}
284286
| undefined {
285287
return this.#lastError;
286288
}

src/events/v1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ export async function handleEvent(
641641
let role = {};
642642
const roles = server.roles;
643643
if (roles.has(event.role_id)) {
644-
role = roles.get(event.role_id) as Role;
644+
role = roles.get(event.role_id) as never;
645645
roles.delete(event.role_id);
646646
} else if (!client.servers.isPartial(event.id)) {
647647
return;

src/hydration/channel.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export type HydratedChannel = {
2323
ownerId?: string;
2424
serverId?: string;
2525

26-
permissions?: number;
27-
defaultPermissions?: OverrideField;
28-
rolePermissions?: Record<string, OverrideField>;
26+
permissions?: bigint;
27+
defaultPermissions?: { a: bigint, d: bigint };
28+
rolePermissions?: Record<string, { a: bigint, d: bigint }>;
2929
nsfw: boolean;
3030

3131
lastMessageId?: string;
@@ -57,9 +57,18 @@ export const channelHydration: Hydrate<Merge<APIChannel>, HydratedChannel> = {
5757
userId: (channel) => channel.user,
5858
ownerId: (channel) => channel.owner,
5959
serverId: (channel) => channel.server,
60-
permissions: (channel) => channel.permissions!,
61-
defaultPermissions: (channel) => channel.default_permissions!,
62-
rolePermissions: (channel) => channel.role_permissions,
60+
permissions: (channel) => BigInt(channel.permissions!),
61+
defaultPermissions: (channel) => ({
62+
a: BigInt(channel.default_permissions?.a ?? 0),
63+
d: BigInt(channel.default_permissions?.d ?? 0),
64+
}),
65+
rolePermissions: (channel) => Object.fromEntries(
66+
Object.entries(channel.role_permissions ?? {})
67+
.map(([k, v]) => [k, {
68+
a: BigInt(v.a),
69+
d: BigInt(v.d)
70+
}])
71+
),
6372
nsfw: (channel) => channel.nsfw || false,
6473
lastMessageId: (channel) => channel.last_message_id!,
6574
voice: (channel) => {

src/hydration/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReactiveMap } from "@solid-primitives/map";
22
import { ReactiveSet } from "@solid-primitives/set";
3-
import type { Interactions, Masquerade, Message } from "stoat-api";
3+
import type { Embed, Interactions, Masquerade, Message } from "stoat-api";
44

55
import type { Client } from "../Client.js";
66
import { File } from "../classes/File.js";

0 commit comments

Comments
 (0)