Tons of Solutions Engineering work done today for the rest of the CS team! Headway, Howard Hanna, Engels, Brighton, etc. Also completed Datasnippers auth flow and worked on Anthology's script. Cloned Anthology's courses (900..) and will clone Full Story on Monday.

This commit is contained in:
Norm Rasmussen
2024-01-05 17:07:59 -05:00
parent ce261975ca
commit a5fe4bd2c8
3157 changed files with 554269 additions and 16 deletions

View File

@ -0,0 +1,54 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
import { EventEmitter } from '../utils/EventEmitter.js';
import type { CdpConnection } from './CdpConnection.js';
export type CdpEvents = {
[Property in keyof ProtocolMapping.Events]: ProtocolMapping.Events[Property][0];
};
/** A error that will be thrown if/when the connection is closed. */
export declare class CloseError extends Error {
}
export interface ICdpClient extends EventEmitter<CdpEvents> {
/** Unique session identifier. */
sessionId: Protocol.Target.SessionID | undefined;
/**
* Provides an unique way to detect if an error was caused by the closure of a
* Target or Session.
*
* @example During the creation of a subframe we navigate the main frame.
* The subframe Target is closed while initialized commands are in-flight.
* In this case we want to swallow the thrown error.
*/
isCloseError(error: unknown): boolean;
/**
* Returns a command promise, which will be resolved with the command result
* after receiving the result from CDP.
* @param method Name of the CDP command to call.
* @param params Parameters to pass to the CDP command.
*/
sendCommand<CdpMethod extends keyof ProtocolMapping.Commands>(method: CdpMethod, params?: ProtocolMapping.Commands[CdpMethod]['paramsType'][0]): Promise<ProtocolMapping.Commands[CdpMethod]['returnType']>;
}
/** Represents a high-level CDP connection to the browser. */
export declare class CdpClient extends EventEmitter<CdpEvents> implements ICdpClient {
#private;
constructor(cdpConnection: CdpConnection, sessionId?: Protocol.Target.SessionID);
get sessionId(): Protocol.Target.SessionID | undefined;
sendCommand<CdpMethod extends keyof ProtocolMapping.Commands>(method: CdpMethod, ...params: ProtocolMapping.Commands[CdpMethod]['paramsType']): Promise<ProtocolMapping.Commands[CdpMethod]['returnType']>;
isCloseError(error: unknown): boolean;
}

View File

@ -0,0 +1,45 @@
"use strict";
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdpClient = exports.CloseError = void 0;
const EventEmitter_js_1 = require("../utils/EventEmitter.js");
/** A error that will be thrown if/when the connection is closed. */
class CloseError extends Error {
}
exports.CloseError = CloseError;
/** Represents a high-level CDP connection to the browser. */
class CdpClient extends EventEmitter_js_1.EventEmitter {
#cdpConnection;
#sessionId;
constructor(cdpConnection, sessionId) {
super();
this.#cdpConnection = cdpConnection;
this.#sessionId = sessionId;
}
get sessionId() {
return this.#sessionId;
}
sendCommand(method, ...params) {
return this.#cdpConnection.sendCommand(method, params[0], this.#sessionId);
}
isCloseError(error) {
return error instanceof CloseError;
}
}
exports.CdpClient = CdpClient;
//# sourceMappingURL=CdpClient.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"CdpClient.js","sourceRoot":"","sources":["../../../src/cdp/CdpClient.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AAKH,8DAAsD;AAQtD,oEAAoE;AACpE,MAAa,UAAW,SAAQ,KAAK;CAAG;AAAxC,gCAAwC;AA4BxC,6DAA6D;AAC7D,MAAa,SAAU,SAAQ,8BAAuB;IACpD,cAAc,CAAgB;IAC9B,UAAU,CAA6B;IAEvC,YACE,aAA4B,EAC5B,SAAqC;QAErC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;QACpC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,WAAW,CACT,MAAiB,EACjB,GAAG,MAAyD;QAE5D,OAAO,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7E,CAAC;IAED,YAAY,CAAC,KAAc;QACzB,OAAO,KAAK,YAAY,UAAU,CAAC;IACrC,CAAC;CACF;AA3BD,8BA2BC"}

View File

@ -0,0 +1,45 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
import type { LoggerFn } from '../utils/log.js';
import type { ITransport } from '../utils/transport.js';
import { CdpClient, type ICdpClient } from './CdpClient.js';
export interface ICdpConnection {
getCdpClient(sessionId: Protocol.Target.SessionID): ICdpClient;
}
/**
* Represents a high-level CDP connection to the browser backend.
*
* Manages all CdpClients (each backed by a Session ID) instance for each active
* CDP session.
*/
export declare class CdpConnection implements ICdpConnection {
#private;
static readonly LOGGER_PREFIX_RECV: "cdp:RECV ◂";
static readonly LOGGER_PREFIX_SEND: "cdp:SEND ▸";
constructor(transport: ITransport, logger?: LoggerFn);
/** Closes the connection to the browser. */
close(): void;
createBrowserSession(): Promise<ICdpClient>;
/**
* Gets a CdpClient instance attached to the given session ID,
* or null if the session is not attached.
*/
getCdpClient(sessionId: Protocol.Target.SessionID): CdpClient;
sendCommand<CdpMethod extends keyof ProtocolMapping.Commands>(method: CdpMethod, params?: ProtocolMapping.Commands[CdpMethod]['paramsType'][0], sessionId?: Protocol.Target.SessionID): Promise<object>;
}

View File

