-
Notifications
You must be signed in to change notification settings - Fork 14
/
serve.js
47 lines (43 loc) · 1.47 KB
/
serve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require('dotenv/config');
const express = require('express');
const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const chalk = require('chalk');
const appRootPath = require('app-root-path');
const { ethers } = require('ethers');
const mocks = path.resolve(`${appRootPath}`, '__mocks__');
const serveTranspiledFile = wallet => async (req, res, next) => {
try {
const { params } = req;
const { wormhole } = params;
const file = path.resolve(mocks, wormhole);
if (!fs.existsSync(file)) {
throw new Error(`Unable to find ${file}`);
}
const src = child_process.execSync(
`npx babel --presets=@babel/preset-env,@babel/preset-react ${wormhole}`,
{ cwd: `${mocks}` },
).toString();
const signature = await wallet.signMessage(src);
return res
.status(200)
.set({ 'X-Csrf-Token': signature })
.send(src);
} catch (e) {
return next(e);
}
};
(async () => {
const { PORT, SIGNER_MNEMONIC } = process.env;
const wallet = await ethers.Wallet.fromMnemonic(SIGNER_MNEMONIC);
await new Promise(
resolve => express()
.get('/__mocks__/:wormhole', serveTranspiledFile(wallet))
.listen(PORT, resolve),
);
console.clear();
console.log(chalk.white.bold`🕳️ 🐛 Wormholes are being served!`);
console.log('Note, request latency will be increased since files will be lazily recompiled on every request.');
console.log(chalk.green.bold`Port: ${PORT}`);
})();