make proxy change port if it's already taken

This commit is contained in:
Jonathan Rainville 2019-08-06 14:28:47 -04:00 committed by Iuri Matias
parent 9d3064e3ae
commit 16a13ee705
2 changed files with 13 additions and 8 deletions

View File

@ -26,7 +26,7 @@
},
"main": "./dist/index.js",
"scripts": {
"build": "cross-env BABEL_ENV=node babel src --out-dir dist --root-mode upward --source-maps",
"build": "cross-env BABEL_ENV=node babel src --extensions \".ts\",\".js\" --out-dir dist --root-mode upward --source-maps",
"ci": "npm run qa",
"clean": "npm run reset",
"lint": "npm-run-all lint:*",

View File

@ -1,6 +1,6 @@
import {Embark, Events, Logger} /* supplied by @types/embark in packages/embark-typings */ from "embark";
import {IPC} from "embark-core";
import {AccountParser, dappPath} from "embark-utils";
import {AccountParser, dappPath, findNextPort} from "embark-utils";
import {Proxy} from "./proxy";
const constants = require("embark-core/constants");
@ -12,8 +12,8 @@ export default class ProxyManager {
private rpcProxy: any;
private wsProxy: any;
private readonly host: string;
private readonly rpcPort: number;
private readonly wsPort: number;
private rpcPort: number | undefined;
private wsPort: number | undefined;
private ready: boolean;
constructor(private embark: Embark, _options: any) {
@ -23,8 +23,6 @@ export default class ProxyManager {
this.ready = false;
this.host = "localhost";
this.rpcPort = this.embark.config.blockchainConfig.rpcPort + constants.blockchain.servicePortOnProxy;
this.wsPort = this.embark.config.blockchainConfig.wsPort + constants.blockchain.servicePortOnProxy;
this.events.once("blockchain:started", async () => {
await this.setupProxy();
@ -37,9 +35,11 @@ export default class ProxyManager {
if (!this.embark.config.blockchainConfig.proxy) {
return cb(null, this.embark.config.blockchainConfig.endpoint);
}
// TODO actually check for the wanted connection
// TODO Check if the proxy can support HTTPS, though it probably doesn't matter since it's local
if (this.embark.config.blockchainConfig.wsRPC) {
return cb(null, `ws://${this.host}:${this.wsPort}`);
}
cb(null, `http://${this.host}:${this.rpcPort}`);
// cb(null, `ws://${this.host}:${this.wsPort}`);
});
}
@ -58,6 +58,11 @@ export default class ProxyManager {
if (!this.embark.config.blockchainConfig.proxy) {
return;
}
const port = await findNextPort(this.embark.config.blockchainConfig.rpcPort + constants.blockchain.servicePortOnProxy);
this.rpcPort = port;
this.wsPort = port + 1;
const addresses = AccountParser.parseAccountsConfig(this.embark.config.blockchainConfig.accounts, false, dappPath(), this.logger);
[this.rpcProxy, this.wsProxy] = await Promise.all([