node-network-devtools
Version:
Inspecting Node.js's Network with Chrome DevTools
4 lines (3 loc) • 3.48 kB
JavaScript
const N=require("url"),x=require("path");var l=typeof document<"u"?document.currentScript:null;class m{constructor(e){typeof e=="string"&&this.parse(e),e instanceof m&&Object.assign(this,e)}parse(e){if(e.match(/^\s*[-]{4,}$/))return this.fileName=e,this;const s=/at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/,t=e.match(s);if(!t)return this;let r=null,o=null,i=null,f=null,c=null,h=t[5]==="native";if(t[1]){i=t[1];let a=i.lastIndexOf(".");if(i[a-1]=="."&&a--,a>0){r=i.substr(0,a),o=i.substr(a+1);const u=r.indexOf(".Module");u>0&&(i=i.substr(u+1),r=r.substr(0,u))}}o&&(f=r,c=o),o==="<anonymous>"&&(c=null,i=null);const p={fileName:t[2]||null,lineNumber:parseInt(t[3],10)||null,functionName:i,typeName:f,methodName:c,columnNumber:parseInt(t[4],10)||null,native:h};return Object.assign(this,p),this}valueOf(){return{fileName:this.fileName,lineNumber:this.lineNumber,functionName:this.functionName,typeName:this.typeName,methodName:this.methodName,columnNumber:this.columnNumber,native:this.native}}toString(){return JSON.stringify(this.valueOf())}}const g=[/\((internal\/)?async_hooks\.js:/,/\(\//,/node_modules/];function b(n){const e=Object.create(null);return n?e.stack=n:(Error.stackTraceLimit=1/0,Error.captureStackTrace(e)),e.stack.split(`
`).slice(1).map(r=>new m(r))}function E(n){return n.filter(s=>!g.some(t=>t.test(s.fileName||"")))}const R=console.log.bind(console,"\x1B[36m[node-network-debugger]:","\x1B[32m"),O=console.warn.bind(console,"\x1B[36m[node-network-debugger](warn):","\x1B[33m"),_=(n,e)=>n+Object.entries(e).map(([s,t])=>`${s}: ${String(t)}`).join(`\r
`),T=n=>n.reduce((e,s,t,r)=>t%2===0?{...e,[r[t]]:r[t+1]}:e,{}),d=n=>Object.keys(n).reduce((e,s)=>{const t=n[s];return e[s]=typeof t=="object"&&t!==null?d(t):String(t),e},{}),S=()=>new Date().getTime()/1e3;function y(){let n=new Date().getTime();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(s){const t=(n+Math.random()*16)%16|0;return n=Math.floor(n/16),(s==="x"?t:t&3|8).toString(16)})}function D(n){let e=0,s,t;if(n.length===0)return e.toString(36);for(s=0;s<n.length;s++)t=n.charCodeAt(s),e=(e<<5)-e+t,e|=0;return e.toString(36)}class k{constructor(e){e?(this.id=e.id,this.responseInfo=e.responseInfo,Object.assign(this,e)):(this.id=y(),this.responseInfo={})}loadCallFrames(e){const t=E(b(e)).map(r=>{const o=r.fileName||"";return{columnNumber:r.columnNumber||0,functionName:r.functionName||"",lineNumber:r.lineNumber||0,url:o.startsWith("/")?`file://${o}`:o}});t.length>0&&(this.initiator={type:"script",stack:{callFrames:t}})}isHiden(){return this.isWebSocket()&&["http://localhost/","ws://localhost/"].includes(this.url)}isWebSocket(){return this.requestHeaders?.Upgrade==="websocket"||this.requestHeaders?.upgrade==="websocket"}}const w=Number(process.env.NETWORK_PORT||5270),j=Number(process.env.NETWORK_SERVER_PORT||5271),v=Number(process.env.REMOTE_DEBUGGER_PORT||9333),I=process.env.NETWORK_DEBUG_MODE==="true",M="ready",P=N.fileURLToPath(typeof document>"u"?require("url").pathToFileURL(__filename).href:l&&l.tagName.toUpperCase()==="SCRIPT"&&l.src||new URL("common-BlGFHnVB.js",document.baseURI).href),U=x.dirname(P);exports.IS_DEV_MODE=I;exports.PORT=w;exports.READY_MESSAGE=M;exports.REMOTE_DEBUGGER_PORT=v;exports.RequestDetail=k;exports.SERVER_PORT=j;exports.__dirname=U;exports.formatHeadersToHeaderText=_;exports.generateHash=D;exports.getTimestamp=S;exports.log=R;exports.parseRawHeaders=T;exports.stringifyNestedObj=d;exports.warn=O;
;