forked from GovTechSG/oobee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.js
157 lines (142 loc) · 3.94 KB
/
combine.js
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
import printMessage from 'print-message';
import crawlSitemap from './crawlers/crawlSitemap.js';
import crawlDomain from './crawlers/crawlDomain.js';
import { generateArtifacts } from './mergeAxeResults.js';
import { getHost, createAndUpdateResultsFolders, createDetailsAndLogs } from './utils.js';
import constants, { basicAuthRegex } from './constants/constants.js';
import { getBlackListedPatterns, submitForm } from './constants/common.js';
import { consoleLogger, silentLogger } from './logs.js';
import runCustom from './crawlers/runCustom.js';
const combineRun = async (details, deviceToScan) => {
const envDetails = { ...details };
const {
type,
url,
nameEmail,
randomToken,
deviceChosen,
customDevice,
viewportWidth,
playwrightDeviceDetailsObject,
maxRequestsPerCrawl,
isLocalSitemap,
browser,
userDataDirectory,
strategy,
specifiedMaxConcurrency,
needsReviewItems,
fileTypes,
blacklistedPatternsFilename,
includeScreenshots,
metadata,
customFlowLabel = 'Custom Flow',
} = envDetails;
process.env.CRAWLEE_LOG_LEVEL = 'ERROR';
process.env.CRAWLEE_STORAGE_DIR = randomToken;
const host = type === constants.scannerTypes.sitemap && isLocalSitemap ? '' : getHost(url);
let blacklistedPatterns = null;
try {
blacklistedPatterns = getBlackListedPatterns(blacklistedPatternsFilename);
} catch (error) {
consoleLogger.error(error);
silentLogger.error(error);
process.exit(1);
}
// remove basic-auth credentials from URL
let finalUrl = url;
if (basicAuthRegex.test(url)) {
finalUrl = `${url.split('://')[0]}://${url.split('@')[1]}`;
}
const scanDetails = {
startTime: new Date().getTime(),
crawlType: type,
requestUrl: finalUrl,
};
const viewportSettings = {
deviceChosen,
customDevice,
viewportWidth,
playwrightDeviceDetailsObject,
};
let urlsCrawled;
switch (type) {
case constants.scannerTypes.custom:
urlsCrawled = await runCustom(
url,
randomToken,
viewportSettings,
needsReviewItems,
blacklistedPatterns,
includeScreenshots,
);
break;
case constants.scannerTypes.sitemap:
urlsCrawled = await crawlSitemap(
url,
randomToken,
host,
viewportSettings,
maxRequestsPerCrawl,
browser,
userDataDirectory,
specifiedMaxConcurrency,
needsReviewItems,
fileTypes,
blacklistedPatterns,
includeScreenshots,
);
break;
case constants.scannerTypes.website:
urlsCrawled = await crawlDomain(
url,
randomToken,
host,
viewportSettings,
maxRequestsPerCrawl,
browser,
userDataDirectory,
strategy,
specifiedMaxConcurrency,
needsReviewItems,
fileTypes,
blacklistedPatterns,
includeScreenshots,
);
break;
default:
consoleLogger.error(`type: ${type} not defined`);
silentLogger.error(`type: ${type} not defined`);
process.exit(1);
}
scanDetails.endTime = new Date().getTime();
scanDetails.urlsCrawled = urlsCrawled;
await createDetailsAndLogs(scanDetails, randomToken);
if (scanDetails.urlsCrawled.scanned.length > 0) {
await createAndUpdateResultsFolders(randomToken);
const pagesNotScanned = [...urlsCrawled.error, ...urlsCrawled.invalid];
const basicFormHTMLSnippet = await generateArtifacts(
randomToken,
url,
type,
deviceToScan,
urlsCrawled.scanned,
pagesNotScanned,
customFlowLabel,
);
const [name, email] = nameEmail.split(':');
await submitForm(
browser,
userDataDirectory,
url,
type,
email,
name,
JSON.stringify(basicFormHTMLSnippet),
urlsCrawled.scanned.length,
metadata,
);
} else {
printMessage([`No pages were scanned.`], constants.alertMessageOptions);
}
};
export default combineRun;