cookie.js 2.4 KB

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