Skip to content

Commit

Permalink
Merge pull request #599 from lytics/bug/598-Only-expire-items-from-lo…
Browse files Browse the repository at this point in the history
…calStorage-added-by-pathforajs

bug: 598 Only expire items from localStorage added by pathforajs
  • Loading branch information
ashyablok authored Jul 9, 2021
2 parents 4f1aa75 + eba68dc commit bdffa3b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: required
language: node_js
dist: trusty
node_js:
- 8.16.2
- 14.17.1
addons:
chrome: stable
script:
Expand Down
9 changes: 7 additions & 2 deletions dist/pathfora.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@

var PAYLOAD_KEY = '$';
var EXPIRES_KEY = '@';
var PATHFORA_IDENTIFIER = 'PATHFORA';

function safeJsonParse (json) {
try {
Expand All @@ -479,7 +480,10 @@
}

function isExpired (record) {
return Date.parse(record[EXPIRES_KEY]) < Date.now();
return Boolean(record[EXPIRES_KEY]) && Date.parse(record[EXPIRES_KEY]) < Date.now();
}
function isCreatedByThisLib (record) {
return Boolean(record[PATHFORA_IDENTIFIER]);
}

var expiringLocalStorage = {
Expand Down Expand Up @@ -511,6 +515,7 @@

record[PAYLOAD_KEY] = '' + payload;
record[EXPIRES_KEY] = expiresOn;
record[PATHFORA_IDENTIFIER] = PF_VERSION; // identify localStorage items created by this lib

localStorage.setItem(key, JSON.stringify(record));
},
Expand All @@ -535,7 +540,7 @@
var key = localStorage.key(i);
var record = safeJsonParse(localStorage.getItem(key));

if (record && isExpired(record)) {
if (record && isCreatedByThisLib(record) && isExpired(record)) {
localStorage.removeItem(key);
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/pathfora.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pathfora.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@
"rollup-plugin-istanbul": "^2.0.1",
"walk": "^2.3.14"
},
"resolutions": {
"graceful-fs": "^4.2.4"
},
"scripts": {
"test": "gulp build && karma start --single-run",
"test:watch": "gulp build && karma start --watch",
"clean": "rm -rf ./dist",
"build:prod": "NODE_ENV=production gulp build",
"prod": "yarn run test && yarn run clean && yarn run build:prod",
Expand Down
10 changes: 8 additions & 2 deletions src/rollup/utils/persist/expiring-local-storage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { PF_VERSION } from '../../globals/config';
var PAYLOAD_KEY = '$';
var EXPIRES_KEY = '@';
var PATHFORA_IDENTIFIER = 'PATHFORA';

function safeJsonParse (json) {
try {
Expand All @@ -10,7 +12,10 @@ function safeJsonParse (json) {
}

function isExpired (record) {
return Date.parse(record[EXPIRES_KEY]) < Date.now();
return Boolean(record[EXPIRES_KEY]) && Date.parse(record[EXPIRES_KEY]) < Date.now();
}
function isCreatedByThisLib (record) {
return Boolean(record[PATHFORA_IDENTIFIER]);
}

export default {
Expand Down Expand Up @@ -42,6 +47,7 @@ export default {

record[PAYLOAD_KEY] = '' + payload;
record[EXPIRES_KEY] = expiresOn;
record[PATHFORA_IDENTIFIER] = PF_VERSION; // identify localStorage items created by this lib

localStorage.setItem(key, JSON.stringify(record));
},
Expand All @@ -66,7 +72,7 @@ export default {
var key = localStorage.key(i);
var record = safeJsonParse(localStorage.getItem(key));

if (record && isExpired(record)) {
if (record && isCreatedByThisLib(record) && isExpired(record)) {
localStorage.removeItem(key);
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/unit/persist/expiring-local-storage.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PF_VERSION } from '../../../src/rollup/globals/config';
import globalReset from '../../utils/global-reset';

describe('`expiringLocalStorage` util', function () {
Expand Down Expand Up @@ -29,7 +30,8 @@ describe('`expiringLocalStorage` util', function () {

expect(tuple).toEqual({
$: 'wow',
'@': expiresOn.toISOString()
'@': expiresOn.toISOString(),
'PATHFORA': PF_VERSION
});
});
});
Expand Down Expand Up @@ -129,5 +131,12 @@ describe('`expiringLocalStorage` util', function () {
expect(localStorage.getItem('current')).not.toBe(null);
expect(localStorage.getItem('expired')).toBe(null);
});
it('should ignore records that have not been created by this library (i.e. do not have an expiry field)', () => {
localStorage.setItem('myCat', 'Bob');
pathfora.utils.store.ttl('myDog', 'Jim', -10000);
pathfora.utils.store.removeExpiredItems();
expect(localStorage.getItem('myCat')).toBe('Bob');
expect(localStorage.getItem('myDog')).toBe(null);
});
});
});
35 changes: 4 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2323,32 +2323,10 @@ glogg@^1.0.0:
dependencies:
sparkles "^1.0.0"

graceful-fs@^3.0.0:
version "3.0.12"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-3.0.12.tgz#0034947ce9ed695ec8ab0b854bc919e82b1ffaef"
integrity sha512-J55gaCS4iTTJfTXIxSVw3EMQckcqkpdRv3IR7gu6sq0+tbC363Zx6KH/SEwXASK9JRbhyZmVjJEVJIOxYsB3Qg==
dependencies:
natives "^1.1.3"

graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "4.2.2"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.2.tgz#6f0952605d0140c1cfdb138ed005775b92d67b02"
integrity sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q==

graceful-fs@~1.2.0:
version "1.2.3"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-1.2.3.tgz#15a4806a57547cb2d2dbf27f42e89a8c3451b364"
integrity sha1-FaSAaldUfLLS2/J/QuiajDRRs2Q=

graceful-fs@~2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-2.0.3.tgz#7cd2cdb228a4a3f36e95efa6cc142de7d1a136d0"
integrity sha1-fNLNsiiko/Nule+mzBQt59GhNtA=

graceful-fs@~4.1.4:
version "4.1.15"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==
graceful-fs@^3.0.0, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.1.9, graceful-fs@^4.2.4, graceful-fs@~1.2.0, graceful-fs@~2.0.0, graceful-fs@~4.1.4:
version "4.2.6"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==

"graceful-readlink@>= 1.0.0":
version "1.0.1"
Expand Down Expand Up @@ -4214,11 +4192,6 @@ nanomatch@^1.2.9:
snapdragon "^0.8.1"
to-regex "^3.0.1"

natives@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/natives/-/natives-1.1.6.tgz#a603b4a498ab77173612b9ea1acdec4d980f00bb"
integrity sha512-6+TDFewD4yxY14ptjKaS63GVdtKiES1pTPyxn9Jb0rBqPMZ7VcCiooEhPNsr+mqHtMGxa/5c/HhcC4uPEUw/nA==

natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
Expand Down

0 comments on commit bdffa3b

Please sign in to comment.