From 8d4b8c8ecb9be952de634a37b23a3bedac4d65df Mon Sep 17 00:00:00 2001 From: mnothman Date: Tue, 23 Sep 2025 17:17:46 -0700 Subject: [PATCH 1/2] Fix: Add support for DAPR_RUNTIME_HOST in Setrtings.getDefaultHost() Problem: getDefaultHttpPort() already respected DAPR_HTTP_PORT however, settings.getDefaultHost() did not use DAPR_RUNTIME_HOST Solution: (closes Custom sidecar port Fixes #646) -Added DAPR_RUNTIME_HOST env variable support to Settings.getDefaultHost() Now users can set both DAPR_RUNTIME_HOST and DAPR_HTTP_PORT to fully control where client connects Tests: Created new dedicated test file for env mutations, so as to not mix up jest.isolateModules and top level Settings import Signed-off-by: mnothman --- .../Client/GRPCClient/GRPCClient.ts | 13 ++-- .../Client/HTTPClient/HTTPClient.ts | 13 ++-- src/utils/Settings.util.ts | 2 +- test/unit/utils/Settings.env.test.ts | 78 +++++++++++++++++++ test/unit/utils/Settings.util.test.ts | 1 + 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 test/unit/utils/Settings.env.test.ts diff --git a/src/implementation/Client/GRPCClient/GRPCClient.ts b/src/implementation/Client/GRPCClient/GRPCClient.ts index a73b51f2..7d17d069 100644 --- a/src/implementation/Client/GRPCClient/GRPCClient.ts +++ b/src/implementation/Client/GRPCClient/GRPCClient.ts @@ -79,18 +79,19 @@ export default class GRPCClient implements IClient { } private generateEndpoint(options: Partial): GrpcEndpoint { - const host = options?.daprHost ?? Settings.getDefaultHost(); - const port = options?.daprPort ?? Settings.getDefaultGrpcPort(); - let uri = `${host}:${port}`; - + // Check for explicit endpoint first (highest priority) if (!(options?.daprHost || options?.daprPort)) { - // If neither host nor port are specified, check the endpoint environment variable. const endpoint = Settings.getDefaultGrpcEndpoint(); if (endpoint != "") { - uri = endpoint; + return new GrpcEndpoint(endpoint); } } + // Use provided options or environment variable defaults + const host = options?.daprHost ?? Settings.getDefaultHost(); + const port = options?.daprPort ?? Settings.getDefaultGrpcPort(); + const uri = `${host}:${port}`; + return new GrpcEndpoint(uri); } diff --git a/src/implementation/Client/HTTPClient/HTTPClient.ts b/src/implementation/Client/HTTPClient/HTTPClient.ts index eb08d83f..fa59b421 100644 --- a/src/implementation/Client/HTTPClient/HTTPClient.ts +++ b/src/implementation/Client/HTTPClient/HTTPClient.ts @@ -75,18 +75,19 @@ export default class HTTPClient implements IClient { } private generateEndpoint(options: Partial): HttpEndpoint { - const host = options?.daprHost ?? Settings.getDefaultHost(); - const port = options?.daprPort ?? Settings.getDefaultHttpPort(); - let uri = `${host}:${port}`; - + // Check for explicit endpoint first (highest priority) if (!(options?.daprHost || options?.daprPort)) { - // If neither host nor port are specified, check the endpoint environment variable. const endpoint = Settings.getDefaultHttpEndpoint(); if (endpoint != "") { - uri = endpoint; + return new HttpEndpoint(endpoint); } } + // Use provided options or environment variable defaults + const host = options?.daprHost ?? Settings.getDefaultHost(); + const port = options?.daprPort ?? Settings.getDefaultHttpPort(); + const uri = `${host}:${port}`; + return new HttpEndpoint(uri); } diff --git a/src/utils/Settings.util.ts b/src/utils/Settings.util.ts index e839199a..cf431b88 100644 --- a/src/utils/Settings.util.ts +++ b/src/utils/Settings.util.ts @@ -57,7 +57,7 @@ export class Settings { } static getDefaultHost(): string { - return Settings.defaultHost; + return process.env.DAPR_RUNTIME_HOST ?? Settings.defaultHost; } static getDefaultHttpPort(): string { diff --git a/test/unit/utils/Settings.env.test.ts b/test/unit/utils/Settings.env.test.ts new file mode 100644 index 00000000..5375b512 --- /dev/null +++ b/test/unit/utils/Settings.env.test.ts @@ -0,0 +1,78 @@ +/* +Copyright 2022 The Dapr Authors +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// No top-level imports - we use jest.isolateModules() to import fresh modules + +describe("Settings - Environment Variable Support", () => { + // Store original env vars to restore later + const originalEnv = { ...process.env }; + + afterEach(() => { + // Restore original env after each test + process.env = { ...originalEnv }; + }); + + describe("getDefaultHost", () => { + it("respects DAPR_RUNTIME_HOST environment variable", () => { + jest.isolateModules(() => { + process.env.DAPR_RUNTIME_HOST = "192.168.1.100"; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultHost()).toEqual("192.168.1.100"); + }); + }); + + it("returns default when DAPR_RUNTIME_HOST is not set", () => { + jest.isolateModules(() => { + delete process.env.DAPR_RUNTIME_HOST; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultHost()).toEqual("127.0.0.1"); + }); + }); + }); + + describe("getDefaultHttpPort", () => { + it("respects DAPR_HTTP_PORT environment variable", () => { + jest.isolateModules(() => { + process.env.DAPR_HTTP_PORT = "3510"; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultHttpPort()).toEqual("3510"); + }); + }); + + it("returns default when DAPR_HTTP_PORT is not set", () => { + jest.isolateModules(() => { + delete process.env.DAPR_HTTP_PORT; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultHttpPort()).toEqual("3500"); + }); + }); + }); + + describe("getDefaultGrpcPort", () => { + it("respects DAPR_GRPC_PORT environment variable", () => { + jest.isolateModules(() => { + process.env.DAPR_GRPC_PORT = "50010"; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultGrpcPort()).toEqual("50010"); + }); + }); + + it("returns default when DAPR_GRPC_PORT is not set", () => { + jest.isolateModules(() => { + delete process.env.DAPR_GRPC_PORT; + const { Settings } = require("../../../src/utils/Settings.util"); + expect(Settings.getDefaultGrpcPort()).toEqual("50001"); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/utils/Settings.util.test.ts b/test/unit/utils/Settings.util.test.ts index e6ae364f..ae8cc180 100644 --- a/test/unit/utils/Settings.util.test.ts +++ b/test/unit/utils/Settings.util.test.ts @@ -23,6 +23,7 @@ describe("Settings", () => { expect(Settings.getDefaultPort(CommunicationProtocolEnum.HTTP)).toEqual(Settings.getDefaultHttpPort()); }); }); + describe("getDefaultAppPort returns the correct default when", () => { it("communication protocol is GRPC", () => { expect(Settings.getDefaultAppPort(CommunicationProtocolEnum.GRPC)).toEqual(Settings.getDefaultGrpcAppPort()); From 41498a6fac4f1a7cb602d7f02edabe9171e67ed8 Mon Sep 17 00:00:00 2001 From: mnothman Date: Tue, 23 Sep 2025 17:35:05 -0700 Subject: [PATCH 2/2] Lint error fix Signed-off-by: mnothman --- test/unit/utils/Settings.env.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/utils/Settings.env.test.ts b/test/unit/utils/Settings.env.test.ts index 5375b512..c7c78f61 100644 --- a/test/unit/utils/Settings.env.test.ts +++ b/test/unit/utils/Settings.env.test.ts @@ -11,6 +11,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +/* eslint-disable @typescript-eslint/no-var-requires */ // No top-level imports - we use jest.isolateModules() to import fresh modules describe("Settings - Environment Variable Support", () => { @@ -75,4 +76,4 @@ describe("Settings - Environment Variable Support", () => { }); }); }); -}); \ No newline at end of file +});