md-curcuma
Version:
A Typescript library for transporting and converting markdown and other data.
211 lines (210 loc) • 8.22 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.Broken_Link_Checker = exports.Duration = exports.BLC_Scan_Summary = exports.BLC_Result = exports.BLC_Parameter = void 0;
const linkinator_1 = require("linkinator");
const perf_hooks_1 = require("perf_hooks");
const fs = require("fs");
const date_fns_1 = require("date-fns");
class BLC_Parameter {
constructor() {
this.scan_source = 'http://localhost:1313/';
this.write_to = './data/linksChecked.json';
this.date_format = 'yyyy-MM-dd HH:mm:SSS';
this.mode = 'extern';
this.special_excludes = ['data:image/webp', 'blog:', 'troubleshooting:', 'mailto:'];
this.checkOptions = {
path: '',
concurrency: 100,
config: 'string',
recurse: true,
skip: 'www.googleapis.com',
format: 'json',
silent: true,
verbosity: 'error',
timeout: 0,
directoryListing: true,
retry: true,
retryErrors: true,
retryErrorsCount: 3,
retryErrorsJitter: 5,
userAgent: 'Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.1)'
};
}
}
exports.BLC_Parameter = BLC_Parameter;
class BLC_Result {
constructor() {
this.url = '';
this.state = '';
this.status = 0;
this.scantime = '';
this.parent = '';
}
}
exports.BLC_Result = BLC_Result;
class BLC_Scan_Summary {
constructor() {
this.scan_source = '';
this.mode = 'extern';
this.special_excludes = [];
this.lastrun = '';
this.runtime = 0;
this.runtime_unit = 'min';
this.found = 0;
this.dropped = 0;
this.finished = false;
this.total = 0;
this.ok = 0;
this.broken = 0;
this.skipped = 0;
this.links_ok = [];
this.links_broken = [];
this.links_skipped = [];
}
}
exports.BLC_Scan_Summary = BLC_Scan_Summary;
class Duration {
constructor() {
this.beginn_time = 0;
this.duration_unit = 'min';
}
start() {
this.beginn_time = perf_hooks_1.performance.now();
}
getDuration() {
let duration = (perf_hooks_1.performance.now() - this.beginn_time) / 60000;
return duration;
}
getDurationUnit() {
return this.duration_unit;
}
}
exports.Duration = Duration;
class Broken_Link_Checker {
static count_push(result, json_obj, item) {
json_obj.total = json_obj.total + 1;
if (result.state === 'OK') {
json_obj.ok = json_obj.ok + 1;
json_obj.links_ok.push(item);
}
else if (result.state === 'BROKEN') {
json_obj.broken = json_obj.broken + 1;
json_obj.links_broken.push(item);
}
else if (result.state === 'SKIPPED') {
json_obj.skipped = json_obj.skipped + 1;
json_obj.links_skipped.push(item);
}
}
static isSpecialExclude(item, param) {
let isExclude = false;
for (const an_exclude of param.special_excludes) {
if (item.url.startsWith(an_exclude)) {
isExclude = true;
console.log(`Skipping ${item.url}`);
break;
}
else {
console.log(`NO_Skipp ${item.url}`);
}
}
return isExclude;
}
static count_push_write(item, param, json_obj, result, json_array_wrapper, duration) {
if (!Broken_Link_Checker.isSpecialExclude(item, param)) {
Broken_Link_Checker.count_push(result, json_obj, item);
json_obj.runtime = duration.getDuration();
json_obj.runtime_unit = duration.getDurationUnit();
fs.writeFileSync(param.write_to, JSON.stringify(json_array_wrapper, null, 4));
}
}
static run(params) {
return __awaiter(this, void 0, void 0, function* () {
if (Array.isArray(params)) {
for (const job of params) {
Broken_Link_Checker.run_job(Broken_Link_Checker.dice_parameters(job));
}
}
else {
Broken_Link_Checker.run_job(Broken_Link_Checker.dice_parameters(params));
}
});
}
static dice_parameters(job) {
let param = new BLC_Parameter();
if (job !== null) {
Object.assign(param, job);
}
return param;
}
static run_job(param) {
return __awaiter(this, void 0, void 0, function* () {
if (fs.existsSync(param.write_to)) {
fs.unlinkSync(param.write_to);
}
const duration = new Duration();
duration.start();
const checker = new linkinator_1.LinkChecker();
const json_obj = new BLC_Scan_Summary();
json_obj.scan_source = param.scan_source;
json_obj.mode = param.mode;
json_obj.special_excludes = param.special_excludes;
json_obj.lastrun = (0, date_fns_1.format)(new Date(), param.date_format);
const json_array_wrapper = [];
json_array_wrapper.push(json_obj);
checker.on('pagestart', (url) => {
console.log(`Scanning ${url}`);
});
checker.on('link', (result) => {
json_obj.found = json_obj.found + 1;
const result_item = {
url: result.url,
state: result.state,
status: result.status,
scantime: (0, date_fns_1.format)(new Date(), param.date_format),
parent: result.parent
};
if (param.mode === 'intern') {
if (result_item.url.startsWith(param.scan_source)) {
Broken_Link_Checker.count_push_write(result_item, param, json_obj, result, json_array_wrapper, duration);
}
else {
json_obj.dropped = json_obj.dropped + 1;
}
}
else if (param.mode === 'extern') {
if (!result_item.url.startsWith(param.scan_source)) {
Broken_Link_Checker.count_push_write(result_item, param, json_obj, result, json_array_wrapper, duration);
}
else {
json_obj.dropped = json_obj.dropped + 1;
}
}
else {
Broken_Link_Checker.count_push_write(result_item, param, json_obj, result, json_array_wrapper, duration);
}
});
param.checkOptions.path = param.scan_source;
const result = yield checker.check(param.checkOptions);
console.log(result);
console.log(result.passed ? 'PASSED :D' : 'FAILED :(');
console.log(`Scanned total of ${result.links.length} links!`);
const brokeLinksCount = result.links.filter((x) => x.state === 'BROKEN');
console.log(`Detected ${brokeLinksCount.length} broken links.`);
json_obj.runtime = duration.getDuration();
json_obj.runtime_unit = duration.getDurationUnit();
json_obj.finished = true;
fs.writeFileSync(param.write_to, JSON.stringify(json_array_wrapper, null, 4));
});
}
}
exports.Broken_Link_Checker = Broken_Link_Checker;