feat(client): Allow tracing without FS
This commit is contained in:
parent
26a7dd4dd4
commit
c49ddaa6ff
748
package-lock.json
generated
748
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -62,6 +62,7 @@
|
|||
"@babel/plugin-transform-optional-chaining": "^7.23.4",
|
||||
"@babel/plugin-transform-typescript": "^7.23.6",
|
||||
"@babel/preset-react": "^7.23.3",
|
||||
"@types/archiver": "^6.0.3",
|
||||
"@types/babel__core": "^7.20.2",
|
||||
"@types/codemirror": "^5.60.7",
|
||||
"@types/formidable": "^2.0.4",
|
||||
|
|
@ -104,5 +105,9 @@
|
|||
"ws": "^8.17.1",
|
||||
"xml2js": "^0.5.0",
|
||||
"yaml": "^2.5.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"archiver": "^7.0.1",
|
||||
"stream": "^0.0.3"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,8 @@
|
|||
/* eslint-disable brace-style */
|
||||
/* eslint-disable nonblock-statement-body-position */
|
||||
/* eslint-disable arrow-parens */
|
||||
/* eslint-disable indent */
|
||||
/* eslint-disable quotes */
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
|
|
@ -13,28 +18,45 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type * as channels from "@protocol/channels";
|
||||
import archiver from "archiver";
|
||||
import { PassThrough } from "stream";
|
||||
import type * as api from "../../types/types";
|
||||
import { ChannelOwner } from "./channelOwner";
|
||||
|
||||
import type * as api from '../../types/types';
|
||||
import type * as channels from '@protocol/channels';
|
||||
import { Artifact } from './artifact';
|
||||
import { ChannelOwner } from './channelOwner';
|
||||
|
||||
export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing {
|
||||
export class Tracing
|
||||
extends ChannelOwner<channels.TracingChannel>
|
||||
implements api.Tracing
|
||||
{
|
||||
private _includeSources = false;
|
||||
_tracesDir: string | undefined;
|
||||
private _stacksId: string | undefined;
|
||||
private _isTracing = false;
|
||||
private _traceBuffer: Buffer | null = null;
|
||||
|
||||
static from(channel: channels.TracingChannel): Tracing {
|
||||
return (channel as any)._object;
|
||||
}
|
||||
|
||||
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.TracingInitializer) {
|
||||
constructor(
|
||||
parent: ChannelOwner,
|
||||
type: string,
|
||||
guid: string,
|
||||
initializer: channels.TracingInitializer
|
||||
) {
|
||||
super(parent, type, guid, initializer);
|
||||
this.markAsInternalType();
|
||||
}
|
||||
|
||||
async start(options: { name?: string, title?: string, snapshots?: boolean, screenshots?: boolean, sources?: boolean, _live?: boolean } = {}) {
|
||||
async start(
|
||||
options: {
|
||||
name?: string;
|
||||
title?: string;
|
||||
snapshots?: boolean;
|
||||
screenshots?: boolean;
|
||||
sources?: boolean;
|
||||
_live?: boolean;
|
||||
} = {}
|
||||
) {
|
||||
this._includeSources = !!options.sources;
|
||||
await this._channel.tracingStart({
|
||||
name: options.name,
|
||||
|
|
@ -42,11 +64,14 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
|
|||
screenshots: options.screenshots,
|
||||
live: options._live,
|
||||
});
|
||||
const { traceName } = await this._channel.tracingStartChunk({ name: options.name, title: options.title });
|
||||
const { traceName } = await this._channel.tracingStartChunk({
|
||||
name: options.name,
|
||||
title: options.title,
|
||||
});
|
||||
await this._startCollectingStacks(traceName);
|
||||
}
|
||||
|
||||
async startChunk(options: { name?: string, title?: string } = {}) {
|
||||
async startChunk(options: { name?: string; title?: string } = {}) {
|
||||
const { traceName } = await this._channel.tracingStartChunk(options);
|
||||
await this._startCollectingStacks(traceName);
|
||||
}
|
||||
|
|
@ -56,53 +81,47 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
|
|||
this._isTracing = true;
|
||||
this._connection.setIsTracing(true);
|
||||
}
|
||||
const result = await this._connection.localUtils()._channel.tracingStarted({ tracesDir: this._tracesDir, traceName });
|
||||
const result = await this._connection
|
||||
.localUtils()
|
||||
._channel.tracingStarted({ traceName });
|
||||
this._stacksId = result.stacksId;
|
||||
}
|
||||
|
||||
async stopChunk(options: { path?: string } = {}) {
|
||||
await this._doStopChunk(options.path);
|
||||
async stopChunk() {
|
||||
this._traceBuffer = await this._collectTraceAsBuffer();
|
||||
}
|
||||
|
||||
async stop(options: { path?: string } = {}) {
|
||||
await this._doStopChunk(options.path);
|
||||
async stop() {
|
||||
this._traceBuffer = await this._collectTraceAsBuffer();
|
||||
await this._channel.tracingStop();
|
||||
}
|
||||
|
||||
private async _doStopChunk(filePath: string | undefined) {
|
||||
this._resetStackCounter();
|
||||
private async _collectTraceAsBuffer(): Promise<Buffer> {
|
||||
const result = await this._channel.tracingStopChunk({ mode: "entries" });
|
||||
if (!result.entries) return Buffer.alloc(0);
|
||||
|
||||
if (!filePath) {
|
||||
// Not interested in artifacts.
|
||||
await this._channel.tracingStopChunk({ mode: 'discard' });
|
||||
if (this._stacksId)
|
||||
await this._connection.localUtils()._channel.traceDiscarded({ stacksId: this._stacksId });
|
||||
return;
|
||||
const archive = archiver("zip", { zlib: { level: 9 } });
|
||||
const chunks: Uint8Array[] = [];
|
||||
const passthrough = new PassThrough();
|
||||
|
||||
archive.pipe(passthrough);
|
||||
passthrough.on("data", (chunk: Buffer) =>
|
||||
chunks.push(Uint8Array.from(chunk))
|
||||
);
|
||||
archive.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
result.entries.forEach((entry) => {
|
||||
archive.append(Buffer.from(entry.value), { name: entry.name });
|
||||
});
|
||||
|
||||
await archive.finalize();
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
|
||||
const isLocal = !this._connection.isRemote();
|
||||
|
||||
if (isLocal) {
|
||||
const result = await this._channel.tracingStopChunk({ mode: 'entries' });
|
||||
await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: result.entries!, mode: 'write', stacksId: this._stacksId, includeSources: this._includeSources });
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await this._channel.tracingStopChunk({ mode: 'archive' });
|
||||
|
||||
// The artifact may be missing if the browser closed while stopping tracing.
|
||||
if (!result.artifact) {
|
||||
if (this._stacksId)
|
||||
await this._connection.localUtils()._channel.traceDiscarded({ stacksId: this._stacksId });
|
||||
return;
|
||||
}
|
||||
|
||||
// Save trace to the final local file.
|
||||
const artifact = Artifact.from(result.artifact);
|
||||
await artifact.saveAs(filePath);
|
||||
await artifact.delete();
|
||||
|
||||
await this._connection.localUtils()._channel.zip({ zipFile: filePath, entries: [], mode: 'append', stacksId: this._stacksId, includeSources: this._includeSources });
|
||||
getTraceBuffer(): Buffer | null {
|
||||
return this._traceBuffer;
|
||||
}
|
||||
|
||||
_resetStackCounter() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue