cookieStore.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.CookieStore = void 0;
  6. exports.domainMatches = domainMatches;
  7. /**
  8. * Copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. class Cookie {
  23. constructor(data) {
  24. this._raw = void 0;
  25. this._raw = data;
  26. }
  27. name() {
  28. return this._raw.name;
  29. }
  30. // https://datatracker.ietf.org/doc/html/rfc6265#section-5.4
  31. matches(url) {
  32. if (this._raw.secure && url.protocol !== 'https:' && url.hostname !== 'localhost') return false;
  33. if (!domainMatches(url.hostname, this._raw.domain)) return false;
  34. if (!pathMatches(url.pathname, this._raw.path)) return false;
  35. return true;
  36. }
  37. equals(other) {
  38. return this._raw.name === other._raw.name && this._raw.domain === other._raw.domain && this._raw.path === other._raw.path;
  39. }
  40. networkCookie() {
  41. return this._raw;
  42. }
  43. updateExpiresFrom(other) {
  44. this._raw.expires = other._raw.expires;
  45. }
  46. expired() {
  47. if (this._raw.expires === -1) return false;
  48. return this._raw.expires * 1000 < Date.now();
  49. }
  50. }
  51. class CookieStore {
  52. constructor() {
  53. this._nameToCookies = new Map();
  54. }
  55. addCookies(cookies) {
  56. for (const cookie of cookies) this._addCookie(new Cookie(cookie));
  57. }
  58. cookies(url) {
  59. const result = [];
  60. for (const cookie of this._cookiesIterator()) {
  61. if (cookie.matches(url)) result.push(cookie.networkCookie());
  62. }
  63. return result;
  64. }
  65. allCookies() {
  66. const result = [];
  67. for (const cookie of this._cookiesIterator()) result.push(cookie.networkCookie());
  68. return result;
  69. }
  70. _addCookie(cookie) {
  71. let set = this._nameToCookies.get(cookie.name());
  72. if (!set) {
  73. set = new Set();
  74. this._nameToCookies.set(cookie.name(), set);
  75. }
  76. // https://datatracker.ietf.org/doc/html/rfc6265#section-5.3
  77. for (const other of set) {
  78. if (other.equals(cookie)) set.delete(other);
  79. }
  80. set.add(cookie);
  81. CookieStore.pruneExpired(set);
  82. }
  83. *_cookiesIterator() {
  84. for (const [name, cookies] of this._nameToCookies) {
  85. CookieStore.pruneExpired(cookies);
  86. for (const cookie of cookies) yield cookie;
  87. if (cookies.size === 0) this._nameToCookies.delete(name);
  88. }
  89. }
  90. static pruneExpired(cookies) {
  91. for (const cookie of cookies) {
  92. if (cookie.expired()) cookies.delete(cookie);
  93. }
  94. }
  95. }
  96. exports.CookieStore = CookieStore;
  97. function domainMatches(value, domain) {
  98. if (value === domain) return true;
  99. // Only strict match is allowed if domain doesn't start with '.' (host-only-flag is true in the spec)
  100. if (!domain.startsWith('.')) return false;
  101. value = '.' + value;
  102. return value.endsWith(domain);
  103. }
  104. function pathMatches(value, path) {
  105. if (value === path) return true;
  106. if (!value.endsWith('/')) value = value + '/';
  107. if (!path.endsWith('/')) path = path + '/';
  108. return value.startsWith(path);
  109. }