File size: 19,460 Bytes
9ada4bc
1
{"version":3,"sources":["../node_modules/set-cookie-parser/lib/set-cookie.js","../src/store.ts"],"names":["value","cookies","parseCookie"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAEA,QAAI,sBAAsB;AAAA,MACxB,cAAc;AAAA,MACd,KAAK;AAAA,MACL,QAAQ;AAAA,IACV;AAEA,aAAS,iBAAiB,KAAK;AAC7B,aAAO,OAAO,QAAQ,YAAY,CAAC,CAAC,IAAI,KAAK;AAAA,IAC/C;AAEA,aAAS,YAAY,gBAAgB,SAAS;AAC5C,UAAI,QAAQ,eAAe,MAAM,GAAG,EAAE,OAAO,gBAAgB;AAE7D,UAAI,mBAAmB,MAAM,MAAM;AACnC,UAAI,SAAS,mBAAmB,gBAAgB;AAChD,UAAI,OAAO,OAAO;AAClB,UAAI,QAAQ,OAAO;AAEnB,gBAAU,UACN,OAAO,OAAO,CAAC,GAAG,qBAAqB,OAAO,IAC9C;AAEJ,UAAI;AACF,gBAAQ,QAAQ,eAAe,mBAAmB,KAAK,IAAI;AAAA,MAC7D,SAAS,GAAP;AACA,gBAAQ;AAAA,UACN,gFACE,QACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAEA,UAAI,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAEA,YAAM,QAAQ,SAAU,MAAM;AAC5B,YAAI,QAAQ,KAAK,MAAM,GAAG;AAC1B,YAAI,MAAM,MAAM,MAAM,EAAE,SAAS,EAAE,YAAY;AAC/C,YAAIA,SAAQ,MAAM,KAAK,GAAG;AAC1B,YAAI,QAAQ,WAAW;AACrB,iBAAO,UAAU,IAAI,KAAKA,MAAK;AAAA,QACjC,WAAW,QAAQ,WAAW;AAC5B,iBAAO,SAAS,SAASA,QAAO,EAAE;AAAA,QACpC,WAAW,QAAQ,UAAU;AAC3B,iBAAO,SAAS;AAAA,QAClB,WAAW,QAAQ,YAAY;AAC7B,iBAAO,WAAW;AAAA,QACpB,WAAW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AAO,IAAI;AAAA,QAC3B;AAAA,MACF,CAAC;AAED,UAAI,cAAc,SAAS,GAAG;AAC5B,aAAK,MAAM,OAAO,MAAM;AAAA,MAC1B;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAEO,IAAM,QAAQ,IAAI,YAAY","sourcesContent":["\"use strict\";\n\nvar defaultParseOptions = {\n  decodeValues: true,\n  map: false,\n  silent: false,\n};\n\nfunction isNonEmptyString(str) {\n  return typeof str === \"string\" && !!str.trim();\n}\n\nfunction parseString(setCookieValue, options) {\n  var parts = setCookieValue.split(\";\").filter(isNonEmptyString);\n\n  var nameValuePairStr = parts.shift();\n  var parsed = parseNameValuePair(nameValuePairStr);\n  var name = parsed.name;\n  var value = parsed.value;\n\n  options = options\n    ? Object.assign({}, defaultParseOptions, options)\n    : defaultParseOptions;\n\n  try {\n    value = options.decodeValues ? decodeURIComponent(value) : value; // decode cookie value\n  } catch (e) {\n    console.error(\n      \"set-cookie-parser encountered an error while decoding a cookie with value '\" +\n        value +\n        \"'. Set options.decodeValues to false to disable this feature.\",\n      e\n    );\n  }\n\n  var cookie = {\n    name: name,\n    value: value,\n  };\n\n  parts.forEach(function (part) {\n    var sides = part.split(\"=\");\n    var key = sides.shift().trimLeft().toLowerCase();\n    var value = sides.join(\"=\");\n    if (key === \"expires\") {\n      cookie.expires = new Date(value);\n    } else if (key === \"max-age\") {\n      cookie.maxAge = parseInt(value, 10);\n    } else if (key === \"secure\") {\n      cookie.secure = true;\n    } else if (key === \"httponly\") {\n      cookie.httpOnly = true;\n    } else if (key === \"samesite\") {\n      cookie.sameSite = value;\n    } else {\n      cookie[key] = value;\n    }\n  });\n\n  return cookie;\n}\n\nfunction parseNameValuePair(nameValuePairStr) {\n  // Parses name-value-pair according to rfc6265bis draft\n\n  var name = \"\";\n  var value = \"\";\n  var nameValueArr = nameValuePairStr.split(\"=\");\n  if (nameValueArr.length > 1) {\n    name = nameValueArr.shift();\n    value = nameValueArr.join(\"=\"); // everything after the first =, joined by a \"=\" if there was more than one part\n  } else {\n    value = nameValuePairStr;\n  }\n\n  return { name: name, value: value };\n}\n\nfunction parse(input, options) {\n  options = options\n    ? Object.assign({}, defaultParseOptions, options)\n    : defaultParseOptions;\n\n  if (!input) {\n    if (!options.map) {\n      return [];\n    } else {\n      return {};\n    }\n  }\n\n  if (input.headers) {\n    if (typeof input.headers.getSetCookie === \"function\") {\n      // for fetch responses - they combine headers of the same type in the headers array,\n      // but getSetCookie returns an uncombined array\n      input = input.headers.getSetCookie();\n    } else if (input.headers[\"set-cookie\"]) {\n      // fast-path for node.js (which automatically normalizes header names to lower-case\n      input = input.headers[\"set-cookie\"];\n    } else {\n      // slow-path for other environments - see #25\n      var sch =\n        input.headers[\n          Object.keys(input.headers).find(function (key) {\n            return key.toLowerCase() === \"set-cookie\";\n          })\n        ];\n      // warn if called on a request-like object with a cookie header rather than a set-cookie header - see #34, 36\n      if (!sch && input.headers.cookie && !options.silent) {\n        console.warn(\n          \"Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning.\"\n        );\n      }\n      input = sch;\n    }\n  }\n  if (!Array.isArray(input)) {\n    input = [input];\n  }\n\n  options = options\n    ? Object.assign({}, defaultParseOptions, options)\n    : defaultParseOptions;\n\n  if (!options.map) {\n    return input.filter(isNonEmptyString).map(function (str) {\n      return parseString(str, options);\n    });\n  } else {\n    var cookies = {};\n    return input.filter(isNonEmptyString).reduce(function (cookies, str) {\n      var cookie = parseString(str, options);\n      cookies[cookie.name] = cookie;\n      return cookies;\n    }, cookies);\n  }\n}\n\n/*\n  Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas\n  that are within a single set-cookie field-value, such as in the Expires portion.\n\n  This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2\n  Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128\n  React Native's fetch does this for *every* header, including set-cookie.\n\n  Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25\n  Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation\n*/\nfunction splitCookiesString(cookiesString) {\n  if (Array.isArray(cookiesString)) {\n    return cookiesString;\n  }\n  if (typeof cookiesString !== \"string\") {\n    return [];\n  }\n\n  var cookiesStrings = [];\n  var pos = 0;\n  var start;\n  var ch;\n  var lastComma;\n  var nextStart;\n  var cookiesSeparatorFound;\n\n  function skipWhitespace() {\n    while (pos < cookiesString.length && /\\s/.test(cookiesString.charAt(pos))) {\n      pos += 1;\n    }\n    return pos < cookiesString.length;\n  }\n\n  function notSpecialChar() {\n    ch = cookiesString.charAt(pos);\n\n    return ch !== \"=\" && ch !== \";\" && ch !== \",\";\n  }\n\n  while (pos < cookiesString.length) {\n    start = pos;\n    cookiesSeparatorFound = false;\n\n    while (skipWhitespace()) {\n      ch = cookiesString.charAt(pos);\n      if (ch === \",\") {\n        // ',' is a cookie separator if we have later first '=', not ';' or ','\n        lastComma = pos;\n        pos += 1;\n\n        skipWhitespace();\n        nextStart = pos;\n\n        while (pos < cookiesString.length && notSpecialChar()) {\n          pos += 1;\n        }\n\n        // currently special character\n        if (pos < cookiesString.length && cookiesString.charAt(pos) === \"=\") {\n          // we found cookies separator\n          cookiesSeparatorFound = true;\n          // pos is inside the next cookie, so back up and return it.\n          pos = nextStart;\n          cookiesStrings.push(cookiesString.substring(start, lastComma));\n          start = pos;\n        } else {\n          // in param ',' or param separator ';',\n          // we continue from that comma\n          pos = lastComma + 1;\n        }\n      } else {\n        pos += 1;\n      }\n    }\n\n    if (!cookiesSeparatorFound || pos >= cookiesString.length) {\n      cookiesStrings.push(cookiesString.substring(start, cookiesString.length));\n    }\n  }\n\n  return cookiesStrings;\n}\n\nmodule.exports = parse;\nmodule.exports.parse = parse;\nmodule.exports.parseString = parseString;\nmodule.exports.splitCookiesString = splitCookiesString;\n","import { Cookie, parse as parseCookie } from 'set-cookie-parser'\n\ninterface RequestLike {\n  credentials: Request['credentials']\n  url: string\n}\n\ninterface HeadersLike {\n  get(name: string): string | null\n}\n\ninterface ResponseLike {\n  headers: HeadersLike\n}\n\nexport type Store = Map<string, StoreEntry>\nexport type StoreEntry = Map<string, Cookie>\nexport type CookieString = Omit<Cookie, 'expires'> & { expires?: string }\n\nexport const PERSISTENCY_KEY = 'MSW_COOKIE_STORE'\n\nfunction supportsLocalStorage() {\n  try {\n    if (localStorage == null) {\n      return false\n    }\n\n    const testKey = PERSISTENCY_KEY + '_test'\n\n    localStorage.setItem(testKey, 'test')\n    localStorage.getItem(testKey)\n    localStorage.removeItem(testKey)\n\n    return true\n  } catch (error) {\n    return false\n  }\n}\n\nconst hasLocalStorageSupport = supportsLocalStorage()\n\n/**\n * Checks that accessing a given property on an object\n * by name does not throw an error.\n *\n * This is generally used to avoid issues in environments\n * like `miniflare` where some properties are defined as getters\n * where accessing that property throws directly.\n */\nfunction isPropertyAccessible<Obj extends Record<string, any>>(\n  object: Obj,\n  method: keyof Obj,\n) {\n  try {\n    object[method]\n    return true\n  } catch {\n    return false\n  }\n}\n\nclass CookieStore {\n  private store: Store\n\n  constructor() {\n    this.store = new Map()\n  }\n\n  /**\n   * Sets the given request cookies into the store.\n   * Respects the `request.credentials` policy.\n   */\n  add(request: RequestLike, response: ResponseLike): void {\n    if (\n      isPropertyAccessible(request, 'credentials') &&\n      request.credentials === 'omit'\n    ) {\n      return\n    }\n\n    const requestUrl = new URL(request.url)\n    const responseCookies = response.headers.get('set-cookie')\n\n    if (!responseCookies) {\n      return\n    }\n\n    const now = Date.now()\n    const parsedResponseCookies = parseCookie(responseCookies).map(\n      ({ maxAge, ...cookie }) => ({\n        ...cookie,\n        expires:\n          maxAge === undefined ? cookie.expires : new Date(now + maxAge * 1000),\n        maxAge,\n      }),\n    )\n\n    const prevCookies =\n      this.store.get(requestUrl.origin) || new Map<string, Cookie>()\n\n    parsedResponseCookies.forEach((cookie) => {\n      this.store.set(requestUrl.origin, prevCookies.set(cookie.name, cookie))\n    })\n  }\n\n  /**\n   * Returns cookies relevant to the given request\n   * and its `request.credentials` policy.\n   */\n  get(request: RequestLike): StoreEntry {\n    this.deleteExpiredCookies()\n\n    const requestUrl = new URL(request.url)\n    const originCookies =\n      this.store.get(requestUrl.origin) || new Map<string, Cookie>()\n\n    if (!isPropertyAccessible(request, 'credentials')) {\n      return originCookies\n    }\n\n    switch (request.credentials) {\n      case 'include': {\n        // Support running this method in Node.js.\n        if (typeof document === 'undefined') {\n          return originCookies\n        }\n\n        const documentCookies = parseCookie(document.cookie)\n\n        documentCookies.forEach((cookie) => {\n          originCookies.set(cookie.name, cookie)\n        })\n\n        return originCookies\n      }\n\n      case 'same-origin': {\n        return originCookies\n      }\n\n      default:\n        return new Map()\n    }\n  }\n\n  /**\n   * Returns a collection of all stored cookies.\n   */\n  getAll(): Store {\n    this.deleteExpiredCookies()\n    return this.store\n  }\n\n  /**\n   * Deletes all cookies associated with the given request.\n   */\n  deleteAll(request: RequestLike): void {\n    const requestUrl = new URL(request.url)\n    this.store.delete(requestUrl.origin)\n  }\n\n  /**\n   * Clears the entire cookie store.\n   */\n  clear(): void {\n    this.store.clear()\n  }\n\n  /**\n   * Hydrates the virtual cookie store from the `localStorage` if defined.\n   */\n  hydrate(): void {\n    if (!hasLocalStorageSupport) {\n      return\n    }\n\n    const persistedCookies = localStorage.getItem(PERSISTENCY_KEY)\n\n    if (!persistedCookies) {\n      return\n    }\n\n    try {\n      const parsedCookies: [string, [string, CookieString][]][] =\n        JSON.parse(persistedCookies)\n\n      parsedCookies.forEach(([origin, cookies]) => {\n        this.store.set(\n          origin,\n          new Map(\n            cookies.map(([token, { expires, ...cookie }]) => [\n              token,\n              expires === undefined\n                ? cookie\n                : { ...cookie, expires: new Date(expires) },\n            ]),\n          ),\n        )\n      })\n    } catch (error) {\n      console.warn(`\n[virtual-cookie] Failed to parse a stored cookie from the localStorage (key \"${PERSISTENCY_KEY}\").\n\nStored value:\n${localStorage.getItem(PERSISTENCY_KEY)}\n\nThrown exception:\n${error}\n\nInvalid value has been removed from localStorage to prevent subsequent failed parsing attempts.`)\n      localStorage.removeItem(PERSISTENCY_KEY)\n    }\n  }\n\n  /**\n   * Persists the current virtual cookies into the `localStorage` if defined,\n   * so they are available on the next page load.\n   */\n  persist(): void {\n    if (!hasLocalStorageSupport) {\n      return\n    }\n\n    const serializedCookies = Array.from(this.store.entries()).map(\n      ([origin, cookies]) => {\n        return [origin, Array.from(cookies.entries())]\n      },\n    )\n\n    localStorage.setItem(PERSISTENCY_KEY, JSON.stringify(serializedCookies))\n  }\n\n  private deleteExpiredCookies() {\n    const now = Date.now()\n\n    this.store.forEach((originCookies, origin) => {\n      originCookies.forEach(({ expires, name }) => {\n        if (expires !== undefined && expires.getTime() <= now) {\n          originCookies.delete(name)\n        }\n      })\n\n      if (originCookies.size === 0) {\n        this.store.delete(origin)\n      }\n    })\n  }\n}\n\nexport const store = new CookieStore()\n"]}