@lensmcp/vite-plugin 1.18.6 → 1.18.8

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,2 +1,2 @@
1
- "use strict";var g=Object.defineProperty;var o=(t,e)=>g(t,"name",{value:e,configurable:!0});var f=Object.defineProperty,a=o((t,e)=>f(t,"name",{value:e,configurable:!0}),"o"),p=Object.defineProperty,u=a((t,e)=>p(t,"name",{value:e,configurable:!0}),"o");import{fingerprint as n,ulid as s}from"@lensmcp/core";const y="pending";export function envelopeToEvent(t,e=y){const r=t.tabId?{tabId:t.tabId}:{};switch(t.kind){case"error":return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"runtime",severity:"error",context:{sessionId:e,...r},fingerprint:n({kind:"runtime",identity:`error:${c(t.stack)}`,file:t.source,detail:t.message}),title:t.message,message:t.stack??t.message,location:t.source?{file:t.source,line:t.lineno,column:t.colno}:void 0,raw:t};case"unhandledrejection":return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"runtime",severity:"error",context:{sessionId:e,...r},fingerprint:n({kind:"runtime",identity:`unhandledrejection:${t.reason.slice(0,80)}`}),title:"Unhandled promise rejection",message:t.reason,raw:t};case"console":{const i=t.level==="error"?"error":t.level==="warn"?"warning":"info",m=t.args.map(d).join(" ");return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"runtime",severity:i,context:{sessionId:e,...r},fingerprint:n({kind:"runtime",identity:`console:${t.level}:${m.slice(0,80)}`}),title:`console.${t.level}`,message:m,raw:t}}case"fetch":{const i=t.error!==void 0||t.status!==void 0&&t.status>=400;return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"network",severity:i?"error":"info",context:{sessionId:e,...r},fingerprint:n({kind:"network",identity:`${t.method}:${l(t.url)}:${t.status??"err"}`}),title:`${t.method} ${t.url}${t.status!==void 0?` \u2192 ${t.status}`:""}`,message:t.error,relatedUrls:[t.url],raw:t}}case"route":return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"runtime",severity:"info",context:{sessionId:e,url:t.url,...r},fingerprint:n({kind:"runtime",identity:`route:${t.url}`}),title:`Route \u2192 ${t.url}`,raw:t};case"ready":return{id:s(),sessionId:e,timestamp:t.at,source:"client-runtime",category:"runtime",severity:"info",context:{sessionId:e,url:t.url,...r},fingerprint:n({kind:"runtime",identity:"client-ready"}),title:"Client runtime ready",message:t.userAgent,raw:t};case"publish":return{id:s(),sessionId:e,timestamp:t.at,source:t.source,category:t.category,severity:t.severity??"info",context:{sessionId:e,...r,...t.context??{}},fingerprint:t.fingerprint??n({kind:"publish",identity:t.title}),title:t.title,message:t.message,raw:t.raw}}}o(envelopeToEvent,"envelopeToEvent"),a(envelopeToEvent,"envelopeToEvent"),u(envelopeToEvent,"envelopeToEvent");function c(t){if(!t)return"unknown";const e=t.split(`
2
- `).slice(0,2);for(const r of e){const i=r.match(/at\s+(\S+)/);if(i&&i[1])return i[1]}return"unknown"}o(c,"c"),a(c,"s"),u(c,"normaliseStackTopFrame");function l(t){try{return`${new URL(t,"http://x").pathname}`}catch{return t}}o(l,"l"),a(l,"m"),u(l,"normaliseUrl");function d(t){if(typeof t=="string")return t;try{return JSON.stringify(t)}catch{return String(t)}}o(d,"d"),a(d,"d"),u(d,"stringify");
1
+ "use strict";var b=Object.defineProperty;var u=(e,t)=>b(e,"name",{value:t,configurable:!0});var I=Object.defineProperty,l=u((e,t)=>I(e,"name",{value:t,configurable:!0}),"u"),w=Object.defineProperty,d=l((e,t)=>w(e,"name",{value:t,configurable:!0}),"o"),k=Object.defineProperty,m=d((e,t)=>k(e,"name",{value:t,configurable:!0}),"o"),$=Object.defineProperty,f=m((e,t)=>$(e,"name",{value:t,configurable:!0}),"o"),h=Object.defineProperty,p=f((e,t)=>h(e,"name",{value:t,configurable:!0}),"o"),j=Object.defineProperty,g=p((e,t)=>j(e,"name",{value:t,configurable:!0}),"o"),x=Object.defineProperty,v=g((e,t)=>x(e,"name",{value:t,configurable:!0}),"o");import{fingerprint as a,ulid as c}from"@lensmcp/core";const T="pending";export function envelopeToEvent(e,t=T){const r=e.tabId?{tabId:e.tabId}:{};switch(e.kind){case"error":return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"runtime",severity:"error",context:{sessionId:t,...r},fingerprint:a({kind:"runtime",identity:`error:${n(e.stack)}`,file:e.source,detail:e.message}),title:e.message,message:e.stack??e.message,location:e.source?{file:e.source,line:e.lineno,column:e.colno}:void 0,raw:e};case"unhandledrejection":return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"runtime",severity:"error",context:{sessionId:t,...r},fingerprint:a({kind:"runtime",identity:`unhandledrejection:${e.reason.slice(0,80)}`}),title:"Unhandled promise rejection",message:e.reason,raw:e};case"console":{const s=e.level==="error"?"error":e.level==="warn"?"warning":"info",y=e.args.map(o).join(" ");return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"runtime",severity:s,context:{sessionId:t,...r},fingerprint:a({kind:"runtime",identity:`console:${e.level}:${y.slice(0,80)}`}),title:`console.${e.level}`,message:y,raw:e}}case"fetch":{const s=e.error!==void 0||e.status!==void 0&&e.status>=400;return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"network",severity:s?"error":"info",context:{sessionId:t,...r},fingerprint:a({kind:"network",identity:`${e.method}:${i(e.url)}:${e.status??"err"}`}),title:`${e.method} ${e.url}${e.status!==void 0?` \u2192 ${e.status}`:""}`,message:e.error,relatedUrls:[e.url],raw:e}}case"route":return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"runtime",severity:"info",context:{sessionId:t,url:e.url,...r},fingerprint:a({kind:"runtime",identity:`route:${e.url}`}),title:`Route \u2192 ${e.url}`,raw:e};case"ready":return{id:c(),sessionId:t,timestamp:e.at,source:"client-runtime",category:"runtime",severity:"info",context:{sessionId:t,url:e.url,...r},fingerprint:a({kind:"runtime",identity:"client-ready"}),title:"Client runtime ready",message:e.userAgent,raw:e};case"publish":return{id:c(),sessionId:t,timestamp:e.at,source:e.source,category:e.category,severity:e.severity??"info",context:{sessionId:t,...r,...e.context??{}},fingerprint:e.fingerprint??a({kind:"publish",identity:e.title}),title:e.title,message:e.message,raw:e.raw}}}u(envelopeToEvent,"envelopeToEvent"),l(envelopeToEvent,"envelopeToEvent"),d(envelopeToEvent,"envelopeToEvent"),m(envelopeToEvent,"envelopeToEvent"),f(envelopeToEvent,"envelopeToEvent"),p(envelopeToEvent,"envelopeToEvent"),g(envelopeToEvent,"envelopeToEvent"),v(envelopeToEvent,"envelopeToEvent");function n(e){if(!e)return"unknown";const t=e.split(`
2
+ `).slice(0,2);for(const r of t){const s=r.match(/at\s+(\S+)/);if(s&&s[1])return s[1]}return"unknown"}u(n,"o"),l(n,"a"),d(n,"l"),m(n,"l"),f(n,"c"),p(n,"c"),g(n,"s"),v(n,"normaliseStackTopFrame");function i(e){try{return`${new URL(e,"http://x").pathname}`}catch{return e}}u(i,"a"),l(i,"c"),d(i,"d"),m(i,"d"),f(i,"l"),p(i,"l"),g(i,"m"),v(i,"normaliseUrl");function o(e){if(typeof e=="string")return e;try{return JSON.stringify(e)}catch{return String(e)}}u(o,"c"),l(o,"u"),d(o,"m"),m(o,"m"),f(o,"d"),p(o,"d"),g(o,"d"),v(o,"stringify");
package/lib/plugin.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var x=Object.defineProperty;var l=(e,t)=>x(e,"name",{value:t,configurable:!0});var P=Object.defineProperty,u=l((e,t)=>P(e,"name",{value:t,configurable:!0}),"l");import{fingerprint as v,ulid as b}from"@lensmcp/core";import{createBridgeServer as D,makePublisher as S,handleClientEnvelope as N}from"@lensmcp/bridge";const _="/@lensmcp/client-runtime",E="\0lensmcp-client-runtime";export function lensmcpVitePlugin(e={},t={}){const r=e.enabled??!0,p=e.wsHost??"127.0.0.1",o=e.injectClient??!0;let g,f;const y=S(t),L=e.aliasValtio??!0,T=e.transformSource??!0;return{name:"@lensmcp/vite-plugin",apply:r?void 0:()=>!1,enforce:"pre",wsPort:u(()=>g,"wsPort"),config(){if(!(!r||!L))return{resolve:{alias:[{find:/^valtio$/,replacement:"@lensmcp/valtio-instrumentation"}]}}},async configureServer(n){r&&(f=await D({host:p,port:e.wsPort??0,bus:t.bus,sessionId:t.sessionId}),g=f.port,n.ws.on("vite:error",s=>{y(C(s,t.sessionId))}))},resolveId(n){return n===_?E:null},load(n){if(n!==E)return null;const s=g??e.wsPort??0;return R({host:p,port:s})},async transform(n,s){if(!r||!T)return null;const i=s.split("?")[0]??s;if(i.includes("/node_modules/")||!/\.[jt]sx$/.test(i))return null;const c=await k(n,i);return c?{code:c.code,map:c.map}:null},transformIndexHtml(){if(!(!o||!r))return[{tag:"script",attrs:{type:"module",src:_},injectTo:"head-prepend"}]},handleHotUpdate(n){if(!r)return;const s=n.modules.map(i=>i.file??i.id??null).filter(i=>!!i);y({id:b(),sessionId:t.sessionId??"pending",timestamp:Date.now(),source:"vite",category:"build",severity:"info",context:{sessionId:t.sessionId??"pending"},fingerprint:v({kind:"hmr",identity:n.file}),title:`HMR ${n.file}`,location:{file:n.file},relatedFiles:s,raw:{kind:"hmr",file:n.file}})},generateBundle(n,s){if(!r)return;const i=[];let c=0;for(const A of Object.values(s)){const d=A,B=d.fileName;let m;if(d.type==="chunk")m=(d.code??"").length;else{const a=d.source;m=typeof a=="string"?a.length:a instanceof Uint8Array?a.byteLength:0}c+=m;const w=[];if(d.type==="chunk"&&d.modules){for(const[a,h]of Object.entries(d.modules)){const F=h.renderedLength??h.originalLength??0;w.push({id:a,sizeBytes:F})}w.sort((a,h)=>h.sizeBytes-a.sizeBytes)}i.push({name:B,sizeBytes:m,modules:w})}const O={timestamp:Date.now(),totalBytes:c,chunks:i};y({id:b(),sessionId:t.sessionId??"pending",timestamp:Date.now(),source:"rollup",category:"build",severity:"info",context:{sessionId:t.sessionId??"pending"},fingerprint:v({kind:"bundle",identity:"report"}),title:`Bundle report: ${i.length} chunks, ${c} bytes`,raw:{kind:"bundle-report",report:O}})},async closeBundle(){f&&(await f.close(),f=void 0)}}}l(lensmcpVitePlugin,"lensmcpVitePlugin"),u(lensmcpVitePlugin,"lensmcpVitePlugin");async function k(e,t){try{const[{transformSync:r},{lensmcpBabelPlugin:p}]=await Promise.all([import("@babel/core"),import("@lensmcp/react-instrumentation")]),o=r(e,{filename:t,babelrc:!1,configFile:!1,sourceMaps:!0,parserOpts:{plugins:["jsx","typescript"]},plugins:[[p,{rootDir:process.cwd(),production:!1}]],code:!0});return o?.code?{code:o.code,map:o.map}:null}catch{return null}}l(k,"B"),u(k,"runLensmcpBabel");export function handleClientEnvelope(e,t){N(e,S(t),t.sessionId)}l(handleClientEnvelope,"handleClientEnvelope"),u(handleClientEnvelope,"handleClientEnvelope");function I(e){const t=e?.loc?.file??e?.id;return typeof t!="string"||t.length===0?void 0:{file:t.split("?")[0]||t,...typeof e?.loc?.line=="number"?{line:e.loc.line}:{},...typeof e?.loc?.column=="number"?{column:e.loc.column}:{}}}l(I,"F"),u(I,"errorLocation");function C(e,t){const r=e.err?.message??"Vite error",p=e.err?.stack,o=I(e.err);return{id:b(),sessionId:t??"pending",timestamp:Date.now(),source:"vite",category:"build",severity:"error",context:{sessionId:t??"pending"},fingerprint:v({kind:"vite",identity:e.type??r,...o?{file:o.file}:{}}),title:r,message:p,...o?{location:o}:{},raw:{kind:"build-diagnostic",...o?{file:o.file}:{},...e.err?.plugin?{plugin:e.err.plugin}:{}}}}l(C,"D"),u(C,"viteErrorToEvent");function R(e){return`// @lensmcp/client-runtime (inlined by @lensmcp/vite-plugin)
1
+ "use strict";var N=Object.defineProperty;var l=(e,t)=>N(e,"name",{value:t,configurable:!0});var x=Object.defineProperty,d=l((e,t)=>x(e,"name",{value:t,configurable:!0}),"l");import{fingerprint as v,ulid as b}from"@lensmcp/core";import{createBridgeServer as F,makePublisher as S,handleClientEnvelope as P}from"@lensmcp/bridge";const _="/@lensmcp/client-runtime",k="\0lensmcp-client-runtime";export function lensmcpVitePlugin(e={},t={}){const r=e.enabled??!0,p=e.wsHost??"127.0.0.1",o=e.injectClient??!0;let g,f;const y=S(t),L=e.aliasValtio??!0,R=e.transformSource??!0;return{name:"@lensmcp/vite-plugin",apply:r?void 0:()=>!1,enforce:"pre",wsPort:d(()=>g,"wsPort"),config(){if(!(!r||!L))return{resolve:{alias:[{find:/^valtio$/,replacement:"@lensmcp/valtio-instrumentation"}]}}},async configureServer(n){r&&(f=await F({host:p,port:e.wsPort??0,bus:t.bus,sessionId:t.sessionId}),g=f.port,n.ws.on("vite:error",i=>{y(T(i,t.sessionId))}))},resolveId(n){return n===_?k:null},load(n){if(n!==k)return null;const i=g??e.wsPort??0;return C({host:p,port:i})},async transform(n,i){if(!r||!R)return null;const s=i.split("?")[0]??i;if(s.includes("/node_modules/")||!/\.[jt]sx$/.test(s))return null;const c=await E(n,s);return c?{code:c.code,map:c.map}:null},transformIndexHtml(){if(!(!o||!r))return[{tag:"script",attrs:{type:"module",src:_},injectTo:"head-prepend"}]},handleHotUpdate(n){if(!r)return;const i=n.modules.map(s=>s.file??s.id??null).filter(s=>!!s);y({id:b(),sessionId:t.sessionId??"pending",timestamp:Date.now(),source:"vite",category:"build",severity:"info",context:{sessionId:t.sessionId??"pending"},fingerprint:v({kind:"hmr",identity:n.file}),title:`HMR ${n.file}`,location:{file:n.file},relatedFiles:i,raw:{kind:"hmr",file:n.file}})},generateBundle(n,i){if(!r)return;const s=[];let c=0;for(const A of Object.values(i)){const u=A,B=u.fileName;let h;if(u.type==="chunk")h=(u.code??"").length;else{const a=u.source;h=typeof a=="string"?a.length:a instanceof Uint8Array?a.byteLength:0}c+=h;const w=[];if(u.type==="chunk"&&u.modules){for(const[a,m]of Object.entries(u.modules)){const D=m.renderedLength??m.originalLength??0;w.push({id:a,sizeBytes:D})}w.sort((a,m)=>m.sizeBytes-a.sizeBytes)}s.push({name:B,sizeBytes:h,modules:w})}const O={timestamp:Date.now(),totalBytes:c,chunks:s};y({id:b(),sessionId:t.sessionId??"pending",timestamp:Date.now(),source:"rollup",category:"build",severity:"info",context:{sessionId:t.sessionId??"pending"},fingerprint:v({kind:"bundle",identity:"report"}),title:`Bundle report: ${s.length} chunks, ${c} bytes`,raw:{kind:"bundle-report",report:O}})},async closeBundle(){f&&(await f.close(),f=void 0)}}}l(lensmcpVitePlugin,"lensmcpVitePlugin"),d(lensmcpVitePlugin,"lensmcpVitePlugin");async function E(e,t){try{const[{transformSync:r},{lensmcpBabelPlugin:p}]=await Promise.all([import("@babel/core"),import("@lensmcp/react-instrumentation")]),o=r(e,{filename:t,babelrc:!1,configFile:!1,sourceMaps:!0,parserOpts:{plugins:["jsx","typescript"]},plugins:[[p,{rootDir:process.cwd(),production:!1}]],code:!0});return o?.code?{code:o.code,map:o.map}:null}catch{return null}}l(E,"B"),d(E,"runLensmcpBabel");export function handleClientEnvelope(e,t){P(e,S(t),t.sessionId)}l(handleClientEnvelope,"handleClientEnvelope"),d(handleClientEnvelope,"handleClientEnvelope");function I(e){const t=e?.loc?.file??e?.id;return typeof t!="string"||t.length===0?void 0:{file:t.split("?")[0]||t,...typeof e?.loc?.line=="number"?{line:e.loc.line}:{},...typeof e?.loc?.column=="number"?{column:e.loc.column}:{}}}l(I,"D"),d(I,"errorLocation");function T(e,t){const r=e.err?.message??"Vite error",p=e.err?.stack,o=I(e.err);return{id:b(),sessionId:t??"pending",timestamp:Date.now(),source:"vite",category:"build",severity:"error",context:{sessionId:t??"pending"},fingerprint:v({kind:"vite",identity:e.type??r,...o?{file:o.file}:{}}),title:r,message:p,...o?{location:o}:{},raw:{kind:"build-diagnostic",...o?{file:o.file}:{},...e.err?.plugin?{plugin:e.err.plugin}:{}}}}l(T,"N"),d(T,"viteErrorToEvent");function C(e){return`// @lensmcp/client-runtime (inlined by @lensmcp/vite-plugin)
2
2
  (() => {
3
3
  if (typeof window === 'undefined') return;
4
4
  if (window.__LENSMCP_CLIENT__) return;
@@ -58,17 +58,42 @@
58
58
  } catch (e) { return 'tab-' + Math.random().toString(36).slice(2, 10); }
59
59
  })();
