-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
254 lines (224 loc) · 8.03 KB
/
start.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require('util').inspect.defaultOptions.depth = process.env.DEBUG_DEPTH
require('dotenv').config()
const Plebbit = require('@plebbit/plebbit-js')
const assert = require('assert')
const config = require('./config')
const express = require('express')
const app = express()
app.on('error', e => console.log(e.message))
const port = config.port
assert(port, 'missing config.port')
const {getCommentMediaInfo} = require('./lib/utils')
const QuickLRU = require('quick-lru')
const commentCache = new QuickLRU({maxSize: 10000})
const htmlCache = new QuickLRU({maxSize: 10000})
const failedCache = new QuickLRU({maxSize: 100000})
const Debug = require('debug')
const debug = Debug('plebbit-previewer:server')
Debug.enable('plebbit-previewer:*')
const maxAttempts = 5
// use google headers on twitter or doesn't work
const ogs = require('open-graph-scraper')
const googleHeaders = {'user-agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'}
const useGoogleHeaders = new Set(['twitter.com', 'www.twitter.com', 'x.com', 'www.x.com'])
const browserHeaders = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'}
let plebbit
Plebbit(config.plebbitOptions).then(_plebbit => {
plebbit = _plebbit
plebbit.on('error', e => debug(e.message))
})
assert(Array.isArray(config.redirects), `config.redirects not an array`)
const allowedRedirects = new Set(config.redirects)
assert(typeof config.redirects[0] === 'string', `config.redirects[0] not a a string`)
const defaultRedirect = config.redirects[0]
const serve = async (req, res, subplebbitAddress, commentCid) => {
let redirect = req.query.redirect?.replace?.(/\/$/, '')
// allow redirect=1 to redirect to config.redirects[1]
if (config.redirects[redirect]) {
redirect = config.redirects[redirect]
}
debug(req.url, subplebbitAddress, commentCid, redirect)
if (!allowedRedirects.has(redirect)) {
redirect = defaultRedirect
}
debug('getting comment', commentCid)
let comment = commentCache.get(commentCid)
if (!comment) {
if (failedCache.get(commentCid) >= maxAttempts) {
debug('failed cache max attempt reached', commentCid)
return res.status(404).end()
}
try {
const res = await plebbit.getComment(commentCid)
comment = {
title: res.title,
subplebbitAddress: res.subplebbitAddress,
link: res.link,
content: res.content,
shortAuthorAddress: res.author.shortAddress,
thumbnailUrl: res.thumbnailUrl,
}
comment.mediaInfo = getCommentMediaInfo(comment)
// try adding short subplebbit addres
try {
comment.shortSubplebbitAddress = (await plebbit.createSubplebbit({address: comment.subplebbitAddress || 'n/a'})).shortAddress
}
catch (e) {}
// fetch thumbnail if doesn't exist
if (comment.link && !comment.mediaInfo) {
try {
const headers = useGoogleHeaders.has(new URL(comment.link).hostname) ? googleHeaders : browserHeaders
const res = await ogs({url: comment.link, headers})
comment.thumbnailUrl = res.result.ogImage.url
if (!comment.thumbnailUrl) {
throw Error(`open-graph-scraper result has no ogImage.url ${JSON.stringify(res.result, null, 2)}`)
}
comment.mediaInfo = {url: comment.thumbnailUrl, type: 'image'}
}
catch (e) {
debug('failed fetching comment.link thumbnail', comment.link, e?.message || e?.result?.error || e)
}
}
}
catch (e) {
failedCache.set(commentCid, (failedCache.get(commentCid) || 0) + 1)
debug('failed getting comment', commentCid, e.message)
return res.status(404).end()
}
commentCache.set(commentCid, comment)
}
debug(comment)
if (subplebbitAddress && subplebbitAddress !== comment.subplebbitAddress) {
debug(`subplebbitAddress '${subplebbitAddress}' !== '${comment.subplebbitAddress}'`)
return res.status(404).end()
}
let html = htmlCache.get(commentCid + redirect)
if (!html) {
let twitterCard = 'summary'
// image
const image = comment.mediaInfo?.url
let ogImageTag = ''
let twitterImageTag = ''
if (image) {
twitterCard = 'summary_large_image'
ogImageTag = `
<meta property="og:image" content="${image}"/>`
twitterImageTag = `
<meta name="twitter:image" content="${image}"/>
<meta name="twitter:image:src" content="${image}"/>`
}
// title
let title = comment.title
if (!title && comment.content) {
title = comment.content
if (title.length > 60) {
title = title.slice(0, 60) + '...'
}
}
if (!title) {
title = '-'
}
// description
let description = `Posted by u/${comment.shortAuthorAddress} in p/${comment.shortSubplebbitAddress || comment.subplebbitAddress}`
if (comment.content?.trim?.()) {
description += ` - ${comment.content.trim()}`
}
// add query string back, useful for ?context=3 on old.reddit
let queryString = ''
for (const query in req.query) {
if (query === 'redirect' || query === 'r') {
continue
}
if (queryString === '') {
queryString += '?'
}
else {
queryString += '&'
}
queryString += `${query}=${req.query[query]}`
}
const redirectUrl = `https://${redirect}/#/p/${comment.subplebbitAddress}/c/${commentCid}${queryString}`
const iconUrl = `https://${redirect}/favicon.ico`
// derive site name from redirect url
let siteName = 'plebbit'
if (redirect.includes('seedit')) {
siteName = 'seedit'
}
else if (redirect.includes('plebchan')) {
siteName = 'plebchan'
}
html = `<!DOCTYPE html>
<html>
<head>
<meta charSet="utf-8"/>
<title>${title}</title>
<meta name="title" content="${title}"/>
<meta name="description" content="${description}"/>
<meta property="og:site_name" content="${siteName}" />
<meta property="og:type" content="website"/>
<meta property="og:url" content="${redirectUrl}"/>
<meta property="og:title" content="${title}"/>
<meta property="og:description" content="${description}"/>${ogImageTag}
<meta name="twitter:card" content="${twitterCard}"/>
<meta name="twitter:url" content="${redirectUrl}"/>
<meta name="twitter:title" content="${title}"/>
<meta name="twitter:description" content="${description}"/>${twitterImageTag}
<link rel="icon" href="${iconUrl}"/>
<link rel="apple-touch-icon" href="${iconUrl}"/>
</head>
<body>
<script>
window.location.replace("${redirectUrl}")
</script>
</body>
</html>`
htmlCache.set(commentCid + redirect, html)
}
// the comment is immutable, so set the cache a long time
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
res.send(html)
}
const dontServe = (req, res) => {
// irrelevant endpoints
if (req.url === '/service-worker.js' || req.url === '/manifest.json' || req.url === '/favicon.ico') {
res.status(404).end()
return true
}
if (!plebbit) {
debug('plebbit not defined yet')
res.status(404).end()
return true
}
return false
}
// robots.txt needed to not prevent bots from crawling the previews
app.get('/robots.txt', async (req, res) => {
res.send(`User-agent: *
Allow: /`)
})
app.get('/p/:subplebbitAddress/c/:commentCid', async (req, res) => {
if (dontServe(req, res)) {
return
}
const {subplebbitAddress, commentCid} = req.params
await serve(req, res, subplebbitAddress, commentCid)
})
app.get('/c/:commentCid', async (req, res) => {
if (dontServe(req, res)) {
return
}
const {commentCid} = req.params
await serve(req, res, undefined, commentCid)
})
app.get('/:commentCid', async (req, res) => {
if (dontServe(req, res)) {
return
}
const {commentCid} = req.params
await serve(req, res, undefined, commentCid)
})
app.listen(port, () => console.log(`listening on port ${port}`))
.on('error', e => console.log(e.message))
// uncomment to listen on port 80 as well
// app.listen(80, () => console.log(`listening on port ${port}`))
// .on('error', e => console.log(e.message))