Skip to content

Commit 31aec22

Browse files
committed
Fix development proxy
1 parent 4caa9bc commit 31aec22

7 files changed

Lines changed: 101 additions & 27 deletions

File tree

server.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@ app.use(express.json());
99
app.use(express.static('dist'));
1010

1111
app.post('/api/rpc',async (req: Express.Request, res: Express.Response, next): Promise<void> => {
12-
fetch(process.env.DEFAULT_DAEMON,{
12+
if(!req.query.url){
13+
res.json({
14+
jsonrpc: '2.0',
15+
id: req.body.id,
16+
error: {
17+
message: 'Missing URL',
18+
},
19+
});
20+
return;
21+
}
22+
23+
const headers: Headers = new Headers();
24+
if(req.header('Authorization')){
25+
headers.append('Authorization',req.header('Authorization'));
26+
}
27+
if(req.header('Content-Type')){
28+
headers.append('Content-Type',req.header('Content-Type'));
29+
}
30+
31+
fetch(req.query.url,{
1332
method: 'POST',
14-
headers: {
15-
'Content-Type': 'application/json',
16-
},
33+
headers: headers,
1734
body: JSON.stringify(req.body),
1835
}).then((resp: Response): void => {
1936
resp.json().then((json): void => {

src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import LBRY from "./LBRY";
55
const notTags = ["porn","porno","nsfw","mature","xxx","sex","creampie","blowjob","handjob","vagina","boobs","big boobs","big dick","pussy","cumshot","anal","hard fucking","ass","fuck","hentai"];
66

77
function App(): JSX.Element {
8-
LBRY.rpc('claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["80d2590ad04e36fb1d077a9b9e3a8bba76defdf8","b58dfaeab6c70754d792cdd9b56ff59b90aea334"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"release_time":">1731193200","include_purchase_receipt":true}).then(json => {
8+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["80d2590ad04e36fb1d077a9b9e3a8bba76defdf8","b58dfaeab6c70754d792cdd9b56ff59b90aea334"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"release_time":">1731193200","include_purchase_receipt":true}).then(json => {
99
document.getElementById('row-1').innerHTML = '';
1010
json.result.items.forEach((item: object): void => {
1111
document.getElementById('row-1').innerHTML += '<div style="border:1px solid red;display:inline-block;max-width:200px;"><img alt="Thumbnail" src="'+(item.value?.thumbnail?.url || item.reposted_claim?.value?.thumbnail?.url)+'" style="height:100px;"><br>'+(item.value?.title || item.reposted_claim?.value?.title)+'</div>';
1212
});
1313
document.getElementById('row-1').innerHTML += '<br>-------<br>';
1414
});
1515

16-
LBRY.rpc('claim_search',{"page_size":4,"claim_type":["stream"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":[],"not_channel_ids":[],"order_by":["effective_amount"],"has_source":true,"release_time":">1762902000","limit_claims_per_channel":2,"include_purchase_receipt":true}).then(json => {
16+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":[],"not_channel_ids":[],"order_by":["effective_amount"],"has_source":true,"release_time":">1762902000","limit_claims_per_channel":2,"include_purchase_receipt":true}).then(json => {
1717
document.getElementById('row-2').innerHTML = '';
1818
json.result.items.forEach((item: object): void => {
1919
document.getElementById('row-2').innerHTML += '<div style="border:1px solid red;display:inline-block;max-width:200px;"><img alt="Thumbnail" src="'+(item.value?.thumbnail?.url || item.reposted_claim?.value?.thumbnail?.url)+'" style="height:100px;"><br>'+(item.value?.title || item.reposted_claim?.value?.title)+'</div>';
2020
});
2121
document.getElementById('row-2').innerHTML += '<br>-------<br>';
2222
});
2323

24-
LBRY.rpc('claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["3fda836a92faaceedfe398225fb9b2ee2ed1f01a"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"include_purchase_receipt":true}).then(json => {
24+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','claim_search',{"page_size":4,"claim_type":["stream","repost","channel"],"no_totals":true,"any_tags":[],"not_tags":notTags,"channel_ids":["3fda836a92faaceedfe398225fb9b2ee2ed1f01a"],"not_channel_ids":[],"order_by":["release_time"],"has_source":true,"include_purchase_receipt":true}).then(json => {
2525
document.getElementById('row-3').innerHTML = '';
2626
json.result.items.forEach((item: object): void => {
2727
document.getElementById('row-3').innerHTML += '<div style="border:1px solid red;display:inline-block;max-width:200px;"><img alt="Thumbnail" src="'+(item.value?.thumbnail?.url || item.reposted_claim?.value?.thumbnail?.url)+'" style="height:100px;"><br>'+(item.value?.title || item.reposted_claim?.value?.title)+'</div>';

src/LBRY.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
async function rpc(method: string,params?: object): Promise<object>{
2-
//return await (await fetch(import.meta.env.VITE_DEFAULT_DAEMON,{
3-
return await (await fetch('/api/rpc',{
1+
async function rpc(url: string,proxy: boolean,method: string,params?: object,authorization?: string): Promise<object>{
2+
const input: URL = new URL(proxy?'/api/rpc':url,window.location.href);
3+
if(proxy){
4+
input.searchParams.set('url',url);
5+
}
6+
7+
const headers: Headers = new Headers({
8+
'Content-Type': 'application/json',
9+
});
10+
if(authorization){
11+
headers.append('Authorization',authorization);
12+
}
13+
14+
return await (await fetch(input,{
415
method: 'POST',
5-
headers: {
6-
'Content-Type': 'application/json',
7-
},
16+
headers: headers,
817
body: JSON.stringify({
918
jsonrpc: '2.0',
1019
method: method,

src/NotFound.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function NotFound() {
2-
return <></>;
2+
return (
3+
<div style={{fontSize:'100px',textAlign:'center'}}>
4+
404
5+
</div>
6+
);
37
}
48

59
export default NotFound;

src/SettingsPage.tsx

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1+
import {useEffect, useState} from "react";
2+
13
import LBRY from "./LBRY";
24

35
function SettingsPage(){
4-
LBRY.rpc('settings_get').then(json => {
5-
if(json.error){
6-
document.getElementById('settings').innerHTML = json.error.message;
7-
return;
8-
}
9-
document.getElementById('settings').innerHTML = '';
10-
json.result.items.forEach((item: object): void => {
11-
document.getElementById('settings').innerHTML += '<div style="border:1px solid red;display:inline-block;max-width:200px;"><img alt="Thumbnail" src="'+(item.value?.thumbnail?.url || item.reposted_claim?.value?.thumbnail?.url)+'" style="height:100px;"><br>'+(item.value?.title || item.reposted_claim?.value?.title)+'</div>';
6+
const [settings,setSettings]: [object,(value: object) => object] = useState();
7+
8+
useEffect((): void => {
9+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','settings_get').then((json: object): void => {
10+
setSettings(json.result || json.error?.message || 'Unknown error');
1211
});
13-
});
12+
},[]);
1413

1514
return (
1615
<>
1716
<h1>Settings</h1>
18-
<div id="settings"></div>
17+
<div id="settings">
18+
{(typeof settings==='string')?(
19+
<>
20+
<b>Error:</b><br/>
21+
<span>{settings}</span>
22+
</>
23+
):null}
24+
{(typeof settings==='object')?(
25+
<>
26+
{(settings==null)?(
27+
<span>Loading...</span>
28+
):(
29+
<>
30+
{Object.keys(settings).map((setting: string, i: number) => (
31+
<div key={i} style={{border:'1px solid red',margin:'8px 0',padding:'16px'}}>
32+
<b>{setting}</b><br/>
33+
<span>{JSON.stringify(settings[setting])}</span>
34+
</div>
35+
))}
36+
</>
37+
)}
38+
</>
39+
):null}
40+
</div>
1941
</>
2042
);
2143
}

src/WalletPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import LBRY from "./LBRY";
22

33
function WalletPage(){
4-
LBRY.rpc('wallet_balance').then(json => {
4+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','wallet_balance').then(json => {
55
if(json.error){
66
document.getElementById('wallet').innerHTML = json.error.message;
77
return;
88
}
99
});
1010

11-
LBRY.rpc('txo_list').then(json => {
11+
LBRY.rpc(import.meta.env.VITE_DAEMON_DEFAULT,import.meta.env.VITE_DAEMON_PROXY==='true','txo_list').then(json => {
1212
if(json.error){
1313
document.getElementById('transactions').innerHTML = json.error.message;
1414
return;

vite.config.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
import { defineConfig } from 'vite';
1+
import type EventEmitter from "events";
2+
import type ClientRequest from "http";
3+
import {defineConfig} from 'vite';
4+
import type ProxyOptions from 'vite';
25
import react from '@vitejs/plugin-react';
36

47
export default defineConfig({
58
base: './',
69
plugins: [react()],
10+
server: {
11+
proxy: {
12+
'/api/rpc': {
13+
configure: (proxy: EventEmitter, options: ProxyOptions): void => {
14+
options.rewrite = (path: string): string => {
15+
const proxyURL: URL = new URL(path,'file:');
16+
const url: URL = new URL(proxyURL.searchParams.get('url'));
17+
18+
proxy.on('proxyReq',(proxyReq: ClientRequest): void => {
19+
proxyReq.removeHeader('Origin');
20+
});
21+
22+
options.target = url.origin;
23+
return url.pathname + url.search;
24+
};
25+
},
26+
},
27+
},
28+
},
729
});

0 commit comments

Comments
 (0)