@clawchatsai/connector 0.0.42 → 0.0.43

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +17 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawchatsai/connector",
3
- "version": "0.0.42",
3
+ "version": "0.0.43",
4
4
  "type": "module",
5
5
  "description": "ClawChats OpenClaw plugin — P2P tunnel + local API bridge",
6
6
  "main": "dist/index.js",
package/server.js CHANGED
@@ -334,14 +334,14 @@ function getActiveDb() {
334
334
  return getDb(getWorkspaces().active);
335
335
  }
336
336
 
337
- let _globalDb = null;
338
- function getGlobalDb() {
339
- if (_globalDb) return _globalDb;
340
- fs.mkdirSync(DATA_DIR, { recursive: true });
341
- const dbPath = path.join(DATA_DIR, 'global.db');
342
- _globalDb = new Database(dbPath);
343
- _globalDb.pragma('journal_mode = WAL');
344
- _globalDb.exec(`
337
+ const _globalDbCache = new Map(); // keyed by resolved dbPath
338
+ function getGlobalDb(dataDir = DATA_DIR) {
339
+ const dbPath = path.join(dataDir, 'global.db');
340
+ if (_globalDbCache.has(dbPath)) return _globalDbCache.get(dbPath);
341
+ fs.mkdirSync(dataDir, { recursive: true });
342
+ const db = new Database(dbPath);
343
+ db.pragma('journal_mode = WAL');
344
+ db.exec(`
345
345
  CREATE TABLE IF NOT EXISTS custom_emojis (
346
346
  name TEXT NOT NULL,
347
347
  pack TEXT NOT NULL DEFAULT 'slackmojis',
@@ -351,7 +351,8 @@ function getGlobalDb() {
351
351
  PRIMARY KEY (name, pack)
352
352
  )
353
353
  `);
354
- return _globalDb;
354
+ _globalDbCache.set(dbPath, db);
355
+ return db;
355
356
  }
356
357
 
357
358
  function closeDb(workspaceName) {
@@ -363,11 +364,10 @@ function closeDb(workspaceName) {
363
364
  }
364
365
 
365
366
  function closeAllDbs() {
366
- for (const [name, db] of dbCache) {
367
- db.close();
368
- }
367
+ for (const [, db] of dbCache) db.close();
369
368
  dbCache.clear();
370
- if (_globalDb) { _globalDb.close(); _globalDb = null; }
369
+ for (const [, db] of _globalDbCache) db.close();
370
+ _globalDbCache.clear();
371
371
  }
372
372
 
373
373
  function _createFtsTables(db) {
@@ -4417,7 +4417,7 @@ export function createApp(config = {}) {
4417
4417
  // Custom emoji listing (no auth)
4418
4418
  if (method === 'GET' && urlPath === '/api/emoji') {
4419
4419
  try {
4420
- const db = getGlobalDb();
4420
+ const db = getGlobalDb(_DATA_DIR);
4421
4421
  const rows = db.prepare('SELECT name, pack, url, mime_type FROM custom_emojis ORDER BY created_at DESC').all();
4422
4422
  res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'public, max-age=300' });
4423
4423
  return res.end(JSON.stringify(rows));
@@ -4461,7 +4461,7 @@ export function createApp(config = {}) {
4461
4461
  if (urlLower.endsWith('.gif')) mimeType = 'image/gif';
4462
4462
  else if (urlLower.endsWith('.webp')) mimeType = 'image/webp';
4463
4463
  else if (urlLower.endsWith('.jpg') || urlLower.endsWith('.jpeg')) mimeType = 'image/jpeg';
4464
- const db = getGlobalDb();
4464
+ const db = getGlobalDb(_DATA_DIR);
4465
4465
  db.prepare('INSERT OR REPLACE INTO custom_emojis (name, pack, url, mime_type) VALUES (?, ?, ?, ?)').run(safeName, targetPack, url, mimeType);
4466
4466
  res.writeHead(200, { 'Content-Type': 'application/json' });
4467
4467
  return res.end(JSON.stringify({ name: safeName, pack: targetPack, url, mime_type: mimeType }));
@@ -4473,7 +4473,7 @@ export function createApp(config = {}) {
4473
4473
  try {
4474
4474
  const { name, pack } = await parseBody(req);
4475
4475
  if (!name || !pack) { res.writeHead(400, { 'Content-Type': 'application/json' }); return res.end(JSON.stringify({ error: 'Missing name or pack' })); }
4476
- const db = getGlobalDb();
4476
+ const db = getGlobalDb(_DATA_DIR);
4477
4477
  db.prepare('DELETE FROM custom_emojis WHERE name = ? AND pack = ?').run(name, pack);
4478
4478
  res.writeHead(200, { 'Content-Type': 'application/json' });
4479
4479
  return res.end(JSON.stringify({ ok: true }));
@@ -4630,7 +4630,7 @@ if (isDirectRun) {
4630
4630
  app.gatewayClient.connect();
4631
4631
 
4632
4632
  // Initialize global DB (custom emojis, etc.)
4633
- getGlobalDb();
4633
+ getGlobalDb(_DATA_DIR);
4634
4634
  });
4635
4635
 
4636
4636
  // Graceful shutdown