jokkerr
Version:
Node package and CLI tool for saving web page as single HTML file
92 lines • 3.39 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.Archiver = void 0;
const utils_1 = require("./utils");
const html_1 = require("./html");
class Archiver {
constructor() {
this.opt = {};
this.req = {
url: '',
};
}
/**
* Set archival request data.
*
* @param {object} [Requests] if error will be thrown
* @return {this} [Cairn] `this` command for chaning
* @api public
*/
request(r) {
const { url } = r;
if (!utils_1.isValidURL(url)) {
utils_1.Err('request url is not specified');
}
this.req.url = url;
return this;
}
/**
* Set archival options data.
*
* @param {object} [Options] if error will be thrown
* @return {this} [Cairn] `this` command for chaning
* @api public
*/
options(o) {
this.opt = o;
return this;
}
/**
* Perform archival request.
*
* @return {Promise} with string
* @api public
*/
archive() {
return __awaiter(this, void 0, void 0, function* () {
return yield (() => __awaiter(this, void 0, void 0, function* () {
let webpage;
let process = true;
return yield this.download(this.req.url)
.then((response) => {
// Check the type of the downloaded file.
// If it's not HTML, just return it as it is.
const contentType = response.headers['content-type'] || '';
process = contentType.includes('text/html');
webpage = { uri: this.req.url, content: response.data, contentType: contentType };
})
.then(() => __awaiter(this, void 0, void 0, function* () {
if (process) {
// If it's HTML process it
webpage.content = yield new html_1.HTML(this.opt).process(webpage);
}
return webpage.content;
}))
.catch((err) => {
console.warn(err);
return webpage.content;
});
}))();
});
}
download(url, referer) {
return __awaiter(this, void 0, void 0, function* () {
const http = new utils_1.HTTP();
if (this.opt.userAgent) {
http.setHeader('User-Agent', this.opt.userAgent);
}
return yield http.fetch(url).catch((err) => utils_1.Err(err));
});
}
}
exports.Archiver = Archiver;
//# sourceMappingURL=archiver.js.map