Skip to content

Commit

Permalink
added new API WSSession for custom CDPSession
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Mar 22, 2024
1 parent b3c5965 commit ed361aa
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- fixed sourcemap if range in a comment
- added cache for sourcemap conversion
- fixed ECONNREFUSED on NodeJS 18
- added new API `WSSession` for custom CDPSession

* 2.7.5
- fixed special generated codes for original coverage
Expand Down
4 changes: 2 additions & 2 deletions lib/client/cdp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const EC = require('eight-colors');
const { WebSocket } = require('../packages/monocart-coverage-vendor.js');
const Util = require('../utils/util.js');

const CDPSession = require('./cdp-session.js');
const WSSession = require('./ws-session.js');
const CoverageClient = require('./coverage-client.js');

const getDebuggerUrl = async (options) => {
Expand Down Expand Up @@ -85,7 +85,7 @@ const getCDPSession = async (options) => {
ws.once('open', () => {
clearTimeout(timeoutId);
Util.logDebug(`${EC.green('Connected')} ${url}`);
const session = new CDPSession(ws);
const session = new WSSession(ws);
resolve([null, session]);
});

Expand Down
4 changes: 2 additions & 2 deletions lib/client/cdp-session.js → lib/client/ws-session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { EventEmitter } = require('events');

class CDPSession extends EventEmitter {
class WSSession extends EventEmitter {
constructor(ws) {
super();
this.ws = ws;
Expand Down Expand Up @@ -60,4 +60,4 @@ class CDPSession extends EventEmitter {
}
}

module.exports = CDPSession;
module.exports = WSSession;
8 changes: 8 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,14 @@ declare namespace MCR {
detach: () => Promise<void>
}

/** custom websocket CDPSession */
export class WSSession implements CDPSession {
constructor(ws: any);
send: (method: string, params?: any) => Promise<any>;
on: (type: string, handler: (e: any) => void) => void;
detach: () => Promise<void>
}

export interface CDPOptions {
/** Adapt to the CDPSession of Playwright or Puppeteer */
session?: CDPSession;
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
getInputData, readFromDir, generateCoverageReports
} = require('./generate.js');
const { getSnapshot, diffSnapshot } = require('./utils/snapshot.js');

const WSSession = require('./client/ws-session.js');
const CDPClient = require('./client/cdp-client.js');

const resolveConfigOptions = async (configPath) => {
Expand Down Expand Up @@ -286,6 +288,7 @@ const MCR = function(options) {
MCR.CoverageReport = CoverageReport;
MCR.getSnapshot = getSnapshot;
MCR.diffSnapshot = diffSnapshot;
MCR.WSSession = WSSession;
MCR.CDPClient = CDPClient;

module.exports = MCR;
2 changes: 2 additions & 0 deletions lib/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export default MCR;
export const CoverageReport = MCR.CoverageReport;
export const getSnapshot = MCR.getSnapshot;
export const diffSnapshot = MCR.diffSnapshot;

export const WSSession = MCR.WSSession;
export const CDPClient = MCR.CDPClient;
3 changes: 1 addition & 2 deletions scripts/template/markdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
<title>{placeholder_title}</title>
<link rel="icon" href="data:,">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<link rel="stylesheet" href="assets/github-markdown.css" />
</head>

<body>
<div class="markdown-body">
<div>
{placeholder_content}
</div>
</body>
Expand Down

0 comments on commit ed361aa

Please sign in to comment.