Skip to content

Commit

Permalink
Use key/value object in place of Array for requests headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Elyahou committed Jan 10, 2024
1 parent 5222782 commit b9cd307
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const createHttpResponse = (statusCode: number, statusMessage: string, message =

interface Options {
method: string;
headers: string[];
headers: Record<string, string>;
path?: string;
localAddress?: string;
family?: number;
Expand Down Expand Up @@ -78,20 +78,19 @@ export const chain = (
const options: Options = {
method: 'CONNECT',
path: request.url,
headers: [
'host',
request.url!,
],
headers: {
host: request.url!,
},
localAddress: handlerOpts.localAddress,
family: handlerOpts.ipFamily,
lookup: handlerOpts.dnsLookup,
};

if (proxy.username || proxy.password) {
options.headers.push('proxy-authorization', getBasicAuthorizationHeader(proxy));
options.headers['proxy-authorization'] = getBasicAuthorizationHeader(proxy);
}

const client = http.request(proxy.origin, options as unknown as http.ClientRequestArgs);
const client = http.request(proxy.origin, options);

client.on('connect', (response, targetSocket, clientHead) => {
countTargetBytes(sourceSocket, targetSocket);
Expand Down
7 changes: 3 additions & 4 deletions src/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pipeline = util.promisify(stream.pipeline);

interface Options {
method: string;
headers: string[];
headers: Record<string, string>;
insecureHTTPParser: boolean;
path?: string;
localAddress?: string;
Expand Down Expand Up @@ -69,7 +69,7 @@ export const forward = async (

try {
if (proxy.username || proxy.password) {
options.headers.push('proxy-authorization', getBasicAuthorizationHeader(proxy));
options.headers['proxy-authorization'] = getBasicAuthorizationHeader(proxy);
}
} catch (error) {
reject(error);
Expand All @@ -79,8 +79,7 @@ export const forward = async (

const fn = origin!.startsWith('https:') ? https.request : http.request;

// We have to force cast `options` because @types/node doesn't support an array.
const client = fn(origin!, options as unknown as http.ClientRequestArgs, async (clientResponse) => {
const client = fn(origin!, options, async (clientResponse) => {
try {
// This is necessary to prevent Node.js throwing an error
let statusCode = clientResponse.statusCode!;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/valid_headers_only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { isHopByHopHeader } from './is_hop_by_hop_header';
/**
* @see https://nodejs.org/api/http.html#http_message_rawheaders
*/
export const validHeadersOnly = (rawHeaders: string[]): string[] => {
const result = [];
export const validHeadersOnly = (rawHeaders: string[]): Record<string, string> => {
const result: Record<string, string> = {};

let containsHost = false;

Expand Down Expand Up @@ -35,7 +35,7 @@ export const validHeadersOnly = (rawHeaders: string[]): string[] => {
containsHost = true;
}

result.push(name, value);
result[name] = value;
}

return result;
Expand Down

0 comments on commit b9cd307

Please sign in to comment.