@xiaou66/vite-plugin-vue-mcp-next 1.3.1 → 1.3.3

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,153 @@ 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, createRpcSafeContext(), 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 createRpcSafeContext() {
158
+ return {
159
+ cache: /* @__PURE__ */ new WeakMap(),
160
+ seen: /* @__PURE__ */ new WeakSet()
161
+ };
162
+ }
163
+ function toRpcSafeChildValue(value, context, arrayItem) {
164
+ if (value === null) {
165
+ return null;
166
+ }
167
+ switch (typeof value) {
168
+ case "string":
169
+ case "boolean":
170
+ return value;
171
+ case "number":
172
+ return Number.isFinite(value) ? value : String(value);
173
+ case "bigint":
174
+ return value.toString();
175
+ case "symbol":
176
+ return toSymbolPlaceholder(value);
177
+ case "undefined":
178
+ case "function":
179
+ return arrayItem ? null : void 0;
180
+ case "object":
181
+ return toRpcSafeObject(value, context);
182
+ default:
183
+ return arrayItem ? null : void 0;
184
+ }
185
+ }
186
+ function toRpcSafeObject(value, context) {
187
+ if (context.seen.has(value)) {
188
+ return RPC_CIRCULAR_VALUE;
189
+ }
190
+ const cached = context.cache.get(value);
191
+ if (cached) {
192
+ return cached;
193
+ }
194
+ context.seen.add(value);
195
+ try {
196
+ let safeValue;
197
+ if (value instanceof Date) {
198
+ safeValue = toSafeDate(value);
199
+ } else if (value instanceof Error) {
200
+ safeValue = toSafeError(value, context);
201
+ } else if (Array.isArray(value)) {
202
+ safeValue = toSafeArray(value, context);
203
+ } else {
204
+ safeValue = toSafeRecord(value, context);
161
205
  }
162
- );
163
- return serialized;
206
+ context.cache.set(value, safeValue);
207
+ return safeValue;
208
+ } finally {
209
+ context.seen.delete(value);
210
+ }
211
+ }
212
+ function toSafeArray(values, context) {
213
+ return values.map((item) => toRpcSafeChildValue(item, context, true) ?? null);
214
+ }
215
+ function toSafeRecord(value, context) {
216
+ const keys = getEnumerableKeys(value);
217
+ if (!keys) {
218
+ return RPC_UNREADABLE_VALUE;
219
+ }
220
+ if (keys.length === 0 && !isPlainRecord(value)) {
221
+ return toObjectSummary(value);
222
+ }
223
+ const result = {};
224
+ keys.forEach((key) => {
225
+ if (key === "toJSON") {
226
+ return;
227
+ }
228
+ const field = readObjectField(value, key);
229
+ if (!field.ok) {
230
+ result[key] = RPC_UNREADABLE_VALUE;
231
+ return;
232
+ }
233
+ const safeValue = toRpcSafeChildValue(field.value, context, false);
234
+ if (safeValue !== void 0) {
235
+ result[key] = safeValue;
236
+ }
237
+ });
238
+ return result;
239
+ }
240
+ function toSafeError(error, context) {
241
+ const result = {
242
+ name: error.name,
243
+ message: error.message
244
+ };
245
+ const stack = readObjectField(error, "stack");
246
+ if (stack.ok && typeof stack.value === "string") {
247
+ result.stack = stack.value;
248
+ }
249
+ if ("cause" in error) {
250
+ const cause = readObjectField(error, "cause");
251
+ result.cause = cause.ok ? toRpcSafeChildValue(cause.value, context, false) ?? null : RPC_UNREADABLE_VALUE;
252
+ }
253
+ return result;
254
+ }
255
+ function toSafeDate(value) {
256
+ return Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
257
+ }
258
+ function getEnumerableKeys(value) {
259
+ try {
260
+ return Object.keys(value);
261
+ } catch {
262
+ return void 0;
263
+ }
264
+ }
265
+ function readObjectField(value, key) {
266
+ try {
267
+ return {
268
+ ok: true,
269
+ value: value[key]
270
+ };
271
+ } catch {
272
+ return { ok: false };
273
+ }
274
+ }
275
+ function isPlainRecord(value) {
276
+ try {
277
+ const prototype = Object.getPrototypeOf(value);
278
+ return prototype === Object.prototype || prototype === null;
279
+ } catch {
280
+ return false;
281
+ }
282
+ }
283
+ function toObjectSummary(value) {
284
+ try {
285
+ return Object.prototype.toString.call(value);
286
+ } catch {
287
+ return "[Object]";
288
+ }
289
+ }
290
+ function toSymbolPlaceholder(value) {
291
+ return value.description ? `[Symbol(${value.description})]` : "[Symbol]";
164
292
  }
