UNPKG

@wabarc/cairn

Version:

Node package and CLI tool for saving web page as single HTML file

94 lines 3.56 kB
"use strict"; /* * Copyright 2023 Wayback Archiver. All rights reserved. * Use of this source code is governed by the MIT * license that can be found in the LICENSE file. */ 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 (!(0, utils_1.isValidURL)(url)) { (0, utils_1.err)(`request url: ${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* () { const archived = { url: this.req.url, webpage: null, status: 400, contentType: 'text/html' }; const response = yield this.download(this.req.url).catch((err) => err(err)); if (response.isAxiosError === true || !response.headers) { return archived; } const contentType = response.headers['content-type'] || response.headers['Content-Type'] || ''; // Check the type of the downloaded file. // If it's not HTML, just return it as it is. if (contentType.includes('text/html') === true) { // If it's HTML process it archived.webpage = yield new html_1.HTML(this.opt).process({ uri: this.req.url, html: Buffer.from(response.data).toString(), }); } archived.status = response.status || archived.status; archived.contentType = contentType; return archived; }); } download(url, referer) { return __awaiter(this, void 0, void 0, function* () { if (this.opt.userAgent) { utils_1.http.setHeader('User-Agent', this.opt.userAgent); } if (this.opt.timeout || this.opt.proxy) { utils_1.http.setOptions({ timeout: this.opt.timeout, proxy: this.opt.proxy }); } return yield utils_1.http.setResponseType('arraybuffer').fetch(url); }); } } exports.Archiver = Archiver; //# sourceMappingURL=archiver.js.map