Skip to content

Commit

Permalink
remove redundant isNaN check and add fast path for empty options (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Dec 7, 2023
1 parent 0c7351c commit 088871f
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@

'use strict'

/**
* Module variables.
* @private
*/

const decode = decodeURIComponent
const encode = encodeURIComponent

/**
* RegExp to match field-content in RFC 7230 sec 3.2
*
Expand All @@ -53,18 +45,18 @@ const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/ // eslint-dis
* The object has the various cookies as keys(names) => values
*
* @param {string} str
* @param {object} [options]
* @param {object} [opt]
* @return {object}
* @public
*/

function parse (str, options) {
function parse (str, opt) {
if (typeof str !== 'string') {
throw new TypeError('argument str must be a string')
}

const result = {}
const dec = (options && options.decode) || decode
const dec = opt?.decode || decodeURIComponent

let pos = 0
let terminatorPos = 0
Expand All @@ -74,6 +66,7 @@ function parse (str, options) {
if (terminatorPos === str.length) {
break
}

terminatorPos = str.indexOf(';', pos)
terminatorPos = (terminatorPos === -1) ? str.length : terminatorPos
eqIdx = str.indexOf('=', pos)
Expand All @@ -92,12 +85,14 @@ function parse (str, options) {
? str.substring(eqIdx + 1, terminatorPos - 1).trim()
: str.substring(eqIdx, terminatorPos).trim()

result[key] = (dec !== decode || val.indexOf('%') !== -1)
result[key] = (dec !== decodeURIComponent || val.indexOf('%') !== -1)
? tryDecode(val, dec)
: val
}

pos = terminatorPos + 1
}

return result
}

Expand All @@ -112,19 +107,18 @@ function parse (str, options) {
*
* @param {string} name
* @param {string} val
* @param {object} [options]
* @param {object} [opt]
* @return {string}
* @public
*/

function serialize (name, val, options) {
const opt = options || {}
const enc = opt.encode || encode
function serialize (name, val, opt) {
const enc = opt?.encode || encodeURIComponent
if (typeof enc !== 'function') {
throw new TypeError('option encode is invalid')
}

if (!fieldContentRegExp.test(name)) {
if (name && !fieldContentRegExp.test(name)) {
throw new TypeError('argument name is invalid')
}

Expand All @@ -134,13 +128,17 @@ function serialize (name, val, options) {
}

let str = name + '=' + value

if (opt == null) return str

if (opt.maxAge != null) {
const maxAge = opt.maxAge - 0
if (isNaN(maxAge) || !isFinite(maxAge)) {
const maxAge = +opt.maxAge

if (!isFinite(maxAge)) {
throw new TypeError('option maxAge is invalid')
}

str += '; Max-Age=' + Math.floor(maxAge)
str += '; Max-Age=' + Math.trunc(maxAge)
}

if (opt.domain) {
Expand Down Expand Up @@ -185,6 +183,7 @@ function serialize (name, val, options) {
const sameSite = typeof opt.sameSite === 'string'
? opt.sameSite.toLowerCase()
: opt.sameSite

switch (sameSite) {
case true:
str += '; SameSite=Strict'
Expand All @@ -211,13 +210,13 @@ function serialize (name, val, options) {
*
* @param {string} str
* @param {function} decode
* @returns {string}
* @private
*/

function tryDecode (str, decode) {
try {
return decode(str)
} catch (e) {
} catch {
return str
}
}
Expand Down

0 comments on commit 088871f

Please sign in to comment.