@xiaou66/vite-plugin-vue-mcp-next 1.3.0 → 1.3.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.
@@ -142,25 +142,141 @@ var DEFAULT_OPTIONS = {
142
142
  var import_nanoid = require("nanoid");
143
143
 
144
144
  // src/shared/serialization.ts
145
+ var RPC_CIRCULAR_VALUE = "[Circular]";
146
+ var RPC_UNREADABLE_VALUE = "[Unreadable]";
147
+ function toRpcSafeValue(value) {
148
+ return toRpcSafeChildValue(value, /* @__PURE__ */ new WeakSet(), false);
149
+ }
145
150
  function safeStringify(value) {
146
151
  if (typeof value === "string") {
147
152
  return value;
148
153
  }
149
- const seen = /* @__PURE__ */ new WeakSet();
150
- const serialized = JSON.stringify(
151
- value,
152
- (_key, current) => {
153
- if (typeof current !== "object" || current === null) {
154
- return current;
155
- }
156
- if (seen.has(current)) {
157
- return "[Circular]";
158
- }
159
- seen.add(current);
160
- return current;
154
+ const safeValue = toRpcSafeValue(value);
155
+ return safeValue === void 0 ? "undefined" : JSON.stringify(safeValue);
156
+ }
157
+ function toRpcSafeChildValue(value, seen, arrayItem) {
158
+ if (value === null) {
159
+ return null;
160
+ }
161
+ switch (typeof value) {
162
+ case "string":
163
+ case "boolean":
164
+ return value;
165
+ case "number":
166
+ return Number.isFinite(value) ? value : String(value);
167
+ case "bigint":
168
+ return value.toString();
169
+ case "symbol":
170
+ return toSymbolPlaceholder(value);
171
+ case "undefined":
172
+ case "function":
173
+ return arrayItem ? null : void 0;
174
+ case "object":
175
+ return toRpcSafeObject(value, seen);
176
+ default:
177
+ return arrayItem ? null : void 0;
178
+ }
179
+ }
180
+ function toRpcSafeObject(value, seen) {
181
+ if (seen.has(value)) {
182
+ return RPC_CIRCULAR_VALUE;
183
+ }
184
+ seen.add(value);
185
+ try {
186
+ if (value instanceof Date) {
187
+ return toSafeDate(value);
161
188
  }
162
- );
163
- return serialized;
189
+ if (value instanceof Error) {
190
+ return toSafeError(value, seen);
191
+ }
192
+ if (Array.isArray(value)) {
193
+ return toSafeArray(value, seen);
194
+ }
195
+ return toSafeRecord(value, seen);
196
+ } finally {
197
+ seen.delete(value);
198
+ }
199
+ }
200
+ function toSafeArray(values, seen) {
201
+ return values.map((item) => toRpcSafeChildValue(item, seen, true) ?? null);
202
+ }
203
+ function toSafeRecord(value, seen) {
204
+ const keys = getEnumerableKeys(value);
205
+ if (!keys) {
206
+ return RPC_UNREADABLE_VALUE;
207
+ }
208
+ if (keys.length === 0 && !isPlainRecord(value)) {
209
+ return toObjectSummary(value);
210
+ }
211
+ const result = {};
212
+ keys.forEach((key) => {
213
+ if (key === "toJSON") {
214
+ return;
215
+ }
216
+ const field = readObjectField(value, key);
217
+ if (!field.ok) {
218
+ result[key] = RPC_UNREADABLE_VALUE;
219
+ return;
220
+ }
221
+ const safeValue = toRpcSafeChildValue(field.value, seen, false);
222
+ if (safeValue !== void 0) {
223
+ result[key] = safeValue;
224
+ }
225
+ });
226
+ return result;
227
+ }
228
+ function toSafeError(error, seen) {
229
+ const result = {
230
+ name: error.name,
231
+ message: error.message
232
+ };
233
+ const stack = readObjectField(error, "stack");
234
+ if (stack.ok && typeof stack.value === "string") {
235
+ result.stack = stack.value;
236
+ }
237
+ if ("cause" in error) {
238
+ const cause = readObjectField(error, "cause");
239
+ result.cause = cause.ok ? toRpcSafeChildValue(cause.value, seen, false) ?? null : RPC_UNREADABLE_VALUE;
240
+ }
241
+ return result;
242
+ }
243
+ function toSafeDate(value) {
244
+ return Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
245
+ }
246
+ function getEnumerableKeys(value) {
247
+ try {
248
+ return Object.keys(value);
249
+ } catch {
250
+ return void 0;
251
+ }
252
+ }
253
+ function readObjectField(value, key) {
254
+ try {
255
+ return {
256
+ ok: true,
257
+ value: value[key]
258
+ };
259
+ } catch {
260
+ return { ok: false };
261
+ }
262
+ }
263
+ function isPlainRecord(value) {
264
+ try {
265
+ const prototype = Object.getPrototypeOf(value);
266
+ return prototype === Object.prototype || prototype === null;
267
+ } catch {
268
+ return false;
269
+ }
270
+ }
271
+ function toObjectSummary(value) {
272
+ try {
273
+ return Object.prototype.toString.call(value);
274
+ } catch {
275
+ return "[Object]";
276
+ }
277
+ }
278
+ function toSymbolPlaceholder(value) {
279
+ return value.description ? `[Symbol(${value.description})]` : "[Symbol]";
164
280
  }
165
281
 
166
282
  // src/runtime/consoleHook.ts
@@ -173,13 +289,14 @@ function installConsoleHook(options) {
173
289
  debug: console.debug
174
290
  };
175
291
  const emit = (level, args) => {
292
+ const serializedArgs = serializeConsoleArgs(args);
176
293
  options.send({
177
294
  id: (0, import_nanoid.nanoid)(),
178
295
  pageId: options.pageId,
179
296
  source: "hook",
180
297
  level,
181
- message: args.map((arg) => safeStringify(arg)).join(" "),
182
- args,
298
+ message: serializedArgs.join(" "),
299
+ args: serializedArgs,
183
300
  timestamp: Date.now()
184
301
  });
185
302
  };
@@ -206,6 +323,9 @@ function installConsoleHook(options) {
206
323
  window.removeEventListener("error", onError);
207
324
  };
208
325
  }
326
+ function serializeConsoleArgs(args) {
327
+ return args.map((arg) => safeStringify(arg));
328
+ }
209
329
 
210
330
  // src/runtime/elementRegistry.ts
211
331
  var import_nanoid2 = require("nanoid");
@@ -2039,6 +2159,15 @@ function installVueBridge(hot) {
2039
2159
  );
2040
2160
  rpcRef.current = rpc;
2041
2161
  }
