File Explorer

/proc/2/root/var/runtime/node_modules/@aws-sdk/util-dns/dist-cjs

This explorer reads the filesystem of the server it runs on, so /workspace/user isn't present here. Browsing and the terminal still work against this server's own disk from /.

index.js9.3 KB · 267 lines
const { HostAddressType } = require("@aws-sdk/types");const { promises, V4MAPPED, ALL } = require("node:dns"); class HostAddressEntryCollection {    data = [];    get length() {        return this.data.length;    }    cycle(collection) {        if (this.data.length === 0) {            throw new Error("Cannot cycle an empty collection");        }        const entry = this.data.shift();        (collection || this).append(entry);        return entry;    }    append(entry) {        this.data.push(entry);    }    remove(entry) {        if (this.length === 0) {            throw new Error("Cannot remove from an empty collection");        }        const index = this.data.findIndex((e) => e.address === entry.address);        const removedEntry = this.data[index];        this.data.splice(index, 1);        return removedEntry;    }    [Symbol.iterator]() {        return this.data[Symbol.iterator]();    }} class HostEntry {    aaaaRecords;    aRecords;    failedAaaaRecords;    failedARecords;    nextTimestampToUpdateMs;    constructor(nextTimestampToProcessMs) {        this.aaaaRecords = new HostAddressEntryCollection();        this.aRecords = new HostAddressEntryCollection();        this.failedAaaaRecords = new HostAddressEntryCollection();        this.failedARecords = new HostAddressEntryCollection();        this.nextTimestampToUpdateMs = nextTimestampToProcessMs;    }    updateRecords(addresses, expirationTtlMs) {        this.nextTimestampToUpdateMs = expirationTtlMs;        const addressesToAppend = [];        for (const freshAddress of addresses) {            const hostAddressEntry = this.findAddress(freshAddress);            if (hostAddressEntry !== undefined) {                hostAddressEntry.expirationTtlMs = expirationTtlMs;                continue;            }            addressesToAppend.push(freshAddress);        }        for (const addressToAppend of addressesToAppend) {            const hostAddressEntry = this.findAddress(addressToAppend);            if (hostAddressEntry !== undefined) {                continue;            }            const successRecords = this.getGoodRecords(addressToAppend);            successRecords.append(Object.assign(addressToAppend, {                expirationTtlMs,            }));        }    }    processRecords() {        this.processRecordsAddressType(this.aRecords, this.failedARecords);        this.processRecordsAddressType(this.aaaaRecords, this.failedAaaaRecords);    }    failAddressInRecords(address) {        const successRecords = this.getGoodRecords(address);        const failedRecords = this.getFailedRecords(address);        const recordsToRemove = [];        for (const hostAddressEntry of successRecords) {            if (hostAddressEntry.address === address.address) {                recordsToRemove.push(hostAddressEntry);                failedRecords.append(hostAddressEntry);            }        }        for (const recordToRemove of recordsToRemove) {            successRecords.remove(recordToRemove);        }    }    findAddress(address) {        const successRecords = this.getGoodRecords(address);        for (const hostAddressEntry of successRecords) {            if (address.address === hostAddressEntry.address) {                return hostAddressEntry;            }        }        const failedRecords = this.getFailedRecords(address);        for (const hostAddressEntry of failedRecords) {            if (address.address === hostAddressEntry.address) {                return hostAddressEntry;            }        }        return undefined;    }    processRecordsAddressType(successRecords, failedRecords) {        const successRecordsToRemove = [];        let successIndex = 0;        for (const hostAddressEntry of successRecords) {            if (successIndex === successRecords.length - 1) {                break;            }            if (Date.now() >= hostAddressEntry.expirationTtlMs) {                successRecordsToRemove.push(hostAddressEntry);            }            successIndex++;        }        for (const recordToRemove of successRecordsToRemove) {            successRecords.remove(recordToRemove);        }        const failedRecordsToRemove = [];        let failedIndex = 0;        for (const hostAddressEntry of failedRecords) {            if (failedIndex === failedRecords.length - 1) {                break;            }            if (Date.now() >= hostAddressEntry.expirationTtlMs) {                failedRecordsToRemove.push(hostAddressEntry);            }            failedIndex++;        }        for (const recordToRemove of failedRecordsToRemove) {            failedRecords.remove(recordToRemove);        }        if (successRecords.length === 0) {            let hostAddressEntryToPromote = undefined;            for (const hostAddressEntry of failedRecords) {                if (Date.now() >= hostAddressEntry.expirationTtlMs) {                    continue;                }                hostAddressEntryToPromote = hostAddressEntry;                break;            }            if (hostAddressEntryToPromote !== undefined) {                failedRecords.remove(hostAddressEntryToPromote);                successRecords.append(hostAddressEntryToPromote);            }        }    }    getGoodRecords(address) {        return address.addressType === HostAddressType.AAAA ? this.aaaaRecords : this.aRecords;    }    getFailedRecords(address) {        return address.addressType === HostAddressType.AAAA ? this.failedAaaaRecords : this.failedARecords;    }} class HostEntryTable {    map;    constructor() {        this.map = new Map();    }    set(args, addresses, nextTimestampToProcessMs) {        const hostEntry = new HostEntry(nextTimestampToProcessMs);        hostEntry.updateRecords(addresses, nextTimestampToProcessMs);        this.map.set(args.hostName, hostEntry);    }    get(hostName) {        return this.map.get(hostName);    }    delete(hostName) {        this.map.delete(hostName);    }    clear() {        this.map.clear();    }    get size() {        return this.map.size;    }} const NODE_DNS_FAMILY_TO_HOST_ADDRESS_TYPE = {    4: HostAddressType.A,    6: HostAddressType.AAAA,};const DNS_LOOKUP_OPTIONS = {    all: true,    hints: V4MAPPED | ALL,    verbatim: true,};class NodeDnsLookupHostResolver {    static DEFAULT_TTL_MS = 30_000;    static createDefaultCacheProvider = () => new HostEntryTable();    static DEFAULT_NODE_DNS_LOOKUP = promises.lookup;    ttlMs;    cache;    nodeDnsLookup;    constructor({ ttlMs = NodeDnsLookupHostResolver.DEFAULT_TTL_MS, cache = NodeDnsLookupHostResolver.createDefaultCacheProvider(), nodeDnsLookup = NodeDnsLookupHostResolver.DEFAULT_NODE_DNS_LOOKUP, } = {}) {        this.ttlMs = ttlMs;        this.cache = cache;        this.nodeDnsLookup = nodeDnsLookup;    }    async resolveAddress(args) {        const possibleHostEntry = this.cache.get(args.hostName);        const newNextTimestampToUpdateMs = Date.now() + this.ttlMs;        if (possibleHostEntry === undefined) {            const addresses = await this.nodeDnsLookupResolveAddress(args);            this.cache.set(args, addresses, newNextTimestampToUpdateMs);        }        const hostEntry = this.cache.get(args.hostName);        if (possibleHostEntry !== undefined && Date.now() >= hostEntry.nextTimestampToUpdateMs) {            try {                const addresses = await this.nodeDnsLookupResolveAddress(args);                hostEntry.updateRecords(addresses, newNextTimestampToUpdateMs);            }            catch (error) {                console.error(`Could not update DNS address cache for "${args.hostName}": ${error}`);            }        }        hostEntry.processRecords();        const result = [];        if (hostEntry.aRecords.length > 0) {            result.push(hostEntry.aRecords.cycle());        }        if (hostEntry.aaaaRecords.length > 0) {            result.push(hostEntry.aaaaRecords.cycle());        }        if (result.length === 0) {            throw new Error(`Could not resolve addresses for "${args.hostName}"`);        }        return result;    }    reportFailureOnAddress(addr) {        const hostEntry = this.cache.get(addr.hostName);        if (hostEntry === undefined) {            throw new Error(`Could not find cached host name "${addr.hostName}"`);        }        hostEntry.failAddressInRecords(addr);    }    purgeCache(args) {        if (args?.hostName) {            this.cache.delete(args.hostName);        }        else {            this.cache.clear();        }    }    async nodeDnsLookupResolveAddress(args) {        const addresses = [];        const ipEntries = await this.nodeDnsLookup(args.hostName, DNS_LOOKUP_OPTIONS);        for (const { address, family } of ipEntries) {            const addressType = NODE_DNS_FAMILY_TO_HOST_ADDRESS_TYPE[family];            if (addressType === undefined) {                throw new Error(`dns.lookup() Node DNS family \`${family}\` is not supported`);            }            addresses.push({                addressType,                address,                hostName: args.hostName,                service: args.service,            });        }        return addresses;    }} const HostResolver = NodeDnsLookupHostResolver; exports.HostResolver = HostResolver;exports.NodeDnsLookupHostResolver = NodeDnsLookupHostResolver;