Рефакторинг и оптимизация обработки заголовков, которые нельзя объединять
This commit is contained in:
parent
bda18e40ec
commit
860d7708e9
@ -18,8 +18,22 @@ const { Socket } = require("net"); //Встроенная поддержка с
|
|||||||
|
|
||||||
//Таймаут по умолчанию
|
//Таймаут по умолчанию
|
||||||
const DEFAULT_TIMEOUT = 30000;
|
const DEFAULT_TIMEOUT = 30000;
|
||||||
|
|
||||||
|
//Считывание всех Set-Cookie из fetch-ответа
|
||||||
|
const getFetchSetCookieValues = responseHeaders => {
|
||||||
|
if (typeof responseHeaders.getSetCookie === "function") {
|
||||||
|
return responseHeaders.getSetCookie();
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
//Заголовки, которые нельзя объединять
|
//Заголовки, которые нельзя объединять
|
||||||
const NON_COMBINABLE_HEADERS = new Set(["set-cookie"]);
|
const NON_COMBINABLE_HEADERS = [
|
||||||
|
{
|
||||||
|
headerName: "set-cookie",
|
||||||
|
getValues: getFetchSetCookieValues
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
//Ошибка HTTP-запроса
|
//Ошибка HTTP-запроса
|
||||||
class HttpError extends Error {
|
class HttpError extends Error {
|
||||||
@ -181,6 +195,19 @@ const applyHeaderValues = (headers, rawHeaders, headerName, values) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//Применение правил обработки некомбинируемых заголовков fetch-ответа
|
||||||
|
const applyNonCombinableFetchHeaders = (headers, rawHeaders, responseHeaders) => {
|
||||||
|
for (const rule of NON_COMBINABLE_HEADERS) {
|
||||||
|
if (!rule || typeof rule !== "object") continue;
|
||||||
|
const headerName = String(rule.headerName || "")
|
||||||
|
.trim()
|
||||||
|
.toLowerCase();
|
||||||
|
if (!headerName || typeof rule.getValues !== "function") continue;
|
||||||
|
const values = rule.getValues(responseHeaders);
|
||||||
|
applyHeaderValues(headers, rawHeaders, headerName, values);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//Формирование заголовков из массива rawHeaders/rawTrailers
|
//Формирование заголовков из массива rawHeaders/rawTrailers
|
||||||
const buildHeadersFromRawPairs = rawPairs => {
|
const buildHeadersFromRawPairs = rawPairs => {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
@ -210,10 +237,7 @@ const buildHeadersFromFetchHeaders = responseHeaders => {
|
|||||||
rawHeaders.push(name, value);
|
rawHeaders.push(name, value);
|
||||||
}
|
}
|
||||||
//Обработка некомбинируемых заголовков
|
//Обработка некомбинируемых заголовков
|
||||||
if (NON_COMBINABLE_HEADERS.has("set-cookie") && typeof responseHeaders.getSetCookie === "function") {
|
applyNonCombinableFetchHeaders(headers, rawHeaders, responseHeaders);
|
||||||
const setCookieValues = responseHeaders.getSetCookie();
|
|
||||||
applyHeaderValues(headers, rawHeaders, "set-cookie", setCookieValues);
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
headers,
|
headers,
|
||||||
rawHeaders
|
rawHeaders
|
||||||
@ -266,13 +290,14 @@ const processResponse = (result, options) => {
|
|||||||
//Формируем результат в зависимости от "resolveWithFullResponse" (это параметр из "Request": true - отвечать с заголовком, false - отвечать сразу телом)
|
//Формируем результат в зависимости от "resolveWithFullResponse" (это параметр из "Request": true - отвечать с заголовком, false - отвечать сразу телом)
|
||||||
if (options.resolveWithFullResponse) {
|
if (options.resolveWithFullResponse) {
|
||||||
//Просили ответить полным ответом
|
//Просили ответить полным ответом
|
||||||
const fullResponse = { ...result };
|
return {
|
||||||
fullResponse.statusCode = result.statusCode;
|
...result,
|
||||||
fullResponse.statusMessage = result.statusMessage || "";
|
statusCode: result.statusCode,
|
||||||
fullResponse.headers = result.headers;
|
statusMessage: result.statusMessage || "",
|
||||||
fullResponse.body = processedBody;
|
headers: result.headers,
|
||||||
fullResponse.url = result.url;
|
body: processedBody,
|
||||||
return fullResponse;
|
url: result.url
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
//Просили только тело
|
//Просили только тело
|
||||||
return processedBody;
|
return processedBody;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user