2162
+ function toVueRpcPayload(value) {
2163
+ return toRpcSafeValue(value);
2164
+ }
2165
+ function toVueRpcJsonPayload(value) {
2166
+ return JSON.stringify(toRpcSafeValue(value) ?? null);
2167
+ }
2168
+ function toVueRpcPrettyJsonPayload(value) {
2169
+ return JSON.stringify(toRpcSafeValue(value), null, 2);
2170
+ }
2042
2171
  function createClientVueRuntimeRpc(getRpc) {
2043
2172
  return {
2044
2173
  ...createRuntimeDevtoolsRpc(getRpc),
@@ -2047,7 +2176,10 @@ function createClientVueRuntimeRpc(getRpc) {
2047
2176
  inspectorId: COMPONENTS_INSPECTOR_ID,
2048
2177
  filter: query.componentName ?? ""
2049
2178
  });
2050
- getRpc().onInspectorTreeUpdated(query.event, inspectorTree[0]);
2179
+ getRpc().onInspectorTreeUpdated(
2180
+ query.event,
2181
+ toVueRpcPayload(inspectorTree[0])
2182
+ );
2051
2183
  },
2052
2184
  onInspectorTreeUpdated: () => void 0,
2053
2185
  async getInspectorState(query) {
@@ -2055,7 +2187,7 @@ function createClientVueRuntimeRpc(getRpc) {
2055
2187
  if (!targetNode) {
2056
2188
  getRpc().onInspectorStateUpdated(
2057
2189
  query.event,
2058
- createMissingComponentError(query.componentName)
2190
+ toVueRpcPayload(createMissingComponentError(query.componentName))
2059
2191
  );
2060
2192
  return;
2061
2193
  }
@@ -2063,7 +2195,10 @@ function createClientVueRuntimeRpc(getRpc) {
2063
2195
  inspectorId: COMPONENTS_INSPECTOR_ID,
2064
2196
  nodeId: targetNode.id
2065
2197
  });
2066
- getRpc().onInspectorStateUpdated(query.event, (0, import_devtools_kit.stringify)(inspectorState));
2198
+ getRpc().onInspectorStateUpdated(
2199
+ query.event,
2200
+ toVueRpcJsonPayload(inspectorState)
2201
+ );
2067
2202
  },
2068
2203
  onInspectorStateUpdated: () => void 0,
2069
2204
  async editComponentState(query) {
@@ -2100,7 +2235,7 @@ function createClientVueRuntimeRpc(getRpc) {
2100
2235
  getRouterInfo(query) {
2101
2236
  getRpc().onRouterInfoUpdated(
2102
2237
  query.event,
2103
- JSON.stringify(import_devtools_kit.devtoolsRouterInfo, null, 2)
2238
+ toVueRpcPrettyJsonPayload(import_devtools_kit.devtoolsRouterInfo)
2104
2239
  );
2105
2240
  },
2106
2241
  onRouterInfoUpdated: () => void 0,
@@ -2111,7 +2246,7 @@ function createClientVueRuntimeRpc(getRpc) {
2111
2246
  filter: ""
2112
2247
  })
2113
2248
  );
