From dde2f3fd5172947b487603068c78ae7fe5377731 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 7 Oct 2024 15:54:59 -0700 Subject: [PATCH] Use URLSearchParams for cookies --- src/index.ts | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3d572dd..93015e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,13 +59,6 @@ const domainValueRegExp = const pathValueRegExp = /^[\u0020-\u003A\u003D-\u007E]*$/; const __toString = Object.prototype.toString; -const __hasOwnProperty = Object.prototype.hasOwnProperty; - -const NullObject = /* @__PURE__ */ (() => { - const C = function () {}; - C.prototype = Object.create(null); - return C; -})() as unknown as { new (): any }; /** * Parse options. @@ -91,11 +84,8 @@ export interface ParseOptions { * Parse the given cookie header string into an object * The object has the various cookies as keys(names) => values */ -export function parse( - str: string, - options?: ParseOptions, -): Record { - const obj: Record = new NullObject(); +export function parse(str: string, options?: ParseOptions): URLSearchParams { + const obj = new URLSearchParams(); const len = str.length; // RFC 6265 sec 4.1.1, RFC 2616 2.2 defines a cookie name consists of one char minimum, plus '='. if (len < 2) return obj; @@ -120,23 +110,20 @@ export function parse( const keyEndIdx = endIndex(str, eqIdx, keyStartIdx); const key = str.slice(keyStartIdx, keyEndIdx); - // only assign once - if (!__hasOwnProperty.call(obj, key)) { - let valStartIdx = startIndex(str, eqIdx + 1, endIdx); - let valEndIdx = endIndex(str, endIdx, valStartIdx); - - if ( - str.charCodeAt(valStartIdx) === 0x22 /* " */ && - str.charCodeAt(valEndIdx - 1) === 0x22 /* " */ - ) { - valStartIdx++; - valEndIdx--; - } - - const val = str.slice(valStartIdx, valEndIdx); - obj[key] = tryDecode(val, dec); + let valStartIdx = startIndex(str, eqIdx + 1, endIdx); + let valEndIdx = endIndex(str, endIdx, valStartIdx); + + if ( + str.charCodeAt(valStartIdx) === 0x22 /* " */ && + str.charCodeAt(valEndIdx - 1) === 0x22 /* " */ + ) { + valStartIdx++; + valEndIdx--; } + const val = str.slice(valStartIdx, valEndIdx); + obj.append(key, tryDecode(val, dec)); + index = endIdx + 1; } while (index < len);