harRouter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var harRouter_exports = {};
  20. __export(harRouter_exports, {
  21. HarRouter: () => HarRouter
  22. });
  23. module.exports = __toCommonJS(harRouter_exports);
  24. class HarRouter {
  25. static async create(localUtils, file, notFoundAction, options) {
  26. const { harId, error } = await localUtils.harOpen({ file });
  27. if (error)
  28. throw new Error(error);
  29. return new HarRouter(localUtils, harId, notFoundAction, options);
  30. }
  31. constructor(localUtils, harId, notFoundAction, options) {
  32. this._localUtils = localUtils;
  33. this._harId = harId;
  34. this._options = options;
  35. this._notFoundAction = notFoundAction;
  36. }
  37. async _handle(route) {
  38. const request = route.request();
  39. const response = await this._localUtils.harLookup({
  40. harId: this._harId,
  41. url: request.url(),
  42. method: request.method(),
  43. headers: await request.headersArray(),
  44. postData: request.postDataBuffer() || void 0,
  45. isNavigationRequest: request.isNavigationRequest()
  46. });
  47. if (response.action === "redirect") {
  48. route._platform.log("api", `HAR: ${route.request().url()} redirected to ${response.redirectURL}`);
  49. await route._redirectNavigationRequest(response.redirectURL);
  50. return;
  51. }
  52. if (response.action === "fulfill") {
  53. if (response.status === -1)
  54. return;
  55. await route.fulfill({
  56. status: response.status,
  57. headers: Object.fromEntries(response.headers.map((h) => [h.name, h.value])),
  58. body: response.body
  59. });
  60. return;
  61. }
  62. if (response.action === "error")
  63. route._platform.log("api", "HAR: " + response.message);
  64. if (this._notFoundAction === "abort") {
  65. await route.abort();
  66. return;
  67. }
  68. await route.fallback();
  69. }
  70. async addContextRoute(context) {
  71. await context.route(this._options.urlMatch || "**/*", (route) => this._handle(route));
  72. }
  73. async addPageRoute(page) {
  74. await page.route(this._options.urlMatch || "**/*", (route) => this._handle(route));
  75. }
  76. async [Symbol.asyncDispose]() {
  77. await this.dispose();
  78. }
  79. dispose() {
  80. this._localUtils.harClose({ harId: this._harId }).catch(() => {
  81. });
  82. }
  83. }
  84. // Annotate the CommonJS export names for ESM import in node:
  85. 0 && (module.exports = {
  86. HarRouter
  87. });