From 9ef96fd959f17824e3ceb655aa1ad19fa04a1a1d Mon Sep 17 00:00:00 2001 From: Grzegorz Godlewski Date: Thu, 10 Oct 2024 10:30:00 +0200 Subject: [PATCH] fix(tcp-proxy): throw an error when users try to use proxy in browser JST-1092 --- package-lock.json | 1 - src/network/tcp-proxy.test.ts | 15 +++++++++++++++ src/network/tcp-proxy.ts | 6 ++++-- 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/network/tcp-proxy.test.ts diff --git a/package-lock.json b/package-lock.json index a5b8c017e..761b98c12 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,6 @@ ], "dependencies": { "@golem-sdk/pino-logger": "^1.1.0", - "@rollup/rollup-win32-x64-msvc": "^4", "async-lock": "^1.4.1", "async-retry": "^1.3.3", "axios": "^1.6.7", diff --git a/src/network/tcp-proxy.test.ts b/src/network/tcp-proxy.test.ts new file mode 100644 index 000000000..44fc54e1f --- /dev/null +++ b/src/network/tcp-proxy.test.ts @@ -0,0 +1,15 @@ +jest.mock("../shared/utils", () => ({ + checkAndThrowUnsupportedInBrowserError: () => { + throw new GolemUserError("Not supported in browser"); + }, +})); + +import { GolemUserError } from "../shared/error/golem-error"; + +import { TcpProxy } from "./tcp-proxy"; + +describe("TCP Proxy in browser", () => { + test("Uses the checkAndThrowUnsupportedInBrowserError util to throw when the function detects browser environment", () => { + expect(() => new TcpProxy("ws://fake.url", "fake-app-key")).toThrow("Not supported in browser"); + }); +}); diff --git a/src/network/tcp-proxy.ts b/src/network/tcp-proxy.ts index 72d18179c..e4ee1fdc0 100644 --- a/src/network/tcp-proxy.ts +++ b/src/network/tcp-proxy.ts @@ -1,7 +1,7 @@ import net from "net"; import { WebSocket } from "ws"; import { EventEmitter } from "eventemitter3"; -import { defaultLogger, Logger } from "../shared/utils"; +import { checkAndThrowUnsupportedInBrowserError, defaultLogger, Logger } from "../shared/utils"; import { Buffer } from "buffer"; export interface TcpProxyEvents { @@ -64,8 +64,10 @@ export class TcpProxy { /** * Additional options of the proxy */ - options: Partial, + options: Partial = {}, ) { + checkAndThrowUnsupportedInBrowserError("TCP Proxy"); + this.heartBeatSec = options.heartBeatSec ?? 10; this.logger = options.logger ? options.logger.child("tcp-proxy") : defaultLogger("tcp-proxy");