@lensmcp/node-instrumentation 1.25.17 → 1.25.21
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.
- package/index.js +1 -1
- package/lib/active-work.js +1 -1
- package/lib/ai-providers.js +9 -9
- package/lib/ai-store.d.ts +3 -1
- package/lib/ai-store.js +1 -1
- package/lib/callsite.js +2 -2
- package/lib/capture-file.d.ts +5 -0
- package/lib/capture-file.js +1 -1
- package/lib/emit-custom.js +1 -1
- package/lib/emit.js +3 -3
- package/lib/flood-guard.js +1 -1
- package/lib/flow-context.js +1 -1
- package/lib/http-capture.d.ts +68 -0
- package/lib/http-capture.js +2 -2
- package/lib/http-store.js +1 -1
- package/lib/install.js +1 -1
- package/lib/require-hook.js +1 -1
- package/lib/sql-accounting.js +1 -1
- package/lib/sql-values.d.ts +21 -0
- package/lib/sql-values.js +3 -0
- package/lib/tap-ai.js +1 -1
- package/lib/tap-bullmq.js +1 -1
- package/lib/tap-egress.js +1 -1
- package/lib/tap-event-loop.js +1 -1
- package/lib/tap-exec.d.ts +2 -1
- package/lib/tap-exec.js +1 -1
- package/lib/tap-fs.d.ts +30 -10
- package/lib/tap-fs.js +1 -1
- package/lib/tap-ingress.js +1 -1
- package/lib/tap-nest-logger.js +5 -5
- package/lib/tap-nest.js +1 -1
- package/lib/tap-pg-pool.js +2 -2
- package/lib/tap-pg.js +1 -1
- package/lib/tap-redis.d.ts +2 -0
- package/lib/tap-redis.js +1 -1
- package/lib/tap-stdout.js +5 -5
- package/package.json +4 -4
- package/register.js +1 -1
package/lib/sql-accounting.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var S=Object.defineProperty;var r=(e,t)=>S(e,"name",{value:t,configurable:!0});var u=Object.defineProperty,n=r((e,t)=>u(e,"name",{value:t,configurable:!0}),"n"),l=Object.defineProperty,s=n((e,t)=>l(e,"name",{value:t,configurable:!0}),"s");Object.defineProperty(exports,"__esModule",{value:!0}),exports.SQL_ACCOUNT_SETTLE_MS=exports.SQL_ACCOUNT_KIND=void 0,exports.beginSqlStatement=beginSqlStatement,exports.endSqlStatement=endSqlStatement,exports.abandonSqlStatement=abandonSqlStatement;const flood_guard_1=require("./flood-guard"),emit_1=require("./emit"),flow_context_1=require("./flow-context");exports.SQL_ACCOUNT_KIND="db-request-account",exports.SQL_ACCOUNT_SETTLE_MS=1e3;const accountGuard=(0,flood_guard_1.createEventFloodGuard)({burst:120,perSec:20}),accounts=new WeakMap;let accountSeq=0;function newAccountId(){return accountSeq=(accountSeq+1)%16777215,`${Date.now().toString(36)}${accountSeq.toString(36)}${Math.random().toString(36).slice(2,6)}`}r(newAccountId,"newAccountId"),n(newAccountId,"newAccountId"),s(newAccountId,"newAccountId");function beginSqlStatement(e,t,o){if(!e||!t?.flowId||!t.requestId)return;let d=accounts.get(e);if(!d){const i=e.runId;d={accountId:newAccountId(),...typeof i=="string"&&i?{runId:i}:{},ctx:t,seq:0,inFlight:0,statements:0,failed:0,workMs:0,recorded:0,recordedWorkMs:0,suppressed:0,unwritten:0,firstStartedAt:o,lastEndedAt:o,awaitsEnd:!1,ended:!1},accounts.set(e,d);const a=(0,flow_context_1.contextEndOf)(e);if(a&&!a.ended){const c=d;c.awaitsEnd=!0,a.onEnd(()=>endSqlAccount(c))}}return d.inFlight+=1,d.timer&&(clearTimeout(d.timer),d.timer=void 0),d}r(beginSqlStatement,"beginSqlStatement"),n(beginSqlStatement,"beginSqlStatement"),s(beginSqlStatement,"beginSqlStatement");function endSqlStatement(e,t){e&&(e.inFlight=Math.max(0,e.inFlight-1),e.statements+=1,e.workMs+=t.durationMs,t.failed&&(e.failed+=1),t.emitted==="written"?(e.recorded+=1,e.recordedWorkMs+=t.durationMs):t.emitted==="suppressed"?e.suppressed+=1:e.unwritten+=1,e.lastEndedAt=Math.max(e.lastEndedAt,Date.now()),writeWhenDue(e))}r(endSqlStatement,"endSqlStatement"),n(endSqlStatement,"endSqlStatement"),s(endSqlStatement,"endSqlStatement");function abandonSqlStatement(e){e&&(e.inFlight=Math.max(0,e.inFlight-1),writeWhenDue(e))}r(abandonSqlStatement,"abandonSqlStatement"),n(abandonSqlStatement,"abandonSqlStatement"),s(abandonSqlStatement,"abandonSqlStatement");function endSqlAccount(e){e.ended=!0,writeWhenDue(e)}r(endSqlAccount,"endSqlAccount"),n(endSqlAccount,"endSqlAccount"),s(endSqlAccount,"endSqlAccount");function writeWhenDue(e){if(!(e.inFlight>0||e.statements===0)&&!(e.awaitsEnd&&!e.ended)){if(e.ended&&e.seq===0){writeAccount(e);return}settleLater(e)}}r(writeWhenDue,"writeWhenDue"),n(writeWhenDue,"writeWhenDue"),s(writeWhenDue,"writeWhenDue");function settleLater(e){e.timer&&clearTimeout(e.timer);const t=setTimeout(()=>writeAccount(e),exports.SQL_ACCOUNT_SETTLE_MS);t.unref?.(),e.timer=t}r(settleLater,"settleLater"),n(settleLater,"settleLater"),s(settleLater,"settleLater");function writeAccount(e){e.timer&&clearTimeout(e.timer),e.timer=void 0,e.seq+=1;const t=e.suppressed+e.unwritten,o=e.ended?"end":"settled";(0,emit_1.lensEmitOutcome)("lens","info",`SQL account: ${e.statements} statement${e.statements===1?"":"s"}`+(t>0?`, ${t} not on the bus`:", all on the bus"),{kind:exports.SQL_ACCOUNT_KIND,accountId:e.accountId,...e.runId?{runId:e.runId}:{},seq:e.seq,basis:o,statements:e.statements,failed:e.failed,workMs:e.workMs,recorded:e.recorded,recordedWorkMs:e.recordedWorkMs,suppressed:e.suppressed,unwritten:e.unwritten,firstStartedAt:e.firstStartedAt,lastEndedAt:e.lastEndedAt},`lens:sql-account:${emit_1.PROJECT}`,e.ctx,"db",accountGuard)}r(writeAccount,"writeAccount"),n(writeAccount,"writeAccount"),s(writeAccount,"writeAccount");
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare const SQL_VALUES_RUN_MAX_CHARS: number;
|
|
2
|
+
/** An id for a run about to be emitted — `undefined` when values are not kept (Settings › Features, or the env). */
|
|
3
|
+
export declare function newSqlValuesId(at: number): string | undefined;
|
|
4
|
+
/** An id for a cache command about to be emitted — under the same switch as a SQL run's. */
|
|
5
|
+
export declare function newCacheValuesId(at: number): string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Keeps a run's values for the event that named `id` — call only once that event reached the bus. Runs on the query's
|
|
8
|
+
* completion, so it is bounded by the budget, not by the result: each value and each row is turned into text once,
|
|
9
|
+
* and the walk stops at the first that does not fit — a 10,000-row SELECT costs the rows that fit, plus one.
|
|
10
|
+
*/
|
|
11
|
+
export declare function keepSqlValues(id: string, at: number, sql: string, values: unknown[] | undefined, rows: unknown[] | undefined): void;
|
|
12
|
+
/**
|
|
13
|
+
* Keeps a cache command's values for the event that named `id` — call only once that event reached the bus, and never
|
|
14
|
+
* for a key that keeps a credential (the tap names none there). Its arguments after the key, each at most its share of
|
|
15
|
+
* half the budget, then the reply in what is left.
|
|
16
|
+
*/
|
|
17
|
+
export declare function keepCacheValues(id: string, at: number, op: string, key: string, args: readonly unknown[] | undefined, reply?: unknown): void;
|
|
18
|
+
/** A day at most and 200 MB in all, oldest first. Returns how many files went. */
|
|
19
|
+
export declare function pruneSqlValues(at: string, now?: number): number;
|
|
20
|
+
/** Tests: write what is buffered now — under `at` as the lens dir when given. */
|
|
21
|
+
export declare function flushSqlValuesForTests(at?: string): void;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";var m=Object.defineProperty;var u=(e,o)=>m(e,"name",{value:o,configurable:!0});var g=Object.defineProperty,a=u((e,o)=>g(e,"name",{value:o,configurable:!0}),"a"),y=Object.defineProperty,c=a((e,o)=>y(e,"name",{value:o,configurable:!0}),"c");Object.defineProperty(exports,"__esModule",{value:!0}),exports.SQL_VALUES_RUN_MAX_CHARS=void 0,exports.newSqlValuesId=newSqlValuesId,exports.newCacheValuesId=newCacheValuesId,exports.keepSqlValues=keepSqlValues,exports.keepCacheValues=keepCacheValues,exports.pruneSqlValues=pruneSqlValues,exports.flushSqlValuesForTests=flushSqlValuesForTests;const tslib_1=require("tslib"),nodeFs=tslib_1.__importStar(require("node:fs")),node_path_1=require("node:path"),sql_values_1=require("@lensmcp/core/sql-values"),redact_1=require("@lensmcp/core/redact"),ai_store_1=require("./ai-store"),capture_file_1=require("./capture-file"),emit_1=require("./emit"),appendFile=nodeFs.appendFile,appendFileSync=nodeFs.appendFileSync,mkdirSync=nodeFs.mkdirSync,readdirSync=nodeFs.readdirSync,statSync=nodeFs.statSync,unlinkSync=nodeFs.unlinkSync;exports.SQL_VALUES_RUN_MAX_CHARS=64*1024;const MAX_AGE_MS=1440*6e4,MAX_TOTAL_BYTES=200*1024*1024,FLUSH_MS=1e3,PRUNE_EVERY_MS=10*6e4,PRUNE_EVERY_BYTES=20*1024*1024;let seq=0,pending=[],timer,lastPrune=0,appended=0;const made=new Set;let lensDir=c(()=>(0,node_path_1.dirname)(emit_1.EVENT_FILE),"lensDir");function newSqlValuesId(e){if((0,capture_file_1.sqlValuesAsked)(e))return seq=(seq+1)%2147483647,(0,sql_values_1.sqlValuesId)(e,process.pid,seq)}u(newSqlValuesId,"newSqlValuesId"),a(newSqlValuesId,"newSqlValuesId"),c(newSqlValuesId,"newSqlValuesId");function newCacheValuesId(e){if((0,capture_file_1.sqlValuesAsked)(e))return seq=(seq+1)%2147483647,(0,sql_values_1.cacheValuesId)(e,process.pid,seq)}u(newCacheValuesId,"newCacheValuesId"),a(newCacheValuesId,"newCacheValuesId"),c(newCacheValuesId,"newCacheValuesId");const MIN_VALUE_CHARS=256;function keptParam(e,o){let s;if(e==null)s=String(e);else if(typeof e=="string")s=e;else if(e instanceof Date)s=e.toISOString();else if(e instanceof Uint8Array)s=`<${e.byteLength} bytes>`;else if(typeof e=="bigint")s=e.toString();else try{s=JSON.stringify(typeof e=="object"?(0,redact_1.redactDeep)(e):e)??String(e)}catch{s=String(e)}const n=s.length>o,r=(0,ai_store_1.redactAiText)(n?s.slice(0,o+4096):s);return r.length>o?{text:`${r.slice(0,o-1)}\u2026`,cut:!0}:{text:r,cut:n}}u(keptParam,"keptParam"),a(keptParam,"keptParam"),c(keptParam,"keptParam");function cells(e,o=0){if(typeof e=="string")return(0,ai_store_1.redactAiText)(e);if(typeof e=="bigint")return e.toString();if(e instanceof Date)return Number.isNaN(e.getTime())?null:e.toISOString();if(e instanceof Uint8Array)return`<${e.byteLength} bytes>`;if(o>8||e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(n=>cells(n,o+1));const s={};for(const[n,r]of Object.entries(e))s[n]=cells(r,o+1);return s}u(cells,"cells"),a(cells,"cells"),c(cells,"cells");function json(e){try{return JSON.stringify(e,function(o,s){const n=this[o];return n instanceof Uint8Array?`<${n.byteLength} bytes>`:typeof s=="bigint"?s.toString():s})??""}catch{return""}}u(json,"json"),a(json,"json"),c(json,"json");function keepSqlValues(e,o,s,n,r){try{const t={id:e,at:o};let i=exports.SQL_VALUES_RUN_MAX_CHARS-256;if(n?.length){const l=(0,redact_1.redactSqlParams)(s,n),f=Math.max(MIN_VALUE_CHARS,Math.floor(i/l.length)-8),p=[];for(const h of l){const d=keptParam(h,f),S=json(d.text).length+1;if(S>i){t.cut=!0;break}d.cut&&(t.cut=!0),p.push(d.text),i-=S}t.params=p,p.length<l.length&&(t.paramCount=l.length)}if(r?.length){const l=[];for(const f of r){if(json(f).length+1>i){t.cut=!0;break}const p=(0,redact_1.redactDeep)(cells(f)),h=json(p).length+1;if(h>i){t.cut=!0;break}l.push(p),i-=h}l.length&&(t.rows=l),t.rowCount=r.length}pending.push(t),timer??=setTimeout(flush,FLUSH_MS),timer.unref?.()}catch{}}u(keepSqlValues,"keepSqlValues"),a(keepSqlValues,"keepSqlValues"),c(keepSqlValues,"keepSqlValues");function keptReply(e,o){if(e==null)return;if(e instanceof Uint8Array)return{reply:`<${e.byteLength} bytes>`,size:24,cut:!1};if(typeof e=="number"||typeof e=="boolean")return{reply:e,size:16,cut:!1};if(typeof e=="string"){const n=e.trimStart();if((n.startsWith("{")||n.startsWith("["))&&e.length<=o)try{const t=(0,redact_1.redactDeep)(cells(JSON.parse(e))),i=json(t).length;if(i<=o)return{reply:t,size:i,cut:!1}}catch{}const r=keptParam(e,Math.max(MIN_VALUE_CHARS,o-8));return{reply:r.text,size:json(r.text).length,cut:r.cut}}if(Array.isArray(e)){const n=[];let r=2;for(const t of e){if(json(t).length+1>o-r)return{reply:n,size:r,cut:!0};const i=(0,redact_1.redactDeep)(cells(t));r+=json(i).length+1,n.push(i)}return{reply:n,size:r,cut:!1}}if(json(e).length>o){const n=keptParam(e,Math.max(MIN_VALUE_CHARS,o-8));return{reply:n.text,size:json(n.text).length,cut:!0}}const s=(0,redact_1.redactDeep)(cells(e));return{reply:s,size:json(s).length,cut:!1}}u(keptReply,"keptReply"),a(keptReply,"keptReply"),c(keptReply,"keptReply");function keepCacheValues(e,o,s,n,r,t){try{const i={id:e,at:o,op:s,key:(0,ai_store_1.redactAiText)(n.slice(0,1024))};let l=exports.SQL_VALUES_RUN_MAX_CHARS-256-i.key.length;if(r?.length){const p=Math.max(MIN_VALUE_CHARS,Math.floor(l/2/r.length)-8),h=[];for(const d of r){const S=keptParam(d,p),_=json(S.text).length+1;if(_>l){i.cut=!0;break}S.cut&&(i.cut=!0),h.push(S.text),l-=_}i.args=h,h.length<r.length&&(i.argCount=r.length)}const f=keptReply(t,l);f&&(i.reply=f.reply,f.cut&&(i.cut=!0)),pending.push(i),timer??=setTimeout(flush,FLUSH_MS),timer.unref?.()}catch{}}u(keepCacheValues,"keepCacheValues"),a(keepCacheValues,"keepCacheValues"),c(keepCacheValues,"keepCacheValues");function flush(e=!1){if(timer=void 0,!pending.length)return;const o=pending;pending=[];const s=new Map;for(const n of o){const r=(0,sql_values_1.keptValuesFile)(lensDir(),n.id);if(!r)continue;const t=s.get(r)??[];t.push(JSON.stringify(n)),s.set(r,t)}try{for(const[n,r]of s){const t=(0,node_path_1.dirname)(n);made.has(t)||(mkdirSync(t,{recursive:!0,mode:448}),made.add(t));const i=`${r.join(`
|
|
2
|
+
`)}
|
|
3
|
+
`;appended+=i.length,e?appendFileSync(n,i,{mode:384}):appendFile(n,i,{mode:384},()=>{})}}catch{}(Date.now()-lastPrune>=PRUNE_EVERY_MS||appended>=PRUNE_EVERY_BYTES)&&(lastPrune=Date.now(),appended=0,pruneSqlValues((0,node_path_1.join)(lensDir(),sql_values_1.SQL_VALUES_DIR)),pruneSqlValues((0,node_path_1.join)(lensDir(),sql_values_1.CACHE_VALUES_DIR)))}u(flush,"flush"),a(flush,"flush"),c(flush,"flush");function pruneSqlValues(e,o=Date.now()){let s;try{s=readdirSync(e).filter(t=>t.endsWith(".jsonl")).map(t=>{const i=statSync((0,node_path_1.join)(e,t));return{file:(0,node_path_1.join)(e,t),mtime:i.mtimeMs,size:i.size}}).sort((t,i)=>t.mtime-i.mtime)}catch{return 0}let n=s.reduce((t,i)=>t+i.size,0),r=0;for(const t of s){if(o-t.mtime<=MAX_AGE_MS&&n<=MAX_TOTAL_BYTES)break;try{unlinkSync(t.file),n-=t.size,r+=1}catch{}}return r}u(pruneSqlValues,"pruneSqlValues"),a(pruneSqlValues,"pruneSqlValues"),c(pruneSqlValues,"pruneSqlValues"),process.once("exit",()=>flush(!0));function flushSqlValuesForTests(e){e&&(lensDir=c(()=>e,"lensDir"),made.clear()),timer&&clearTimeout(timer),flush(!0)}u(flushSqlValuesForTests,"flushSqlValuesForTests"),a(flushSqlValuesForTests,"flushSqlValuesForTests"),c(flushSqlValuesForTests,"flushSqlValuesForTests");
|
package/lib/tap-ai.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var E=Object.defineProperty;var u=(e,t)=>E(e,"name",{value:t,configurable:!0});var R=Object.defineProperty,c=u((e,t)=>R(e,"name",{value:t,configurable:!0}),"c"),x=Object.defineProperty,d=c((e,t)=>x(e,"name",{value:t,configurable:!0}),"d");Object.defineProperty(exports,"__esModule",{value:!0}),exports.aiCaptureMode=aiCaptureMode,exports.flushAiTapForTests=flushAiTapForTests,exports.resetAiTapForTests=resetAiTapForTests,exports.tapAi=tapAi;const emit_1=require("./emit"),flow_context_1=require("./flow-context"),ai_providers_1=require("./ai-providers"),ai_store_1=require("./ai-store");function aiCaptureMode(e=process.env){const t=(e.LENSMCP_AI_CAPTURE??"").trim().toLowerCase();return t==="content"?"content":t==="metadata"||t==="1"||t==="true"||t==="on"?"metadata":"off"}u(aiCaptureMode,"aiCaptureMode"),c(aiCaptureMode,"aiCaptureMode"),d(aiCaptureMode,"aiCaptureMode");const MAX_REQUEST_PARSE_BYTES=2*1024*1024,MAX_RESPONSE_PARSE_BYTES=8*1024*1024,MAX_STREAM_BYTES=8*1024*1024,MAX_STREAM_MS=300*1e3,MAX_ENDPOINT_CHARS=256;function locate(e){try{const t=new URL(typeof e=="string"?e:e instanceof URL?e.href:e.url);return{host:t.hostname,port:t.port,pathname:t.pathname}}catch{return}}u(locate,"locate"),c(locate,"locate"),d(locate,"locate");const methodOf=d((e,t)=>{const r=typeof e=="object"&&e!==null&&!(e instanceof URL)?e.method:void 0;return String(t?.method??r??"GET").toUpperCase()},"methodOf"),decode=d(e=>({bytes:e.byteLength,...e.byteLength<=MAX_REQUEST_PARSE_BYTES?{text:Buffer.from(e.buffer,e.byteOffset,e.byteLength).toString("utf8")}:{}}),"decode");function takeRequestBody(e,t){const r=t?.body;if(typeof r=="string")return{text:r,bytes:Buffer.byteLength(r)};if(r instanceof ArrayBuffer)return decode(new Uint8Array(r));if(ArrayBuffer.isView(r))return decode(new Uint8Array(r.buffer,r.byteOffset,r.byteLength));if(typeof Blob<"u"&&r instanceof Blob)return{bytes:r.size,...r.size<=MAX_REQUEST_PARSE_BYTES?{pending:r.text().catch(()=>{})}:{}};if(r==null&&typeof e=="object"&&e!==null&&!(e instanceof URL)){const n=e;if(!n.bodyUsed&&n.body)try{return{pending:n.clone().text().catch(()=>{})}}catch{return{}}}return{}}u(takeRequestBody,"takeRequestBody"),c(takeRequestBody,"takeRequestBody"),d(takeRequestBody,"takeRequestBody");const sum=d(e=>e.reduce((t,r)=>t+r,0),"sum");function parseJson(e){if(e)try{return JSON.parse(e)}catch{return}}u(parseJson,"parseJson"),c(parseJson,"parseJson"),d(parseJson,"parseJson");function headerOf(e,t){try{return e?.headers?.get?.(t)??""}catch{return""}}u(headerOf,"headerOf"),c(headerOf,"headerOf"),d(headerOf,"headerOf");const statusOf=d(e=>typeof e?.status=="number"?e.status:void 0,"statusOf");function failureKind(e,t){const r=e?.name,n=r==="TimeoutError";return{timedOut:n,aborted:!n&&(r==="AbortError"||t?.aborted===!0)}}u(failureKind,"failureKind"),c(failureKind,"failureKind"),d(failureKind,"failureKind");async function readStream(e,t,r){const n=(0,ai_providers_1.createStreamAccumulator)(t);let i,o=!1;const a=e.body;if(a){const p=a.getReader(),s=new TextDecoder;let m=0;try{for(;;){const{done:g,value:y}=await p.read();if(g)break;if(m+=y.byteLength,n.push(s.decode(y,{stream:!0})),i===void 0&&n.eventCount>0&&(i=Date.now()-r),m>MAX_STREAM_BYTES||Date.now()-r>MAX_STREAM_MS){o=!0,await p.cancel().catch(()=>{});break}}}catch{o=!0}}return{response:n.end(),sse:!0,cut:o,...i!==void 0?{firstEventMs:i}:{}}}u(readStream,"readStream"),c(readStream,"readStream"),d(readStream,"readStream");async function readBody(e){const t={completion:"",toolCalls:[],usage:{}};if(!e.res||!e.clone)return{response:t,sse:!1,cut:!1};try{if(headerOf(e.res,"content-type").includes("text/event-stream"))return await readStream(e.clone,e.target,e.startedAt);const r=Number(headerOf(e.res,"content-length"));if(Number.isFinite(r)&&r>MAX_RESPONSE_PARSE_BYTES)return{response:t,sse:!1,cut:!1};const n=await e.clone.text().catch(()=>{});if(n&&/^\s*(?:event|data):/.test(n)){const i=(0,ai_providers_1.createStreamAccumulator)(e.target);return i.push(n),{response:i.end(),sse:!0,cut:!1}}return{response:(0,ai_providers_1.normalizeResponse)(e.target,parseJson(n)),sse:!1,cut:!1}}catch{return{response:t,sse:!1,cut:!1}}}u(readBody,"readBody"),c(readBody,"readBody"),d(readBody,"readBody");async function capture(e){const t=readBody(e),r=e.request.text??await e.request.pending,n=e.request.bytes??(r?Buffer.byteLength(r):void 0),i=n!==void 0&&n>MAX_REQUEST_PARSE_BYTES,o=(0,ai_providers_1.normalizeRequest)(e.target,i?void 0:parseJson(r)),a=statusOf(e.res),p=await t;let s=p.response;const m=o.stream||p.sse,g=p.firstEventMs,y=p.cut,{aborted:v,timedOut:_}=e.failure!==void 0?failureKind(e.failure,e.signal):{aborted:!1,timedOut:!1};e.failure!==void 0&&(s={...s,error:String(e.failure?.message??e.failure)});const A=v||y,T=Date.now()-e.startedAt,l=s.model??o.model,w=sum(o.messages.map(S=>S.content.length))+(o.system?.length??0);let f,h=!1;if(e.mode==="content"&&e.target.api!=="embeddings"){const S={schemaVersion:1,id:(0,ai_store_1.newAiCallId)(),capturedAt:Date.now(),...e.ctx?.flowId?{flowId:e.ctx.flowId}:{},...e.ctx?.requestId?{requestId:e.ctx.requestId}:{},provider:e.target.provider,api:e.target.api,...l?{model:l}:{},host:e.host,endpoint:e.endpoint,stream:m,...a!==void 0?{status:a}:{},request:{...o.system?{system:o.system}:{},messages:o.messages,...o.tools.length?{tools:o.tools}:{},...Object.keys(o.params).length?{params:o.params}:{}},response:{completion:s.completion,...s.reasoning?{reasoning:s.reasoning}:{},...s.toolCalls.length?{toolCalls:s.toolCalls}:{},...s.finishReason?{finishReason:s.finishReason}:{},usage:s.usage,...s.error?{error:s.error}:{}}},M=(0,ai_store_1.writeAiContent)(S);M&&(f=M.id,h=M.truncated)}const b=a!==void 0&&a>=400||e.failure!==void 0&&!v,C=l??"?";!(0,emit_1.lensEmit)("ai",e.failure!==void 0&&!v?"error":"info",a!==void 0?`ai ${e.target.provider}/${C} ${e.target.api} \u2192 ${a}`:`ai ${e.target.provider}/${C} ${e.target.api} ${v?"aborted":"failed"}`,{name:e.target.provider,kind:"ai-call",provider:e.target.provider,api:e.target.api,...l?{model:l}:{},...o.model&&l&&o.model!==l?{requestedModel:o.model}:{},host:e.host,endpoint:e.endpoint,method:e.method,...a!==void 0?{status:a}:{},stream:m,startedAt:e.startedAt,durationMs:T,...e.ttfbMs!==void 0?{ttfbMs:e.ttfbMs}:{},...g!==void 0?{firstEventMs:g}:{},...Object.keys(s.usage).length?{usage:s.usage}:{},...s.finishReason?{finishReason:s.finishReason}:{},toolCallCount:s.toolCalls.length,sizes:{messageCount:o.messages.length,toolCount:o.tools.length,promptChars:w,completionChars:s.completion.length,...n!==void 0?{requestBytes:n}:{}},...o.inputCount!==void 0?{inputCount:o.inputCount}:{},...s.vectorCount!==void 0?{vectorCount:s.vectorCount}:{},contentCaptured:f!==void 0,...f?{contentRef:f}:{},...h?{truncated:!0}:{},...A?{aborted:!0}:{},..._?{timedOut:!0}:{},...s.error?{error:(0,ai_store_1.redactAiText)(s.error).slice(0,300)}:{}},b?`ai:error:${e.target.provider}:${C}`:`ai:${e.target.provider}:${C}`,e.ctx,"ai")&&f&&(0,ai_store_1.removeAiContent)(f)}u(capture,"capture"),c(capture,"capture"),d(capture,"capture");let savedFetch;const pending=new Set;async function flushAiTapForTests(){await Promise.allSettled([...pending])}u(flushAiTapForTests,"flushAiTapForTests"),c(flushAiTapForTests,"flushAiTapForTests"),d(flushAiTapForTests,"flushAiTapForTests");function resetAiTapForTests(){const e=globalThis;savedFetch&&(e.fetch=savedFetch),delete e.__lensmcpAiTap,savedFetch=void 0,pending.clear()}u(resetAiTapForTests,"resetAiTapForTests"),c(resetAiTapForTests,"resetAiTapForTests"),d(resetAiTapForTests,"resetAiTapForTests");function tapAi(){const e=aiCaptureMode();if(e==="off")return;const t=globalThis;if(typeof t.fetch!="function"||t.__lensmcpAiTap)return;t.__lensmcpAiTap=!0;const r=(0,ai_providers_1.parseExtraHosts)(process.env.LENSMCP_AI_HOSTS),n=t.fetch.bind(globalThis);savedFetch=t.fetch,t.fetch=(async(i,o)=>{const a=locate(i),p=methodOf(i,o),s=a&&p==="POST"?(0,ai_providers_1.matchTarget)(a.host,a.port,a.pathname,r):void 0;if(!a||!s)return n(i,o);const m=Date.now(),g=(0,flow_context_1.flowContext)(),y=a.pathname.length>MAX_ENDPOINT_CHARS?`${a.pathname.slice(0,MAX_ENDPOINT_CHARS)}\u2026`:a.pathname,v=takeRequestBody(i,o),_=o?.signal??(typeof i=="object"&&i!==null&&!(i instanceof URL)?i.signal:void 0),A=d(h=>{const b=capture(h).catch(()=>{});pending.add(b),b.finally(()=>pending.delete(b))},"track"),T={mode:e,target:s,host:a.host,endpoint:y,method:p,startedAt:m,ctx:g,signal:_,request:v};let l;try{l=await n(i,o)}catch(h){throw A({...T,failure:h}),h}const w=Date.now()-m;let f;try{f=l.clone()}catch{f=void 0}return A({...T,ttfbMs:w,res:l,...f?{clone:f}:{}}),l})}u(tapAi,"tapAi"),c(tapAi,"tapAi"),d(tapAi,"tapAi");
|
package/lib/tap-bullmq.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var b=Object.defineProperty;var a=(t,e)=>b(t,"name",{value:e,configurable:!0});var g=Object.defineProperty,i=a((t,e)=>g(t,"name",{value:e,configurable:!0}),"i");Object.defineProperty(exports,"__esModule",{value:!0}),exports.bullmqTap=bullmqTap,exports.stopWorkIntake=stopWorkIntake,exports.workIntakeReport=workIntakeReport,exports.resetQueueStatePublishersForTests=resetQueueStatePublishersForTests,exports.resetWorkIntakeForTests=resetWorkIntakeForTests;const active_work_1=require("./active-work"),workerGeneration=new WeakMap,headless_flow_1=require("@lensmcp/core/headless-flow"),emit_1=require("./emit"),flow_context_1=require("./flow-context");function jobFlow(t){const e=t?.opts?.__lensmcpFlow;return e&&typeof e=="object"?e:void 0}a(jobFlow,"jobFlow"),i(jobFlow,"jobFlow");const jobLegId=i((t,e)=>`job:${t}#${e?.id??"?"}`,"jobLegId");function flowOfJob(t,e){const o=jobFlow(e)?.flowId;if(o)return{flowId:o,own:!1};if((0,headless_flow_1.headlessFlowsEnabled)())return{flowId:(0,headless_flow_1.headlessJobFlowId)(t,e?.id),own:!0}}a(flowOfJob,"flowOfJob"),i(flowOfJob,"flowOfJob");const publishers=new Map,sources=new Map,sourceFor=i(t=>{let e=sources.get(t);return e||sources.set(t,e={}),e},"sourceFor"),usable=i(t=>!!t&&t.closing===void 0,"usable"),currentQueue=i(t=>{const e=sources.get(t),o=e?.app?.deref();return usable(o)?o:e?.own},"currentQueue");function ownConnection(t){if(!t||typeof t!="object")return;const e=t;if(typeof e.duplicate=="function")try{return e.duplicate()}catch{return}return Object.fromEntries(Object.entries(e).filter(([,o])=>typeof o!="function"))}a(ownConnection,"ownConnection"),i(ownConnection,"ownConnection");function bullmqTap(t){const e=t;if(typeof e?.Queue!="function"||typeof e?.Worker!="function")return!1;tappedQueue=e.Queue;try{tapQueueAdd(e)}catch{}try{tapWorkerEmit(e)}catch{}try{tapWorkerProcess(e)}catch{}try{tapWorkerStamp(e)}catch{}try{tapWorkerIntake(e)}catch{}try{tapWorkerConstruct(e)}catch{}try{installWorkIntakeControl()}catch{}return!0}a(bullmqTap,"bullmqTap"),i(bullmqTap,"bullmqTap");function stampWorker(t){try{if(workerGeneration.has(t))return;workerGeneration.set(t,(0,active_work_1.activeWorkGeneration)()),knownWorkers.add(new WeakRef(t))}catch{}}a(stampWorker,"stampWorker"),i(stampWorker,"stampWorker");function tapWorkerConstruct(t){const e=t.Worker;if(typeof e!="function"||e.__lensmcpGen)return;const o=new Proxy(e,{construct(r,n,s){const f=Reflect.construct(r,n,s);return stampWorker(f),f}});Object.defineProperty(e,"__lensmcpGen",{value:!0,enumerable:!1}),t.Worker=o}a(tapWorkerConstruct,"tapWorkerConstruct"),i(tapWorkerConstruct,"tapWorkerConstruct");function tapWorkerStamp(t){const e=t.Worker?.prototype;if(e)for(const o of["createLockManager","run"]){const r=e[o];if(typeof r!="function"||r.__lensmcpStamp)continue;const n=i(function(...s){return stampWorker(this),r.apply(this,s)},"wrapped");n.__lensmcpStamp=!0,e[o]=n}}a(tapWorkerStamp,"tapWorkerStamp"),i(tapWorkerStamp,"tapWorkerStamp");const knownWorkers=new Set,stoppedWorkers=new WeakSet,intake=new Map,INTAKE_GENERATIONS_KEPT=64;function intakeOf(t){let e=intake.get(t);if(!e){intake.set(t,e={workers:0,running:0,runningAtStop:0,takenAfterStop:0,finishedAfterStop:0});for(const o of intake.keys()){if(intake.size<=INTAKE_GENERATIONS_KEPT)break;intake.delete(o)}}return e}a(intakeOf,"intakeOf"),i(intakeOf,"intakeOf");function beginIntake(t,e){if(e===void 0)return()=>{};const o=intakeOf(e);return o.running+=1,stoppedWorkers.has(t)&&(o.takenAfterStop+=1),()=>{o.running-=1,o.stoppedAt!==void 0&&(o.finishedAfterStop+=1)}}a(beginIntake,"beginIntake"),i(beginIntake,"beginIntake");function stopWorkIntake(t){const e={workers:0,running:0,runningAtStop:0,takenAfterStop:0,finishedAfterStop:0},o=Date.now();for(const r of knownWorkers){const n=r.deref();if(!n){knownWorkers.delete(r);continue}const s=workerGeneration.get(n);if(s===void 0||s>=t||stoppedWorkers.has(n))continue;const f=n.pause;if(typeof f!="function")continue;stoppedWorkers.add(n),knownWorkers.delete(r);try{Promise.resolve(f.call(n,!0)).catch(()=>{})}catch{}const u=intakeOf(s);u.stoppedAt===void 0&&(u.stoppedAt=o,u.runningAtStop=u.running,e.running+=u.running,e.runningAtStop+=u.running),u.workers+=1,e.workers+=1}return e}a(stopWorkIntake,"stopWorkIntake"),i(stopWorkIntake,"stopWorkIntake");function workIntakeReport(t){const e=intake.get(t);if(!(!e||e.stoppedAt===void 0))return{workers:e.workers,running:e.running,runningAtStop:e.runningAtStop,takenAfterStop:e.takenAfterStop,finishedAfterStop:e.finishedAfterStop}}a(workIntakeReport,"workIntakeReport"),i(workIntakeReport,"workIntakeReport");function tapWorkerIntake(t){const e=t.Worker?.prototype;if(!e)return;const o=e.moveToActive;if(typeof o=="function"&&!o.__lensmcpIntake){const n=i(function(...s){return stoppedWorkers.has(this)?Promise.resolve(void 0):o.apply(this,s)},"wrapped");n.__lensmcpIntake=!0,e.moveToActive=n}const r=e.resume;if(typeof r=="function"&&!r.__lensmcpIntake){const n=i(function(...s){if(!stoppedWorkers.has(this))return r.apply(this,s)},"wrapped");n.__lensmcpIntake=!0,e.resume=n}}a(tapWorkerIntake,"tapWorkerIntake"),i(tapWorkerIntake,"tapWorkerIntake");function installWorkIntakeControl(){const t=globalThis;t.__lensmcpStopWorkIntake=stopWorkIntake,t.__lensmcpWorkIntakeReport=workIntakeReport}a(installWorkIntakeControl,"installWorkIntakeControl"),i(installWorkIntakeControl,"installWorkIntakeControl");function tapWorkerProcess(t){const e=t.Worker?.prototype,o=e?.callProcessJob;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(async function(n,...s){const f=workerGeneration.get(this),u=(0,active_work_1.beginActiveWork)(f),p=beginIntake(this,f),c=i(()=>{u(),p()},"endWork"),l=String(this?.name??n?.queueName??"queue"),m=flowOfJob(l,n);if(!m)return o.call(this,n,...s).finally(c);const w=jobLegId(l,n),d=Date.now(),h={flowId:m.flowId,requestId:w};if(m.own){const k=`job:${l}/${n?.name??"job"}`;(0,emit_1.lensEmit)("queue","info",`job ${n?.name??""}#${n?.id??"?"} \xB7 ${l}`,{queue:l,kind:"flow-origin",job:n?.name,jobId:n?.id,originNodeId:k},`queue-origin:${l}`,{...h,originType:"job",originNodeId:k})}return(0,flow_context_1.runInFlowContext)(h,async()=>{try{const k=await o.call(this,n,...s);return(0,emit_1.lensEmit)("queue","info",`process ${n?.name}#${n?.id} (${Date.now()-d}ms)`,{queue:l,kind:"queue-process",job:n?.name,jobId:n?.id,startedAt:d,durationMs:Date.now()-d,status:"ok"},`queue:${l}`,h),k}catch(k){throw(0,emit_1.lensEmit)("queue","error",`process ${n?.name}#${n?.id} failed (${Date.now()-d}ms)`,{queue:l,kind:"queue-process",job:n?.name,jobId:n?.id,startedAt:d,durationMs:Date.now()-d,status:"error",error:String(k?.message??k)},`queue:${l}`,h),k}finally{c()}})},"wrapped");r.__lensmcp=!0,e.callProcessJob=r}a(tapWorkerProcess,"tapWorkerProcess"),i(tapWorkerProcess,"tapWorkerProcess");function tapQueueAdd(t){const e=t.Queue?.prototype,o=e?.add;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(async function(n,...s){const f=Date.now(),u=(0,flow_context_1.flowContext)();if(u?.flowId){const c=s[1]??{};s[1]={...c,__lensmcpFlow:{flowId:u.flowId,...u.requestId?{originRequestId:u.requestId}:{}}}}const p=await o.call(this,n,...s);try{const c=String(this?.name??"queue"),l=s[0];let m;try{const d=JSON.stringify(l);m=d&&d!=="undefined"?d.length>300?d.slice(0,299)+"\u2026":d:void 0}catch{}(0,emit_1.lensEmit)("queue","info",`enqueued ${n}#${p?.id} \u2192 ${c}`,{queue:c,kind:"queue-enqueued",job:n,jobId:p?.id,...m?{data:m}:{},startedAt:f,durationMs:Date.now()-f},`queue:${c}`,u);const w=sourceFor(c);w.app?.deref()!==this&&(w.app=new WeakRef(this)),ensureStatePublisher(c)}catch{}return p},"wrapped");r.__lensmcp=!0,e.add=r}a(tapQueueAdd,"tapQueueAdd"),i(tapQueueAdd,"tapQueueAdd");function tapWorkerEmit(t){const e=t.Worker?.prototype,o=e?.emit;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(function(n,...s){try{observeWorkerEvent(this,String(n),s)}catch{}return o.call(this,n,...s)},"wrapped");r.__lensmcp=!0,e.emit=r}a(tapWorkerEmit,"tapWorkerEmit"),i(tapWorkerEmit,"tapWorkerEmit");function observeWorkerEvent(t,e,o){if(e!=="active"&&e!=="completed"&&e!=="failed")return;const r=String(t.name??"queue"),n=o[0],s=flowOfJob(r,n),f=s?{flowId:s.flowId,requestId:jobLegId(r,n)}:void 0;if(e==="active")(0,emit_1.lensEmit)("queue","info",`processing ${n?.name}#${n?.id}`,{queue:r,kind:"queue-active",jobId:n?.id},`queue:${r}`,f);else if(e==="completed"){let u;try{const p=n?.returnvalue??o[1],c=JSON.stringify(p);u=c&&c!=="undefined"?c.length>160?c.slice(0,159)+"\u2026":c:void 0}catch{}(0,emit_1.lensEmit)("queue","info",`completed ${n?.name}#${n?.id}`,{queue:r,kind:"queue-completed",jobId:n?.id,...u?{returned:u}:{}},`queue:${r}`,f)}else{const u=o[1];(0,emit_1.lensEmit)("queue","error",`job ${n?.id} failed: ${u?.message}`,{queue:r,kind:"queue-failed",jobId:n?.id,error:String(u?.message??u)},`queue:${r}`,f)}if(!currentQueue(r))try{const u=t.opts,p=ownConnection(u?.connection);if(p){const c=snapshotQueueClass();c&&(sourceFor(r).own=new c(r,{connection:p}))}}catch{}ensureStatePublisher(r)}a(observeWorkerEvent,"observeWorkerEvent"),i(observeWorkerEvent,"observeWorkerEvent");let tappedQueue;function snapshotQueueClass(){if(tappedQueue)return tappedQueue;try{return require("bullmq").Queue}catch{return}}a(snapshotQueueClass,"snapshotQueueClass"),i(snapshotQueueClass,"snapshotQueueClass");function ensureStatePublisher(t){if(publishers.has(t))return;let e="";const o=setInterval(()=>{(async()=>{try{const r=currentQueue(t);if(!r)return;const n=await r.getJobCounts(),s=[];for(const p of["active","waiting","failed","completed","delayed"])for(const c of await r.getJobs([p],0,7)){const l=c?.data?.to;s.push({id:String(c.id),name:c.name,state:p,timestamp:c.timestamp??0,...typeof l=="string"?{to:l}:{}})}s.sort((p,c)=>Number(c.timestamp)-Number(p.timestamp));const f=s.slice(0,8),u=JSON.stringify([n,f.map(p=>`${p.id}${p.state}`)]);if(u===e)return;e=u,(0,emit_1.lensEmit)("queue","info",`queue state: ${n.active??0} active \xB7 ${n.waiting??0} waiting \xB7 ${n.failed??0} failed`,{queue:t,kind:"queue-state",counts:n,recent:f},`queue:${t}`)}catch{}})()},2e3);o.unref?.(),publishers.set(t,o)}a(ensureStatePublisher,"ensureStatePublisher"),i(ensureStatePublisher,"ensureStatePublisher");function resetQueueStatePublishersForTests(){for(const t of publishers.values())clearInterval(t);publishers.clear(),sources.clear()}a(resetQueueStatePublishersForTests,"resetQueueStatePublishersForTests"),i(resetQueueStatePublishersForTests,"resetQueueStatePublishersForTests");function resetWorkIntakeForTests(){knownWorkers.clear(),intake.clear()}a(resetWorkIntakeForTests,"resetWorkIntakeForTests"),i(resetWorkIntakeForTests,"resetWorkIntakeForTests");
|
|
1
|
+
"use strict";var y=Object.defineProperty;var f=(t,e)=>y(t,"name",{value:e,configurable:!0});var b=Object.defineProperty,a=f((t,e)=>b(t,"name",{value:e,configurable:!0}),"a"),g=Object.defineProperty,i=a((t,e)=>g(t,"name",{value:e,configurable:!0}),"i");Object.defineProperty(exports,"__esModule",{value:!0}),exports.bullmqTap=bullmqTap,exports.stopWorkIntake=stopWorkIntake,exports.workIntakeReport=workIntakeReport,exports.resetQueueStatePublishersForTests=resetQueueStatePublishersForTests,exports.resetWorkIntakeForTests=resetWorkIntakeForTests;const active_work_1=require("./active-work"),workerGeneration=new WeakMap,headless_flow_1=require("@lensmcp/core/headless-flow"),emit_1=require("./emit"),flow_context_1=require("./flow-context");function jobFlow(t){const e=t?.opts?.__lensmcpFlow;return e&&typeof e=="object"?e:void 0}f(jobFlow,"jobFlow"),a(jobFlow,"jobFlow"),i(jobFlow,"jobFlow");const jobLegId=i((t,e)=>`job:${t}#${e?.id??"?"}`,"jobLegId");function flowOfJob(t,e){const o=jobFlow(e)?.flowId;if(o)return{flowId:o,own:!1};if((0,headless_flow_1.headlessFlowsEnabled)())return{flowId:(0,headless_flow_1.headlessJobFlowId)(t,e?.id),own:!0}}f(flowOfJob,"flowOfJob"),a(flowOfJob,"flowOfJob"),i(flowOfJob,"flowOfJob");const publishers=new Map,sources=new Map,sourceFor=i(t=>{let e=sources.get(t);return e||sources.set(t,e={}),e},"sourceFor"),usable=i(t=>!!t&&t.closing===void 0,"usable"),currentQueue=i(t=>{const e=sources.get(t),o=e?.app?.deref();return usable(o)?o:e?.own},"currentQueue");function ownConnection(t){if(!t||typeof t!="object")return;const e=t;if(typeof e.duplicate=="function")try{return e.duplicate()}catch{return}return Object.fromEntries(Object.entries(e).filter(([,o])=>typeof o!="function"))}f(ownConnection,"ownConnection"),a(ownConnection,"ownConnection"),i(ownConnection,"ownConnection");function bullmqTap(t){const e=t;if(typeof e?.Queue!="function"||typeof e?.Worker!="function")return!1;tappedQueue=e.Queue;try{tapQueueAdd(e)}catch{}try{tapWorkerEmit(e)}catch{}try{tapWorkerProcess(e)}catch{}try{tapWorkerStamp(e)}catch{}try{tapWorkerIntake(e)}catch{}try{tapWorkerConstruct(e)}catch{}try{installWorkIntakeControl()}catch{}return!0}f(bullmqTap,"bullmqTap"),a(bullmqTap,"bullmqTap"),i(bullmqTap,"bullmqTap");function stampWorker(t){try{if(workerGeneration.has(t))return;workerGeneration.set(t,(0,active_work_1.activeWorkGeneration)()),knownWorkers.add(new WeakRef(t))}catch{}}f(stampWorker,"stampWorker"),a(stampWorker,"stampWorker"),i(stampWorker,"stampWorker");function tapWorkerConstruct(t){const e=t.Worker;if(typeof e!="function"||e.__lensmcpGen)return;const o=new Proxy(e,{construct(r,n,s){const p=Reflect.construct(r,n,s);return stampWorker(p),p}});Object.defineProperty(e,"__lensmcpGen",{value:!0,enumerable:!1}),t.Worker=o}f(tapWorkerConstruct,"tapWorkerConstruct"),a(tapWorkerConstruct,"tapWorkerConstruct"),i(tapWorkerConstruct,"tapWorkerConstruct");function tapWorkerStamp(t){const e=t.Worker?.prototype;if(e)for(const o of["createLockManager","run"]){const r=e[o];if(typeof r!="function"||r.__lensmcpStamp)continue;const n=i(function(...s){return stampWorker(this),r.apply(this,s)},"wrapped");n.__lensmcpStamp=!0,e[o]=n}}f(tapWorkerStamp,"tapWorkerStamp"),a(tapWorkerStamp,"tapWorkerStamp"),i(tapWorkerStamp,"tapWorkerStamp");const knownWorkers=new Set,stoppedWorkers=new WeakSet,intake=new Map,INTAKE_GENERATIONS_KEPT=64;function intakeOf(t){let e=intake.get(t);if(!e){intake.set(t,e={workers:0,running:0,runningAtStop:0,takenAfterStop:0,finishedAfterStop:0});for(const o of intake.keys()){if(intake.size<=INTAKE_GENERATIONS_KEPT)break;intake.delete(o)}}return e}f(intakeOf,"intakeOf"),a(intakeOf,"intakeOf"),i(intakeOf,"intakeOf");function beginIntake(t,e){if(e===void 0)return()=>{};const o=intakeOf(e);return o.running+=1,stoppedWorkers.has(t)&&(o.takenAfterStop+=1),()=>{o.running-=1,o.stoppedAt!==void 0&&(o.finishedAfterStop+=1)}}f(beginIntake,"beginIntake"),a(beginIntake,"beginIntake"),i(beginIntake,"beginIntake");function stopWorkIntake(t){const e={workers:0,running:0,runningAtStop:0,takenAfterStop:0,finishedAfterStop:0},o=Date.now();for(const r of knownWorkers){const n=r.deref();if(!n){knownWorkers.delete(r);continue}const s=workerGeneration.get(n);if(s===void 0||s>=t||stoppedWorkers.has(n))continue;const p=n.pause;if(typeof p!="function")continue;stoppedWorkers.add(n),knownWorkers.delete(r);try{Promise.resolve(p.call(n,!0)).catch(()=>{})}catch{}const u=intakeOf(s);u.stoppedAt===void 0&&(u.stoppedAt=o,u.runningAtStop=u.running,e.running+=u.running,e.runningAtStop+=u.running),u.workers+=1,e.workers+=1}return e}f(stopWorkIntake,"stopWorkIntake"),a(stopWorkIntake,"stopWorkIntake"),i(stopWorkIntake,"stopWorkIntake");function workIntakeReport(t){const e=intake.get(t);if(!(!e||e.stoppedAt===void 0))return{workers:e.workers,running:e.running,runningAtStop:e.runningAtStop,takenAfterStop:e.takenAfterStop,finishedAfterStop:e.finishedAfterStop}}f(workIntakeReport,"workIntakeReport"),a(workIntakeReport,"workIntakeReport"),i(workIntakeReport,"workIntakeReport");function tapWorkerIntake(t){const e=t.Worker?.prototype;if(!e)return;const o=e.moveToActive;if(typeof o=="function"&&!o.__lensmcpIntake){const n=i(function(...s){return stoppedWorkers.has(this)?Promise.resolve(void 0):o.apply(this,s)},"wrapped");n.__lensmcpIntake=!0,e.moveToActive=n}const r=e.resume;if(typeof r=="function"&&!r.__lensmcpIntake){const n=i(function(...s){if(!stoppedWorkers.has(this))return r.apply(this,s)},"wrapped");n.__lensmcpIntake=!0,e.resume=n}}f(tapWorkerIntake,"tapWorkerIntake"),a(tapWorkerIntake,"tapWorkerIntake"),i(tapWorkerIntake,"tapWorkerIntake");function installWorkIntakeControl(){const t=globalThis;t.__lensmcpStopWorkIntake=stopWorkIntake,t.__lensmcpWorkIntakeReport=workIntakeReport}f(installWorkIntakeControl,"installWorkIntakeControl"),a(installWorkIntakeControl,"installWorkIntakeControl"),i(installWorkIntakeControl,"installWorkIntakeControl");function tapWorkerProcess(t){const e=t.Worker?.prototype,o=e?.callProcessJob;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(async function(n,...s){const p=workerGeneration.get(this),u=(0,active_work_1.beginActiveWork)(p),l=beginIntake(this,p),c=i(()=>{u(),l()},"endWork"),d=String(this?.name??n?.queueName??"queue"),w=flowOfJob(d,n);if(!w)return o.call(this,n,...s).finally(c);const h=jobLegId(d,n),k=Date.now(),I={flowId:w.flowId,requestId:h};if(w.own){const m=`job:${d}/${n?.name??"job"}`;(0,emit_1.lensEmit)("queue","info",`job ${n?.name??""}#${n?.id??"?"} \xB7 ${d}`,{queue:d,kind:"flow-origin",job:n?.name,jobId:n?.id,originNodeId:m},`queue-origin:${d}`,{...I,originType:"job",originNodeId:m})}return(0,flow_context_1.runInFlowContext)(I,async()=>{try{const m=await o.call(this,n,...s);return(0,emit_1.lensEmit)("queue","info",`process ${n?.name}#${n?.id} (${Date.now()-k}ms)`,{queue:d,kind:"queue-process",job:n?.name,jobId:n?.id,startedAt:k,durationMs:Date.now()-k,status:"ok"},`queue:${d}`,I),m}catch(m){throw(0,emit_1.lensEmit)("queue","error",`process ${n?.name}#${n?.id} failed (${Date.now()-k}ms)`,{queue:d,kind:"queue-process",job:n?.name,jobId:n?.id,startedAt:k,durationMs:Date.now()-k,status:"error",error:String(m?.message??m)},`queue:${d}`,I),m}finally{c()}})},"wrapped");r.__lensmcp=!0,e.callProcessJob=r}f(tapWorkerProcess,"tapWorkerProcess"),a(tapWorkerProcess,"tapWorkerProcess"),i(tapWorkerProcess,"tapWorkerProcess");function tapQueueAdd(t){const e=t.Queue?.prototype,o=e?.add;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(async function(n,...s){const p=Date.now(),u=(0,flow_context_1.flowContext)();if(u?.flowId){const c=s[1]??{};s[1]={...c,__lensmcpFlow:{flowId:u.flowId,...u.requestId?{originRequestId:u.requestId}:{}}}}const l=await o.call(this,n,...s);try{const c=String(this?.name??"queue"),d=s[0];let w;try{const k=JSON.stringify(d);w=k&&k!=="undefined"?k.length>300?k.slice(0,299)+"\u2026":k:void 0}catch{}(0,emit_1.lensEmit)("queue","info",`enqueued ${n}#${l?.id} \u2192 ${c}`,{queue:c,kind:"queue-enqueued",job:n,jobId:l?.id,...w?{data:w}:{},startedAt:p,durationMs:Date.now()-p},`queue:${c}`,u);const h=sourceFor(c);h.app?.deref()!==this&&(h.app=new WeakRef(this)),ensureStatePublisher(c)}catch{}return l},"wrapped");r.__lensmcp=!0,e.add=r}f(tapQueueAdd,"tapQueueAdd"),a(tapQueueAdd,"tapQueueAdd"),i(tapQueueAdd,"tapQueueAdd");function tapWorkerEmit(t){const e=t.Worker?.prototype,o=e?.emit;if(!e||typeof o!="function"||o.__lensmcp)return;const r=i(function(n,...s){try{observeWorkerEvent(this,String(n),s)}catch{}return o.call(this,n,...s)},"wrapped");r.__lensmcp=!0,e.emit=r}f(tapWorkerEmit,"tapWorkerEmit"),a(tapWorkerEmit,"tapWorkerEmit"),i(tapWorkerEmit,"tapWorkerEmit");function observeWorkerEvent(t,e,o){if(e!=="active"&&e!=="completed"&&e!=="failed")return;const r=String(t.name??"queue"),n=o[0],s=flowOfJob(r,n),p=s?{flowId:s.flowId,requestId:jobLegId(r,n)}:void 0;if(e==="active")(0,emit_1.lensEmit)("queue","info",`processing ${n?.name}#${n?.id}`,{queue:r,kind:"queue-active",jobId:n?.id},`queue:${r}`,p);else if(e==="completed"){let u;try{const l=n?.returnvalue??o[1],c=JSON.stringify(l);u=c&&c!=="undefined"?c.length>160?c.slice(0,159)+"\u2026":c:void 0}catch{}(0,emit_1.lensEmit)("queue","info",`completed ${n?.name}#${n?.id}`,{queue:r,kind:"queue-completed",jobId:n?.id,...u?{returned:u}:{}},`queue:${r}`,p)}else{const u=o[1];(0,emit_1.lensEmit)("queue","error",`job ${n?.id} failed: ${u?.message}`,{queue:r,kind:"queue-failed",jobId:n?.id,error:String(u?.message??u)},`queue:${r}`,p)}if(!currentQueue(r))try{const u=t.opts,l=ownConnection(u?.connection);if(l){const c=snapshotQueueClass();c&&(sourceFor(r).own=new c(r,{connection:l}))}}catch{}ensureStatePublisher(r)}f(observeWorkerEvent,"observeWorkerEvent"),a(observeWorkerEvent,"observeWorkerEvent"),i(observeWorkerEvent,"observeWorkerEvent");let tappedQueue;function snapshotQueueClass(){if(tappedQueue)return tappedQueue;try{return require("bullmq").Queue}catch{return}}f(snapshotQueueClass,"snapshotQueueClass"),a(snapshotQueueClass,"snapshotQueueClass"),i(snapshotQueueClass,"snapshotQueueClass");function ensureStatePublisher(t){if(publishers.has(t))return;let e="";const o=setInterval(()=>{(async()=>{try{const r=currentQueue(t);if(!r)return;const n=await r.getJobCounts(),s=[];for(const l of["active","waiting","failed","completed","delayed"])for(const c of await r.getJobs([l],0,7)){const d=c?.data?.to;s.push({id:String(c.id),name:c.name,state:l,timestamp:c.timestamp??0,...typeof d=="string"?{to:d}:{}})}s.sort((l,c)=>Number(c.timestamp)-Number(l.timestamp));const p=s.slice(0,8),u=JSON.stringify([n,p.map(l=>`${l.id}${l.state}`)]);if(u===e)return;e=u,(0,emit_1.lensEmit)("queue","info",`queue state: ${n.active??0} active \xB7 ${n.waiting??0} waiting \xB7 ${n.failed??0} failed`,{queue:t,kind:"queue-state",counts:n,recent:p},`queue:${t}`)}catch{}})()},2e3);o.unref?.(),publishers.set(t,o)}f(ensureStatePublisher,"ensureStatePublisher"),a(ensureStatePublisher,"ensureStatePublisher"),i(ensureStatePublisher,"ensureStatePublisher");function resetQueueStatePublishersForTests(){for(const t of publishers.values())clearInterval(t);publishers.clear(),sources.clear()}f(resetQueueStatePublishersForTests,"resetQueueStatePublishersForTests"),a(resetQueueStatePublishersForTests,"resetQueueStatePublishersForTests"),i(resetQueueStatePublishersForTests,"resetQueueStatePublishersForTests");function resetWorkIntakeForTests(){knownWorkers.clear(),intake.clear()}f(resetWorkIntakeForTests,"resetWorkIntakeForTests"),a(resetWorkIntakeForTests,"resetWorkIntakeForTests"),i(resetWorkIntakeForTests,"resetWorkIntakeForTests");
|
package/lib/tap-egress.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var A=Object.defineProperty;var u=(t,e)=>A(t,"name",{value:e,configurable:!0});var F=Object.defineProperty,i=u((t,e)=>F(t,"name",{value:e,configurable:!0}),"i");Object.defineProperty(exports,"__esModule",{value:!0}),exports.flushEgressCaptureForTests=flushEgressCaptureForTests,exports.tapEgress=tapEgress,exports.resetEgressTapForTests=resetEgressTapForTests;const redact_1=require("@lensmcp/core/redact"),emit_1=require("./emit"),flow_context_1=require("./flow-context"),ai_providers_1=require("./ai-providers"),http_capture_1=require("./http-capture"),http_store_1=require("./http-store"),capture_file_1=require("./capture-file"),tap_ai_1=require("./tap-ai"),INTERNAL=[/\.local$/i,/\.internal$/i,/^localhost$/i,/^127\./,/^\[?::1\]?$/,/^0\.0\.0\.0$/,/^10\./,/^192\.168\./,/^172\.(1[6-9]|2\d|3[01])\./,/^169\.254\./,/^\[?[fF][cCdD][0-9a-fA-F]{2}:/,/^\[?[fF][eE]80:/],isInternal=i(t=>t===""||INTERNAL.some(e=>e.test(t)),"isInternal"),CAPTURE_QUERY=process.env.LENSMCP_EGRESS_QUERY==="1",MAX_PATH_CHARS=256,clipPath=i(t=>t.length>MAX_PATH_CHARS?`${t.slice(0,MAX_PATH_CHARS)}\u2026`:t,"clipPath");function capturePath(t,e){const s=t||"/";return!CAPTURE_QUERY||e.length<=1?clipPath(s):(0,redact_1.redactUrl)(`${s}${e}`)}u(capturePath,"capturePath"),i(capturePath,"capturePath");const fingerprintFor=i((t,e)=>e?`external:error:${t}`:`external:${t}`,"fingerprintFor"),pendingExchanges=new Set;async function flushEgressCaptureForTests(){await Promise.allSettled([...pendingExchanges])}u(flushEgressCaptureForTests,"flushEgressCaptureForTests"),i(flushEgressCaptureForTests,"flushEgressCaptureForTests");const headerPairs=i(t=>{const e=[];try{t?.headers?.forEach?.((s,p)=>e.push([p,s]))}catch{}return e},"headerPairs");async function finishExchange(t){const e=[],s=[],p=await(0,http_capture_1.requestPart)(t.request,t.url.search,t.mode,!!t.aiOpaque,s,e),r=t.reading?await t.reading.catch(()=>{}):void 0,{headers:c,redacted:a}=(0,http_store_1.redactHeaders)(headerPairs(t.res));e.push(...a);const n=typeof t.res?.status=="number"?t.res.status:void 0,d={schemaVersion:1,id:t.id,capturedAt:Date.now(),side:"egress",service:emit_1.PROJECT,...t.ctx?.flowId?{flowId:t.ctx.flowId}:{},...t.ctx?.requestId?{requestId:t.ctx.requestId}:{},method:t.method,url:`${t.url.protocol}//${t.url.host}${t.path}`,host:t.url.hostname,path:t.path,...n!==void 0?{status:n}:{},...t.res?.statusText?{statusText:t.res.statusText}:{},...t.error?{error:t.error}:{},startedAt:t.startedAt,...t.ttfbMs!==void 0?{ttfbMs:t.ttfbMs}:{},durationMs:(r?.endedAt??Date.now())-t.startedAt,capture:t.mode==="bodies"?"bodies":"headers",request:p,response:{headers:c,...r?.body?{body:r.body}:{},...r?.stream?{stream:r.stream}:{}},redacted:{headers:[...new Set(e)],bodyFields:[...s.map(o=>`request.${o}`),...t.responseFields.map(o=>`response.${o}`)]},...t.ai?{ai:{provider:t.ai}}:{}};(0,http_store_1.writeHttpExchange)(d)}u(finishExchange,"finishExchange"),i(finishExchange,"finishExchange");function trackExchange(t){const e=finishExchange(t).catch(()=>{});pendingExchanges.add(e),e.finally(()=>pendingExchanges.delete(e))}u(trackExchange,"trackExchange"),i(trackExchange,"trackExchange");function tapEgress(){tapFetch(),tapHttpModule("http"),tapHttpModule("https")}u(tapEgress,"tapEgress"),i(tapEgress,"tapEgress");let savedFetch;const httpOriginals=new Map;function resetEgressTapForTests(){const t=globalThis;savedFetch&&(t.fetch=savedFetch),delete t.__lensmcpFetchTap,restoreHttp("http"),restoreHttp("https"),savedFetch=void 0}u(resetEgressTapForTests,"resetEgressTapForTests"),i(resetEgressTapForTests,"resetEgressTapForTests");function restoreHttp(t){const e=httpOriginals.get(t);if(!e)return;const s=require(`node:${t}`);s.request=e.request,s.get=e.get,httpOriginals.delete(t)}u(restoreHttp,"restoreHttp"),i(restoreHttp,"restoreHttp");function tapFetch(){const t=globalThis;if(typeof t.fetch!="function"||t.__lensmcpFetchTap)return;t.__lensmcpFetchTap=!0;const e=t.fetch.bind(globalThis);savedFetch=t.fetch;const s=(0,http_store_1.httpCaptureMode)(),p=(0,ai_providers_1.parseExtraHosts)(process.env.LENSMCP_AI_HOSTS);t.fetch=(async(r,c)=>{let a="",n="/",d=!1,o;try{const h=new URL(typeof r=="string"?r:r instanceof URL?r.href:r.url);o=h,a=h.hostname,d=h.search.length>1,n=capturePath(h.pathname,h.search)}catch{}if(isInternal(a))return e(r,c);const f=Date.now(),l=(0,flow_context_1.flowContext)(),E=(0,flow_context_1.liveFlowContext)();E&&(E.externalCallCount=(E.externalCallCount??0)+1);const C=typeof r=="object"&&r!==null&&!(r instanceof URL)?r.method:void 0,m=String(c?.method??C??"GET").toUpperCase(),g=s!=="off"&&o?(0,http_store_1.newExchangeId)():void 0,b=g?(0,http_capture_1.takeRequest)(r,c,s==="bodies"):void 0,_=g&&o&&m==="POST"?(0,ai_providers_1.matchTarget)(a,o.port,o.pathname,p)?.provider:void 0,w=!!_&&(0,tap_ai_1.aiCaptureMode)()!=="content"&&!(0,capture_file_1.aiBodiesAsked)(),$=i(h=>({id:g,mode:s,method:m,url:o,path:o.pathname.length>256?`${o.pathname.slice(0,256)}\u2026`:o.pathname,startedAt:f,ctx:l,request:b,..._?{ai:_}:{},...w?{aiOpaque:w}:{},responseFields:[],...h}),"job");try{const h=await e(r,c),x=Date.now()-f,T=h.status>=500,q=[];let y;if(g&&s==="bodies")try{y=(0,http_capture_1.readResponse)(h,h.clone(),f,{ai:w,paths:q})}catch{y=void 0}const v=(0,emit_1.lensEmit)("external","info",`egress ${m} ${a} \u2192 ${h.status}`,{name:a,kind:"egress",method:m,host:a,path:n,status:h.status,...d?{hasQuery:!0}:{},startedAt:f,durationMs:x,...g?{exchangeId:g}:{}},fingerprintFor(a,T),l);return g&&v&&trackExchange($({res:h,ttfbMs:x,responseFields:q,...y?{reading:y}:{}})),h}catch(h){const x=String(h?.message??h),T=(0,emit_1.lensEmit)("external","error",`egress ${m} ${a} failed`,{name:a,kind:"egress",method:m,host:a,path:n,...d?{hasQuery:!0}:{},error:x,startedAt:f,durationMs:Date.now()-f,...g?{exchangeId:g}:{}},fingerprintFor(a,!0),l);throw g&&T&&trackExchange($({error:x})),h}})}u(tapFetch,"tapFetch"),i(tapFetch,"tapFetch");function tapHttpModule(t){const e=require(`node:${t}`),s=e.request;if(typeof s!="function"||s.__lensmcp)return;httpOriginals.set(t,{request:s,get:e.get});const p=i(function(...r){const c=s.apply(this,r);try{observe(r,c)}catch{}return c},"wrapped");p.__lensmcp=!0,e.request=p,typeof e.get=="function"&&(e.get=function(...r){const c=p.apply(this,r);return c.end(),c})}u(tapHttpModule,"tapHttpModule"),i(tapHttpModule,"tapHttpModule");function observe(t,e){const{host:s,method:p,path:r,hasQuery:c}=parseTarget(t);if(isInternal(s))return;const a=Date.now(),n=(0,flow_context_1.flowContext)(),d=(0,flow_context_1.liveFlowContext)();d&&(d.externalCallCount=(d.externalCallCount??0)+1);let o=!1;e.once("response",(...f)=>{if(o)return;o=!0;const l=f[0]?.statusCode;(0,emit_1.lensEmit)("external","info",`egress ${p} ${s} \u2192 ${l??"?"}`,{name:s,kind:"egress",method:p,host:s,path:r,...l!=null?{status:l}:{},...c?{hasQuery:!0}:{},startedAt:a,durationMs:Date.now()-a},fingerprintFor(s,l!=null&&l>=500),n)}),e.once("error",(...f)=>{if(o)return;o=!0;const l=f[0];(0,emit_1.lensEmit)("external","error",`egress ${p} ${s} failed`,{name:s,kind:"egress",method:p,host:s,path:r,...c?{hasQuery:!0}:{},error:String(l?.message??l),startedAt:a,durationMs:Date.now()-a},fingerprintFor(s,!0),n)})}u(observe,"observe"),i(observe,"observe");function parseTarget(t){let e="",s="GET",p="/",r=!1;const c=i((a,n)=>{r=n.length>1,p=capturePath(a,n)},"take");for(const a of t)if(typeof a=="string")try{const n=new URL(a);e=n.hostname,c(n.pathname,n.search)}catch{}else if(a instanceof URL)e=a.hostname,c(a.pathname,a.search);else if(a&&typeof a=="object"){const n=a;if(typeof n.method=="string"&&(s=n.method.toUpperCase()),typeof n.socketPath=="string")return{host:"",method:s,path:p,hasQuery:r};if(typeof n.path=="string"){const d=n.path,o=d.indexOf("?");c(o===-1?d:d.slice(0,o),o===-1?"":d.slice(o))}typeof n.hostname=="string"&&n.hostname?e=n.hostname:typeof n.host=="string"&&n.host&&(e=n.host.split(":")[0]??"")}return{host:e,method:s,path:p,hasQuery:r}}u(parseTarget,"parseTarget"),i(parseTarget,"parseTarget");
|
|
1
|
+
"use strict";var k=Object.defineProperty;var E=(e,t)=>k(e,"name",{value:t,configurable:!0});var P=Object.defineProperty,x=E((e,t)=>P(e,"name",{value:t,configurable:!0}),"x"),H=Object.defineProperty,c=x((e,t)=>H(e,"name",{value:t,configurable:!0}),"c");Object.defineProperty(exports,"__esModule",{value:!0}),exports.flushEgressCaptureForTests=flushEgressCaptureForTests,exports.tapEgress=tapEgress,exports.resetEgressTapForTests=resetEgressTapForTests;const redact_1=require("@lensmcp/core/redact"),emit_1=require("./emit"),flow_context_1=require("./flow-context"),ai_providers_1=require("./ai-providers"),http_capture_1=require("./http-capture"),http_store_1=require("./http-store"),capture_file_1=require("./capture-file"),tap_ai_1=require("./tap-ai"),INTERNAL=[/\.local$/i,/\.internal$/i,/^localhost$/i,/^127\./,/^\[?::1\]?$/,/^0\.0\.0\.0$/,/^10\./,/^192\.168\./,/^172\.(1[6-9]|2\d|3[01])\./,/^169\.254\./,/^\[?[fF][cCdD][0-9a-fA-F]{2}:/,/^\[?[fF][eE]80:/],isInternal=c(e=>e===""||INTERNAL.some(t=>t.test(e)),"isInternal"),CAPTURE_QUERY=process.env.LENSMCP_EGRESS_QUERY==="1",MAX_PATH_CHARS=256,clipPath=c(e=>e.length>MAX_PATH_CHARS?`${e.slice(0,MAX_PATH_CHARS)}\u2026`:e,"clipPath");function capturePath(e,t){const r=e||"/";return!CAPTURE_QUERY||t.length<=1?clipPath(r):(0,redact_1.redactUrl)(`${r}${t}`)}E(capturePath,"capturePath"),x(capturePath,"capturePath"),c(capturePath,"capturePath");const fingerprintFor=c((e,t)=>t?`external:error:${e}`:`external:${e}`,"fingerprintFor"),pendingExchanges=new Set;async function flushEgressCaptureForTests(){await Promise.allSettled([...pendingExchanges])}E(flushEgressCaptureForTests,"flushEgressCaptureForTests"),x(flushEgressCaptureForTests,"flushEgressCaptureForTests"),c(flushEgressCaptureForTests,"flushEgressCaptureForTests");async function finishExchange(e){const t=[],r=[],u=await(0,http_capture_1.requestPart)(e.request,e.url.search,e.mode,!!e.aiOpaque,r,t),a=e.reading?await e.reading.catch(()=>{}):void 0,{headers:d,redacted:s}=(0,http_store_1.redactHeaders)(e.responseHeaders??[]);t.push(...s);const o=typeof e.status=="number"?e.status:void 0,m={schemaVersion:1,id:e.id,capturedAt:Date.now(),side:"egress",service:emit_1.PROJECT,...e.ctx?.flowId?{flowId:e.ctx.flowId}:{},...e.ctx?.requestId?{requestId:e.ctx.requestId}:{},method:e.method,url:`${e.url.protocol}//${e.url.host}${e.path}`,host:e.url.hostname,path:e.path,...o!==void 0?{status:o}:{},...e.statusText?{statusText:e.statusText}:{},...e.error?{error:e.error}:{},startedAt:e.startedAt,...e.ttfbMs!==void 0?{ttfbMs:e.ttfbMs}:{},durationMs:(a?.endedAt??Date.now())-e.startedAt,capture:e.mode==="bodies"?"bodies":"headers",request:u,response:{headers:d,...a?.body?{body:a.body}:{},...a?.stream?{stream:a.stream}:{}},redacted:{headers:[...new Set(t)],bodyFields:[...r.map(i=>`request.${i}`),...e.responseFields.map(i=>`response.${i}`)]},...e.ai?{ai:{provider:e.ai}}:{}};(0,http_store_1.writeHttpExchange)(m)}E(finishExchange,"finishExchange"),x(finishExchange,"finishExchange"),c(finishExchange,"finishExchange");function trackExchange(e){const t=(typeof e=="function"?new Promise(r=>setImmediate(r)).then(()=>finishExchange(e())):finishExchange(e)).catch(()=>{});pendingExchanges.add(t),t.finally(()=>pendingExchanges.delete(t))}E(trackExchange,"trackExchange"),x(trackExchange,"trackExchange"),c(trackExchange,"trackExchange");function tapEgress(){tapFetch(),tapHttpModule("http"),tapHttpModule("https")}E(tapEgress,"tapEgress"),x(tapEgress,"tapEgress"),c(tapEgress,"tapEgress");let savedFetch;const httpOriginals=new Map;function resetEgressTapForTests(){const e=globalThis;savedFetch&&(e.fetch=savedFetch),delete e.__lensmcpFetchTap,restoreHttp("http"),restoreHttp("https"),savedFetch=void 0}E(resetEgressTapForTests,"resetEgressTapForTests"),x(resetEgressTapForTests,"resetEgressTapForTests"),c(resetEgressTapForTests,"resetEgressTapForTests");function restoreHttp(e){const t=httpOriginals.get(e);if(!t)return;const r=require(`node:${e}`);r.request=t.request,r.get=t.get,httpOriginals.delete(e)}E(restoreHttp,"restoreHttp"),x(restoreHttp,"restoreHttp"),c(restoreHttp,"restoreHttp");function tapFetch(){const e=globalThis;if(typeof e.fetch!="function"||e.__lensmcpFetchTap)return;e.__lensmcpFetchTap=!0;const t=e.fetch.bind(globalThis);savedFetch=e.fetch;const r=(0,http_store_1.httpCaptureMode)(),u=(0,ai_providers_1.parseExtraHosts)(process.env.LENSMCP_AI_HOSTS);e.fetch=(async(a,d)=>{let s="",o="/",m=!1,i;try{const h=new URL(typeof a=="string"?a:a instanceof URL?a.href:a.url);i=h,s=h.hostname,m=h.search.length>1,o=capturePath(h.pathname,h.search)}catch{}if(isInternal(s))return t(a,d);const l=Date.now(),f=(0,flow_context_1.flowContext)(),n=(0,flow_context_1.liveFlowContext)();n&&(n.externalCallCount=(n.externalCallCount??0)+1);const T=typeof a=="object"&&a!==null&&!(a instanceof URL)?a.method:void 0,p=String(d?.method??T??"GET").toUpperCase(),w=r!=="off"&&i?(0,http_store_1.newExchangeId)():void 0,b=w?(0,http_capture_1.takeRequest)(a,d,r==="bodies"):void 0,C=w&&i&&p==="POST"?(0,ai_providers_1.matchTarget)(s,i.port,i.pathname,u)?.provider:void 0,v=!!C&&(0,tap_ai_1.aiCaptureMode)()!=="content"&&!(0,capture_file_1.aiBodiesAsked)(),$=c(h=>({id:w,mode:r,method:p,url:i,path:i.pathname.length>256?`${i.pathname.slice(0,256)}\u2026`:i.pathname,startedAt:l,ctx:f,request:b,...C?{ai:C}:{},...v?{aiOpaque:v}:{},responseFields:[],...h}),"job");try{const h=await t(a,d),_=Date.now()-l,F=h.status>=500,g=[];let y;if(w&&r==="bodies")try{y=(0,http_capture_1.readResponse)(h,h.clone(),l,{ai:v,paths:g})}catch{y=void 0}const q=(0,emit_1.lensEmit)("external","info",`egress ${p} ${s} \u2192 ${h.status}`,{name:s,kind:"egress",method:p,host:s,path:o,status:h.status,...m?{hasQuery:!0}:{},startedAt:l,durationMs:_,...w?{exchangeId:w}:{}},fingerprintFor(s,F),f);if(w&&q){const M={status:h.status,...h.statusText?{statusText:h.statusText}:{},responseHeaders:(0,http_capture_1.headerPairs)(h.headers)};trackExchange($({...M,ttfbMs:_,responseFields:g,...y?{reading:y}:{}}))}return h}catch(h){const _=String(h?.message??h),F=(0,emit_1.lensEmit)("external","error",`egress ${p} ${s} failed`,{name:s,kind:"egress",method:p,host:s,path:o,...m?{hasQuery:!0}:{},error:_,startedAt:l,durationMs:Date.now()-l,...w?{exchangeId:w}:{}},fingerprintFor(s,!0),f);throw w&&F&&trackExchange($({error:_})),h}})}E(tapFetch,"tapFetch"),x(tapFetch,"tapFetch"),c(tapFetch,"tapFetch");function tapHttpModule(e){const t=require(`node:${e}`),r=t.request;if(typeof r!="function"||r.__lensmcp)return;httpOriginals.set(e,{request:r,get:t.get});const u=(0,http_store_1.httpCaptureMode)(),a=(0,ai_providers_1.parseExtraHosts)(process.env.LENSMCP_AI_HOSTS),d=c(function(...s){const o=r.apply(this,s);try{observe(e,s,o,u,a)}catch{}return o},"wrapped");d.__lensmcp=!0,t.request=d,typeof t.get=="function"&&(t.get=function(...s){const o=d.apply(this,s);return o.end(),o})}E(tapHttpModule,"tapHttpModule"),x(tapHttpModule,"tapHttpModule"),c(tapHttpModule,"tapHttpModule");const headerOf=c((e,t)=>{const r=e?.[t];return Array.isArray(r)?r.join(", "):r??""},"headerOf");function teeRequest(e,t,r){const u=t?new http_capture_1.WrittenBody(!r):void 0,a=c(()=>{try{return e.getHeaders?.()}catch{return}},"sentHeaders");let d,s=!1;const o=e.write;u&&typeof o=="function"&&(e.write=function(i,...l){try{s||u.take(i,l[0])}catch{}return o.call(this,i,...l)});const m=e.end;return e.end=function(i,...l){try{s||(s=!0,u?.take(i,l[0]),d=a())}catch{}return m.call(this,i,...l)},()=>(0,http_capture_1.writtenRequest)(d??a(),u)}E(teeRequest,"teeRequest"),x(teeRequest,"teeRequest"),c(teeRequest,"teeRequest");function teeResponse(e,t,r){let u=!1;const a=c(o=>{if(u)return;u=!0,clearTimeout(d);const m=Date.now();r(()=>t.size?t.finish(o,m):{endedAt:m})},"finish"),d=setTimeout(()=>a(!0),http_capture_1.MAX_STREAM_MS);d.unref?.();const s=e.emit;e.emit=function(o,...m){try{return s.call(this,o,...m)}finally{try{if(o==="data"){const i=u?void 0:(0,http_capture_1.chunkBytes)(m[0],e.readableEncoding);i&&!t.push(i)&&a(!1)}else o==="end"?a(!1):(o==="aborted"||o==="error"||o==="close")&&a(!0)}catch{}}}}E(teeResponse,"teeResponse"),x(teeResponse,"teeResponse"),c(teeResponse,"teeResponse");function observe(e,t,r,u,a){const d=parseTarget(t,e),{host:s,method:o,path:m,hasQuery:i}=d;if(isInternal(s))return;const l=Date.now(),f=(0,flow_context_1.flowContext)(),n=(0,flow_context_1.liveFlowContext)();n&&(n.externalCallCount=(n.externalCallCount??0)+1);const T=u!=="off"?targetUrl(d):void 0,p=T?(0,http_store_1.newExchangeId)():void 0,w=p&&T&&o==="POST"?(0,ai_providers_1.matchTarget)(s,T.port,T.pathname,a)?.provider:void 0,b=!!w&&(0,tap_ai_1.aiCaptureMode)()!=="content"&&!(0,capture_file_1.aiBodiesAsked)(),C=p?teeRequest(r,u==="bodies",b):void 0,v=c(g=>({id:p,mode:u,method:o,url:T,path:clipPath(T.pathname),startedAt:l,ctx:f,request:C(),...w?{ai:w}:{},...b?{aiOpaque:b}:{},responseFields:[],...g}),"job");let $=!1;const h=c(g=>{if($)return;$=!0;const y=g?.statusCode,q=Date.now()-l,M=(0,emit_1.lensEmit)("external","info",`egress ${o} ${s} \u2192 ${y??"?"}`,{name:s,kind:"egress",method:o,host:s,path:m,...y!=null?{status:y}:{},...i?{hasQuery:!0}:{},startedAt:l,durationMs:q,...p?{exchangeId:p}:{}},fingerprintFor(s,y!=null&&y>=500),f);if(!p||!M||!g||typeof g.emit!="function")return;const A={...y!=null?{status:y}:{},...g.statusMessage?{statusText:g.statusMessage}:{},responseHeaders:(0,http_capture_1.headerPairs)(g.headers),ttfbMs:q};if(u!=="bodies"){trackExchange(()=>v(A));return}const R=[],S=new http_capture_1.ResponseSink(headerOf(g.headers,"content-type"),l,{ai:b,paths:R,encoding:headerOf(g.headers,"content-encoding")});teeResponse(g,S,I=>trackExchange(()=>v({...A,responseFields:R,reading:Promise.resolve(I())})))},"onResponse"),_=c(g=>{if($)return;$=!0;const y=String(g?.message??g),q=(0,emit_1.lensEmit)("external","error",`egress ${o} ${s} failed`,{name:s,kind:"egress",method:o,host:s,path:m,...i?{hasQuery:!0}:{},error:y,startedAt:l,durationMs:Date.now()-l,...p?{exchangeId:p}:{}},fingerprintFor(s,!0),f);p&&q&&trackExchange(()=>v({error:y}))},"onError"),F=r.emit;r.emit=function(g,...y){try{g==="response"?h(y[0]):g==="error"&&_(y[0])}catch{}return F.call(this,g,...y)}}E(observe,"observe"),x(observe,"observe"),c(observe,"observe");function targetUrl(e){try{const t=e.host.includes(":")&&!e.host.startsWith("[")?`[${e.host}]`:e.host,r=e.pathname.startsWith("/")?e.pathname:`/${e.pathname}`;return new URL(`${e.protocol}//${t}${e.port?`:${e.port}`:""}${r}${e.search}`)}catch{return}}E(targetUrl,"targetUrl"),x(targetUrl,"targetUrl"),c(targetUrl,"targetUrl");function parseTarget(e,t="http"){let r="",u="GET",a="/",d=!1,s=`${t}:`,o="",m="/",i="";const l=c((f,n)=>{d=n.length>1,a=capturePath(f,n),m=f||"/",i=n},"take");for(const f of e)if(typeof f=="string")try{const n=new URL(f);r=n.hostname,s=n.protocol,o=n.port,l(n.pathname,n.search)}catch{}else if(f instanceof URL)r=f.hostname,s=f.protocol,o=f.port,l(f.pathname,f.search);else if(f&&typeof f=="object"){const n=f;if(typeof n.method=="string"&&(u=n.method.toUpperCase()),typeof n.socketPath=="string")return{host:"",method:u,path:a,hasQuery:d,protocol:s,port:o,pathname:m,search:i};if(typeof n.path=="string"){const T=n.path,p=T.indexOf("?");l(p===-1?T:T.slice(0,p),p===-1?"":T.slice(p))}if(typeof n.protocol=="string"&&n.protocol&&(s=n.protocol),(typeof n.port=="number"||typeof n.port=="string"&&n.port)&&(o=String(n.port)),typeof n.hostname=="string"&&n.hostname)r=n.hostname;else if(typeof n.host=="string"&&n.host){const[T,p]=n.host.split(":");r=T??"",p&&!(typeof n.port=="number"||typeof n.port=="string")&&(o=p)}}return{host:r,method:u,path:a,hasQuery:d,protocol:s,port:o,pathname:m,search:i}}E(parseTarget,"parseTarget"),x(parseTarget,"parseTarget"),c(parseTarget,"parseTarget");
|
package/lib/tap-event-loop.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var v=Object.defineProperty;var s=(e,o)=>v(e,"name",{value:o,configurable:!0});var m=Object.defineProperty,r=s((e,o)=>m(e,"name",{value:o,configurable:!0}),"r"),a=Object.defineProperty,n=r((e,o)=>a(e,"name",{value:o,configurable:!0}),"n");Object.defineProperty(exports,"__esModule",{value:!0}),exports.tapEventLoop=tapEventLoop,exports.stopEventLoopTapForTests=stopEventLoopTapForTests;const node_perf_hooks_1=require("node:perf_hooks"),emit_1=require("./emit"),SAMPLE_MS=(()=>{const e=Number(process.env.LENSMCP_EVENT_LOOP_SAMPLE_MS);return Number.isFinite(e)&&e>=1e3?e:3e4})(),REPORT_ABOVE_MS=(()=>{const e=Number(process.env.LENSMCP_EVENT_LOOP_MIN_MS);return Number.isFinite(e)&&e>=0?e:50})(),RESOLUTION_MS=20;let timer,published=!1;function tapEventLoop(){if(process.env.LENSMCP_EVENT_LOOP==="0"||timer)return;const e=(0,node_perf_hooks_1.monitorEventLoopDelay)({resolution:RESOLUTION_MS});e.enable(),timer=setInterval(()=>{const o=RESOLUTION_MS,p=e.mean/1e6;if(!Number.isFinite(p))return;const i=Math.max(0,p-o),t=Math.max(0,e.max/1e6-o),u=Math.max(0,e.percentile(99)/1e6-o);e.reset(),!(published&&t<REPORT_ABOVE_MS)&&(published=!0,(0,emit_1.lensEmit)("perf",t>=1e3?"error":"info",`event loop lag ${i.toFixed(1)}ms mean / ${t.toFixed(1)}ms max`,{kind:"event-loop-lag",lagMs:Number(i.toFixed(2)),maxMs:Number(t.toFixed(2)),p99Ms:Number(u.toFixed(2)),windowMs:SAMPLE_MS},"perf:event-loop-lag",void 0,"performance"))},SAMPLE_MS),timer.unref?.()}s(tapEventLoop,"tapEventLoop"),r(tapEventLoop,"tapEventLoop"),n(tapEventLoop,"tapEventLoop");function stopEventLoopTapForTests(){timer&&clearInterval(timer),timer=void 0,published=!1}s(stopEventLoopTapForTests,"stopEventLoopTapForTests"),r(stopEventLoopTapForTests,"stopEventLoopTapForTests"),n(stopEventLoopTapForTests,"stopEventLoopTapForTests");
|
package/lib/tap-exec.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
/** `cp`: the module to patch — `node:child_process`; a spec hands its own, never the test process's. */
|
|
2
|
+
export declare function tapExec(cp?: Record<string, unknown>): void;
|
package/lib/tap-exec.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var y=Object.defineProperty;var g=(r,e)=>y(r,"name",{value:e,configurable:!0});var m=Object.defineProperty,p=g((r,e)=>m(r,"name",{value:e,configurable:!0}),"p"),f=Object.defineProperty,d=p((r,e)=>f(r,"name",{value:e,configurable:!0}),"d");Object.defineProperty(exports,"__esModule",{value:!0}),exports.tapExec=tapExec;const redact_1=require("@lensmcp/core/redact"),emit_1=require("./emit"),flow_context_1=require("./flow-context"),shown=d(r=>{const e=(0,redact_1.redactSecretText)(r.slice(0,4096));return e.length>200?`${e.slice(0,199)}\u2026`:e},"shown"),fmtCmd=d((r,e)=>{const t=Array.isArray(e)?e.map(String).slice(0,8).join(" "):"";return shown(`${String(r)}${t?" "+t:""}`)},"fmtCmd");function wrap(r,e,t){const n=r[e];if(typeof n!="function"||n.__lensmcp)return;const o=t(n);o.__lensmcp=!0,r[e]=o}g(wrap,"wrap"),p(wrap,"wrap"),d(wrap,"wrap");function observeChild(r,e,t,n){if(!t||typeof t.once!="function")return;const o=t;if(o.__lensmcpObserved)return;o.__lensmcpObserved=!0;const a=Date.now();t.once("exit",(...c)=>{const s=c[0],x=c[1];(0,emit_1.lensEmit)("exec",s===0?"info":x?"error":"warning",`exec ${e.split(" ")[0]} \u2192 ${s??x}`,{name:"exec",kind:"exec",cmd:e,via:r,...s!=null?{code:s}:{},...x?{signal:x}:{},durationMs:Date.now()-a,...t.pid?{pid:t.pid}:{}},void 0,n)}),t.once("error",(...c)=>{const s=c[0];(0,emit_1.lensEmit)("exec","error",`exec ${e.split(" ")[0]} failed to start`,{name:"exec",kind:"exec",cmd:e,via:r,error:(0,redact_1.clip)((0,redact_1.redactSecretText)(String(s?.message??s)),500),durationMs:Date.now()-a},void 0,n)})}g(observeChild,"observeChild"),p(observeChild,"observeChild"),d(observeChild,"observeChild");function tapExec(r=require("node:child_process")){for(const e of["spawn","fork"])wrap(r,e,t=>function(n,...o){const a=Array.isArray(o[0])?o[0]:[],c=(0,flow_context_1.flowContext)(),s=t.call(this,n,...o);try{observeChild(e,fmtCmd(n,a),s,c)}catch{}return s});wrap(r,"exec",e=>function(t,...n){const o=(0,flow_context_1.flowContext)(),a=e.call(this,t,...n);try{observeChild("exec",shown(String(t)),a,o)}catch{}return a}),wrap(r,"execFile",e=>function(t,...n){const o=Array.isArray(n[0])?n[0]:[],a=(0,flow_context_1.flowContext)(),c=e.call(this,t,...n);try{observeChild("execFile",fmtCmd(t,o),c,a)}catch{}return c});for(const e of["execSync","execFileSync","spawnSync"])wrap(r,e,t=>function(n,...o){const a=Array.isArray(o[0])?o[0]:[],c=e==="execSync"?shown(String(n)):fmtCmd(n,a),s=(0,flow_context_1.flowContext)(),x=Date.now();try{const l=t.call(this,n,...o),i=l&&typeof l=="object"?l:{},u=i.status??0;return i.error||i.signal?((0,emit_1.lensEmit)("exec","error",i.error?`exec ${c.split(" ")[0]} failed to start`:`exec ${c.split(" ")[0]} \u2192 ${i.signal}`,{name:"exec",kind:"exec",cmd:c,via:e,...i.error?{error:(0,redact_1.clip)((0,redact_1.redactSecretText)(String(i.error?.message??i.error)),500)}:{},...i.signal?{signal:i.signal}:{},durationMs:Date.now()-x,sync:!0},void 0,s),l):((0,emit_1.lensEmit)("exec",u===0?"info":"warning",`exec ${c.split(" ")[0]} \u2192 ${u}`,{name:"exec",kind:"exec",cmd:c,via:e,code:u,durationMs:Date.now()-x,sync:!0},void 0,s),l)}catch(l){const i=l,u=typeof i?.status=="number"&&!i.signal;throw(0,emit_1.lensEmit)("exec",u?"warning":"error",u?`exec ${c.split(" ")[0]} \u2192 ${i.status}`:`exec ${c.split(" ")[0]} failed`,{name:"exec",kind:"exec",cmd:c,via:e,...u?{code:i.status}:{error:(0,redact_1.clip)((0,redact_1.redactSecretText)(String(i?.message??l)),500)},...typeof i?.signal=="string"?{signal:i.signal}:{},durationMs:Date.now()-x,sync:!0},void 0,s),l}})}g(tapExec,"tapExec"),p(tapExec,"tapExec"),d(tapExec,"tapExec");
|
package/lib/tap-fs.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** Flows of a second that get an event of their own; the rest share one. */
|
|
2
|
+
export declare const OWN_EVENTS = 4;
|
|
1
3
|
export type FsOp = 'read' | 'write' | 'append';
|
|
2
4
|
/** One path in a window: its operation, how often and how many bytes, and the flow of its latest call. */
|
|
3
5
|
export interface FsPathOp {
|
|
@@ -18,20 +20,38 @@ export interface FsOpsPayload {
|
|
|
18
20
|
bytesWritten: number;
|
|
19
21
|
top: string[];
|
|
20
22
|
paths: FsPathOp[];
|
|
23
|
+
/** A flow's event: its first call of the second — where a flow view places it. */
|
|
24
|
+
startedAt?: number;
|
|
25
|
+
/** The shared event: how many flows' traffic went into it rather than an event of their own. */
|
|
26
|
+
merged?: number;
|
|
21
27
|
}
|
|
22
|
-
/** One
|
|
28
|
+
/** One event of a drained window: what to emit, and the flow and request it ran in (none for the shared one). */
|
|
29
|
+
export interface FsDrained {
|
|
30
|
+
ctx?: {
|
|
31
|
+
flowId: string;
|
|
32
|
+
requestId?: string;
|
|
33
|
+
};
|
|
34
|
+
payload: FsOpsPayload;
|
|
35
|
+
}
|
|
36
|
+
/** One second of fs traffic: counted as it comes, by flow and request, drained into at most five `fs-ops` events. */
|
|
23
37
|
export declare class FsWindow {
|
|
24
|
-
private
|
|
25
|
-
|
|
26
|
-
private
|
|
27
|
-
private bytesWritten;
|
|
28
|
-
private readonly counts;
|
|
29
|
-
private readonly ops;
|
|
38
|
+
private readonly slices;
|
|
39
|
+
/** Path entries across every slice — bounded as the one window was. */
|
|
40
|
+
private kept;
|
|
30
41
|
record(op: FsOp, path: string, bytes: number, at: number, ctx?: {
|
|
31
42
|
flowId?: string;
|
|
32
43
|
requestId?: string;
|
|
33
44
|
}, stream?: boolean): void;
|
|
34
|
-
/**
|
|
35
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The window as events — the {@link OWN_EVENTS} flows with the most traffic each its own (ties: the latest, then by
|
|
47
|
+
* id), the rest one shared event — and a fresh window. Nothing when it saw nothing. Their counts add up to the
|
|
48
|
+
* window's: every call is in exactly one.
|
|
49
|
+
*/
|
|
50
|
+
drain(): FsDrained[];
|
|
36
51
|
}
|
|
37
|
-
|
|
52
|
+
/** Tests: emit what the window holds now, as the 1-second timer would. */
|
|
53
|
+
export declare function flushFsWindowForTests(): void;
|
|
54
|
+
/** `fs`: the module to patch — `node:fs`; a spec hands its own, never the test process's. */
|
|
55
|
+
export declare function tapFs(fs?: Record<string, unknown> & {
|
|
56
|
+
promises?: Record<string, unknown>;
|
|
57
|
+
}): void;
|
package/lib/tap-fs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var y=Object.defineProperty;var u=(r,n)=>y(r,"name",{value:n,configurable:!0});var h=Object.defineProperty,c=u((r,n)=>h(r,"name",{value:n,configurable:!0}),"c"),d=Object.defineProperty,f=c((r,n)=>d(r,"name",{value:n,configurable:!0}),"f");Object.defineProperty(exports,"__esModule",{value:!0}),exports.FsWindow=exports.OWN_EVENTS=void 0,exports.flushFsWindowForTests=flushFsWindowForTests,exports.tapFs=tapFs;const tslib_1=require("tslib"),os=tslib_1.__importStar(require("node:os")),emit_1=require("./emit"),flow_context_1=require("./flow-context"),PATHS_KEPT=50,PATHS_SENT=20;exports.OWN_EVENTS=4;const SLICES_KEPT=64;class FsSlice{static{u(this,"FsSlice")}static{c(this,"FsSlice")}static{f(this,"FsSlice")}constructor(n,s){this.flowId=n,this.requestId=s,this.reads=0,this.writes=0,this.bytesRead=0,this.bytesWritten=0,this.first=1/0,this.last=0,this.counts=new Map,this.ops=new Map}absorb(n){this.reads+=n.reads,this.writes+=n.writes,this.bytesRead+=n.bytesRead,this.bytesWritten+=n.bytesWritten;for(const[s,e]of n.counts)this.counts.set(s,(this.counts.get(s)??0)+e);for(const[s,e]of n.ops)this.ops.set(s,e)}payload(n={}){const s=[...this.counts.entries()].sort((t,i)=>i[1]-t[1]).slice(0,3).map(([t])=>t),e=[...this.ops.values()].sort((t,i)=>i.at-t.at).slice(0,PATHS_SENT);return{reads:this.reads,writes:this.writes,bytesRead:this.bytesRead,bytesWritten:this.bytesWritten,top:s,paths:e,...n}}}class FsWindow{static{u(this,"FsWindow")}static{c(this,"FsWindow")}static{f(this,"FsWindow")}constructor(){this.slices=new Map,this.kept=0}record(n,s,e,t,i,o=!1){let l=i?.flowId?`${i.flowId}\0${i.requestId??""}`:"";l&&!this.slices.has(l)&&this.slices.size>=SLICES_KEPT&&(l="");let a=this.slices.get(l);a||(a=l?new FsSlice(i?.flowId,i?.requestId):new FsSlice,this.slices.set(l,a)),n==="read"?(a.reads+=1,a.bytesRead+=e):(a.writes+=1,a.bytesWritten+=e),a.first=Math.min(a.first,t),a.last=Math.max(a.last,t),(a.counts.size<PATHS_KEPT||a.counts.has(s))&&a.counts.set(s,(a.counts.get(s)??0)+1);const p=`${n}\0${s}\0${i?.flowId??""}`,w=a.ops.get(p);w?(w.n+=1,w.bytes+=e,w.at=t,i?.requestId&&(w.requestId=i.requestId)):this.kept<PATHS_KEPT&&(this.kept+=1,a.ops.set(p,{path:s,op:n,n:1,bytes:e,...o?{stream:!0}:{},at:t,...i?.flowId?{flowId:i.flowId}:{},...i?.requestId?{requestId:i.requestId}:{}}))}drain(){if(!this.slices.size)return[];const n=[...this.slices.entries()].filter(([o])=>o!=="").sort(([o,l],[a,p])=>p.reads+p.writes-(l.reads+l.writes)||p.last-l.last||(o<a?-1:1)),s=n.slice(0,exports.OWN_EVENTS),e=n.slice(exports.OWN_EVENTS),t=this.slices.get("")??new FsSlice;for(const[,o]of e)t.absorb(o);const i=s.map(([,o])=>({ctx:{flowId:o.flowId,...o.requestId?{requestId:o.requestId}:{}},payload:o.payload({startedAt:o.first})}));return t.reads+t.writes>0&&i.push({payload:t.payload(e.length?{merged:e.length}:{})}),this.slices.clear(),this.kept=0,i}}exports.FsWindow=FsWindow;const current=new FsWindow;let timer=null;const TMP=os.tmpdir(),CWD=process.cwd(),SKIP=["node_modules","/.nx/","/dist/","/.git/",".lensmcp"],CODE=/\.(?:[cm]?[jt]sx?|map|node)$/;let appending=0;function trackedPath(r){if((0,emit_1.isLensWriting)())return null;const n=typeof r=="string"?r:Buffer.isBuffer(r)?r.toString():r instanceof URL?r.pathname:null;return!n||n===emit_1.EVENT_FILE||n.startsWith(TMP)||SKIP.some(s=>n.includes(s))?null:n}u(trackedPath,"trackedPath"),c(trackedPath,"trackedPath"),f(trackedPath,"trackedPath");function record(r,n,s=0,e,t=!1){const i=trackedPath(n);if(!i||r==="read"&&CODE.test(i))return;const o=i.startsWith(CWD)?i.slice(CWD.length+1):i;current.record(r,o,s,Date.now(),e??(0,flow_context_1.flowContext)(),t),timer||(timer=setTimeout(flush,1e3),timer.unref?.())}u(record,"record"),c(record,"record"),f(record,"record");function flush(){timer=null;for(const{ctx:r,payload:n}of current.drain()){const s=n.paths.find(t=>t.op!=="read")??n.paths[0],e=r&&s?`fs ${s.op} ${s.path}${n.paths.length>1?` +${n.paths.length-1}`:""}`:`fs ${n.reads} reads \xB7 ${n.writes} writes`;(0,emit_1.lensEmit)("fs","info",e,{name:"fs",kind:"fs-ops",...n},`fs:${emit_1.PROJECT}`,r)}}u(flush,"flush"),c(flush,"flush"),f(flush,"flush");const byteLen=f(r=>typeof r=="string"?Buffer.byteLength(r):Buffer.isBuffer(r)?r.length:0,"byteLen");function wrap(r,n,s){const e=r?.[n];if(!r||typeof e!="function"||e.__lensmcp)return;const t=s(e);t.__lensmcp=!0;try{r[n]=t}catch{}}u(wrap,"wrap"),c(wrap,"wrap"),f(wrap,"wrap");function flushFsWindowForTests(){timer&&clearTimeout(timer),flush()}u(flushFsWindowForTests,"flushFsWindowForTests"),c(flushFsWindowForTests,"flushFsWindowForTests"),f(flushFsWindowForTests,"flushFsWindowForTests");function tapFs(r=require("node:fs")){wrap(r,"readFileSync",s=>function(e,...t){const i=s.call(this,e,...t);return record("read",e,byteLen(i)),i}),wrap(r,"writeFileSync",s=>function(e,t,...i){const o=appending>0,l=s.call(this,e,t,...i);return o||record("write",e,byteLen(t)),l}),wrap(r,"appendFileSync",s=>function(e,t,...i){appending++;let o;try{o=s.call(this,e,t,...i)}finally{appending--}return record("append",e,byteLen(t)),o}),wrap(r,"readFile",s=>function(e,...t){const i=t[t.length-1];if(typeof i=="function"){const o=(0,flow_context_1.flowContext)();t[t.length-1]=(l,a)=>{l||record("read",e,byteLen(a),o),i(l,a)}}return s.call(this,e,...t)});for(const s of["writeFile","appendFile"])wrap(r,s,e=>function(t,i,...o){const l=o[o.length-1],a=appending>0;if(typeof l=="function"){const p=(0,flow_context_1.flowContext)();o[o.length-1]=w=>{!w&&!a&&record(s==="appendFile"?"append":"write",t,byteLen(i),p),l(w)}}if(s!=="appendFile")return e.call(this,t,i,...o);appending++;try{return e.call(this,t,i,...o)}finally{appending--}});const n=r.promises;wrap(n,"readFile",s=>async function(e,...t){const i=(0,flow_context_1.flowContext)(),o=await s.call(this,e,...t);return record("read",e,byteLen(o),i),o});for(const s of["writeFile","appendFile"])wrap(n,s,e=>async function(t,i,...o){const l=(0,flow_context_1.flowContext)(),a=await e.call(this,t,i,...o);return record(s==="appendFile"?"append":"write",t,byteLen(i),l),a});wrap(r,"createReadStream",s=>function(e,...t){return record("read",e,0,(0,flow_context_1.flowContext)(),!0),s.call(this,e,...t)}),wrap(r,"createWriteStream",s=>function(e,...t){return record("write",e,0,(0,flow_context_1.flowContext)(),!0),s.call(this,e,...t)})}u(tapFs,"tapFs"),c(tapFs,"tapFs"),f(tapFs,"tapFs");
|
package/lib/tap-ingress.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var I=Object.defineProperty;var c=(s,e)=>I(s,"name",{value:e,configurable:!0});var q=Object.defineProperty,a=c((s,e)=>q(s,"name",{value:e,configurable:!0}),"a"),_=Object.defineProperty,d=a((s,e)=>_(s,"name",{value:e,configurable:!0}),"d");Object.defineProperty(exports,"__esModule",{value:!0}),exports.EXCHANGE_ID=void 0,exports.tapIngress=tapIngress,exports.resetIngressTapForTests=resetIngressTapForTests;const tslib_1=require("tslib"),http=tslib_1.__importStar(require("node:http")),zlib=tslib_1.__importStar(require("node:zlib")),headless_flow_1=require("@lensmcp/core/headless-flow"),emit_1=require("./emit"),http_store_1=require("./http-store");exports.EXCHANGE_ID=Symbol.for("lensmcp.exchangeId");const MAX_READ=2*1024*1024,MAX_PATH_CHARS=256;let originalEmit;function tapIngress(){if(originalEmit)return;const s=(0,http_store_1.httpCaptureMode)();if(s==="off")return;const e=http.Server.prototype,r=e.emit;originalEmit=r,e.emit=function(t,...i){if(t==="request")try{watch(i[0],i[1],s)}catch{}return r.call(this,t,...i)}}c(tapIngress,"tapIngress"),a(tapIngress,"tapIngress"),d(tapIngress,"tapIngress");function resetIngressTapForTests(){originalEmit&&(http.Server.prototype.emit=originalEmit),originalEmit=void 0}c(resetIngressTapForTests,"resetIngressTapForTests"),a(resetIngressTapForTests,"resetIngressTapForTests"),d(resetIngressTapForTests,"resetIngressTapForTests");const one=d(s=>s===void 0?void 0:Array.isArray(s)?s.join(", "):String(s),"one"),pairs=d(s=>Object.entries(s).flatMap(([e,r])=>r===void 0?[]:[[e,one(r)]]),"pairs");class Tee{static{c(this,"Tee")}static{a(this,"Tee")}static{d(this,"Tee")}constructor(){this.chunks=[],this.read=0,this.size=0}take(e,r){if(e==null||typeof e=="function")return;const t=Buffer.isBuffer(e)?e:e instanceof Uint8Array?Buffer.from(e):typeof e=="string"?Buffer.from(e,typeof r=="string"?r:"utf8"):void 0;t&&(this.size+=t.length,this.read<MAX_READ&&(this.chunks.push(t),this.read+=t.length))}bytes(){return Buffer.concat(this.chunks)}}function watch(s,e,r){if(!s||!e||s[exports.EXCHANGE_ID])return;const t=s.url??"/",i=t.indexOf("?"),l=i<0?t:t.slice(0,i);if((0,headless_flow_1.isProbePath)(l))return;const p=(0,http_store_1.newExchangeId)();s[exports.EXCHANGE_ID]=p;const b=Date.now(),g=r==="bodies",y=new Tee,u=new Tee;if(g){const m=s.emit;s.emit=function(o,...n){return o==="data"&&y.take(n[0]),m.call(this,o,...n)};const w=e.write,x=e.end;e.write=function(o,...n){return u.take(o,n[0]),w.call(this,o,...n)},e.end=function(o,...n){return u.take(o,n[0]),x.call(this,o,...n)}}let h=!1;const f=d(()=>{if(!h){h=!0;try{(0,http_store_1.writeHttpExchange)(exchangeOf(p,s,e,{url:t,q:i,path:l,startedAt:b,bodies:g,sent:y,answered:u}))}catch{}}},"finish");e.once("finish",f),e.once("close",f)}c(watch,"watch"),a(watch,"watch"),d(watch,"watch");function unpacked(s,e){try{if(s==="gzip"||s==="x-gzip")return zlib.gunzipSync(e,{maxOutputLength:MAX_READ});if(s==="br")return zlib.brotliDecompressSync(e,{maxOutputLength:MAX_READ});if(s==="deflate")return zlib.inflateSync(e,{maxOutputLength:MAX_READ})}catch{return}}c(unpacked,"unpacked"),a(unpacked,"unpacked"),d(unpacked,"unpacked");function exchangeOf(s,e,r,t){const i=[],l=[],p=[],b=(0,http_store_1.redactHeaders)(pairs(e.headers));i.push(...b.redacted);const g=(0,http_store_1.redactHeaders)(pairs(r.getHeaders()));i.push(...g.redacted);const y=t.bodies&&t.q>=0?(0,http_store_1.redactQuery)(t.url.slice(t.q)):[];let u;t.bodies&&t.sent.size&&(u=(0,http_store_1.keepBody)(one(e.headers["content-type"])??"",t.sent.bytes(),t.sent.size,l));let h;if(t.bodies&&t.answered.size){const n=one(r.getHeader("content-type"))??"",A=(one(r.getHeader("content-encoding"))??"").trim().toLowerCase();if(!A||A==="identity")h=(0,http_store_1.keepBody)(n,t.answered.bytes(),t.answered.size,p);else{const v=t.answered.size<=MAX_READ?unpacked(A,t.answered.bytes()):void 0;h=v?(0,http_store_1.keepBody)(n,v,v.length,p):{contentType:n.split(";")[0].trim().toLowerCase(),size:t.answered.size,kept:0,truncated:!1,omitted:"encoded"}}}const f=one(e.headers.host)??"localhost",m=t.path.length>MAX_PATH_CHARS?`${t.path.slice(0,MAX_PATH_CHARS)}\u2026`:t.path,w=one(e.headers["x-lensmcp-flow-id"]),x=one(e.headers["x-request-id"]),o=!r.writableFinished;return{schemaVersion:1,id:s,capturedAt:Date.now(),side:"ingress",service:emit_1.PROJECT,...w?{flowId:w}:{},...x?{requestId:x}:{},method:(e.method??"GET").toUpperCase(),url:`http://${f}${m}`,host:f.split(":")[0]??f,path:m,status:r.statusCode,...r.statusMessage?{statusText:r.statusMessage}:{},...o?{error:"the client went away before the answer ended"}:{},startedAt:t.startedAt,durationMs:Date.now()-t.startedAt,capture:t.bodies?"bodies":"headers",request:{headers:b.headers,...y.length?{query:y}:{},...u?{body:u}:{}},response:{headers:g.headers,...h?{body:h}:{}},redacted:{headers:[...new Set(i)],bodyFields:[...l.map(n=>`request.${n}`),...p.map(n=>`response.${n}`)]}}}c(exchangeOf,"exchangeOf"),a(exchangeOf,"exchangeOf"),d(exchangeOf,"exchangeOf");
|
package/lib/tap-nest-logger.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`,1)[0]??"";if(r.length<ECHO_MIN_CHARS)return;const n=r.slice(0,ECHO_NEEDLE_CHARS),
|
|
1
|
+
"use strict";var x=Object.defineProperty;var c=(e,t)=>x(e,"name",{value:t,configurable:!0});var T=Object.defineProperty,s=c((e,t)=>T(e,"name",{value:t,configurable:!0}),"s"),S=Object.defineProperty,o=s((e,t)=>S(e,"name",{value:t,configurable:!0}),"o");Object.defineProperty(exports,"__esModule",{value:!0}),exports.isLoggerEcho=isLoggerEcho,exports.resetNestLoggerTapForTests=resetNestLoggerTapForTests,exports.nestLoggerTap=nestLoggerTap;const redact_1=require("@lensmcp/core/redact"),flood_guard_1=require("./flood-guard"),emit_1=require("./emit"),flow_context_1=require("./flow-context"),LEVELS=["verbose","debug","log","warn","error","fatal"],SEVERITY={verbose:"debug",debug:"debug",log:"info",warn:"warning",error:"error",fatal:"fatal"},MAX_TEXT_CHARS=4e3,TITLE_CHARS=240,shapeGuard=(0,flood_guard_1.createEventFloodGuard)({burst:30,perSec:5}),podGuard=(0,flood_guard_1.createEventFloodGuard)({burst:400,perSec:100});let depth=0;const recent=[],ECHO_WINDOW_MS=2e3,ECHO_NEEDLE_CHARS=96,ECHO_MIN_CHARS=12,MAX_RECENT=64;function isLoggerEcho(e,t=Date.now()){for(let r=recent.length-1;r>=0;r--){const n=recent[r];if(t-n.at>ECHO_WINDOW_MS)return recent.splice(0,r+1),!1;if(e.includes(n.needle)||n.escaped!==void 0&&e.includes(n.escaped))return recent.splice(r,1),!0}return!1}c(isLoggerEcho,"isLoggerEcho"),s(isLoggerEcho,"isLoggerEcho"),o(isLoggerEcho,"isLoggerEcho");function expectEcho(e,t){const r=e.split(`
|
|
2
|
+
`,1)[0]??"";if(r.length<ECHO_MIN_CHARS)return;const n=r.slice(0,ECHO_NEEDLE_CHARS),i=JSON.stringify(n).slice(1,-1);recent.push({needle:n,at:t,...i!==n?{escaped:i}:{}}),recent.length>MAX_RECENT&&recent.splice(0,recent.length-MAX_RECENT)}c(expectEcho,"expectEcho"),s(expectEcho,"expectEcho"),o(expectEcho,"expectEcho");function resetNestLoggerTapForTests(){recent.length=0,depth=0}c(resetNestLoggerTapForTests,"resetNestLoggerTapForTests"),s(resetNestLoggerTapForTests,"resetNestLoggerTapForTests"),o(resetNestLoggerTapForTests,"resetNestLoggerTapForTests");function textOf(e){if(typeof e=="string")return e;if(e instanceof Error)return e.stack??`${e.name}: ${e.message}`;if(e===null||typeof e!="object")return String(e);try{return JSON.stringify(e)??String(e)}catch{return Object.prototype.toString.call(e)}}c(textOf,"textOf"),s(textOf,"textOf"),o(textOf,"textOf");function openingOf(e){return typeof e=="string"?e.split(`
|
|
3
3
|
`,1)[0]??"":e instanceof Error?`${e.name}: ${e.message}`.split(`
|
|
4
|
-
`,1)[0]??"":e===null||typeof e!="object"?String(e):"{object}"}s(openingOf,"openingOf"),o(openingOf,"openingOf");const looksLikeStack=o(e=>typeof e=="string"&&/\n\s+at\s/.test(e),"looksLikeStack");function shapeOf(e){return e.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,"#").replace(/\b[0-9a-f]{12,}\b/gi,"#").replace(/\d+/g,"#").slice(0,80)}s(shapeOf,"shapeOf"),o(shapeOf,"shapeOf");function capture(e,t,r,n){const
|
|
5
|
-
`:" ")+
|
|
6
|
-
`,1)[0]??"",
|
|
4
|
+
`,1)[0]??"":e===null||typeof e!="object"?String(e):"{object}"}c(openingOf,"openingOf"),s(openingOf,"openingOf"),o(openingOf,"openingOf");const looksLikeStack=o(e=>typeof e=="string"&&/\n\s+at\s/.test(e),"looksLikeStack");function shapeOf(e){return e.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi,"#").replace(/\b[0-9a-f]{12,}\b/gi,"#").replace(/\d+/g,"#").slice(0,80)}c(shapeOf,"shapeOf"),s(shapeOf,"shapeOf"),o(shapeOf,"shapeOf");function capture(e,t,r,n){const i=(0,flow_context_1.flowContext)();if(!i?.flowId&&!i?.requestId)return;const f=t?.context,a=n.slice();let g=typeof f=="string"&&f?f:void 0;!g&&a.length>0&&typeof a[a.length-1]=="string"&&!looksLikeStack(a[a.length-1])&&(g=a.pop());const E=openingOf(r),L=`${g??""}:${shapeOf((0,redact_1.redactText)(E.slice(0,MAX_TEXT_CHARS*2)))}`,_=Date.now(),d=shapeGuard.admit(L),y=d.ok?podGuard.admit("pod"):d;if(!d.ok||!y.ok){expectEcho(E,_);return}const b=[r,...a].filter(p=>p!==void 0).map(textOf);let h="";for(const p of b)h+=h===""?p:(looksLikeStack(p)?`
|
|
5
|
+
`:" ")+p;const l=(0,redact_1.redactText)(h.slice(0,MAX_TEXT_CHARS*2)).slice(0,MAX_TEXT_CHARS);if(!l)return;const u=l.split(`
|
|
6
|
+
`,1)[0]??"",O=looksLikeStack(l);(0,emit_1.lensLog)(SEVERITY[e],u.length>TITLE_CHARS?`${u.slice(0,TITLE_CHARS)}\u2026`:u,{...O?{stack:l}:l!==u||u.length>TITLE_CHARS?{text:l}:{},stream:"logger",level:e,...g?{logger:g}:{},bucket:L,suppressed:d.suppressed+y.suppressed});for(const p of b)expectEcho(p,_)}c(capture,"capture"),s(capture,"capture"),o(capture,"capture");function wrap(e,t){const r=e[t];if(typeof r!="function"||r.__lensmcpLog)return;const n=o(function(i,...f){if(depth===0)try{capture(t,this,i,f)}catch{}depth+=1;try{return r.call(this,i,...f)}finally{depth-=1}},"wrapped");n.__lensmcpLog=!0,e[t]=n}c(wrap,"wrap"),s(wrap,"wrap"),o(wrap,"wrap");function nestLoggerTap(e){if(process.env.LENSMCP_LOGS==="0")return!0;const t=e?.Logger;if(typeof t!="function"||!t.prototype)return!1;for(const r of LEVELS)wrap(t.prototype,r),wrap(t,r);return!0}c(nestLoggerTap,"nestLoggerTap"),s(nestLoggerTap,"nestLoggerTap"),o(nestLoggerTap,"nestLoggerTap");
|
package/lib/tap-nest.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var y=Object.defineProperty;var p=(e,t)=>y(e,"name",{value:t,configurable:!0});var d=Object.defineProperty,c=p((e,t)=>d(e,"name",{value:t,configurable:!0}),"c"),f=Object.defineProperty,s=c((e,t)=>f(e,"name",{value:t,configurable:!0}),"s");Object.defineProperty(exports,"__esModule",{value:!0}),exports.installChildAppBridge=installChildAppBridge,exports.nestTap=nestTap;const emit_1=require("./emit"),WRAPPED=Symbol.for("lensmcp.wrappedRoot"),captures=[],waiters=[];function pushCapture(e){const t=waiters.shift();t?(clearTimeout(t.timer),t.resolve(e)):captures.push(e)}p(pushCapture,"pushCapture"),c(pushCapture,"pushCapture"),s(pushCapture,"pushCapture");function nextCapture(e=6e4){const t=captures.shift();return t?Promise.resolve(t):new Promise((n,o)=>{const i={resolve:n,reject:o,timer:setTimeout(()=>{const r=waiters.indexOf(i);r>=0&&waiters.splice(r,1),o(new Error("[lensmcp] no NestJS app captured within 60s \u2014 does main.ts call app.listen()? (zero-touch pods boot by running your main and intercepting listen)"))},e)};i.timer.unref?.(),waiters.push(i)})}p(nextCapture,"nextCapture"),c(nextCapture,"nextCapture"),s(nextCapture,"nextCapture");function installChildAppBridge(){if(process.env.APP_RUNNER!=="1")return;const e=global,t=s(()=>nextCapture(),"factory");e.createChildApp=t,e.__lensmcpCreateChildApp=t}p(installChildAppBridge,"installChildAppBridge"),c(installChildAppBridge,"installChildAppBridge"),s(installChildAppBridge,"installChildAppBridge");function nestTap(e){const t=e,n=t?.NestFactory,o=n?.create;if(!n||typeof o!="function")return!1;if(o.__lensmcp)return!0;const i=s(async function(r,...l){let a=r;const u=r,m=!!(u?.[WRAPPED]||u?.module?.[WRAPPED]);if(process.env.LENSMCP_NEST!=="0"&&!m)try{a=await buildLensmcpRoot(r)}catch{a=r}return o.call(this??n,a,...l)},"wrapped");if(i.__lensmcp=!0,n.create=i,process.env.APP_RUNNER==="1"){const r=t?.NestApplication?.prototype,l=r?.listen;if(r&&typeof l=="function"&&!l.__lensmcp){const a=s(async function(u,...m){return captureApp(this,u).catch(h=>(console.error("[lensmcp] pod capture failed, falling back to TCP listen:",h),l.call(this,u,...m)))},"listenWrapped");a.__lensmcp=!0,r.listen=a}}return!0}p(nestTap,"nestTap"),c(nestTap,"nestTap"),s(nestTap,"nestTap");async function buildLensmcpRoot(e){const t=await import("@lensmcp/nest-instrumentation"),n=require("@nestjs/common"),o=t.LensmcpModule.forRoot({projectName:emit_1.PROJECT,trace:{autoInstrumentMethods:!0},memory:{mode:"light",scope:"all"}});class i{static{p(this,"i")}static{c(this,"n")}static{s(this,"LensmcpZeroTouchRoot")}}return i[WRAPPED]=!0,n.Module({imports:[o,e]})(i),i}p(buildLensmcpRoot,"buildLensmcpRoot"),c(buildLensmcpRoot,"buildLensmcpRoot"),s(buildLensmcpRoot,"buildLensmcpRoot");async function captureApp(e,t){await e.init();let n=e.getHttpAdapter?.()?.getInstance?.();if(typeof n!="function"){const o=n;o?.ready&&await o.ready(),typeof o?.routing=="function"&&(n=s((i,r)=>o.routing(i,r),"handler"))}if(typeof n!="function")throw new Error("[lensmcp] cannot derive a request handler from the HTTP adapter");return pushCapture({handler:n,serviceConfig:{port:t??process.env.PORT??0},close:s(()=>Promise.resolve(e.close()).then(()=>{}),"close"),app:e}),e.getHttpServer?.()}p(captureApp,"captureApp"),c(captureApp,"captureApp"),s(captureApp,"captureApp");
|
package/lib/tap-pg-pool.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`).slice(1).map(r=>r.trim()).filter(r=>r.startsWith("at ")&&!/tap-pg-pool\.[cm]?[jt]s:/.test(r)),s=o.filter(r=>!r.includes("node_modules")&&!r.includes("node:"));return(s.length?s:o).slice(0,SITE_FRAMES)}h(siteOf,"siteOf"),p(siteOf,"siteOf");const secs=p(e=>e>=1e4?`${Math.round(e/1e3)} s`:e>=1e3?`${(e/1e3).toFixed(1)} s`:`${Math.round(e)} ms`,"secs");function failed(e,o,s,r,t,n){o.failures+=1;const i=(0,pg_errors_1.pgConnectReason)(s),u=counters(e),d=n?n.total>=n.max&&n.idle===0:u.total>=u.max&&u.idle===0,l=e.options?.connectionTimeoutMillis??0,
|
|
1
|
+
"use strict";var _=Object.defineProperty;var f=(e,o)=>_(e,"name",{value:o,configurable:!0});var k=Object.defineProperty,h=f((e,o)=>k(e,"name",{value:o,configurable:!0}),"h"),$=Object.defineProperty,p=h((e,o)=>$(e,"name",{value:o,configurable:!0}),"p");Object.defineProperty(exports,"__esModule",{value:!0}),exports.checkoutSite=void 0,exports.pgPoolTap=pgPoolTap,exports.samplePgPoolsForTests=samplePgPoolsForTests,exports.resetPgPoolTapForTests=resetPgPoolTapForTests;const node_async_hooks_1=require("node:async_hooks"),pg_errors_1=require("@lensmcp/core/pg-errors"),active_work_1=require("./active-work"),emit_1=require("./emit"),flow_context_1=require("./flow-context"),envMs=p((e,o,s)=>{const r=Number(process.env[e]);return Number.isFinite(r)&&r>=s?r:o},"envMs"),SAMPLE_MS=envMs("LENSMCP_PG_POOL_SAMPLE_MS",1e4,1e3),HOLD_WARN_MS=envMs("LENSMCP_PG_HOLD_WARN_MS",3e4,1e3),SLOW_WAIT_MS=250,BLOCKED_MIN_MS=1e3,WAITS_KEPT=256,SITE_FRAMES=8,stats=new WeakMap,live=[];let nextId=1,timer;const counters=p(e=>({total:e.totalCount??0,idle:e.idleCount??0,waiting:e.waitingCount??0,max:e.options?.max??10}),"counters");function whereOf(e){const o=e.options??{};let s=typeof o.host=="string"?o.host:void 0,r=typeof o.port=="number"||typeof o.port=="string"?String(o.port):void 0,t=typeof o.database=="string"?o.database:void 0;if(typeof o.connectionString=="string")try{const n=new URL(o.connectionString);s??=n.hostname||void 0,r??=n.port||void 0,t??=decodeURIComponent(n.pathname.replace(/^\//,""))||void 0}catch{}return{...t?{database:t}:{},...s?{server:`${s}:${r??5432}`}:{}}}f(whereOf,"whereOf"),h(whereOf,"whereOf"),p(whereOf,"whereOf");function statsOf(e){let o=stats.get(e);return o||(o={id:nextId++,gen:(0,active_work_1.activeWorkGeneration)(),ref:new WeakRef(e),createdAt:Date.now(),where:whereOf(e),out:new Map,acquires:0,waits:[],waitMax:0,peakWaiting:0,peakTotal:0,failures:0,holdMax:0},stats.set(e,o),live.push(o),startSampling()),o}f(statsOf,"statsOf"),h(statsOf,"statsOf"),p(statsOf,"statsOf");function siteOf(e){const o=(e.stack??"").split(`
|
|
2
|
+
`).slice(1).map(r=>r.trim()).filter(r=>r.startsWith("at ")&&!/tap-pg-pool\.[cm]?[jt]s:/.test(r)),s=o.filter(r=>!r.includes("node_modules")&&!r.includes("node:"));return(s.length?s:o).slice(0,SITE_FRAMES)}f(siteOf,"siteOf"),h(siteOf,"siteOf"),p(siteOf,"siteOf");const secs=p(e=>e>=1e4?`${Math.round(e/1e3)} s`:e>=1e3?`${(e/1e3).toFixed(1)} s`:`${Math.round(e)} ms`,"secs");function failed(e,o,s,r,t,n){o.failures+=1;const i=(0,pg_errors_1.pgConnectReason)(s),u=counters(e),d=n?n.total>=n.max&&n.idle===0:u.total>=u.max&&u.idle===0,l=e.options?.connectionTimeoutMillis??0,g=i==="pool-wait-timeout"||i==="connect-timeout",c=g&&l>0?Math.max(0,r-l):0,a=c>=Math.max(BLOCKED_MIN_MS,l*.2),w=`${u.total}/${u.max} open, ${u.idle} idle, ${u.waiting} waiting`,m=n?`when it asked ${n.total}/${n.max} open, ${n.idle} idle, ${n.waiting} waiting; now ${w}`:w,x=i==="pool-wait-timeout"?a?`no connection in time \u2014 waited ${secs(r)} on a ${secs(l)} limit: the process was blocked ${secs(c)} (${m})`:d?`pool full \u2014 every connection in use, none free within ${secs(l)} (${m})`:`no connection in time \u2014 the pool had room when it asked, yet the ${secs(l)} wait ran out: the process was busy or the pool's queue stalled (${m})`:i==="connect-timeout"?`opening a connection took over ${secs(l)}${a?` \u2014 the process was blocked ${secs(c)}`:""} (${w})`:i?`could not get a connection: ${pg_errors_1.PG_CONNECT_MEANING[i]}`:`could not get a connection (${secs(r)})`,M=s;(0,emit_1.lensEmitOutcome)("db","error",x,{name:"postgres",kind:"db-pool-error",...i?{reason:i}:{},waitedMs:r,...l?{timeoutMs:l}:{},...g?{lateMs:c}:{},...a?{blockedMs:c}:{},...u,...n?{asked:n}:{},fullWhenAsked:d,pool:o.id,generation:o.gen,serving:(0,active_work_1.activeWorkGeneration)(),pools:live.length,...o.where,error:String(M?.message??s),...typeof M?.code=="string"?{code:M.code}:{}},`db:pool-error:${i??"other"}`,t)}f(failed,"failed"),h(failed,"failed"),p(failed,"failed");function checkedOut(e,o,s,r,t,n){if(e.waits.push(n),e.waits.length>WAITS_KEPT&&e.waits.splice(0,e.waits.length-WAITS_KEPT),n>e.waitMax&&(e.waitMax=n),!o||typeof o!="object")return s;const i=o;e.out.set(i,{at:Date.now(),site:r,ctx:t});const u=p(c=>{if(!c)return;e.out.delete(i);const a=Date.now()-c.at;a>e.holdMax&&(e.holdMax=a)},"back"),d=o.release,l=p(c=>typeof c=="function"?function(...a){return u(e.out.get(i)),c.apply(this??o,a)}:c,"wrap"),g=l(d);return g&&(o.release=g),s===d?g:l(s)}f(checkedOut,"checkedOut"),h(checkedOut,"checkedOut"),p(checkedOut,"checkedOut");function percentile(e,o){if(!e.length)return 0;const s=e.slice().sort((r,t)=>r-t);return s[Math.min(s.length-1,Math.floor(s.length*o))]}f(percentile,"percentile"),h(percentile,"percentile"),p(percentile,"percentile");function sample(e=Date.now()){for(let t=live.length-1;t>=0;t--){const n=live[t],i=n.ref.deref();(!i||i.ended&&n.out.size===0)&&live.splice(t,1)}const o=(0,active_work_1.activeWorkGeneration)(),s=live.reduce((t,n)=>Math.max(t,n.gen),0),r=live.map(t=>{const n=t.ref.deref(),i=counters(n);return{pool:t.id,generation:t.gen,total:i.total,idle:i.idle,waiting:i.waiting,max:i.max,...n.ending?{ending:!0}:{}}});for(const t of live){const n=t.ref.deref();if(!n)continue;const i=[];let u=0;for(const c of t.out.values()){const a=e-c.at;if(a>u&&(u=a),a<HOLD_WARN_MS||c.warned)continue;c.warned=!0;const w=siteOf(c.site);i.push({heldMs:a,site:w}),(0,emit_1.lensEmitOutcome)("db","info",`a connection held ${secs(a)} and not given back${w[0]?` \u2014 taken ${w[0].replace(/^at /,"at ")}`:""}`,{name:"postgres",kind:"db-pool-hold",heldMs:a,site:w,pool:t.id,generation:t.gen,...counters(n),...t.where},"db:pool-hold",c.ctx)}const d=counters(n),l=t.gen<s&&d.total>0,g=t.peakWaiting>0||t.waitMax>=SLOW_WAIT_MS||t.failures>0||i.length>0;(g||l)&&(0,emit_1.lensEmit)("db","info",l&&!g?`pool of generation ${t.gen} still holds ${d.total} connection(s) \u2014 the app serving now is generation ${o}`:`pool ${d.total}/${d.max} open \xB7 ${t.acquires} checkouts \xB7 wait p95 ${secs(percentile(t.waits,.95))} max ${secs(t.waitMax)}${t.peakWaiting?` \xB7 up to ${t.peakWaiting} waiting`:""}${t.failures?` \xB7 ${t.failures} failed`:""}`,{name:"postgres",kind:"db-pool",pool:t.id,generation:t.gen,serving:o,...d,acquires:t.acquires,waitP95Ms:percentile(t.waits,.95),waitMaxMs:t.waitMax,peakWaiting:t.peakWaiting,peakTotal:t.peakTotal,failures:t.failures,holdMaxMs:t.holdMax,out:t.out.size,longestHeldMs:u,...l?{leftover:!0}:{},windowMs:SAMPLE_MS,pools:r,...t.where},"db:pool",void 0),t.acquires=0,t.waits=[],t.waitMax=0,t.failures=0,t.holdMax=0,t.peakWaiting=d.waiting,t.peakTotal=d.total}}f(sample,"sample"),h(sample,"sample"),p(sample,"sample");function startSampling(){timer||(timer=setInterval(()=>{try{sample()}catch{}},SAMPLE_MS),timer.unref?.())}f(startSampling,"startSampling"),h(startSampling,"startSampling"),p(startSampling,"startSampling");const checkoutSites=new node_async_hooks_1.AsyncLocalStorage,checkoutSite=p(()=>checkoutSites.getStore(),"checkoutSite");exports.checkoutSite=checkoutSite;function pgPoolTap(e){let o=e?.Pool?.prototype??null;for(;o&&!Object.prototype.hasOwnProperty.call(o,"connect");)o=Object.getPrototypeOf(o);const s=o?.connect;if(!o||typeof s!="function")return!1;if(s.__lensmcp)return!0;const r=p(function(t){let n;try{n=statsOf(this)}catch{return s.call(this,t)}const i=Date.now(),u=(0,flow_context_1.flowContext)();let d;try{d=counters(this)}catch{}const l=new Error("pg checkout");n.acquires+=1;const g=p(()=>{try{const a=counters(this);a.waiting>n.peakWaiting&&(n.peakWaiting=a.waiting),a.total>n.peakTotal&&(n.peakTotal=a.total)}catch{}},"note");if(typeof t=="function"){const a=s.call(this,(w,m,x)=>{let M=x;try{w?failed(this,n,w,Date.now()-i,u,d):M=checkedOut(n,m,x,l,u,Date.now()-i)}catch{}checkoutSites.run(l,()=>t(w,m,M))});return g(),a}const c=s.call(this);return g(),!c||typeof c.then!="function"?c:c.then(a=>{try{checkedOut(n,a,a?.release,l,u,Date.now()-i)}catch{}return a},a=>{try{failed(this,n,a,Date.now()-i,u,d)}catch{}throw a})},"wrapped");return r.__lensmcp=!0,o.connect=r,!0}f(pgPoolTap,"pgPoolTap"),h(pgPoolTap,"pgPoolTap"),p(pgPoolTap,"pgPoolTap");function samplePgPoolsForTests(e){sample(e)}f(samplePgPoolsForTests,"samplePgPoolsForTests"),h(samplePgPoolsForTests,"samplePgPoolsForTests"),p(samplePgPoolsForTests,"samplePgPoolsForTests");function resetPgPoolTapForTests(){timer&&clearInterval(timer),timer=void 0,live.splice(0,live.length),nextId=1}f(resetPgPoolTapForTests,"resetPgPoolTapForTests"),h(resetPgPoolTapForTests,"resetPgPoolTapForTests"),p(resetPgPoolTapForTests,"resetPgPoolTapForTests");
|