Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/isomorphic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ export * from './stringUtils';
export * from './formatUtils';
export * from './time';
export * from './timeoutRunner';
export * from './trace/snapshotServer';
export * from './urlMatch';
export * from './cssParser';
export * from './locatorParser';
export * from './selectorParser';
export * from './trace/snapshotStorage';
export * from './trace/traceLoader';
export * from './trace/traceModel';
export * from './trace/traceUtils';
// Re-exports for the `iso` runtime namespace (see playwright-core/coreBundle.ts).
// The code lives in packages/tracing/.
export * from '@tracing/reader/snapshotServer';
export * from '@tracing/reader/snapshotStorage';
export * from '@tracing/reader/traceLoader';
export * from '@tracing/reader/traceModel';
export * from '@tracing/reader/traceUtils';
export * from './yaml';
1 change: 0 additions & 1 deletion packages/isomorphic/trace/DEPS.list

This file was deleted.

1 change: 1 addition & 0 deletions packages/playwright-core/src/server/DEPS.list
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[*]
@isomorphic/**
@tracing/**
@utils/**
../generated/
../package.ts
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/server/dispatchers/DEPS.list
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
../../protocol/
@utils/**
@isomorphic/**
@tracing/**
../**
node_modules/yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import net from 'net';
import { resolveGlobToRegexPattern } from '@isomorphic/urlMatch';
import * as stackSession from '@tracing/writer/stackSession';
import * as tracingZip from '@tracing/writer/zip';
import { fetchData } from '../utils';
import { getUserAgent } from '../userAgent';
import { Dispatcher } from './dispatcher';
Expand All @@ -38,7 +40,7 @@ import type { HTTPRequestParams } from '@utils/network';
export class LocalUtilsDispatcher extends Dispatcher<SdkObject, channels.LocalUtilsChannel, RootDispatcher> implements channels.LocalUtilsChannel {
_type_LocalUtils: boolean;
private _harBackends = new Map<string, HarBackend>();
private _stackSessions = new Map<string, localUtils.StackSession>();
private _stackSessions = new Map<string, stackSession.StackSession>();

constructor(scope: RootDispatcher, playwright: Playwright) {
const localUtils = new SdkObject(playwright, 'localUtils', 'localUtils');
Expand All @@ -52,7 +54,7 @@ export class LocalUtilsDispatcher extends Dispatcher<SdkObject, channels.LocalUt
}

async zip(params: channels.LocalUtilsZipParams, progress: Progress): Promise<void> {
return await localUtils.zip(progress, this._stackSessions, params);
return await progress.race(tracingZip.zip(progress.signal, this._stackSessions, params));
}

async harOpen(params: channels.LocalUtilsHarOpenParams, progress: Progress): Promise<channels.LocalUtilsHarOpenResult> {
Expand All @@ -72,15 +74,15 @@ export class LocalUtilsDispatcher extends Dispatcher<SdkObject, channels.LocalUt
}

async tracingStarted(params: channels.LocalUtilsTracingStartedParams, progress: Progress): Promise<channels.LocalUtilsTracingStartedResult> {
return await localUtils.tracingStarted(progress, this._stackSessions, params);
return await progress.race(stackSession.tracingStarted(progress.signal, this._stackSessions, params));
}

async traceDiscarded(params: channels.LocalUtilsTraceDiscardedParams, progress: Progress): Promise<void> {
return await localUtils.traceDiscarded(progress, this._stackSessions, params);
return await progress.race(stackSession.traceDiscarded(progress.signal, this._stackSessions, params.stacksId));
}

async addStackToTracingNoReply(params: channels.LocalUtilsAddStackToTracingNoReplyParams, progress: Progress): Promise<void> {
localUtils.addStackToTracingNoReply(this._stackSessions, params);
stackSession.addStackToTracingNoReply(this._stackSessions, params.callData);
}

async connect(params: channels.LocalUtilsConnectParams, progress: Progress): Promise<channels.LocalUtilsConnectResult> {
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import type { HeadersArray, ProxySettings } from './types';
import type { HTTPCredentials } from '../../types/types';
import type { RegisteredListener } from '@utils/eventsHelper';
import type * as channels from '@protocol/channels';
import type * as har from '@trace/har';
import type * as har from '@tracing/format/har';
import type { LookupAddress } from 'dns';
import type { Readable, TransformCallback } from 'stream';

Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/har/harRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { HarTracerDelegate } from './harTracer';
import type { Page } from '../page';
import type { NameValue } from '@isomorphic/types';
import type * as channels from '@protocol/channels';
import type * as har from '@trace/har';
import type * as har from '@tracing/format/har';

export class HarRecorder implements HarTracerDelegate {
private _context: BrowserContext | APIRequestContext;
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/har/harTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { APIRequestEvent, APIRequestFinishedEvent } from '../fetch';
import type { Page } from '../page';
import type { Worker } from '../page';
import type { HeadersArray, LifecycleEvent } from '../types';
import type * as har from '@trace/har';
import type * as har from '@tracing/format/har';

const FALLBACK_HTTP_VERSION = 'HTTP/1.1';

Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/harBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { isPathInside } from '@utils/fileUtils';
import { ZipFile } from '@utils/zipFile';

import type { HeadersArray } from '@isomorphic/types';
import type * as har from '@trace/har';
import type * as har from '@tracing/format/har';

const redirectStatus = [301, 302, 303, 307, 308];

Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/server/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function createRootSdkObject() {
export interface Instrumentation {
addListener(listener: InstrumentationListener, context: BrowserContext | APIRequestContext | null): void;
removeListener(listener: InstrumentationListener): void;
onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata, parentId?: string): Promise<void>;
onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onBeforeInputAction(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onCallLog(sdkObject: SdkObject, metadata: CallMetadata, logName: string, message: string): void;
onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
Expand All @@ -95,7 +95,7 @@ export interface Instrumentation {
}

export interface InstrumentationListener {
onBeforeCall?(sdkObject: SdkObject, metadata: CallMetadata, parentId?: string): Promise<void>;
onBeforeCall?(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onBeforeInputAction?(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onCallLog?(sdkObject: SdkObject, metadata: CallMetadata, logName: string, message: string): void;
onAfterCall?(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
Expand Down
139 changes: 2 additions & 137 deletions packages/playwright-core/src/server/localUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,125 +15,15 @@
*/

