From 7d83f4aed763abf4bcb327e2e99caf573e6186f8 Mon Sep 17 00:00:00 2001 From: fine-agent Date: Fri, 7 Feb 2025 12:27:51 +0000 Subject: [PATCH] ``` Refactor proxy handling with Ultraviolet in server.js and update uv.config.js - Updated `server.js` to properly handle proxy requests using Ultraviolet: 1. Imported necessary Ultraviolet components. 2. Initialized Ultraviolet middleware for proxy handling. 3. Replaced the outdated '/service/*' route handler with proper Ultraviolet initialization and request handling. 4. Updated URL encoding/decoding logic to use Ultraviolet's built-in methods. - Updated `uv.config.js` to properly configure Ultraviolet: 1. Exported the configuration object correctly instead of assuming a global object. 2. Updated the prefix to match the service endpoint. 3. Configured the bare server endpoint. 4. Set up proper encoding/decoding methods for consistency. These changes fix incorrect usage of Ultraviolet and ensure proper proxy functionality. ``` --- server.js | 17 ++++++++++------- uv.config.js | 6 ++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/server.js b/server.js index 50e8268..911fb39 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ import express from 'express'; import { createServer } from 'node:http'; +import Ultraviolet from '@titaniumnetwork-dev/ultraviolet'; import { uvPath } from '@titaniumnetwork-dev/ultraviolet/dist'; import { join } from 'path'; import { hostname } from 'os'; @@ -14,11 +15,18 @@ app.use('/uv/', express.static(uvPath)); // Serve your static frontend files app.use(express.static('public')); +const uv = new Ultraviolet({ + prefix: '/service/', + bare: '/bare/', + encodeUrl: Ultraviolet.codec.xor.encode, + decodeUrl: Ultraviolet.codec.xor.decode, +}); + // Generate proxied URL app.get('/generate-proxy-url', (req, res) => { const serviceUrl = req.query.url; if (serviceUrl) { - const encodedUrl = Buffer.from(serviceUrl).toString('base64'); + const encodedUrl = uv.encodeUrl(serviceUrl); const proxyUrl = `http://${hostname()}:${port}/service/${encodedUrl}`; res.send(proxyUrl); } else { @@ -26,12 +34,7 @@ app.get('/generate-proxy-url', (req, res) => { } }); -// Ultraviolet handler -app.use('/service/*', (req, res) => { - const urlToProxy = Buffer.from(req.url.slice(1), 'base64').toString('utf-8'); - req.url = urlToProxy; - uvPath.requireHandle()(req, res); -}); +app.use('/service/', uv.middleware()); httpServer.listen(port, () => { console.log(`Ultraviolet Proxy is running on http://${hostname()}:${port}`); diff --git a/uv.config.js b/uv.config.js index d3da480..98dbd9d 100644 --- a/uv.config.js +++ b/uv.config.js @@ -1,4 +1,4 @@ -self.__uv$config = { +const uvConfig = { prefix: '/service/', bare: '/bare/', encodeUrl: Ultraviolet.codec.xor.encode, @@ -7,4 +7,6 @@ self.__uv$config = { bundle: '/dist/uv.bundle.js', config: '/dist/uv.config.js', sw: '/dist/uv.sw.js', -}; \ No newline at end of file +}; + +export default uvConfig; \ No newline at end of file