1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const amqplib_1 = require("amqplib");
|
12 | const dam_util_1 = require("./dam.util");
|
13 | const pages_util_1 = require("./pages.util");
|
14 | const retry = require("retry");
|
15 | const AsyncLock = require("async-lock");
|
16 | class MagnoliaSource {
|
17 | constructor(options, callback) {
|
18 | this.generationLock = new AsyncLock({ maxPending: 1 });
|
19 | this.options = options;
|
20 | this.callback = callback;
|
21 | }
|
22 | generate() {
|
23 | return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
24 | const sitemap = yield pages_util_1.fetchSitemap(this.options);
|
25 | const website = yield pages_util_1.fetchPages(this.options);
|
26 | const pages = sitemap
|
27 | .map(path => website && website.find((page) => page["@path"] === path))
|
28 | .filter(page => typeof page !== "undefined");
|
29 | const workspaces = {};
|
30 | if (this.options.magnolia.workspaces) {
|
31 | for (const workspace of this.options.magnolia.workspaces) {
|
32 | workspaces[workspace] = yield pages_util_1.fetchWorkspace(workspace, this.options);
|
33 | }
|
34 | }
|
35 |
|
36 | const nodes = pages.concat(Object.keys(workspaces).reduce((prev, current) => prev.concat(workspaces[current]), []));
|
37 | const match = JSON.stringify(nodes).match(/jcr:([0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})/g);
|
38 | let damUuids = match ? match.map(id => id.substring(4)) : [];
|
39 | damUuids = damUuids.filter((id, pos) => {
|
40 | return damUuids.indexOf(id) === pos;
|
41 | });
|
42 | const damAssets = yield dam_util_1.fetchDamAssets(damUuids, this.options);
|
43 | const pagesObj = pages.map((page) => pages_util_1.sanitizeJson(page, damAssets, pages, this.options, workspaces));
|
44 | yield pages_util_1.writePagesFile(pagesObj, this.options);
|
45 | if (this.options.magnolia.workspaces) {
|
46 | for (const workspace of Object.keys(workspaces)) {
|
47 | const workspaceData = workspaces[workspace];
|
48 | if (workspaceData) {
|
49 | const sanitized = [];
|
50 | for (const item of workspaceData) {
|
51 | sanitized.push(pages_util_1.sanitizeJson(item, damAssets, workspaceData, this.options, workspaces));
|
52 | }
|
53 | yield pages_util_1.writeWorkspaceFile(workspace, sanitized, this.options);
|
54 | }
|
55 | }
|
56 | }
|
57 | resolve();
|
58 | }));
|
59 | }
|
60 | start() {
|
61 | return __awaiter(this, void 0, void 0, function* () {
|
62 | const operation = retry.operation({ forever: true });
|
63 | let conn;
|
64 | operation.attempt(() => __awaiter(this, void 0, void 0, function* () {
|
65 | try {
|
66 | conn = yield amqplib_1.connect(this.options.queue.uri);
|
67 | ["error", "close"].forEach($event => conn.on($event, this.retryConnection.bind(this, this.options.queue)));
|
68 | const channel = yield conn.createChannel();
|
69 | yield channel.assertExchange(this.options.queue.exchangeName || "paperboy", "fanout", {
|
70 | durable: false
|
71 | });
|
72 | const qok = yield channel.assertQueue(null, {
|
73 | autoDelete: true
|
74 | });
|
75 | channel.bindQueue(qok.queue, this.options.queue.exchangeName || "paperboy", "");
|
76 | channel.consume(qok.queue, this.consumeMessage.bind(this), {
|
77 | noAck: true
|
78 | });
|
79 | }
|
80 | catch (error) {
|
81 | if (operation.retry(error)) {
|
82 | console.error(`Could not establish connection to queue: ${error}`);
|
83 | return;
|
84 | }
|
85 | }
|
86 | }), {
|
87 | timeout: 10 * 1000,
|
88 | callback: () => {
|
89 | if (conn) {
|
90 | conn.close();
|
91 | }
|
92 | }
|
93 | });
|
94 | });
|
95 | }
|
96 | retryConnection() {
|
97 | console.info("Connection to queue dropped...");
|
98 | this.start();
|
99 | }
|
100 | consumeMessage(message) {
|
101 | console.info("[x] from Magnolia: %s -> '%s'", message.fields.routingKey, message.content.toString());
|
102 | this.generationLock.acquire("generationLock", (done) => __awaiter(this, void 0, void 0, function* () {
|
103 | try {
|
104 | yield this.generate();
|
105 | yield this.callback();
|
106 | }
|
107 | catch (err) {
|
108 | console.error("Generation failed.", err);
|
109 | }
|
110 | done();
|
111 | }), err => {
|
112 | if (err) {
|
113 | console.info("Already another pending message. Message discarded!");
|
114 | }
|
115 | });
|
116 | }
|
117 | }
|
118 | exports.MagnoliaSource = MagnoliaSource;
|
119 |
|
\ | No newline at end of file |