import fs from 'fs';
import os from 'os';
import path from 'path';

import * as yauzl from 'yauzl';
import * as yazl from 'yazl';
import { ManualPromise } from '@isomorphic/manualPromise';
import { serializeClientSideCallMetadata } from '@isomorphic/trace/traceUtils';
import { assert } from '@isomorphic/assert';
import { calculateSha1 } from '@utils/crypto';
import { ZipFile } from '@utils/zipFile';
import { removeFolders } from '@utils/fileUtils';
import { HarBackend } from './harBackend';

import type * as channels from '@protocol/channels';
import type * as har from '@trace/har';
import type EventEmitter from 'events';
import type * as har from '@tracing/format/har';
import type { Progress } from '@protocol/progress';


export type StackSession = {
file: string;
writer: Promise<void>;
tmpDir: string | undefined;
callStacks: channels.ClientSideCallMetadata[];
live?: boolean;
};

export async function zip(progress: Progress, stackSessions: Map<string, StackSession>, params: channels.LocalUtilsZipParams): Promise<void> {
const promise = new ManualPromise<void>();
const zipFile = new yazl.ZipFile();
(zipFile as any as EventEmitter).on('error', error => promise.reject(error));

const addFile = (file: string, name: string) => {
try {
if (fs.statSync(file).isFile())
zipFile.addFile(file, name);
} catch (e) {
}
};

for (const entry of params.entries)
addFile(entry.value, entry.name);

// Add stacks and the sources.
const stackSession = params.stacksId ? stackSessions.get(params.stacksId) : undefined;
if (stackSession?.callStacks.length) {
await progress.race(stackSession.writer);
const buffer = Buffer.from(JSON.stringify(serializeClientSideCallMetadata(stackSession.callStacks)));
zipFile.addBuffer(buffer, 'trace.stacks');
}

// Collect sources from stacks.
if (params.includeSources) {
const sourceFiles = new Set<string>(params.additionalSources);
for (const { stack } of stackSession?.callStacks || []) {
if (!stack)
continue;
for (const { file } of stack)
sourceFiles.add(file);
}
for (const sourceFile of sourceFiles)
addFile(sourceFile, 'resources/src@' + calculateSha1(sourceFile) + '.txt');
}

if (params.mode === 'write') {
// New file, just compress the entries.
await progress.race(fs.promises.mkdir(path.dirname(params.zipFile), { recursive: true }));
zipFile.end(undefined, () => {
zipFile.outputStream.pipe(fs.createWriteStream(params.zipFile))
.on('close', () => promise.resolve())
.on('error', error => promise.reject(error));
});
await progress.race(promise);
await deleteStackSession(progress, stackSessions, params.stacksId);
return;
}

// File already exists. Repack and add new entries.
const tempFile = params.zipFile + '.tmp';
await progress.race(fs.promises.rename(params.zipFile, tempFile));

yauzl.open(tempFile, (err, inZipFile) => {
if (err) {
promise.reject(err);
return;
}
assert(inZipFile);
let pendingEntries = inZipFile.entryCount;
inZipFile.on('entry', entry => {
inZipFile.openReadStream(entry, (err, readStream) => {
if (err) {
promise.reject(err);
return;
}
zipFile.addReadStream(readStream!, entry.fileName);
if (--pendingEntries === 0) {
zipFile.end(undefined, () => {
zipFile.outputStream.pipe(fs.createWriteStream(params.zipFile)).on('close', () => {
fs.promises.unlink(tempFile).then(() => {
promise.resolve();
}).catch(error => promise.reject(error));
});
});
}
});
});
});
await progress.race(promise);
await deleteStackSession(progress, stackSessions, params.stacksId);
}

