21 lines
828 B
TypeScript
21 lines
828 B
TypeScript
export function byteStringResponseHeaders(headers: Record<string, string | string[]> = {}) {
|
|
return Object.fromEntries(Object.entries(headers).map(([name, values]) => [
|
|
name,
|
|
(Array.isArray(values) ? values : [values]).map((value) => value.replace(/[^\x00-\xff]+/gu, encodeURIComponent)),
|
|
]))
|
|
}
|
|
|
|
export function prepareFetchHeaders(target: Electron.Session) {
|
|
// Electron net.fetch throws outside its promise when a response header contains Unicode.
|
|
target.webRequest.onHeadersReceived((details, callback) => {
|
|
callback({
|
|
responseHeaders: byteStringResponseHeaders(details.responseHeaders),
|
|
statusLine: asciiStatusLine(details.statusLine),
|
|
})
|
|
})
|
|
}
|
|
|
|
export function asciiStatusLine(line: string) {
|
|
return /[^\x20-\x7e]/.test(line) ? line.replace(/^(HTTP\/\S+\s+\d{3}).*$/, '$1 ') : line
|
|
}
|