mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: roll back interfaces for wrapping CDP (#10389)
This commit is contained in:
parent
3229a23f41
commit
ca4ab96812
@ -18,6 +18,8 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
|
|
||||||
import {ElementHandle} from '../api/ElementHandle.js';
|
import {ElementHandle} from '../api/ElementHandle.js';
|
||||||
|
|
||||||
|
import {CDPSession} from './Connection.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Node and the properties of it that are relevant to Accessibility.
|
* Represents a Node and the properties of it that are relevant to Accessibility.
|
||||||
* @public
|
* @public
|
||||||
@ -107,14 +109,6 @@ export interface SnapshotOptions {
|
|||||||
root?: ElementHandle<Node>;
|
root?: ElementHandle<Node>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export interface DataProvider {
|
|
||||||
getFullAXTree(): Promise<Protocol.Accessibility.GetFullAXTreeResponse>;
|
|
||||||
describeNode(id: string): Promise<Protocol.DOM.DescribeNodeResponse>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Accessibility class provides methods for inspecting the browser's
|
* The Accessibility class provides methods for inspecting the browser's
|
||||||
* accessibility tree. The accessibility tree is used by assistive technology
|
* accessibility tree. The accessibility tree is used by assistive technology
|
||||||
@ -138,13 +132,13 @@ export interface DataProvider {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export class Accessibility {
|
export class Accessibility {
|
||||||
#dataProvider: DataProvider;
|
#client: CDPSession;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
constructor(dataProvider: DataProvider) {
|
constructor(client: CDPSession) {
|
||||||
this.#dataProvider = dataProvider;
|
this.#client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -190,10 +184,12 @@ export class Accessibility {
|
|||||||
options: SnapshotOptions = {}
|
options: SnapshotOptions = {}
|
||||||
): Promise<SerializedAXNode | null> {
|
): Promise<SerializedAXNode | null> {
|
||||||
const {interestingOnly = true, root = null} = options;
|
const {interestingOnly = true, root = null} = options;
|
||||||
const {nodes} = await this.#dataProvider.getFullAXTree();
|
const {nodes} = await this.#client.send('Accessibility.getFullAXTree');
|
||||||
let backendNodeId: number | undefined;
|
let backendNodeId: number | undefined;
|
||||||
if (root && root.id) {
|
if (root) {
|
||||||
const {node} = await this.#dataProvider.describeNode(root.id);
|
const {node} = await this.#client.send('DOM.describeNode', {
|
||||||
|
objectId: root.id,
|
||||||
|
});
|
||||||
backendNodeId = node.backendNodeId;
|
backendNodeId = node.backendNodeId;
|
||||||
}
|
}
|
||||||
const defaultRoot = AXNode.createTree(nodes);
|
const defaultRoot = AXNode.createTree(nodes);
|
||||||
|
@ -167,16 +167,7 @@ export class CDPPage extends Page {
|
|||||||
this.#keyboard = new Keyboard(client);
|
this.#keyboard = new Keyboard(client);
|
||||||
this.#mouse = new Mouse(client, this.#keyboard);
|
this.#mouse = new Mouse(client, this.#keyboard);
|
||||||
this.#touchscreen = new Touchscreen(client, this.#keyboard);
|
this.#touchscreen = new Touchscreen(client, this.#keyboard);
|
||||||
this.#accessibility = new Accessibility({
|
this.#accessibility = new Accessibility(client);
|
||||||
describeNode(id: string) {
|
|
||||||
return client.send('DOM.describeNode', {
|
|
||||||
objectId: id,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getFullAXTree() {
|
|
||||||
return client.send('Accessibility.getFullAXTree');
|
|
||||||
},
|
|
||||||
});
|
|
||||||
this.#frameManager = new FrameManager(
|
this.#frameManager = new FrameManager(
|
||||||
client,
|
client,
|
||||||
this,
|
this,
|
||||||
@ -184,25 +175,7 @@ export class CDPPage extends Page {
|
|||||||
this.#timeoutSettings
|
this.#timeoutSettings
|
||||||
);
|
);
|
||||||
this.#emulationManager = new EmulationManager(client);
|
this.#emulationManager = new EmulationManager(client);
|
||||||
this.#tracing = new Tracing({
|
this.#tracing = new Tracing(client);
|
||||||
read: opts => {
|
|
||||||
return this.#client.send('IO.read', opts);
|
|
||||||
},
|
|
||||||
close: opts => {
|
|
||||||
return this.#client.send('IO.close', opts);
|
|
||||||
},
|
|
||||||
start: opts => {
|
|
||||||
return client.send('Tracing.start', opts);
|
|
||||||
},
|
|
||||||
stop: async () => {
|
|
||||||
const deferred = Deferred.create();
|
|
||||||
this.#client.once('Tracing.tracingComplete', event => {
|
|
||||||
deferred.resolve(event);
|
|
||||||
});
|
|
||||||
await this.#client.send('Tracing.end');
|
|
||||||
return deferred.valueOrThrow() as Promise<Protocol.Tracing.TracingCompleteEvent>;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
this.#coverage = new Coverage(client);
|
this.#coverage = new Coverage(client);
|
||||||
this.#screenshotTaskQueue = screenshotTaskQueue;
|
this.#screenshotTaskQueue = screenshotTaskQueue;
|
||||||
this.#viewport = null;
|
this.#viewport = null;
|
||||||
@ -1494,17 +1467,7 @@ export class CDPPage extends Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert(result.stream, '`stream` is missing from `Page.printToPDF');
|
assert(result.stream, '`stream` is missing from `Page.printToPDF');
|
||||||
return getReadableFromProtocolStream(
|
return getReadableFromProtocolStream(this.#client, result.stream);
|
||||||
{
|
|
||||||
read: opts => {
|
|
||||||
return this.#client.send('IO.read', opts);
|
|
||||||
},
|
|
||||||
close: opts => {
|
|
||||||
return this.#client.send('IO.close', opts);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
result.stream
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override async pdf(options: PDFOptions = {}): Promise<Buffer> {
|
override async pdf(options: PDFOptions = {}): Promise<Buffer> {
|
||||||
|
@ -13,15 +13,12 @@
|
|||||||
* 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 Protocol from 'devtools-protocol';
|
|
||||||
|
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {Deferred} from '../util/Deferred.js';
|
||||||
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
|
|
||||||
import {
|
import {CDPSession} from './Connection.js';
|
||||||
getReadableAsBuffer,
|
import {getReadableAsBuffer, getReadableFromProtocolStream} from './util.js';
|
||||||
getReadableFromProtocolStream,
|
|
||||||
ProtocolReadable,
|
|
||||||
} from './util.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
@ -32,14 +29,6 @@ export interface TracingOptions {
|
|||||||
categories?: string[];
|
categories?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export interface TracingSource extends ProtocolReadable {
|
|
||||||
start(opts: Protocol.Tracing.StartRequest): Promise<void>;
|
|
||||||
stop(): Promise<Protocol.Tracing.TracingCompleteEvent>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Tracing class exposes the tracing audit interface.
|
* The Tracing class exposes the tracing audit interface.
|
||||||
* @remarks
|
* @remarks
|
||||||
@ -57,15 +46,15 @@ export interface TracingSource extends ProtocolReadable {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export class Tracing {
|
export class Tracing {
|
||||||
#source: TracingSource;
|
#client: CDPSession;
|
||||||
#recording = false;
|
#recording = false;
|
||||||
#path?: string;
|
#path?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
constructor(source: TracingSource) {
|
constructor(client: CDPSession) {
|
||||||
this.#source = source;
|
this.#client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,7 +102,7 @@ export class Tracing {
|
|||||||
|
|
||||||
this.#path = path;
|
this.#path = path;
|
||||||
this.#recording = true;
|
this.#recording = true;
|
||||||
await this.#source.start({
|
await this.#client.send('Tracing.start', {
|
||||||
transferMode: 'ReturnAsStream',
|
transferMode: 'ReturnAsStream',
|
||||||
traceConfig: {
|
traceConfig: {
|
||||||
excludedCategories,
|
excludedCategories,
|
||||||
@ -127,13 +116,25 @@ export class Tracing {
|
|||||||
* @returns Promise which resolves to buffer with trace data.
|
* @returns Promise which resolves to buffer with trace data.
|
||||||
*/
|
*/
|
||||||
async stop(): Promise<Buffer | undefined> {
|
async stop(): Promise<Buffer | undefined> {
|
||||||
const result = await this.#source.stop();
|
const contentDeferred = Deferred.create<Buffer | undefined>();
|
||||||
const readable = await getReadableFromProtocolStream(
|
this.#client.once('Tracing.tracingComplete', async event => {
|
||||||
this.#source,
|
try {
|
||||||
result.stream!
|
const readable = await getReadableFromProtocolStream(
|
||||||
);
|
this.#client,
|
||||||
const buffer = await getReadableAsBuffer(readable, this.#path);
|
event.stream
|
||||||
|
);
|
||||||
|
const buffer = await getReadableAsBuffer(readable, this.#path);
|
||||||
|
contentDeferred.resolve(buffer ?? undefined);
|
||||||
|
} catch (error) {
|
||||||
|
if (isErrorLike(error)) {
|
||||||
|
contentDeferred.reject(error);
|
||||||
|
} else {
|
||||||
|
contentDeferred.reject(new Error(`Unknown error: ${error}`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await this.#client.send('Tracing.end');
|
||||||
this.#recording = false;
|
this.#recording = false;
|
||||||
return buffer ?? undefined;
|
return contentDeferred.valueOrThrow();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
import type {Readable} from 'stream';
|
import type {Readable} from 'stream';
|
||||||
|
|
||||||
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
||||||
import Protocol from 'devtools-protocol';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Page as PageBase,
|
Page as PageBase,
|
||||||
@ -144,41 +143,10 @@ export class Page extends PageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: https://github.com/w3c/webdriver-bidi/issues/443
|
// TODO: https://github.com/w3c/webdriver-bidi/issues/443
|
||||||
this.#accessibility = new Accessibility({
|
this.#accessibility = new Accessibility(
|
||||||
describeNode: (id: string) => {
|
this.mainFrame().context().cdpSession
|
||||||
return this.mainFrame().context().sendCDPCommand('DOM.describeNode', {
|
);
|
||||||
objectId: id,
|
this.#tracing = new Tracing(this.mainFrame().context().cdpSession);
|
||||||
});
|
|
||||||
},
|
|
||||||
getFullAXTree: () => {
|
|
||||||
return this.mainFrame()
|
|
||||||
.context()
|
|
||||||
.sendCDPCommand('Accessibility.getFullAXTree');
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
this.#tracing = new Tracing({
|
|
||||||
read: opts => {
|
|
||||||
return this.mainFrame().context().sendCDPCommand('IO.read', opts);
|
|
||||||
},
|
|
||||||
close: opts => {
|
|
||||||
return this.mainFrame().context().sendCDPCommand('IO.close', opts);
|
|
||||||
},
|
|
||||||
start: opts => {
|
|
||||||
return this.mainFrame().context().sendCDPCommand('Tracing.start', opts);
|
|
||||||
},
|
|
||||||
stop: async () => {
|
|
||||||
const deferred = Deferred.create();
|
|
||||||
this.mainFrame()
|
|
||||||
.context()
|
|
||||||
.cdpSession.once('Tracing.tracingComplete', event => {
|
|
||||||
deferred.resolve(event);
|
|
||||||
});
|
|
||||||
await this.mainFrame().context().sendCDPCommand('Tracing.end');
|
|
||||||
return deferred.valueOrThrow() as Promise<Protocol.Tracing.TracingCompleteEvent>;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
this.#coverage = new Coverage(this.mainFrame().context().cdpSession);
|
this.#coverage = new Coverage(this.mainFrame().context().cdpSession);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -574,19 +574,11 @@ export async function getReadableAsBuffer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
export interface ProtocolReadable {
|
|
||||||
read(opts: {handle: string; size: number}): Promise<Protocol.IO.ReadResponse>;
|
|
||||||
close(opts: {handle: string}): Promise<void>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export async function getReadableFromProtocolStream(
|
export async function getReadableFromProtocolStream(
|
||||||
source: ProtocolReadable,
|
client: CDPSession,
|
||||||
handle: string
|
handle: string
|
||||||
): Promise<Readable> {
|
): Promise<Readable> {
|
||||||
// TODO: Once Node 18 becomes the lowest supported version, we can migrate to
|
// TODO: Once Node 18 becomes the lowest supported version, we can migrate to
|
||||||
@ -605,11 +597,11 @@ export async function getReadableFromProtocolStream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await source.read({handle, size});
|
const response = await client.send('IO.read', {handle, size});
|
||||||
this.push(response.data, response.base64Encoded ? 'base64' : undefined);
|
this.push(response.data, response.base64Encoded ? 'base64' : undefined);
|
||||||
if (response.eof) {
|
if (response.eof) {
|
||||||
eof = true;
|
eof = true;
|
||||||
await source.close({handle});
|
await client.send('IO.close', {handle});
|
||||||
this.push(null);
|
this.push(null);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user