async function deleteStackSession(progress: Progress, stackSessions: Map<string, StackSession>, stacksId?: string) {
const session = stacksId ? stackSessions.get(stacksId) : undefined;
if (!session)
return;
stackSessions.delete(stacksId!);
if (session.tmpDir)
await progress.race(removeFolders([session.tmpDir]));
}

export async function harOpen(progress: Progress, harBackends: Map<string, HarBackend>, params: channels.LocalUtilsHarOpenParams): Promise<channels.LocalUtilsHarOpenResult> {
let harBackend: HarBackend;
if (params.file.endsWith('.zip')) {
Expand Down Expand Up @@ -195,28 +85,3 @@ export async function harUnzip(progress: Progress, params: channels.LocalUtilsHa
zipFile.close();
}
}

export async function tracingStarted(progress: Progress, stackSessions: Map<string, StackSession>, params: channels.LocalUtilsTracingStartedParams): Promise<channels.LocalUtilsTracingStartedResult> {
let tmpDir = undefined;
if (!params.tracesDir)
tmpDir = await progress.race(fs.promises.mkdtemp(path.join(os.tmpdir(), 'playwright-tracing-')));
const traceStacksFile = path.join(params.tracesDir || tmpDir!, params.traceName + '.stacks');
stackSessions.set(traceStacksFile, { callStacks: [], file: traceStacksFile, writer: Promise.resolve(), tmpDir, live: params.live });
return { stacksId: traceStacksFile };
}

export async function traceDiscarded(progress: Progress, stackSessions: Map<string, StackSession>, params: channels.LocalUtilsTraceDiscardedParams): Promise<void> {
await deleteStackSession(progress, stackSessions, params.stacksId);
}

export function addStackToTracingNoReply(stackSessions: Map<string, StackSession>, params: channels.LocalUtilsAddStackToTracingNoReplyParams) {
for (const session of stackSessions.values()) {
session.callStacks.push(params.callData);
if (session.live) {
session.writer = session.writer.then(() => {
const buffer = Buffer.from(JSON.stringify(serializeClientSideCallMetadata(session.callStacks)));
return fs.promises.writeFile(session.file, buffer);
});
}
}
}
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/screencast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class Screencast implements InstrumentationListener {
}
}

async onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata, parentId?: string): Promise<void> {
async onBeforeCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void> {
if (!this._actions)
return;
metadata.annotate = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[*]
@isomorphic/**
@tracing/**
@utils/**
../../
../../har/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import { monotonicTime } from '@isomorphic/time';
import { calculateSha1, createGuid } from '@utils/crypto';
import { debugLogger } from '@utils/debugLogger';
import { eventsHelper } from '@utils/eventsHelper';
import { frameSnapshotStreamer } from './snapshotterInjected';
import { frameSnapshotStreamer } from '@tracing/writer/snapshotterInjected';
import { BrowserContext } from '../../browserContext';
import { Page } from '../../page';
import { nullProgress } from '../../progress';

import type { SnapshotData } from './snapshotterInjected';
import type { SnapshotData } from '@tracing/writer/snapshotterInjected';
import type { RegisteredListener } from '@utils/eventsHelper';
import type { Frame } from '../../frames';
import type { InitScript } from '../../page';
import type { FrameSnapshot } from '@trace/snapshot';
import type { FrameSnapshot } from '@tracing/format/snapshot';
import type { Progress } from '../../progress';

export type SnapshotterBlob = {
Expand Down
Loading