-
Notifications
You must be signed in to change notification settings - Fork 631
/
index.ts
390 lines (355 loc) · 10.5 KB
/
index.ts
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
import crypto from 'crypto'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { createRequestHandler, type RequestHandler } from '@remix-run/express'
import { installGlobals, type ServerBuild } from '@remix-run/node'
import {
init as sentryInit,
setContext as sentrySetContext,
} from '@sentry/remix'
import { ip as ipAddress } from 'address'
import chalk from 'chalk'
import closeWithGrace from 'close-with-grace'
import compression from 'compression'
import express from 'express'
import 'express-async-errors'
import getPort, { portNumbers } from 'get-port'
import helmet from 'helmet'
import morgan from 'morgan'
import onFinished from 'on-finished'
import serverTiming from 'server-timing'
import sourceMapSupport from 'source-map-support'
import { type WebSocketServer } from 'ws'
import { getInstanceInfo } from '../app/utils/cjs/litefs-js.server.js'
import {
getRedirectsMiddleware,
oldImgSocial,
rickRollMiddleware,
} from './redirects.js'
sourceMapSupport.install()
installGlobals()
const viteDevServer =
process.env.NODE_ENV === 'production'
? undefined
: await import('vite').then((vite) =>
vite.createServer({
server: { middlewareMode: true },
}),
)
const getBuild = async (): Promise<ServerBuild> => {
if (viteDevServer) {
return viteDevServer.ssrLoadModule('virtual:remix/server-build') as any
}
// @ts-ignore (this file may or may not exist yet)
return import('../build/server/index.js') as Promise<ServerBuild>
}
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const here = (...d: Array<string>) => path.join(__dirname, ...d)
const primaryHost = 'kentcdodds.com'
const getHost = (req: { get: (key: string) => string | undefined }) =>
req.get('X-Forwarded-Host') ?? req.get('host') ?? ''
const MODE = process.env.NODE_ENV
if (MODE === 'production' && process.env.SENTRY_DSN) {
void import('./utils/monitoring.js').then(({ init }) => init())
}
if (MODE === 'production') {
sentryInit({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 0.3,
environment: process.env.NODE_ENV,
})
sentrySetContext('region', { name: process.env.FLY_INSTANCE ?? 'unknown' })
}
const app = express()
app.use(serverTiming())
app.get('/img/social', oldImgSocial)
// TODO: remove this once all clients are updated
app.post('/__metronome', (req, res) => {
res.status(503)
return res.send('Metronome is deprecated and no longer in use.')
})
app.use(async (req, res, next) => {
const { currentInstance, primaryInstance } = await getInstanceInfo()
res.set('X-Powered-By', 'Kody the Koala')
res.set('X-Fly-Region', process.env.FLY_REGION ?? 'unknown')
res.set('X-Fly-App', process.env.FLY_APP_NAME ?? 'unknown')
res.set('X-Fly-Instance', currentInstance)
res.set('X-Fly-Primary-Instance', primaryInstance)
res.set('X-Frame-Options', 'SAMEORIGIN')
const proto = req.get('X-Forwarded-Proto') ?? req.protocol
const host = getHost(req)
if (!host.endsWith(primaryHost)) {
res.set('X-Robots-Tag', 'noindex')
}
res.set('Access-Control-Allow-Origin', `${proto}://${host}`)
// if they connect once with HTTPS, then they'll connect with HTTPS for the next hundred years
res.set('Strict-Transport-Security', `max-age=${60 * 60 * 24 * 365 * 100}`)
next()
})
app.use(async (req, res, next) => {
if (req.get('cf-visitor')) {
// console.log(`👺 disallowed cf-visitor`, req.headers) // <-- this can be kinda noisy
// make them wait for it... Which should cost them money...
await new Promise((resolve) => setTimeout(resolve, 90_000))
return res.send(
'Please go to https://kcd.dev instead! Ping Kent if you think you should not be seeing this...',
)
} else {
return next()
}
})
app.use((req, res, next) => {
const proto = req.get('X-Forwarded-Proto')
const host = getHost(req)
if (proto === 'http') {
res.set('X-Forwarded-Proto', 'https')
res.redirect(`https://${host}${req.originalUrl}`)
return
}
next()
})
app.all(
'*',
getRedirectsMiddleware({
redirectsString: fs.readFileSync(here('./_redirects.txt'), 'utf8'),
}),
)
app.use((req, res, next) => {
if (req.path.endsWith('/') && req.path.length > 1) {
const query = req.url.slice(req.path.length)
const safepath = req.path.slice(0, -1).replace(/\/+/g, '/')
res.redirect(301, safepath + query)
} else {
next()
}
})
app.use(compression())
const publicAbsolutePath = here('../build/client')
if (viteDevServer) {
app.use(viteDevServer.middlewares)
} else {
app.use(
express.static(publicAbsolutePath, {
maxAge: '1w',
setHeaders(res, resourcePath) {
const relativePath = resourcePath.replace(`${publicAbsolutePath}/`, '')
if (relativePath.startsWith('build/info.json')) {
res.setHeader('cache-control', 'no-cache')
return
}
// If we ever change our font (which we quite possibly never will)
// then we'll just want to change the filename or something...
// Remix fingerprints its assets so we can cache forever
if (
relativePath.startsWith('fonts') ||
relativePath.startsWith('build')
) {
res.setHeader('cache-control', 'public, max-age=31536000, immutable')
}
},
}),
)
}
app.get(['/build/*', '/images/*', '/fonts/*', '/favicons/*'], (req, res) => {
// if we made it past the express.static for /build, then we're missing something. No bueno.
return res.status(404).send('Not found')
})
// log the referrer for 404s
app.use((req, res, next) => {
onFinished(res, () => {
const referrer = req.get('referer')
if (res.statusCode === 404 && referrer) {
console.info(
`👻 404 on ${req.method} ${req.path} referred by: ${referrer}`,
)
}
})
next()
})
app.use(
morgan(
(tokens, req, res) => {
try {
const host = getHost(req)
return [
tokens.method?.(req, res),
`${host}${decodeURIComponent(tokens.url?.(req, res) ?? '')}`,
tokens.status?.(req, res),
tokens.res?.(req, res, 'content-length'),
'-',
tokens['response-time']?.(req, res),
'ms',
].join(' ')
} catch (error: unknown) {
console.error(
`Error generating morgan log line`,
error,
req.originalUrl,
)
return ''
}
},
{
skip: (req, res) => {
if (res.statusCode !== 200) return false
// skip health check related requests
const headToRoot = req.method === 'HEAD' && req.originalUrl === '/'
const getToHealthcheck =
req.method === 'GET' && req.originalUrl === '/healthcheck'
return headToRoot || getToHealthcheck
},
},
),
)
app.use((req, res, next) => {
res.locals.cspNonce = crypto.randomBytes(16).toString('hex')
next()
})
app.use(
helmet({
crossOriginEmbedderPolicy: false,
contentSecurityPolicy: {
directives: {
'connect-src': [
...(MODE === 'development' ? ['ws:'] : []),
"'self'",
].filter(Boolean),
'font-src': ["'self'"],
'frame-src': [
"'self'",
'youtube.com',
'www.youtube.com',
'youtu.be',
'youtube-nocookie.com',
'www.youtube-nocookie.com',
'player.simplecast.com',
'egghead.io',
'app.egghead.io',
'calendar.google.com',
'codesandbox.io',
'share.transistor.fm',
'codepen.io',
],
'img-src': [
"'self'",
'data:',
'res.cloudinary.com',
'www.gravatar.com',
'cdn.usefathom.com',
'pbs.twimg.com',
'i.ytimg.com',
'image.simplecastcdn.com',
'images.transistor.fm',
'img.transistor.fm',
'i2.wp.com',
'i1.wp.com',
'og-image-react-egghead.now.sh',
'og-image-react-egghead.vercel.app',
'www.epicweb.dev',
...(MODE === 'development' ? ['cloudflare-ipfs.com'] : []),
],
'media-src': [
"'self'",
'res.cloudinary.com',
'data:',
'blob:',
'www.dropbox.com',
'*.dropboxusercontent.com',
],
'script-src': [
"'strict-dynamic'",
"'unsafe-eval'",
"'self'",
'cdn.usefathom.com',
// @ts-expect-error middleware is the worst
(req, res) => `'nonce-${res.locals.cspNonce}'`,
],
'script-src-attr': [
"'unsafe-inline'",
// TODO: figure out how to make the nonce work instead of
// unsafe-inline. I tried adding a nonce attribute where we're using
// inline attributes, but that didn't work. I still got that it
// violated the CSP.
],
'upgrade-insecure-requests': null,
},
},
}),
)
app.get('/redirect.html', rickRollMiddleware)
async function getRequestHandler(): Promise<RequestHandler> {
function getLoadContext(req: any, res: any) {
return { cspNonce: res.locals.cspNonce }
}
return createRequestHandler({
build: MODE === 'development' ? getBuild : await getBuild(),
mode: MODE,
getLoadContext,
})
}
app.all('*', await getRequestHandler())
const desiredPort = Number(process.env.PORT || 3000)
const portToUse = await getPort({
port: portNumbers(desiredPort, desiredPort + 100),
})
const server = app.listen(portToUse, () => {
const addy = server.address()
const portUsed =
desiredPort === portToUse
? desiredPort
: addy && typeof addy === 'object'
? addy.port
: 0
if (portUsed !== desiredPort) {
console.warn(
chalk.yellow(
`⚠️ Port ${desiredPort} is not available, using ${portUsed} instead.`,
),
)
}
console.log(`\n🐨 let's get rolling!`)
const localUrl = `http://localhost:${portUsed}`
let lanUrl: string | null = null
const localIp = ipAddress() ?? 'Unknown'
// Check if the address is a private ip
// https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
// https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-dev-utils/WebpackDevServerUtils.js#LL48C9-L54C10
if (/^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(localIp)) {
lanUrl = `http://${localIp}:${portUsed}`
}
console.log(
`
${chalk.bold('Local:')} ${chalk.cyan(localUrl)}
${lanUrl ? `${chalk.bold('On Your Network:')} ${chalk.cyan(lanUrl)}` : ''}
${chalk.bold('Press Ctrl+C to stop')}
`.trim(),
)
})
let wss: WebSocketServer | undefined
if (process.env.NODE_ENV === 'development') {
try {
const { contentWatcher } = await import('./content-watcher.js')
wss = contentWatcher(server)
} catch (error: unknown) {
console.error('unable to start content watcher', error)
}
}
closeWithGrace(() => {
return Promise.all([
new Promise((resolve, reject) => {
server.close((e) => (e ? reject(e) : resolve('ok')))
}),
new Promise((resolve, reject) => {
wss?.close((e) => (e ? reject(e) : resolve('ok')))
}),
])
})
/*
eslint
@typescript-eslint/ban-ts-comment: "off",
@typescript-eslint/prefer-ts-expect-error: "off",
@typescript-eslint/no-shadow: "off",
import/namespace: "off",
no-inner-declarations: "off",
*/