localUtils.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var localUtils_exports = {};
  30. __export(localUtils_exports, {
  31. addStackToTracingNoReply: () => addStackToTracingNoReply,
  32. harClose: () => harClose,
  33. harLookup: () => harLookup,
  34. harOpen: () => harOpen,
  35. harUnzip: () => harUnzip,
  36. traceDiscarded: () => traceDiscarded,
  37. tracingStarted: () => tracingStarted,
  38. zip: () => zip
  39. });
  40. module.exports = __toCommonJS(localUtils_exports);
  41. var import_fs = __toESM(require("fs"));
  42. var import_os = __toESM(require("os"));
  43. var import_path = __toESM(require("path"));
  44. var import_crypto = require("./utils/crypto");
  45. var import_harBackend = require("./harBackend");
  46. var import_manualPromise = require("../utils/isomorphic/manualPromise");
  47. var import_zipFile = require("./utils/zipFile");
  48. var import_zipBundle = require("../zipBundle");
  49. var import_traceUtils = require("../utils/isomorphic/traceUtils");
  50. var import_assert = require("../utils/isomorphic/assert");
  51. var import_fileUtils = require("./utils/fileUtils");
  52. async function zip(progress, stackSessions, params) {
  53. const promise = new import_manualPromise.ManualPromise();
  54. const zipFile = new import_zipBundle.yazl.ZipFile();
  55. zipFile.on("error", (error) => promise.reject(error));
  56. const addFile = (file, name) => {
  57. try {
  58. if (import_fs.default.statSync(file).isFile())
  59. zipFile.addFile(file, name);
  60. } catch (e) {
  61. }
  62. };
  63. for (const entry of params.entries)
  64. addFile(entry.value, entry.name);
  65. const stackSession = params.stacksId ? stackSessions.get(params.stacksId) : void 0;
  66. if (stackSession?.callStacks.length) {
  67. await progress.race(stackSession.writer);
  68. const buffer = Buffer.from(JSON.stringify((0, import_traceUtils.serializeClientSideCallMetadata)(stackSession.callStacks)));
  69. zipFile.addBuffer(buffer, "trace.stacks");
  70. }
  71. if (params.includeSources) {
  72. const sourceFiles = /* @__PURE__ */ new Set();
  73. for (const { stack } of stackSession?.callStacks || []) {
  74. if (!stack)
  75. continue;
  76. for (const { file } of stack)
  77. sourceFiles.add(file);
  78. }
  79. for (const sourceFile of sourceFiles)
  80. addFile(sourceFile, "resources/src@" + await (0, import_crypto.calculateSha1)(sourceFile) + ".txt");
  81. }
  82. if (params.mode === "write") {
  83. await progress.race(import_fs.default.promises.mkdir(import_path.default.dirname(params.zipFile), { recursive: true }));
  84. zipFile.end(void 0, () => {
  85. zipFile.outputStream.pipe(import_fs.default.createWriteStream(params.zipFile)).on("close", () => promise.resolve()).on("error", (error) => promise.reject(error));
  86. });
  87. await progress.race(promise);
  88. await deleteStackSession(progress, stackSessions, params.stacksId);
  89. return;
  90. }
  91. const tempFile = params.zipFile + ".tmp";
  92. await progress.race(import_fs.default.promises.rename(params.zipFile, tempFile));
  93. import_zipBundle.yauzl.open(tempFile, (err, inZipFile) => {
  94. if (err) {
  95. promise.reject(err);
  96. return;
  97. }
  98. (0, import_assert.assert)(inZipFile);
  99. let pendingEntries = inZipFile.entryCount;
  100. inZipFile.on("entry", (entry) => {
  101. inZipFile.openReadStream(entry, (err2, readStream) => {
  102. if (err2) {
  103. promise.reject(err2);
  104. return;
  105. }
  106. zipFile.addReadStream(readStream, entry.fileName);
  107. if (--pendingEntries === 0) {
  108. zipFile.end(void 0, () => {
  109. zipFile.outputStream.pipe(import_fs.default.createWriteStream(params.zipFile)).on("close", () => {
  110. import_fs.default.promises.unlink(tempFile).then(() => {
  111. promise.resolve();
  112. }).catch((error) => promise.reject(error));
  113. });
  114. });
  115. }
  116. });
  117. });
  118. });
  119. await progress.race(promise);
  120. await deleteStackSession(progress, stackSessions, params.stacksId);
  121. }
  122. async function deleteStackSession(progress, stackSessions, stacksId) {
  123. const session = stacksId ? stackSessions.get(stacksId) : void 0;
  124. if (!session)
  125. return;
  126. stackSessions.delete(stacksId);
  127. if (session.tmpDir)
  128. await progress.race((0, import_fileUtils.removeFolders)([session.tmpDir]));
  129. }
  130. async function harOpen(progress, harBackends, params) {
  131. let harBackend;
  132. if (params.file.endsWith(".zip")) {
  133. const zipFile = new import_zipFile.ZipFile(params.file);
  134. try {
  135. const entryNames = await progress.race(zipFile.entries());
  136. const harEntryName = entryNames.find((e) => e.endsWith(".har"));
  137. if (!harEntryName)
  138. return { error: "Specified archive does not have a .har file" };
  139. const har = await progress.race(zipFile.read(harEntryName));
  140. const harFile = JSON.parse(har.toString());
  141. harBackend = new import_harBackend.HarBackend(harFile, null, zipFile);
  142. } catch (error) {
  143. zipFile.close();
  144. throw error;
  145. }
  146. } else {
  147. const harFile = JSON.parse(await progress.race(import_fs.default.promises.readFile(params.file, "utf-8")));
  148. harBackend = new import_harBackend.HarBackend(harFile, import_path.default.dirname(params.file), null);
  149. }
  150. harBackends.set(harBackend.id, harBackend);
  151. return { harId: harBackend.id };
  152. }
  153. async function harLookup(progress, harBackends, params) {
  154. const harBackend = harBackends.get(params.harId);
  155. if (!harBackend)
  156. return { action: "error", message: `Internal error: har was not opened` };
  157. return await progress.race(harBackend.lookup(params.url, params.method, params.headers, params.postData, params.isNavigationRequest));
  158. }
  159. function harClose(harBackends, params) {
  160. const harBackend = harBackends.get(params.harId);
  161. if (harBackend) {
  162. harBackends.delete(harBackend.id);
  163. harBackend.dispose();
  164. }
  165. }
  166. async function harUnzip(progress, params) {
  167. const dir = import_path.default.dirname(params.zipFile);
  168. const zipFile = new import_zipFile.ZipFile(params.zipFile);
  169. try {
  170. for (const entry of await progress.race(zipFile.entries())) {
  171. const buffer = await progress.race(zipFile.read(entry));
  172. if (entry === "har.har")
  173. await progress.race(import_fs.default.promises.writeFile(params.harFile, buffer));
  174. else
  175. await progress.race(import_fs.default.promises.writeFile(import_path.default.join(dir, entry), buffer));
  176. }
  177. await progress.race(import_fs.default.promises.unlink(params.zipFile));
  178. } finally {
  179. zipFile.close();
  180. }
  181. }
  182. async function tracingStarted(progress, stackSessions, params) {
  183. let tmpDir = void 0;
  184. if (!params.tracesDir)
  185. tmpDir = await progress.race(import_fs.default.promises.mkdtemp(import_path.default.join(import_os.default.tmpdir(), "playwright-tracing-")));
  186. const traceStacksFile = import_path.default.join(params.tracesDir || tmpDir, params.traceName + ".stacks");
  187. stackSessions.set(traceStacksFile, { callStacks: [], file: traceStacksFile, writer: Promise.resolve(), tmpDir, live: params.live });
  188. return { stacksId: traceStacksFile };
  189. }
  190. async function traceDiscarded(progress, stackSessions, params) {
  191. await deleteStackSession(progress, stackSessions, params.stacksId);
  192. }
  193. function addStackToTracingNoReply(stackSessions, params) {
  194. for (const session of stackSessions.values()) {
  195. session.callStacks.push(params.callData);
  196. if (session.live) {
  197. session.writer = session.writer.then(() => {
  198. const buffer = Buffer.from(JSON.stringify((0, import_traceUtils.serializeClientSideCallMetadata)(session.callStacks)));
  199. return import_fs.default.promises.writeFile(session.file, buffer);
  200. });
  201. }
  202. }
  203. }
  204. // Annotate the CommonJS export names for ESM import in node:
  205. 0 && (module.exports = {
  206. addStackToTracingNoReply,
  207. harClose,
  208. harLookup,
  209. harOpen,
  210. harUnzip,
  211. traceDiscarded,
  212. tracingStarted,
  213. zip
  214. });