forked from CITKParus/P8-ExchangeService
Рефакторинг и оптимизация обработки заголовков, которые нельзя объединять
This commit is contained in:
parent
bda18e40ec
commit
860d7708e9
@ -18,8 +18,22 @@ const { Socket } = require("net"); //Встроенная поддержка с
|
||||
|
||||
//Таймаут по умолчанию
|
||||
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-запроса
|
||||
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
|
||||
const buildHeadersFromRawPairs = rawPairs => {
|
||||
const headers = {};
|
||||
@ -210,10 +237,7 @@ const buildHeadersFromFetchHeaders = responseHeaders => {
|
||||
rawHeaders.push(name, value);
|
||||
}
|
||||
//Обработка некомбинируемых заголовков
|
||||
if (NON_COMBINABLE_HEADERS.has("set-cookie") && typeof responseHeaders.getSetCookie === "function") {
|
||||
const setCookieValues = responseHeaders.getSetCookie();
|
||||
applyHeaderValues(headers, rawHeaders, "set-cookie", setCookieValues);
|
||||
}
|
||||
applyNonCombinableFetchHeaders(headers, rawHeaders, responseHeaders);
|
||||
return {
|
||||
headers,
|
||||
rawHeaders
|
||||
@ -266,13 +290,14 @@ const processResponse = (result, options) => {
|
||||
//Формируем результат в зависимости от "resolveWithFullResponse" (это параметр из "Request": true - отвечать с заголовком, false - отвечать сразу телом)
|
||||
if (options.resolveWithFullResponse) {
|
||||
//Просили ответить полным ответом
|
||||
const fullResponse = { ...result };
|
||||
fullResponse.statusCode = result.statusCode;
|
||||
fullResponse.statusMessage = result.statusMessage || "";
|
||||
fullResponse.headers = result.headers;
|
||||
fullResponse.body = processedBody;
|
||||
fullResponse.url = result.url;
|
||||
return fullResponse;
|
||||
return {
|
||||
...result,
|
||||
statusCode: result.statusCode,
|
||||
statusMessage: result.statusMessage || "",
|
||||
headers: result.headers,
|
||||
body: processedBody,
|
||||
url: result.url
|
||||
};
|
||||
} else {
|
||||
//Просили только тело
|
||||
return processedBody;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user