keryx 0.21.5 → 0.21.7

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/classes/API.ts CHANGED
@@ -92,6 +92,8 @@ export class API {
92
92
  }
93
93
  }
94
94
 
95
+ this.validateInitializerProperties("initialize");
96
+
95
97
  this.initialized = true;
96
98
  this.logger.warn("--- 🔄 Initializing complete ---");
97
99
  }
@@ -138,6 +140,8 @@ export class API {
138
140
  }
139
141
  }
140
142
 
143
+ this.validateInitializerProperties("start");
144
+
141
145
  this.started = true;
142
146
  this.logger.warn("--- 🔼 Starting complete ---");
143
147
  }
@@ -272,4 +276,37 @@ export class API {
272
276
  private sortInitializers(key: InitializerSortKeys) {
273
277
  this.initializers.sort((a, b) => a[key] - b[key]);
274
278
  }
279
+
280
+ /**
281
+ * Assert that every initializer which claims an API namespace (via
282
+ * `declaresAPIProperty`) has actually attached `api[initializer.name]`.
283
+ * Closes the silent-drift gap between TypeScript module augmentation
284
+ * and runtime property assignment — a missing namespace becomes a fast,
285
+ * loud startup failure instead of a confusing `Cannot read property of
286
+ * undefined` deep inside a request handler.
287
+ *
288
+ * Initializers excluded from the current `runMode` are skipped during the
289
+ * `start` phase since their `start()` method never ran.
290
+ */
291
+ private validateInitializerProperties(phase: "initialize" | "start") {
292
+ const missing: string[] = [];
293
+ for (const initializer of this.initializers) {
294
+ if (initializer.declaresAPIProperty === false) continue;
295
+ if (phase === "start" && !initializer.runModes.includes(this.runMode)) {
296
+ continue;
297
+ }
298
+ if (this[initializer.name] == null) {
299
+ missing.push(initializer.name);
300
+ }
301
+ }
302
+ if (missing.length > 0) {
303
+ throw new TypedError({
304
+ type: ErrorType.INITIALIZER_VALIDATION,
305
+ message:
306
+ `Initializers did not attach their namespace to api after the ${phase} phase: ` +
307
+ `${missing.join(", ")}. Each initializer must either return a namespace object ` +
308
+ `from initialize() (and/or populate it in start()), or set declaresAPIProperty = false.`,
309
+ });
310
+ }
311
+ }
275
312
  }
@@ -1,10 +1,10 @@
1
- import colors from "colors";
2
1
  import { randomUUID } from "crypto";
3
2
  import { api, logger } from "../api";
4
3
  import { config } from "../config";
5
4
  import type { PubSubMessage } from "../initializers/pubsub";
6
5
  import type { SessionData } from "../initializers/session";
7
6
  import type { RateLimitInfo } from "../middleware/rateLimit";
7
+ import { ansi } from "../util/ansi";
8
8
  import { isSecret } from "../util/zodMixins";
9
9
  import type { Action, ActionParams } from "./Action";
10
10
  import { LogFormat } from "./Logger";
