UNPKG

node-network-devtools

Version:

Inspecting Node.js's Network with Chrome DevTools

6 lines (5 loc) 22.7 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const ne=require("http"),F=require("https"),g=require("./common-B2WLGzKJ.js"),ie=require("stream"),S=require("node:zlib"),q=require("node:buffer"),D=require("ws"),oe=require("child_process"),H=require("path"),N=require("fs"),ae=require("net"),L=require("undici");function te(n){const e={};return n.forEach((t,r)=>{e[r]=t}),e}const he=typeof Blob<"u",M=["nodebuffer","arraybuffer","fragments",he?"blob":null].filter(Boolean),C=Buffer.alloc(0),ce=Symbol("status-code"),le=Symbol("websocket"),fe=Buffer[Symbol.species];function b(n,e){if(n.length===0)return C;if(n.length===1)return n[0];const t=Buffer.allocUnsafe(e);let r=0;for(let s=0;s<n.length;s++){const i=n[s];t.set(i,r),r+=i.length}return r<e?new fe(t.buffer,t.byteOffset,r):t}function de(n,e){for(let t=0;t<n.length;t++)n[t]^=e[t&3]}function _e(n){return n.length===n.buffer.byteLength?n.buffer:n.buffer.slice(n.byteOffset,n.byteOffset+n.length)}const V=Symbol("kDone"),A=Symbol("kRun");class ue{constructor(e=1/0){this[V]=()=>{this.pending--,this[A]()},this.concurrency=e,this.jobs=[],this.pending=0}add(e){this.jobs.push(e),this[A]()}[A](){if(this.pending!==this.concurrency&&this.jobs.length){const e=this.jobs.shift();this.pending++,e(this[V])}}}const pe=Buffer.from([0,0,255,255]),U=Symbol("permessage-deflate"),y=Symbol("total-length"),x=Symbol("callback"),E=Symbol("buffers"),me=Symbol("error");let T;class ${constructor(e={},t=!1,r=0){if(this._maxPayload=r|0,this._options=e,this._threshold=this._options.threshold!==void 0?this._options.threshold:1024,this._isServer=!!t,this._deflate=null,this._inflate=null,this.params=null,!T){const s=this._options.concurrencyLimit!==void 0?this._options.concurrencyLimit:10;T=new ue(s)}}static get extensionName(){return"permessage-deflate"}offer(){const e={};return this._options.serverNoContextTakeover&&(e.server_no_context_takeover=!0),this._options.clientNoContextTakeover&&(e.client_no_context_takeover=!0),this._options.serverMaxWindowBits&&(e.server_max_window_bits=this._options.serverMaxWindowBits),this._options.clientMaxWindowBits?e.client_max_window_bits=this._options.clientMaxWindowBits:this._options.clientMaxWindowBits==null&&(e.client_max_window_bits=!0),e}accept(e){return e=this.normalizeParams(e),this.params=this._isServer?this.acceptAsServer(e):this.acceptAsClient(e),this.params}cleanup(){if(this._inflate&&(this._inflate.close(),this._inflate=null),this._deflate){const e=this._deflate[x];this._deflate.close(),this._deflate=null,e&&e(new Error("The deflate stream was closed while data was being processed"))}}acceptAsServer(e){const t=this._options,r=e.find(s=>!(t.serverNoContextTakeover===!1&&s.server_no_context_takeover||s.server_max_window_bits&&(t.serverMaxWindowBits===!1||typeof t.serverMaxWindowBits=="number"&&t.serverMaxWindowBits>s.server_max_window_bits)||typeof t.clientMaxWindowBits=="number"&&!s.client_max_window_bits));if(!r)throw new Error("None of the extension offers can be accepted");return t.serverNoContextTakeover&&(r.server_no_context_takeover=!0),t.clientNoContextTakeover&&(r.client_no_context_takeover=!0),typeof t.serverMaxWindowBits=="number"&&(r.server_max_window_bits=t.serverMaxWindowBits),typeof t.clientMaxWindowBits=="number"?r.client_max_window_bits=t.clientMaxWindowBits:(r.client_max_window_bits===!0||t.clientMaxWindowBits===!1)&&delete r.client_max_window_bits,r}acceptAsClient(e){const t=e[0];if(this._options.clientNoContextTakeover===!1&&t.client_no_context_takeover)throw new Error('Unexpected parameter "client_no_context_takeover"');if(!t.client_max_window_bits)typeof this._options.clientMaxWindowBits=="number"&&(t.client_max_window_bits=this._options.clientMaxWindowBits);else if(this._options.clientMaxWindowBits===!1||typeof this._options.clientMaxWindowBits=="number"&&t.client_max_window_bits>this._options.clientMaxWindowBits)throw new Error('Unexpected or invalid parameter "client_max_window_bits"');return t}normalizeParams(e){return e.forEach(t=>{Object.keys(t).forEach(r=>{let s=t[r];if(s.length>1)throw new Error(`Parameter "${r}" must have only a single value`);if(s=s[0],r==="client_max_window_bits"){if(s!==!0){const i=+s;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${s}`);s=i}else if(!this._isServer)throw new TypeError(`Invalid value for parameter "${r}": ${s}`)}else if(r==="server_max_window_bits"){const i=+s;if(!Number.isInteger(i)||i<8||i>15)throw new TypeError(`Invalid value for parameter "${r}": ${s}`);s=i}else if(r==="client_no_context_takeover"||r==="server_no_context_takeover"){if(s!==!0)throw new TypeError(`Invalid value for parameter "${r}": ${s}`)}else throw new Error(`Unknown parameter "${r}"`);t[r]=s})}),e}decompress(e,t,r){T?.add(s=>{this._decompress(e,t,(i,o)=>{s(),r(i,o)})})}compress(e,t,r){T?.add(s=>{this._compress(e,t,(i,o)=>{s(),r(i,o)})})}_decompress(e,t,r){const s=this._isServer?"client":"server";if(!this._inflate){const i=`${s}_max_window_bits`,o=typeof this.params[i]!="number"?S.constants.Z_DEFAULT_WINDOWBITS:this.params[i];this._inflate=S.createInflateRaw({...this._options.zlibInflateOptions,windowBits:o}),this._inflate[U]=this,this._inflate[y]=0,this._inflate[E]=[],this._inflate.on("error",ge),this._inflate.on("data",we)}this._inflate[x]=r,this._inflate.write(e),t&&this._inflate.write(pe),this._inflate.flush(()=>{const i=this._inflate[me];if(i){this._inflate.close(),this._inflate=null,r(i);return}const o=b(this._inflate[E],this._inflate[y]);if(this._maxPayload<1||o.length<=this._maxPayload){r(null,o);return}r(new RangeError("Max payload size exceeded"),null)})}_compress(e,t,r){const s=this._isServer?"server":"client";if(!this._deflate){const i=`${s}_max_window_bits`,o=typeof this.params[i]!="number"?S.constants.Z_DEFAULT_WINDOWBITS:this.params[i];this._deflate=S.createDeflateRaw({...this._options.zlibDeflateOptions,windowBits:o}),this._deflate[y]=0,this._deflate[E]=[],this._deflate.on("error",ye),this._deflate.on("data",Ee)}this._deflate[x]=r,this._deflate.write(e),t&&this._deflate.flush(S.Z_SYNC_FLUSH,()=>{const i=b(this._deflate[E],this._deflate[y]);if(this._maxPayload<1||i.length<=this._maxPayload){r(null,i);return}r(new RangeError("Max payload size exceeded"),null)})}}function ge(n){this[U][x](n)}function we(n){this[y]+=n.length,this[E].push(n)}function ye(n){this[U][x](n)}function Ee(n){this[y]+=n.length,this[E].push(n)}function Re(n){return n>=1e3&&n<=1014&&n!==1004&&n!==1005&&n!==1006||n>=3e3&&n<=4999}function G(n){const e=n.length;let t=0;for(;t<e;)if(!(n[t]&128))t++;else if((n[t]&224)===192){if(t+1===e||(n[t+1]&192)!==128||(n[t]&254)===192)return!1;t+=2}else if((n[t]&240)===224){if(t+2>=e||(n[t+1]&192)!==128||(n[t+2]&192)!==128||n[t]===224&&(n[t+1]&224)===128||n[t]===237&&(n[t+1]&224)===160)return!1;t+=3}else if((n[t]&248)===240){if(t+3>=e||(n[t+1]&192)!==128||(n[t+2]&192)!==128||(n[t+3]&192)!==128||n[t]===240&&(n[t+1]&240)===128||n[t]===244&&n[t+1]>143||n[t]>244)return!1;t+=4}else return!1;return!0}const j=q.isUtf8?function(n){return n.length<24?G(n):q.isUtf8(n)}:G;var z;const k=Buffer[Symbol.species],m=0,Y=1,X=2,J=3,B=4,P=5,I=6;class K extends(z=ie.Writable,z){constructor(e={}){super(),this._allowSynchronousEvents=e.allowSynchronousEvents!==void 0?e.allowSynchronousEvents:!0,this._binaryType=e.binaryType||M[0],this._extensions=e.extensions||{},this._isServer=!!e.isServer,this._maxPayload=e.maxPayload||0,this._skipUTF8Validation=!!e.skipUTF8Validation,this[le]=void 0,this._bufferedBytes=0,this._buffers=[],this._compressed=!1,this._payloadLength=0,this._mask=void 0,this._fragmented=0,this._masked=!1,this._fin=!1,this._opcode=0,this._totalPayloadLength=0,this._messageLength=0,this._fragments=[],this._errored=!1,this._loop=!1,this._state=m}_write(e,t,r){if(this._opcode===8&&this._state===m)return r();this._bufferedBytes+=e.length,this._buffers.push(e),this.startLoop(r)}consume(e){if(this._bufferedBytes-=e,e===this._buffers[0].length)return this._buffers.shift();if(e<this._buffers[0].length){const r=this._buffers[0];return this._buffers[0]=new k(r.buffer,r.byteOffset+e,r.length-e),new k(r.buffer,r.byteOffset,e)}const t=Buffer.allocUnsafe(e);do{const r=this._buffers[0],s=t.length-e;e>=r.length?t.set(this._buffers.shift(),s):(t.set(new Uint8Array(r.buffer,r.byteOffset,e),s),this._buffers[0]=new k(r.buffer,r.byteOffset+e,r.length-e)),e-=r.length}while(e>0);return t}startLoop(e){this._loop=!0;do switch(this._state){case m:this.getInfo(e);break;case Y:this.getPayloadLength16(e);break;case X:this.getPayloadLength64(e);break;case J:this.getMask();break;case B:this.getData(e);break;case P:case I:this._loop=!1;return}while(this._loop);this._errored||e()}getInfo(e){if(this._bufferedBytes<2){this._loop=!1;return}const t=this.consume(2);if(t[0]&48){const s=this.createError(RangeError,"RSV2 and RSV3 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_2_3");e(s);return}const r=(t[0]&64)===64;if(r&&!this._extensions[$.extensionName]){const s=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(s);return}if(this._fin=(t[0]&128)===128,this._opcode=t[0]&15,this._payloadLength=t[1]&127,this._opcode===0){if(r){const s=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(s);return}if(!this._fragmented){const s=this.createError(RangeError,"invalid opcode 0",!0,1002,"WS_ERR_INVALID_OPCODE");e(s);return}this._opcode=this._fragmented}else if(this._opcode===1||this._opcode===2){if(this._fragmented){const s=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(s);return}this._compressed=r}else if(this._opcode>7&&this._opcode<11){if(!this._fin){const s=this.createError(RangeError,"FIN must be set",!0,1002,"WS_ERR_EXPECTED_FIN");e(s);return}if(r){const s=this.createError(RangeError,"RSV1 must be clear",!0,1002,"WS_ERR_UNEXPECTED_RSV_1");e(s);return}if(this._payloadLength>125||this._opcode===8&&this._payloadLength===1){const s=this.createError(RangeError,`invalid payload length ${this._payloadLength}`,!0,1002,"WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH");e(s);return}}else{const s=this.createError(RangeError,`invalid opcode ${this._opcode}`,!0,1002,"WS_ERR_INVALID_OPCODE");e(s);return}if(!this._fin&&!this._fragmented&&(this._fragmented=this._opcode),this._masked=(t[1]&128)===128,this._isServer){if(!this._masked){const s=this.createError(RangeError,"MASK must be set",!0,1002,"WS_ERR_EXPECTED_MASK");e(s);return}}else if(this._masked){const s=this.createError(RangeError,"MASK must be clear",!0,1002,"WS_ERR_UNEXPECTED_MASK");e(s);return}this._payloadLength===126?this._state=Y:this._payloadLength===127?this._state=X:this.haveLength(e)}getPayloadLength16(e){if(this._bufferedBytes<2){this._loop=!1;return}this._payloadLength=this.consume(2).readUInt16BE(0),this.haveLength(e)}getPayloadLength64(e){if(this._bufferedBytes<8){this._loop=!1;return}const t=this.consume(8),r=t.readUInt32BE(0);if(r>Math.pow(2,21)-1){const s=this.createError(RangeError,"Unsupported WebSocket frame: payload length > 2^53 - 1",!1,1009,"WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH");e(s);return}this._payloadLength=r*Math.pow(2,32)+t.readUInt32BE(4),this.haveLength(e)}haveLength(e){if(this._payloadLength&&this._opcode<8&&(this._totalPayloadLength+=this._payloadLength,this._totalPayloadLength>this._maxPayload&&this._maxPayload>0)){const t=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");e(t);return}this._masked?this._state=J:this._state=B}getMask(){if(this._bufferedBytes<4){this._loop=!1;return}this._mask=this.consume(4),this._state=B}getData(e){let t=C;if(this._payloadLength){if(this._bufferedBytes<this._payloadLength){this._loop=!1;return}t=this.consume(this._payloadLength),this._masked&&this._mask[0]|this._mask[1]|this._mask[2]|this._mask[3]&&de(t,this._mask)}if(this._opcode>7){this.controlMessage(t,e);return}if(this._compressed){this._state=P,this.decompress(t,e);return}t.length&&(this._messageLength=this._totalPayloadLength,this._fragments.push(t)),this.dataMessage(e)}decompress(e,t){this._extensions[$.extensionName].decompress(e,this._fin,(s,i)=>{if(s)return t(s);if(i&&i.length){if(this._messageLength+=i.length,this._messageLength>this._maxPayload&&this._maxPayload>0){const o=this.createError(RangeError,"Max payload size exceeded",!1,1009,"WS_ERR_UNSUPPORTED_MESSAGE_LENGTH");t(o);return}this._fragments.push(i)}this.dataMessage(t),this._state===m&&this.startLoop(t)})}dataMessage(e){if(!this._fin){this._state=m;return}const t=this._messageLength,r=this._fragments;if(this._totalPayloadLength=0,this._messageLength=0,this._fragmented=0,this._fragments=[],this._opcode===2){let s;this._binaryType==="nodebuffer"?s=b(r,t):this._binaryType==="arraybuffer"?s=_e(b(r,t)):this._binaryType==="blob"?s=new Blob(r):s=r,this._allowSynchronousEvents?(this.emit("message",s,!0),this._state=m):(this._state=I,setImmediate(()=>{this.emit("message",s,!0),this._state=m,this.startLoop(e)}))}else{const s=b(r,t);if(!this._skipUTF8Validation&&!j(s)){const i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");e(i);return}this._state===P||this._allowSynchronousEvents?(this.emit("message",s,!1),this._state=m):(this._state=I,setImmediate(()=>{this.emit("message",s,!1),this._state=m,this.startLoop(e)}))}}controlMessage(e,t){if(this._opcode===8){if(e.length===0)this._loop=!1,this.emit("conclude",1005,C),this.end();else{const r=e.readUInt16BE(0);if(!Re(r)){const i=this.createError(RangeError,`invalid status code ${r}`,!0,1002,"WS_ERR_INVALID_CLOSE_CODE");t(i);return}const s=new k(e.buffer,e.byteOffset+2,e.length-2);if(!this._skipUTF8Validation&&!j(s)){const i=this.createError(Error,"invalid UTF-8 sequence",!0,1007,"WS_ERR_INVALID_UTF8");t(i);return}this._loop=!1,this.emit("conclude",r,s),this.end()}this._state=m;return}this._allowSynchronousEvents?(this.emit(this._opcode===9?"ping":"pong",e),this._state=m):(this._state=I,setImmediate(()=>{this.emit(this._opcode===9?"ping":"pong",e),this._state=m,this.startLoop(t)}))}createError(e,t,r,s,i){this._loop=!1,this._errored=!0;const o=new e(r?`Invalid WebSocket frame: ${t}`:t);return Error.captureStackTrace(o,this.createError),o.code=i,o[ce]=s,o}}function Z(n,e,t){const r=n.write;return n.write=s=>{let i;return Buffer.isBuffer(s)?i=s.toString("utf-8"):typeof s=="string"?i=s:s!=null?i=JSON.stringify(s):i="",e.requestData?e.requestData+=i:e.requestData=i,r.bind(n)(s)},n.on("error",()=>{e.responseStatusCode=0,e.requestEndTime=new Date().getTime(),t.sendRequest("endRequest",e)}),e.isWebSocket()?n.on("upgrade",async(s,i,o)=>{const f=i.write;if(e.isHiden())return;await t.send({type:"Network.webSocketCreated",data:{requestId:e.id,url:e.url,initiator:e.initiator,response:s}});const h=new K({allowSynchronousEvents:!0,binaryType:M[0],isServer:!1}),_=new K({allowSynchronousEvents:!0,binaryType:M[0],isServer:!0}),a=d=>{const p=d.toString();t.send({type:"Network.webSocketFrameReceived",data:{requestId:e.id,response:{payloadData:p,opcode:1,mask:!1}}})},u=d=>{const p=d.toString();t.send({type:"Network.webSocketFrameSent",data:{requestId:e.id,response:{payloadData:p,opcode:1,mask:!0}}})};h.on("message",a),_.on("message",u);let c;i.write=(d,...p)=>{const l=Buffer.from(d);return _.write(l),f.call(i,d,...p)},i.addListener("data",d=>{const p=Buffer.from(d);h.write(p)}),i.addListener("close",()=>{c=i.read(),c!==null&&(h.write(c),_.write(c)),h.end(),_.end(),h.removeAllListeners(),_.removeAllListeners(),t.send({method:"Network.webSocketClosed",params:{requestId:e.id,timestamp:g.getTimestamp()}})}),i.addListener("end",()=>{h.end(),_.end(),h.removeAllListeners(),_.removeAllListeners()})}):t.sendRequest("registerRequest",e),n}function ve(n,e,t){return r=>{e.responseHeaders=r.headers,typeof n=="function"&&n(r),t.responseRequest(e.id,r)}}function Q(n,e){let t=n.setHeader;n.setHeader=function(r,s){return Array.isArray(s)?s.forEach(i=>{e.requestHeaders[r]=i}):e.requestHeaders[r]=s,t.call(n,r,s)}}function Se(n,e,t){return(s,i,o)=>{let f,h,_;typeof s=="string"||s instanceof URL?(f=s,h=i,_=o):(h=s,_=i);const a=new g.RequestDetail;if(typeof f=="string")a.url=f,a.method="GET";else if(f instanceof URL)a.url=f.toString(),a.method="GET";else if(h&&typeof h!="string"&&!(h instanceof URL)){const c=e?"https":"http";a.url=`${c}://${h.hostname||h.host}${h.path}`}h&&typeof h!="string"&&!(h instanceof URL)&&(a.method=h.method,a.requestHeaders=h.headers),a.loadCallFrames(),a.isWebSocket()&&(a.url=a.url.replace("http://","ws://").replace("https://","wss://")),t.sendRequest("initRequest",a);const u=ve(_,a,t);if(typeof s=="string"||s instanceof URL){const c=n(f,h,u);return Q(c,a),Z(c,a,t)}else{const c=n(h,u);return Q(c,a),Z(c,a,t)}}}const be=n=>new Promise(e=>setTimeout(e,n)),xe=(n,e)=>{try{return Number(n)===process.pid?Promise.resolve(!0):(process.kill(Number(n),0),new Promise(t=>{const r=ae.createServer();r.once("error",s=>{s.code==="EADDRINUSE"&&t(!1),t(!1)}),r.once("listening",()=>{r.close(()=>t(!0))}),r.listen(e)}))}catch{return Promise.resolve(!1)}},W=n=>{try{N.unlinkSync(n)}catch{}};let se=null;function R(){return se}function O(n){se=n}class ee extends Error{constructor(e){super(e)}}class Le{constructor(e){this.options=e,this.ws=new Promise(async(t,r)=>{const s=H.resolve(g.__dirname,`./${e.key}`);if(N.existsSync(s)){const o=N.readFileSync(s,"utf-8");if(await be(1),await xe(o,e.port)){g.warn("The main process with same options is already running, skip it.");return}W(s)}N.writeFileSync(s,`${process.pid}`);const i=new D(`ws://127.0.0.1:${e.port}`);i.on("open",()=>{W(s),t(i)}),i.on("error",()=>{this.openProcess(()=>{W(s);const o=new D(`ws://127.0.0.1:${e.port}`);o.on("open",()=>{t(o)}),o.on("error",r)})})}),this.ws.then(t=>{this.healthCheck(),t.on("error",r=>{console.error("MainProcess Socket Error: ",r)})}).catch(t=>{if(!(t instanceof ee))throw t})}openProcess(e){(()=>{const r=oe.fork(H.resolve(g.__dirname,"./fork"),{env:{...process.env,NETWORK_OPTIONS:JSON.stringify(this.options)}}),s=i=>{i===g.READY_MESSAGE&&(e&&e(r),r.off("message",s))};r.on("message",s),this.cp=r})()}async send(e){if(R()?.isAborted)return;const r=await this.ws.catch(s=>{if(s instanceof ee)return null;throw s});r&&r.send(JSON.stringify(e))}sendRequest(e,t){const r=R();let s=t;return r&&(r.request=s,r.pipes.filter(o=>o.type===e).map(o=>o.pipe).forEach(o=>{s=o(s)}),r.request=s),this.send({type:e,data:t}),this}async healthCheck(){const e=await this.ws,t=()=>{e.send(JSON.stringify({type:"healthcheck",data:{}}))};t(),setInterval(t,2e3)}responseRequest(e,t){const r=[];t.on("data",s=>{r.push(s)}),t.on("end",()=>{const s=Buffer.concat(r);this.ws.then(i=>{i.send(JSON.stringify({type:"responseData",data:{id:e,rawData:s,statusCode:t.statusCode,headers:t.headers}}),{binary:!0})})})}async dispose(){const e=await this.ws;e.removeAllListeners(),e.terminate(),this.cp&&(this.cp.removeAllListeners(),this.cp.kill(),this.cp=void 0)}}function Te(n){if(!globalThis.fetch)return;const e=globalThis.fetch;return globalThis.fetch=re(e,n),()=>{globalThis.fetch=e}}function re(n,e){return function(t,r){const s=new g.RequestDetail;s.requestStartTime=Date.now(),O({request:s,pipes:[],isAborted:!1}),typeof t=="string"?s.url=t:t instanceof URL&&(s.url=t.toString()),s.method=r?.method??"GET";const i=r?.headers;if(i instanceof Headers){const f=te(i);s.requestHeaders=f}else s.requestHeaders=i??{};s.requestData=r?.body;const o=n(t,r).then(Ne(s,e)).catch(Ae(s,e)).finally(()=>{O(null)});return e.sendRequest("initRequest",s).sendRequest("registerRequest",s),o}}function ke(n){return(n.headers.get("content-type")||"").includes("text/event-stream")}async function Ie(n,e,t){const r=n.clone().body;if(!r)return;const s=r.getReader(),i=new TextDecoder;let o="";const f=[];t.sendRequest("updateRequest",e);try{for(;;){const{done:u,value:c}=await s.read();if(u)break;if(c){f.push(c),o+=i.decode(c,{stream:!0});const d=o.split(` `);o=d.pop()||"";let p="message",l="",v="";for(const w of d)w.startsWith("event:")?p=w.slice(6).trim():w.startsWith("data:")?l+=(l?` `:"")+w.slice(5).trim():w.startsWith("id:")?v=w.slice(3).trim():w===""&&(l&&t.send({type:"eventSourceMessage",data:{requestId:e.id,eventName:p,eventId:v,data:l}}),p="message",l="",v="")}}if(o.trim()){const u=o.split(` `);let c="message",d="",p="";for(const l of u)l.startsWith("event:")?c=l.slice(6).trim():l.startsWith("data:")?d+=(d?` `:"")+l.slice(5).trim():l.startsWith("id:")&&(p=l.slice(3).trim());d&&t.send({type:"eventSourceMessage",data:{requestId:e.id,eventName:c,eventId:p,data:d}})}const h=f.reduce((u,c)=>u+c.length,0),_=new Uint8Array(h);let a=0;for(const u of f)_.set(u,a),a+=u.length;e.responseData=Buffer.from(_),e.responseInfo.dataLength=h,e.responseInfo.encodedDataLength=h}catch{if(f.length>0){const _=f.reduce((c,d)=>c+d.length,0),a=new Uint8Array(_);let u=0;for(const c of f)a.set(c,u),u+=c.length;e.responseData=Buffer.from(a),e.responseInfo.dataLength=_,e.responseInfo.encodedDataLength=_}}finally{e.requestEndTime=Date.now(),t.sendRequest("updateRequest",e).sendRequest("endRequest",e)}}function Ne(n,e){return t=>(n.requestEndTime=new Date().getTime(),n.responseHeaders=te(t.headers),n.responseStatusCode=t.status||0,ke(t)?(Ie(t,n,e),t):(t.clone().arrayBuffer().then(r=>{const s=Buffer.from(r);n.responseData=s,n.responseInfo.dataLength=s.length,n.responseInfo.encodedDataLength=s.length}).finally(()=>{e.sendRequest("updateRequest",n).sendRequest("endRequest",n)}),t))}function Ae(n,e){return t=>{throw n.requestEndTime=Date.now(),n.responseStatusCode=0,e.sendRequest("updateRequest",n).sendRequest("endRequest",n),t}}const Be=n=>{if(!L.fetch)return;const e=L.fetch;return L.fetch=re(e,n),()=>{L.fetch=e}},Pe=n=>{const e=R();if(!e)throw new Error("useRegisterRequest must be used in request handler");e.pipes.push({pipe:n,type:"registerRequest"})},We=(n,e)=>{const t=R();if(!t)throw new Error("useRequestPipe must be used in request handler");t.pipes.push({pipe:e,type:n})},Me=()=>{const n=R();if(!n)throw new Error("useRegisterRequest must be used in request handler");n.isAborted=!0};function Ce(n){const{port:e=g.PORT,serverPort:t=g.SERVER_PORT,autoOpenDevtool:r=!0,intercept:s={}}=n||{},{fetch:i=!0,normal:o=!0,undici:f=!1}=s,h=f&&f.fetch,_=g.generateHash(JSON.stringify({port:e,serverPort:t,autoOpenDevtool:r})),a=new Le({port:e,serverPort:t,autoOpenDevtool:r,key:_}),u=i?Te(a):void 0,c=new WeakMap,d=[ne,F];o&&d.forEach(l=>{c.set(l,l.request);const v=l.request;l.request=Se(v,l===F,a)});const p=h?Be(a):void 0;return()=>{u&&u(),o&&d.forEach(l=>{l.request=c.get(l),c.delete(l)}),p&&p(),a.dispose()}}exports.getCurrentCell=R;exports.register=Ce;exports.setCurrentCell=O;exports.useAbortRequest=Me;exports.useRegisterRequest=Pe;exports.useRequestPipe=We;