Skip to content

Commit

Permalink
fix(node): config properties could be undefined
Browse files Browse the repository at this point in the history
Object.assign doesn't work with nested objects, so various things can be undefined
  • Loading branch information
micahriggan committed Jan 2, 2019
1 parent 8aeb2ba commit 6fd40d0
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 33 deletions.
21 changes: 6 additions & 15 deletions packages/bitcore-node/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { homedir, cpus } from 'os';
import parseArgv from './utils/parseArgv';
import { ConfigType } from "./types/Config";
import { ConfigType } from './types/Config';
let program = parseArgv([], ['config']);

function findConfig(): ConfigType | undefined {
Expand Down Expand Up @@ -60,7 +60,6 @@ const Config = function(): ConfigType {
chains: {},
services: {
api: {
enabled: true,
rateLimiter: {
whitelist: ['::ffff:127.0.0.1']
},
Expand All @@ -69,23 +68,15 @@ const Config = function(): ConfigType {
allowUnauthenticatedCalls: false
}
},
event: {
enabled: true
},
p2p: {
enabled: true
},
socket: {
enabled: true
},
storage: {
enabled: true
}
event: {},
p2p: {},
socket: {},
storage: {}
}
};

let foundConfig = findConfig();
Object.assign(config, foundConfig, {});
Object.assign(config.services, foundConfig, {});
if (!Object.keys(config.chains).length) {
Object.assign(config.chains, {
BTC: {
Expand Down
3 changes: 2 additions & 1 deletion packages/bitcore-node/src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ export class TransactionModel extends BaseModel<ITransaction> {
}
}

if (initialSyncComplete || Config.for('api').wallets.allowCreationBeforeCompleteSync) {
const walletConfig = Config.for('api').wallets;
if (initialSyncComplete || (walletConfig && walletConfig.allowCreationBeforeCompleteSync)) {
let mintOpsAddresses = {};
for (const mintOp of mintOps) {
mintOpsAddresses[mintOp.updateOne.update.$set.address] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ export class InternalStateProvider implements CSP.IChainStateService {
const state = await StateStorage.collection.findOne({});
const initialSyncComplete =
state && state.initialSyncComplete && state.initialSyncComplete.includes(`${chain}:${network}`);
if (!initialSyncComplete && !Config.for('api').wallets.allowCreationBeforeCompleteSync) {
const walletConfig = Config.for('api').wallets;
const canCreate = walletConfig && walletConfig.allowCreationBeforeCompleteSync;
if (!initialSyncComplete && !canCreate) {
throw 'Wallet creation not permitted before intitial sync is complete';
}
const wallet: IWallet = {
Expand Down
5 changes: 3 additions & 2 deletions packages/bitcore-node/src/routes/api/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from "../../services/config";
import { Config } from '../../services/config';
import { Request, Response, Router } from 'express';
import { ChainNetwork } from '../../types/ChainNetwork';
import { IWallet } from '../../models/wallet';
Expand Down Expand Up @@ -53,7 +53,8 @@ const authenticate: RequestHandler = async (req: PreAuthRequest, res: Response,
return res.status(404).send('Wallet not found');
}
Object.assign(req, { wallet });
if(Config.for('api').wallets.allowUnauthenticatedCalls) {
const walletConfig = Config.for('api').wallets;
if (walletConfig && walletConfig.allowUnauthenticatedCalls) {
return next();
}
try {
Expand Down
5 changes: 3 additions & 2 deletions packages/bitcore-node/src/routes/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logger from '../logger';
import * as express from 'express';
import { RateLimitStorage } from '../models/rateLimit';
import { Config } from "../services/config";
import { Config } from '../services/config';

type TimedRequest = {
startTime?: Date;
Expand Down Expand Up @@ -71,7 +71,8 @@ export function RateLimiter(method: string, perSecond: number, perMinute: number
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const identifier = req.header('CF-Connecting-IP') || req.socket.remoteAddress || '';
if (Config.for('api').rateLimiter.whitelist.includes(identifier)) {
const rateLimiter = Config.for('api').rateLimiter;
if (rateLimiter && rateLimiter.whitelist.includes(identifier)) {
return next();
}
let [perSecondResult, perMinuteResult, perHourResult] = await RateLimitStorage.incrementAndCheck(
Expand Down
10 changes: 7 additions & 3 deletions packages/bitcore-node/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConfigType } from '../types/Config';
import config from '../config';
import { ChainNetwork } from '../types/ChainNetwork';
import { valueOrDefault } from '../utils/check';

type RecursivePartial<T> = { [P in keyof T]?: RecursivePartial<T[P]> };
type ServiceName = keyof ConfigType['services'];
Expand Down Expand Up @@ -39,16 +40,19 @@ export class ConfigService {
return chainNetworks;
}

public chainConfig({chain, network}: ChainNetwork) {
public chainConfig({ chain, network }: ChainNetwork) {
return this.get().chains[chain][network];
}

public for<T extends keyof ConfigType['services']>(service: T): ConfigType['services'][T] {
return this.get().services[service];
return this.get().services[service] || {};
}

public isEnabled(service: ServiceName) {
return this.for(service) && this.for(service).enabled;
const serviceConfig = this.for(service);
const isDefined = x => x !== undefined;
const disabled = isDefined(serviceConfig) ? valueOrDefault(serviceConfig.disabled, false) : false;
return !disabled;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/services/p2p.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class P2pWorker {
}

async start(config: ConfigType) {
if (!config.services.p2p.enabled) {
if (!config.services.p2p.disabled) {
return;
}
logger.debug(`Started worker for chain ${this.chain}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/services/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class SocketService {
}

start({ server, config = this.configService.get() }: { server: http.Server; config: ConfigType }) {
if (!config.services.socket.enabled) {
if (!config.services.socket.disabled) {
return;
}
logger.info('Starting Socket Service');
Expand Down
14 changes: 7 additions & 7 deletions packages/bitcore-node/src/types/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ export interface ConfigType {
};
services: {
api: {
enabled: boolean;
rateLimiter: {
disabled?: boolean;
rateLimiter?: {
whitelist: [string];
};
wallets: {
wallets?: {
allowCreationBeforeCompleteSync?: boolean;
allowUnauthenticatedCalls?: boolean;
};
};
event: {
enabled: boolean;
disabled?: boolean;
};
p2p: {
enabled: boolean;
disabled?: boolean;
};
socket: {
enabled: boolean;
disabled?: boolean;
};
storage: {
enabled: boolean;
disabled?: boolean;
};
};
}

0 comments on commit 6fd40d0

Please sign in to comment.