-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvite.config.js
More file actions
144 lines (127 loc) · 4.73 KB
/
vite.config.js
File metadata and controls
144 lines (127 loc) · 4.73 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
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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import http from 'http'
import https from 'https'
import fs from 'fs'
import path from 'path'
const DATA_FILE = path.resolve(__dirname, 'sonos-data.json')
/**
* Vite plugin: persists app state to sonos-data.json so data survives
* port changes (localStorage is origin-bound; this file is not).
* GET /sonos-store → returns the full JSON object
* POST /sonos-store → merges posted keys into the file
*/
function sonosStorePlugin() {
return {
name: 'sonos-store',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url.split('?')[0]
if (url !== '/sonos-store') return next()
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
if (req.method === 'OPTIONS') {
res.statusCode = 204
res.end()
return
}
if (req.method === 'GET') {
fs.promises.readFile(DATA_FILE, 'utf8')
.then(raw => JSON.parse(raw))
.catch(() => ({}))
.then(data => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
})
return
}
if (req.method === 'POST') {
let body = ''
req.on('data', (chunk) => { body += chunk })
req.on('end', async () => {
try {
const updates = JSON.parse(body)
const existing = await fs.promises.readFile(DATA_FILE, 'utf8')
.then(raw => JSON.parse(raw))
.catch(() => ({}))
await fs.promises.writeFile(DATA_FILE, JSON.stringify({ ...existing, ...updates }, null, 2))
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ ok: true }))
} catch (e) {
res.statusCode = 500
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: e.message }))
}
})
return
}
next()
})
},
}
}
/**
* Vite plugin: proxies GET /sonos-proxy?url=<encoded-target-url>
* through Node.js so the browser never makes a cross-origin request.
* This completely eliminates CORS issues with node-sonos-http-api.
*/
function sonosProxyPlugin() {
return {
name: 'sonos-proxy',
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (!req.url.startsWith('/sonos-proxy')) return next()
const queryStart = req.url.indexOf('?')
const qs = queryStart !== -1 ? req.url.slice(queryStart + 1) : ''
const params = new URLSearchParams(qs)
const targetUrl = params.get('url')
if (!targetUrl) {
res.statusCode = 400
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: 'Missing url param' }))
return
}
// Validate URL before passing to http.request
try { new URL(targetUrl) } catch {
res.statusCode = 400
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ error: `Invalid URL: ${targetUrl}` }))
return
}
// Set CORS headers so the browser accepts our proxy response
res.setHeader('Access-Control-Allow-Origin', '*')
const transport = targetUrl.startsWith('https:') ? https : http
const proxyReq = transport.request(targetUrl, { method: 'GET', timeout: 8000 }, (proxyRes) => {
res.statusCode = proxyRes.statusCode
// Pass through the real content-type so images render correctly
const contentType = proxyRes.headers['content-type']
if (contentType) res.setHeader('Content-Type', contentType)
// Collect binary-safe chunks as Buffers
const chunks = []
proxyRes.on('data', (chunk) => { chunks.push(chunk) })
proxyRes.on('end', () => res.end(Buffer.concat(chunks)))
})
proxyReq.on('timeout', () => {
proxyReq.destroy()
res.statusCode = 504
res.end(JSON.stringify({ error: 'Gateway timeout' }))
})
proxyReq.on('error', (err) => {
res.statusCode = 502
res.end(JSON.stringify({ error: err.code || err.message || 'ECONNREFUSED' }))
})
proxyReq.end()
})
},
}
}
export default defineConfig({
plugins: [react(), sonosStorePlugin(), sonosProxyPlugin()],
server: {
host: true,
port: process.env.PORT ? parseInt(process.env.PORT) : 5173,
},
})