@a11ywatch/core
Version:
a11ywatch central API
177 lines • 7.08 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addWebsiteWrapper = exports.addWebsite = void 0;
const website_source_builder_1 = require("@a11ywatch/website-source-builder");
const strings_1 = require("../../../strings");
const utils_1 = require("../../../utils");
const website_1 = require("../../../models/website");
const find_1 = require("../find");
const users_1 = require("../../users");
const watcher_crawl_1 = require("../../../actions/accessibility/watcher_crawl");
const database_1 = require("../../../../database");
const config_1 = require("../../../../config/config");
const runners_1 = require("../../../utils/filters/runners");
const allowedStandards = ["WCAG2A", "WCAG2AA", "WCAG2AAA", "Section508"];
const addWebsite = ({ userId, url: urlMap, customHeaders, canScan, pageInsights, mobile, ua, standard, actions, robots = true, subdomains = false, tld = false, ignore = [], rules = [], runners = [], proxy = "", sitemap = false, monitoringEnabled = true, }) => __awaiter(void 0, void 0, void 0, function* () {
const decodedUrl = decodeURIComponent(urlMap);
const url = (0, utils_1.stripUrlEndingSlash)((0, website_source_builder_1.initUrl)(decodedUrl));
const domain = (0, utils_1.getHostName)(url);
if (!domain) {
throw new Error(strings_1.WEBSITE_URL_ERROR);
}
const [siteExist, collection] = yield (0, find_1.getWebsite)({ userId, url });
if (siteExist) {
throw new Error(strings_1.WEBSITE_EXIST_ERROR);
}
const collectionCount = yield collection.countDocuments({ userId });
const [user] = yield (0, users_1.getUser)({ id: userId });
if (!user) {
throw new Error("User required to add website.");
}
if ((0, utils_1.blockWebsiteAdd)({
audience: user === null || user === void 0 ? void 0 : user.role,
collectionCount,
})) {
throw new Error(strings_1.ADD_FREE_MAX_ERROR);
}
let wcagStandard = undefined;
if (standard && allowedStandards.includes(standard)) {
wcagStandard = standard;
}
const accessRules = [];
if (rules && Array.isArray(rules)) {
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (rule && typeof rule === "string" && rule.length < 200) {
accessRules.push(rule);
}
if (i > 250) {
break;
}
}
}
const ignoreRules = [];
if (ignore && Array.isArray(ignore)) {
for (let i = 0; i < ignore.length; i++) {
const rule = ignore[i];
if (rule && typeof rule === "string" && rule.length < 200) {
ignoreRules.push(rule);
}
if (i > 250) {
break;
}
}
}
const testRunners = [];
if (runners && Array.isArray(runners)) {
const runnerItems = (0, runners_1.filterRunnerDuplicates)(runners);
for (let i = 0; i < runnerItems.length; i++) {
const runner = runnerItems[i];
if (runner &&
typeof runner === "string" &&
["htmlcs", "axe"].includes(runner)) {
testRunners.push(runner);
}
if (i > 3) {
break;
}
}
}
const actionsEnabled = actions && Array.isArray(actions) && actions.length;
const subdomainsEnabled = subdomains && (config_1.SUPER_MODE || !!user.role);
const tldEnabled = tld && (config_1.SUPER_MODE || !!user.role);
const proxyHost = proxy &&
typeof proxy === "string" &&
(proxy.startsWith("http") ||
proxy.startsWith("https") ||
proxy.startsWith("socks5"))
? (0, utils_1.cipher)(proxy)
: "";
const sitemapEnabled = config_1.SUPER_MODE ? sitemap : user.role && sitemap;
const website = (0, website_1.makeWebsite)({
userId,
url,
domain,
pageHeaders: customHeaders,
pageInsights: !!pageInsights,
mobile,
ua,
standard: wcagStandard,
actionsEnabled,
robots,
subdomains: subdomainsEnabled,
tld: tldEnabled,
ignore: ignoreRules,
rules: accessRules,
runners: testRunners,
proxy: config_1.DEV ||
(!config_1.DEV &&
user.role &&
(!proxyHost.startsWith("http://localhost") ||
!proxyHost.startsWith("https://localhost")))
? proxyHost
: "",
sitemap: sitemapEnabled,
monitoringEnabled,
});
yield collection.insertOne(website);
setImmediate(() => __awaiter(void 0, void 0, void 0, function* () {
if (actionsEnabled) {
for (let i = 0; i < actions.length; i++) {
const action = actions[i];
const update = {
$set: Object.assign(Object.assign({}, action), { userId,
domain }),
};
const path = action.path && action.path[0] === "/"
? action.path
: `/${action.path}`;
yield database_1.actionsCollection.updateOne({
userId,
domain,
path,
}, update, { upsert: true });
}
}
if (canScan) {
yield (0, watcher_crawl_1.watcherCrawl)({
url: url,
userId,
robots,
subdomains: subdomainsEnabled,
tld: tldEnabled,
scan: true,
agent: ua,
proxy,
sitemap: sitemapEnabled,
}, true);
}
}));
return {
website: Object.assign(Object.assign({}, website), { actions }),
code: 200,
success: !!canScan,
message: canScan
? strings_1.SUCCESS
: "Scan limit reached for the day. Upgrade your account or wait until your limit resets tomorrow.",
};
});
exports.addWebsite = addWebsite;
const addWebsiteWrapper = (params) => __awaiter(void 0, void 0, void 0, function* () {
const { website } = yield (0, exports.addWebsite)(params);
if (website) {
return website;
}
return null;
});
exports.addWebsiteWrapper = addWebsiteWrapper;
//# sourceMappingURL=add-website.js.map
;