Skip to content

Commit

Permalink
defSPass agent to manager client, set agent in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
alicja-gruzdz authored and karoltylenda committed Feb 13, 2024
1 parent 019e9f2 commit cdf3176
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
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

0 comments on commit cdf3176

Please sign in to comment.