feat: Change to a different interface

This commit is contained in:
Blu-J
2023-05-30 16:28:01 -06:00
parent a6514161ba
commit 19e3af3536
4 changed files with 63 additions and 72 deletions

View File

@@ -0,0 +1,20 @@
import { getHostname } from "../util/getNetworkInterface"
describe("getHostname ", () => {
const inputToExpected = [
["http://localhost:3000", "localhost"],
["http://localhost", "localhost"],
["localhost", "localhost"],
["http://127.0.0.1/", "127.0.0.1"],
["http://127.0.0.1/testing/1234?314345", "127.0.0.1"],
["127.0.0.1/", "127.0.0.1"],
["http://mail.google.com/", "mail.google.com"],
["mail.google.com/", "mail.google.com"],
]
for (const [input, expectValue] of inputToExpected) {
test(`should return ${expectValue} for ${input}`, () => {
expect(getHostname(input)).toEqual(expectValue)
})
}
})

View File

@@ -2,6 +2,7 @@ export * as configTypes from "./config/configTypes"
import { InputSpec } from "./config/configTypes"
import { DependenciesReceipt } from "./config/setupConfig"
import { PortOptions } from "./interfaces/Host"
import { UrlString } from "./util/getNetworkInterface"
export type ExportedAction = (options: {
effects: Effects
@@ -247,13 +248,6 @@ export type Effects = {
callback: () => void
}): 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
* to get something from some website, and return the response.
*/
@@ -350,6 +344,16 @@ export type Effects = {
callback: () => void
}): Promise<NetworkInterface>
/**
* The user sets the primary url for a interface
* @param options
*/
getPrimaryUrl(options: {
packageId?: PackageId
interfaceId: InterfaceId
callback: () => void
}): Promise<UrlString | null>
/**
* There are times that we want to see the addresses that where exported
* @param options.addressId If we want to filter the address id

View File

@@ -4,6 +4,16 @@ import * as regexes from "./regexes"
export type UrlString = string
export type HostId = string
const getHostnameRegex = /^(\w+:\/\/)?([^\/\:]+)(:\d{1,3})?(\/)?/
export const getHostname = (url: string): HostName | null => {
const founds = url.match(getHostnameRegex)?.[2]
if (!founds) return null
const parts = founds.split("@")
const last = parts[parts.length - 1] as HostName | null
console.log({ url, parts, founds, last })
return last
}
export type Filled = {
hostnames: HostName[]
onionHostnames: HostName[]
@@ -13,7 +23,6 @@ export type Filled = {
ipv6Hostnames: HostName[]
nonIpHostnames: HostName[]
allHostnames: HostName[]
primaryHostname: HostName | null
urls: UrlString[]
onionUrls: UrlString[]
@@ -23,7 +32,6 @@ export type Filled = {
ipv6Urls: UrlString[]
nonIpUrls: UrlString[]
allUrls: UrlString[]
primaryUrl: UrlString | null
}
export type FilledAddress = Address & Filled
export type NetworkInterfaceFilled = {
@@ -42,6 +50,9 @@ export type NetworkInterfaceFilled = {
* ui interface
*/
ui: boolean
primaryHostname: HostName | null
primaryUrl: UrlString | null
} & Filled
const either =
<A>(...args: ((a: A) => boolean)[]) =>
@@ -63,9 +74,6 @@ export const filledAddress = (
mapHostnames: {
[hostId: string]: HostName[]
},
mapPrimaryHostname: {
[hostId: string]: HostName | null
},
address: Address,
): FilledAddress => {
const toUrl = addressHostToUrl.bind(null, address)
@@ -94,9 +102,6 @@ export const filledAddress = (
)
},
allHostnames: hostnames,
get primaryHostname() {
return mapPrimaryHostname[address.hostId] ?? null
},
get urls() {
return hostnames.map(toUrl)
},
@@ -125,16 +130,12 @@ export const filledAddress = (
get allUrls() {
return hostnames.map(toUrl)
},
get primaryUrl() {
const primaryHostName = mapPrimaryHostname[address.hostId] ?? null
if (primaryHostName == null) return null
return toUrl(primaryHostName)
},
}
}
export const networkInterfaceFilled = (
interfaceValue: NetworkInterface,
primaryUrl: UrlString | null,
addresses: FilledAddress[],
): NetworkInterfaceFilled => {
return {
@@ -165,11 +166,8 @@ export const networkInterfaceFilled = (
return unique(addresses.flatMap((x) => x.allHostnames))
},
get primaryHostname() {
for (const address of addresses) {
const primaryHostname = address.primaryHostname
if (primaryHostname != null) return primaryHostname
}
return null
if (primaryUrl == null) return null
return getHostname(primaryUrl)
},
get urls() {
return unique(addresses.flatMap((x) => x.urls))
@@ -195,13 +193,7 @@ export const networkInterfaceFilled = (
get allUrls() {
return unique(addresses.flatMap((x) => x.allUrls))
},
get primaryUrl() {
for (const address of addresses) {
const primaryUrl = address.primaryUrl
if (primaryUrl != null) return primaryUrl
}
return null
},
primaryUrl,
}
}
const makeInterfaceFilled = async ({
@@ -233,27 +225,19 @@ const makeInterfaceFilled = async ({
] 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 primaryUrl = effects.getPrimaryUrl({
interfaceId,
packageId,
callback,
})
const fillAddress = filledAddress.bind(
null,
Object.fromEntries(await hostIdsRecord),
Object.fromEntries(await hostIdPrimaryRecord),
)
const interfaceFilled: NetworkInterfaceFilled = networkInterfaceFilled(
interfaceValue,
await primaryUrl,
interfaceValue.addresses.map(fillAddress),
)
return interfaceFilled

View File

@@ -38,37 +38,20 @@ const makeManyInterfaceFilled = async ({
),
),
)
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 fillAddress = filledAddress.bind(null, hostIdsRecord)
const interfacesFilled: NetworkInterfaceFilled[] = interfaceValues.map(
(interfaceValue) =>
const interfacesFilled: NetworkInterfaceFilled[] = await Promise.all(
interfaceValues.map(async (interfaceValue) =>
networkInterfaceFilled(
interfaceValue,
await effects.getPrimaryUrl({
interfaceId: interfaceValue.interfaceId,
packageId,
callback,
}),
interfaceValue.addresses.map(fillAddress),
),
),
)
return interfacesFilled
}