60
60
 
61
+ // BATCHED transport: capture is synchronous (per-event at/tabId stamped NOW \u2014 the recorded
62
+ // timeline is exact), but the stringify + socket frame happen ONCE per macrotask, off the
63
+ // interaction task. A commit storm (a tab switch mounting hundreds of components) used to pay
64
+ // one JSON.stringify + one ws.send INLINE per event \u2014 measured as the dominant slice of the
65
+ // post-switch long tasks. Same events, same order, same timestamps; only the transport moves.
66
+ let batch = [];
67
+ let flushScheduled = false;
68
+ function flushBatch() {
69
+ flushScheduled = false;
70
+ if (!batch.length) return;
71
+ const events = batch;
72
+ batch = [];
73
+ if (ws && ws.readyState === 1) {
74
+ try {
75
+ ws.send(JSON.stringify(events.length === 1 ? events[0] : { kind: 'batch', at: Date.now(), tabId: TAB_ID, events }));
76
+ return;
77
+ } catch (e) { /* fall through to queue */ }
78
+ }
79
+ for (const ev of events) queue.push(ev);
80
+ }
61
81
  function send(env) {
62
82
  env.at = Date.now();
63
83
  env.tabId = TAB_ID;
64
- if (ws && ws.readyState === 1) ws.send(JSON.stringify(env));
65
- else queue.push(env);
84
+ batch.push(env);
85
+ if (!flushScheduled) { flushScheduled = true; setTimeout(flushBatch, 0); }
66
86
  }
