Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URLSearchParams for cookies #181

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@
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.
Expand All @@ -91,11 +84,8 @@
* 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<string, string> {
const obj: Record<string, string> = new NullObject();
export function parse(str: string, options?: ParseOptions): URLSearchParams {

Check failure on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Cannot find name 'URLSearchParams'.

Check failure on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Cannot find name 'URLSearchParams'.

Check failure on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Cannot find name 'URLSearchParams'.

Check failure on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Cannot find name 'URLSearchParams'.

Check failure on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 22

Cannot find name 'URLSearchParams'.
const obj = new URLSearchParams();

Check failure on line 88 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Cannot find name 'URLSearchParams'.

Check failure on line 88 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Cannot find name 'URLSearchParams'.

Check failure on line 88 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Cannot find name 'URLSearchParams'.

Check failure on line 88 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Cannot find name 'URLSearchParams'.

Check failure on line 88 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node.js 22

Cannot find name '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;
Expand All @@ -120,23 +110,20 @@
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);

Expand Down
Loading