xmlhttprequest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.CookieJar = exports.parse = exports.createCookieJar = exports.XHR = void 0;
  27. const XMLHttpRequestModule = __importStar(require("xmlhttprequest-ssl"));
  28. exports.XHR = XMLHttpRequestModule.default || XMLHttpRequestModule;
  29. function createCookieJar() {
  30. return new CookieJar();
  31. }
  32. exports.createCookieJar = createCookieJar;
  33. /**
  34. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
  35. */
  36. function parse(setCookieString) {
  37. const parts = setCookieString.split("; ");
  38. const i = parts[0].indexOf("=");
  39. if (i === -1) {
  40. return;
  41. }
  42. const name = parts[0].substring(0, i).trim();
  43. if (!name.length) {
  44. return;
  45. }
  46. let value = parts[0].substring(i + 1).trim();
  47. if (value.charCodeAt(0) === 0x22) {
  48. // remove double quotes
  49. value = value.slice(1, -1);
  50. }
  51. const cookie = {
  52. name,
  53. value,
  54. };
  55. for (let j = 1; j < parts.length; j++) {
  56. const subParts = parts[j].split("=");
  57. if (subParts.length !== 2) {
  58. continue;
  59. }
  60. const key = subParts[0].trim();
  61. const value = subParts[1].trim();
  62. switch (key) {
  63. case "Expires":
  64. cookie.expires = new Date(value);
  65. break;
  66. case "Max-Age":
  67. const expiration = new Date();
  68. expiration.setUTCSeconds(expiration.getUTCSeconds() + parseInt(value, 10));
  69. cookie.expires = expiration;
  70. break;
  71. default:
  72. // ignore other keys
  73. }
  74. }
  75. return cookie;
  76. }
  77. exports.parse = parse;
  78. class CookieJar {
  79. constructor() {
  80. this.cookies = new Map();
  81. }
  82. parseCookies(xhr) {
  83. const values = xhr.getResponseHeader("set-cookie");
  84. if (!values) {
  85. return;
  86. }
  87. values.forEach((value) => {
  88. const parsed = parse(value);
  89. if (parsed) {
  90. this.cookies.set(parsed.name, parsed);
  91. }
  92. });
  93. }
  94. addCookies(xhr) {
  95. const cookies = [];
  96. this.cookies.forEach((cookie, name) => {
  97. var _a;
  98. if (((_a = cookie.expires) === null || _a === void 0 ? void 0 : _a.getTime()) < Date.now()) {
  99. this.cookies.delete(name);
  100. }
  101. else {
  102. cookies.push(`${name}=${cookie.value}`);
  103. }
  104. });
  105. if (cookies.length) {
  106. xhr.setDisableHeaderCheck(true);
  107. xhr.setRequestHeader("cookie", cookies.join("; "));
  108. }
  109. }
  110. }
  111. exports.CookieJar = CookieJar;