feat(client): Allow tracing without FS

This commit is contained in:
Edward Jibson 2024-11-05 10:34:03 +00:00
parent 26a7dd4dd4
commit c49ddaa6ff
3 changed files with 789 additions and 73 deletions

748
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -62,6 +62,7 @@
"@babel/plugin-transform-optional-chaining": "^7.23.4", "@babel/plugin-transform-optional-chaining": "^7.23.4",
"@babel/plugin-transform-typescript": "^7.23.6", "@babel/plugin-transform-typescript": "^7.23.6",
"@babel/preset-react": "^7.23.3", "@babel/preset-react": "^7.23.3",
"@types/archiver": "^6.0.3",
"@types/babel__core": "^7.20.2", "@types/babel__core": "^7.20.2",
"@types/codemirror": "^5.60.7", "@types/codemirror": "^5.60.7",
"@types/formidable": "^2.0.4", "@types/formidable": "^2.0.4",
@ -104,5 +105,9 @@
"ws": "^8.17.1", "ws": "^8.17.1",
"xml2js": "^0.5.0", "xml2js": "^0.5.0",
"yaml": "^2.5.1" "yaml": "^2.5.1"
},
"dependencies": {
"archiver": "^7.0.1",
"stream": "^0.0.3"
} }
} }

View file

@ -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. * Copyright (c) Microsoft Corporation.
* *
@ -13,28 +18,45 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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'; export class Tracing
import type * as channels from '@protocol/channels'; extends ChannelOwner<channels.TracingChannel>
import { Artifact } from './artifact'; implements api.Tracing
import { ChannelOwner } from './channelOwner'; {
export class Tracing extends ChannelOwner<channels.TracingChannel> implements api.Tracing {
private _includeSources = false; private _includeSources = false;
_tracesDir: string | undefined;
private _stacksId: string | undefined; private _stacksId: string | undefined;
private _isTracing = false; private _isTracing = false;
private _traceBuffer: Buffer | null = null;
static from(channel: channels.TracingChannel): Tracing { static from(channel: channels.TracingChannel): Tracing {
return (channel as any)._object; 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); super(parent, type, guid, initializer);
this.markAsInternalType(); 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; this._includeSources = !!options.sources;
await this._channel.tracingStart({ await this._channel.tracingStart({
name: options.name, name: options.name,
@ -42,11 +64,14 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
screenshots: options.screenshots, screenshots: options.screenshots,
live: options._live, 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); 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); const { traceName } = await this._channel.tracingStartChunk(options);
await this._startCollectingStacks(traceName); await this._startCollectingStacks(traceName);
} }
@ -56,53 +81,47 @@ export class Tracing extends ChannelOwner<channels.TracingChannel> implements ap
this._isTracing = true; this._isTracing = true;
this._connection.setIsTracing(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; this._stacksId = result.stacksId;
} }
async stopChunk(options: { path?: string } = {}) { async stopChunk() {
await this._doStopChunk(options.path); this._traceBuffer = await this._collectTraceAsBuffer();
} }
async stop(options: { path?: string } = {}) { async stop() {
await this._doStopChunk(options.path); this._traceBuffer = await this._collectTraceAsBuffer();
await this._channel.tracingStop(); await this._channel.tracingStop();
} }
private async _doStopChunk(filePath: string | undefined) { private async _collectTraceAsBuffer(): Promise<Buffer> {
this._resetStackCounter(); const result = await this._channel.tracingStopChunk({ mode: "entries" });
if (!result.entries) return Buffer.alloc(0);
if (!filePath) { const archive = archiver("zip", { zlib: { level: 9 } });
// Not interested in artifacts. const chunks: Uint8Array[] = [];
await this._channel.tracingStopChunk({ mode: 'discard' }); const passthrough = new PassThrough();
if (this._stacksId)
await this._connection.localUtils()._channel.traceDiscarded({ stacksId: this._stacksId });
return;
}
const isLocal = !this._connection.isRemote(); archive.pipe(passthrough);
passthrough.on("data", (chunk: Buffer) =>
chunks.push(Uint8Array.from(chunk))
);
archive.on("error", (err) => {
throw err;
});
if (isLocal) { result.entries.forEach((entry) => {
const result = await this._channel.tracingStopChunk({ mode: 'entries' }); archive.append(Buffer.from(entry.value), { name: entry.name });
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' }); await archive.finalize();
return Buffer.concat(chunks);
}
// The artifact may be missing if the browser closed while stopping tracing. getTraceBuffer(): Buffer | null {
if (!result.artifact) { return this._traceBuffer;
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 });
} }
_resetStackCounter() { _resetStackCounter() {