wkInterceptableRequest.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.WKRouteImpl = exports.WKInterceptableRequest = void 0;
  6. var network = _interopRequireWildcard(require("../network"));
  7. var _utils = require("../../utils");
  8. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  9. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  10. /**
  11. * Copyright 2017 Google Inc. All rights reserved.
  12. * Modifications copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. const errorReasons = {
  27. 'aborted': 'Cancellation',
  28. 'accessdenied': 'AccessControl',
  29. 'addressunreachable': 'General',
  30. 'blockedbyclient': 'Cancellation',
  31. 'blockedbyresponse': 'General',
  32. 'connectionaborted': 'General',
  33. 'connectionclosed': 'General',
  34. 'connectionfailed': 'General',
  35. 'connectionrefused': 'General',
  36. 'connectionreset': 'General',
  37. 'internetdisconnected': 'General',
  38. 'namenotresolved': 'General',
  39. 'timedout': 'Timeout',
  40. 'failed': 'General'
  41. };
  42. class WKInterceptableRequest {
  43. constructor(session, frame, event, redirectedFrom, documentId) {
  44. this._session = void 0;
  45. this.request = void 0;
  46. this._requestId = void 0;
  47. this._timestamp = void 0;
  48. this._wallTime = void 0;
  49. this._session = session;
  50. this._requestId = event.requestId;
  51. const resourceType = event.type ? event.type.toLowerCase() : redirectedFrom ? redirectedFrom.request.resourceType() : 'other';
  52. let postDataBuffer = null;
  53. this._timestamp = event.timestamp;
  54. this._wallTime = event.walltime * 1000;
  55. if (event.request.postData) postDataBuffer = Buffer.from(event.request.postData, 'base64');
  56. this.request = new network.Request(frame._page._browserContext, frame, null, (redirectedFrom === null || redirectedFrom === void 0 ? void 0 : redirectedFrom.request) || null, documentId, event.request.url, resourceType, event.request.method, postDataBuffer, (0, _utils.headersObjectToArray)(event.request.headers));
  57. }
  58. createResponse(responsePayload) {
  59. const getResponseBody = async () => {
  60. const response = await this._session.send('Network.getResponseBody', {
  61. requestId: this._requestId
  62. });
  63. return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
  64. };
  65. const timingPayload = responsePayload.timing;
  66. const timing = {
  67. startTime: this._wallTime,
  68. domainLookupStart: timingPayload ? wkMillisToRoundishMillis(timingPayload.domainLookupStart) : -1,
  69. domainLookupEnd: timingPayload ? wkMillisToRoundishMillis(timingPayload.domainLookupEnd) : -1,
  70. connectStart: timingPayload ? wkMillisToRoundishMillis(timingPayload.connectStart) : -1,
  71. secureConnectionStart: timingPayload ? wkMillisToRoundishMillis(timingPayload.secureConnectionStart) : -1,
  72. connectEnd: timingPayload ? wkMillisToRoundishMillis(timingPayload.connectEnd) : -1,
  73. requestStart: timingPayload ? wkMillisToRoundishMillis(timingPayload.requestStart) : -1,
  74. responseStart: timingPayload ? wkMillisToRoundishMillis(timingPayload.responseStart) : -1
  75. };
  76. const setCookieSeparator = process.platform === 'darwin' ? ',' : '\n';
  77. const response = new network.Response(this.request, responsePayload.status, responsePayload.statusText, (0, _utils.headersObjectToArray)(responsePayload.headers, ',', setCookieSeparator), timing, getResponseBody, responsePayload.source === 'service-worker');
  78. // No raw response headers in WebKit, use "provisional" ones.
  79. response.setRawResponseHeaders(null);
  80. // Transfer size is not available in WebKit.
  81. response.setTransferSize(null);
  82. if (responsePayload.requestHeaders && Object.keys(responsePayload.requestHeaders).length) {
  83. const headers = {
  84. ...responsePayload.requestHeaders
  85. };
  86. if (!headers['host']) headers['Host'] = new URL(this.request.url()).host;
  87. this.request.setRawRequestHeaders((0, _utils.headersObjectToArray)(headers));
  88. } else {
  89. // No raw headers available, use provisional ones.
  90. this.request.setRawRequestHeaders(null);
  91. }
  92. return response;
  93. }
  94. }
  95. exports.WKInterceptableRequest = WKInterceptableRequest;
  96. class WKRouteImpl {
  97. constructor(session, requestId) {
  98. this._session = void 0;
  99. this._requestId = void 0;
  100. this._session = session;
  101. this._requestId = requestId;
  102. }
  103. async abort(errorCode) {
  104. const errorType = errorReasons[errorCode];
  105. (0, _utils.assert)(errorType, 'Unknown error code: ' + errorCode);
  106. // In certain cases, protocol will return error if the request was already canceled
  107. // or the page was closed. We should tolerate these errors.
  108. await this._session.sendMayFail('Network.interceptRequestWithError', {
  109. requestId: this._requestId,
  110. errorType
  111. });
  112. }
  113. async fulfill(response) {
  114. if (300 <= response.status && response.status < 400) throw new Error('Cannot fulfill with redirect status: ' + response.status);
  115. // In certain cases, protocol will return error if the request was already canceled
  116. // or the page was closed. We should tolerate these errors.
  117. let mimeType = response.isBase64 ? 'application/octet-stream' : 'text/plain';
  118. const headers = (0, _utils.headersArrayToObject)(response.headers, true /* lowerCase */);
  119. const contentType = headers['content-type'];
  120. if (contentType) mimeType = contentType.split(';')[0].trim();
  121. await this._session.sendMayFail('Network.interceptRequestWithResponse', {
  122. requestId: this._requestId,
  123. status: response.status,
  124. statusText: network.STATUS_TEXTS[String(response.status)],
  125. mimeType,
  126. headers,
  127. base64Encoded: response.isBase64,
  128. content: response.body
  129. });
  130. }
  131. async continue(request, overrides) {
  132. // In certain cases, protocol will return error if the request was already canceled
  133. // or the page was closed. We should tolerate these errors.
  134. await this._session.sendMayFail('Network.interceptWithRequest', {
  135. requestId: this._requestId,
  136. url: overrides.url,
  137. method: overrides.method,
  138. headers: overrides.headers ? (0, _utils.headersArrayToObject)(overrides.headers, false /* lowerCase */) : undefined,
  139. postData: overrides.postData ? Buffer.from(overrides.postData).toString('base64') : undefined
  140. });
  141. }
  142. }
  143. exports.WKRouteImpl = WKRouteImpl;
  144. function wkMillisToRoundishMillis(value) {
  145. // WebKit uses -1000 for unavailable.
  146. if (value === -1000) return -1;
  147. // WebKit has a bug, instead of -1 it sends -1000 to be in ms.
  148. if (value <= 0) {
  149. // DNS can start before request start on Mac Network Stack
  150. return -1;
  151. }
  152. return (value * 1000 | 0) / 1000;
  153. }