Skip to content

Commit 3da5407

Browse files
authored
Refactor IPFS proxy (#28)
* DAPPs Skeleton loader * Fetch CID/path directly from IPFS server and serve buffer to the client without streaming
1 parent f648daf commit 3da5407

File tree

3 files changed

+26
-42
lines changed

3 files changed

+26
-42
lines changed

src/main/main.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { fetchPeers } from './peers';
3636
import { SYNTHETIX_NODE_APP_CONFIG } from '../const';
3737
import * as settings from './settings';
3838
import http from 'http';
39-
import { proxy } from './proxy';
39+
import fetch from 'node-fetch';
4040

4141
logger.transports.file.level = 'info';
4242

@@ -374,13 +374,27 @@ waitForIpfs().then(debouncedDappsUpdater).catch(logger.error);
374374
ipcMain.handle('peers', async () => fetchPeers());
375375

376376
http
377-
.createServer((req, res) => {
377+
.createServer(async (req, res) => {
378378
const id = `${req.headers.host}`.replace('.localhost:8888', '');
379379
const dapp = DAPPS.find((dapp) => dapp.id === id);
380-
if (dapp && dapp.url) {
381-
req.headers.host = dapp.url;
382-
proxy({ host: '127.0.0.1', port: 8080 }, req, res);
383-
return;
380+
if (dapp && dapp.qm) {
381+
try {
382+
const response = await fetch(`http://127.0.0.1:8080/ipfs/${dapp.qm}${req.url}`);
383+
if (response.status !== 404) {
384+
// @ts-ignore
385+
res.writeHead(response.status, {
386+
'Content-Length': response.headers.get('content-length'),
387+
'Content-Type': response.headers.get('content-type'),
388+
});
389+
res.end(await response.buffer());
390+
return;
391+
}
392+
} catch (e: any) {
393+
logger.error(e);
394+
res.writeHead(500);
395+
res.end(e.message);
396+
return;
397+
}
384398
}
385399
res.writeHead(404);
386400
res.end('Not found');

src/main/proxy.ts

-32
This file was deleted.

src/renderer/DApps/Dapps.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box, Button, Heading, Image, Link, Spinner, Flex } from '@chakra-ui/react';
1+
import { Box, Button, Heading, Image, Link, Spinner, Flex, Skeleton } from '@chakra-ui/react';
22
import { ExternalLinkIcon } from '@chakra-ui/icons';
33
import { useDapps } from './useDapps';
44
import { DappType } from '../../config';
@@ -31,9 +31,11 @@ export function Dapps() {
3131
Available DApps:
3232
</Heading>
3333
<Flex direction="row" gap={2} justifyContent="start" mb="2" flexWrap="wrap">
34-
{dapps.map((dapp: DappType) => (
35-
<DappButton key={dapp.id} dapp={dapp} />
36-
))}
34+
{dapps.length > 0 ? (
35+
dapps.map((dapp: DappType) => <DappButton key={dapp.id} dapp={dapp} />)
36+
) : (
37+
<Skeleton w="full" height={8} />
38+
)}
3739
</Flex>
3840
</Box>
3941
</Box>

0 commit comments

Comments
 (0)