UNPKG

@a11ywatch/core

Version:
155 lines 6.13 kB
"use strict"; 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.updateWebsite = void 0; const strings_1 = require("../../../../core/strings"); const database_1 = require("../../../../database"); const find_1 = require("../find"); const utils_1 = require("../../../../core/utils"); const config_1 = require("../../../../config"); const runners_1 = require("../../../utils/filters/runners"); const updateWebsite = ({ userId, url, pageHeaders, pageInsights, mobile, standard, ua, actions, robots = true, tld, subdomains, ignore, rules, runners, proxy, sitemap, monitoringEnabled, actionsEnabled, }) => __awaiter(void 0, void 0, void 0, function* () { const [website, collection] = yield (0, find_1.getWebsite)({ userId, url }); if (!website) { throw new Error(strings_1.WEBSITE_NOT_FOUND); } const pageParams = { actionsEnabled: website.actionsEnabled, robots, pageHeaders: website.pageHeaders, pageInsights: website.pageInsights, mobile: website.mobile, standard: website.standard, ua: website.ua ? website.ua : undefined, subdomains: !!website.subdomains, tld: !!website.tld, ignore: website.ignore, rules: website.rules, runners: website.runners, proxy: website.proxy, sitemap: !!website.sitemap, monitoringEnabled: website.monitoringEnabled, }; if (typeof pageHeaders !== "undefined" && Array.isArray(pageHeaders)) { const pageHeaderSrc = (pageHeaders === null || pageHeaders === void 0 ? void 0 : pageHeaders.length) === 1 && !pageHeaders[0].key ? null : pageHeaders; pageParams.pageHeaders = pageHeaderSrc; } if (typeof pageInsights !== "undefined") { pageParams.pageInsights = !!pageInsights; } if (typeof mobile !== "undefined") { pageParams.mobile = !!mobile; } if (typeof standard !== "undefined") { pageParams.standard = standard; } if (typeof ua !== "undefined") { pageParams.ua = ua; } if (typeof tld !== "undefined") { pageParams.tld = tld; } if (typeof subdomains !== "undefined") { pageParams.subdomains = subdomains; } if (typeof sitemap !== "undefined") { pageParams.sitemap = sitemap; } if (typeof monitoringEnabled !== "undefined") { pageParams.monitoringEnabled = monitoringEnabled; } if (typeof actionsEnabled !== "undefined") { pageParams.actionsEnabled = actionsEnabled; } if ((typeof proxy !== "undefined" && !proxy) || (proxy && typeof proxy === "string" && (proxy.startsWith("http") || proxy.startsWith("https") || proxy.startsWith("socks5")))) { if (config_1.DEV || !proxy || (!config_1.DEV && (!proxy.startsWith("http://localhost") || !proxy.startsWith("https://localhost")))) { pageParams.proxy = !proxy ? "" : (0, utils_1.cipher)(proxy); } } 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; } } pageParams.rules = accessRules; } 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; } } pageParams.ignore = ignoreRules; } 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 (typeof runner === "string" && (runner === "axe" || runner === "htmlcs")) { testRunners.push(runner); } if (i > 3) { break; } } pageParams.runners = testRunners; } yield collection.updateOne({ url, userId }, { $set: pageParams }); if (actions && Array.isArray(actions) && actions.length) { const domain = website.domain; for (let i = 0; i < actions.length; i++) { if (i > 1000) { break; } 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 }); } } return { website: Object.assign(Object.assign(Object.assign({}, website), pageParams), { actions }), code: 200, success: true, message: strings_1.SUCCESS, }; }); exports.updateWebsite = updateWebsite; //# sourceMappingURL=update-website.js.map