87
+ // The tab may die with a flush pending \u2014 drain synchronously so nothing is lost.
88
+ window.addEventListener('pagehide', flushBatch);
67
89
  function open() {
68
90
  try { ws = new WebSocket(WS_URL); } catch (e) { setTimeout(open, 1000); return; }
69
91
  ws.addEventListener('open', () => {
70
92
  openOnce = true;
71
- while (queue.length) { try { ws.send(JSON.stringify(queue.shift())); } catch {} }
93
+ while (queue.length) {
94
+ const chunk = queue.splice(0, 200);
95
+ try { ws.send(JSON.stringify(chunk.length === 1 ? chunk[0] : { kind: 'batch', at: Date.now(), tabId: TAB_ID, events: chunk })); } catch (e) {}
96
+ }
72
97
  send({ kind: 'ready', userAgent: navigator.userAgent, url: location.href });
73
98
  });
74
99
  ws.addEventListener('close', () => { if (openOnce) setTimeout(open, 1000); });
@@ -196,4 +221,4 @@
196
221
  try { return JSON.parse(JSON.stringify(v)); } catch { return String(v); }
197
222
  }
198
223
  })();
199
- `}l(R,"N"),u(R,"buildInlineClientRuntime");
224
+ `}l(C,"F"),d(C,"buildInlineClientRuntime");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lensmcp/vite-plugin",
3
- "version": "1.18.6",
3
+ "version": "1.18.8",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "module": "./index.js",
@@ -15,11 +15,11 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@babel/core": "^7.0.0",
18
- "@lensmcp/bridge": "1.18.6",
19
- "@lensmcp/core": "1.18.6",
20
- "@lensmcp/protocol-types": "1.18.6",
21
- "@lensmcp/react-instrumentation": "1.18.6",
22
- "@lensmcp/valtio-instrumentation": "1.18.6",
18
+ "@lensmcp/bridge": "1.18.8",
19
+ "@lensmcp/core": "1.18.8",
20
+ "@lensmcp/protocol-types": "1.18.8",
21
+ "@lensmcp/react-instrumentation": "1.18.8",
22
+ "@lensmcp/valtio-instrumentation": "1.18.8",
23
23
  "tslib": "^2.3.0"
24
24
  },
25
25
  "peerDependencies": {