cookies.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Cookies.js - 1.2.3 (patched for SameSite=Strict and secure=true)
  3. * https://github.com/ScottHamper/Cookies
  4. *
  5. * This is free and unencumbered software released into the public domain.
  6. */
  7. (function (global, undefined) {
  8. 'use strict';
  9. var factory = function (window) {
  10. if (typeof window.document !== 'object') {
  11. throw new Error('Cookies.js requires a `window` with a `document` object');
  12. }
  13. var Cookies = function (key, value, options) {
  14. return arguments.length === 1 ?
  15. Cookies.get(key) : Cookies.set(key, value, options);
  16. };
  17. // Allows for setter injection in unit tests
  18. Cookies._document = window.document;
  19. // Used to ensure cookie keys do not collide with
  20. // built-in `Object` properties
  21. Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :)
  22. Cookies._maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC');
  23. Cookies.defaults = {
  24. path: '/',
  25. SameSite: 'Strict',
  26. secure: true
  27. };
  28. Cookies.get = function (key) {
  29. if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
  30. Cookies._renewCache();
  31. }
  32. var value = Cookies._cache[Cookies._cacheKeyPrefix + key];
  33. return value === undefined ? undefined : decodeURIComponent(value);
  34. };
  35. Cookies.set = function (key, value, options) {
  36. options = Cookies._getExtendedOptions(options);
  37. options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);
  38. Cookies._document.cookie = Cookies._generateCookieString(key, value, options);
  39. return Cookies;
  40. };
  41. Cookies.expire = function (key, options) {
  42. return Cookies.set(key, undefined, options);
  43. };
  44. Cookies._getExtendedOptions = function (options) {
  45. return {
  46. path: options && options.path || Cookies.defaults.path,
  47. domain: options && options.domain || Cookies.defaults.domain,
  48. SameSite: options && options.SameSite || Cookies.defaults.SameSite,
  49. expires: options && options.expires || Cookies.defaults.expires,
  50. secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
  51. };
  52. };
  53. Cookies._isValidDate = function (date) {
  54. return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
  55. };
  56. Cookies._getExpiresDate = function (expires, now) {
  57. now = now || new Date();
  58. if (typeof expires === 'number') {
  59. expires = expires === Infinity ?
  60. Cookies._maxExpireDate : new Date(now.getTime() + expires * 1000);
  61. } else if (typeof expires === 'string') {
  62. expires = new Date(expires);
  63. }
  64. if (expires && !Cookies._isValidDate(expires)) {
  65. throw new Error('`expires` parameter cannot be converted to a valid Date instance');
  66. }
  67. return expires;
  68. };
  69. Cookies._generateCookieString = function (key, value, options) {
  70. key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);
  71. key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');
  72. value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
  73. options = options || {};
  74. var cookieString = key + '=' + value;
  75. cookieString += options.path ? ';path=' + options.path : '';
  76. cookieString += options.domain ? ';domain=' + options.domain : '';
  77. cookieString += options.SameSite ? ';SameSite=' + options.SameSite : '';
  78. cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
  79. cookieString += options.secure ? ';secure' : '';
  80. return cookieString;
  81. };
  82. Cookies._getCacheFromString = function (documentCookie) {
  83. var cookieCache = {};
  84. var cookiesArray = documentCookie ? documentCookie.split('; ') : [];
  85. for (var i = 0; i < cookiesArray.length; i++) {
  86. var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);
  87. if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) {
  88. cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value;
  89. }
  90. }
  91. return cookieCache;
  92. };
  93. Cookies._getKeyValuePairFromCookieString = function (cookieString) {
  94. // "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
  95. var separatorIndex = cookieString.indexOf('=');
  96. // IE omits the "=" when the cookie value is an empty string
  97. separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;
  98. var key = cookieString.substr(0, separatorIndex);
  99. var decodedKey;
  100. try {
  101. decodedKey = decodeURIComponent(key);
  102. } catch (e) {
  103. if (console && typeof console.error === 'function') {
  104. console.error('Could not decode cookie with key "' + key + '"', e);
  105. }
  106. }
  107. return {
  108. key: decodedKey,
  109. value: cookieString.substr(separatorIndex + 1) // Defer decoding value until accessed
  110. };
  111. };
  112. Cookies._renewCache = function () {
  113. Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie);
  114. Cookies._cachedDocumentCookie = Cookies._document.cookie;
  115. };
  116. Cookies._areEnabled = function () {
  117. var testKey = 'cookies.js';
  118. var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
  119. Cookies.expire(testKey);
  120. return areEnabled;
  121. };
  122. Cookies.enabled = Cookies._areEnabled();
  123. return Cookies;
  124. };
  125. var cookiesExport = (global && typeof global.document === 'object') ? factory(global) : factory;
  126. // AMD support
  127. if (typeof define === 'function' && define.amd) {
  128. define(function () { return cookiesExport; });
  129. // CommonJS/Node.js support
  130. } else if (typeof exports === 'object') {
  131. // Support Node.js specific `module.exports` (which can be a function)
  132. if (typeof module === 'object' && typeof module.exports === 'object') {
  133. exports = module.exports = cookiesExport;
  134. }
  135. // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
  136. exports.Cookies = cookiesExport;
  137. } else {
  138. global.Cookies = cookiesExport;
  139. }
  140. })(typeof window === 'undefined' ? this : window);