From 7b72630e87e1d4fac71d8ede3207bc1403ebafaf Mon Sep 17 00:00:00 2001 From: Ben Rogozinski Date: Tue, 2 Apr 2024 19:18:30 -0500 Subject: [PATCH 1/5] Added the cache-control to the list of forwarede headers This allows reverse proxy servers such as Nginx or a CloudFlare upstream to properly utilize caching with the appropriate cache keys --- src/V1.ts | 5 +++++ src/V2.ts | 1 + src/V3.ts | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/V1.ts b/src/V1.ts index 5420bd5..c30ed0d 100644 --- a/src/V1.ts +++ b/src/V1.ts @@ -213,6 +213,11 @@ const tunnelRequest: RouteCallback = async (request, res, options) => { 'content-length', flattenHeader(response.headers[header]!), ); + else if (header === 'cache-control') + responseHeaders.set( + 'cache-control', + flattenHeader(response.headers[header]!), + ) } responseHeaders.set( diff --git a/src/V2.ts b/src/V2.ts index 234514e..2e84d43 100644 --- a/src/V2.ts +++ b/src/V2.ts @@ -57,6 +57,7 @@ const defaultForwardHeaders: string[] = [ 'sec-websocket-extensions', 'sec-websocket-key', 'sec-websocket-version', + 'cache-control', ]; const defaultPassHeaders: string[] = [ diff --git a/src/V3.ts b/src/V3.ts index b40ebb9..eaebf76 100644 --- a/src/V3.ts +++ b/src/V3.ts @@ -46,7 +46,7 @@ const forbiddenPassHeaders: string[] = [ ]; // common defaults -const defaultForwardHeaders: string[] = ['accept-encoding', 'accept-language']; +const defaultForwardHeaders: string[] = ['accept-encoding', 'accept-language', 'cache-control']; const defaultPassHeaders: string[] = [ 'content-encoding', From 3928a1c9b61083fe6365c3793b8febdb84ec5860 Mon Sep 17 00:00:00 2001 From: BenRogozinski Date: Wed, 10 Apr 2024 08:55:48 -0500 Subject: [PATCH 2/5] Attempting fix for crashes when abort is detected --- src/V1.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/V1.ts b/src/V1.ts index c30ed0d..573ee88 100644 --- a/src/V1.ts +++ b/src/V1.ts @@ -182,11 +182,11 @@ function readHeaders(request: BareRequest): BareHeaderData { const tunnelRequest: RouteCallback = async (request, res, options) => { const abort = new AbortController(); - request.native.on('close', () => { + request.native.on('abort', () => { if (!request.native.complete) abort.abort(); }); - res.on('close', () => { + res.on('abort', () => { abort.abort(); }); From 377e1731024e755f12eaeab4f2550cf031f78874 Mon Sep 17 00:00:00 2001 From: BenRogozinski Date: Wed, 10 Apr 2024 09:52:28 -0500 Subject: [PATCH 3/5] Changed outgoingError function to run asynchronously --- src/V1.ts | 6 ++-- src/requestUtil.ts | 73 ++++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/V1.ts b/src/V1.ts index 573ee88..20c8ecd 100644 --- a/src/V1.ts +++ b/src/V1.ts @@ -182,11 +182,11 @@ function readHeaders(request: BareRequest): BareHeaderData { const tunnelRequest: RouteCallback = async (request, res, options) => { const abort = new AbortController(); - request.native.on('abort', () => { + request.native.on('close', () => { if (!request.native.complete) abort.abort(); }); - res.on('abort', () => { + res.on('close', () => { abort.abort(); }); @@ -214,7 +214,7 @@ const tunnelRequest: RouteCallback = async (request, res, options) => { flattenHeader(response.headers[header]!), ); else if (header === 'cache-control') - responseHeaders.set( + responseHeaders.set( 'cache-control', flattenHeader(response.headers[header]!), ) diff --git a/src/requestUtil.ts b/src/requestUtil.ts index 2a761c0..4f2b011 100644 --- a/src/requestUtil.ts +++ b/src/requestUtil.ts @@ -21,38 +21,49 @@ export function randomHex(byteLength: number) { return hex; } -function outgoingError(error: T): T | BareError { - if (error instanceof Error) { - switch ((error).code) { - case 'ENOTFOUND': - return new BareError(500, { - code: 'HOST_NOT_FOUND', - id: 'request', - message: 'The specified host could not be resolved.', - }); - case 'ECONNREFUSED': - return new BareError(500, { - code: 'CONNECTION_REFUSED', - id: 'response', - message: 'The remote rejected the request.', - }); - case 'ECONNRESET': - return new BareError(500, { - code: 'CONNECTION_RESET', - id: 'response', - message: 'The request was forcibly closed.', - }); - case 'ETIMEOUT': - return new BareError(500, { - code: 'CONNECTION_TIMEOUT', - id: 'response', - message: 'The response timed out.', - }); +async function outgoingError(error: T): Promise { + return new Promise((resolve, reject) => { + setTimeout(() => { // Simulating asynchronous operation + if (error instanceof Error) { + switch ((error).code) { + case 'ENOTFOUND': + resolve(new BareError(500, { + code: 'HOST_NOT_FOUND', + id: 'request', + message: 'The specified host could not be resolved.', + })); + break; + case 'ECONNREFUSED': + resolve(new BareError(500, { + code: 'CONNECTION_REFUSED', + id: 'response', + message: 'The remote rejected the request.', + })); + break; + case 'ECONNRESET': + resolve(new BareError(500, { + code: 'CONNECTION_RESET', + id: 'response', + message: 'The request was forcibly closed.', + })); + break; + case 'ETIMEOUT': + resolve(new BareError(500, { + code: 'CONNECTION_TIMEOUT', + id: 'response', + message: 'The response timed out.', + })); + break; + default: + resolve(error); + break; + } + } else { + resolve(error); } - } - - return error; -} + }, 0); + }); + } export async function bareFetch( request: BareRequest, From 52074fbdd18df93ad36183817323ced1f81cd001 Mon Sep 17 00:00:00 2001 From: BenRogozinski Date: Wed, 10 Apr 2024 10:01:23 -0500 Subject: [PATCH 4/5] Trying a different approach with the error handling --- src/requestUtil.ts | 76 ++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/requestUtil.ts b/src/requestUtil.ts index 4f2b011..3bc4569 100644 --- a/src/requestUtil.ts +++ b/src/requestUtil.ts @@ -21,49 +21,38 @@ export function randomHex(byteLength: number) { return hex; } -async function outgoingError(error: T): Promise { - return new Promise((resolve, reject) => { - setTimeout(() => { // Simulating asynchronous operation - if (error instanceof Error) { - switch ((error).code) { - case 'ENOTFOUND': - resolve(new BareError(500, { - code: 'HOST_NOT_FOUND', - id: 'request', - message: 'The specified host could not be resolved.', - })); - break; - case 'ECONNREFUSED': - resolve(new BareError(500, { - code: 'CONNECTION_REFUSED', - id: 'response', - message: 'The remote rejected the request.', - })); - break; - case 'ECONNRESET': - resolve(new BareError(500, { - code: 'CONNECTION_RESET', - id: 'response', - message: 'The request was forcibly closed.', - })); - break; - case 'ETIMEOUT': - resolve(new BareError(500, { - code: 'CONNECTION_TIMEOUT', - id: 'response', - message: 'The response timed out.', - })); - break; - default: - resolve(error); - break; - } - } else { - resolve(error); +function outgoingError(error: T): T | BareError { + if (error instanceof Error) { + switch ((error).code) { + case 'ENOTFOUND': + return new BareError(500, { + code: 'HOST_NOT_FOUND', + id: 'request', + message: 'The specified host could not be resolved.', + }); + case 'ECONNREFUSED': + return new BareError(500, { + code: 'CONNECTION_REFUSED', + id: 'response', + message: 'The remote rejected the request.', + }); + case 'ECONNRESET': + return new BareError(500, { + code: 'CONNECTION_RESET', + id: 'response', + message: 'The request was forcibly closed.', + }); + case 'ETIMEOUT': + return new BareError(500, { + code: 'CONNECTION_TIMEOUT', + id: 'response', + message: 'The response timed out.', + }); } - }, 0); - }); - } + } + + return error; +} export async function bareFetch( request: BareRequest, @@ -115,7 +104,8 @@ export async function bareFetch( }); outgoing.on('error', (error: Error) => { - reject(outgoingError(error)); + // Reject only with the original error, not another error + reject(error); }); }); } From 01d2e52a69bc8132dda2e9c088e25f01f811393e Mon Sep 17 00:00:00 2001 From: BenRogozinski Date: Wed, 10 Apr 2024 10:04:04 -0500 Subject: [PATCH 5/5] Back to the drawing board --- src/requestUtil.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/requestUtil.ts b/src/requestUtil.ts index 3bc4569..2a761c0 100644 --- a/src/requestUtil.ts +++ b/src/requestUtil.ts @@ -104,8 +104,7 @@ export async function bareFetch( }); outgoing.on('error', (error: Error) => { - // Reject only with the original error, not another error - reject(error); + reject(outgoingError(error)); }); }); }