-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathstart-session.ts
More file actions
127 lines (116 loc) · 3.23 KB
/
Copy pathstart-session.ts
File metadata and controls
127 lines (116 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import logger from "../../logger.js";
import childProcess from "child_process";
import { filterDesktop } from "./desktop-filter.js";
import { filterMobile } from "./mobile-filter.js";
import { DOMAINS } from "../../lib/domains.js";
import {
DesktopSearchArgs,
MobileSearchArgs,
DesktopEntry,
MobileEntry,
PlatformType,
} from "./types.js";
import {
isLocalURL,
ensureLocalBinarySetup,
killExistingBrowserStackLocalProcesses,
} from "../../lib/local.js";
/**
* Prepares local tunnel setup based on URL type
*/
async function prepareLocalTunnel(url: string): Promise<boolean> {
const isLocal = isLocalURL(url);
if (isLocal) {
await ensureLocalBinarySetup();
} else {
await killExistingBrowserStackLocalProcesses();
}
return isLocal;
}
/**
* Entrypoint: detects platformType & delegates.
*/
export async function startBrowserSession(
args: DesktopSearchArgs | MobileSearchArgs,
): Promise<string> {
const entry =
args.platformType === PlatformType.DESKTOP
? await filterDesktop(args as DesktopSearchArgs)
: await filterMobile(args as MobileSearchArgs);
const isLocal = await prepareLocalTunnel(args.url);
const url =
args.platformType === PlatformType.DESKTOP
? buildDesktopUrl(
args as DesktopSearchArgs,
entry as DesktopEntry,
isLocal,
)
: buildMobileUrl(args as MobileSearchArgs, entry as MobileEntry, isLocal);
openBrowser(url);
return entry.notes ? `${url}, ${entry.notes}` : url;
}
function buildDesktopUrl(
args: DesktopSearchArgs,
e: DesktopEntry,
isLocal: boolean,
): string {
const params = new URLSearchParams({
os: e.os,
os_version: e.os_version,
browser: e.browser,
browser_version: e.browser_version,
url: args.url,
scale_to_fit: "true",
resolution: "responsive-mode",
speed: "1",
local: isLocal ? "true" : "false",
start: "true",
});
return `${DOMAINS.LIVE}/dashboard#${params.toString()}`;
}
function buildMobileUrl(
args: MobileSearchArgs,
d: MobileEntry,
isLocal: boolean,
): string {
const os_map = {
android: "Android",
ios: "iOS",
winphone: "Winphone",
};
const os = os_map[d.os as keyof typeof os_map] || d.os;
const params = new URLSearchParams({
os: os,
os_version: d.os_version,
device: d.display_name,
device_browser: args.browser,
url: args.url,
scale_to_fit: "true",
speed: "1",
local: isLocal ? "true" : "false",
start: "true",
});
return `${DOMAINS.LIVE}/dashboard#${params.toString()}`;
}
// ——— Open a browser window ———
function openBrowser(launchUrl: string): void {
try {
const command =
process.platform === "darwin"
? ["open", launchUrl]
: process.platform === "win32"
? ["cmd", "/c", "start", launchUrl]
: ["xdg-open", launchUrl];
// nosemgrep:javascript.lang.security.detect-child-process.detect-child-process
const child = childProcess.spawn(command[0], command.slice(1), {
stdio: "ignore",
detached: true,
});
child.on("error", (err) =>
logger.error(`Failed to open browser: ${err}. URL: ${launchUrl}`),
);
child.unref();
} catch (err) {
logger.error(`Failed to launch browser: ${err}. URL: ${launchUrl}`);
}
}