localUtilsDispatcher.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.LocalUtilsDispatcher = void 0;
  6. exports.urlToWSEndpoint = urlToWSEndpoint;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _path = _interopRequireDefault(require("path"));
  9. var _os = _interopRequireDefault(require("os"));
  10. var _manualPromise = require("../../utils/manualPromise");
  11. var _utils = require("../../utils");
  12. var _dispatcher = require("./dispatcher");
  13. var _zipBundle = require("../../zipBundle");
  14. var _zipFile = require("../../utils/zipFile");
  15. var _jsonPipeDispatcher = require("../dispatchers/jsonPipeDispatcher");
  16. var _transport = require("../transport");
  17. var _socksInterceptor = require("../socksInterceptor");
  18. var _userAgent = require("../../utils/userAgent");
  19. var _progress = require("../progress");
  20. var _network = require("../../utils/network");
  21. var _instrumentation = require("../../server/instrumentation");
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. /**
  24. * Copyright (c) Microsoft Corporation.
  25. *
  26. * Licensed under the Apache License, Version 2.0 (the 'License");
  27. * you may not use this file except in compliance with the License.
  28. * You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing, software
  33. * distributed under the License is distributed on an "AS IS" BASIS,
  34. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  35. * See the License for the specific language governing permissions and
  36. * limitations under the License.
  37. */
  38. class LocalUtilsDispatcher extends _dispatcher.Dispatcher {
  39. constructor(scope, playwright) {
  40. const localUtils = new _instrumentation.SdkObject(playwright, 'localUtils', 'localUtils');
  41. const descriptors = require('../deviceDescriptors');
  42. const deviceDescriptors = Object.entries(descriptors).map(([name, descriptor]) => ({
  43. name,
  44. descriptor
  45. }));
  46. super(scope, localUtils, 'LocalUtils', {
  47. deviceDescriptors
  48. });
  49. this._type_LocalUtils = void 0;
  50. this._harBackends = new Map();
  51. this._stackSessions = new Map();
  52. this._type_LocalUtils = true;
  53. }
  54. async zip(params) {
  55. const promise = new _manualPromise.ManualPromise();
  56. const zipFile = new _zipBundle.yazl.ZipFile();
  57. zipFile.on('error', error => promise.reject(error));
  58. const addFile = (file, name) => {
  59. try {
  60. if (_fs.default.statSync(file).isFile()) zipFile.addFile(file, name);
  61. } catch (e) {}
  62. };
  63. for (const entry of params.entries) addFile(entry.value, entry.name);
  64. // Add stacks and the sources.
  65. const stackSession = params.stacksId ? this._stackSessions.get(params.stacksId) : undefined;
  66. if (stackSession !== null && stackSession !== void 0 && stackSession.callStacks.length) {
  67. await stackSession.writer;
  68. if (process.env.PW_LIVE_TRACE_STACKS) {
  69. zipFile.addFile(stackSession.file, 'trace.stacks');
  70. } else {
  71. const buffer = Buffer.from(JSON.stringify((0, _utils.serializeClientSideCallMetadata)(stackSession.callStacks)));
  72. zipFile.addBuffer(buffer, 'trace.stacks');
  73. }
  74. }
  75. // Collect sources from stacks.
  76. if (params.includeSources) {
  77. const sourceFiles = new Set();
  78. for (const {
  79. stack
  80. } of (stackSession === null || stackSession === void 0 ? void 0 : stackSession.callStacks) || []) {
  81. if (!stack) continue;
  82. for (const {
  83. file
  84. } of stack) sourceFiles.add(file);
  85. }
  86. for (const sourceFile of sourceFiles) addFile(sourceFile, 'resources/src@' + (0, _utils.calculateSha1)(sourceFile) + '.txt');
  87. }
  88. if (params.mode === 'write') {
  89. // New file, just compress the entries.
  90. await _fs.default.promises.mkdir(_path.default.dirname(params.zipFile), {
  91. recursive: true
  92. });
  93. zipFile.end(undefined, () => {
  94. zipFile.outputStream.pipe(_fs.default.createWriteStream(params.zipFile)).on('close', () => promise.resolve()).on('error', error => promise.reject(error));
  95. });
  96. await promise;
  97. await this._deleteStackSession(params.stacksId);
  98. return;
  99. }
  100. // File already exists. Repack and add new entries.
  101. const tempFile = params.zipFile + '.tmp';
  102. await _fs.default.promises.rename(params.zipFile, tempFile);
  103. _zipBundle.yauzl.open(tempFile, (err, inZipFile) => {
  104. if (err) {
  105. promise.reject(err);
  106. return;
  107. }
  108. (0, _utils.assert)(inZipFile);
  109. let pendingEntries = inZipFile.entryCount;
  110. inZipFile.on('entry', entry => {
  111. inZipFile.openReadStream(entry, (err, readStream) => {
  112. if (err) {
  113. promise.reject(err);
  114. return;
  115. }
  116. zipFile.addReadStream(readStream, entry.fileName);
  117. if (--pendingEntries === 0) {
  118. zipFile.end(undefined, () => {
  119. zipFile.outputStream.pipe(_fs.default.createWriteStream(params.zipFile)).on('close', () => {
  120. _fs.default.promises.unlink(tempFile).then(() => {
  121. promise.resolve();
  122. }).catch(error => promise.reject(error));
  123. });
  124. });
  125. }
  126. });
  127. });
  128. });
  129. await promise;
  130. await this._deleteStackSession(params.stacksId);
  131. }
  132. async harOpen(params, metadata) {
  133. let harBackend;
  134. if (params.file.endsWith('.zip')) {
  135. const zipFile = new _zipFile.ZipFile(params.file);
  136. const entryNames = await zipFile.entries();
  137. const harEntryName = entryNames.find(e => e.endsWith('.har'));
  138. if (!harEntryName) return {
  139. error: 'Specified archive does not have a .har file'
  140. };
  141. const har = await zipFile.read(harEntryName);
  142. const harFile = JSON.parse(har.toString());
  143. harBackend = new HarBackend(harFile, null, zipFile);
  144. } else {
  145. const harFile = JSON.parse(await _fs.default.promises.readFile(params.file, 'utf-8'));
  146. harBackend = new HarBackend(harFile, _path.default.dirname(params.file), null);
  147. }
  148. this._harBackends.set(harBackend.id, harBackend);
  149. return {
  150. harId: harBackend.id
  151. };
  152. }
  153. async harLookup(params, metadata) {
  154. const harBackend = this._harBackends.get(params.harId);
  155. if (!harBackend) return {
  156. action: 'error',
  157. message: `Internal error: har was not opened`
  158. };
  159. return await harBackend.lookup(params.url, params.method, params.headers, params.postData, params.isNavigationRequest);
  160. }
  161. async harClose(params, metadata) {
  162. const harBackend = this._harBackends.get(params.harId);
  163. if (harBackend) {
  164. this._harBackends.delete(harBackend.id);
  165. harBackend.dispose();
  166. }
  167. }
  168. async harUnzip(params, metadata) {
  169. const dir = _path.default.dirname(params.zipFile);
  170. const zipFile = new _zipFile.ZipFile(params.zipFile);
  171. for (const entry of await zipFile.entries()) {
  172. const buffer = await zipFile.read(entry);
  173. if (entry === 'har.har') await _fs.default.promises.writeFile(params.harFile, buffer);else await _fs.default.promises.writeFile(_path.default.join(dir, entry), buffer);
  174. }
  175. zipFile.close();
  176. await _fs.default.promises.unlink(params.zipFile);
  177. }
  178. async connect(params, metadata) {
  179. const controller = new _progress.ProgressController(metadata, this._object);
  180. controller.setLogName('browser');
  181. return await controller.run(async progress => {
  182. var _params$exposeNetwork;
  183. const wsHeaders = {
  184. 'User-Agent': (0, _userAgent.getUserAgent)(),
  185. 'x-playwright-proxy': (_params$exposeNetwork = params.exposeNetwork) !== null && _params$exposeNetwork !== void 0 ? _params$exposeNetwork : '',
  186. ...params.headers
  187. };
  188. const wsEndpoint = await urlToWSEndpoint(progress, params.wsEndpoint);
  189. const transport = await _transport.WebSocketTransport.connect(progress, wsEndpoint, wsHeaders, true, 'x-playwright-debug-log');
  190. const socksInterceptor = new _socksInterceptor.SocksInterceptor(transport, params.exposeNetwork, params.socksProxyRedirectPortForTest);
  191. const pipe = new _jsonPipeDispatcher.JsonPipeDispatcher(this);
  192. transport.onmessage = json => {
  193. if (socksInterceptor.interceptMessage(json)) return;
  194. const cb = () => {
  195. try {
  196. pipe.dispatch(json);
  197. } catch (e) {
  198. transport.close();
  199. }
  200. };
  201. if (params.slowMo) setTimeout(cb, params.slowMo);else cb();
  202. };
  203. pipe.on('message', message => {
  204. transport.send(message);
  205. });
  206. transport.onclose = () => {
  207. socksInterceptor === null || socksInterceptor === void 0 ? void 0 : socksInterceptor.cleanup();
  208. pipe.wasClosed();
  209. };
  210. pipe.on('close', () => transport.close());
  211. return {
  212. pipe,
  213. headers: transport.headers
  214. };
  215. }, params.timeout || 0);
  216. }
  217. async tracingStarted(params, metadata) {
  218. let tmpDir = undefined;
  219. if (!params.tracesDir) tmpDir = await _fs.default.promises.mkdtemp(_path.default.join(_os.default.tmpdir(), 'playwright-tracing-'));
  220. const traceStacksFile = _path.default.join(params.tracesDir || tmpDir, params.traceName + '.stacks');
  221. this._stackSessions.set(traceStacksFile, {
  222. callStacks: [],
  223. file: traceStacksFile,
  224. writer: Promise.resolve(),
  225. tmpDir
  226. });
  227. return {
  228. stacksId: traceStacksFile
  229. };
  230. }
  231. async traceDiscarded(params, metadata) {
  232. await this._deleteStackSession(params.stacksId);
  233. }
  234. async addStackToTracingNoReply(params, metadata) {
  235. for (const session of this._stackSessions.values()) {
  236. session.callStacks.push(params.callData);
  237. if (process.env.PW_LIVE_TRACE_STACKS) {
  238. session.writer = session.writer.then(() => {
  239. const buffer = Buffer.from(JSON.stringify((0, _utils.serializeClientSideCallMetadata)(session.callStacks)));
  240. return _fs.default.promises.writeFile(session.file, buffer);
  241. });
  242. }
  243. }
  244. }
  245. async _deleteStackSession(stacksId) {
  246. const session = stacksId ? this._stackSessions.get(stacksId) : undefined;
  247. if (!session) return;
  248. await session.writer;
  249. if (session.tmpDir) await (0, _utils.removeFolders)([session.tmpDir]);
  250. this._stackSessions.delete(stacksId);
  251. }
  252. }
  253. exports.LocalUtilsDispatcher = LocalUtilsDispatcher;
  254. const redirectStatus = [301, 302, 303, 307, 308];
  255. class HarBackend {
  256. constructor(harFile, baseDir, zipFile) {
  257. this.id = (0, _utils.createGuid)();
  258. this._harFile = void 0;
  259. this._zipFile = void 0;
  260. this._baseDir = void 0;
  261. this._harFile = harFile;
  262. this._baseDir = baseDir;
  263. this._zipFile = zipFile;
  264. }
  265. async lookup(url, method, headers, postData, isNavigationRequest) {
  266. let entry;
  267. try {
  268. entry = await this._harFindResponse(url, method, headers, postData);
  269. } catch (e) {
  270. return {
  271. action: 'error',
  272. message: 'HAR error: ' + e.message
  273. };
  274. }
  275. if (!entry) return {
  276. action: 'noentry'
  277. };
  278. // If navigation is being redirected, restart it with the final url to ensure the document's url changes.
  279. if (entry.request.url !== url && isNavigationRequest) return {
  280. action: 'redirect',
  281. redirectURL: entry.request.url
  282. };
  283. const response = entry.response;
  284. try {
  285. const buffer = await this._loadContent(response.content);
  286. return {
  287. action: 'fulfill',
  288. status: response.status,
  289. headers: response.headers,
  290. body: buffer
  291. };
  292. } catch (e) {
  293. return {
  294. action: 'error',
  295. message: e.message
  296. };
  297. }
  298. }
  299. async _loadContent(content) {
  300. const file = content._file;
  301. let buffer;
  302. if (file) {
  303. if (this._zipFile) buffer = await this._zipFile.read(file);else buffer = await _fs.default.promises.readFile(_path.default.resolve(this._baseDir, file));
  304. } else {
  305. buffer = Buffer.from(content.text || '', content.encoding === 'base64' ? 'base64' : 'utf-8');
  306. }
  307. return buffer;
  308. }
  309. async _harFindResponse(url, method, headers, postData) {
  310. const harLog = this._harFile.log;
  311. const visited = new Set();
  312. while (true) {
  313. const entries = [];
  314. for (const candidate of harLog.entries) {
  315. if (candidate.request.url !== url || candidate.request.method !== method) continue;
  316. if (method === 'POST' && postData && candidate.request.postData) {
  317. const buffer = await this._loadContent(candidate.request.postData);
  318. if (!buffer.equals(postData)) continue;
  319. }
  320. entries.push(candidate);
  321. }
  322. if (!entries.length) return;
  323. let entry = entries[0];
  324. // Disambiguate using headers - then one with most matching headers wins.
  325. if (entries.length > 1) {
  326. const list = [];
  327. for (const candidate of entries) {
  328. const matchingHeaders = countMatchingHeaders(candidate.request.headers, headers);
  329. list.push({
  330. candidate,
  331. matchingHeaders
  332. });
  333. }
  334. list.sort((a, b) => b.matchingHeaders - a.matchingHeaders);
  335. entry = list[0].candidate;
  336. }
  337. if (visited.has(entry)) throw new Error(`Found redirect cycle for ${url}`);
  338. visited.add(entry);
  339. // Follow redirects.
  340. const locationHeader = entry.response.headers.find(h => h.name.toLowerCase() === 'location');
  341. if (redirectStatus.includes(entry.response.status) && locationHeader) {
  342. const locationURL = new URL(locationHeader.value, url);
  343. url = locationURL.toString();
  344. if ((entry.response.status === 301 || entry.response.status === 302) && method === 'POST' || entry.response.status === 303 && !['GET', 'HEAD'].includes(method)) {
  345. // HTTP-redirect fetch step 13 (https://fetch.spec.whatwg.org/#http-redirect-fetch)
  346. method = 'GET';
  347. }
  348. continue;
  349. }
  350. return entry;
  351. }
  352. }
  353. dispose() {
  354. var _this$_zipFile;
  355. (_this$_zipFile = this._zipFile) === null || _this$_zipFile === void 0 ? void 0 : _this$_zipFile.close();
  356. }
  357. }
  358. function countMatchingHeaders(harHeaders, headers) {
  359. const set = new Set(headers.map(h => h.name.toLowerCase() + ':' + h.value));
  360. let matches = 0;
  361. for (const h of harHeaders) {
  362. if (set.has(h.name.toLowerCase() + ':' + h.value)) ++matches;
  363. }
  364. return matches;
  365. }
  366. async function urlToWSEndpoint(progress, endpointURL) {
  367. var _progress$timeUntilDe;
  368. if (endpointURL.startsWith('ws')) return endpointURL;
  369. progress === null || progress === void 0 ? void 0 : progress.log(`<ws preparing> retrieving websocket url from ${endpointURL}`);
  370. const fetchUrl = new URL(endpointURL);
  371. if (!fetchUrl.pathname.endsWith('/')) fetchUrl.pathname += '/';
  372. fetchUrl.pathname += 'json';
  373. const json = await (0, _network.fetchData)({
  374. url: fetchUrl.toString(),
  375. method: 'GET',
  376. timeout: (_progress$timeUntilDe = progress === null || progress === void 0 ? void 0 : progress.timeUntilDeadline()) !== null && _progress$timeUntilDe !== void 0 ? _progress$timeUntilDe : 30_000,
  377. headers: {
  378. 'User-Agent': (0, _userAgent.getUserAgent)()
  379. }
  380. }, async (params, response) => {
  381. return new Error(`Unexpected status ${response.statusCode} when connecting to ${fetchUrl.toString()}.\n` + `This does not look like a Playwright server, try connecting via ws://.`);
  382. });
  383. progress === null || progress === void 0 ? void 0 : progress.throwIfAborted();
  384. const wsUrl = new URL(endpointURL);
  385. let wsEndpointPath = JSON.parse(json).wsEndpointPath;
  386. if (wsEndpointPath.startsWith('/')) wsEndpointPath = wsEndpointPath.substring(1);
  387. if (!wsUrl.pathname.endsWith('/')) wsUrl.pathname += '/';
  388. wsUrl.pathname += wsEndpointPath;
  389. wsUrl.protocol = wsUrl.protocol === 'https:' ? 'wss:' : 'ws:';
  390. return wsUrl.toString();
  391. }