-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsumer.js
137 lines (130 loc) · 3.65 KB
/
consumer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import axios from 'axios';
import { Queue, UnrecoverableError, Worker } from 'bullmq';
import * as cheerio from 'cheerio';
import IORedis from 'ioredis';
import auth from '../backend/config/auth.config.js';
import { discoverWebmentionEndpoint } from './discoverWebmentionEndpoint.js';
import { processSaveWebmention } from './processSaveWebmention.js';
import { makeExternalRequestConfig } from './requestConfig.js';
const connection = new IORedis(
{ port: 32856, password: auth.redisPassword },
{ maxRetriesPerRequest: null },
);
const jobQueue = new Queue('jobQueue', {
connection,
defaultJobOptions: {
attempts: 3,
backoff: {
type: 'exponential',
delay: 1000,
},
},
});
const processDiscoverWebMention = async ({ source, target }) => {
try {
const webmentionEndpoint = await discoverWebmentionEndpoint(target);
if (webmentionEndpoint) {
// If an endpoint exists, add a send job to the queue
await jobQueue.add('send-webmention', {
source,
target,
webmentionEndpoint,
});
}
return;
} catch (err) {
if (err.response) {
if (err.response.status >= 400 && err.response.status < 500) {
// Don't retry
throw new UnrecoverableError(err);
}
}
// Retry on timeout/etc
throw err;
}
};
async function processSendWebmention({ source, target, webmentionEndpoint }) {
try {
await axios.post(
webmentionEndpoint,
{ source, target },
{
...makeExternalRequestConfig({
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}),
},
);
return;
} catch (err) {
if (err.response) {
if (err.response.status >= 400 && err.response.status < 500) {
// Don't retry
throw new UnrecoverableError(err);
}
}
// Retry on timeout/etc
throw err;
}
}
async function processReceiveWebmention({ source, target }) {
try {
const sourceResp = await axios.get(source, {
...makeExternalRequestConfig({
headers: { Accept: 'text/html, application/json, text/plain' },
}),
});
// If the source links to the target, add a job to save the webmention
if (sourceResp.headers['content-type'].includes('text/html')) {
const $ = cheerio.load(sourceResp.data);
const links = $(
`a[href="${target.href}"], img[src="${target.href}"], video[src="${target.href}"]`,
);
if (links) {
await jobQueue.add('save-webmention', { source, target });
}
return;
} else if (
(sourceResp.headers['content-type'].includes('application/json') &&
new RegExp(`["']${target.href}["']`).test(sourceResp.data)) ||
(sourceResp.headers['content-type'].includes('text/plain') &&
sourceResp.data.includes(target.href))
) {
await jobQueue.add('save-webmention', { source, target });
}
} catch (err) {
if (err.response) {
if (err.response.status >= 400 && err.response.status < 500) {
// Don't retry
throw new UnrecoverableError(err);
}
}
// Retry on timeout/etc
throw err;
}
}
new Worker(
'jobQueue',
async (job) => {
switch (job.name) {
case 'discover-webmention':
await processDiscoverWebMention(job.data);
break;
case 'send-webmention':
await processSendWebmention(job.data);
break;
case 'receive-webmention':
await processReceiveWebmention(job.data);
break;
case 'save-webmention':
await processSaveWebmention(job.data);
break;
}
},
{
connection,
removeOnComplete: { count: 100 },
removeOnFail: { count: 1000 },
},
);