123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.fromNodeHeaders = fromNodeHeaders;
- exports.splitCookiesString = splitCookiesString;
- exports.toNodeHeaders = toNodeHeaders;
- exports.validateURL = validateURL;
- function fromNodeHeaders(object) {
- const headers = new Headers();
- for (let [key, value] of Object.entries(object)){
- const values = Array.isArray(value) ? value : [
- value
- ];
- for (let v of values){
- if (v !== undefined) {
- headers.append(key, v);
- }
- }
- }
- return headers;
- }
- function splitCookiesString(cookiesString) {
- 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 === ",") {
- // ',' is a cookie separator if we have later first '=', not ';' or ','
- lastComma = pos;
- pos += 1;
- skipWhitespace();
- nextStart = pos;
- while(pos < cookiesString.length && notSpecialChar()){
- pos += 1;
- }
- // currently special character
- if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
- // we found cookies separator
- cookiesSeparatorFound = true;
- // pos is inside the next cookie, so back up and return it.
- pos = nextStart;
- cookiesStrings.push(cookiesString.substring(start, lastComma));
- start = pos;
- } else {
- // in param ',' or param separator ';',
- // we continue from that comma
- pos = lastComma + 1;
- }
- } else {
- pos += 1;
- }
- }
- if (!cookiesSeparatorFound || pos >= cookiesString.length) {
- cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
- }
- }
- return cookiesStrings;
- }
- function toNodeHeaders(headers) {
- const result = {};
- if (headers) {
- for (const [key, value] of headers.entries()){
- result[key] = value;
- if (key.toLowerCase() === "set-cookie") {
- result[key] = splitCookiesString(value);
- }
- }
- }
- return result;
- }
- function validateURL(url) {
- try {
- return String(new URL(String(url)));
- } catch (error) {
- throw new Error(`URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`, {
- cause: error
- });
- }
- }
- //# sourceMappingURL=utils.js.map
|