Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
v0.12.0 (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLoneRonin authored Jun 5, 2021
1 parent bbcec1e commit f6d5d37
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ INDICES=["App-Name", "app", "domain", "namespace"]

CACHING=1
CACHE_FOLDER=/gateway/cache
CACHE_OFFSET=0
CACHE_OFFSET=0

MANIFEST_PREFIX=http://localhost:3000
4 changes: 3 additions & 1 deletion .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ INDICES=["App-Name", "app", "domain", "namespace"]

CACHING=1
CACHE_FOLDER=/gateway/cache
CACHE_OFFSET=0
CACHE_OFFSET=0

MANIFEST_PREFIX=https://gateway.amplify.host
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ INDICES=["App-Name", "app", "domain", "namespace"]
CACHING=1
CACHE_FOLDER=/gateway/cache
CACHE_OFFSET=0
MANIFEST_PREFIX=https://gateway.amplify.host
```

Make sure you copy this configuration to `.env`.
Expand Down
2 changes: 2 additions & 0 deletions docs/DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ INDICES=["App-Name", "app", "domain", "namespace"]
CACHING=1
CACHE_FOLDER=/gateway/cache
CACHE_OFFSET=0
MANIFEST_PREFIX=https://gateway.amplify.host
```

Make sure you copy this configuration to `.env`.
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ INDICES=["App-Name", "app", "domain", "namespace"]
CACHING=1
CACHE_FOLDER=/gateway/cache
CACHE_OFFSET=0
MANIFEST_PREFIX=https://gateway.amplify.host
```

Make sure you copy this configuration to `.env`.
Expand Down
3 changes: 1 addition & 2 deletions src/Gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {graphServer} from './graphql/server.graphql';
import {statusRoute} from './route/status.route';
import {syncRoute} from './route/sync.route';
import {proxyRoute} from './route/proxy.route';
import {dataRouteRegex, dataHeadRoute, dataRoute} from './route/data.route';
import {dataRouteRegex, dataRoute} from './route/data.route';
import {peerRoute} from './route/peer.route';
import {koiLogger, koiLogsRoute, koiLogsRawRoute} from './route/koi.route';
import {startSync} from './database/sync.database';
Expand All @@ -32,7 +32,6 @@ export function start() {
app.get('/', statusRoute);
app.get('/status', syncRoute);

app.head(dataRouteRegex, dataHeadRoute);
app.get(dataRouteRegex, dataRoute);

graphServer({introspection: true, playground: true}).applyMiddleware({app, path: '/graphql'});
Expand Down
12 changes: 10 additions & 2 deletions src/database/sync.database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export let SIGKILL: boolean = false;
export let bar: ProgressBar;
export let topHeight = 0;
export let currentHeight = 0;
export let timer = setTimeout(() => {}, 0);

export function configureSyncBar(start: number, end: number) {
bar = new ProgressBar(
Expand Down Expand Up @@ -75,6 +76,12 @@ export async function startSync() {
}

export async function parallelize(height: number) {
clearTimeout(timer);
timer = setTimeout(() => {
log.info('[database] sync timed out, restarting server');
process.exit();
}, 300 * 1000);

currentHeight = height;

if (height >= topHeight) {
Expand All @@ -83,8 +90,9 @@ export async function parallelize(height: number) {
const nodeInfo = await getNodeInfo();
if (nodeInfo.height > topHeight) {
log.info(`[database] updated height from ${topHeight} to ${nodeInfo.height} syncing new blocks`);
topHeight = nodeInfo.height;
}
topHeight = nodeInfo.height;

await parallelize(height);
} else {
const batch = [];
Expand Down Expand Up @@ -149,7 +157,7 @@ export async function storeBlock(height: number, retry: number = 0) {
}
} catch (error) {
if (SIGKILL === false) {
if (retry >= 3) {
if (retry >= 25) {
log.info(`[snapshot] there were problems retrieving ${height}, restarting the server`);
process.exit();
} else {
Expand Down
63 changes: 43 additions & 20 deletions src/route/data.route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import {exists} from 'fs-jetpack';
import {config} from 'dotenv';
import {read, exists} from 'fs-jetpack';
import {Request, Response} from 'express';
import {stringToBip39, stringToHash} from '../utility/bip39.utility';
import {ManifestV1} from '../types/manifest.types';
import {log} from '../utility/log.utility';
import {transaction as getTransaction, tagValue} from '../query/transaction.query';
import {cacheFolder, cacheFile, cacheAnsFile} from '../caching/file.caching';

config();

export const dataRouteRegex = /^\/?([a-zA-Z0-9-_]{43})\/?$|^\/?([a-zA-Z0-9-_]{43})\/(.*)$/i;
export const pathRegex = /^\/?([a-z0-9-_]{43})/i;

export const manifestPrefix = process.env.MANIFEST_PREFIX || 'https://gateway.amplify.host';

export async function dataHeadRoute(req: Request, res: Response) {
const path = req.path.match(pathRegex) || [];
const transaction = path.length > 1 ? path[1] : '';
Expand All @@ -23,36 +28,54 @@ export async function dataHeadRoute(req: Request, res: Response) {
export async function dataRoute(req: Request, res: Response) {
const path = req.path.match(pathRegex) || [];
const transaction = path.length > 1 ? path[1] : '';
const hostname = req.hostname;

if (hostname !== 'localhost' && process.env.MANIFESTS === '1') {
const subdomain = process.env.BIP39 === '1' ? stringToBip39(transaction) : stringToHash(transaction);

if (hostname.indexOf(subdomain) === -1) {
return res.redirect(308, `http://${subdomain}.${hostname}/${transaction}`);
}
}

