cookie.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. /**
  3. * This code was originally copied from the 'cookie` module at v0.5.0 and was simplified for our use case.
  4. * https://github.com/jshttp/cookie/blob/a0c84147aab6266bdb3996cf4062e93907c0b0fc/index.js
  5. * It had the following license:
  6. *
  7. * (The MIT License)
  8. *
  9. * Copyright (c) 2012-2014 Roman Shtylman <shtylman@gmail.com>
  10. * Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining
  13. * a copy of this software and associated documentation files (the
  14. * 'Software'), to deal in the Software without restriction, including
  15. * without limitation the rights to use, copy, modify, merge, publish,
  16. * distribute, sublicense, and/or sell copies of the Software, and to
  17. * permit persons to whom the Software is furnished to do so, subject to
  18. * the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be
  21. * included in all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  26. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  27. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  28. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  29. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. /**
  32. * Parses a cookie string
  33. */
  34. function parseCookie(str) {
  35. const obj = {};
  36. let index = 0;
  37. while (index < str.length) {
  38. const eqIdx = str.indexOf('=', index);
  39. // no more cookie pairs
  40. if (eqIdx === -1) {
  41. break;
  42. }
  43. let endIdx = str.indexOf(';', index);
  44. if (endIdx === -1) {
  45. endIdx = str.length;
  46. } else if (endIdx < eqIdx) {
  47. // backtrack on prior semicolon
  48. index = str.lastIndexOf(';', eqIdx - 1) + 1;
  49. continue;
  50. }
  51. const key = str.slice(index, eqIdx).trim();
  52. // only assign once
  53. if (undefined === obj[key]) {
  54. let val = str.slice(eqIdx + 1, endIdx).trim();
  55. // quoted values
  56. if (val.charCodeAt(0) === 0x22) {
  57. val = val.slice(1, -1);
  58. }
  59. try {
  60. obj[key] = val.indexOf('%') !== -1 ? decodeURIComponent(val) : val;
  61. } catch (e) {
  62. obj[key] = val;
  63. }
  64. }
  65. index = endIdx + 1;
  66. }
  67. return obj;
  68. }
  69. exports.parseCookie = parseCookie;
  70. //# sourceMappingURL=cookie.js.map