Skip to content

Commit

Permalink
massive refactoring changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurschreiber committed Sep 27, 2023
1 parent 335313c commit 08c2a03
Show file tree
Hide file tree
Showing 14 changed files with 1,221 additions and 660 deletions.
914 changes: 415 additions & 499 deletions src/connection.ts

Large diffs are not rendered by default.

23 changes: 7 additions & 16 deletions src/connector.ts
Expand Up @@ -3,16 +3,13 @@ import dns, { type LookupAddress } from 'dns';

import * as punycode from 'punycode';
import { AbortSignal } from 'node-abort-controller';
import AbortError from './errors/abort-error';

import AggregateError from 'es-aggregate-error';

type LookupFunction = (hostname: string, options: dns.LookupAllOptions, callback: (err: NodeJS.ErrnoException | null, addresses: dns.LookupAddress[]) => void) => void;

export async function connectInParallel(options: { host: string, port: number, localAddress?: string | undefined }, lookup: LookupFunction, signal: AbortSignal) {
if (signal.aborted) {
throw new AbortError();
}
signal.throwIfAborted();

const addresses = await lookupAllAddresses(options.host, lookup, signal);

Expand Down Expand Up @@ -64,7 +61,7 @@ export async function connectInParallel(options: { host: string, port: number, l
socket.destroy();
}

reject(new AbortError());
reject(signal.reason);
};

for (let i = 0, len = addresses.length; i < len; i++) {
Expand All @@ -83,9 +80,7 @@ export async function connectInParallel(options: { host: string, port: number, l
}

export async function connectInSequence(options: { host: string, port: number, localAddress?: string | undefined }, lookup: LookupFunction, signal: AbortSignal) {
if (signal.aborted) {
throw new AbortError();
}
signal.throwIfAborted();

const errors: any[] = [];
const addresses = await lookupAllAddresses(options.host, lookup, signal);
Expand All @@ -105,7 +100,7 @@ export async function connectInSequence(options: { host: string, port: number, l

socket.destroy();

reject(new AbortError());
reject(signal.reason);
};

const onError = (err: Error) => {
Expand Down Expand Up @@ -134,9 +129,7 @@ export async function connectInSequence(options: { host: string, port: number, l
socket.on('connect', onConnect);
});
} catch (err) {
if (err instanceof Error && err.name === 'AbortError') {
throw err;
}
signal.throwIfAborted();

errors.push(err);

Expand All @@ -151,9 +144,7 @@ export async function connectInSequence(options: { host: string, port: number, l
* Look up all addresses for the given hostname.
*/
export async function lookupAllAddresses(host: string, lookup: LookupFunction, signal: AbortSignal): Promise<dns.LookupAddress[]> {
if (signal.aborted) {
throw new AbortError();
}
signal.throwIfAborted();

if (net.isIPv6(host)) {
return [{ address: host, family: 6 }];
Expand All @@ -162,7 +153,7 @@ export async function lookupAllAddresses(host: string, lookup: LookupFunction, s
} else {
return await new Promise<LookupAddress[]>((resolve, reject) => {
const onAbort = () => {
reject(new AbortError());
reject(signal.reason);
};

signal.addEventListener('abort', onAbort);
Expand Down

0 comments on commit 08c2a03

Please sign in to comment.