try {
const metadata = await getTransaction(transaction);
const contentType = tagValue(metadata.tags, 'Content-Type');
const ans102 = tagValue(metadata.tags, 'Bundle-Type') === 'ANS-102';

res.setHeader('content-type', contentType);

if (ans102) {
await cacheAnsFile(transaction);
} else {
await cacheFile(transaction);
}
} catch (error) {

}
if (exists(`${cacheFolder}/${transaction}`)) {
if (contentType === 'application/x.arweave-manifest+json') {
res.setHeader('content-type', 'text/html');

if (exists(`${cacheFolder}/${transaction}`)) {
res.status(200);
res.sendFile(`${cacheFolder}/${transaction}`);
} else {
const manifestFile = read(`${cacheFolder}/${transaction}`) || '{}';
const manifest: ManifestV1 = JSON.parse(manifestFile.toString());

const manifestIndex = manifest.index.path;
const manifestIndexTransaction = manifest.paths[manifestIndex].id;

const cachePaths = Object.keys(manifest.paths).map((key) => cacheFile(manifest.paths[key].id));
await Promise.all(cachePaths);

if (exists(`${cacheFolder}/${manifestIndexTransaction}`)) {
let manifestHtml = read(`${cacheFolder}/${manifestIndexTransaction}`) || '';

Object.keys(manifest.paths).map((key) => {
const id = manifest.paths[key].id;
manifestHtml = manifestHtml.split(key).join(`${manifestPrefix}/${id}`);
});

res.status(200);
res.send(manifestHtml);
} else {
throw new Error('Could not parse manifest html file');
}
} else {
res.setHeader('content-type', contentType);

res.status(200);
res.sendFile(`${cacheFolder}/${transaction}`);
}
}
}
} catch (error) {
log.error(`[route] error generating response for ${transaction}`);
console.error(error);
res.status(500);
res.json({status: 'ERROR', message: 'Could not retrieve transaction'});
}
Expand Down
8 changes: 8 additions & 0 deletions src/types/manifest.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface ManifestV1 {
manifest: string;
version: string;
index: {
path: string;
};
paths: any;
}

0 comments on commit f6d5d37

Please sign in to comment.