@ -0,0 +1,125 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CdpConnection = void 0;
const log_js_1 = require("../utils/log.js");
const CdpClient_js_1 = require("./CdpClient.js");
/**
* Represents a high-level CDP connection to the browser backend.
*
* Manages all CdpClients (each backed by a Session ID) instance for each active
* CDP session.
*/
class CdpConnection {
static LOGGER_PREFIX_RECV = `${log_js_1.LogType.cdp}:RECV ◂`;
static LOGGER_PREFIX_SEND = `${log_js_1.LogType.cdp}:SEND ▸`;
#mainBrowserCdpClient;
#transport;
/** Map from session ID to CdpClient.
* `undefined` points to the main browser session. */
#sessionCdpClients = new Map();
#commandCallbacks = new Map();
#logger;
#nextId = 0;
constructor(transport, logger) {
this.#transport = transport;
this.#logger = logger;
this.#transport.setOnMessage(this.#onMessage);
// Create default Browser CDP Session.
this.#mainBrowserCdpClient = this.#createCdpClient(undefined);
}
/** Closes the connection to the browser. */
close() {
this.#transport.close();
for (const [, { reject, error }] of this.#commandCallbacks) {
reject(error);
}
this.#commandCallbacks.clear();
this.#sessionCdpClients.clear();
}
async createBrowserSession() {
const { sessionId } = await this.#mainBrowserCdpClient.sendCommand('Target.attachToBrowserTarget');
return this.#createCdpClient(sessionId);
}
/**
* Gets a CdpClient instance attached to the given session ID,
* or null if the session is not attached.
*/
getCdpClient(sessionId) {
const cdpClient = this.#sessionCdpClients.get(sessionId);
if (!cdpClient) {
throw new Error(`Unknown CDP session ID: ${sessionId}`);
}
return cdpClient;
}
sendCommand(method, params, sessionId) {
return new Promise((resolve, reject) => {
const id = this.#nextId++;
this.#commandCallbacks.set(id, {
resolve,
reject,
error: new CdpClient_js_1.CloseError(`${method} ${JSON.stringify(params)} ${sessionId ?? ''} call rejected because the connection has been closed.`),
});
const cdpMessage = { id, method, params };
if (sessionId) {
cdpMessage.sessionId = sessionId;
}
void this.#transport
.sendMessage(JSON.stringify(cdpMessage))
?.catch((error) => {
this.#logger?.(log_js_1.LogType.debugError, error);
this.#transport.close();
});
this.#logger?.(CdpConnection.LOGGER_PREFIX_SEND, cdpMessage);
});
}
#onMessage = (json) => {
const message = JSON.parse(json);
this.#logger?.(CdpConnection.LOGGER_PREFIX_RECV, message);
// Update client map if a session is attached
// Listen for these events on every session.
if (message.method === 'Target.attachedToTarget') {
const { sessionId } = message.params;
this.#createCdpClient(sessionId);
}
if (message.id !== undefined) {
// Handle command response.
const callbacks = this.#commandCallbacks.get(message.id);
this.#commandCallbacks.delete(message.id);
if (callbacks) {
if (message.result) {
callbacks.resolve(message.result);
}
else if (message.error) {
callbacks.reject(message.error);
}
}
}
else if (message.method) {
const client = this.#sessionCdpClients.get(message.sessionId ?? undefined);
client?.emit(message.method, message.params || {});
// Update client map if a session is detached
// But emit on that session
if (message.method === 'Target.detachedFromTarget') {
const { sessionId } = message.params;
const client = this.#sessionCdpClients.get(sessionId);
if (client) {
this.#sessionCdpClients.delete(sessionId);
client.removeAllListeners();
}
}
}
};
/**
* Creates a new CdpClient instance for the given session ID.
* @param sessionId either a string, or undefined for the main browser session.
* The main browser session is used only to create new browser sessions.
* @private
*/
#createCdpClient(sessionId) {
const cdpClient = new CdpClient_js_1.CdpClient(this, sessionId);
this.#sessionCdpClients.set(sessionId, cdpClient);
return cdpClient;
}
}
exports.CdpConnection = CdpConnection;
//# sourceMappingURL=CdpConnection.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"CdpConnection.js","sourceRoot":"","sources":["../../../src/cdp/CdpConnection.ts"],"names":[],"m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,CAAC,MAAM,KAAK,2BAA2B,EAAE,CAAC;gBACnD,MAAM,EAAC,SAAS,EAAC,GAAG,OAAO,CAAC,MAAM,CAAC;gBACnC,MAAM,MAAM,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;gBACtD,IAAI,MAAM,EAAE,CAAC;oBACX,IAAI,CAAC,kBAAkB,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;oBAC1C,MAAM,CAAC,kBAAkB,EAAE,CAAC;gBAC9B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IAEF;;;;;OAKG;IACH,gBAAgB,CACd,SAAgD;QAEhD,MAAM,SAAS,GAAG,IAAI,wBAAS,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACjD,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QAClD,OAAO,SAAS,CAAC;IACnB,CAAC;;AA3IH,sCA4IC"}

View File

@ -0,0 +1,30 @@
/**
* Copyright 2021 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Protocol } from 'devtools-protocol';
import type { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
export interface CdpError {
code: number;
message: string;
}
export interface CdpMessage<CdpMethod extends keyof ProtocolMapping.Commands> {
sessionId?: Protocol.Target.SessionID;
id?: number;
error?: CdpError;
method?: CdpMethod;
params?: ProtocolMapping.Commands[CdpMethod]['paramsType'][0];
result?: ProtocolMapping.Commands[CdpMethod]['returnType'];
}

View File

@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=cdpMessage.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"cdpMessage.js","sourceRoot":"","sources":["../../../src/cdp/cdpMessage.ts"],"names":[],"mappings":""}