12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var cookies = require('./cookies');
  2. var storageBuilder = require('./storageBuilder');
  3. var config = require('./config');
  4. // Building this as an object allows us to mock the functions in our tests
  5. var storageUtil = {};
  6. // IE11 bug that Microsoft doesn't plan to fix
  7. // https://connect.microsoft.com/IE/Feedback/Details/1496040
  8. storageUtil.browserHasLocalStorage = function() {
  9. try {
  10. if (storageUtil.getLocalStorage()) {
  11. return true;
  12. } else {
  13. return false;
  14. }
  15. } catch (e) {
  16. return false;
  17. }
  18. };
  19. storageUtil.browserHasSessionStorage = function() {
  20. try {
  21. if (storageUtil.getSessionStorage()) {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. } catch (e) {
  27. return false;
  28. }
  29. };
  30. storageUtil.getHttpCache = function() {
  31. if (storageUtil.browserHasLocalStorage()) {
  32. return storageBuilder(storageUtil.getLocalStorage(), config.CACHE_STORAGE_NAME);
  33. } else if (storageUtil.browserHasSessionStorage()) {
  34. return storageBuilder(storageUtil.getSessionStorage(), config.CACHE_STORAGE_NAME);
  35. } else {
  36. return storageBuilder(storageUtil.getCookieStorage(), config.CACHE_STORAGE_NAME);
  37. }
  38. };
  39. storageUtil.getLocalStorage = function() {
  40. return localStorage;
  41. };
  42. storageUtil.getSessionStorage = function() {
  43. return sessionStorage;
  44. };
  45. // Provides webStorage-like interface for cookies
  46. storageUtil.getCookieStorage = function() {
  47. return {
  48. getItem: cookies.getCookie,
  49. setItem: function(key, value) {
  50. // Cookie shouldn't expire
  51. cookies.setCookie(key, value, '2038-01-19T03:14:07.000Z');
  52. }
  53. };
  54. };
  55. module.exports = storageUtil;