Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with strict mode Typescript in Connection.ts and ConnectionTransport.ts #7919

Merged
merged 1 commit into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/common/Connection.ts
Expand Up @@ -71,7 +71,7 @@ export class Connection extends EventEmitter {
this._transport.onclose = this._onClose.bind(this);
}

static fromSession(session: CDPSession): Connection {
static fromSession(session: CDPSession): Connection | undefined {
return session._connection;
}

Expand Down Expand Up @@ -170,8 +170,8 @@ export class Connection extends EventEmitter {
_onClose(): void {
if (this._closed) return;
this._closed = true;
this._transport.onmessage = null;
this._transport.onclose = null;
this._transport.onmessage = undefined;
this._transport.onclose = undefined;
for (const callback of this._callbacks.values())
callback.reject(
rewriteError(
Expand Down Expand Up @@ -201,7 +201,11 @@ export class Connection extends EventEmitter {
targetId: targetInfo.targetId,
flatten: true,
});
return this._sessions.get(sessionId);
const session = this._sessions.get(sessionId);
if (!session) {
throw new Error('CDPSession creation failed.');
}
return session;
}
}

Expand Down Expand Up @@ -254,7 +258,7 @@ export class CDPSession extends EventEmitter {
/**
* @internal
*/
_connection: Connection;
_connection?: Connection;
private _sessionId: string;
private _targetType: string;
private _callbacks: Map<number, ConnectionCallback> = new Map();
Expand All @@ -269,7 +273,7 @@ export class CDPSession extends EventEmitter {
this._sessionId = sessionId;
}

connection(): Connection {
connection(): Connection | undefined {
return this._connection;
}

Expand Down Expand Up @@ -307,8 +311,8 @@ export class CDPSession extends EventEmitter {
* @internal
*/
_onMessage(object: CDPSessionOnMessageObject): void {
if (object.id && this._callbacks.has(object.id)) {
const callback = this._callbacks.get(object.id);
const callback = object.id ? this._callbacks.get(object.id) : undefined;
if (object.id && callback) {
this._callbacks.delete(object.id);
if (object.error)
callback.reject(
Expand Down Expand Up @@ -347,7 +351,7 @@ export class CDPSession extends EventEmitter {
)
);
this._callbacks.clear();
this._connection = null;
this._connection = undefined;
this.emit(CDPSessionEmittedEvents.Disconnected);
}

Expand Down Expand Up @@ -386,6 +390,6 @@ function rewriteError(
originalMessage?: string
): Error {
error.message = message;
error.originalMessage = originalMessage;
error.originalMessage = originalMessage ?? error.originalMessage;
return error;
}
4 changes: 2 additions & 2 deletions src/common/ConnectionTransport.ts
Expand Up @@ -18,8 +18,8 @@
* @public
*/
export interface ConnectionTransport {
send(string);
close();
send(message: string): void;
close(): void;
onmessage?: (message: string) => void;
onclose?: () => void;
}