UNPKG

5.77 kBJavaScriptView Raw
1"use strict";
2var __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};
10Object.defineProperty(exports, "__esModule", { value: true });
11const amqplib_1 = require("amqplib");
12const retry = require("retry");
13const dam_util_1 = require("./dam.util");
14const pages_util_1 = require("./pages.util");
15const AsyncLock = require("async-lock");
16class 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 // get dam jcr ids
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 const channel = yield conn.createChannel();
68 yield channel.assertExchange(this.options.queue.exchangeName || "paperboy", "fanout", {
69 durable: false
70 });
71 const qok = yield channel.assertQueue(null, {
72 autoDelete: true
73 });
74 channel.bindQueue(qok.queue, this.options.queue.exchangeName || "paperboy", "");
75 channel.consume(qok.queue, this.consumeMessage.bind(this), {
76 noAck: true
77 });
78 ["error", "close"].forEach($event => conn.once($event, this.retryConnection.bind(this, this.options.queue)));
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.log("Connection to queue dropped. Will start attempting to reconnect in 5 seconds.");
98 setTimeout(this.start.bind(this), 5000);
99 }
100 consumeMessage(message) {
101 const content = JSON.parse(message.content.toString());
102 console.info(`[x] from ${content.source}`);
103 this.generationLock.acquire("generationLock", (done) => __awaiter(this, void 0, void 0, function* () {
104 try {
105 yield this.generate();
106 yield this.callback();
107 }
108 catch (err) {
109 console.error("Generation failed.", err);
110 }
111 done();
112 }), err => {
113 if (err) {
114 console.info("Already another pending message. Message discarded!");
115 }
116 });
117 }
118}
119exports.MagnoliaSource = MagnoliaSource;
120//# sourceMappingURL=magnolia-source.module.js.map
\No newline at end of file