UNPKG

5.09 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 dam_util_1 = require("./dam.util");
13const pages_util_1 = require("./pages.util");
14const AsyncLock = require("async-lock");
15class MagnoliaSource {
16 constructor(options, callback) {
17 this.generationLock = new AsyncLock({ maxPending: 1 });
18 this.options = options;
19 this.callback = callback;
20 }
21 generate() {
22 return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
23 const sitemap = yield pages_util_1.fetchSitemap(this.options);
24 const website = yield pages_util_1.fetchPages(this.options);
25 const pages = sitemap
26 .map(path => website.find((page) => page['@path'] === path))
27 .filter(page => typeof page !== 'undefined');
28 const workspaces = {};
29 for (const workspace of this.options.magnolia.workspaces) {
30 workspaces[workspace] = yield pages_util_1.fetchWorkspace(workspace, this.options);
31 }
32 // get dam jcr ids
33 const nodes = pages.concat(Object.keys(workspaces).reduce((prev, current) => prev.concat(workspaces[current]), []));
34 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);
35 let damUuids = match ? match.map(id => id.substring(4)) : [];
36 damUuids = damUuids.filter((id, pos) => {
37 return damUuids.indexOf(id) === pos;
38 });
39 const damAssets = yield dam_util_1.fetchDamAssets(damUuids, this.options);
40 const pagesObj = pages.map((page) => pages_util_1.sanitizeJson(page, damAssets, pages, this.options));
41 yield pages_util_1.writePagesFile(pagesObj, this.options);
42 if (this.options.magnolia.workspaces) {
43 for (const workspace of Object.keys(workspaces)) {
44 const workspaceData = workspaces[workspace];
45 if (workspaceData) {
46 const sanitized = [];
47 for (const item of workspaceData) {
48 sanitized.push(pages_util_1.sanitizeJson(item, damAssets, workspaceData, this.options));
49 }
50 yield pages_util_1.writeWorkspaceFile(workspace, sanitized, this.options);
51 }
52 }
53 }
54 resolve();
55 }));
56 }
57 start() {
58 return __awaiter(this, void 0, void 0, function* () {
59 amqplib_1.connect(this.options.queue.uri)
60 .then(conn => {
61 conn.on('error', this.retryConnection.bind(this, this.options.queue));
62 return conn.createChannel();
63 })
64 .then(channel => {
65 channel
66 .assertExchange(this.options.queue.exchangeName || 'paperboy', 'fanout', {
67 durable: false
68 })
69 .then(() => {
70 return channel.assertQueue(null, {
71 autoDelete: true
72 });
73 })
74 .then(qok => {
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 })
81 .catch(() => {
82 this.retryConnection();
83 });
84 });
85 }
86 retryConnection() {
87 console.info('Connection to queue failed, will retry in 10s...');
88 setTimeout(() => {
89 this.start();
90 }, 10000);
91 }
92 consumeMessage(message) {
93 console.info("[x] from Magnolia: %s -> '%s'", message.fields.routingKey, message.content.toString());
94 this.generationLock.acquire('generationLock', (done) => {
95 this.generate().then(() => {
96 this.callback().then(() => done());
97 }).catch((err) => {
98 console.error('Generation failed.', err);
99 done();
100 });
101 }, (err, ret) => {
102 if (err) {
103 console.info('Already another pending message. Message discarded!');
104 }
105 });
106 }
107}
108exports.MagnoliaSource = MagnoliaSource;
109//# sourceMappingURL=magnolia-source.module.js.map
\No newline at end of file