Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

devtools.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const fs_extra_1 = __importDefault(require("fs-extra"));
  7. const path_1 = __importDefault(require("path"));
  8. const atob_1 = __importDefault(require("atob"));
  9. const minimatch_1 = __importDefault(require("minimatch"));
  10. const logger_1 = __importDefault(require("@wdio/logger"));
  11. const _1 = __importDefault(require("."));
  12. const __1 = require("..");
  13. const constants_1 = require("../../constants");
  14. const log = logger_1.default('webdriverio');
  15. class DevtoolsInterception extends _1.default {
  16. static handleRequestInterception(client, mocks) {
  17. return async (event) => {
  18. const isRequest = !event.responseHeaders;
  19. const eventResponseHeaders = event.responseHeaders || [];
  20. const responseHeaders = eventResponseHeaders.reduce((headers, { name, value }) => {
  21. headers[name] = value;
  22. return headers;
  23. }, {});
  24. const { requestId, request, responseStatusCode = 200 } = event;
  25. for (const mock of mocks) {
  26. if (isRequest && (mock.respondOverwrites.length === 0 ||
  27. (!mock.respondOverwrites[0].errorReason &&
  28. mock.respondOverwrites[0].params &&
  29. mock.respondOverwrites[0].params.fetchResponse !== false))) {
  30. continue;
  31. }
  32. if (!minimatch_1.default(request.url, mock.url)) {
  33. continue;
  34. }
  35. request.statusCode = responseStatusCode;
  36. request.responseHeaders = { ...responseHeaders };
  37. if (filterMethod(request.method, mock.filterOptions.method) ||
  38. filterHeaders(request.headers, mock.filterOptions.requestHeaders) ||
  39. filterHeaders(responseHeaders, mock.filterOptions.headers) ||
  40. filterRequest(request.postData, mock.filterOptions.postData) ||
  41. filterStatusCode(responseStatusCode, mock.filterOptions.statusCode)) {
  42. continue;
  43. }
  44. const { body, base64Encoded = undefined } = isRequest ? { body: '' } : await client.send('Fetch.getResponseBody', { requestId }).catch(() => ({}));
  45. request.body = base64Encoded ? atob_1.default(body) : body;
  46. const contentTypeHeader = Object.keys(responseHeaders).find(h => h.toLowerCase() === 'content-type') || '';
  47. const responseContentType = responseHeaders[contentTypeHeader];
  48. request.body = responseContentType && responseContentType.includes('application/json')
  49. ? tryParseJson(request.body)
  50. : request.body;
  51. mock.matches.push(request);
  52. if (mock.respondOverwrites.length === 0) {
  53. continue;
  54. }
  55. const { errorReason, overwrite, params = {} } = mock.respondOverwrites[0].sticky
  56. ? mock.respondOverwrites[0]
  57. : mock.respondOverwrites.shift() || {};
  58. if (overwrite !== undefined) {
  59. let newBody = overwrite;
  60. if (typeof overwrite === 'function') {
  61. newBody = await overwrite(request, client);
  62. }
  63. const isBodyUndefined = typeof newBody === 'undefined';
  64. if (isBodyUndefined) {
  65. newBody = '';
  66. }
  67. if (typeof newBody !== 'string') {
  68. newBody = JSON.stringify(newBody);
  69. }
  70. let responseCode = typeof params.statusCode === 'function' ? params.statusCode(request) : params.statusCode || responseStatusCode;
  71. let responseHeaders = [
  72. ...eventResponseHeaders,
  73. ...Object.entries(typeof params.headers === 'function' ? params.headers(request) : params.headers || {}).map(([name, value]) => ({ name, value }))
  74. ];
  75. const responseFilePath = path_1.default.isAbsolute(newBody) ? newBody : path_1.default.join(process.cwd(), newBody);
  76. if (newBody.length > 0 && await fs_extra_1.default.pathExists(responseFilePath) && await canAccess(responseFilePath)) {
  77. newBody = await fs_extra_1.default.readFile(responseFilePath);
  78. }
  79. else if (newBody.startsWith('http')) {
  80. responseCode = 301;
  81. responseHeaders = responseHeaders.filter(({ name }) => name.toLowerCase() !== 'location');
  82. responseHeaders.push({ name: 'Location', value: newBody });
  83. }
  84. request.mockedResponse = newBody;
  85. return client.send('Fetch.fulfillRequest', {
  86. requestId,
  87. responseCode,
  88. responseHeaders,
  89. body: isBodyUndefined ? undefined : (newBody instanceof Buffer ? newBody : Buffer.from(newBody, 'utf8')).toString('base64')
  90. }).catch(logFetchError);
  91. }
  92. if (errorReason) {
  93. return client.send('Fetch.failRequest', {
  94. requestId,
  95. errorReason
  96. }).catch(logFetchError);
  97. }
  98. }
  99. return client.send('Fetch.continueRequest', { requestId }).catch(logFetchError);
  100. };
  101. }
  102. get calls() {
  103. return this.matches;
  104. }
  105. clear() {
  106. this.matches = [];
  107. }
  108. restore() {
  109. this.clear();
  110. this.respondOverwrites = [];
  111. }
  112. respond(overwrite, params = {}) {
  113. this.respondOverwrites.push({ overwrite, params, sticky: true });
  114. }
  115. respondOnce(overwrite, params = {}) {
  116. this.respondOverwrites.push({ overwrite, params });
  117. }
  118. abort(errorReason, sticky = true) {
  119. if (typeof errorReason !== 'string' || !constants_1.ERROR_REASON.includes(errorReason)) {
  120. throw new Error(`Invalid value for errorReason, allowed are: ${constants_1.ERROR_REASON.join(', ')}`);
  121. }
  122. this.respondOverwrites.push({ errorReason, sticky });
  123. }
  124. abortOnce(errorReason) {
  125. this.abort(errorReason, false);
  126. }
  127. }
  128. exports.default = DevtoolsInterception;
  129. const filterMethod = (method, expected) => {
  130. if (typeof expected === 'undefined') {
  131. return false;
  132. }
  133. if (typeof expected === 'function') {
  134. return expected(method) !== true;
  135. }
  136. return expected.toLowerCase() !== method.toLowerCase();
  137. };
  138. const filterHeaders = (responseHeaders, expected) => {
  139. if (typeof expected === 'undefined') {
  140. return false;
  141. }
  142. if (typeof expected === 'function') {
  143. return expected(responseHeaders) !== true;
  144. }
  145. return !__1.containsHeaderObject(responseHeaders, expected);
  146. };
  147. const filterRequest = (postData, expected) => {
  148. if (typeof expected === 'undefined') {
  149. return false;
  150. }
  151. if (typeof expected === 'function') {
  152. return expected(postData) !== true;
  153. }
  154. return postData !== expected;
  155. };
  156. const filterStatusCode = (statusCode, expected) => {
  157. if (typeof expected === 'undefined') {
  158. return false;
  159. }
  160. if (typeof expected === 'function') {
  161. return expected(statusCode) !== true;
  162. }
  163. return statusCode !== expected;
  164. };
  165. const canAccess = async (filepath) => {
  166. try {
  167. await fs_extra_1.default.access(filepath);
  168. return true;
  169. }
  170. catch (_a) {
  171. return false;
  172. }
  173. };
  174. const tryParseJson = (body) => {
  175. try {
  176. return JSON.parse(body) || body;
  177. }
  178. catch (_a) {
  179. return body;
  180. }
  181. };
  182. const logFetchError = (err) => {
  183. log.debug(err === null || err === void 0 ? void 0 : err.message);
  184. };