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