-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathscanner.ts
More file actions
215 lines (199 loc) · 6.27 KB
/
scanner.ts
File metadata and controls
215 lines (199 loc) · 6.27 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { apiClient } from "../../lib/apiClient.js";
import { randomUUID } from "node:crypto";
import logger from "../../logger.js";
import {
isLocalURL,
ensureLocalBinarySetup,
killExistingBrowserStackLocalProcesses,
} from "../../lib/local.js";
import config from "../../config.js";
import { getA11yBaseURL } from "../../lib/a11y-base-url.js";
import { BrowserStackConfig } from "../../lib/types.js";
export interface AccessibilityScanResponse {
success: boolean;
data?: { id: string; scanRunId: string };
errors?: string[];
}
export interface AccessibilityScanStatus {
success: boolean;
data?: { status: string };
errors?: string[];
}
export class AccessibilityScanner {
private auth: { username: string; password: string } | undefined;
private config: BrowserStackConfig;
constructor(config: BrowserStackConfig) {
this.config = config;
}
public setAuth(auth: { username: string; password: string }): void {
this.auth = auth;
}
async startScan(
name: string,
urlList: string[],
authConfigId?: number,
): Promise<AccessibilityScanResponse> {
if (!this.auth?.username || !this.auth?.password) {
throw new Error(
"BrowserStack credentials are not set for AccessibilityScanner.",
);
}
// Check if any URL is local
const hasLocal = urlList.some(isLocalURL);
const localIdentifier = randomUUID();
const localHosts = new Set(["127.0.0.1", "localhost", "0.0.0.0"]);
const BS_LOCAL_DOMAIN = "bs-local.com";
if (config.USE_OWN_LOCAL_BINARY_PROCESS && hasLocal) {
throw new Error(
"Cannot start scan with local URLs when using own BrowserStack Local binary process. Please set USE_OWN_LOCAL_BINARY_PROCESS to false.",
);
}
if (config.REMOTE_MCP && hasLocal) {
throw new Error(
"Local URLs are not supported in this remote mcp. Please use a public URL.",
);
}
if (hasLocal) {
await ensureLocalBinarySetup(
this.auth.username,
this.auth.password,
localIdentifier,
);
} else {
await killExistingBrowserStackLocalProcesses();
}
const transformedUrlList = urlList.map((url) => {
try {
const parsed = new URL(url);
if (localHosts.has(parsed.hostname)) {
parsed.hostname = BS_LOCAL_DOMAIN;
return parsed.toString();
}
return url;
} catch (e) {
logger.warn(`[AccessibilityScan] Invalid URL skipped: ${e}`);
return url;
}
});
const baseRequestBody = {
name,
urlList: transformedUrlList,
recurring: false,
...(authConfigId && { authConfigId }),
};
let requestBody = baseRequestBody;
if (hasLocal) {
const localConfig = {
localTestingInfo: {
localIdentifier,
localEnabled: true,
},
};
requestBody = { ...baseRequestBody, ...localConfig };
}
try {
const baseUrl = await getA11yBaseURL(this.config);
const response = await apiClient.post<AccessibilityScanResponse>({
url: `${baseUrl}/api/website-scanner/v1/scans`,
headers: {
Authorization:
"Basic " +
Buffer.from(`${this.auth.username}:${this.auth.password}`).toString(
"base64",
),
"Content-Type": "application/json",
},
body: requestBody,
});
const data = response.data;
if (!data.success)
throw new Error(`Unable to start scan: ${data.errors?.join(", ")}`);
return data;
} catch (err: any) {
// apiClient throws generic errors, try to extract message
if (err?.response?.status === 422) {
throw new Error(
"A scan with this name already exists. please update the name and run again.",
);
}
const msg =
err?.response?.data?.error ||
err?.response?.data?.message ||
err?.message ||
String(err);
throw new Error(`Failed to start scan: ${msg}`);
}
}
async pollStatus(
scanId: string,
scanRunId: string,
): Promise<AccessibilityScanStatus> {
try {
const baseUrl = await getA11yBaseURL(this.config);
const response = await apiClient.get<AccessibilityScanStatus>({
url: `${baseUrl}/api/website-scanner/v1/scans/${scanId}/scan_runs/${scanRunId}/status`,
headers: {
Authorization:
"Basic " +
Buffer.from(
`${this.auth?.username}:${this.auth?.password}`,
).toString("base64"),
},
});
const data = response.data;
if (!data.success)
throw new Error(`Failed to get status: ${data.errors?.join(", ")}`);
return data;
} catch (err: any) {
const msg = err?.response?.data?.message || err?.message || String(err);
throw new Error(`Failed to get scan status: ${msg}`);
}
}
async waitUntilComplete(
scanId: string,
scanRunId: string,
context: any,
): Promise<string> {
return new Promise((resolve, reject) => {
let timepercent = 0;
let dotCount = 1;
const interval = setInterval(async () => {
try {
const statusResp = await this.pollStatus(scanId, scanRunId);
const status = statusResp.data!.status;
timepercent += 1.67;
const progress = status === "completed" ? 100 : timepercent;
const dots = ".".repeat(dotCount);
dotCount = (dotCount % 4) + 1;
const message =
status === "completed" || status === "failed"
? `Scan completed with status: ${status}`
: `Scan in progress${dots}`;
await context.sendNotification({
method: "notifications/progress",
params: {
progressToken: context._meta?.progressToken ?? "NOT_FOUND",
message: message,
progress: progress,
total: 100,
},
});
if (status === "completed" || status === "failed") {
clearInterval(interval);
resolve(status);
}
} catch (e) {
clearInterval(interval);
reject(e);
}
}, 5000);
setTimeout(
() => {
clearInterval(interval);
reject(new Error("Scan timed out after 5 minutes"));
},
5 * 60 * 1000,
);
});
}
}