tracing.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var tracing_exports = {};
  20. __export(tracing_exports, {
  21. Tracing: () => Tracing
  22. });
  23. module.exports = __toCommonJS(tracing_exports);
  24. var import_artifact = require("./artifact");
  25. var import_channelOwner = require("./channelOwner");
  26. class Tracing extends import_channelOwner.ChannelOwner {
  27. constructor(parent, type, guid, initializer) {
  28. super(parent, type, guid, initializer);
  29. this._includeSources = false;
  30. this._isTracing = false;
  31. }
  32. static from(channel) {
  33. return channel._object;
  34. }
  35. async start(options = {}) {
  36. await this._wrapApiCall(async () => {
  37. this._includeSources = !!options.sources;
  38. await this._channel.tracingStart({
  39. name: options.name,
  40. snapshots: options.snapshots,
  41. screenshots: options.screenshots,
  42. live: options._live
  43. });
  44. const { traceName } = await this._channel.tracingStartChunk({ name: options.name, title: options.title });
  45. await this._startCollectingStacks(traceName);
  46. });
  47. }
  48. async startChunk(options = {}) {
  49. await this._wrapApiCall(async () => {
  50. const { traceName } = await this._channel.tracingStartChunk(options);
  51. await this._startCollectingStacks(traceName);
  52. });
  53. }
  54. async group(name, options = {}) {
  55. await this._channel.tracingGroup({ name, location: options.location });
  56. }
  57. async groupEnd() {
  58. await this._channel.tracingGroupEnd();
  59. }
  60. async _startCollectingStacks(traceName) {
  61. if (!this._isTracing) {
  62. this._isTracing = true;
  63. this._connection.setIsTracing(true);
  64. }
  65. const result = await this._connection.localUtils()?.tracingStarted({ tracesDir: this._tracesDir, traceName });
  66. this._stacksId = result?.stacksId;
  67. }
  68. async stopChunk(options = {}) {
  69. await this._wrapApiCall(async () => {
  70. await this._doStopChunk(options.path);
  71. });
  72. }
  73. async stop(options = {}) {
  74. await this._wrapApiCall(async () => {
  75. await this._doStopChunk(options.path);
  76. await this._channel.tracingStop();
  77. });
  78. }
  79. async _doStopChunk(filePath) {
  80. this._resetStackCounter();
  81. if (!filePath) {
  82. await this._channel.tracingStopChunk({ mode: "discard" });
  83. if (this._stacksId)
  84. await this._connection.localUtils().traceDiscarded({ stacksId: this._stacksId });
  85. return;
  86. }
  87. const localUtils = this._connection.localUtils();
  88. if (!localUtils)
  89. throw new Error("Cannot save trace in thin clients");
  90. const isLocal = !this._connection.isRemote();
  91. if (isLocal) {
  92. const result2 = await this._channel.tracingStopChunk({ mode: "entries" });
  93. await localUtils.zip({ zipFile: filePath, entries: result2.entries, mode: "write", stacksId: this._stacksId, includeSources: this._includeSources });
  94. return;
  95. }
  96. const result = await this._channel.tracingStopChunk({ mode: "archive" });
  97. if (!result.artifact) {
  98. if (this._stacksId)
  99. await localUtils.traceDiscarded({ stacksId: this._stacksId });
  100. return;
  101. }
  102. const artifact = import_artifact.Artifact.from(result.artifact);
  103. await artifact.saveAs(filePath);
  104. await artifact.delete();
  105. await localUtils.zip({ zipFile: filePath, entries: [], mode: "append", stacksId: this._stacksId, includeSources: this._includeSources });
  106. }
  107. _resetStackCounter() {
  108. if (this._isTracing) {
  109. this._isTracing = false;
  110. this._connection.setIsTracing(false);
  111. }
  112. }
  113. }
  114. // Annotate the CommonJS export names for ESM import in node:
  115. 0 && (module.exports = {
  116. Tracing
  117. });