Skip to content

Commit

Permalink
redo helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 3, 2022
1 parent deb0cbb commit 9f1f0d4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 67 deletions.
108 changes: 59 additions & 49 deletions src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,99 @@
import { nativeIsNaN } from './number-utils';

/**
* Prepares path string if given parameters are ok
* @param {string} pathArg
* @returns {string|null} returns path string if ok OR null if not
* Checks whether the input path is supported
*
* @param {string} rawPath input path
*
* @returns {boolean}
*/
export const prepareCookiePath = (pathArg) => {
// eslint-disable-next-line no-console
const log = console.log.bind(console);
export const isValidCookieRawPath = (rawPath) => rawPath === '/' || rawPath === 'none';

let pathString;
if (pathArg === '/') {
pathString = 'path=/';
} else if (pathArg === 'none') {
pathString = '';
} else {
log(`Invalid cookie path: '${pathArg}'`);
return null;
/**
* Returns 'path=/' if rawPath is '/'
* or empty string '' for other cases, `rawPath === 'none'` included
*
* @param {string} rawPath
*
* @returns {string}
*/
export const getCookiePath = (rawPath) => {
if (rawPath === '/') {
return 'path=/';
}
return pathString;
// otherwise do not set path as invalid
// the same for pathArg === 'none'
//
return '';
};

/**
* Combines input cookie name, value, and path into string.
*
* @param {string} rawName
* @param {string} rawValue
* @param {string} rawPath
*
* @returns {string} string OR `null` if path is not supported
*/
export const concatCookieNameValuePath = (rawName, rawValue, rawPath) => {
const pathToSet = prepareCookiePath(rawPath);
return `${encodeURIComponent(rawName)}=${encodeURIComponent(rawValue)}; ${pathToSet}`;
const log = console.log.bind(console); // eslint-disable-line no-console
if (!isValidCookieRawPath(rawPath)) {
log(`Invalid cookie path: '${rawPath}'`);
return null;
}
// eslint-disable-next-line max-len
return `${encodeURIComponent(rawName)}=${encodeURIComponent(rawValue)}; ${getCookiePath(rawPath)}`;
};

/**
* Prepares cookie string if given parameters are ok
* @param {string} name cookie name to set
* @param {string} value cookie value to set
* @param {string} path cookie path to set, 'none' for no path
* @returns {string|null} cookie string if ok OR null if not
* Gets supported cookie value
*
* @param {string} value input cookie value
*
* @returns {string|null} valid cookie string if ok OR null if not
*/
export const prepareLimitedCookieString = (name, value, path) => {
if (!name || !value) {
export const getLimitedCookieValue = (value) => {
if (!value) {
return null;
}

const log = console.log.bind(console); // eslint-disable-line no-console

let valueToSet;
let validValue;
if (value === 'true') {
valueToSet = 'true';
validValue = 'true';
} else if (value === 'True') {
valueToSet = 'True';
validValue = 'True';
} else if (value === 'false') {
valueToSet = 'false';
validValue = 'false';
} else if (value === 'False') {
valueToSet = 'False';
validValue = 'False';
} else if (value === 'yes') {
valueToSet = 'yes';
validValue = 'yes';
} else if (value === 'Yes') {
valueToSet = 'Yes';
validValue = 'Yes';
} else if (value === 'Y') {
valueToSet = 'Y';
validValue = 'Y';
} else if (value === 'no') {
valueToSet = 'no';
validValue = 'no';
} else if (value === 'ok') {
valueToSet = 'ok';
validValue = 'ok';
} else if (value === 'OK') {
valueToSet = 'OK';
validValue = 'OK';
} else if (/^\d+$/.test(value)) {
valueToSet = parseFloat(value);
if (nativeIsNaN(valueToSet)) {
validValue = parseFloat(value);
if (nativeIsNaN(validValue)) {
log(`Invalid cookie value: '${value}'`);
return null;
}
if (Math.abs(valueToSet) < 0 || Math.abs(valueToSet) > 15) {
if (Math.abs(validValue) < 0 || Math.abs(validValue) > 15) {
log(`Invalid cookie value: '${value}'`);
return null;
}
} else {
return null;
}

const pathToSet = prepareCookiePath(path);
if (!pathToSet) {
return null;
}

// eslint-disable-next-line max-len
const cookieData = concatCookieNameValuePath(name, valueToSet, pathToSet);

return cookieData;
return validValue;
};

/**
Expand Down
15 changes: 10 additions & 5 deletions src/scriptlets/set-cookie-reload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
hit,
nativeIsNaN,
prepareLimitedCookieString,
isCookieSetWithValue,
getLimitedCookieValue,
concatCookieNameValuePath,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
isValidCookieRawPath,
getCookiePath,
} from '../helpers/index';

/**
Expand Down Expand Up @@ -48,7 +50,8 @@ export function setCookieReload(source, name, value, path = '/') {
return;
}

const cookieData = prepareLimitedCookieString(name, value, path);
const validValue = getLimitedCookieValue(value);
const cookieData = concatCookieNameValuePath(name, validValue, path);

if (cookieData) {
document.cookie = cookieData;
Expand All @@ -69,7 +72,9 @@ setCookieReload.names = [
setCookieReload.injections = [
hit,
nativeIsNaN,
prepareLimitedCookieString,
isCookieSetWithValue,
prepareCookiePath,
getLimitedCookieValue,
concatCookieNameValuePath,
isValidCookieRawPath,
getCookiePath,
];
17 changes: 12 additions & 5 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
hit,
nativeIsNaN,
prepareLimitedCookieString,
isCookieSetWithValue,
getLimitedCookieValue,
concatCookieNameValuePath,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
isValidCookieRawPath,
getCookiePath,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -43,7 +46,8 @@ import {
*/
/* eslint-enable max-len */
export function setCookie(source, name, value, path = '/') {
const cookieData = prepareLimitedCookieString(name, value, path);
const validValue = getLimitedCookieValue(value);
const cookieData = concatCookieNameValuePath(name, validValue, path);

if (cookieData) {
hit(source);
Expand All @@ -58,6 +62,9 @@ setCookie.names = [
setCookie.injections = [
hit,
nativeIsNaN,
prepareLimitedCookieString,
prepareCookiePath,
isCookieSetWithValue,
getLimitedCookieValue,
concatCookieNameValuePath,
isValidCookieRawPath,
getCookiePath,
];
14 changes: 6 additions & 8 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
concatCookieNameValuePath,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
isValidCookieRawPath,
getCookiePath,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -93,8 +94,6 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel
const ONE_YEAR_EXPIRATION_KEYWORD = '1year';
const ONE_DAY_EXPIRATION_KEYWORD = '1day';

let cookieToSet = '';

let parsedValue;

if (value === NOW_VALUE_KEYWORD) {
Expand All @@ -107,13 +106,11 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel
parsedValue = value;
}

const pathToSet = prepareCookiePath(path);
if (!pathToSet) {
let cookieToSet = concatCookieNameValuePath(name, parsedValue, path);
if (!cookieToSet) {
return;
}

cookieToSet += concatCookieNameValuePath(name, parsedValue, pathToSet);

// Set expiration date if offsetExpiresSec was passed
if (offsetExpiresSec) {
const MS_IN_SEC = 1000;
Expand Down Expand Up @@ -163,5 +160,6 @@ trustedSetCookie.injections = [
nativeIsNaN,
isCookieSetWithValue,
concatCookieNameValuePath,
prepareCookiePath,
isValidCookieRawPath,
getCookiePath,
];

0 comments on commit 9f1f0d4

Please sign in to comment.