Skip to content

Commit 9b88aac

Browse files
committed
fix: fetch error keep pending
1 parent 438cf95 commit 9b88aac

File tree

8 files changed

+195
-171
lines changed

8 files changed

+195
-171
lines changed

.eslintrc.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

.eslintrc.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"browser": true,
5+
"node": true
6+
},
7+
"parser": "@typescript-eslint/parser",
8+
"plugins": [
9+
"@typescript-eslint"
10+
],
11+
"extends": [
12+
"eslint:recommended",
13+
"plugin:@typescript-eslint/recommended",
14+
"prettier"
15+
],
16+
"rules": {
17+
"@typescript-eslint/no-explicit-any": "off",
18+
"prefer-rest-params": "off",
19+
"@typescript-eslint/no-this-alias": "off",
20+
"@typescript-eslint/no-var-requires": "off",
21+
"@typescript-eslint/no-require-imports": "off",
22+
"@typescript-eslint/no-unused-vars": "off",
23+
"prefer-spread": "off"
24+
}
25+
}

devtools/network.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
setTimeout(function () {
2+
fetch(location.href)
3+
}, 1000)
4+
5+
function testFetch() {
6+
fetch('/index.js')
7+
fetch('https://domainnotexist.com').catch(err => {
8+
console.error(err)
9+
})
10+
}
11+
12+
function testXhr() {
13+
let xhr = new XMLHttpRequest()
14+
xhr.open('GET', '/index.js', true)
15+
xhr.send()
16+
17+
xhr = new XMLHttpRequest()
18+
xhr.open('GET', 'https://domainnotexist.com', true)
19+
xhr.send()
20+
}
21+
22+
function testWs() {
23+
const text = 'This is the text used for testing!'
24+
25+
const enc = new TextEncoder()
26+
const ws = new WebSocket('wss://echo.websocket.org')
27+
28+
ws.onopen = function () {
29+
ws.send(text)
30+
ws.send(enc.encode(text))
31+
}
32+
setTimeout(() => {
33+
ws.close()
34+
}, 1000)
35+
}
36+
37+
testFetch()
38+
testXhr()
39+
testWs()

devtools/target.html

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,8 @@
8686
console.log('Page loaded!')
8787
setTimeout(function () {
8888
console.log('Hello Chii')
89-
fetch(location.href)
9089
}, 1000)
9190
</script>
92-
<script>
93-
function testFetch() {
94-
fetch('/index.js')
95-
}
96-
function testWs() {
97-
const text = 'This is the text used for testing!'
98-
99-
const enc = new TextEncoder()
100-
const ws = new WebSocket('wss://echo.websocket.org')
101-
102-
ws.onopen = function () {
103-
ws.send(text)
104-
ws.send(enc.encode(text))
105-
}
106-
}
107-
testFetch()
108-
testWs()
109-
</script>
11091
<script type="module" src="target.js"></script>
11192
<script>
11293
console.log('console right after target injected')

devtools/target.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './db.js'
2+
import './network.js'
23

34
const targetOrigin = location.protocol + '//' + location.host
45
function sendToDevtools(message) {
@@ -48,29 +49,29 @@ function resetDevtools() {
4849
resourceTreeModel
4950
)
5051
}
51-
})
52-
sendToDevtools({
53-
method: 'Page.frameNavigated',
54-
params: {
55-
frame: {
56-
id: '1',
57-
mimeType: 'text/html',
58-
securityOrigin: location.origin,
59-
url: location.href,
52+
sendToDevtools({
53+
method: 'Page.frameNavigated',
54+
params: {
55+
frame: {
56+
id: '1',
57+
mimeType: 'text/html',
58+
securityOrigin: location.origin,
59+
url: location.href,
60+
},
61+
type: 'Navigation',
6062
},
61-
type: 'Navigation',
62-
},
63+
})
64+
sendToChobitsu({ method: 'Network.enable' })
65+
sendToDevtools({ method: 'Runtime.executionContextsCleared' })
66+
sendToChobitsu({ method: 'Runtime.enable' })
67+
sendToChobitsu({ method: 'Debugger.enable' })
68+
sendToChobitsu({ method: 'DOMStorage.enable' })
69+
sendToChobitsu({ method: 'DOM.enable' })
70+
sendToChobitsu({ method: 'CSS.enable' })
71+
sendToChobitsu({ method: 'Overlay.enable' })
72+
sendToDevtools({ method: 'DOM.documentUpdated' })
73+
sendToChobitsu({ method: 'Page.enable' })
74+
sendToDevtools({ method: 'Page.loadEventFired' })
6375
})
64-
sendToChobitsu({ method: 'Network.enable' })
65-
sendToDevtools({ method: 'Runtime.executionContextsCleared' })
66-
sendToChobitsu({ method: 'Runtime.enable' })
67-
sendToChobitsu({ method: 'Debugger.enable' })
68-
sendToChobitsu({ method: 'DOMStorage.enable' })
69-
sendToChobitsu({ method: 'DOM.enable' })
70-
sendToChobitsu({ method: 'CSS.enable' })
71-
sendToChobitsu({ method: 'Overlay.enable' })
72-
sendToDevtools({ method: 'DOM.documentUpdated' })
73-
sendToChobitsu({ method: 'Page.enable' })
74-
sendToDevtools({ method: 'Page.loadEventFired' })
7576
}, 0)
7677
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"@babel/preset-env": "^7.17.10",
4949
"@jsdevtools/coverage-istanbul-loader": "^3.0.5",
5050
"@types/node": "^20.14.12",
51-
"@typescript-eslint/eslint-plugin": "^5.33.0",
52-
"@typescript-eslint/parser": "^5.33.0",
51+
"@typescript-eslint/eslint-plugin": "^8.9.0",
52+
"@typescript-eslint/parser": "^8.9.0",
5353
"babel-loader": "^8.2.5",
5454
"concurrently": "^7.6.0",
5555
"es-check": "^6.2.1",

src/domains/Network.ts

Lines changed: 59 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import rmCookie from 'licia/rmCookie'
55
import isNative from 'licia/isNative'
66
import contain from 'licia/contain'
77
import now from 'licia/now'
8+
import Emitter from 'licia/Emitter'
89
import isStr from 'licia/isStr'
910
import isBlob from 'licia/isBlob'
1011
import 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+
214188
function enableWebSocket() {
215189
const origWebSocket = window.WebSocket
216190
function WebSocket(url: string, protocols?: string | string[]) {

0 commit comments

Comments
 (0)