Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTP vs HTTPS issue in clients #1040

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/cli/src/lib/platform/common.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { MiddlewareClient } from "@scramjet/middleware-api-client";
import { sessionConfig, profileManager, ProfileConfig } from "../config";
import { displayError, displayMessage } from "../output";
import { ClientUtils } from "@scramjet/client-utils";
import { ClientUtils, ClientUtilsCustomAgent } from "@scramjet/client-utils";
import { configEnv, isProductionEnv } from "../../types";
import { Command } from "commander";
import http from "http";
import https from "https";

/**
* Returns host client for host pointed by command options.
Expand All @@ -17,7 +19,11 @@ export const getMiddlewareClient = (): MiddlewareClient => {
throw new Error("Middleware API URL is not specified");
}

const middlewareClient = new MiddlewareClient(middlewareApiUrl);
const agent = middlewareApiUrl.startsWith("https") ? https.Agent : http.Agent;
const middlewareClient = new MiddlewareClient(
middlewareApiUrl,
new ClientUtilsCustomAgent(middlewareApiUrl, new agent({ keepAlive: true }))
);

if (debug) {
middlewareClient.client.addLogger({
Expand Down
2 changes: 1 addition & 1 deletion packages/client-utils/src/client-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class ClientUtilsBase implements HttpClient {
const fetchInit: RequestInit & { agent?: HTTPAgent } = { signal: abortController.signal, ...init };

fetchInit.headers = { ...ClientUtilsBase.headers, ...fetchInit.headers };
fetchInit.agent ||= new HTTPAgent();
fetchInit.agent ||= this.agent;

options.throwOnErrorHttpCode ??= true;

Expand Down
4 changes: 2 additions & 2 deletions packages/client-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ClientUtils extends ClientUtilsBase implements HttpClient {
export class ClientUtilsCustomAgent extends ClientUtilsBase implements HttpClient {
constructor(
apiBase: string,
agent: http.Agent
agent: http.Agent | https.Agent
) {
super(
apiBase,
Expand All @@ -50,7 +50,7 @@ export class ClientUtilsCustomAgent extends ClientUtilsBase implements HttpClien
},
normalizeUrl
);

this.agent = agent;
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/middleware-api-client/src/middleware-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import { ClientProvider, ClientUtils } from "@scramjet/client-utils";
import { ClientProvider, ClientUtils, ClientUtilsCustomAgent } from "@scramjet/client-utils";
import { ManagerClient } from "@scramjet/api-client";
import { MWRestAPI, MMRestAPI } from "@scramjet/types";

Expand All @@ -25,7 +25,10 @@ export class MiddlewareClient implements ClientProvider {
* @returns {ManagerClient} ManagerClient for space management
*/
getManagerClient(id: string, mutliManagerApiBase = "/api/v1"): ManagerClient {
return new ManagerClient(`${this.apiBase}/space/${id}${mutliManagerApiBase}`);
return new ManagerClient(
`${this.apiBase}/space/${id}${mutliManagerApiBase}`,
new ClientUtilsCustomAgent(`${this.apiBase}/space/${id}${mutliManagerApiBase}`, this.client.agent)
);
}

/**
Expand Down
Loading