diff --git a/index.ts b/index.ts index 1df3ed8..831147e 100644 --- a/index.ts +++ b/index.ts @@ -19,11 +19,11 @@ interface Options { } class Client { - source: string; - target: string; - fetch: typeof global.fetch; - logger: Pick; - events!: EventSource; + #source: string; + #target: string; + #fetch: typeof global.fetch; + #logger: Pick; + #events!: EventSource; constructor({ source, @@ -31,10 +31,10 @@ class Client { logger = console, fetch = undiciFetch, }: Options) { - this.source = source; - this.target = target; - this.logger = logger!; - this.fetch = fetch; + this.#source = source; + this.#target = target; + this.#logger = logger!; + this.#fetch = fetch; if (!validator.isURL(this.source)) { throw new Error("The provided URL is invalid."); @@ -57,7 +57,7 @@ class Client { async onmessage(msg: MessageEvent) { const data = JSON.parse(msg.data); - const target = url.parse(this.target, true); + const target = url.parse(this.#target, true); const mergedQuery = { ...target.query, ...data.query }; target.search = querystring.stringify(mergedQuery); @@ -80,28 +80,28 @@ class Client { headers["content-type"] = "application/json"; try { - const response = await this.fetch(url.format(target), { + const response = await this.#fetch(url.format(target), { method: "POST", mode: data["sec-fetch-mode"], body, headers, }); - this.logger.info(`POST ${response.url} - ${response.status}`); + this.#logger.info(`POST ${response.url} - ${response.status}`); } catch (err) { - this.logger.error(err); + this.#logger.error(err); } } onopen() { - this.logger.info("Connected", this.events.url); + this.#logger.info("Connected", this.events.url); } onerror(err: ErrorEvent) { - this.logger.error(err); + this.#logger.error(err); } start() { - const events = new EventSource(this.source, { + const events = new EventSource(this.#source, { dispatcher: new EnvHttpProxyAgent(), }); @@ -112,8 +112,8 @@ class Client { events.addEventListener("open", this.onopen.bind(this)); events.addEventListener("error", this.onerror.bind(this)); - this.logger.info(`Forwarding ${this.source} to ${this.target}`); - this.events = events; + this.#logger.info(`Forwarding ${this.#source} to ${this.#target}`); + this.#events = events; return events; }