mock-mcp 0.5.0 → 0.5.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.
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var http2 = require('http');
4
- var fs2 = require('fs/promises');
4
+ var fs = require('fs/promises');
5
5
  var crypto = require('crypto');
6
6
  var ws = require('ws');
7
7
  var fssync = require('fs');
@@ -14,7 +14,7 @@ require('module');
14
14
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
15
 
16
16
  var http2__default = /*#__PURE__*/_interopDefault(http2);
17
- var fs2__default = /*#__PURE__*/_interopDefault(fs2);
17
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
18
18
  var crypto__default = /*#__PURE__*/_interopDefault(crypto);
19
19
  var fssync__default = /*#__PURE__*/_interopDefault(fssync);
20
20
  var os__default = /*#__PURE__*/_interopDefault(os);
@@ -49,6 +49,8 @@ var __importMetaUrl = (function() {
49
49
  return 'file:///unknown';
50
50
  }
51
51
  })();
52
+ function debugLog(_msg) {
53
+ }
52
54
  (() => {
53
55
  try {
54
56
  const metaUrl = __importMetaUrl;
@@ -92,12 +94,48 @@ function getPaths(projectId, cacheDir) {
92
94
  return { base, registryPath, lockPath, ipcPath };
93
95
  }
94
96
  async function writeRegistry(registryPath, registry) {
95
- await fs2__default.default.writeFile(registryPath, JSON.stringify(registry, null, 2), {
97
+ await fs__default.default.writeFile(registryPath, JSON.stringify(registry, null, 2), {
96
98
  encoding: "utf-8",
97
99
  mode: 384
98
100
  // Read/write for owner only
99
101
  });
100
102
  }
103
+ function getGlobalIndexPath(cacheDir) {
104
+ const base = path__default.default.join(getCacheDir(cacheDir), "mock-mcp");
105
+ return path__default.default.join(base, "active-daemons.json");
106
+ }
107
+ async function readGlobalIndex(cacheDir) {
108
+ const indexPath = getGlobalIndexPath(cacheDir);
109
+ try {
110
+ const txt = await fs__default.default.readFile(indexPath, "utf-8");
111
+ return JSON.parse(txt);
112
+ } catch {
113
+ return { daemons: [], updatedAt: (/* @__PURE__ */ new Date()).toISOString() };
114
+ }
115
+ }
116
+ async function writeGlobalIndex(index, cacheDir) {
117
+ const indexPath = getGlobalIndexPath(cacheDir);
118
+ const base = path__default.default.dirname(indexPath);
119
+ await fs__default.default.mkdir(base, { recursive: true });
120
+ await fs__default.default.writeFile(indexPath, JSON.stringify(index, null, 2), {
121
+ encoding: "utf-8",
122
+ mode: 384
123
+ });
124
+ }
125
+ async function registerDaemonGlobally(entry, cacheDir) {
126
+ const index = await readGlobalIndex(cacheDir);
127
+ index.daemons = index.daemons.filter((d) => d.projectId !== entry.projectId);
128
+ index.daemons.push(entry);
129
+ index.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
130
+ await writeGlobalIndex(index, cacheDir);
131
+ debugLog(`Registered daemon ${entry.projectId} in global index`);
132
+ }
133
+ async function unregisterDaemonGlobally(projectId, cacheDir) {
134
+ const index = await readGlobalIndex(cacheDir);
135
+ index.daemons = index.daemons.filter((d) => d.projectId !== projectId);
136
+ index.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
137
+ await writeGlobalIndex(index, cacheDir);
138
+ }
101
139
 
102
140
  // src/shared/protocol.ts
103
141
  var HELLO_TEST = "HELLO_TEST";
@@ -173,10 +211,10 @@ var MockMcpDaemon = class {
173
211
  async start() {
174
212
  const projectId = computeProjectId(this.opts.projectRoot);
175
213
  const { base, registryPath, ipcPath } = getPaths(projectId, this.opts.cacheDir);
176
- await fs2__default.default.mkdir(base, { recursive: true });
214
+ await fs__default.default.mkdir(base, { recursive: true });
177
215
  if (process.platform !== "win32") {
178
216
  try {
179
- await fs2__default.default.rm(ipcPath);
217
+ await fs__default.default.rm(ipcPath);
180
218
  } catch {
181
219
  }
182
220
  }
@@ -212,6 +250,16 @@ var MockMcpDaemon = class {
212
250
  version: this.opts.version
213
251
  };
214
252
  await writeRegistry(registryPath, registry);
253
+ const globalEntry = {
254
+ projectId,
255
+ projectRoot: this.opts.projectRoot,
256
+ ipcPath,
257
+ registryPath,
258
+ pid: process.pid,
259
+ startedAt: registry.startedAt,
260
+ version: this.opts.version
261
+ };
262
+ await registerDaemonGlobally(globalEntry, this.opts.cacheDir);
215
263
  this.sweepTimer = setInterval(() => this.sweepExpiredClaims(), this.opts.sweepIntervalMs);
216
264
  this.sweepTimer.unref?.();
217
265
  this.resetIdleTimer();
@@ -240,6 +288,8 @@ var MockMcpDaemon = class {
240
288
  });
241
289
  this.batches.clear();
242
290
  this.pendingQueue.length = 0;
291
+ const projectId = computeProjectId(this.opts.projectRoot);
292
+ await unregisterDaemonGlobally(projectId, this.opts.cacheDir);
243
293
  this.logger.error("\u{1F44B} Daemon stopped");
244
294
  }
245
295
  // ===========================================================================
@@ -1,5 +1,5 @@
1
1
  import http2 from 'http';
2
- import fs2 from 'fs/promises';
2
+ import fs from 'fs/promises';
3
3
  import crypto from 'crypto';
4
4
  import { WebSocketServer, WebSocket } from 'ws';
5
5
  import fssync from 'fs';
@@ -10,6 +10,8 @@ import { fileURLToPath } from 'url';
10
10
  import 'module';
11
11
 
12
12
  // src/daemon/daemon.ts
13
+ function debugLog(_msg) {
14
+ }
13
15
  (() => {
14
16
  try {
15
17
  const metaUrl = import.meta.url;
@@ -53,12 +55,48 @@ function getPaths(projectId, cacheDir) {
53
55
  return { base, registryPath, lockPath, ipcPath };
54
56
  }
55
57
  async function writeRegistry(registryPath, registry) {
56
- await fs2.writeFile(registryPath, JSON.stringify(registry, null, 2), {
58
+ await fs.writeFile(registryPath, JSON.stringify(registry, null, 2), {
57
59
  encoding: "utf-8",
58
60
  mode: 384
59
61
  // Read/write for owner only
60
62
  });
61
63
  }
64
+ function getGlobalIndexPath(cacheDir) {
65
+ const base = path.join(getCacheDir(cacheDir), "mock-mcp");
66
+ return path.join(base, "active-daemons.json");
67
+ }
68
+ async function readGlobalIndex(cacheDir) {
69
+ const indexPath = getGlobalIndexPath(cacheDir);
70
+ try {
71
+ const txt = await fs.readFile(indexPath, "utf-8");
72
+ return JSON.parse(txt);
73
+ } catch {
74
+ return { daemons: [], updatedAt: (/* @__PURE__ */ new Date()).toISOString() };
75
+ }
76
+ }
77
+ async function writeGlobalIndex(index, cacheDir) {
78
+ const indexPath = getGlobalIndexPath(cacheDir);
79
+ const base = path.dirname(indexPath);
80
+ await fs.mkdir(base, { recursive: true });
81
+ await fs.writeFile(indexPath, JSON.stringify(index, null, 2), {
82
+ encoding: "utf-8",
83
+ mode: 384
84
+ });
85
+ }
86
+ async function registerDaemonGlobally(entry, cacheDir) {
87
+ const index = await readGlobalIndex(cacheDir);
88
+ index.daemons = index.daemons.filter((d) => d.projectId !== entry.projectId);
89
+ index.daemons.push(entry);
90
+ index.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
91
+ await writeGlobalIndex(index, cacheDir);
92
+ debugLog(`Registered daemon ${entry.projectId} in global index`);
93
+ }
94
+ async function unregisterDaemonGlobally(projectId, cacheDir) {
95
+ const index = await readGlobalIndex(cacheDir);
96
+ index.daemons = index.daemons.filter((d) => d.projectId !== projectId);
97
+ index.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
98
+ await writeGlobalIndex(index, cacheDir);
99
+ }
62
100
 
63
101
  // src/shared/protocol.ts
64
102
  var HELLO_TEST = "HELLO_TEST";
@@ -134,10 +172,10 @@ var MockMcpDaemon = class {
134
172
  async start() {
135
173
  const projectId = computeProjectId(this.opts.projectRoot);
136
174
  const { base, registryPath, ipcPath } = getPaths(projectId, this.opts.cacheDir);
137
- await fs2.mkdir(base, { recursive: true });
175
+ await fs.mkdir(base, { recursive: true });
138
176
  if (process.platform !== "win32") {
139
177
  try {
140
- await fs2.rm(ipcPath);
178
+ await fs.rm(ipcPath);
141
179
  } catch {
142
180
  }
143
181
  }
@@ -173,6 +211,16 @@ var MockMcpDaemon = class {
173
211
  version: this.opts.version
174
212
  };
175
213
  await writeRegistry(registryPath, registry);
214
+ const globalEntry = {
215
+ projectId,
216
+ projectRoot: this.opts.projectRoot,
217
+ ipcPath,
218
+ registryPath,
219
+ pid: process.pid,
220
+ startedAt: registry.startedAt,
221
+ version: this.opts.version
222
+ };
223
+ await registerDaemonGlobally(globalEntry, this.opts.cacheDir);
176
224
  this.sweepTimer = setInterval(() => this.sweepExpiredClaims(), this.opts.sweepIntervalMs);
177
225
  this.sweepTimer.unref?.();
178
226
  this.resetIdleTimer();
@@ -201,6 +249,8 @@ var MockMcpDaemon = class {
201
249
  });
202
250
  this.batches.clear();
203
251
  this.pendingQueue.length = 0;
252
+ const projectId = computeProjectId(this.opts.projectRoot);
253
+ await unregisterDaemonGlobally(projectId, this.opts.cacheDir);
204
254
  this.logger.error("\u{1F44B} Daemon stopped");
205
255
  }
206
256
  // ===========================================================================