@@ -380,20 +380,20 @@ function logAction(opts: {
380
380
  logger.info(`action: ${opts.actionName}`, data);
381
381
  } else {
382
382
  const loggingParams = config.logger.colorize
383
- ? colors.gray(JSON.stringify(opts.params))
383
+ ? ansi.gray(JSON.stringify(opts.params))
384
384
  : JSON.stringify(opts.params);
385
385
 
386
386
  const statusMessage = `[ACTION:${opts.connectionType.toUpperCase()}:${opts.status}]`;
387
387
  const messagePrefix = config.logger.colorize
388
388
  ? opts.status === "OK"
389
- ? colors.bgBlue(statusMessage)
390
- : colors.bgMagenta(statusMessage)
389
+ ? ansi.bgBlue(statusMessage)
390
+ : ansi.bgMagenta(statusMessage)
391
391
  : statusMessage;
392
392
 
393
393
  const errorStack =
394
394
  opts.error && opts.error.stack
395
395
  ? config.logger.colorize
396
- ? "\r\n" + colors.gray(opts.error.stack)
396
+ ? "\r\n" + ansi.gray(opts.error.stack)
397
397
  : "\r\n" + opts.error.stack
398
398
  : "";
399
399
 
@@ -17,6 +17,13 @@ export abstract class Initializer {
17
17
  stopPriority: number;
18
18
  /** Which run modes this initializer participates in. Defaults to both SERVER and CLI. */
19
19
  runModes: RUN_MODE[];
20
+ /**
21
+ * Whether this initializer attaches a property named `this.name` on the `api` singleton.
22
+ * Runtime validation in `API.initialize()` and `API.start()` will fail if the declared
23
+ * property is missing. Default: `true`. Set to `false` for initializers that intentionally
24
+ * don't augment the `API` interface.
25
+ */
26
+ declaresAPIProperty: boolean;
20
27
 
21
28
  constructor(name: string) {
22
29
  this.name = name;
@@ -24,6 +31,7 @@ export abstract class Initializer {
24
31
  this.startPriority = 1000;
25
32
  this.stopPriority = 1000;
26
33
  this.runModes = [RUN_MODE.SERVER, RUN_MODE.CLI];
34
+ this.declaresAPIProperty = true;
27
35
  }
28
36
 
29
37
  /**
package/classes/Logger.ts CHANGED
@@ -1,6 +1,5 @@
1
- import colors from "colors";
2
-
3
1
  import type { configLogger } from "../config/logger";
2
+ import { ansi } from "../util/ansi";
4
3
 
5
4
  export enum LogLevel {
6
5
  trace = "trace",
@@ -133,7 +132,7 @@ export class Logger {
133
132
  private logText(level: LogLevel, message: string, data?: any) {
134
133
  let timestamp = this.includeTimestamps ? `${new Date().toISOString()}` : "";
135
134
  if (this.colorize && timestamp.length > 0) {
136
- timestamp = colors.gray(timestamp);
135
+ timestamp = ansi.gray(timestamp);
137
136
  }
138
137
 
139
138
  let formattedLevel = `[${level}]`;
@@ -146,7 +145,7 @@ export class Logger {
146
145
  ? JSON.stringify(data, null, this.jSONObjectParsePadding)
147
146
  : "";
148
147
  if (this.colorize && prettyObject.length > 0) {
149
- prettyObject = colors.cyan(prettyObject);
148
+ prettyObject = ansi.cyan(prettyObject);
150
149
  }
151
150
 
152
151
  this.outputStream(
@@ -176,17 +175,17 @@ export class Logger {
176
175
  private colorFromLogLevel(level: LogLevel) {
177
176
  switch (level) {
178
177
  case LogLevel.trace:
179
- return colors.gray;
178
+ return ansi.gray;
180
179
  case LogLevel.debug:
181
- return colors.blue;
180
+ return ansi.blue;
182
181
  case LogLevel.info:
183
- return colors.green;
182
+ return ansi.green;
184
183
  case LogLevel.warn:
185
- return colors.yellow;
184
+ return ansi.yellow;
186
185
  case LogLevel.error:
187
- return colors.red;
186
+ return ansi.red;
188
187
  case LogLevel.fatal:
189
- return colors.magenta;
188
+ return ansi.magenta;
190
189
  }
191
190
  }
192
191
  }
@@ -1,11 +1,11 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
3
- import colors from "colors";
4
3
  import { randomUUID } from "crypto";
5
4
  import { api, logger } from "../api";
6
5
  import { Initializer } from "../classes/Initializer";
7
6
  import { ErrorType, TypedError } from "../classes/TypedError";
8
7
  import { config } from "../config";
8
+ import { ansi } from "../util/ansi";
9
9
  import { buildCorsHeaders, getExternalOrigin } from "../util/http";
10
10
  import {
11
11
  createMcpServer,
@@ -236,7 +236,7 @@ export class McpInitializer extends Initializer {
236
236
 
237
237
  const mcpUrl = `${config.server.web.applicationUrl}${mcpRoute}`;
238
238
  const startMessage = `started MCP server @ ${mcpUrl}`;
239
- logger.info(logger.colorize ? colors.bgBlue(startMessage) : startMessage);
239
+ logger.info(logger.colorize ? ansi.bgBlue(startMessage) : startMessage);
240
240
  }
241
241
 
242
242
  async stop() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keryx",
3
- "version": "0.21.5",
3
+ "version": "0.21.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -85,7 +85,6 @@
85
85
  "@modelcontextprotocol/sdk": "^1.29.0",
86
86
  "@opentelemetry/api": "^1.9.1",
87
87
  "@opentelemetry/sdk-metrics": "^2.6.1",
88
- "colors": "^1.4.0",
89
88
  "ioredis": "^5.10.1",
90
89
  "mustache": "^4.2.0",
91
90
  "node-resque": "^9.5.0",
@@ -103,7 +102,6 @@
103
102
  },
104
103
  "devDependencies": {
105
104
  "@types/bun": "^1.3.11",
106
- "@types/formidable": "^3.5.1",
107
105
  "drizzle-kit": "^0.31.10",
108
106
  "drizzle-zod": "^0.8.3"
109
107
  }
package/servers/web.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { parse } from "node:url";
2
2
  import type { ServerWebSocket } from "bun";
3
- import colors from "colors";
4
3
  import { randomUUID } from "crypto";
5
4
  import { api, logger } from "../api";
6
5
  import { type HTTP_METHOD } from "../classes/Action";
@@ -10,6 +9,7 @@ import { StreamingResponse } from "../classes/StreamingResponse";
10
9
  import { ErrorStatusCodes, ErrorType, TypedError } from "../classes/TypedError";
11
10
  import { config } from "../config";
12
11
  import type { PubSubMessage } from "../initializers/pubsub";
12
+ import { ansi } from "../util/ansi";
13
13
  import { isOriginAllowed } from "../util/http";
14
14
  import { compressResponse } from "../util/webCompression";
15
15
  import {
@@ -67,7 +67,7 @@ export class WebServer extends Server<ReturnType<typeof Bun.serve>> {
67
67
  this.port = server.port ?? config.server.web.port;
68
68
  this.url = `http://${config.server.web.host}:${this.port}`;
69
69
  const startMessage = `started server @ ${this.url}`;
70
- logger.info(logger.colorize ? colors.bgBlue(startMessage) : startMessage);
70
+ logger.info(logger.colorize ? ansi.bgBlue(startMessage) : startMessage);
71
71
  } catch (e) {
72
72
  await Bun.sleep(1000);
73
73
  startupAttempts++;
package/util/ansi.ts ADDED
@@ -0,0 +1,14 @@
1
+ const RESET = "\x1b[0m";
2
+
3
+ /** ANSI terminal color helpers. Each wraps a string in an escape code and reset suffix. */
4
+ export const ansi = {
5
+ gray: (s: string) => `\x1b[90m${s}${RESET}`,
6
+ blue: (s: string) => `\x1b[34m${s}${RESET}`,
7
+ cyan: (s: string) => `\x1b[36m${s}${RESET}`,
8
+ green: (s: string) => `\x1b[32m${s}${RESET}`,
9
+ yellow: (s: string) => `\x1b[33m${s}${RESET}`,
10
+ red: (s: string) => `\x1b[31m${s}${RESET}`,
11
+ magenta: (s: string) => `\x1b[35m${s}${RESET}`,
12
+ bgBlue: (s: string) => `\x1b[44m${s}${RESET}`,
13
+ bgMagenta: (s: string) => `\x1b[45m${s}${RESET}`,
14
+ };