@@ -5,6 +5,7 @@ import rmCookie from 'licia/rmCookie'
55import isNative from 'licia/isNative'
66import contain from 'licia/contain'
77import now from 'licia/now'
8+ import Emitter from 'licia/Emitter'
89import isStr from 'licia/isStr'
910import isBlob from 'licia/isBlob'
1011import isUndef from 'licia/isUndef'
@@ -78,55 +79,7 @@ function enableXhr() {
7879 url
7980 ) )
8081
81- req . on ( 'send' , ( id : string , data : any ) => {
82- const request : any = {
83- method : data . method ,
84- url : data . url ,
85- headers : data . reqHeaders ,
86- }
87- if ( data . data ) {
88- request . postData = data . data
89- }
90-
91- trigger ( 'Network.requestWillBeSent' , {
92- requestId : id ,
93- type : 'XHR' ,
94- request,
95- timestamp : data . time / 1000 ,
96- } )
97- } )
98- req . on ( 'headersReceived' , ( id : string , data : any ) => {
99- trigger ( 'Network.responseReceivedExtraInfo' , {
100- requestId : id ,
101- blockedCookies : [ ] ,
102- headers : data . resHeaders ,
103- } )
104- } )
105- req . on ( 'done' , ( id : string , data : any ) => {
106- trigger ( 'Network.responseReceived' , {
107- requestId : id ,
108- type : 'XHR' ,
109- response : {
110- status : data . status ,
111- } ,
112- timestamp : data . time / 1000 ,
113- } )
114- resTxtMap . set ( id , data . resTxt )
115- trigger ( 'Network.loadingFinished' , {
116- requestId : id ,
117- encodedDataLength : data . size ,
118- timestamp : data . time / 1000 ,
119- } )
120- } )
121-
122- xhr . addEventListener ( 'readystatechange' , function ( ) {
123- switch ( xhr . readyState ) {
124- case 2 :
125- return req . handleHeadersReceived ( )
126- case 4 :
127- return req . handleDone ( )
128- }
129- } )
82+ bindRequestEvent ( req , 'XHR' )
13083
13184 origOpen . apply ( this , arguments )
13285 }
@@ -168,49 +121,70 @@ function enableFetch() {
168121
169122 window . fetch = function ( ...args ) {
170123 const req = new FetchRequest ( ...args )
171- req . on ( 'send' , ( id , data ) => {
172- const request : any = {
173- method : data . method ,
174- url : data . url ,
175- headers : data . reqHeaders ,
176- }
177-
178- if ( data . data ) {
179- request . postData = data . data
180- }
181-
182- trigger ( 'Network.requestWillBeSent' , {
183- requestId : id ,
184- type : 'Fetch' ,
185- request,
186- timestamp : data . time / 1000 ,
187- } )
188- } )
189- req . on ( 'done' , ( id , data ) => {
190- trigger ( 'Network.responseReceived' , {
191- requestId : id ,
192- type : 'Fetch' ,
193- response : {
194- status : data . status ,
195- headers : data . resHeaders ,
196- } ,
197- timestamp : data . time / 1000 ,
198- } )
199- resTxtMap . set ( id , data . resTxt )
200- trigger ( 'Network.loadingFinished' , {
201- requestId : id ,
202- encodedDataLength : data . size ,
203- timestamp : data . time / 1000 ,
204- } )
205- } )
206-
124+ bindRequestEvent ( req , 'Fetch' )
207125 const fetchResult = origFetch ( ...args )
208126 req . send ( fetchResult )
209127
210128 return fetchResult
211129 }
212130}
213131
132+ function bindRequestEvent ( req : Emitter , type : string ) {
133+ req . on ( 'send' , ( id : string , data : any ) => {
134+ const request : any = {
135+ method : data . method ,
136+ url : data . url ,
137+ headers : data . reqHeaders ,
138+ }
139+ if ( data . data ) {
140+ request . postData = data . data
141+ }
142+
143+ trigger ( 'Network.requestWillBeSent' , {
144+ requestId : id ,
145+ type,
146+ request,
147+ timestamp : data . time / 1000 ,
148+ } )
149+ } )
150+ req . on ( 'headersReceived' , ( id : string , data : any ) => {
151+ trigger ( 'Network.responseReceivedExtraInfo' , {
152+ requestId : id ,
153+ blockedCookies : [ ] ,
154+ headers : data . resHeaders ,
155+ } )
156+ } )
157+ req . on ( 'done' , ( id : string , data : any ) => {
158+ const response : any = {
159+ status : data . status ,
160+ }
161+ if ( data . resHeaders ) {
162+ response . headers = data . resHeaders
163+ }
164+
165+ trigger ( 'Network.responseReceived' , {
166+ requestId : id ,
167+ type,
168+ response,
169+ timestamp : data . time / 1000 ,
170+ } )
171+ resTxtMap . set ( id , data . resTxt )
172+ trigger ( 'Network.loadingFinished' , {
173+ requestId : id ,
174+ encodedDataLength : data . size ,
175+ timestamp : data . time / 1000 ,
176+ } )
177+ } )
178+ req . on ( 'error' , ( id , data ) => {
179+ trigger ( 'Network.loadingFailed' , {
180+ requestId : id ,
181+ errorText : data . errorText ,
182+ timestamp : data . time / 1000 ,
183+ type,
184+ } )
185+ } )
186+ }
187+
214188function enableWebSocket ( ) {
215189 const origWebSocket = window . WebSocket
216190 function WebSocket ( url : string , protocols ?: string | string [ ] ) {
0 commit comments