-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (81 loc) · 2.66 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
import fastify from 'fastify';
import fastifyBabel from 'fastify-babel';
import fastifyStatic from '@fastify/static';
import fastifyHttpProxy from '@fastify/http-proxy';
export class FastifyIntegrationDaemon {
constructor(fastifyOptions) {
this.daemon = fastify(fastifyOptions);
}
serveProxy({prefix, upstream, onResponse}) {
const replyOptions = onResponse ? {onResponse} : undefined;
this.daemon.register(fastifyHttpProxy, {
upstream: `${upstream}${prefix}`,
prefix,
replyOptions
});
return this;
}
forwardWS({path, destination}) {
this.daemon.register(fastifyHttpProxy, {
websocket: true,
prefix: path,
upstream: destination,
wsUpstream: destination
});
return this;
}
serveBuilt({prefix, root}) {
this.daemon.register((fastify, options, next) => {
fastify
.register(fastifyStatic, {
root,
redirect: true
})
.setNotFoundHandler((_, reply) => reply.sendFile('index.html'));
next();
}, {prefix});
return this;
}
serveSource({prefix, root, manifest, nodeModules, babelrc}) {
const babelCache = new Map();
this.daemon.get('/purge-cache', (_, reply) => {
const message = `Cleared ${babelCache.size} entries`;
babelCache.clear();
console.log(message);
reply.send(`${message}\n`);
});
this.daemon.register((fastify, options, next) => {
fastify
.register((fastify, options, next) => {
fastify.register(fastifyStatic, {
root,
redirect: true
});
fastify.get('/manifest.webmanifest', (_, reply) => reply.send(manifest));
fastify.setNotFoundHandler((_, reply) => reply.sendFile('index.html'));
next();
}, {prefix})
.register(fastifyStatic, {
prefix: '/node_modules',
root: nodeModules,
decorateReply: false
})
.register(fastifyBabel, {
babelrc,
maskError: false,
cache: babelCache
});
next();
});
return this;
}
listen(...args) {
return this.daemon.listen(...args);
}
unref() {
this.daemon.server.unref();
}
address() {
return this.daemon.server.address();
}
}