tracing.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Tracing = void 0;
  6. var _artifact = require("./artifact");
  7. var _channelOwner = require("./channelOwner");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class Tracing extends _channelOwner.ChannelOwner {
  24. static from(channel) {
  25. return channel._object;
  26. }
  27. constructor(parent, type, guid, initializer) {
  28. super(parent, type, guid, initializer);
  29. this._includeSources = false;
  30. this._tracesDir = void 0;
  31. this._stacksId = void 0;
  32. this._isTracing = false;
  33. }
  34. async start(options = {}) {
  35. this._includeSources = !!options.sources;
  36. const traceName = await this._wrapApiCall(async () => {
  37. await this._channel.tracingStart({
  38. name: options.name,
  39. snapshots: options.snapshots,
  40. screenshots: options.screenshots,
  41. live: options._live
  42. });
  43. const response = await this._channel.tracingStartChunk({
  44. name: options.name,
  45. title: options.title
  46. });
  47. return response.traceName;
  48. }, true);
  49. await this._startCollectingStacks(traceName);
  50. }
  51. async startChunk(options = {}) {
  52. const {
  53. traceName
  54. } = await this._channel.tracingStartChunk(options);
  55. await this._startCollectingStacks(traceName);
  56. }
  57. async _startCollectingStacks(traceName) {
  58. if (!this._isTracing) {
  59. this._isTracing = true;
  60. this._connection.setIsTracing(true);
  61. }
  62. const result = await this._connection.localUtils()._channel.tracingStarted({
  63. tracesDir: this._tracesDir,
  64. traceName
  65. });
  66. this._stacksId = result.stacksId;
  67. }
  68. async stopChunk(options = {}) {
  69. await this._wrapApiCall(async () => {
  70. await this._doStopChunk(options.path);
  71. }, true);
  72. }
  73. async stop(options = {}) {
  74. await this._wrapApiCall(async () => {
  75. await this._doStopChunk(options.path);
  76. await this._channel.tracingStop();
  77. }, true);
  78. }
  79. async _doStopChunk(filePath) {
  80. if (this._isTracing) {
  81. this._isTracing = false;
  82. this._connection.setIsTracing(false);
  83. }
  84. if (!filePath) {
  85. // Not interested in artifacts.
  86. await this._channel.tracingStopChunk({
  87. mode: 'discard'
  88. });
  89. if (this._stacksId) await this._connection.localUtils()._channel.traceDiscarded({
  90. stacksId: this._stacksId
  91. });
  92. return;
  93. }
  94. const isLocal = !this._connection.isRemote();
  95. if (isLocal) {
  96. const result = await this._channel.tracingStopChunk({
  97. mode: 'entries'
  98. });
  99. await this._connection.localUtils()._channel.zip({
  100. zipFile: filePath,
  101. entries: result.entries,
  102. mode: 'write',
  103. stacksId: this._stacksId,
  104. includeSources: this._includeSources
  105. });
  106. return;
  107. }
  108. const result = await this._channel.tracingStopChunk({
  109. mode: 'archive'
  110. });
  111. // The artifact may be missing if the browser closed while stopping tracing.
  112. if (!result.artifact) {
  113. if (this._stacksId) await this._connection.localUtils()._channel.traceDiscarded({
  114. stacksId: this._stacksId
  115. });
  116. return;
  117. }
  118. // Save trace to the final local file.
  119. const artifact = _artifact.Artifact.from(result.artifact);
  120. await artifact.saveAs(filePath);
  121. await artifact.delete();
  122. await this._connection.localUtils()._channel.zip({
  123. zipFile: filePath,
  124. entries: [],
  125. mode: 'append',
  126. stacksId: this._stacksId,
  127. includeSources: this._includeSources
  128. });
  129. }
  130. }
  131. exports.Tracing = Tracing;