165
293
 
166
294
  // src/runtime/consoleHook.ts
@@ -2043,6 +2171,15 @@ function installVueBridge(hot) {
2043
2171
  );
2044
2172
  rpcRef.current = rpc;
2045
2173
  }
2174
+ function toVueRpcPayload(value) {
2175
+ return toRpcSafeValue(value);
2176
+ }
2177
+ function toVueRpcJsonPayload(value) {
2178
+ return JSON.stringify(toRpcSafeValue(value) ?? null);
2179
+ }
2180
+ function toVueRpcPrettyJsonPayload(value) {
2181
+ return JSON.stringify(toRpcSafeValue(value), null, 2);
2182
+ }
2046
2183
  function createClientVueRuntimeRpc(getRpc) {
2047
2184
  return {
2048
2185
  ...createRuntimeDevtoolsRpc(getRpc),
@@ -2051,7 +2188,10 @@ function createClientVueRuntimeRpc(getRpc) {
2051
2188
  inspectorId: COMPONENTS_INSPECTOR_ID,
2052
2189
  filter: query.componentName ?? ""
2053
2190
  });
2054
- getRpc().onInspectorTreeUpdated(query.event, inspectorTree[0]);
2191
+ getRpc().onInspectorTreeUpdated(
2192
+ query.event,
2193
+ toVueRpcPayload(inspectorTree[0])
2194
+ );
2055
2195
  },
2056
2196
  onInspectorTreeUpdated: () => void 0,
2057
2197
  async getInspectorState(query) {
@@ -2059,7 +2199,7 @@ function createClientVueRuntimeRpc(getRpc) {
2059
2199
  if (!targetNode) {
2060
2200
  getRpc().onInspectorStateUpdated(
2061
2201
  query.event,
2062
- createMissingComponentError(query.componentName)
2202
+ toVueRpcPayload(createMissingComponentError(query.componentName))
2063
2203
  );
2064
2204
  return;
2065
2205
  }
@@ -2067,7 +2207,10 @@ function createClientVueRuntimeRpc(getRpc) {
2067
2207
  inspectorId: COMPONENTS_INSPECTOR_ID,
2068
2208
  nodeId: targetNode.id
2069
2209
  });
2070
- getRpc().onInspectorStateUpdated(query.event, (0, import_devtools_kit.stringify)(inspectorState));
2210
+ getRpc().onInspectorStateUpdated(
2211
+ query.event,
2212
+ toVueRpcJsonPayload(inspectorState)
2213
+ );
2071
2214
  },
2072
2215
  onInspectorStateUpdated: () => void 0,
2073
2216
  async editComponentState(query) {
@@ -2104,7 +2247,7 @@ function createClientVueRuntimeRpc(getRpc) {
2104
2247
  getRouterInfo(query) {
2105
2248
  getRpc().onRouterInfoUpdated(
2106
2249
  query.event,
2107
- JSON.stringify(import_devtools_kit.devtoolsRouterInfo, null, 2)
2250
+ toVueRpcPrettyJsonPayload(import_devtools_kit.devtoolsRouterInfo)
2108
2251
  );
2109
2252
  },
2110
2253
  onRouterInfoUpdated: () => void 0,
@@ -2115,7 +2258,7 @@ function createClientVueRuntimeRpc(getRpc) {
2115
2258
  filter: ""
2116
2259
  })
2117
2260
  );
2118
- getRpc().onPiniaTreeUpdated(query.event, inspectorTree);
2261
+ getRpc().onPiniaTreeUpdated(query.event, toVueRpcPayload(inspectorTree));
2119
2262
  },
2120
2263
  onPiniaTreeUpdated: () => void 0,
