Skip to content

Commit 5304885

Browse files
committed
refactor: use export browser to split platfrom specific logic
1 parent c54d897 commit 5304885

7 files changed

Lines changed: 59 additions & 26 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"exports": {
2929
".": {
3030
"types": "./dist/index.d.ts",
31+
"browser": "./dist/index.browser.js",
3132
"import": "./dist/index.js"
3233
}
3334
},

src/Inspector.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ import {
1111
} from "./types.js";
1212
import highlightConfig from "./highlightConfig.js";
1313

14-
let JSDOM: any = null;
15-
16-
if (typeof window === "undefined") {
17-
const pkgs = typeof window === "undefined" ? ["jsdom"] : [];
18-
JSDOM = (await import(pkgs[0])).JSDOM;
19-
}
20-
2114
function findNodeIdx(nodes: CDPNode[], nodeId: number): number {
2215
for (let i = 0; i < nodes.length; i++) {
2316
if (nodes[i].nodeId === nodeId) {
@@ -27,13 +20,13 @@ function findNodeIdx(nodes: CDPNode[], nodeId: number): number {
2720
return null;
2821
}
2922

30-
type CDPClient = {
23+
export type CDPClient = {
3124
send: (method: string, params?: object) => Promise<any>;
3225
on: (event: string, callback: (data: any) => void) => void;
3326
off: (event: string, callback: (data: any) => void) => void;
3427
};
3528

36-
type InspectorOptions = {
29+
export type InspectorOptions = {
3730
documentImpl?: DOMImplementation;
3831
eventTimeout?: number;
3932
};
@@ -174,10 +167,12 @@ export class Inspector extends EventEmitter {
174167

175168
if (options.documentImpl) {
176169
this.documentImpl = options.documentImpl;
170+
} else if (window?.document?.implementation) {
171+
this.documentImpl = window.document.implementation;
177172
} else {
178-
this.documentImpl = JSDOM
179-
? new JSDOM("<!DOCTYPE html>").window.document.implementation
180-
: window.document.implementation;
173+
throw new Error(
174+
"No window.document.implementation. documentImpl must be provided in your environment.",
175+
);
181176
}
182177
this.eventTimeout = options.eventTimeout || 100;
183178
}

src/NodeInspector.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Inspector, InspectorOptions, CDPClient } from "./Inspector.js";
2+
3+
let JSDOM: typeof import("jsdom").JSDOM;
4+
let documentImpl: DOMImplementation;
5+
6+
async function initDocumentImpl() {
7+
if (!documentImpl) {
8+
JSDOM = (await import("jsdom")).JSDOM;
9+
documentImpl = new JSDOM("<!DOCTYPE html>").window.document.implementation;
10+
}
11+
}
12+
13+
export class NodeInspector extends Inspector {
14+
static async fromCDPClient(
15+
client: CDPClient,
16+
options: InspectorOptions = {},
17+
): Promise<NodeInspector> {
18+
const sendCommand = (method: string, params?: any) =>
19+
client.send(method, params);
20+
const onCDP = (event: string, callback: (data: any) => void) =>
21+
client.on(event, callback);
22+
const offCDP = (event: string, callback: (data: any) => void) =>
23+
client.off(event, callback);
24+
25+
if (!options.documentImpl) {
26+
await initDocumentImpl();
27+
options.documentImpl = documentImpl;
28+
}
29+
30+
const inspector = new NodeInspector(sendCommand, onCDP, offCDP, options);
31+
await inspector.init();
32+
await inspector.initDOM();
33+
return inspector;
34+
}
35+
}

src/extensionPath.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
let CHROME_INSPECTOR_SYNC_EXTENSION_PATH: string | undefined;
1+
import { fileURLToPath } from "url";
2+
import path from "path";
23

3-
const pkgs = typeof window === "undefined" ? ["url", "path"] : [];
4+
const __filename = fileURLToPath(import.meta.url);
5+
const __dirname = path.dirname(__filename);
46

5-
if (typeof window === "undefined") {
6-
const { fileURLToPath } = await import(pkgs[0]);
7-
const path = await import(pkgs[1]);
8-
const __filename = fileURLToPath(import.meta.url);
9-
const __dirname = path.dirname(__filename);
10-
11-
CHROME_INSPECTOR_SYNC_EXTENSION_PATH = path.join(__dirname, "extension");
12-
}
7+
const CHROME_INSPECTOR_SYNC_EXTENSION_PATH = path.join(__dirname, "extension");
138

149
export { CHROME_INSPECTOR_SYNC_EXTENSION_PATH };

src/index.browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from "./index.shared.js";
2+
3+
// browser specific
4+
export { Inspector } from "./Inspector.js";

src/index.shared.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./constants.js";
2+
export * from "./types.js";
3+
export * from "./InspectorDOM.js";

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from "./Inspector.js";
2-
export * from "./constants.js";
3-
export * from "./types.js";
4-
export * from "./InspectorDOM.js";
1+
export * from "./index.shared.js";
2+
3+
// node specific
54
export * from "./extensionPath.js";
5+
export { NodeInspector as Inspector } from "./NodeInspector.js";

0 commit comments

Comments
 (0)