-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
569 lines (526 loc) · 14.7 KB
/
index.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// HTTP Server Request Handler
import Busboy from 'busboy'
import constants from './constants.js'
import * as commonmark from 'commonmark'
import cookie from 'cookie'
import doNotCache from 'do-not-cache'
import escapeHTML from 'escape-html'
import etag from 'etag'
import formatTime from './format-time.js'
import fs from 'fs'
import grayMatter from 'gray-matter'
import html from './html.js'
import httpHash from 'http-hash'
import mustache from 'mustache'
import parseURL from 'url-parse'
import path from 'path'
import querystring from 'querystring'
import relativeDate from 'tiny-relative-date'
import runParallel from 'run-parallel'
import semver from 'semver'
import yaml from 'js-yaml'
import { spawnSync } from 'child_process'
const about = preloadMarkdown('about.md')
const contribute = preloadMarkdown('contribute.md')
const thanks = preloadMarkdown('thanks.md')
const contact = preloadMarkdown('contact.md')
const versionsBlurb = preloadMarkdown('versions.md')
const dealTerms = (() => {
const { content, data: { title, description } } = grayMatter(fs.readFileSync('deal.md', 'utf8'))
return {
title,
description,
content: preprocessMarkdown(content)
}
})()
dealTerms.version = new Date(spawnSync('/usr/bin/git', ['log', '-1', '--format="%ad"', '--date=rfc', '--', 'deal.md']).stdout.toString()).toISOString()
function preloadMarkdown (file) {
return preprocessMarkdown(fs.readFileSync(file, 'utf8'))
}
function preprocessMarkdown (text) {
return replaceConstants(markdown(text, { unsafe: true }))
}
// Router
const routes = httpHash()
routes.set('/', serveHomepage)
routes.set('/pay', servePay)
const dealHREF = '/deal'
routes.set(dealHREF, serveDeal)
routes.set('/privacy', servePrivacy)
routes.set('/versions', serveVersionsIndex)
routes.set('/versions/:version', requireCookie(serveVersion))
routes.set('/contribute', serveContribute)
routes.set('/thanks', serveThanks)
routes.set('/contact', serveContact)
if (process.env.NODE_ENV !== 'production') {
routes.set('/internal-error', (request, response) => {
serve500(request, response, new Error('test error'))
})
}
const staticFiles = {
'ads.txt': 'text/plain; charset=UTF-8',
'styles.css': 'text/css; charset=UTF-8',
'normalize.css': 'text/css; charset=UTF-8',
'credits.txt': 'text/plain; charset=UTF-8',
'security.txt': 'text/plain; charset=UTF-8',
'logo.svg': 'image/svg+xml',
'logo-on-white-100.png': 'image/png'
}
for (const name in staticFiles) {
const data = fs.readFileSync(name, 'utf8')
staticFiles[name] = {
content: staticFiles[name],
data,
etag: etag(data)
}
routes.set(`/${name}`, (request, response) => {
const { etag, data, content } = staticFiles[name]
response.setHeader('Content-Type', content)
response.setHeader('ETag', etag)
response.end(data)
})
}
// HTTP Request Handler
export default (request, response) => {
const parsed = request.parsed = parseURL(request.url, true)
const pathname = request.pathname = parsed.pathname
request.query = parsed.query
const { handler, params } = routes.get(pathname)
if (handler) {
request.parameters = params
return handler(request, response)
}
serve404(request, response)
}
// Partials
function meta ({
title = constants.name,
description = constants.slogan
}) {
let returned = html`
<meta charset=UTF-8>
<meta name=viewport content="width=device-width, initial-scale=1">
`
if (description) {
returned += html`
<meta name="description" content="${escapeHTML(description)}">
`
}
if (title && description) {
returned += html`
<meta name="twitter:card" content="summary">
<meta name="twitter:description" content="${escapeHTML(description)}">
<meta name="twitter:image" content="${process.env.BASE_HREF}/logo-on-white-100.png">
<meta name="twitter:site" content="@${escapeHTML(constants.twitter)}">
<meta name="twitter:title" content="${escapeHTML(title)}">
<meta name="og:type" content="website">
<meta name="og:title" content="${escapeHTML(title)}">
<meta name="og:description" content="${escapeHTML(description)}">
<meta name="og:image" content="${process.env.BASE_HREF}/logo-on-white-100.png">
<meta name="og:site" content="${escapeHTML(constants.name)}">
`
}
returned += html`
<link href=/normalize.css rel=stylesheet>
<link href=/styles.css rel=stylesheet>
`
return returned
}
const header = `
<header role=banner>
<a href=/><img src=/logo.svg id=logo alt=logo></a>
<h1>${escapeHTML(constants.name)}</h1>
<p class=slogan>${escapeHTML(constants.slogan)}</p>
</header>
`
const footer = `
<footer role=contentinfo>
<a href=${dealHREF}>Deal</a>
<a href=/contact>Contact</a>
<a href=/credits.txt>Software</a>
<a href=/thanks>Thanks</a>
<p>an <a href=https://artlessdevices.com>Artless Devices</a> project</p>
</footer>
`
const latestVersionHREF = '/versions/latest'
const nav = `
<nav role=navigation>
<a href=/>About</a>
<a id=latest href=${latestVersionHREF}>Terms</a>
<a href=/versions>Versions</a>
<a href=/pay>Pay</a>
<a href=/contribute>Contribute</a>
</nav>
`
// Routes
function serveHomepage (request, response) {
if (request.method !== 'GET') return serve405(request, response)
response.setHeader('Content-Type', 'text/html')
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({
title: constants.name,
description: constants.slogan
})}
<title>${escapeHTML(constants.name)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
${about}
</main>
${footer}
</body>
</html>
`)
}
function serveContribute (request, response) {
serveStatic(request, response, {
content: contribute,
title: `Contribute to ${constants.name}`,
heading: 'Contribute',
description: constants.slogan
})
}
function serveThanks (request, response) {
serveStatic(request, response, {
content: thanks,
title: `${constants.name} Thanks`,
heading: 'Thanks',
description: constants.slogan
})
}
function serveContact (request, response) {
serveStatic(request, response, {
content: contact,
title: 'Contact',
heading: 'Contact',
description: constants.slogan
})
}
function serveStatic (request, response, { title, heading, description, content }) {
if (request.method !== 'GET') return serve405(request, response)
response.setHeader('Content-Type', 'text/html')
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({ title, description })}
<title>${escapeHTML(title)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
<h2>${escapeHTML(heading)}</h2>
${content}
</main>
${footer}
</body>
</html>
`)
}
function serveDeal (request, response) {
const { method } = request
if (method === 'POST') {
let parser
let valid = false
try {
parser = new Busboy({
headers: request.headers,
limits: {
fieldNameSize: 'version'.length,
fields: 1,
fieldSizeLimit: new Date().toISOString().length,
parts: 1
}
})
.once('field', (name, value, truncated, encoding, mime) => {
if (name === 'version' && value === dealTerms.version) {
valid = true
}
})
.once('finish', () => {
if (valid) {
setCookie(response, dealTerms.version)
const location = request.query.destination || latestVersionHREF
serve303(request, response, location)
} else {
serveDealForm(request, response)
}
})
request.pipe(parser)
} catch (error) {
response.statusCode = 400
response.end()
}
} else if (method === 'GET') {
serveDealForm(request, response)
} else {
serve405(request, response)
}
}
function serveDealForm (request, response) {
doNotCache(response)
if (
request.headers.cookie &&
cookie.parse(request.headers.cookie)[constants.cookie.name] !== dealTerms.version
) clearCookie(response)
const title = `${dealTerms.title} — ${constants.name}`
response.setHeader('Content-Type', 'text/html')
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({
title,
description: dealTerms.description
})}
<title>${escapeHTML(title)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
<form id=passwordForm method=post>
<input type=hidden name=version value="${dealTerms.version}">
<p id=version>These terms were last updated ${escapeHTML(relativeDate(new Date(dealTerms.version)))}, on ${escapeHTML(formatTime(dealTerms.version))}.</p>
<button id=agree type=submit>Agree and Continue</button>
${dealTerms.content}
<button type=submit>Agree and Continue</button>
</form>
</main>
${footer}
</body>
</html>
`)
}
function servePay (request, response) {
serve302(request, response, process.env.STRIPE_LINK)
}
function serveVersionsIndex (request, response) {
if (request.method !== 'GET') return serve405(request, response)
response.setHeader('Content-Type', 'text/html')
readVersions((error, versions) => {
if (error) return serve500(request, response, error)
const title = `${constants.name} Versions`
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({
title: title,
description: constants.slogan
})}
<title>${escapeHTML(title)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
<h2>Versions</h2>
${versionsBlurb}
<ul>
${versions.map(v => `<li><a href=/versions/${escapeHTML(v)}>Version ${escapeHTML(v)}</a></li>`)}
</ul>
</main>
${footer}
</body>
</html>
`)
})
}
function serveVersion (request, response) {
const { version } = request.parameters
if (version === 'latest') {
return redirectToLatestVersion(request, response)
}
runParallel({
prompts: done => {
const file = path.join(process.env.DIRECTORY, 'versions', version, 'prompts.yml')
fs.readFile(file, 'utf8', (error, data) => {
if (error) return done(error)
let parsed
try {
parsed = yaml.load(data, { schema: yaml.JSON_SCHEMA })
} catch (error) {
return done(error)
}
done(null, parsed)
})
},
terms: readTemplate('terms'),
order: readTemplate('order')
}, (error, results) => {
if (error) return serve500(request, response, error)
response.setHeader('Content-Type', 'text/html')
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({
title: `${constants.name} ${version}`,
description: constants.slogan
})}
<title>${escapeHTML(constants.name)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
<h2>Version ${escapeHTML(version)}</h2>
<h3>Prompts</h3>
<pre>${escapeHTML(results.prompts)}</pre>
<h3>Order</h3>
<pre>${escapeHTML(results.order)}</pre>
<h3>Terms</h3>
<pre>${escapeHTML(results.terms)}</pre>
</main>
${footer}
</body>
</html>
`)
})
function readTemplate (basename) {
return done => {
const file = path.join(process.env.DIRECTORY, 'versions', version, `${basename}.md`)
fs.readFile(file, 'utf8', done)
}
}
}
function redirectToLatestVersion (request, response) {
readLatestVersion((error, version) => {
if (error) return serve500(request, response, error)
serve302(request, response, `/versions/${version}`)
})
}
function servePrivacy (request, response) {
serve404(request, response)
}
function serve404 (request, response) {
response.statusCode = 404
const title = 'Not Found'
response.setHeader('Content-Type', 'text/html')
response.end(`
<!doctype html>
<html lang=en-US>
<head>
${meta({})}
<title>${escapeHTML(title)}</title>
</head>
<body>
${header}
${nav}
<main role=main>
<h2>${escapeHTML(title)}</h2>
<p>The page you tried to visit doesn’t exist on this site.</p>
</main>
${footer}
</body>
</html>
`)
}
function serve500 (request, response, error) {
request.log.error(error)
response.statusCode = 500
const title = 'Internal Error'
response.setHeader('Content-Type', 'text/html')
response.end(html`
<!doctype html>
<html lang=en-US>
<head>
${meta({})}
<title>${escapeHTML(title)}</title>
</head>
<body>
<main role=main>
<h1>${escapeHTML(title)}</h1>
<p>The server ran into an error.</p>
<p>
If you'd like, you can
<a href=mailto:${escapeHTML(constants.email)}>e-mail support</a>,
pasting in this unique support number:
<code>${escapeHTML(request.id)}</code>
</p>
</main>
${footer}
</body>
</html>
`)
}
function serve405 (request, response) {
response.statusCode = 405
response.setHeader('Content-Type', 'text/plain')
response.end('Method Not Allowed')
}
function serve303 (request, response, location) {
response.statusCode = 303
response.setHeader('Location', location)
response.end()
}
function serve302 (request, response, location) {
response.statusCode = 302
response.setHeader('Location', location)
response.end()
}
function requireCookie (handler) {
return (request, response) => {
const header = request.headers.cookie
if (!header) return redirect()
const parsed = cookie.parse(header)
const version = parsed[constants.cookie.name]
if (!version) return redirect()
if (version !== dealTerms.version) return redirect()
handler(request, response)
function redirect () {
const query = querystring.stringify({ destination: request.url })
const location = dealHREF + '?' + query
serve303(request, response, location)
}
}
}
function setCookie (response, value) {
const expires = new Date(
Date.now() + (constants.cookie.days * 24 * 60 * 60 * 1000)
)
response.setHeader(
'Set-Cookie',
cookie.serialize(constants.cookie.name, value, {
expires,
httpOnly: true,
sameSite: 'strict',
secure: process.env.NODE_ENV === 'production'
})
)
}
function clearCookie (response) {
setCookie(response, '', new Date('1970-01-01'))
}
function readVersions (callback) {
const directory = path.join(process.env.DIRECTORY, 'versions')
fs.readdir(directory, (error, entries) => {
if (error) return callback(error)
const versions = entries
.filter(semver.valid)
.sort(semver.rcompare)
callback(null, versions)
})
}
function readLatestVersion (callback) {
readVersions((error, versions) => {
if (error) return callback(error)
callback(null, versions[0])
})
}
function replaceConstants (template) {
return mustache.render(template, constants)
}
function markdown (input) {
const reader = new commonmark.Parser()
const writer = new commonmark.HtmlRenderer()
const parsed = reader.parse(input, { smart: true, safe: false })
return writer.render(parsed)
}