2121
2264
  async getPiniaState(query) {
@@ -2130,7 +2273,7 @@ function createClientVueRuntimeRpc(getRpc) {
2130
2273
  }
2131
2274
  return import_devtools_kit.devtools.ctx.api.getInspectorState(payload);
2132
2275
  });
2133
- getRpc().onPiniaInfoUpdated(query.event, (0, import_devtools_kit.stringify)(result));
2276
+ getRpc().onPiniaInfoUpdated(query.event, toVueRpcJsonPayload(result));
2134
2277
  },
2135
2278
  onPiniaInfoUpdated: () => void 0,
2136
2279
  async recordPerformance(query) {
@@ -2138,7 +2281,7 @@ function createClientVueRuntimeRpc(getRpc) {
2138
2281
  if (!collector) {
2139
2282
  getRpc().onPerformanceRecorded(
2140
2283
  query.event,
2141
- createPerformanceUnavailableError()
2284
+ toVueRpcPayload(createPerformanceUnavailableError())
2142
2285
  );
2143
2286
  return;
2144
2287
  }
@@ -2148,11 +2291,11 @@ function createClientVueRuntimeRpc(getRpc) {
2148
2291
  includeMemory: query.includeMemory,
2149
2292
  includeStacks: query.includeStacks
2150
2293
  });
2151
- getRpc().onPerformanceRecorded(query.event, report);
2294
+ getRpc().onPerformanceRecorded(query.event, toVueRpcPayload(report));
2152
2295
  } catch (error) {
2153
2296
  getRpc().onPerformanceRecorded(
2154
2297
  query.event,
2155
- createPerformanceError(error)
2298
+ toVueRpcPayload(createPerformanceError(error))
2156
2299
  );
2157
2300
  }
2158
2301
  },
@@ -2162,7 +2305,7 @@ function createClientVueRuntimeRpc(getRpc) {
2162
2305
  if (!collector) {
2163
2306
  getRpc().onPerformanceRecordingStarted(
2164
2307
  query.event,
2165
- createPerformanceUnavailableError()
2308
+ toVueRpcPayload(createPerformanceUnavailableError())
2166
2309
  );
2167
2310
  return;
2168
2311
  }
@@ -2171,16 +2314,19 @@ function createClientVueRuntimeRpc(getRpc) {
2171
2314
  includeMemory: query.includeMemory,
2172
2315
  includeStacks: query.includeStacks
2173
2316
  });
2174
- getRpc().onPerformanceRecordingStarted(query.event, {
2175
- ok: true,
2176
- recordingId,
2177
- startedAt: Date.now(),
2178
- source: "hook"
2179
- });
2317
+ getRpc().onPerformanceRecordingStarted(
2318
+ query.event,
2319
+ toVueRpcPayload({
2320
+ ok: true,
2321
+ recordingId,
2322
+ startedAt: Date.now(),
2323
+ source: "hook"
2324
+ })
2325
+ );
2180
2326
  } catch (error) {
2181
2327
  getRpc().onPerformanceRecordingStarted(
2182
2328
  query.event,
2183
- createPerformanceError(error)
2329
+ toVueRpcPayload(createPerformanceError(error))
2184
2330
  );
2185
2331
  }
2186
2332
  },
@@ -2190,17 +2336,20 @@ function createClientVueRuntimeRpc(getRpc) {
2190
2336
  if (!collector) {
2191
2337
  getRpc().onPerformanceRecordingStopped(
2192
2338
  query.event,
2193
- createPerformanceUnavailableError()
2339
+ toVueRpcPayload(createPerformanceUnavailableError())
2194
2340
  );
2195
2341
  return;
2196
2342
  }
2197
2343
  try {
2198
2344
  const report = collector.stop(query.recordingId);
2199
- getRpc().onPerformanceRecordingStopped(query.event, report);
2345
+ getRpc().onPerformanceRecordingStopped(
2346
+ query.event,
2347
+ toVueRpcPayload(report)
2348
+ );
2200
2349
  } catch (error) {
2201
2350
  getRpc().onPerformanceRecordingStopped(
2202
2351
  query.event,
2203
- createPerformanceError(error)
2352
+ toVueRpcPayload(createPerformanceError(error))
2204
2353
  );
2205
2354
  }
2206
2355
  },