tracing.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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._isLive = false;
  31. this._isTracing = false;
  32. }
  33. static from(channel) {
  34. return channel._object;
  35. }
  36. async start(options = {}) {
  37. await this._wrapApiCall(async () => {
  38. this._includeSources = !!options.sources;
  39. this._isLive = !!options._live;
  40. await this._channel.tracingStart({
  41. name: options.name,
  42. snapshots: options.snapshots,
  43. screenshots: options.screenshots,
  44. live: options._live
  45. });
  46. const { traceName } = await this._channel.tracingStartChunk({ name: options.name, title: options.title });
  47. await this._startCollectingStacks(traceName, this._isLive);
  48. });
  49. }
  50. async startChunk(options = {}) {
  51. await this._wrapApiCall(async () => {
  52. const { traceName } = await this._channel.tracingStartChunk(options);
  53. await this._startCollectingStacks(traceName, this._isLive);
  54. });
  55. }
  56. async group(name, options = {}) {
  57. await this._channel.tracingGroup({ name, location: options.location });
  58. }
  59. async groupEnd() {
  60. await this._channel.tracingGroupEnd();
  61. }
  62. async _startCollectingStacks(traceName, live) {
  63. if (!this._isTracing) {
  64. this._isTracing = true;
  65. this._connection.setIsTracing(true);
  66. }
  67. const result = await this._connection.localUtils()?.tracingStarted({ tracesDir: this._tracesDir, traceName, live });
  68. this._stacksId = result?.stacksId;
  69. }
  70. async stopChunk(options = {}) {
  71. await this._wrapApiCall(async () => {
  72. await this._doStopChunk(options.path);
  73. });
  74. }
  75. async stop(options = {}) {
  76. await this._wrapApiCall(async () => {
  77. await this._doStopChunk(options.path);
  78. await this._channel.tracingStop();
  79. });
  80. }
  81. async _doStopChunk(filePath) {
  82. this._resetStackCounter();
  83. if (!filePath) {
  84. await this._channel.tracingStopChunk({ mode: "discard" });
  85. if (this._stacksId)
  86. await this._connection.localUtils().traceDiscarded({ stacksId: this._stacksId });
  87. return;
  88. }
  89. const localUtils = this._connection.localUtils();
  90. if (!localUtils)
  91. throw new Error("Cannot save trace in thin clients");
  92. const isLocal = !this._connection.isRemote();
  93. if (isLocal) {
  94. const result2 = await this._channel.tracingStopChunk({ mode: "entries" });
  95. await localUtils.zip({ zipFile: filePath, entries: result2.entries, mode: "write", stacksId: this._stacksId, includeSources: this._includeSources });
  96. return;
  97. }
  98. const result = await this._channel.tracingStopChunk({ mode: "archive" });
  99. if (!result.artifact) {
  100. if (this._stacksId)
  101. await localUtils.traceDiscarded({ stacksId: this._stacksId });
  102. return;
  103. }
  104. const artifact = import_artifact.Artifact.from(result.artifact);
  105. await artifact.saveAs(filePath);
  106. await artifact.delete();
  107. await localUtils.zip({ zipFile: filePath, entries: [], mode: "append", stacksId: this._stacksId, includeSources: this._includeSources });
  108. }
  109. _resetStackCounter() {
  110. if (this._isTracing) {
  111. this._isTracing = false;
  112. this._connection.setIsTracing(false);
  113. }
  114. }
  115. }
  116. // Annotate the CommonJS export names for ESM import in node:
  117. 0 && (module.exports = {
  118. Tracing
  119. });