Skip to content

Commit

Permalink
move path parsing to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 31, 2022
1 parent 9d9584e commit 3fb1827
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
30 changes: 23 additions & 7 deletions src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
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
*/
export const prepareCookiePath = (pathArg) => {
// eslint-disable-line no-console
const log = console.log.bind(console);

let pathString;
if (pathArg === '/') {
pathString = 'path=/';
} else if (pathArg === 'none') {
pathString = '';
} else {
log(`Invalid cookie path: '${pathArg}'`);
return null;
}
return pathString;
};

/**
* Prepares cookie string if given parameters are ok
* @param {string} name cookie name to set
Expand Down Expand Up @@ -49,13 +70,8 @@ export const prepareCookie = (name, value, path) => {
return null;
}

let pathToSet;
if (path === '/') {
pathToSet = 'path=/';
} else if (path === 'none') {
pathToSet = '';
} else {
log(`Invalid cookie path: '${path}'`);
const pathToSet = prepareCookiePath(path);
if (!pathToSet) {
return null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/scriptlets/set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
nativeIsNaN,
prepareCookie,
isCookieSetWithValue,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
} from '../helpers/index';

/**
Expand Down Expand Up @@ -68,4 +71,5 @@ setCookieReload.injections = [
nativeIsNaN,
prepareCookie,
isCookieSetWithValue,
prepareCookiePath,
];
16 changes: 14 additions & 2 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { hit, nativeIsNaN, prepareCookie } from '../helpers/index';
import {
hit,
nativeIsNaN,
prepareCookie,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
} from '../helpers/index';

/* eslint-disable max-len */
/**
Expand Down Expand Up @@ -48,4 +55,9 @@ setCookie.names = [
'set-cookie',
];

setCookie.injections = [hit, nativeIsNaN, prepareCookie];
setCookie.injections = [
hit,
nativeIsNaN,
prepareCookie,
prepareCookiePath,
];
14 changes: 7 additions & 7 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
hit,
nativeIsNaN,
isCookieSetWithValue,
// following helpers should be imported and injected
// because they are used by helpers above
prepareCookiePath,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -100,15 +103,11 @@ export function trustedSetCookie(source, name, value, offsetExpiresSec = '', rel

cookieToSet += `${encodedName}=${encodedValue};`;

let pathToSet;
if (path === '/') {
pathToSet = 'path=/';
} else if (path === 'none') {
pathToSet = '';
} else {
log(`Invalid cookie path: '${path}'`);
const pathToSet = prepareCookiePath(path);
if (!pathToSet) {
return;
}

cookieToSet += pathToSet;

// Set expiration date if offsetExpiresSec was passed
Expand Down Expand Up @@ -159,4 +158,5 @@ trustedSetCookie.injections = [
hit,
nativeIsNaN,
isCookieSetWithValue,
prepareCookiePath,
];

0 comments on commit 3fb1827

Please sign in to comment.