@threadbase-sh/streamer 1.21.0 → 1.22.1

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/dist/index.js CHANGED
@@ -278,8 +278,12 @@ import { randomBytes, timingSafeEqual } from "crypto";
278
278
  import { chmodSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
279
279
  import { homedir } from "os";
280
280
  import { join as join2 } from "path";
281
- var CONFIG_DIR = join2(homedir(), ".threadbase");
282
- var CONFIG_FILE = join2(CONFIG_DIR, "server.yaml");
281
+ function configDir() {
282
+ return process.env.THREADBASE_CONFIG_DIR ?? join2(homedir(), ".threadbase");
283
+ }
284
+ function configFile() {
285
+ return join2(configDir(), "server.yaml");
286
+ }
283
287
  function generateApiKey() {
284
288
  return `tb_${randomBytes(16).toString("hex")}`;
285
289
  }
@@ -291,20 +295,20 @@ function validateApiKey(provided, expected) {
291
295
  }
292
296
  function loadOrCreateApiKey() {
293
297
  try {
294
- const content = readFileSync(CONFIG_FILE, "utf-8");
298
+ const content = readFileSync(configFile(), "utf-8");
295
299
  const match = content.match(/api_key:\s*(.+)/);
296
300
  if (match?.[1]) return match[1].trim();
297
301
  } catch {
298
302
  }
299
303
  const key = generateApiKey();
300
- mkdirSync(CONFIG_DIR, { recursive: true });
301
- writeFileSync(CONFIG_FILE, `api_key: ${key}
304
+ mkdirSync(configDir(), { recursive: true });
305
+ writeFileSync(configFile(), `api_key: ${key}
302
306
  `, "utf-8");
303
307
  return key;
304
308
  }
305
309
  function loadBrowseRoot() {
306
310
  try {
307
- const content = readFileSync(CONFIG_FILE, "utf-8");
311
+ const content = readFileSync(configFile(), "utf-8");
308
312
  const match = content.match(/browse_root:\s*(.+)/);
309
313
  if (match?.[1]) return match[1].trim();
310
314
  } catch {
@@ -313,7 +317,7 @@ function loadBrowseRoot() {
313
317
  }
314
318
  function loadPublicUrl() {
315
319
  try {
316
- const content = readFileSync(CONFIG_FILE, "utf-8");
320
+ const content = readFileSync(configFile(), "utf-8");
317
321
  const match = content.match(/public_url:\s*(.+)/);
318
322
  if (match?.[1]) return match[1].trim();
319
323
  } catch {
@@ -322,7 +326,7 @@ function loadPublicUrl() {
322
326
  }
323
327
  function loadCacheDir() {
324
328
  try {
325
- const content = readFileSync(CONFIG_FILE, "utf-8");
329
+ const content = readFileSync(configFile(), "utf-8");
326
330
  const match = content.match(/cache_dir:\s*(.+)/);
327
331
  if (match?.[1]) return match[1].trim();
328
332
  } catch {
@@ -331,7 +335,7 @@ function loadCacheDir() {
331
335
  }
332
336
  function loadTailSize() {
333
337
  try {
334
- const content = readFileSync(CONFIG_FILE, "utf-8");
338
+ const content = readFileSync(configFile(), "utf-8");
335
339
  const match = content.match(/tail_size:\s*(\d+)/);
336
340
  if (match?.[1]) return Number.parseInt(match[1], 10);
337
341
  } catch {
@@ -361,10 +365,11 @@ function stripTrailingSlash(url) {
361
365
  return url.endsWith("/") ? url.slice(0, -1) : url;
362
366
  }
363
367
  function setApiKey(key) {
364
- mkdirSync(CONFIG_DIR, { recursive: true });
368
+ const file = configFile();
369
+ mkdirSync(configDir(), { recursive: true });
365
370
  let content = "";
366
371
  try {
367
- content = readFileSync(CONFIG_FILE, "utf-8");
372
+ content = readFileSync(file, "utf-8");
368
373
  } catch (err) {
369
374
  if (err.code !== "ENOENT") throw err;
370
375
  }
@@ -380,10 +385,10 @@ function setApiKey(key) {
380
385
  ${apiKeyLine}
381
386
  `;
382
387
  }
383
- const tmpFile = `${CONFIG_FILE}.tmp`;
388
+ const tmpFile = `${file}.tmp`;
384
389
  writeFileSync(tmpFile, updated, { encoding: "utf-8", mode: 384 });
385
390
  chmodSync(tmpFile, 384);
386
- renameSync(tmpFile, CONFIG_FILE);
391
+ renameSync(tmpFile, file);
387
392
  }
388
393
 
389
394
  // src/db/config.ts
@@ -4843,10 +4848,17 @@ var StreamerServer = class {
4843
4848
  });
4844
4849
  }
4845
4850
  rotateApiKey() {
4851
+ const oldKey = this.apiKey;
4846
4852
  const newKey = generateApiKey();
4847
4853
  const persisted = this.apiKeySource === "config";
4848
4854
  if (persisted) setApiKey(newKey);
4849
4855
  this.apiKey = newKey;
4856
+ this.log.info("API key rotated", {
4857
+ event: "auth.api_key_rotated",
4858
+ oldKeyMasked: `${oldKey.slice(0, 6)}\u2026`,
4859
+ newKeyMasked: `${newKey.slice(0, 6)}\u2026`,
4860
+ persisted
4861
+ });
4850
4862
  return { newKey, persisted };
4851
4863
  }
4852
4864
  checkRateLimit(map, key, limit, windowMs) {