-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.ts
78 lines (72 loc) · 2.15 KB
/
server.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
import fastifyStatic from '@fastify/static'
import { logging, server as wisp } from '@mercuryworkshop/wisp-js/server'
import Fastify from 'fastify'
import { createServer } from 'node:http'
import type { Socket } from 'node:net'
import { argv } from 'node:process'
import { fileURLToPath } from 'node:url'
import { mkdir } from 'node:fs/promises'
import { baremuxPath } from '@mercuryworkshop/bare-mux/node'
// @ts-expect-error
import { epoxyPath } from '@mercuryworkshop/epoxy-transport'
import { consola } from 'consola'
import { context } from 'esbuild'
import { rimraf } from 'rimraf'
const port = Number(process.env.PORT) || 9000
logging.set_level(logging.DEBUG)
Fastify({
serverFactory: (handler) =>
createServer(handler).on(
'upgrade',
(req, socket: Socket, head) =>
req.url?.endsWith('/wisp/') && wisp.routeRequest(req, socket, head)
)
})
.setNotFoundHandler((_req, reply) => {
reply.code(404).sendFile('404.html', './demo/')
})
.register(fastifyStatic, {
root: fileURLToPath(new URL('./demo', import.meta.url))
})
.register(fastifyStatic, {
root: fileURLToPath(new URL('./dist', import.meta.url)),
prefix: '/meteor/',
decorateReply: false
})
.register(fastifyStatic, {
root: baremuxPath,
prefix: '/baremux/',
decorateReply: false
})
.register(fastifyStatic, {
root: epoxyPath,
prefix: '/epoxy/',
decorateReply: false
})
.register(fastifyStatic, {
root: fileURLToPath(new URL('./assets', import.meta.url)),
prefix: '/assets/',
decorateReply: false
})
.listen({ port }, () => {
consola.success(`Server listening on http://localhost:${port}`)
})
if (!argv.includes('--no-build')) {
await rimraf('dist')
await mkdir('dist')
const dev = await context({
sourcemap: true,
minify: process.env.NODE_ENV !== 'development',
entryPoints: {
'meteor.bundle': './src/bundle/index.ts',
'meteor.client': './src/client/index.ts',
'meteor.worker': './src/worker.ts',
'meteor.codecs': './src/codecs/index.ts',
'meteor.config': './src/config.ts'
},
bundle: true,
logLevel: 'info',
outdir: 'dist/'
})
await dev.watch()
}