-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-storage.js
1016 lines (889 loc) · 26.8 KB
/
proxy-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! proxyStorage@v2.3.2. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["proxyStorage"] = factory();
else
root["proxyStorage"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isObject = isObject;
exports.checkEmpty = checkEmpty;
exports.setProperty = setProperty;
exports.tryParse = tryParse;
/**
* Determines whether a value is a plain object.
*
* @param {any} value: the object to test
* @return {boolean}
*/
function isObject(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
/**
* Validates if the key is not empty.
* (null, undefined or empty string)
*
* @param {string} key: keyname of an element in the storage mechanism
* @return {void}
*/
function checkEmpty(key) {
if (key == null || key === '') {
throw new Error('The key provided can not be empty');
}
}
/**
* Creates a non-enumerable read-only property.
*
* @param {object} obj: the object to add the property
* @param {string} name: the name of the property
* @param {any} value: the value of the property
* @return {void}
*/
function setProperty(obj, name, value) {
var descriptor = {
configurable: false,
enumerable: false,
writable: false
};
if (typeof value !== 'undefined') {
descriptor.value = value;
}
Object.defineProperty(obj, name, descriptor);
}
/**
* Try to parse a value from JSON.
*
* @param {string} value: the value to parse
* @return {any}
*/
function tryParse(value) {
var parsed = void 0;
try {
parsed = JSON.parse(value);
} catch (e) {
parsed = value;
}
return parsed;
}
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* @public
*
* Used to determine which storage mechanisms are available.
*
* @type {object}
*/
var isAvailable = exports.isAvailable = {
localStorage: false,
cookieStorage: false,
sessionStorage: false,
memoryStorage: true // fallback storage
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isAvailable = exports.configStorage = exports.WebStorage = exports.default = undefined;
var _webStorage = __webpack_require__(3);
var _webStorage2 = _interopRequireDefault(_webStorage);
var _isAvailable = __webpack_require__(1);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @public
*
* Current storage mechanism.
*
* @type {object}
*/
/**
* This library uses an adapter that implements the Web Storage interface,
* which is very useful to deal with the lack of compatibility between
* document.cookie and localStorage and sessionStorage.
*
* It also provides a memoryStorage fallback that stores the data in memory
* when all of above mechanisms are not available.
*
* Author: David Rivera
* Github: https://github.com/jherax
* License: "MIT"
*
* You can fork this project on github:
* https://github.com/jherax/proxy-storage.git
*/
var storage = null;
/**
* @public
*
* Get/Set the storage mechanism to use by default.
*
* @type {object}
*/
var configStorage = {
get: function get() {
return storage.__storage__;
},
/**
* Sets the storage mechanism to use by default.
*
* @param {string} storageType: it can be "localStorage", "sessionStorage", "cookieStorage", or "memoryStorage"
* @return {void}
*/
set: function set(storageType) {
exports.default = storage = new _webStorage2.default(storageType);
}
};
/**
* @private
*
* Checks whether a storage mechanism is available.
*
* @param {string} storageType: it can be "localStorage", "sessionStorage", "cookieStorage", or "memoryStorage"
* @return {boolean}
*/
function isStorageAvailable(storageType) {
var storageObj = _webStorage.proxy[storageType];
var data = '__proxy-storage__';
try {
storageObj.setItem(data, data);
storageObj.removeItem(data);
} catch (e) {
return false;
}
return true;
}
/**
* @private
*
* Sets the first or default storage available.
*
* @param {string} storageType: it can be "localStorage", "sessionStorage", "cookieStorage", or "memoryStorage"
* @return {boolean}
*/
function storageAvailable(storageType) {
if (_isAvailable.isAvailable[storageType]) {
_webStorage.webStorageSettings.default = storageType;
configStorage.set(storageType);
}
return _isAvailable.isAvailable[storageType];
}
/**
* @private
*
* Initializes the module.
*
* @return {void}
*/
function init() {
_isAvailable.isAvailable.localStorage = isStorageAvailable('localStorage');
_isAvailable.isAvailable.cookieStorage = isStorageAvailable('cookieStorage');
_isAvailable.isAvailable.sessionStorage = isStorageAvailable('sessionStorage');
_webStorage.webStorageSettings.isAvailable = _isAvailable.isAvailable;
// sets the default storage mechanism available
Object.keys(_isAvailable.isAvailable).some(storageAvailable);
}
init();
/**
* @public API
*/
exports.default = storage;
exports.WebStorage = _webStorage2.default;
exports.configStorage = configStorage;
exports.isAvailable = _isAvailable.isAvailable;
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.proxy = exports.webStorageSettings = exports.default = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _interceptors = __webpack_require__(4);
var _interceptors2 = _interopRequireDefault(_interceptors);
var _utils = __webpack_require__(0);
var _isAvailable = __webpack_require__(1);
var _proxyMechanism = __webpack_require__(5);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* @private
*
* Keeps WebStorage instances by type as singletons.
*
* @type {object}
*/
var INSTANCES = {};
/**
* @private
*
* Keys not allowed for cookies.
*
* @type {RegExp}
*/
var BANNED_KEYS = /^(?:expires|max-age|path|domain|secure)$/i;
/**
* @private
*
* Copies all existing keys in the storage.
*
* @param {CookieStorage} instance: the object to where copy the keys
* @param {object} storage: the storage mechanism
* @return {object}
*/
function copyKeys(instance, storage) {
Object.keys(storage).forEach(function (key) {
instance[key] = (0, _utils.tryParse)(storage[key]);
});
return instance;
}
/**
* @public
*
* Allows to validate if a storage mechanism is valid
*
* @type {object}
*/
var webStorageSettings = {
default: null,
isAvailable: _isAvailable.isAvailable
};
/**
* @private
*
* Validates if the storage mechanism is available and can be used safely.
*
* @param {string} storageType: it can be "localStorage", "sessionStorage", "cookieStorage", or "memoryStorage"
* @return {string}
*/
function storageAvailable(storageType) {
if (webStorageSettings.isAvailable[storageType]) return storageType;
var fallback = storageType === 'sessionStorage' ? 'memoryStorage' : webStorageSettings.default;
var msg = storageType + ' is not available. Falling back to ' + fallback;
console.warn(msg); // eslint-disable-line
return fallback;
}
/**
* @public
*
* Implementation of the Web Storage interface.
* It saves and retrieves values as JSON.
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
*
* @type {class}
*/
var WebStorage = function () {
/**
* Creates an instance of WebStorage.
*
* @param {string} storageType: it can be "localStorage", "sessionStorage", "cookieStorage", or "memoryStorage"
*
* @memberOf WebStorage
*/
function WebStorage(storageType) {
_classCallCheck(this, WebStorage);
if (!Object.prototype.hasOwnProperty.call(_proxyMechanism.proxy, storageType)) {
throw new Error('Storage type "' + storageType + '" is not valid');
}
// gets the requested storage mechanism
var storage = _proxyMechanism.proxy[storageType];
// if the storage is not available, sets the default
storageType = storageAvailable(storageType);
// keeps only one instance by storageType (singleton)
var cachedInstance = INSTANCES[storageType];
if (cachedInstance) {
return copyKeys(cachedInstance, storage);
}
(0, _utils.setProperty)(this, '__storage__', storageType);
// copies all existing keys in the storage mechanism
INSTANCES[storageType] = copyKeys(this, storage);
}
/**
* Stores a value given a key name.
*
* @param {string} key: keyname of the storage
* @param {any} value: data to save in the storage
* @param {object} options: additional options for cookieStorage
* @return {void}
*
* @memberOf WebStorage
*/
_createClass(WebStorage, [{
key: 'setItem',
value: function setItem(key, value, options) {
(0, _utils.checkEmpty)(key);
var storageType = this.__storage__;
if (storageType === 'cookieStorage' && BANNED_KEYS.test(key)) {
throw new Error('The key is a reserved word, therefore not allowed');
}
var v = (0, _interceptors2.default)('setItem', key, value, options);
if (v !== undefined) value = v;
this[key] = value;
// prevents converting strings to JSON to avoid extra quotes
if (typeof value !== 'string') value = JSON.stringify(value);
_proxyMechanism.proxy[storageType].setItem(key, value, options);
// checks if the cookie was created, or delete it if the domain or path are not valid
if (storageType === 'cookieStorage' && _proxyMechanism.proxy[storageType].getItem(key) === null) {
delete this[key];
}
}
/**
* Retrieves a value by its key name.
*
* @param {string} key: keyname of the storage
* @param {boolean} noParse: if the value shoudn't be parsed with `JSON.parse`
* @return {void}
*
* @memberOf WebStorage
*/
}, {
key: 'getItem',
value: function getItem(key, noParse) {
(0, _utils.checkEmpty)(key);
var value = _proxyMechanism.proxy[this.__storage__].getItem(key);
if (value == null) {
// null or undefined
delete this[key];
value = null;
} else {
if (noParse !== true) value = (0, _utils.tryParse)(value);
this[key] = value;
}
var v = (0, _interceptors2.default)('getItem', key, value);
if (v !== undefined) value = v;
return value;
}
/**
* Deletes a key from the storage.
*
* @param {string} key: keyname of the storage
* @param {object} options: additional options for cookieStorage
* @return {void}
*
* @memberOf WebStorage
*/
}, {
key: 'removeItem',
value: function removeItem(key, options) {
(0, _utils.checkEmpty)(key);
(0, _interceptors2.default)('removeItem', key, options);
delete this[key];
_proxyMechanism.proxy[this.__storage__].removeItem(key, options);
}
/**
* Removes all keys from the storage.
*
* @return {void}
*
* @memberOf WebStorage
*/
}, {
key: 'clear',
value: function clear() {
var _this = this;
(0, _interceptors2.default)('clear');
Object.keys(this).forEach(function (key) {
delete _this[key];
}, this);
_proxyMechanism.proxy[this.__storage__].clear();
}
/**
* Gets the number of data items stored in the Storage object.
*
* @readonly
*
* @memberOf WebStorage
*/
}, {
key: 'length',
get: function get() {
return Object.keys(this).length;
}
/**
* Adds an interceptor to a WebStorage method.
*
* @param {string} command: name of the API method to intercept
* @param {function} action: callback executed when the API method is called
* @return {void}
*
* @memberOf WebStorage
*/
}], [{
key: 'interceptors',
value: function interceptors(command, action) {
if (command in _interceptors.INTERCEPTORS && typeof action === 'function') {
_interceptors.INTERCEPTORS[command].push(action);
}
}
}]);
return WebStorage;
}();
/**
* @public API
*/
exports.default = WebStorage;
exports.webStorageSettings = webStorageSettings;
exports.proxy = _proxyMechanism.proxy;
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.default = executeInterceptors;
/**
* Stores the interceptors for WebStorage methods.
*
* @type {object}
*/
var INTERCEPTORS = exports.INTERCEPTORS = {
setItem: [],
getItem: [],
removeItem: [],
clear: []
};
/**
* Executes the interceptors for a WebStorage method and allows
* the transformation in chain of the value passed through.
*
* @param {string} command: name of the method to intercept
* @return {any}
*/
function executeInterceptors(command) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var key = args.shift();
var value = args.shift();
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
// clone the object to prevent mutations
value = JSON.parse(JSON.stringify(value));
}
return INTERCEPTORS[command].reduce(function (val, action) {
var transformed = action.apply(undefined, [key, val].concat(args));
if (transformed == null) return val;
return transformed;
}, value);
}
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.proxy = undefined;
var _cookieStorage = __webpack_require__(6);
var _cookieStorage2 = _interopRequireDefault(_cookieStorage);
var _memoryStorage = __webpack_require__(9);
var _memoryStorage2 = _interopRequireDefault(_memoryStorage);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @public
*
* Proxy for the storage mechanisms.
* All members implement the Web Storage interface.
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/Storage
*
* @type {object}
*/
var proxy = exports.proxy = {
localStorage: window.localStorage,
sessionStorage: window.sessionStorage,
cookieStorage: (0, _cookieStorage2.default)(),
memoryStorage: (0, _memoryStorage2.default)()
};
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _utils = __webpack_require__(0);
var _formatMetadata = __webpack_require__(7);
var _formatMetadata2 = _interopRequireDefault(_formatMetadata);
var _expirationDate = __webpack_require__(8);
var _expirationDate2 = _interopRequireDefault(_expirationDate);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @private
*
* Proxy for document.cookie
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
*
* @type {object}
*/
var $cookie = {
get: function get() {
return document.cookie;
},
set: function set(value) {
document.cookie = value;
},
data: {} // metadata associated to the cookies
};
/**
* @private
*
* Finds an element in the array.
*
* @param {string} cookie: key=value
* @return {boolean}
*/
function findCookie(cookie) {
var nameEQ = this.toString();
// prevent leading spaces before the key
return cookie.trim().indexOf(nameEQ) === 0;
}
/**
* @public
*
* Create, read, and delete elements from document.cookie,
* and implements the Web Storage interface.
*
* @return {object}
*/
function cookieStorage() {
var api = {
setItem: function setItem(key, value, options) {
options = Object.assign({ path: '/' }, options);
// keep track of the metadata associated to the cookie
$cookie.data[key] = { path: options.path };
var metadata = $cookie.data[key];
if ((0, _utils.isObject)(options.expires) || options.expires instanceof Date) {
metadata.expires = (0, _expirationDate2.default)(options.expires);
}
if (options.domain && typeof options.domain === 'string') {
metadata.domain = options.domain.trim();
}
if (options.secure === true) metadata.secure = true;
var cookie = key + '=' + encodeURIComponent(value) + (0, _formatMetadata2.default)(metadata);
// TODO: should encodeURIComponent(key) ?
$cookie.set(cookie);
},
getItem: function getItem(key) {
var value = null;
var nameEQ = key + '=';
var cookie = $cookie.get().split(';').find(findCookie, nameEQ);
if (cookie) {
// prevent leading spaces before the key name
value = cookie.trim().substring(nameEQ.length, cookie.length);
value = decodeURIComponent(value);
}
if (value === null) delete $cookie.data[key];
return value;
},
// TODO: Add the method getAll() to get all cookies
// https://github.com/jherax/proxy-storage/issues/6
removeItem: function removeItem(key, options) {
var metadata = Object.assign({}, $cookie.data[key], options);
metadata.expires = { days: -1 };
api.setItem(key, '', metadata);
delete $cookie.data[key];
},
clear: function clear() {
var key = void 0,
indexEQ = void 0;
$cookie.get().split(';').forEach(function (cookie) {
indexEQ = cookie.indexOf('=');
if (indexEQ > -1) {
key = cookie.substring(0, indexEQ);
// prevent leading spaces before the key
api.removeItem(key.trim());
}
});
}
};
return initialize(api);
}
/**
* @private
*
* Copy the current items in the cookie storage.
*
* @param {object} api: the storage mechanism to initialize
* @return {object}
*/
function initialize(api) {
// sets API members to read-only and non-enumerable
for (var prop in api) {
// eslint-disable-line
(0, _utils.setProperty)(api, prop);
}
// copies all existing elements in the storage
$cookie.get().split(';').forEach(function (cookie) {
var index = cookie.indexOf('=');
var key = cookie.substring(0, index).trim();
var value = cookie.substring(index + 1).trim();
if (key) api[key] = decodeURIComponent(value);
});
return api;
}
/**
* @public API
*/
exports.default = cookieStorage;
/***/ }),
/* 7 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = formatMetadata;
/**
* @private
*
* Builds the string for the cookie metadata.
*
* @param {string} key: name of the metadata
* @param {object} data: metadata of the cookie
* @return {string}
*/
function buildMetadataFor(key, data) {
if (!data[key]) return '';
return ';' + key + '=' + data[key];
}
/**
* Builds the whole string for the cookie metadata.
*
* @param {object} data: metadata of the cookie
* @return {string}
*/
function formatMetadata(data) {
var expires = buildMetadataFor('expires', data);
var domain = buildMetadataFor('domain', data);
var path = buildMetadataFor('path', data);
var secure = data.secure ? ';secure' : '';
return '' + expires + domain + path + secure;
}
/***/ }),
/* 8 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = buildExpiration;
/**
* @private
*
* Adds or subtracts date portions to the given date and returns the new date.
* @see https://gist.github.com/jherax/bbc43e479a492cc9cbfc7ccc20c53cd2
*
* @param {object} options: It contains the date parts to add or remove, and can have the following properties:
* - {Date} date: if provided, this date will be affected, otherwise the current date will be used.
* - {number} minutes: minutes to add/subtract
* - {number} hours: hours to add/subtract
* - {number} days: days to add/subtract
* - {number} months: months to add/subtract
* - {number} years: years to add/subtract
* @return {Date}
*/
function alterDate() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var opt = Object.assign({}, options);
var d = opt.date instanceof Date ? opt.date : new Date();
if (+opt.minutes) d.setMinutes(d.getMinutes() + opt.minutes);
if (+opt.hours) d.setHours(d.getHours() + opt.hours);
if (+opt.days) d.setDate(d.getDate() + opt.days);
if (+opt.months) d.setMonth(d.getMonth() + opt.months);
if (+opt.years) d.setFullYear(d.getFullYear() + opt.years);
return d;
}
/**
* Builds the expiration for the cookie.
*
* @param {Date|object} date: the expiration date
* @return {string}
*/
function buildExpiration(date) {
var expires = date instanceof Date ? alterDate({ date: date }) : alterDate(date);
return expires.toUTCString();
}
/***/ }),
/* 9 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.default = memoryStorage;
var _utils = __webpack_require__(0);
/**
* @private
*
* Gets the hashtable-store from the current window.
*
* @return {object}
*/
function getStoreFromWindow() {
var store = void 0;
try {
store = JSON.parse(window.self.name);
} catch (e) {
return {};
}
if (store && (typeof store === 'undefined' ? 'undefined' : _typeof(store)) === 'object') return store;
return {};
}
/**
* @private
*
* Saves the hashtable-store in the current window.
*
* @param {object} hashtable: {key,value} pairs stored in memoryStorage
* @return {void}
*/
function setStoreToWindow(hashtable) {
var store = JSON.stringify(hashtable);
window.self.name = store;
}
/**
* @public
*
* Create, read, and delete elements from memory, and implements
* the Web Storage interface. It also adds a hack to persist
* the storage in the session for the current tab (browser).
*
* @return {object}
*/
function memoryStorage() {
var hashtable = getStoreFromWindow();
var api = {
setItem: function setItem(key, value) {
hashtable[key] = value;
setStoreToWindow(hashtable);
},
getItem: function getItem(key) {
var value = hashtable[key];
return value === undefined ? null : value;
},
removeItem: function removeItem(key) {
delete hashtable[key];
setStoreToWindow(hashtable);
},
clear: function clear() {
Object.keys(hashtable).forEach(function (key) {
return delete hashtable[key];
});
setStoreToWindow(hashtable);
}
};
return initialize(api, hashtable);
}
/**
* @private
*
* Copy the current items in the cookie storage.
*
* @param {object} api: the storage mechanism to initialize
* @param {object} hashtable: store from the window tab