Skip to content

Commit d75f4ee

Browse files
committed
use native fetch is some other helper
1 parent 3849954 commit d75f4ee

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

‎lib/helper/CDPBrowser.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class CDPBrowser extends Helper {
162162
let endpoint = this.options.endpoint
163163
if (endpoint.startsWith('http')) {
164164
const res = await fetch(`${endpoint.replace(/\/$/, '')}/json/version`, { headers: this.options.headers })
165+
if (!res.ok) throw new Error(`Failed to get version from ${endpoint}: ${res.status} ${res.statusText}`)
165166
const data = await res.json()
166167
endpoint = data.webSocketDebuggerUrl
167168
}
@@ -302,11 +303,11 @@ class CDPBrowser extends Helper {
302303
const controller = new AbortController()
303304
const timeout = setTimeout(() => controller.abort(), 1000)
304305
try {
305-
await fetch(url, { signal: controller.signal })
306+
const res = await fetch(url, { signal: controller.signal })
307+
return res.ok
306308
} finally {
307309
clearTimeout(timeout)
308310
}
309-
return true
310311
} catch (e) {
311312
return false
312313
}
@@ -334,11 +335,12 @@ class CDPBrowser extends Helper {
334335
const timeoutMs = Math.min(1000, deadline - Date.now())
335336
const timeoutHandle = setTimeout(() => controller.abort(), timeoutMs)
336337
try {
337-
await fetch(`http://127.0.0.1:${this.options.port}/json/version`, { signal: controller.signal })
338+
const res = await fetch(`http://127.0.0.1:${this.options.port}/json/version`, { signal: controller.signal })
339+
if (res.ok) return
338340
} finally {
339341
clearTimeout(timeoutHandle)
340342
}
341-
return
343+
await new Promise(r => setTimeout(r, 20))
342344
} catch (e) {
343345
await new Promise(r => setTimeout(r, 20))
344346
}

‎lib/helper/Kitesurf.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class Kitesurf extends CDPBrowser {
9494
}
9595
const url = `${this.options.apiBase}/accounts/${this.options.accountId}/browser-run/devtools/browser?browser=kitesurf&keep_alive=${this.options.keepAlive}`
9696
const res = await fetch(url, { method: 'POST', headers: { Authorization: `Bearer ${this.options.apiToken}` } })
97+
if (!res.ok) throw new Error(`Failed to acquire Kitesurf session: ${res.status} ${res.statusText}`)
9798
const data = await res.json()
9899
if (!data || !data.webSocketDebuggerUrl) {
99100
throw new Error(`Could not acquire Kitesurf session: ${JSON.stringify(data).slice(0, 300)}`)

0 commit comments

Comments
 (0)