From a10790c6cb52fd92aac170bffdbfcda50ded7b11 Mon Sep 17 00:00:00 2001 From: Aaron Sutula Date: Tue, 9 Jun 2020 22:36:31 -0500 Subject: [PATCH] fix: config resolution Signed-off-by: Aaron Sutula --- examples/node/.env.example | 5 ++- examples/node/package-lock.json | 6 +-- examples/node/package.json | 2 +- examples/node/src/app.ts | 6 +-- examples/node/src/util/env.ts | 4 ++ package-lock.json | 8 +++- package.json | 7 +++- src/ffs/index.spec.ts | 10 ++--- src/ffs/index.ts | 72 ++++++++++++++++----------------- src/health/index.spec.ts | 4 +- src/health/index.ts | 4 +- src/index.spec.ts | 1 - src/index.ts | 17 ++++++-- src/miners/index.spec.ts | 2 +- src/miners/index.ts | 4 +- src/net/index.spec.ts | 6 +-- src/net/index.ts | 20 ++++----- 17 files changed, 101 insertions(+), 77 deletions(-) diff --git a/examples/node/.env.example b/examples/node/.env.example index eb3db0d6..7e1ec3cb 100644 --- a/examples/node/.env.example +++ b/examples/node/.env.example @@ -10,4 +10,7 @@ GITHUB_CLIENT_ID=f47f0f65206fc6f12d6c5 GITHUB_CLIENT_SECRET=3csdf87yf60ab5141dasfgsegvsfc3eaa3d12865270 # Application Port - express server listens on this port (default 3000). -PORT=3000 +EXPRESS_PORT=3000 + +# Powergate host (default is http://0.0.0.0:6002) +POW_HOST=http://my.powergate.io:6002 diff --git a/examples/node/package-lock.json b/examples/node/package-lock.json index d28fe582..c1945bf6 100644 --- a/examples/node/package-lock.json +++ b/examples/node/package-lock.json @@ -1487,9 +1487,9 @@ } }, "@textile/powergate-client": { - "version": "0.1.0-beta.7", - "resolved": "https://registry.npmjs.org/@textile/powergate-client/-/powergate-client-0.1.0-beta.7.tgz", - "integrity": "sha512-FDRkjWPhEHDd+ZFZKnS66fT/l+8GGzzrRluZeZFCtSzsSlICa0Ayti0g52MsYSs9sdwyIVwVViiW3qxq+WKyyw==", + "version": "0.1.0-beta.8", + "resolved": "https://registry.npmjs.org/@textile/powergate-client/-/powergate-client-0.1.0-beta.8.tgz", + "integrity": "sha512-L3OOWLgZJNfyyvvdP8HH0TzGw07KJQ/gqwspPFLEge1kRfFlEIgL9Vrnv9+D4915zJIbLUjg92gqrcte5fCILQ==", "requires": { "@improbable-eng/grpc-web-node-http-transport": "^0.12.0", "@textile/grpc-powergate-client": "0.0.1-beta.8" diff --git a/examples/node/package.json b/examples/node/package.json index cff57692..463eb961 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -27,7 +27,7 @@ "watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\"" }, "dependencies": { - "@textile/powergate-client": "0.1.0-beta.7", + "@textile/powergate-client": "0.1.0-beta.8", "dotenv": "^8.2.0", "errorhandler": "^1.5.1", "express": "^4.17.1", diff --git a/examples/node/src/app.ts b/examples/node/src/app.ts index b6e0263d..b2f30fe1 100644 --- a/examples/node/src/app.ts +++ b/examples/node/src/app.ts @@ -5,16 +5,16 @@ import passport from "passport" import path from "path" import * as passportConfig from "./config/passport" import { save, User } from "./models/user" -import { SESSION_SECRET } from "./util/env" +import { EXPRESS_PORT, POW_HOST, SESSION_SECRET } from "./util/env" // Create the Powergate client -const pow = createPow() +const pow = createPow({ host: POW_HOST }) // Create Express server const app = express() // Express configuration -app.set("port", process.env.PORT || 3000) +app.set("port", EXPRESS_PORT || 3000) app.use(express.static(path.join(__dirname, "public"), { maxAge: 31557600000 })) app.set("views", path.join(__dirname, "../views")) app.set("view engine", "pug") diff --git a/examples/node/src/util/env.ts b/examples/node/src/util/env.ts index 4f6e0a6c..af4f40f7 100644 --- a/examples/node/src/util/env.ts +++ b/examples/node/src/util/env.ts @@ -13,6 +13,10 @@ if (fs.existsSync(".env")) { export const ENV = process.env.NODE_ENV export const PROD = ENV === "production" // Anything else is treated as 'dev' +export const EXPRESS_PORT = process.env["EXPRESS_PORT"] + +export const POW_HOST = process.env["POW_HOST"] + export const SESSION_SECRET = mustResolve("SESSION_SECRET") export const GITHUB_CLIENT_ID = mustResolve("GITHUB_CLIENT_ID") diff --git a/package-lock.json b/package-lock.json index 7e662ddf..42ee01c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@textile/powergate-client", - "version": "0.1.0-beta.7", + "version": "0.1.0-beta.8", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -5562,6 +5562,12 @@ "fast-diff": "^1.1.2" } }, + "prettier-plugin-organize-imports": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/prettier-plugin-organize-imports/-/prettier-plugin-organize-imports-1.0.4.tgz", + "integrity": "sha512-qwfiitOICk7g26MLqf8pDmeuSFFjeFJrhB5YSLIaooyYXeG2+10fyIpVQr/HwNAqXlbVVJlxcwuPNm7gRsSA9Q==", + "dev": true + }, "process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index 8cbac88d..5cd0ff29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@textile/powergate-client", - "version": "0.1.0-beta.7", + "version": "0.1.0-beta.8", "description": "Client for Textile's Powergate", "main": "dist/index", "types": "dist/index", @@ -16,7 +16,9 @@ "test:node": "TS_NODE_FILES=true mocha --exit", "test:browser": "polendina --timeout 3000 --cleanup src/**/*.spec.ts --webpack-config ./webpack.test.js", "lint": "eslint --fix --ext .js,.ts src/**/*", - "docs": "typedoc" + "docs": "typedoc", + "devnet:up": "docker-compose -p devnet -f docker-compose-devnet.yml up --build -V", + "devnet:down": "docker-compose -p devnet -f docker-compose-devnet.yml down" }, "keywords": [ "typescript", @@ -47,6 +49,7 @@ "mocha": "^7.1.1", "polendina": "^1.0.0", "prettier": "^2.0.2", + "prettier-plugin-organize-imports": "^1.0.4", "rimraf": "^3.0.2", "touch": "^3.1.0", "ts-loader": "^7.0.4", diff --git a/src/ffs/index.spec.ts b/src/ffs/index.spec.ts index 74ba7306..b6e96bfb 100644 --- a/src/ffs/index.spec.ts +++ b/src/ffs/index.spec.ts @@ -1,13 +1,13 @@ -import fs from "fs" -import { expect } from "chai" import { AddrInfo, - DefaultConfig, CidConfig, + DefaultConfig, JobStatus, } from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb" -import { useToken, getTransport, host } from "../util" -import { createFFS, withOverrideConfig, withConfig, withHistory } from "." +import { expect } from "chai" +import fs from "fs" +import { createFFS, withConfig, withHistory, withOverrideConfig } from "." +import { getTransport, host, useToken } from "../util" describe("ffs", () => { const { getMeta, setToken } = useToken("") diff --git a/src/ffs/index.ts b/src/ffs/index.ts index 49ab154f..4e182e74 100644 --- a/src/ffs/index.ts +++ b/src/ffs/index.ts @@ -1,61 +1,61 @@ +import { grpc } from "@improbable-eng/grpc-web" import { - CreateRequest, - CreateResponse, - ListAPIRequest, - ListAPIResponse, - IDRequest, - IDResponse, AddrsRequest, AddrsResponse, - DefaultConfigRequest, - DefaultConfigResponse, - NewAddrRequest, - NewAddrResponse, AddToHotRequest, AddToHotResponse, - GetDefaultCidConfigRequest, - GetDefaultCidConfigResponse, - GetCidConfigRequest, - GetCidConfigResponse, - DefaultConfig, - HotConfig, - IpfsConfig, + CidConfig, + CloseRequest, ColdConfig, + CreatePayChannelRequest, + CreatePayChannelResponse, + CreateRequest, + CreateResponse, + DefaultConfig, + DefaultConfigRequest, + DefaultConfigResponse, FilConfig, FilRenew, - SetDefaultConfigRequest, + GetCidConfigRequest, + GetCidConfigResponse, + GetDefaultCidConfigRequest, + GetDefaultCidConfigResponse, + GetRequest, + HotConfig, + IDRequest, + IDResponse, InfoRequest, InfoResponse, + IpfsConfig, + Job, + ListAPIRequest, + ListAPIResponse, + ListPayChannelsRequest, + ListPayChannelsResponse, + LogEntry, + NewAddrRequest, + NewAddrResponse, PushConfigRequest, - CidConfig, PushConfigResponse, - ShowRequest, - ShowResponse, + RedeemPayChannelRequest, + RemoveRequest, ReplaceRequest, ReplaceResponse, - RemoveRequest, SendFilRequest, - CloseRequest, - WatchJobsRequest, - Job, - LogEntry, - WatchLogsRequest, - GetRequest, + SetDefaultConfigRequest, ShowAllRequest, ShowAllResponse, - ListPayChannelsRequest, - ListPayChannelsResponse, - CreatePayChannelRequest, - CreatePayChannelResponse, - RedeemPayChannelRequest, + ShowRequest, + ShowResponse, + WatchJobsRequest, + WatchLogsRequest, } from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb" import { - RPCServiceClient, RPCService, + RPCServiceClient, } from "@textile/grpc-powergate-client/dist/ffs/rpc/rpc_pb_service" -import { grpc } from "@improbable-eng/grpc-web" -import { promise } from "../util" import { Config } from "../types" +import { promise } from "../util" type PushConfigOption = (req: PushConfigRequest) => void diff --git a/src/health/index.spec.ts b/src/health/index.spec.ts index 192c6f7a..04ac38dd 100644 --- a/src/health/index.spec.ts +++ b/src/health/index.spec.ts @@ -1,7 +1,7 @@ -import { expect } from "chai" import { Status } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb" -import { getTransport, host } from "../util" +import { expect } from "chai" import { createHealth } from "." +import { getTransport, host } from "../util" describe("health", () => { const health = createHealth({ host, transport: getTransport() }) diff --git a/src/health/index.ts b/src/health/index.ts index 9885a32c..41331d18 100644 --- a/src/health/index.ts +++ b/src/health/index.ts @@ -1,7 +1,7 @@ -import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb_service" import { CheckRequest, CheckResponse } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb" -import { promise } from "../util" +import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/health/rpc/rpc_pb_service" import { Config } from "../types" +import { promise } from "../util" /** * Creates the Health API client diff --git a/src/index.spec.ts b/src/index.spec.ts index 87dc8eac..461b5c94 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -2,7 +2,6 @@ import { expect } from "chai" import cp from "child_process" import path from "path" import wait from "wait-on" - import { createPow } from "." import { host } from "./util" diff --git a/src/index.ts b/src/index.ts index f9c9b39f..2dfb6798 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ -import { createHealth } from "./health" -import { createNet } from "./net" import { createFFS } from "./ffs" +import { createHealth } from "./health" import { createMiners } from "./miners" -import { useToken, getTransport, host } from "./util" +import { createNet } from "./net" import { Config } from "./types" +import { getTransport, host, useToken } from "./util" const defaultConfig: Config = { host, @@ -16,7 +16,7 @@ const defaultConfig: Config = { * @returns A Powergate client API */ export const createPow = (config?: Partial) => { - const c = { ...defaultConfig, ...config } + const c = { ...defaultConfig, ...removeEmpty(config) } const { getMeta, setToken } = useToken(c.authToken) @@ -48,3 +48,12 @@ export const createPow = (config?: Partial) => { miners: createMiners(c), } } + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const removeEmpty = (obj: any) => { + Object.keys(obj).forEach((key) => { + if (obj[key] && typeof obj[key] === "object") removeEmpty(obj[key]) + else if (obj[key] === undefined) delete obj[key] + }) + return obj +} diff --git a/src/miners/index.spec.ts b/src/miners/index.spec.ts index 36a5cf71..efd0ad06 100644 --- a/src/miners/index.spec.ts +++ b/src/miners/index.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai" -import { getTransport, host } from "../util" import { createMiners } from "." +import { getTransport, host } from "../util" describe("miners", () => { const miners = createMiners({ host, transport: getTransport() }) diff --git a/src/miners/index.ts b/src/miners/index.ts index 343a87d7..461b0149 100644 --- a/src/miners/index.ts +++ b/src/miners/index.ts @@ -1,7 +1,7 @@ -import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb_service" import { GetRequest, GetResponse } from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb" -import { promise } from "../util" +import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/index/miner/rpc/rpc_pb_service" import { Config } from "../types" +import { promise } from "../util" /** * Creates the Miners API client diff --git a/src/net/index.spec.ts b/src/net/index.spec.ts index 4e271547..9794dc99 100644 --- a/src/net/index.spec.ts +++ b/src/net/index.spec.ts @@ -1,7 +1,7 @@ -import { expect, assert } from "chai" -import { PeersResponse, Connectedness } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb" -import { getTransport, host } from "../util" +import { Connectedness, PeersResponse } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb" +import { assert, expect } from "chai" import { createNet } from "." +import { getTransport, host } from "../util" describe("net", () => { const net = createNet({ host, transport: getTransport() }) diff --git a/src/net/index.ts b/src/net/index.ts index 56d2ae55..0481f226 100644 --- a/src/net/index.ts +++ b/src/net/index.ts @@ -1,19 +1,19 @@ -import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb_service" import { - PeersRequest, - PeersResponse, - ListenAddrRequest, - ListenAddrResponse, + ConnectednessRequest, + ConnectednessResponse, + ConnectPeerRequest, + DisconnectPeerRequest, FindPeerRequest, FindPeerResponse, - ConnectPeerRequest, + ListenAddrRequest, + ListenAddrResponse, PeerAddrInfo, - DisconnectPeerRequest, - ConnectednessRequest, - ConnectednessResponse, + PeersRequest, + PeersResponse, } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb" -import { promise } from "../util" +import { RPCServiceClient } from "@textile/grpc-powergate-client/dist/net/rpc/rpc_pb_service" import { Config } from "../types" +import { promise } from "../util" /** * Creates the Net API client