refactor(desktop): use native fetch in dashboard-token
Node >=18 / Electron 40 ship fetch; the hand-rolled http/https.request plumbing buys nothing. AbortSignal.timeout replaces the socket timeout, protocol guard and >=400 rejection semantics preserved. 13/13 unit tests and the live web_server.py repro both green over the new transport.
This commit is contained in:
parent
cc726aad68
commit
b097d7b033
@ -7,47 +7,26 @@
|
|||||||
* probes still pass while /api/ws rejects the renderer's token.
|
* probes still pass while /api/ws rejects the renderer's token.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const http = require('node:http')
|
|
||||||
const https = require('node:https')
|
|
||||||
|
|
||||||
const DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 3_000
|
const DEFAULT_TOKEN_FETCH_TIMEOUT_MS = 3_000
|
||||||
|
|
||||||
function fetchPublicText(url, options = {}) {
|
async function fetchPublicText(url, options = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
const { protocol } = new URL(url)
|
||||||
let parsed
|
if (protocol !== 'http:' && protocol !== 'https:') {
|
||||||
try {
|
throw new Error(`Unsupported Hermes backend URL protocol: ${protocol}`)
|
||||||
parsed = new URL(url)
|
}
|
||||||
} catch (error) {
|
|
||||||
reject(new Error(`Invalid URL: ${error.message}`))
|
const timeoutMs = options.timeoutMs ?? DEFAULT_TOKEN_FETCH_TIMEOUT_MS
|
||||||
return
|
const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) }).catch(error => {
|
||||||
|
if (error.name === 'TimeoutError') {
|
||||||
|
throw new Error(`Timed out connecting to Hermes backend after ${timeoutMs}ms`)
|
||||||
}
|
}
|
||||||
|
throw error
|
||||||
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
||||||
reject(new Error(`Unsupported Hermes backend URL protocol: ${parsed.protocol}`))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = parsed.protocol === 'https:' ? https : http
|
|
||||||
const timeoutMs = options.timeoutMs ?? DEFAULT_TOKEN_FETCH_TIMEOUT_MS
|
|
||||||
const req = client.request(parsed, { method: options.method || 'GET' }, res => {
|
|
||||||
const chunks = []
|
|
||||||
res.on('data', chunk => chunks.push(chunk))
|
|
||||||
res.on('end', () => {
|
|
||||||
const text = Buffer.concat(chunks).toString('utf8')
|
|
||||||
if ((res.statusCode || 500) >= 400) {
|
|
||||||
reject(new Error(`${res.statusCode}: ${text || res.statusMessage}`))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
resolve(text)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
req.on('error', reject)
|
|
||||||
req.setTimeout(timeoutMs, () => {
|
|
||||||
req.destroy(new Error(`Timed out connecting to Hermes backend after ${timeoutMs}ms`))
|
|
||||||
})
|
|
||||||
req.end()
|
|
||||||
})
|
})
|
||||||
|
const text = await res.text()
|
||||||
|
|
||||||
|
if (!res.ok) throw new Error(`${res.status}: ${text || res.statusText}`)
|
||||||
|
|
||||||
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractInjectedDashboardToken(html) {
|
function extractInjectedDashboardToken(html) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user