harRouter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.HarRouter = void 0;
  6. var _debugLogger = require("../common/debugLogger");
  7. let _Symbol$asyncDispose;
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. _Symbol$asyncDispose = Symbol.asyncDispose;
  24. class HarRouter {
  25. static async create(localUtils, file, notFoundAction, options) {
  26. const {
  27. harId,
  28. error
  29. } = await localUtils._channel.harOpen({
  30. file
  31. });
  32. if (error) throw new Error(error);
  33. return new HarRouter(localUtils, harId, notFoundAction, options);
  34. }
  35. constructor(localUtils, harId, notFoundAction, options) {
  36. this._localUtils = void 0;
  37. this._harId = void 0;
  38. this._notFoundAction = void 0;
  39. this._options = void 0;
  40. this._localUtils = localUtils;
  41. this._harId = harId;
  42. this._options = options;
  43. this._notFoundAction = notFoundAction;
  44. }
  45. async _handle(route) {
  46. const request = route.request();
  47. const response = await this._localUtils._channel.harLookup({
  48. harId: this._harId,
  49. url: request.url(),
  50. method: request.method(),
  51. headers: await request.headersArray(),
  52. postData: request.postDataBuffer() || undefined,
  53. isNavigationRequest: request.isNavigationRequest()
  54. });
  55. if (response.action === 'redirect') {
  56. _debugLogger.debugLogger.log('api', `HAR: ${route.request().url()} redirected to ${response.redirectURL}`);
  57. await route._redirectNavigationRequest(response.redirectURL);
  58. return;
  59. }
  60. if (response.action === 'fulfill') {
  61. await route.fulfill({
  62. status: response.status,
  63. headers: Object.fromEntries(response.headers.map(h => [h.name, h.value])),
  64. body: response.body
  65. });
  66. return;
  67. }
  68. if (response.action === 'error') _debugLogger.debugLogger.log('api', 'HAR: ' + response.message);
  69. // Report the error, but fall through to the default handler.
  70. if (this._notFoundAction === 'abort') {
  71. await route.abort();
  72. return;
  73. }
  74. await route.fallback();
  75. }
  76. async addContextRoute(context) {
  77. await context.route(this._options.urlMatch || '**/*', route => this._handle(route));
  78. }
  79. async addPageRoute(page) {
  80. await page.route(this._options.urlMatch || '**/*', route => this._handle(route));
  81. }
  82. async [_Symbol$asyncDispose]() {
  83. await this.dispose();
  84. }
  85. dispose() {
  86. this._localUtils._channel.harClose({
  87. harId: this._harId
  88. }).catch(() => {});
  89. }
  90. }
  91. exports.HarRouter = HarRouter;