2114
- getRpc().onPiniaTreeUpdated(query.event, inspectorTree);
2249
+ getRpc().onPiniaTreeUpdated(query.event, toVueRpcPayload(inspectorTree));
2115
2250
  },
2116
2251
  onPiniaTreeUpdated: () => void 0,
2117
2252
  async getPiniaState(query) {
@@ -2126,7 +2261,7 @@ function createClientVueRuntimeRpc(getRpc) {
2126
2261
  }
2127
2262
  return import_devtools_kit.devtools.ctx.api.getInspectorState(payload);
2128
2263
  });
2129
- getRpc().onPiniaInfoUpdated(query.event, (0, import_devtools_kit.stringify)(result));
2264
+ getRpc().onPiniaInfoUpdated(query.event, toVueRpcJsonPayload(result));
2130
2265
  },
2131
2266
  onPiniaInfoUpdated: () => void 0,
2132
2267
  async recordPerformance(query) {
@@ -2134,7 +2269,7 @@ function createClientVueRuntimeRpc(getRpc) {
2134
2269
  if (!collector) {
2135
2270
  getRpc().onPerformanceRecorded(
2136
2271
  query.event,
2137
- createPerformanceUnavailableError()
2272
+ toVueRpcPayload(createPerformanceUnavailableError())
2138
2273
  );
2139
2274
  return;
2140
2275
  }
@@ -2144,11 +2279,11 @@ function createClientVueRuntimeRpc(getRpc) {
2144
2279
  includeMemory: query.includeMemory,
2145
2280
  includeStacks: query.includeStacks
2146
2281
  });
2147
- getRpc().onPerformanceRecorded(query.event, report);
2282
+ getRpc().onPerformanceRecorded(query.event, toVueRpcPayload(report));
2148
2283
  } catch (error) {
2149
2284
  getRpc().onPerformanceRecorded(
2150
2285
  query.event,
2151
- createPerformanceError(error)
2286
+ toVueRpcPayload(createPerformanceError(error))
2152
2287
  );
2153
2288
  }
2154
2289
  },
@@ -2158,7 +2293,7 @@ function createClientVueRuntimeRpc(getRpc) {
2158
2293
  if (!collector) {
2159
2294
  getRpc().onPerformanceRecordingStarted(
2160
2295
  query.event,
2161
- createPerformanceUnavailableError()
2296
+ toVueRpcPayload(createPerformanceUnavailableError())
2162
2297
  );
2163
2298
  return;
2164
2299
  }
@@ -2167,16 +2302,19 @@ function createClientVueRuntimeRpc(getRpc) {
2167
2302
  includeMemory: query.includeMemory,
2168
2303
  includeStacks: query.includeStacks
2169
2304
  });
2170
- getRpc().onPerformanceRecordingStarted(query.event, {
2171
- ok: true,
2172
- recordingId,
2173
- startedAt: Date.now(),
2174
- source: "hook"
2175
- });
2305
+ getRpc().onPerformanceRecordingStarted(
2306
+ query.event,
2307
+ toVueRpcPayload({
2308
+ ok: true,
2309
+ recordingId,
2310
+ startedAt: Date.now(),
2311
+ source: "hook"
2312
+ })
2313
+ );
2176
2314
  } catch (error) {
2177
2315
  getRpc().onPerformanceRecordingStarted(
2178
2316
  query.event,
2179
- createPerformanceError(error)
2317
+ toVueRpcPayload(createPerformanceError(error))
2180
2318
  );
2181
2319
  }
2182
2320
  },
@@ -2186,17 +2324,20 @@ function createClientVueRuntimeRpc(getRpc) {
2186
2324
  if (!collector) {
2187
2325
  getRpc().onPerformanceRecordingStopped(
2188
2326
  query.event,
2189
- createPerformanceUnavailableError()
2327
+ toVueRpcPayload(createPerformanceUnavailableError())
2190
2328
  );
2191
2329
  return;
2192
2330
  }
2193
2331
  try {
2194
2332
  const report = collector.stop(query.recordingId);
2195
- getRpc().onPerformanceRecordingStopped(query.event, report);
2333
+ getRpc().onPerformanceRecordingStopped(
2334
+ query.event,
2335
+ toVueRpcPayload(report)
2336
+ );
2196
2337
  } catch (error) {
2197
2338
  getRpc().onPerformanceRecordingStopped(
2198
2339
  query.event,
2199
- createPerformanceError(error)
2340
+ toVueRpcPayload(createPerformanceError(error))
2200
2341
  );
2201
2342
  }
2202
2343
  },