Skip to content

Commit

Permalink
Change destroySession to string. Check if shared library exists. Dest…
Browse files Browse the repository at this point in the history
…roy session immediately to prevent memory leaks when an error is thrown.
  • Loading branch information
AzureFlow committed Sep 9, 2023
1 parent a7212d8 commit 57dbf80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/http_client/TLSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "./types.js";
import { execSync } from "child_process";
import createDebugMessages from "debug";
import { existsSync } from "fs";

const debug = createDebugMessages("tls-client:client");
const __dirname = dirname(fileURLToPath(import.meta.url));
Expand All @@ -38,12 +39,18 @@ export default class TLSClient implements TLSClientInstance {
});
}

destroySession(payload: TLSClientReleaseSessionPayload): TLSClientReleaseSessionResponse {
destroySession(sessionId: string): TLSClientReleaseSessionResponse {
const payload: TLSClientReleaseSessionPayload = {
sessionId,
};
const resp = this.wrapper.destroySession(JSON.stringify(payload));
return JSON.parse(resp) as TLSClientReleaseSessionResponse;
}

async destroySessionAsync(payload: TLSClientReleaseSessionPayload): Promise<TLSClientReleaseSessionResponse> {
async destroySessionAsync(sessionId: string): Promise<TLSClientReleaseSessionResponse> {
const payload: TLSClientReleaseSessionPayload = {
sessionId,
};
return new Promise((resolve) => {
this.wrapper.destroySession.async(JSON.stringify(payload), (error: Error, response: string) => {
const clientResponse: TLSClientReleaseSessionResponse = JSON.parse(response);
Expand Down Expand Up @@ -98,9 +105,13 @@ const createWrapper = (libVersion: string): LibraryObject<never> => {
throw new Error("Invalid platform!");
}

debug(`Loading shared library: "${sharedLibraryFilename}"`);
const libFile = join(__dirname, "../../lib", sharedLibraryFilename);
debug(`Loading shared library: "${libFile}"`);
if (!existsSync(libFile)) {
throw new Error("Shared library not found!");
}

return Library(join(__dirname, "../../lib", sharedLibraryFilename), {
return Library(libFile, {
request: ["string", ["string"]],
getCookiesFromSession: ["string", ["string"]],
addCookiesToSession: ["string", ["string"]],
Expand Down
8 changes: 3 additions & 5 deletions src/http_client/fetch_compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export default async function fetch(url: string, init?: FetchInit): Promise<Resp
const resp = await tlsClient.requestAsync(payload);
debug("response: %O", resp);

// Free request
tlsClient.destroySession(sessionId);

// Return result
if (resp.status === 0) {
if (resp.body.includes("No connection could be made because the target machine actively refused it.")) {
Expand All @@ -85,11 +88,6 @@ export default async function fetch(url: string, init?: FetchInit): Promise<Resp
}
}

// Free request
tlsClient.destroySession({
sessionId: sessionId,
});

return new Response(resp.body, {
headers: headers,
status: resp.status,
Expand Down
4 changes: 2 additions & 2 deletions src/http_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export interface TLSClientInstance {
getCookiesFromSessionAsync: (
payload: TLSClientFetchCookiesForSessionRequestPayload,
) => Promise<TLSClientFetchCookiesForSessionResponse>;
destroySession: (payload: TLSClientReleaseSessionPayload) => TLSClientReleaseSessionResponse;
destroySessionAsync: (payload: TLSClientReleaseSessionPayload) => Promise<TLSClientReleaseSessionResponse>;
destroySession: (sessionId: string) => TLSClientReleaseSessionResponse;
destroySessionAsync: (sessionId: string) => Promise<TLSClientReleaseSessionResponse>;
}

export interface TLSClientRequestPayload {
Expand Down

0 comments on commit 57dbf80

Please sign in to comment.