|
1 | | -import path from 'path'; |
2 | 1 | import { defineConfig } from 'vite'; |
3 | 2 | import { VitePWA } from 'vite-plugin-pwa'; |
4 | 3 | import authGatePlugin from './vite-plugin-auth-gate.js'; |
| 4 | +import path from 'path'; |
| 5 | +import uploadPlugin from './vite-plugin-upload.js'; |
5 | 6 | import blobAssetPlugin from './vite-plugin-blob.js'; |
6 | 7 | import svgUse from './vite-plugin-svg-use.js'; |
7 | | -import uploadPlugin from './vite-plugin-upload.js'; |
8 | 8 | // import purgecss from 'vite-plugin-purgecss'; |
9 | | -import { playwright } from '@vitest/browser-playwright'; |
10 | | -import { execSync } from 'child_process'; |
11 | 9 | import purgecss from 'vite-plugin-purgecss'; |
| 10 | +import { execSync } from 'child_process'; |
| 11 | +import { playwright } from '@vitest/browser-playwright'; |
| 12 | +import type { IncomingMessage, ServerResponse } from 'http'; |
12 | 13 |
|
13 | 14 | function proxyAudioPlugin() { |
14 | 15 | return { |
15 | 16 | name: 'proxy-audio-dev', |
16 | 17 | configureServer(server) { |
17 | | - // No longer needed: local proxy-audio middleware replaced by remote proxy |
| 18 | + server.middlewares.use('/proxy', async (req: IncomingMessage, res: ServerResponse) => { |
| 19 | + const url = new URL(req.url ?? '', 'http://localhost'); |
| 20 | + const targetUrl = url.searchParams.get('url'); |
| 21 | + |
| 22 | + if (!targetUrl) { |
| 23 | + res.writeHead(400); |
| 24 | + res.end('Missing url parameter'); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const headers = new Headers(); |
| 30 | + headers.set('Origin', 'https://listen.tidal.com'); |
| 31 | + headers.set('User-Agent', req.headers['user-agent']); |
| 32 | + |
| 33 | + const upstream = await fetch(targetUrl, { |
| 34 | + method: req.method, |
| 35 | + headers, |
| 36 | + redirect: 'follow', |
| 37 | + }); |
| 38 | + |
| 39 | + const resHead = new Headers(upstream.headers); |
| 40 | + Object.entries({ |
| 41 | + 'Access-Control-Allow-Origin': '*', |
| 42 | + 'Access-Control-Allow-Methods': 'GET, HEAD, OPTIONS', |
| 43 | + 'Access-Control-Expose-Headers': '*', |
| 44 | + }).forEach(([key, value]) => resHead.set(key, value)); |
| 45 | + |
| 46 | + res.writeHead(upstream.status, { ...Object.fromEntries(resHead.entries()) }); |
| 47 | + |
| 48 | + const reader = upstream.body?.getReader(); |
| 49 | + |
| 50 | + if (reader) { |
| 51 | + while (true) { |
| 52 | + const { done, value } = await reader.read(); |
| 53 | + if (done) break; |
| 54 | + await res.write(value); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + res.end(); |
| 59 | + } catch (error) { |
| 60 | + res.writeHead(500); |
| 61 | + res.end('Proxy Error: ' + error.message); |
| 62 | + } |
| 63 | + }); |
18 | 64 | }, |
19 | 65 | }; |
20 | 66 | } |
@@ -44,6 +90,9 @@ export default defineConfig((_options) => { |
44 | 90 | define: { |
45 | 91 | __COMMIT_HASH__: JSON.stringify(commitHash), |
46 | 92 | __VITEST__: !!process.env.VITEST, |
| 93 | + __VITE_PROXY__: JSON.stringify( |
| 94 | + _options.mode == 'development' ? '/proxy' : 'https://audio-proxy.binimum.org/proxy-audio' |
| 95 | + ), |
47 | 96 | }, |
48 | 97 | worker: { |
49 | 98 | format: 'es', |
|
0 commit comments