-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
59 lines (51 loc) · 2.11 KB
/
Copy pathvite.config.js
File metadata and controls
59 lines (51 loc) · 2.11 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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { createServer as createHttpsServer } from 'https'
import { request as httpsRequest } from 'https'
import { request as httpRequest } from 'http'
// Dev proxy middleware — replicates what api/lnurl.js does on Vercel
function lnurlProxyPlugin() {
return {
name: 'lnurl-proxy',
configureServer(server) {
server.middlewares.use('/api/lnurl', (req, res) => {
const url = new URL(req.url, 'http://localhost')
const addr = url.searchParams.get('address')
const cb = url.searchParams.get('callback')
const amt = url.searchParams.get('amount')
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Content-Type', 'application/json')
if (req.method === 'OPTIONS') { res.statusCode = 204; res.end(); return }
const proxyFetch = (targetUrl) => {
const mod = targetUrl.startsWith('https') ? httpsRequest : httpRequest
const r = mod(targetUrl, { headers: { Accept: 'application/json' } }, (upstream) => {
let data = ''
upstream.on('data', c => data += c)
upstream.on('end', () => { res.statusCode = upstream.statusCode; res.end(data) })
})
r.on('error', (e) => { res.statusCode = 502; res.end(JSON.stringify({ error: e.message })) })
r.end()
}
if (addr) {
const [user, domain] = addr.split('@')
if (!user || !domain) { res.statusCode = 400; res.end(JSON.stringify({ error: 'Indirizzo non valido' })); return }
proxyFetch(`https://${domain}/.well-known/lnurlp/${user}`)
return
}
if (cb && amt) {
const cbUrl = new URL(decodeURIComponent(cb))
cbUrl.searchParams.set('amount', amt)
proxyFetch(cbUrl.toString())
return
}
res.statusCode = 400
res.end(JSON.stringify({ error: 'Parametri mancanti' }))
})
},
}
}
export default defineConfig({
plugins: [react(), lnurlProxyPlugin()],
define: { global: 'globalThis' },
resolve: { alias: { '@': '/src' } },
})