feat: Remove the todo and use a get primary hostname

This commit is contained in:
Blu-J
2023-05-30 15:15:40 -06:00
parent 348669e5c3
commit a6514161ba
3 changed files with 86 additions and 24 deletions

View File

@@ -247,6 +247,13 @@ export type Effects = {
callback: () => void callback: () => void
}): Promise<[HostName, ...HostName[]]> }): Promise<[HostName, ...HostName[]]>
getPrimaryHostname(options: {
kind?: "multi"
packageId?: string
hostId: string
callback: () => void
}): Promise<HostName | null>
/** Similar to the fetch api via the mdn, this is simplified but the point is /** Similar to the fetch api via the mdn, this is simplified but the point is
* to get something from some website, and return the response. * to get something from some website, and return the response.
*/ */

View File

@@ -13,7 +13,7 @@ export type Filled = {
ipv6Hostnames: HostName[] ipv6Hostnames: HostName[]
nonIpHostnames: HostName[] nonIpHostnames: HostName[]
allHostnames: HostName[] allHostnames: HostName[]
primaryHostname: HostName primaryHostname: HostName | null
urls: UrlString[] urls: UrlString[]
onionUrls: UrlString[] onionUrls: UrlString[]
@@ -23,7 +23,7 @@ export type Filled = {
ipv6Urls: UrlString[] ipv6Urls: UrlString[]
nonIpUrls: UrlString[] nonIpUrls: UrlString[]
allUrls: UrlString[] allUrls: UrlString[]
primaryUrl: UrlString primaryUrl: UrlString | null
} }
export type FilledAddress = Address & Filled export type FilledAddress = Address & Filled
export type NetworkInterfaceFilled = { export type NetworkInterfaceFilled = {
@@ -63,6 +63,9 @@ export const filledAddress = (
mapHostnames: { mapHostnames: {
[hostId: string]: HostName[] [hostId: string]: HostName[]
}, },
mapPrimaryHostname: {
[hostId: string]: HostName | null
},
address: Address, address: Address,
): FilledAddress => { ): FilledAddress => {
const toUrl = addressHostToUrl.bind(null, address) const toUrl = addressHostToUrl.bind(null, address)
@@ -92,7 +95,7 @@ export const filledAddress = (
}, },
allHostnames: hostnames, allHostnames: hostnames,
get primaryHostname() { get primaryHostname() {
return this.allHostnames[0] // @TODO this is a placeholder return mapPrimaryHostname[address.hostId] ?? null
}, },
get urls() { get urls() {
return hostnames.map(toUrl) return hostnames.map(toUrl)
@@ -123,7 +126,9 @@ export const filledAddress = (
return hostnames.map(toUrl) return hostnames.map(toUrl)
}, },
get primaryUrl() { get primaryUrl() {
return this.allUrls[0] // @TODO this is a placeholder const primaryHostName = mapPrimaryHostname[address.hostId] ?? null
if (primaryHostName == null) return null
return toUrl(primaryHostName)
}, },
} }
} }
@@ -160,7 +165,11 @@ export const networkInterfaceFilled = (
return unique(addresses.flatMap((x) => x.allHostnames)) return unique(addresses.flatMap((x) => x.allHostnames))
}, },
get primaryHostname() { get primaryHostname() {
return this.allHostnames[0] // @TODO this is a placeholder for (const address of addresses) {
const primaryHostname = address.primaryHostname
if (primaryHostname != null) return primaryHostname
}
return null
}, },
get urls() { get urls() {
return unique(addresses.flatMap((x) => x.urls)) return unique(addresses.flatMap((x) => x.urls))
@@ -187,7 +196,11 @@ export const networkInterfaceFilled = (
return unique(addresses.flatMap((x) => x.allUrls)) return unique(addresses.flatMap((x) => x.allUrls))
}, },
get primaryUrl() { get primaryUrl() {
return this.allUrls[0] // @TODO this is a placeholder for (const address of addresses) {
const primaryUrl = address.primaryUrl
if (primaryUrl != null) return primaryUrl
}
return null
}, },
} }
} }
@@ -207,22 +220,38 @@ const makeInterfaceFilled = async ({
packageId, packageId,
callback, callback,
}) })
const hostIdsRecord: { [hostId: HostId]: HostName[] } = Object.fromEntries( const hostIdsRecord = Promise.all(
await Promise.all( unique(interfaceValue.addresses.map((x) => x.hostId)).map(
unique(interfaceValue.addresses.map((x) => x.hostId)).map( async (hostId) =>
async (hostId) => [ [
hostId, hostId,
effects.getHostnames({ await effects.getHostnames({
packageId, packageId,
hostId, hostId,
callback, callback,
}), }),
], ] as const,
),
), ),
) )
const hostIdPrimaryRecord = Promise.all(
unique(interfaceValue.addresses.map((x) => x.hostId)).map(
async (hostId) =>
[
hostId,
await effects.getPrimaryHostname({
packageId,
hostId,
callback,
}),
] as const,
),
).then((xs) => xs.filter(([, x]) => x != null) as Array<[HostId, HostName]>)
const fillAddress = filledAddress.bind(null, hostIdsRecord) const fillAddress = filledAddress.bind(
null,
Object.fromEntries(await hostIdsRecord),
Object.fromEntries(await hostIdPrimaryRecord),
)
const interfaceFilled: NetworkInterfaceFilled = networkInterfaceFilled( const interfaceFilled: NetworkInterfaceFilled = networkInterfaceFilled(
interfaceValue, interfaceValue,
interfaceValue.addresses.map(fillAddress), interfaceValue.addresses.map(fillAddress),

View File

@@ -19,23 +19,49 @@ const makeManyInterfaceFilled = async ({
packageId, packageId,
callback, callback,
}) })
const hostIdsRecord: { [hostId: HostId]: HostName[] } = Object.fromEntries( const hostIdsRecord = Object.fromEntries(
await Promise.all( await Promise.all(
Array.from( Array.from(
new Set( new Set(
interfaceValues.flatMap((x) => x.addresses).map((x) => x.hostId), interfaceValues.flatMap((x) => x.addresses).map((x) => x.hostId),
), ),
).map(async (hostId) => [ ).map(
hostId, async (hostId) =>
effects.getHostnames({ [
packageId, hostId,
hostId, await effects.getHostnames({
callback, packageId,
}), hostId,
]), callback,
}),
] as const,
),
), ),
) )
const fillAddress = filledAddress.bind(null, hostIdsRecord) const primaryHostIdsRecord = Object.fromEntries(
await Promise.all(
Array.from(
new Set(
interfaceValues.flatMap((x) => x.addresses).map((x) => x.hostId),
),
).map(
async (hostId) =>
[
hostId,
await effects.getPrimaryHostname({
packageId,
hostId,
callback,
}),
] as const,
),
),
)
const fillAddress = filledAddress.bind(
null,
hostIdsRecord,
primaryHostIdsRecord,
)
const interfacesFilled: NetworkInterfaceFilled[] = interfaceValues.map( const interfacesFilled: NetworkInterfaceFilled[] = interfaceValues.map(
(interfaceValue) => (interfaceValue) =>