headers-polyfill
Advanced tools
Comparing version 3.2.2 to 3.2.3
1230
lib/index.js
@@ -0,1 +1,168 @@ | ||
function _array_like_to_array(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _array_with_holes(arr) { | ||
if (Array.isArray(arr)) return arr; | ||
} | ||
function _class_call_check(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperties(target, props) { | ||
for(var i = 0; i < props.length; i++){ | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function _create_class(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _instanceof(left, right) { | ||
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { | ||
return !!right[Symbol.hasInstance](left); | ||
} else { | ||
return left instanceof right; | ||
} | ||
} | ||
function _iterable_to_array_limit(arr, i) { | ||
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; | ||
if (_i == null) return; | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _s, _e; | ||
try { | ||
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally{ | ||
try { | ||
if (!_n && _i["return"] != null) _i["return"](); | ||
} finally{ | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function _non_iterable_rest() { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
function _sliced_to_array(arr, i) { | ||
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); | ||
} | ||
function _unsupported_iterable_to_array(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _array_like_to_array(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(n); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); | ||
} | ||
function _ts_generator(thisArg, body) { | ||
var f, y, t, g, _ = { | ||
label: 0, | ||
sent: function() { | ||
if (t[0] & 1) throw t[1]; | ||
return t[1]; | ||
}, | ||
trys: [], | ||
ops: [] | ||
}; | ||
return g = { | ||
next: verb(0), | ||
"throw": verb(1), | ||
"return": verb(2) | ||
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() { | ||
return this; | ||
}), g; | ||
function verb(n) { | ||
return function(v) { | ||
return step([ | ||
n, | ||
v | ||
]); | ||
}; | ||
} | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while(_)try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [ | ||
op[0] & 2, | ||
t.value | ||
]; | ||
switch(op[0]){ | ||
case 0: | ||
case 1: | ||
t = op; | ||
break; | ||
case 4: | ||
_.label++; | ||
return { | ||
value: op[1], | ||
done: false | ||
}; | ||
case 5: | ||
_.label++; | ||
y = op[1]; | ||
op = [ | ||
0 | ||
]; | ||
continue; | ||
case 7: | ||
op = _.ops.pop(); | ||
_.trys.pop(); | ||
continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { | ||
_ = 0; | ||
continue; | ||
} | ||
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) { | ||
_.label = op[1]; | ||
break; | ||
} | ||
if (op[0] === 6 && _.label < t[1]) { | ||
_.label = t[1]; | ||
t = op; | ||
break; | ||
} | ||
if (t && _.label < t[2]) { | ||
_.label = t[2]; | ||
_.ops.push(op); | ||
break; | ||
} | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); | ||
continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { | ||
op = [ | ||
6, | ||
e | ||
]; | ||
y = 0; | ||
} finally{ | ||
f = t = 0; | ||
} | ||
if (op[0] & 5) throw op[1]; | ||
return { | ||
value: op[0] ? op[1] : void 0, | ||
done: true | ||
}; | ||
} | ||
} | ||
var __create = Object.create; | ||
@@ -7,230 +174,279 @@ var __defProp = Object.defineProperty; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
var __commonJS = function(cb, mod) { | ||
return function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { | ||
exports: {} | ||
}).exports, mod), mod.exports; | ||
}; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
var __export = function(target, all) { | ||
for(var name in all)__defProp(target, name, { | ||
get: all[name], | ||
enumerable: true | ||
}); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
var __copyProps = function(to, from, except, desc) { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
try { | ||
var _loop = function() { | ||
var key = _step.value; | ||
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { | ||
get: function() { | ||
return from[key]; | ||
}, | ||
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable | ||
}); | ||
}; | ||
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop(); | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally{ | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __toESM = function(mod, isNodeMode, target) { | ||
return target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { | ||
value: mod, | ||
enumerable: true | ||
}) : target, mod); | ||
}; | ||
var __toCommonJS = function(mod) { | ||
return __copyProps(__defProp({}, "__esModule", { | ||
value: true | ||
}), mod); | ||
}; | ||
// node_modules/set-cookie-parser/lib/set-cookie.js | ||
var require_set_cookie = __commonJS({ | ||
"node_modules/set-cookie-parser/lib/set-cookie.js"(exports, module2) { | ||
"use strict"; | ||
var defaultParseOptions = { | ||
decodeValues: true, | ||
map: false, | ||
silent: false | ||
}; | ||
function isNonEmptyString(str) { | ||
return typeof str === "string" && !!str.trim(); | ||
} | ||
function parseString(setCookieValue, options) { | ||
var parts = setCookieValue.split(";").filter(isNonEmptyString); | ||
var nameValuePairStr = parts.shift(); | ||
var parsed = parseNameValuePair(nameValuePairStr); | ||
var name = parsed.name; | ||
var value = parsed.value; | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
try { | ||
value = options.decodeValues ? decodeURIComponent(value) : value; | ||
} catch (e) { | ||
console.error( | ||
"set-cookie-parser encountered an error while decoding a cookie with value '" + value + "'. Set options.decodeValues to false to disable this feature.", | ||
e | ||
); | ||
} | ||
var cookie = { | ||
name, | ||
value | ||
}; | ||
parts.forEach(function(part) { | ||
var sides = part.split("="); | ||
var key = sides.shift().trimLeft().toLowerCase(); | ||
var value2 = sides.join("="); | ||
if (key === "expires") { | ||
cookie.expires = new Date(value2); | ||
} else if (key === "max-age") { | ||
cookie.maxAge = parseInt(value2, 10); | ||
} else if (key === "secure") { | ||
cookie.secure = true; | ||
} else if (key === "httponly") { | ||
cookie.httpOnly = true; | ||
} else if (key === "samesite") { | ||
cookie.sameSite = value2; | ||
} else { | ||
cookie[key] = value2; | ||
} | ||
}); | ||
return cookie; | ||
} | ||
function parseNameValuePair(nameValuePairStr) { | ||
var name = ""; | ||
var value = ""; | ||
var nameValueArr = nameValuePairStr.split("="); | ||
if (nameValueArr.length > 1) { | ||
name = nameValueArr.shift(); | ||
value = nameValueArr.join("="); | ||
} else { | ||
value = nameValuePairStr; | ||
} | ||
return { name, value }; | ||
} | ||
function parse(input, options) { | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
if (!input) { | ||
if (!options.map) { | ||
return []; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
if (input.headers) { | ||
if (typeof input.headers.getSetCookie === "function") { | ||
input = input.headers.getSetCookie(); | ||
} else if (input.headers["set-cookie"]) { | ||
input = input.headers["set-cookie"]; | ||
} else { | ||
var sch = input.headers[Object.keys(input.headers).find(function(key) { | ||
return key.toLowerCase() === "set-cookie"; | ||
})]; | ||
if (!sch && input.headers.cookie && !options.silent) { | ||
console.warn( | ||
"Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning." | ||
); | ||
} | ||
input = sch; | ||
} | ||
} | ||
if (!Array.isArray(input)) { | ||
input = [input]; | ||
} | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
if (!options.map) { | ||
return input.filter(isNonEmptyString).map(function(str) { | ||
return parseString(str, options); | ||
}); | ||
} else { | ||
var cookies = {}; | ||
return input.filter(isNonEmptyString).reduce(function(cookies2, str) { | ||
var cookie = parseString(str, options); | ||
cookies2[cookie.name] = cookie; | ||
return cookies2; | ||
}, cookies); | ||
} | ||
} | ||
function splitCookiesString2(cookiesString) { | ||
if (Array.isArray(cookiesString)) { | ||
return cookiesString; | ||
} | ||
if (typeof cookiesString !== "string") { | ||
return []; | ||
} | ||
var cookiesStrings = []; | ||
var pos = 0; | ||
var start; | ||
var ch; | ||
var lastComma; | ||
var nextStart; | ||
var cookiesSeparatorFound; | ||
function skipWhitespace() { | ||
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { | ||
pos += 1; | ||
} | ||
return pos < cookiesString.length; | ||
} | ||
function notSpecialChar() { | ||
ch = cookiesString.charAt(pos); | ||
return ch !== "=" && ch !== ";" && ch !== ","; | ||
} | ||
while (pos < cookiesString.length) { | ||
start = pos; | ||
cookiesSeparatorFound = false; | ||
while (skipWhitespace()) { | ||
ch = cookiesString.charAt(pos); | ||
if (ch === ",") { | ||
lastComma = pos; | ||
pos += 1; | ||
skipWhitespace(); | ||
nextStart = pos; | ||
while (pos < cookiesString.length && notSpecialChar()) { | ||
pos += 1; | ||
"node_modules/set-cookie-parser/lib/set-cookie.js": function(exports, module2) { | ||
"use strict"; | ||
var isNonEmptyString = function isNonEmptyString(str) { | ||
return typeof str === "string" && !!str.trim(); | ||
}; | ||
var parseString = function parseString(setCookieValue, options) { | ||
var parts = setCookieValue.split(";").filter(isNonEmptyString); | ||
var nameValuePairStr = parts.shift(); | ||
var parsed = parseNameValuePair(nameValuePairStr); | ||
var name = parsed.name; | ||
var value = parsed.value; | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
try { | ||
value = options.decodeValues ? decodeURIComponent(value) : value; | ||
} catch (e) { | ||
console.error("set-cookie-parser encountered an error while decoding a cookie with value '" + value + "'. Set options.decodeValues to false to disable this feature.", e); | ||
} | ||
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") { | ||
cookiesSeparatorFound = true; | ||
pos = nextStart; | ||
cookiesStrings.push(cookiesString.substring(start, lastComma)); | ||
start = pos; | ||
var cookie = { | ||
name: name, | ||
value: value | ||
}; | ||
parts.forEach(function(part) { | ||
var sides = part.split("="); | ||
var key = sides.shift().trimLeft().toLowerCase(); | ||
var value2 = sides.join("="); | ||
if (key === "expires") { | ||
cookie.expires = new Date(value2); | ||
} else if (key === "max-age") { | ||
cookie.maxAge = parseInt(value2, 10); | ||
} else if (key === "secure") { | ||
cookie.secure = true; | ||
} else if (key === "httponly") { | ||
cookie.httpOnly = true; | ||
} else if (key === "samesite") { | ||
cookie.sameSite = value2; | ||
} else { | ||
cookie[key] = value2; | ||
} | ||
}); | ||
return cookie; | ||
}; | ||
var parseNameValuePair = function parseNameValuePair(nameValuePairStr) { | ||
var name = ""; | ||
var value = ""; | ||
var nameValueArr = nameValuePairStr.split("="); | ||
if (nameValueArr.length > 1) { | ||
name = nameValueArr.shift(); | ||
value = nameValueArr.join("="); | ||
} else { | ||
pos = lastComma + 1; | ||
value = nameValuePairStr; | ||
} | ||
} else { | ||
pos += 1; | ||
} | ||
} | ||
if (!cookiesSeparatorFound || pos >= cookiesString.length) { | ||
cookiesStrings.push(cookiesString.substring(start, cookiesString.length)); | ||
} | ||
} | ||
return cookiesStrings; | ||
return { | ||
name: name, | ||
value: value | ||
}; | ||
}; | ||
var parse = function parse(input, options) { | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
if (!input) { | ||
if (!options.map) { | ||
return []; | ||
} else { | ||
return {}; | ||
} | ||
} | ||
if (input.headers) { | ||
if (typeof input.headers.getSetCookie === "function") { | ||
input = input.headers.getSetCookie(); | ||
} else if (input.headers["set-cookie"]) { | ||
input = input.headers["set-cookie"]; | ||
} else { | ||
var sch = input.headers[Object.keys(input.headers).find(function(key) { | ||
return key.toLowerCase() === "set-cookie"; | ||
})]; | ||
if (!sch && input.headers.cookie && !options.silent) { | ||
console.warn("Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning."); | ||
} | ||
input = sch; | ||
} | ||
} | ||
if (!Array.isArray(input)) { | ||
input = [ | ||
input | ||
]; | ||
} | ||
options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; | ||
if (!options.map) { | ||
return input.filter(isNonEmptyString).map(function(str) { | ||
return parseString(str, options); | ||
}); | ||
} else { | ||
var cookies = {}; | ||
return input.filter(isNonEmptyString).reduce(function(cookies2, str) { | ||
var cookie = parseString(str, options); | ||
cookies2[cookie.name] = cookie; | ||
return cookies2; | ||
}, cookies); | ||
} | ||
}; | ||
var splitCookiesString2 = function splitCookiesString2(cookiesString) { | ||
if (Array.isArray(cookiesString)) { | ||
return cookiesString; | ||
} | ||
if (typeof cookiesString !== "string") { | ||
return []; | ||
} | ||
var cookiesStrings = []; | ||
var pos = 0; | ||
var start; | ||
var ch; | ||
var lastComma; | ||
var nextStart; | ||
var cookiesSeparatorFound; | ||
function skipWhitespace() { | ||
while(pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))){ | ||
pos += 1; | ||
} | ||
return pos < cookiesString.length; | ||
} | ||
function notSpecialChar() { | ||
ch = cookiesString.charAt(pos); | ||
return ch !== "=" && ch !== ";" && ch !== ","; | ||
} | ||
while(pos < cookiesString.length){ | ||
start = pos; | ||
cookiesSeparatorFound = false; | ||
while(skipWhitespace()){ | ||
ch = cookiesString.charAt(pos); | ||
if (ch === ",") { | ||
lastComma = pos; | ||
pos += 1; | ||
skipWhitespace(); | ||
nextStart = pos; | ||
while(pos < cookiesString.length && notSpecialChar()){ | ||
pos += 1; | ||
} | ||
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") { | ||
cookiesSeparatorFound = true; | ||
pos = nextStart; | ||
cookiesStrings.push(cookiesString.substring(start, lastComma)); | ||
start = pos; | ||
} else { | ||
pos = lastComma + 1; | ||
} | ||
} else { | ||
pos += 1; | ||
} | ||
} | ||
if (!cookiesSeparatorFound || pos >= cookiesString.length) { | ||
cookiesStrings.push(cookiesString.substring(start, cookiesString.length)); | ||
} | ||
} | ||
return cookiesStrings; | ||
}; | ||
var defaultParseOptions = { | ||
decodeValues: true, | ||
map: false, | ||
silent: false | ||
}; | ||
module2.exports = parse; | ||
module2.exports.parse = parse; | ||
module2.exports.parseString = parseString; | ||
module2.exports.splitCookiesString = splitCookiesString2; | ||
} | ||
module2.exports = parse; | ||
module2.exports.parse = parse; | ||
module2.exports.parseString = parseString; | ||
module2.exports.splitCookiesString = splitCookiesString2; | ||
} | ||
}); | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
Headers: () => HeadersPolyfill, | ||
flattenHeadersList: () => flattenHeadersList, | ||
flattenHeadersObject: () => flattenHeadersObject, | ||
headersToList: () => headersToList, | ||
headersToObject: () => headersToObject, | ||
headersToString: () => headersToString, | ||
listToHeaders: () => listToHeaders, | ||
objectToHeaders: () => objectToHeaders, | ||
reduceHeadersObject: () => reduceHeadersObject, | ||
stringToHeaders: () => stringToHeaders | ||
Headers: function() { | ||
return HeadersPolyfill; | ||
}, | ||
flattenHeadersList: function() { | ||
return flattenHeadersList; | ||
}, | ||
flattenHeadersObject: function() { | ||
return flattenHeadersObject; | ||
}, | ||
headersToList: function() { | ||
return headersToList; | ||
}, | ||
headersToObject: function() { | ||
return headersToObject; | ||
}, | ||
headersToString: function() { | ||
return headersToString; | ||
}, | ||
listToHeaders: function() { | ||
return listToHeaders; | ||
}, | ||
objectToHeaders: function() { | ||
return objectToHeaders; | ||
}, | ||
reduceHeadersObject: function() { | ||
return reduceHeadersObject; | ||
}, | ||
stringToHeaders: function() { | ||
return stringToHeaders; | ||
} | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/Headers.ts | ||
var import_set_cookie_parser = __toESM(require_set_cookie()); | ||
// src/utils/normalizeHeaderName.ts | ||
var HEADERS_INVALID_CHARACTERS = /[^a-z0-9\-#$%&'*+.^_`|~]/i; | ||
function normalizeHeaderName(name) { | ||
if (typeof name !== "string") { | ||
name = String(name); | ||
} | ||
if (HEADERS_INVALID_CHARACTERS.test(name) || name.trim() === "") { | ||
throw new TypeError("Invalid character in header field name"); | ||
} | ||
return name.toLowerCase(); | ||
if (typeof name !== "string") { | ||
name = String(name); | ||
} | ||
if (HEADERS_INVALID_CHARACTERS.test(name) || name.trim() === "") { | ||
throw new TypeError("Invalid character in header field name"); | ||
} | ||
return name.toLowerCase(); | ||
} | ||
// src/utils/normalizeHeaderValue.ts | ||
function normalizeHeaderValue(value) { | ||
if (typeof value !== "string") { | ||
value = String(value); | ||
} | ||
return value; | ||
if (typeof value !== "string") { | ||
value = String(value); | ||
} | ||
return value; | ||
} | ||
// src/Headers.ts | ||
@@ -241,210 +457,464 @@ var NORMALIZED_HEADERS = Symbol("normalizedHeaders"); | ||
var _a, _b; | ||
var HeadersPolyfill = class { | ||
constructor(init) { | ||
this[_a] = {}; | ||
this[_b] = /* @__PURE__ */ new Map(); | ||
if (["Headers", "HeadersPolyfill"].includes(init == null ? void 0 : init.constructor.name) || init instanceof HeadersPolyfill) { | ||
const initialHeaders = init; | ||
initialHeaders.forEach((value, name) => { | ||
this.append(name, value); | ||
}, this); | ||
} else if (Array.isArray(init)) { | ||
init.forEach(([name, value]) => { | ||
this.append( | ||
name, | ||
Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value | ||
); | ||
}); | ||
} else if (init) { | ||
Object.getOwnPropertyNames(init).forEach((name) => { | ||
const value = init[name]; | ||
this.append( | ||
name, | ||
Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value | ||
); | ||
}); | ||
var HeadersPolyfill = /*#__PURE__*/ function() { | ||
"use strict"; | ||
function HeadersPolyfill1(init) { | ||
var _this = this; | ||
_class_call_check(this, HeadersPolyfill1); | ||
var _init; | ||
this[_a] = {}; | ||
this[_b] = /* @__PURE__ */ new Map(); | ||
if ([ | ||
"Headers", | ||
"HeadersPolyfill" | ||
].includes((_init = init) === null || _init === void 0 ? void 0 : _init.constructor.name) || _instanceof(init, HeadersPolyfill)) { | ||
var initialHeaders = init; | ||
initialHeaders.forEach(function(value, name) { | ||
_this.append(name, value); | ||
}, this); | ||
} else if (Array.isArray(init)) { | ||
init.forEach(function(param) { | ||
var _param = _sliced_to_array(param, 2), name = _param[0], value = _param[1]; | ||
_this.append(name, Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value); | ||
}); | ||
} else if (init) { | ||
Object.getOwnPropertyNames(init).forEach(function(name) { | ||
var value = init[name]; | ||
_this.append(name, Array.isArray(value) ? value.join(HEADER_VALUE_DELIMITER) : value); | ||
}); | ||
} | ||
} | ||
} | ||
[(_a = NORMALIZED_HEADERS, _b = RAW_HEADER_NAMES, Symbol.iterator)]() { | ||
return this.entries(); | ||
} | ||
*keys() { | ||
for (const name of Object.keys(this[NORMALIZED_HEADERS])) { | ||
yield name; | ||
} | ||
} | ||
*values() { | ||
for (const value of Object.values(this[NORMALIZED_HEADERS])) { | ||
yield value; | ||
} | ||
} | ||
*entries() { | ||
for (const name of Object.keys(this[NORMALIZED_HEADERS])) { | ||
yield [name, this.get(name)]; | ||
} | ||
} | ||
get(name) { | ||
return this[NORMALIZED_HEADERS][normalizeHeaderName(name)] ?? null; | ||
} | ||
set(name, value) { | ||
const normalizedName = normalizeHeaderName(name); | ||
this[NORMALIZED_HEADERS][normalizedName] = normalizeHeaderValue(value); | ||
this[RAW_HEADER_NAMES].set(normalizedName, name); | ||
} | ||
append(name, value) { | ||
const normalizedName = normalizeHeaderName(name); | ||
let resolvedValue = this.has(normalizedName) ? `${this.get(normalizedName)}, ${value}` : value; | ||
this.set(name, resolvedValue); | ||
} | ||
delete(name) { | ||
if (!this.has(name)) { | ||
return; | ||
} | ||
const normalizedName = normalizeHeaderName(name); | ||
delete this[NORMALIZED_HEADERS][normalizedName]; | ||
this[RAW_HEADER_NAMES].delete(normalizedName); | ||
} | ||
all() { | ||
return this[NORMALIZED_HEADERS]; | ||
} | ||
raw() { | ||
const rawHeaders = {}; | ||
for (const [name, value] of this.entries()) { | ||
rawHeaders[this[RAW_HEADER_NAMES].get(name)] = value; | ||
} | ||
return rawHeaders; | ||
} | ||
has(name) { | ||
return this[NORMALIZED_HEADERS].hasOwnProperty(normalizeHeaderName(name)); | ||
} | ||
forEach(callback, thisArg) { | ||
for (const name in this[NORMALIZED_HEADERS]) { | ||
if (this[NORMALIZED_HEADERS].hasOwnProperty(name)) { | ||
callback.call(thisArg, this[NORMALIZED_HEADERS][name], name, this); | ||
} | ||
} | ||
} | ||
getSetCookie() { | ||
const setCookieHeader = this.get("set-cookie"); | ||
if (setCookieHeader === null) { | ||
return []; | ||
} | ||
if (setCookieHeader === "") { | ||
return [""]; | ||
} | ||
return (0, import_set_cookie_parser.splitCookiesString)(setCookieHeader); | ||
} | ||
}; | ||
_create_class(HeadersPolyfill1, [ | ||
{ | ||
key: (_a = NORMALIZED_HEADERS, _b = RAW_HEADER_NAMES, Symbol.iterator), | ||
value: function value() { | ||
return this.entries(); | ||
} | ||
}, | ||
{ | ||
key: "keys", | ||
value: function keys() { | ||
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, name, err; | ||
return _ts_generator(this, function(_state) { | ||
switch(_state.label){ | ||
case 0: | ||
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
_state.label = 1; | ||
case 1: | ||
_state.trys.push([ | ||
1, | ||
6, | ||
7, | ||
8 | ||
]); | ||
_iterator = Object.keys(this[NORMALIZED_HEADERS])[Symbol.iterator](); | ||
_state.label = 2; | ||
case 2: | ||
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [ | ||
3, | ||
5 | ||
]; | ||
name = _step.value; | ||
return [ | ||
4, | ||
name | ||
]; | ||
case 3: | ||
_state.sent(); | ||
_state.label = 4; | ||
case 4: | ||
_iteratorNormalCompletion = true; | ||
return [ | ||
3, | ||
2 | ||
]; | ||
case 5: | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 6: | ||
err = _state.sent(); | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 7: | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
return [ | ||
7 | ||
]; | ||
case 8: | ||
return [ | ||
2 | ||
]; | ||
} | ||
}); | ||
} | ||
}, | ||
{ | ||
key: "values", | ||
value: function values() { | ||
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, value, err; | ||
return _ts_generator(this, function(_state) { | ||
switch(_state.label){ | ||
case 0: | ||
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
_state.label = 1; | ||
case 1: | ||
_state.trys.push([ | ||
1, | ||
6, | ||
7, | ||
8 | ||
]); | ||
_iterator = Object.values(this[NORMALIZED_HEADERS])[Symbol.iterator](); | ||
_state.label = 2; | ||
case 2: | ||
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [ | ||
3, | ||
5 | ||
]; | ||
value = _step.value; | ||
return [ | ||
4, | ||
value | ||
]; | ||
case 3: | ||
_state.sent(); | ||
_state.label = 4; | ||
case 4: | ||
_iteratorNormalCompletion = true; | ||
return [ | ||
3, | ||
2 | ||
]; | ||
case 5: | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 6: | ||
err = _state.sent(); | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 7: | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
return [ | ||
7 | ||
]; | ||
case 8: | ||
return [ | ||
2 | ||
]; | ||
} | ||
}); | ||
} | ||
}, | ||
{ | ||
key: "entries", | ||
value: function entries() { | ||
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, name, err; | ||
return _ts_generator(this, function(_state) { | ||
switch(_state.label){ | ||
case 0: | ||
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
_state.label = 1; | ||
case 1: | ||
_state.trys.push([ | ||
1, | ||
6, | ||
7, | ||
8 | ||
]); | ||
_iterator = Object.keys(this[NORMALIZED_HEADERS])[Symbol.iterator](); | ||
_state.label = 2; | ||
case 2: | ||
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [ | ||
3, | ||
5 | ||
]; | ||
name = _step.value; | ||
return [ | ||
4, | ||
[ | ||
name, | ||
this.get(name) | ||
] | ||
]; | ||
case 3: | ||
_state.sent(); | ||
_state.label = 4; | ||
case 4: | ||
_iteratorNormalCompletion = true; | ||
return [ | ||
3, | ||
2 | ||
]; | ||
case 5: | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 6: | ||
err = _state.sent(); | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
return [ | ||
3, | ||
8 | ||
]; | ||
case 7: | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
return [ | ||
7 | ||
]; | ||
case 8: | ||
return [ | ||
2 | ||
]; | ||
} | ||
}); | ||
} | ||
}, | ||
{ | ||
key: "get", | ||
value: function get(name) { | ||
var _this_NORMALIZED_HEADERS_normalizeHeaderName; | ||
return (_this_NORMALIZED_HEADERS_normalizeHeaderName = this[NORMALIZED_HEADERS][normalizeHeaderName(name)]) !== null && _this_NORMALIZED_HEADERS_normalizeHeaderName !== void 0 ? _this_NORMALIZED_HEADERS_normalizeHeaderName : null; | ||
} | ||
}, | ||
{ | ||
key: "set", | ||
value: function set(name, value) { | ||
var normalizedName = normalizeHeaderName(name); | ||
this[NORMALIZED_HEADERS][normalizedName] = normalizeHeaderValue(value); | ||
this[RAW_HEADER_NAMES].set(normalizedName, name); | ||
} | ||
}, | ||
{ | ||
key: "append", | ||
value: function append(name, value) { | ||
var normalizedName = normalizeHeaderName(name); | ||
var resolvedValue = this.has(normalizedName) ? "".concat(this.get(normalizedName), ", ").concat(value) : value; | ||
this.set(name, resolvedValue); | ||
} | ||
}, | ||
{ | ||
key: "delete", | ||
value: function _delete(name) { | ||
if (!this.has(name)) { | ||
return; | ||
} | ||
var normalizedName = normalizeHeaderName(name); | ||
delete this[NORMALIZED_HEADERS][normalizedName]; | ||
this[RAW_HEADER_NAMES].delete(normalizedName); | ||
} | ||
}, | ||
{ | ||
key: "all", | ||
value: function all() { | ||
return this[NORMALIZED_HEADERS]; | ||
} | ||
}, | ||
{ | ||
key: "raw", | ||
value: function raw() { | ||
var rawHeaders = {}; | ||
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; | ||
try { | ||
for(var _iterator = this.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ | ||
var _step_value = _sliced_to_array(_step.value, 2), name = _step_value[0], value = _step_value[1]; | ||
rawHeaders[this[RAW_HEADER_NAMES].get(name)] = value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally{ | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally{ | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return rawHeaders; | ||
} | ||
}, | ||
{ | ||
key: "has", | ||
value: function has(name) { | ||
return this[NORMALIZED_HEADERS].hasOwnProperty(normalizeHeaderName(name)); | ||
} | ||
}, | ||
{ | ||
key: "forEach", | ||
value: function forEach(callback, thisArg) { | ||
for(var name in this[NORMALIZED_HEADERS]){ | ||
if (this[NORMALIZED_HEADERS].hasOwnProperty(name)) { | ||
callback.call(thisArg, this[NORMALIZED_HEADERS][name], name, this); | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
key: "getSetCookie", | ||
value: function getSetCookie() { | ||
var setCookieHeader = this.get("set-cookie"); | ||
if (setCookieHeader === null) { | ||
return []; | ||
} | ||
if (setCookieHeader === "") { | ||
return [ | ||
"" | ||
]; | ||
} | ||
return (0, import_set_cookie_parser.splitCookiesString)(setCookieHeader); | ||
} | ||
} | ||
]); | ||
return HeadersPolyfill1; | ||
}(); | ||
// src/transformers/headersToList.ts | ||
function headersToList(headers) { | ||
const headersList = []; | ||
headers.forEach((value, name) => { | ||
const resolvedValue = value.includes(",") ? value.split(",").map((value2) => value2.trim()) : value; | ||
headersList.push([name, resolvedValue]); | ||
}); | ||
return headersList; | ||
var headersList = []; | ||
headers.forEach(function(value, name) { | ||
var resolvedValue = value.includes(",") ? value.split(",").map(function(value2) { | ||
return value2.trim(); | ||
}) : value; | ||
headersList.push([ | ||
name, | ||
resolvedValue | ||
]); | ||
}); | ||
return headersList; | ||
} | ||
// src/transformers/headersToString.ts | ||
function headersToString(headers) { | ||
const list = headersToList(headers); | ||
const lines = list.map(([name, value]) => { | ||
const values = [].concat(value); | ||
return `${name}: ${values.join(", ")}`; | ||
}); | ||
return lines.join("\r\n"); | ||
var list = headersToList(headers); | ||
var lines = list.map(function(param) { | ||
var _param = _sliced_to_array(param, 2), name = _param[0], value = _param[1]; | ||
var values = [].concat(value); | ||
return "".concat(name, ": ").concat(values.join(", ")); | ||
}); | ||
return lines.join("\r\n"); | ||
} | ||
// src/transformers/headersToObject.ts | ||
var singleValueHeaders = ["user-agent"]; | ||
var singleValueHeaders = [ | ||
"user-agent" | ||
]; | ||
function headersToObject(headers) { | ||
const headersObject = {}; | ||
headers.forEach((value, name) => { | ||
const isMultiValue = !singleValueHeaders.includes(name.toLowerCase()) && value.includes(","); | ||
headersObject[name] = isMultiValue ? value.split(",").map((s) => s.trim()) : value; | ||
}); | ||
return headersObject; | ||
var headersObject = {}; | ||
headers.forEach(function(value, name) { | ||
var isMultiValue = !singleValueHeaders.includes(name.toLowerCase()) && value.includes(","); | ||
headersObject[name] = isMultiValue ? value.split(",").map(function(s) { | ||
return s.trim(); | ||
}) : value; | ||
}); | ||
return headersObject; | ||
} | ||
// src/transformers/stringToHeaders.ts | ||
function stringToHeaders(str) { | ||
const lines = str.trim().split(/[\r\n]+/); | ||
return lines.reduce((headers, line) => { | ||
if (line.trim() === "") { | ||
return headers; | ||
} | ||
const parts = line.split(": "); | ||
const name = parts.shift(); | ||
const value = parts.join(": "); | ||
headers.append(name, value); | ||
return headers; | ||
}, new HeadersPolyfill()); | ||
var lines = str.trim().split(/[\r\n]+/); | ||
return lines.reduce(function(headers, line) { | ||
if (line.trim() === "") { | ||
return headers; | ||
} | ||
var parts = line.split(": "); | ||
var name = parts.shift(); | ||
var value = parts.join(": "); | ||
headers.append(name, value); | ||
return headers; | ||
}, new HeadersPolyfill()); | ||
} | ||
// src/transformers/listToHeaders.ts | ||
function listToHeaders(list) { | ||
const headers = new HeadersPolyfill(); | ||
list.forEach(([name, value]) => { | ||
const values = [].concat(value); | ||
values.forEach((value2) => { | ||
headers.append(name, value2); | ||
var headers = new HeadersPolyfill(); | ||
list.forEach(function(param) { | ||
var _param = _sliced_to_array(param, 2), name = _param[0], value = _param[1]; | ||
var values = [].concat(value); | ||
values.forEach(function(value2) { | ||
headers.append(name, value2); | ||
}); | ||
}); | ||
}); | ||
return headers; | ||
return headers; | ||
} | ||
// src/transformers/reduceHeadersObject.ts | ||
function reduceHeadersObject(headers, reducer, initialState) { | ||
return Object.keys(headers).reduce((nextHeaders, name) => { | ||
return reducer(nextHeaders, name, headers[name]); | ||
}, initialState); | ||
return Object.keys(headers).reduce(function(nextHeaders, name) { | ||
return reducer(nextHeaders, name, headers[name]); | ||
}, initialState); | ||
} | ||
// src/transformers/objectToHeaders.ts | ||
function objectToHeaders(headersObject) { | ||
return reduceHeadersObject( | ||
headersObject, | ||
(headers, name, value) => { | ||
const values = [].concat(value).filter(Boolean); | ||
values.forEach((value2) => { | ||
headers.append(name, value2); | ||
}); | ||
return headers; | ||
}, | ||
new HeadersPolyfill() | ||
); | ||
return reduceHeadersObject(headersObject, function(headers, name, value) { | ||
var values = [].concat(value).filter(Boolean); | ||
values.forEach(function(value2) { | ||
headers.append(name, value2); | ||
}); | ||
return headers; | ||
}, new HeadersPolyfill()); | ||
} | ||
// src/transformers/flattenHeadersList.ts | ||
function flattenHeadersList(list) { | ||
return list.map(([name, values]) => { | ||
return [name, [].concat(values).join(", ")]; | ||
}); | ||
return list.map(function(param) { | ||
var _param = _sliced_to_array(param, 2), name = _param[0], values = _param[1]; | ||
return [ | ||
name, | ||
[].concat(values).join(", ") | ||
]; | ||
}); | ||
} | ||
// src/transformers/flattenHeadersObject.ts | ||
function flattenHeadersObject(headersObject) { | ||
return reduceHeadersObject( | ||
headersObject, | ||
(headers, name, value) => { | ||
headers[name] = [].concat(value).join(", "); | ||
return headers; | ||
}, | ||
{} | ||
); | ||
return reduceHeadersObject(headersObject, function(headers, name, value) { | ||
headers[name] = [].concat(value).join(", "); | ||
return headers; | ||
}, {}); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
Headers, | ||
flattenHeadersList, | ||
flattenHeadersObject, | ||
headersToList, | ||
headersToObject, | ||
headersToString, | ||
listToHeaders, | ||
objectToHeaders, | ||
reduceHeadersObject, | ||
stringToHeaders | ||
Headers: Headers, | ||
flattenHeadersList: flattenHeadersList, | ||
flattenHeadersObject: flattenHeadersObject, | ||
headersToList: headersToList, | ||
headersToObject: headersToObject, | ||
headersToString: headersToString, | ||
listToHeaders: listToHeaders, | ||
objectToHeaders: objectToHeaders, | ||
reduceHeadersObject: reduceHeadersObject, | ||
stringToHeaders: stringToHeaders | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "headers-polyfill", | ||
"version": "3.2.2", | ||
"version": "3.2.3", | ||
"description": "A native \"Headers\" class polyfill.", | ||
@@ -32,4 +32,5 @@ "main": "./lib/index.js", | ||
"@ossjs/release": "^0.7.2", | ||
"@swc/core": "^1.3.82", | ||
"@types/jest": "^28.1.4", | ||
"@types/set-cookie-parser": "^2.4.3", | ||
"@types/jest": "^28.1.4", | ||
"jest": "^28.1.2", | ||
@@ -36,0 +37,0 @@ "jest-environment-jsdom": "^28.1.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
111489
1409
11