keryx 0.24.1 → 0.24.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/initializers/channels.ts +13 -16
- package/initializers/db.ts +5 -5
- package/initializers/redis.ts +5 -6
- package/package.json +1 -1
- package/util/connectionString.ts +14 -0
package/initializers/channels.ts
CHANGED
|
@@ -12,6 +12,16 @@ const namespace = "channels";
|
|
|
12
12
|
const PRESENCE_KEY_PREFIX = "presence:";
|
|
13
13
|
const LUA_DIR = join(import.meta.dir, "..", "lua");
|
|
14
14
|
|
|
15
|
+
const ADD_PRESENCE_LUA = await Bun.file(
|
|
16
|
+
join(LUA_DIR, "add-presence.lua"),
|
|
17
|
+
).text();
|
|
18
|
+
const REMOVE_PRESENCE_LUA = await Bun.file(
|
|
19
|
+
join(LUA_DIR, "remove-presence.lua"),
|
|
20
|
+
).text();
|
|
21
|
+
const REFRESH_PRESENCE_LUA = await Bun.file(
|
|
22
|
+
join(LUA_DIR, "refresh-presence.lua"),
|
|
23
|
+
).text();
|
|
24
|
+
|
|
15
25
|
declare module "../classes/API" {
|
|
16
26
|
export interface API {
|
|
17
27
|
[namespace]: Awaited<ReturnType<Channels["initialize"]>>;
|
|
@@ -19,9 +29,6 @@ declare module "../classes/API" {
|
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
export class Channels extends Initializer {
|
|
22
|
-
private addPresenceLua = "";
|
|
23
|
-
private removePresenceLua = "";
|
|
24
|
-
private refreshPresenceLua = "";
|
|
25
32
|
private heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
26
33
|
|
|
27
34
|
constructor() {
|
|
@@ -107,7 +114,7 @@ export class Channels extends Initializer {
|
|
|
107
114
|
const connectionSetKey = `${PRESENCE_KEY_PREFIX}${channelName}:${key}`;
|
|
108
115
|
|
|
109
116
|
const added = await api.redis.redis.eval(
|
|
110
|
-
|
|
117
|
+
ADD_PRESENCE_LUA,
|
|
111
118
|
2,
|
|
112
119
|
connectionSetKey,
|
|
113
120
|
channelKey,
|
|
@@ -140,7 +147,7 @@ export class Channels extends Initializer {
|
|
|
140
147
|
const connectionSetKey = `${PRESENCE_KEY_PREFIX}${channelName}:${key}`;
|
|
141
148
|
|
|
142
149
|
const shouldLeave = await api.redis.redis.eval(
|
|
143
|
-
|
|
150
|
+
REMOVE_PRESENCE_LUA,
|
|
144
151
|
2,
|
|
145
152
|
connectionSetKey,
|
|
146
153
|
channelKey,
|
|
@@ -190,7 +197,7 @@ export class Channels extends Initializer {
|
|
|
190
197
|
|
|
191
198
|
const keys = [...keysToRefresh];
|
|
192
199
|
await api.redis.redis.eval(
|
|
193
|
-
|
|
200
|
+
REFRESH_PRESENCE_LUA,
|
|
194
201
|
keys.length,
|
|
195
202
|
...keys,
|
|
196
203
|
config.channels.presenceTTL,
|
|
@@ -218,16 +225,6 @@ export class Channels extends Initializer {
|
|
|
218
225
|
};
|
|
219
226
|
|
|
220
227
|
async initialize() {
|
|
221
|
-
this.addPresenceLua = await Bun.file(
|
|
222
|
-
join(LUA_DIR, "add-presence.lua"),
|
|
223
|
-
).text();
|
|
224
|
-
this.removePresenceLua = await Bun.file(
|
|
225
|
-
join(LUA_DIR, "remove-presence.lua"),
|
|
226
|
-
).text();
|
|
227
|
-
this.refreshPresenceLua = await Bun.file(
|
|
228
|
-
join(LUA_DIR, "refresh-presence.lua"),
|
|
229
|
-
).text();
|
|
230
|
-
|
|
231
228
|
// Load plugin channels
|
|
232
229
|
const pluginChannels: Channel[] = [];
|
|
233
230
|
for (const plugin of config.plugins) {
|
package/initializers/db.ts
CHANGED
|
@@ -11,7 +11,10 @@ import { api, logger } from "../api";
|
|
|
11
11
|
import { Initializer } from "../classes/Initializer";
|
|
12
12
|
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
13
13
|
import { config } from "../config";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
formatConnectionStringForLogging,
|
|
16
|
+
throwConnectionError,
|
|
17
|
+
} from "../util/connectionString";
|
|
15
18
|
|
|
16
19
|
const namespace = "db";
|
|
17
20
|
|
|
@@ -59,10 +62,7 @@ export class DB extends Initializer {
|
|
|
59
62
|
try {
|
|
60
63
|
await api.db.db.execute(sql`SELECT NOW()`);
|
|
61
64
|
} catch (e) {
|
|
62
|
-
|
|
63
|
-
type: ErrorType.SERVER_INITIALIZATION,
|
|
64
|
-
message: `Cannot connect to database (${formatConnectionStringForLogging(config.database.connectionString)}): ${e}`,
|
|
65
|
-
});
|
|
65
|
+
throwConnectionError("database", config.database.connectionString, e);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
if (config.database.autoMigrate) {
|
package/initializers/redis.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Redis as RedisClient } from "ioredis";
|
|
2
2
|
import { api, logger } from "../api";
|
|
3
3
|
import { Initializer } from "../classes/Initializer";
|
|
4
|
-
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
5
4
|
import { config } from "../config";
|
|
6
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
formatConnectionStringForLogging,
|
|
7
|
+
throwConnectionError,
|
|
8
|
+
} from "../util/connectionString";
|
|
7
9
|
|
|
8
10
|
const namespace = "redis";
|
|
9
11
|
const testKey = `__keryx_test_key:${config.process.name}`;
|
|
@@ -42,10 +44,7 @@ export class Redis extends Initializer {
|
|
|
42
44
|
await api.redis.subscription.set(testKey, Date.now());
|
|
43
45
|
await api.redis.subscription.del(testKey);
|
|
44
46
|
} catch (e) {
|
|
45
|
-
|
|
46
|
-
type: ErrorType.SERVER_INITIALIZATION,
|
|
47
|
-
message: `Cannot connect to redis (${formatConnectionStringForLogging(config.redis.connectionString)}): ${e}`,
|
|
48
|
-
});
|
|
47
|
+
throwConnectionError("redis", config.redis.connectionString, e);
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
logger.info(
|
package/package.json
CHANGED
package/util/connectionString.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
+
import { ErrorType, TypedError } from "../classes/TypedError";
|
|
2
|
+
|
|
1
3
|
/** Strip the password from a connection string for safe logging. Preserves protocol, user, host, port, and path. */
|
|
2
4
|
export function formatConnectionStringForLogging(connectionString: string) {
|
|
3
5
|
const connectionStringParsed = new URL(connectionString);
|
|
4
6
|
const connectionStringInfo = `${connectionStringParsed.protocol ? `${connectionStringParsed.protocol}//` : ""}${connectionStringParsed.username ? `${connectionStringParsed.username}@` : ""}${connectionStringParsed.hostname}:${connectionStringParsed.port}${connectionStringParsed.pathname}`;
|
|
5
7
|
return connectionStringInfo;
|
|
6
8
|
}
|
|
9
|
+
|
|
10
|
+
/** Throw a standardized `SERVER_INITIALIZATION` error for a failed connection probe. The connection string is password-stripped via {@link formatConnectionStringForLogging} before being embedded in the message. */
|
|
11
|
+
export function throwConnectionError(
|
|
12
|
+
service: string,
|
|
13
|
+
connectionString: string,
|
|
14
|
+
error: unknown,
|
|
15
|
+
): never {
|
|
16
|
+
throw new TypedError({
|
|
17
|
+
type: ErrorType.SERVER_INITIALIZATION,
|
|
18
|
+
message: `Cannot connect to ${service} (${formatConnectionStringForLogging(connectionString)}): ${error}`,
|
|
19
|
+
});
|
|
20
|
+
}
|