-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkProcessor.js
160 lines (137 loc) · 4.72 KB
/
linkProcessor.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
158
159
160
'use strict';
const Promise = require("bluebird");
const LinkStatus = require('./linkStatus');
const config = require('./config');
const request = require("request-promise");
const BodyParser = require('./bodyParser');
const chalk = require('chalk');
const ora = require('ora');
let spinner;
let urlCounter = 0;
var requestOptions = {
proxy: config.proxy,
strictSSL: config.strictSSL,
resolveWithFullResponse: true
};
const LinkProcessor = {
start: (fileUrls, options) => {
const keys = Object.getOwnPropertyNames(fileUrls);
spinner = ora('Starting...').start();
let fileCounter = 0;
let fileUrlsStatus = {};
return Promise.mapSeries(keys, (keyItem) => {
fileCounter += 1;
spinner.text = "Checking urls in file " + fileCounter + " of " + options.totalFiles;
Object.assign(options, { fileCounter: fileCounter });
return processFileUlrs(fileUrls[keyItem], options)
.then((statusUrls) => {
fileUrlsStatus[keyItem] = statusUrls;
});
})
.then(() => {
spinner.stop();
return Promise.resolve(fileUrlsStatus);
});
}
};
const processFileUlrs = (urls, options) => {
let checkedUrls = [];
return Promise.mapSeries(urls, (url) => {
urlCounter += 1;
spinner.text = "Checking URL " + chalk.bold(urlCounter + "/" + options.totalUrls) + chalk.gray(" (file " + options.fileCounter + "/" + options.totalFiles + ") : " + url);
return getLinkStatus(url, options)
.then((response) => {
checkedUrls.push({
"url": url,
"status": response.status,
"code": response.code
});
return Promise.resolve();
})
}).then(() => {
return Promise.resolve(checkedUrls);
});
};
const getLinkStatus = (link, options) => {
Object.assign(options, requestOptions);
Object.assign(options, { url: link });
return request.get(prepareRequestOptions(options))
.then((response) => {
if (response.statusCode !== 200) {
return error(response, link);
} else {
return BodyParser.parse(response.body).then(status => {
return evaluateLink(status, link, response.statusCode);
});
}
})
.catch((response) => {
return error(response, link);
})
};
const evaluateLink = (status, link, statusCode) => {
if (status === LinkStatus.SUCESSFUL) {
console.log(chalk.green("\n✅ Success! " + link));
} else if (status === LinkStatus.NOT_AUTHENTICATED) {
console.log("\n⛔️ Seems not authenticated: " + link);
} else if (status === LinkStatus.NOT_FOUND) {
return notFoundLink(link);
}
return Promise.resolve({
status: status,
code: statusCode
});
};
const notFoundLink = (link) => {
console.log(chalk.red.bold("\n🕵️ Link not found! " + link));
return Promise.resolve({
status: LinkStatus.NOT_FOUND,
code: 404
});
};
const error = (response, url) => {
let errorCode = response.statusCode ? response.statusCode : response.error.code;
if (errorCode === 404) {
return notFoundLink(url);
} else {
console.log("\n❌ Error " + errorCode + " in " + url);
return Promise.resolve({
status: LinkStatus.ERROR,
code: errorCode
});
}
};
const prepareRequestOptions = (options) => {
let requestOptions = {};
const hostConfig = getHostConfig(options.url);
requestOptions.proxy = hostConfig ? (hostConfig.proxy ? hostConfig.proxy : undefined) : undefined;
requestOptions.strictSSL = options.strictSSL;
requestOptions.url = options.url;
requestOptions.resolveWithFullResponse = options.resolveWithFullResponse;
requestOptions.headers = getCookieHeader(options, hostConfig);
return requestOptions;
};
const getHostConfig = (url) => {
let hostConfig;
if (!config.hosts)
return undefined;
config.hosts.forEach((entry) => {
if (url.indexOf(entry.host) !== -1) {
hostConfig = entry;
}
});
return hostConfig;
};
const getCookieHeader = (options, hostConfig) => {
if (!hostConfig || !hostConfig.cookies) {
return undefined;
}
let cookieHeader = "";
hostConfig.cookies.forEach((cookieKey) => {
if (options.hasOwnProperty(config.cookiePrefix + cookieKey)) {
cookieHeader += cookieKey + "=" + options[config.cookiePrefix + cookieKey] + ";";
}
})
return { "cookie": cookieHeader };
};
module.exports = LinkProcessor;