publishport-opencli 1.0.64 → 1.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,12 @@
1
- import{existsSync as ue,mkdirSync as le,readFileSync as de,writeFileSync as fe}from"node:fs";import{homedir as pe}from"node:os";import{dirname as we,join as H}from"node:path";import{cli as me,Strategy as he}from"@jackwener/opencli/registry";import{AuthRequiredError as U,CommandExecutionError as V}from"@jackwener/opencli/errors";const q=1000,Q=300,X=60,M=30,Se=600,Y=30000,Z=2000,ve=20;function ye(){return H(pe(),".ppcli","watch")}function ee(e,r){const t=String(process.env.PPCLI_ACTIVE_PROFILE??"").trim();return H(ye(),`${e}${t?`@${t}`:""}.${r}.seen.json`)}function _e(e,r){const t=ee(e,r);try{if(!ue(t))return{ids:[],set:new Set,lastTs:0,firstRun:!0};const i=JSON.parse(de(t,"utf-8")),a=Array.isArray(i?.ids)?i.ids.map(String):[],c=Number.isFinite(i?.lastTs)?Number(i.lastTs):0;return{ids:a,set:new Set(a),lastTs:c,firstRun:c<=0}}catch{return{ids:[],set:new Set,lastTs:0,firstRun:!0}}}function C(e,r,t){const i=ee(e,r);try{le(we(i),{recursive:!0});fe(i,JSON.stringify({ids:t.ids.slice(-Z),lastTs:t.lastTs||0}),"utf-8")}catch{}}function Ne(e,r){if(e.set.has(r))return!1;e.set.add(r);e.ids.push(r);while(e.ids.length>Z){const t=e.ids.shift();e.set.delete(t)}return!0}function F(e){process.stdout.write(`${JSON.stringify(e)}
1
+ import{existsSync as ue,mkdirSync as le,readFileSync as de,writeFileSync as fe}from"node:fs";import{homedir as pe}from"node:os";import{dirname as we,join as H}from"node:path";import{cli as me,Strategy as he}from"@jackwener/opencli/registry";import{AuthRequiredError as q,CommandExecutionError as V}from"@jackwener/opencli/errors";const U=1000,Q=300,X=60,M=30,Se=600,Y=30000,Z=2000,ve=20;function ye(){return H(pe(),".ppcli","watch")}function ee(e,r){const t=String(process.env.PPCLI_ACTIVE_PROFILE??"").trim();return H(ye(),`${e}${t?`@${t}`:""}.${r}.seen.json`)}function _e(e,r){const t=ee(e,r);try{if(!ue(t))return{ids:[],set:new Set,lastTs:0,firstRun:!0};const i=JSON.parse(de(t,"utf-8")),a=Array.isArray(i?.ids)?i.ids.map(String):[],c=Number.isFinite(i?.lastTs)?Number(i.lastTs):0;return{ids:a,set:new Set(a),lastTs:c,firstRun:c<=0}}catch{return{ids:[],set:new Set,lastTs:0,firstRun:!0}}}function C(e,r,t){const i=ee(e,r);try{le(we(i),{recursive:!0});fe(i,JSON.stringify({ids:t.ids.slice(-Z),lastTs:t.lastTs||0}),"utf-8")}catch{}}function Ne(e,r){if(e.set.has(r))return!1;e.set.add(r);e.ids.push(r);while(e.ids.length>Z){const t=e.ids.shift();e.set.delete(t)}return!0}function F(e){process.stdout.write(`${JSON.stringify(e)}
2
2
  `)}function d(e,r,t={}){F({type:"status",site:e,state:r,ts:Date.now(),...t})}function te(e,r){if(!r||typeof r!=="object")return null;const t=r.id==null?"":String(r.id);if(!t)return null;const i=r.sender&&typeof r.sender==="object"?{id:r.sender.id==null?"":String(r.sender.id),name:String(r.sender.name??"")}:{id:"",name:String(r.sender??"")};return{type:"event",site:e,event:String(r.event??"message"),id:t,ts:Number.isFinite(r.ts)?Number(r.ts):Date.now(),conversation_id:r.conversation_id==null?"":String(r.conversation_id),sender:i,text:String(r.text??""),url:r.url==null?"":String(r.url),raw:r.raw??null}}function re(e){if(!e)return[];return Array.isArray(e)?e.filter(Boolean):[e]}function ne(e){try{return new URL(e).origin}catch{return""}}function be(e,r){const t=(i)=>{try{const a=new URL(i);if(a.protocol==="https:"&&a.port==="443"||a.protocol==="http:"&&a.port==="80")a.port="";return`${a.protocol}//${a.host}${a.pathname==="/"?"":a.pathname}${a.search}${a.hash}`}catch{return String(i??"")}};return Boolean(e)&&t(e)===t(r)}async function Ee(e,r){if(typeof e.getCurrentUrl!=="function")return!1;try{if(!be(await e.getCurrentUrl(),r))return!1;await e.evaluate("(function(){ window.__pp_watch_doc = 1; return 1; })()");return!0}catch{return!1}}async function Te(e){try{return await e.evaluate("(function(){ return window.__pp_watch_doc === 1; })()")===!0}catch{return!1}}async function ae(e){await e.evaluate("(function(){ window.__pp_watch_doc = 1; setTimeout(function(){ location.reload(); }, 0); return 1; })()");const r=Date.now()+Y;while(Date.now()<r){await new Promise((t)=>setTimeout(t,500));try{if(await e.evaluate('(function(){ return window.__pp_watch_doc === 1 ? "old" : document.readyState; })()')==="complete")return}catch{}}throw new V(`监听页 ${Y/1000} 秒内没有重新加载完`)}function ie(){return`(function(){
3
3
  if (window.__pp_timers) return;
4
+ if (window.top !== window || !/^https?:$/.test(location.protocol)) return;
4
5
  window.__pp_timers = 'native';
5
6
  var worker;
6
7
  try {
7
8
  var src = "var t={};onmessage=function(e){var d=e.data;if(d[0]==='s'){t[d[1]]=(d[3]?setInterval:setTimeout)(function(){postMessage(d[1]);},d[2]);}else{clearTimeout(t[d[1]]);clearInterval(t[d[1]]);delete t[d[1]];}};";
8
- worker = new Worker(URL.createObjectURL(new Blob([src], { type: 'text/javascript' })));
9
+ worker = new Worker('data:text/javascript,' + encodeURIComponent(src));
9
10
  } catch (e) { window.__pp_timers = 'worker-failed: ' + e; return; }
10
11
  var nST = window.setTimeout, nCT = window.clearTimeout, nSI = window.setInterval, nCI = window.clearInterval;
11
12
  var seq = 1000000000, cbs = {};
@@ -96,4 +97,4 @@ import{existsSync as ue,mkdirSync as le,readFileSync as de,writeFileSync as fe}f
96
97
  WrappedES.prototype = NativeES.prototype;
97
98
  window.EventSource = WrappedES;
98
99
  }
99
- })()`}async function Ie(e){let r;try{r=await e.evaluate("(function(){ var o = window.__pp_stream || []; window.__pp_stream = []; return JSON.stringify(o); })()")}catch{return[]}let t;try{t=JSON.parse(typeof r==="string"?r:"[]")}catch{return[]}if(!Array.isArray(t))return[];return t.map((i)=>({kind:"page-hook",channel:i.channel,direction:i.direction,url:i.url,method:i.channel==="websocket"?"WS":"SSE",eventName:i.eventName||void 0,responseStatus:200,responseContentType:i.binary?"application/octet-stream":"text/plain",responsePreview:i.binary?"":String(i.data??""),timestamp:i.ts||Date.now()}))}export function registerWatch(e){if(!e?.site||!e?.domain||!e?.watchUrl||typeof e.parse!=="function")throw Error("registerWatch 需要 site、domain、watchUrl 和 parse");const r=e.name??"watch",t=e.site,i=ne(e.watchUrl);me({site:t,name:r,access:"read",description:e.description??`长驻监听${t}的新消息,旁听页面自身的实时通道并按 NDJSON 逐条输出(不对平台发轮询请求)`,domain:e.domain,strategy:he.COOKIE,browser:!0,navigateBefore:!1,sessionKey:e.sessionKey??"watch",siteSession:"persistent",defaultWindowMode:"background",args:[{name:"interval",type:"int",required:!1,default:q,help:`本机取帧间隔毫秒(默认 ${q})。纯本机 IPC,不发平台请求`},{name:"reconcile-interval",type:"int",required:!1,default:0,help:`对账间隔秒(0 = 用该平台的默认值:${Number(e.reconcileSeconds)>0?`本平台 ${Number(e.reconcileSeconds)} 秒`:`可旁听实时通道的平台 ${Q} 秒兜底,只能靠拉取的平台 ${X} 秒一轮`});-1 = 彻底关闭对账`},{name:"quiet-seconds",type:"int",required:!1,default:0,help:`实时通道这么多秒一帧都没收到就重载监听页重连(0 = 用该平台的默认值${Number(e.quietSeconds)>0?`:本平台 ${Number(e.quietSeconds)} 秒`:";本平台默认不查"})`},{name:"heartbeat",type:"int",required:!1,default:M,help:`心跳间隔秒(默认 ${M}),无事件时输出一行 status 表示还活着`},{name:"events",type:"str",required:!1,help:"只输出这些事件类型,逗号分隔(如 dm,comment);默认全部"},{name:"once",type:"bool",required:!1,default:!1,help:"只跑一轮对账后退出,不长驻。用于验证解析器和补历史"},{name:"replay-seen",type:"bool",required:!1,default:!1,help:"忽略去重表,把解析到的事件全部重新输出(调试用,会导致重复触发)"},{name:"backfill",type:"bool",required:!1,default:!1,help:"首次运行时把已存在的历史消息也当成新事件输出(默认只静默登记,不触发)"},{name:"dump-frames",type:"bool",required:!1,default:!1,help:"侦察模式:把收到的每一条原始帧(url / 通道 / 内容片段)也打出来,用来给新平台摸清事件通道"},{name:"include-own",type:"bool",required:!1,default:!1,help:"连自己发出去的消息也当成事件(默认不算——否则自动回复会追着自己的回复自我触发)。调试解析用"}],columns:["event","id","sender","text","ts"],func:async(a,c)=>{const oe=Math.max(200,Number(c.interval??q)),x=e.mode==="reconcile"?"reconcile":"stream",I=Number(c["reconcile-interval"]??0),D=I<0?0:I>0?I:Number(e.reconcileSeconds)>0?Number(e.reconcileSeconds):x==="reconcile"?X:Q,B=Math.max(0,Number(c.heartbeat??M)),J=c.once===!0||c.once==="true",S=c["replay-seen"]===!0||c["replay-seen"]==="true",O=c.backfill===!0||c.backfill==="true",se=c["dump-frames"]===!0||c["dump-frames"]==="true",v=String(c.events??"").split(",").map((u)=>u.trim()).filter(Boolean);if(typeof a.startNetworkCapture!=="function"||typeof a.readNetworkCapture!=="function")throw new V("当前浏览器通道不支持网络捕获,watch 无法工作(需要 CDP 或扩展路径)");const f=_e(t,r),g={site:t,page:a,state:{},includeOwn:c["include-own"]===!0||c["include-own"]==="true"};let w=0,A=0,y=!1;const W=[];let m=f.firstRun&&!O&&!S,k=!1;const _=f.lastTs,G=(u)=>{const o=te(t,u);if(!o)return!1;if(v.length&&!v.includes(o.event))return!1;if(!S&&!Ne(f,`${o.event}:${o.id}`))return!1;if(o.ts>f.lastTs)f.lastTs=o.ts;if(m){A++;return!1}if(!O&&!S&&_>0&&o.ts>0&&o.ts<=_){A++;return!1}if(!J)F(o);W.push(o);w++;return!0},ce=(u)=>{let o=0;for(const l of u){const n=te(t,l);if(!n)continue;if(v.length&&!v.includes(n.event))continue;if(f.set.has(`${n.event}:${n.id}`))continue;if(_>0&&n.ts>0&&n.ts<=_)continue;o++}return o},h=async(u,o=!1)=>{if(typeof e.reconcile!=="function")return;try{const l=re(await e.reconcile(a,g));if(o&&!m&&!O&&!S&&ce(l)>ve){m=!0;k=!0}let n=0;for(const s of l)if(G(s))n++;d(t,"reconciled",{reason:u,scanned:l.length,new:n})}catch(l){if(l instanceof U)throw l;d(t,"reconcile_failed",{reason:u,error:l?.message??String(l)})}},N=async(u)=>{let o=!1;try{o=await a.startNetworkCapture(e.pattern??"")!==!1}catch{o=!1}const l=Ce(e.pattern??"");y=!1;let n=!1;if(typeof a.cdp==="function")try{await a.cdp("Page.enable",{});await a.cdp("Page.addScriptToEvaluateOnNewDocument",{source:l});if(e.unthrottleTimers!==!1)await a.cdp("Page.addScriptToEvaluateOnNewDocument",{source:ie()});n=!0;y=!0}catch{}const s=await Ee(a,e.watchUrl);await a.goto(e.watchUrl,{waitUntil:n?"load":"none"});if(s&&await Te(a)){d(t,"page_reloading",{reason:u});await ae(a);await a.goto(e.watchUrl,{waitUntil:n?"load":"none"})}if(!n){if(e.unthrottleTimers!==!1)await a.evaluate(ie()).catch((p)=>d(t,"timer_shim_failed",{error:p?.message??String(p)}));try{await a.evaluate(l);y=!0;d(t,"page_hook_late",{note:"浏览器扩展版本较旧,旁听脚本只能在导航后注入;页面在此之前已建立的连接要等它重连才能被看到"})}catch(p){d(t,"page_hook_unavailable",{error:p?.message??String(p)})}}if(!o){await a.startNetworkCapture(e.pattern??"");await ae(a);await a.goto(e.watchUrl,{waitUntil:"load"})}if(typeof e.onAttach==="function")await e.onAttach(a,g);d(t,"watching",{reason:u,mode:x,url:e.watchUrl,pattern:e.pattern??"",reconcile_seconds:D})};await N("start");await h("startup",!0);if(m){C(t,r,f);d(t,"seeded",{seeded:A,desynced:k,note:k?"去重表和收件箱对不上(上次没跑完 / 换了账号 / 表被裁剪),本轮把当前收件箱重新登记为「已见过」,不触发任何事件。从现在起只有新消息才会上报":"首次运行:已把当前收件箱登记为「已见过」,不触发任何事件。从现在起只有新消息才会上报(要补历史请加 --backfill)"});m=!1}if(J){C(t,r,f);return W}let L=!1;const b=()=>{L=!0};process.on("SIGINT",b);process.on("SIGTERM",b);let j=Date.now(),R=Date.now(),K=w;const z=Math.max(0,Number(c["quiet-seconds"]??0)||Number(e.quietSeconds??0)||0);let E=Date.now(),P=0,T=0;try{while(!L){await new Promise((n)=>setTimeout(n,oe));if(L)break;let u=[];try{u=await a.readNetworkCapture()??[];if(y)u=u.concat(await Ie(a))}catch(n){d(t,"reattaching",{error:n?.message??String(n)});try{await N("capture_lost");await h("reattach")}catch(s){if(s instanceof U)throw s;d(t,"reattach_failed",{error:s?.message??String(s)})}continue}if(u.some((n)=>n?.direction==="sent"))P=Date.now();if(u.some((n)=>n?.direction!=="sent")){E=Date.now();T=0}for(const n of u){if(se)F({type:"frame",site:t,via:n?.kind==="page-hook"?"page-hook":"cdp",channel:n?.channel??"http",direction:n?.direction??"",method:n?.method??"",url:String(n?.url??"").slice(0,200),size:n?.responseBodyFullSize??String(n?.responsePreview??"").length,preview:String(n?.responsePreview??"").slice(0,600)});if(n?.direction==="sent")continue;let s;try{s=await e.parse(n,g)}catch(p){d(t,"parse_failed",{url:n?.url??"",error:p?.message??String(p)});continue}for(const p of re(s))G(p)}if(w!==K){C(t,r,f);K=w;R=Date.now()}if(i&&typeof a.getCurrentUrl==="function")try{const n=await a.getCurrentUrl();if(n&&ne(n)!==i){d(t,"page_drifted",{url:n});await N("drift");await h("drift")}}catch{}const o=Math.min(z*2**T,Se);if(z&&Date.now()-E>=o*1000){let n=null;try{n=await a.evaluate('(function(){ return JSON.stringify({ href: location.href, ready: document.readyState, hidden: document.hidden, buffered: Array.isArray(window.__pp_stream) ? window.__pp_stream.length : -1, hooked: !!window.__pp_stream_hooked, closed: window.__pp_stream_closed || null, timers: String(window.__pp_timers || "") }); })()')}catch(s){n=`evaluate 失败:${s?.message??String(s)}`}d(t,"quiet",{silent_ms:Date.now()-E,sent_ago_ms:P?Date.now()-P:-1,strikes:T,page:n});T++;E=Date.now();try{await N("quiet");await h("quiet")}catch(s){if(s instanceof U)throw s;d(t,"reattach_failed",{error:s?.message??String(s)})}continue}const l=Date.now();if(D&&l-j>=D*1000){j=l;await h("interval")}if(B&&l-R>=B*1000){R=l;d(t,"alive",{emitted:w})}}}finally{process.off("SIGINT",b);process.off("SIGTERM",b);C(t,r,f);d(t,"stopped",{emitted:w})}return W}})}export function frameJson(e){const r=e?.responsePreview;if(typeof r!=="string"||!r)return null;if(r.startsWith("base64:"))return null;try{return JSON.parse(r)}catch{return null}}
100
+ })()`}async function Ie(e){let r;try{r=await e.evaluate("(function(){ var o = window.__pp_stream || []; window.__pp_stream = []; return JSON.stringify(o); })()")}catch{return[]}let t;try{t=JSON.parse(typeof r==="string"?r:"[]")}catch{return[]}if(!Array.isArray(t))return[];return t.map((i)=>({kind:"page-hook",channel:i.channel,direction:i.direction,url:i.url,method:i.channel==="websocket"?"WS":"SSE",eventName:i.eventName||void 0,responseStatus:200,responseContentType:i.binary?"application/octet-stream":"text/plain",responsePreview:i.binary?"":String(i.data??""),timestamp:i.ts||Date.now()}))}export function registerWatch(e){if(!e?.site||!e?.domain||!e?.watchUrl||typeof e.parse!=="function")throw Error("registerWatch 需要 site、domain、watchUrl 和 parse");const r=e.name??"watch",t=e.site,i=ne(e.watchUrl);me({site:t,name:r,access:"read",description:e.description??`长驻监听${t}的新消息,旁听页面自身的实时通道并按 NDJSON 逐条输出(不对平台发轮询请求)`,domain:e.domain,strategy:he.COOKIE,browser:!0,navigateBefore:!1,sessionKey:e.sessionKey??"watch",siteSession:"persistent",defaultWindowMode:"background",args:[{name:"interval",type:"int",required:!1,default:U,help:`本机取帧间隔毫秒(默认 ${U})。纯本机 IPC,不发平台请求`},{name:"reconcile-interval",type:"int",required:!1,default:0,help:`对账间隔秒(0 = 用该平台的默认值:${Number(e.reconcileSeconds)>0?`本平台 ${Number(e.reconcileSeconds)} 秒`:`可旁听实时通道的平台 ${Q} 秒兜底,只能靠拉取的平台 ${X} 秒一轮`});-1 = 彻底关闭对账`},{name:"quiet-seconds",type:"int",required:!1,default:0,help:`实时通道这么多秒一帧都没收到就重载监听页重连(0 = 用该平台的默认值${Number(e.quietSeconds)>0?`:本平台 ${Number(e.quietSeconds)} 秒`:";本平台默认不查"})`},{name:"heartbeat",type:"int",required:!1,default:M,help:`心跳间隔秒(默认 ${M}),无事件时输出一行 status 表示还活着`},{name:"events",type:"str",required:!1,help:"只输出这些事件类型,逗号分隔(如 dm,comment);默认全部"},{name:"once",type:"bool",required:!1,default:!1,help:"只跑一轮对账后退出,不长驻。用于验证解析器和补历史"},{name:"replay-seen",type:"bool",required:!1,default:!1,help:"忽略去重表,把解析到的事件全部重新输出(调试用,会导致重复触发)"},{name:"backfill",type:"bool",required:!1,default:!1,help:"首次运行时把已存在的历史消息也当成新事件输出(默认只静默登记,不触发)"},{name:"dump-frames",type:"bool",required:!1,default:!1,help:"侦察模式:把收到的每一条原始帧(url / 通道 / 内容片段)也打出来,用来给新平台摸清事件通道"},{name:"include-own",type:"bool",required:!1,default:!1,help:"连自己发出去的消息也当成事件(默认不算——否则自动回复会追着自己的回复自我触发)。调试解析用"}],columns:["event","id","sender","text","ts"],func:async(a,c)=>{const oe=Math.max(200,Number(c.interval??U)),x=e.mode==="reconcile"?"reconcile":"stream",I=Number(c["reconcile-interval"]??0),D=I<0?0:I>0?I:Number(e.reconcileSeconds)>0?Number(e.reconcileSeconds):x==="reconcile"?X:Q,B=Math.max(0,Number(c.heartbeat??M)),J=c.once===!0||c.once==="true",S=c["replay-seen"]===!0||c["replay-seen"]==="true",O=c.backfill===!0||c.backfill==="true",se=c["dump-frames"]===!0||c["dump-frames"]==="true",v=String(c.events??"").split(",").map((u)=>u.trim()).filter(Boolean);if(typeof a.startNetworkCapture!=="function"||typeof a.readNetworkCapture!=="function")throw new V("当前浏览器通道不支持网络捕获,watch 无法工作(需要 CDP 或扩展路径)");const f=_e(t,r),g={site:t,page:a,state:{},includeOwn:c["include-own"]===!0||c["include-own"]==="true"};let w=0,A=0,y=!1;const W=[];let m=f.firstRun&&!O&&!S,k=!1;const _=f.lastTs,G=(u)=>{const o=te(t,u);if(!o)return!1;if(v.length&&!v.includes(o.event))return!1;if(!S&&!Ne(f,`${o.event}:${o.id}`))return!1;if(o.ts>f.lastTs)f.lastTs=o.ts;if(m){A++;return!1}if(!O&&!S&&_>0&&o.ts>0&&o.ts<=_){A++;return!1}if(!J)F(o);W.push(o);w++;return!0},ce=(u)=>{let o=0;for(const l of u){const n=te(t,l);if(!n)continue;if(v.length&&!v.includes(n.event))continue;if(f.set.has(`${n.event}:${n.id}`))continue;if(_>0&&n.ts>0&&n.ts<=_)continue;o++}return o},h=async(u,o=!1)=>{if(typeof e.reconcile!=="function")return;try{const l=re(await e.reconcile(a,g));if(o&&!m&&!O&&!S&&ce(l)>ve){m=!0;k=!0}let n=0;for(const s of l)if(G(s))n++;d(t,"reconciled",{reason:u,scanned:l.length,new:n})}catch(l){if(l instanceof q)throw l;d(t,"reconcile_failed",{reason:u,error:l?.message??String(l)})}},N=async(u)=>{let o=!1;try{o=await a.startNetworkCapture(e.pattern??"")!==!1}catch{o=!1}const l=Ce(e.pattern??"");y=!1;let n=!1;if(typeof a.cdp==="function")try{await a.cdp("Page.enable",{});await a.cdp("Page.addScriptToEvaluateOnNewDocument",{source:l});if(e.unthrottleTimers!==!1)await a.cdp("Page.addScriptToEvaluateOnNewDocument",{source:ie()});n=!0;y=!0}catch{}const s=await Ee(a,e.watchUrl);await a.goto(e.watchUrl,{waitUntil:n?"load":"none"});if(s&&await Te(a)){d(t,"page_reloading",{reason:u});await ae(a);await a.goto(e.watchUrl,{waitUntil:n?"load":"none"})}if(!n){if(e.unthrottleTimers!==!1)await a.evaluate(ie()).catch((p)=>d(t,"timer_shim_failed",{error:p?.message??String(p)}));try{await a.evaluate(l);y=!0;d(t,"page_hook_late",{note:"浏览器扩展版本较旧,旁听脚本只能在导航后注入;页面在此之前已建立的连接要等它重连才能被看到"})}catch(p){d(t,"page_hook_unavailable",{error:p?.message??String(p)})}}if(!o){await a.startNetworkCapture(e.pattern??"");await ae(a);await a.goto(e.watchUrl,{waitUntil:"load"})}if(typeof e.onAttach==="function")await e.onAttach(a,g);d(t,"watching",{reason:u,mode:x,url:e.watchUrl,pattern:e.pattern??"",reconcile_seconds:D})};await N("start");await h("startup",!0);if(m){C(t,r,f);d(t,"seeded",{seeded:A,desynced:k,note:k?"去重表和收件箱对不上(上次没跑完 / 换了账号 / 表被裁剪),本轮把当前收件箱重新登记为「已见过」,不触发任何事件。从现在起只有新消息才会上报":"首次运行:已把当前收件箱登记为「已见过」,不触发任何事件。从现在起只有新消息才会上报(要补历史请加 --backfill)"});m=!1}if(J){C(t,r,f);return W}let L=!1;const b=()=>{L=!0};process.on("SIGINT",b);process.on("SIGTERM",b);let j=Date.now(),P=Date.now(),K=w;const z=Math.max(0,Number(c["quiet-seconds"]??0)||Number(e.quietSeconds??0)||0);let E=Date.now(),R=0,T=0;try{while(!L){await new Promise((n)=>setTimeout(n,oe));if(L)break;let u=[];try{u=await a.readNetworkCapture()??[];if(y)u=u.concat(await Ie(a))}catch(n){d(t,"reattaching",{error:n?.message??String(n)});try{await N("capture_lost");await h("reattach")}catch(s){if(s instanceof q)throw s;d(t,"reattach_failed",{error:s?.message??String(s)})}continue}if(u.some((n)=>n?.direction==="sent"))R=Date.now();if(u.some((n)=>n?.direction!=="sent")){E=Date.now();T=0}for(const n of u){if(se)F({type:"frame",site:t,via:n?.kind==="page-hook"?"page-hook":"cdp",channel:n?.channel??"http",direction:n?.direction??"",method:n?.method??"",url:String(n?.url??"").slice(0,200),size:n?.responseBodyFullSize??String(n?.responsePreview??"").length,preview:String(n?.responsePreview??"").slice(0,600)});if(n?.direction==="sent")continue;let s;try{s=await e.parse(n,g)}catch(p){d(t,"parse_failed",{url:n?.url??"",error:p?.message??String(p)});continue}for(const p of re(s))G(p)}if(w!==K){C(t,r,f);K=w;P=Date.now()}if(i&&typeof a.getCurrentUrl==="function")try{const n=await a.getCurrentUrl();if(n&&ne(n)!==i){d(t,"page_drifted",{url:n});await N("drift");await h("drift")}}catch{}const o=Math.min(z*2**T,Se);if(z&&Date.now()-E>=o*1000){let n=null;try{n=await a.evaluate('(function(){ return JSON.stringify({ href: location.href, ready: document.readyState, hidden: document.hidden, buffered: Array.isArray(window.__pp_stream) ? window.__pp_stream.length : -1, hooked: !!window.__pp_stream_hooked, closed: window.__pp_stream_closed || null, timers: String(window.__pp_timers || "") }); })()')}catch(s){n=`evaluate 失败:${s?.message??String(s)}`}d(t,"quiet",{silent_ms:Date.now()-E,sent_ago_ms:R?Date.now()-R:-1,strikes:T,page:n});T++;E=Date.now();try{await N("quiet");await h("quiet")}catch(s){if(s instanceof q)throw s;d(t,"reattach_failed",{error:s?.message??String(s)})}continue}const l=Date.now();if(D&&l-j>=D*1000){j=l;await h("interval")}if(B&&l-P>=B*1000){P=l;d(t,"alive",{emitted:w})}}}finally{process.off("SIGINT",b);process.off("SIGTERM",b);C(t,r,f);d(t,"stopped",{emitted:w})}return W}})}export function frameJson(e){const r=e?.responsePreview;if(typeof r!=="string"||!r)return null;if(r.startsWith("base64:"))return null;try{return JSON.parse(r)}catch{return null}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "publishport-opencli",
3
- "version": "1.0.64",
3
+ "version": "1.0.65",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": false