@edge-runtime/cookies
Advanced tools
Comparing version 3.2.1 to 3.2.2
@@ -189,2 +189,16 @@ // Type definitions for cookie 0.5 | ||
export { CookieListItem, RequestCookie, RequestCookies, ResponseCookie, ResponseCookies }; | ||
/** | ||
* @source https://github.com/nfriedly/set-cookie-parser/blob/master/lib/set-cookie.js | ||
* | ||
* Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas | ||
* that are within a single set-cookie field-value, such as in the Expires portion. | ||
* This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2 | ||
* Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128 | ||
* React Native's fetch does this for *every* header, including set-cookie. | ||
* | ||
* Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25 | ||
* Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation | ||
*/ | ||
declare function splitCookiesString(cookiesString: string): string[]; | ||
export { CookieListItem, RequestCookie, RequestCookies, ResponseCookie, ResponseCookies, splitCookiesString }; |
@@ -24,3 +24,4 @@ "use strict"; | ||
RequestCookies: () => RequestCookies, | ||
ResponseCookies: () => ResponseCookies | ||
ResponseCookies: () => ResponseCookies, | ||
splitCookiesString: () => splitCookiesString | ||
}); | ||
@@ -30,3 +31,3 @@ module.exports = __toCommonJS(src_exports); | ||
// src/serialize.ts | ||
function serialize(c) { | ||
function stringifyCookie(c) { | ||
var _a; | ||
@@ -44,3 +45,3 @@ const attrs = [ | ||
} | ||
function parseCookieString(cookie) { | ||
function parseCookie(cookie) { | ||
const map = /* @__PURE__ */ new Map(); | ||
@@ -63,7 +64,7 @@ for (const pair of cookie.split(/; */)) { | ||
} | ||
function parseSetCookieString(setCookie) { | ||
function parseSetCookie(setCookie) { | ||
if (!setCookie) { | ||
return void 0; | ||
} | ||
const [[name, value], ...attributes] = parseCookieString(setCookie); | ||
const [[name, value], ...attributes] = parseCookie(setCookie); | ||
const { domain, expires, httponly, maxage, path, samesite, secure } = Object.fromEntries( | ||
@@ -99,2 +100,53 @@ attributes.map(([key, value2]) => [key.toLowerCase(), value2]) | ||
} | ||
function splitCookiesString(cookiesString) { | ||
if (!cookiesString) | ||
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; | ||
} | ||
@@ -109,3 +161,3 @@ // src/request-cookies.ts | ||
if (header) { | ||
const parsed = parseCookieString(header); | ||
const parsed = parseCookie(header); | ||
for (const [name, value] of parsed) { | ||
@@ -147,3 +199,3 @@ this._parsed.set(name, { name, value }); | ||
"cookie", | ||
Array.from(map).map(([_, value2]) => serialize(value2)).join("; ") | ||
Array.from(map).map(([_, value2]) => stringifyCookie(value2)).join("; ") | ||
); | ||
@@ -160,3 +212,3 @@ return this; | ||
"cookie", | ||
Array.from(map).map(([_, value]) => serialize(value)).join("; ") | ||
Array.from(map).map(([_, value]) => stringifyCookie(value)).join("; ") | ||
); | ||
@@ -196,3 +248,3 @@ return result; | ||
for (const cookieString of cookieStrings) { | ||
const parsed = parseSetCookieString(cookieString); | ||
const parsed = parseSetCookie(cookieString); | ||
if (parsed) | ||
@@ -242,3 +294,3 @@ this._parsed.set(parsed.name, parsed); | ||
toString() { | ||
return [...this._parsed.values()].map(serialize).join("; "); | ||
return [...this._parsed.values()].map(stringifyCookie).join("; "); | ||
} | ||
@@ -249,3 +301,3 @@ }; | ||
for (const [, value] of bag) { | ||
const serialized = serialize(value); | ||
const serialized = stringifyCookie(value); | ||
headers.append("set-cookie", serialized); | ||
@@ -266,57 +318,7 @@ } | ||
} | ||
function splitCookiesString(cookiesString) { | ||
if (!cookiesString) | ||
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; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
RequestCookies, | ||
ResponseCookies | ||
ResponseCookies, | ||
splitCookiesString | ||
}); |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://edge-runtime.vercel.app/packages/cookies", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"main": "dist/index.js", | ||
@@ -30,5 +30,5 @@ "module": "dist/index.mjs", | ||
"@types/cookie": "0.5.1", | ||
"tsup": "6", | ||
"tsup": "7", | ||
"@edge-runtime/format": "2.1.0", | ||
"@edge-runtime/jest-environment": "2.2.1" | ||
"@edge-runtime/jest-environment": "2.2.5" | ||
}, | ||
@@ -35,0 +35,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
46561
783