-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
68 lines (58 loc) · 1.89 KB
/
vite.config.ts
File metadata and controls
68 lines (58 loc) · 1.89 KB
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
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig, type PluginOption } from 'vite';
import type { IncomingMessage } from 'http';
import type { Socket } from 'node:net';
import { WebSocketServer } from 'ws';
import { WebSocketManager } from './lib/websocket/server';
function websocketDevPlugin(): PluginOption {
return {
name: 'dev-websocket-server',
apply: 'serve',
configureServer(server) {
const httpServer = server.httpServer;
if (!httpServer) {
return;
}
const wsManager = new WebSocketManager();
const wssKeyboard = new WebSocketServer({ noServer: true });
const wssControl = new WebSocketServer({ noServer: true });
const handleUpgrade = (request: IncomingMessage, socket: Socket, head: Buffer) => {
if (!request.url) {
return;
}
const host = request.headers.host ?? 'localhost';
const pathname = new URL(request.url, `http://${host}`).pathname;
if (pathname === '/ws/keyboard') {
wssKeyboard.handleUpgrade(request, socket, head, (ws) => {
wsManager.handleKeyboardConnection(ws, request);
});
return;
}
if (pathname === '/ws/control') {
wssControl.handleUpgrade(request, socket, head, (ws) => {
wsManager.handleControlConnection(ws, request);
});
return;
}
// Not one of ours; let Vite's own upgrade handlers run
};
httpServer.on('upgrade', handleUpgrade);
httpServer.once('close', () => {
httpServer.off('upgrade', handleUpgrade);
wssKeyboard.clients.forEach((client) => client.terminate());
wssControl.clients.forEach((client) => client.terminate());
wssKeyboard.close();
wssControl.close();
});
console.log('[vite] WebSocket endpoints available at /ws/keyboard and /ws/control');
}
};
}
export default defineConfig({
plugins: [sveltekit(), websocketDevPlugin()],
server: {
fs: {
allow: ['recordings']
}
}
});