UNPKG

@wabarc/broker

Version:

Pack and store webpages from Telegram channel.

278 lines 14 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHub = void 0; var rest_1 = require("@octokit/rest"); var fs_1 = require("fs"); var path_1 = require("path"); var utils_1 = require("./utils"); var GitHub = /** @class */ (function () { function GitHub(contract) { this.contract = contract; this.prefix = 'broker.gh.'; var token = contract.token, owner = contract.owner, repo = contract.repo; this.contract = contract; this.credentials = { owner: owner, repo: repo }; try { this.octokit = new rest_1.Octokit({ auth: token }); } catch (_) { throw new Error('Bad credentials.'); } var date = new Date(); var month = date.getUTCMonth() + 1; var year = date.getUTCFullYear(); this.folder = "contents/" + year + month.toString().padStart(2, '0'); } GitHub.prototype.process = function (stages) { return __awaiter(this, void 0, void 0, function () { var created, task, _i, stages_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!stages || stages.length < 1) { return [2 /*return*/, true]; } created = []; _i = 0, stages_1 = stages; _a.label = 1; case 1: if (!(_i < stages_1.length)) return [3 /*break*/, 5]; task = stages_1[_i]; if (!(task.success === true && task.path.length > 1)) return [3 /*break*/, 4]; return [4 /*yield*/, this.createContent(task.path).then(function (success) { task.success = success; task.path = success ? _this.folder + "/" + path_1.basename(task.path) : ''; created.push(task); })]; case 2: _a.sent(); return [4 /*yield*/, utils_1.sleep(500)]; case 3: _a.sent(); _a.label = 4; case 4: _i++; return [3 /*break*/, 1]; case 5: return [4 /*yield*/, this.tagging(created)]; case 6: return [2 /*return*/, _a.sent()]; } }); }); }; GitHub.prototype.latestID = function () { return __awaiter(this, void 0, void 0, function () { var matchTag, response, tag, latest, id, _1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: matchTag = function (tags) { var regexp = new RegExp((_this.prefix + "\\d+\\-\\d+").replace(/\./g, '\\$&'), 'g'); for (var _i = 0, _a = Object.values(tags); _i < _a.length; _i++) { var tag = _a[_i]; if (regexp.test(tag.name)) { return tag; } } }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.octokit.repos.listTags(this.credentials)]; case 2: response = _a.sent(); if (!response || !response.data) { return [2 /*return*/, 0]; } tag = matchTag(response.data); if (tag === undefined) { return [2 /*return*/, 0]; } latest = tag.name || ''; id = latest.replace(this.prefix, '').split('-')[1] || ''; return [2 /*return*/, id.length > 0 ? parseInt(id) : 0]; case 3: _1 = _a.sent(); return [2 /*return*/, 0]; case 4: return [2 /*return*/]; } }); }); }; GitHub.prototype.tagging = function (stages) { return __awaiter(this, void 0, void 0, function () { var from, to, commit, credentials, tagSha, response, err_1, ref, _2; return __generator(this, function (_a) { switch (_a.label) { case 0: console.info('Process tagging start...'); if (!stages || stages.length < 1) { console.info('Process tagging failure, message: without task, skip.'); return [2 /*return*/, false]; } from = stages[0].id || -1; to = stages[stages.length - 1].id || -1; if (from <= 0 || to <= 0 || from > to) { console.info('Process tagging failure, message: params [from, to] invalid.'); return [2 /*return*/, false]; } commit = 'commit'; credentials = { owner: this.credentials.owner, repo: this.credentials.repo, tag: "" + this.prefix + from + "-" + to, message: "\n" + JSON.stringify(stages, null, 2) + "\n", object: this.credentials.sha, type: commit, }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, this.octokit.git.createTag(credentials)]; case 2: response = _a.sent(); tagSha = response.data.sha || this.credentials.sha; return [3 /*break*/, 4]; case 3: err_1 = _a.sent(); console.warn("Process tagging failure, message: create tag object error, details: " + err_1.message + ", data: " + JSON.stringify(credentials)); return [2 /*return*/, false]; case 4: ref = { owner: this.credentials.owner, repo: this.credentials.repo, ref: "refs/tags/" + credentials.tag, sha: tagSha, }; _a.label = 5; case 5: _a.trys.push([5, 7, , 8]); // doc: https://octokit.github.io/rest.js/v18#git-create-ref return [4 /*yield*/, this.octokit.git.createRef(ref)]; case 6: // doc: https://octokit.github.io/rest.js/v18#git-create-ref _a.sent(); return [3 /*break*/, 8]; case 7: _2 = _a.sent(); return [2 /*return*/, false]; case 8: console.info('done.'); return [2 /*return*/, true]; } }); }); }; GitHub.prototype.createContent = function (filepath) { return __awaiter(this, void 0, void 0, function () { var _3, path, response, err_2, ext, _a, response, succeed, _4; return __generator(this, function (_b) { switch (_b.label) { case 0: console.info('Process createContent start... ' + filepath); _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); return [4 /*yield*/, fs_1.promises.stat(filepath)]; case 2: _b.sent(); return [3 /*break*/, 4]; case 3: _3 = _b.sent(); console.info("Process createContent, file [" + filepath + "] not exists..."); return [2 /*return*/, false]; case 4: path = this.folder + "/" + path_1.basename(filepath); this.credentials.path = path; _b.label = 5; case 5: _b.trys.push([5, 7, , 8]); return [4 /*yield*/, this.octokit.repos.getContent({ owner: this.credentials.owner, repo: this.credentials.repo, path: this.credentials.path, })]; case 6: response = _b.sent(); this.credentials.sha = response.data['sha']; return [3 /*break*/, 8]; case 7: err_2 = _b.sent(); if (err_2.status && err_2.status === 403 && err_2.errors && err_2.errors[0]['code'] && err_2.errors[0]['code'] === 'too_large') { ext = Math.random().toString(36).substring(2, 7) + '.html'; this.credentials.path = this.credentials.path.replace(/\.(htm|html)$/g, ext); } return [3 /*break*/, 8]; case 8: this.credentials.message = "Add " + path_1.basename(this.credentials.path); _a = this.credentials; return [4 /*yield*/, fs_1.promises.readFile(filepath, { encoding: 'base64' })]; case 9: _a.content = _b.sent(); _b.label = 10; case 10: _b.trys.push([10, 12, , 13]); return [4 /*yield*/, this.octokit.repos.createOrUpdateFileContents(this.credentials)]; case 11: response = _b.sent(); succeed = response.data !== undefined && response.data.commit !== undefined; if (succeed) { this.credentials.sha = response.data.commit.sha || ''; } fs_1.unlinkSync(filepath); return [2 /*return*/, succeed]; case 12: _4 = _b.sent(); fs_1.unlinkSync(filepath); console.info('Process createContent failure...'); return [2 /*return*/, false]; case 13: return [2 /*return*/]; } }); }); }; return GitHub; }()); exports.GitHub = GitHub; //# sourceMappingURL=github.js.map