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 { InputSpec } from "./config/configTypes"
import { DependenciesReceipt } from "./config/setupConfig" import { DependenciesReceipt } from "./config/setupConfig"
import { PortOptions } from "./interfaces/Host" import { PortOptions } from "./interfaces/Host"
import { UrlString } from "./util/getNetworkInterface"
export type ExportedAction = (options: { export type ExportedAction = (options: {
effects: Effects effects: Effects
@@ -247,13 +248,6 @@ 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.
*/ */
@@ -350,6 +344,16 @@ export type Effects = {
callback: () => void callback: () => void
}): Promise<NetworkInterface> }): 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 * There are times that we want to see the addresses that where exported
* @param options.addressId If we want to filter the address id * @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 UrlString = string
export type HostId = 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 = { export type Filled = {
hostnames: HostName[] hostnames: HostName[]
onionHostnames: HostName[] onionHostnames: HostName[]
@@ -13,7 +23,6 @@ export type Filled = {
ipv6Hostnames: HostName[] ipv6Hostnames: HostName[]
nonIpHostnames: HostName[] nonIpHostnames: HostName[]
allHostnames: HostName[] allHostnames: HostName[]
primaryHostname: HostName | null
urls: UrlString[] urls: UrlString[]
onionUrls: UrlString[] onionUrls: UrlString[]
@@ -23,7 +32,6 @@ export type Filled = {
ipv6Urls: UrlString[] ipv6Urls: UrlString[]
nonIpUrls: UrlString[] nonIpUrls: UrlString[]
allUrls: UrlString[] allUrls: UrlString[]
primaryUrl: UrlString | null
} }
export type FilledAddress = Address & Filled export type FilledAddress = Address & Filled
export type NetworkInterfaceFilled = { export type NetworkInterfaceFilled = {
@@ -42,6 +50,9 @@ export type NetworkInterfaceFilled = {
* ui interface * ui interface
*/ */
ui: boolean ui: boolean
primaryHostname: HostName | null
primaryUrl: UrlString | null
} & Filled } & Filled
const either = const either =
<A>(...args: ((a: A) => boolean)[]) => <A>(...args: ((a: A) => boolean)[]) =>
@@ -63,9 +74,6 @@ 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)
@@ -94,9 +102,6 @@ export const filledAddress = (
) )
}, },
allHostnames: hostnames, allHostnames: hostnames,
get primaryHostname() {
return mapPrimaryHostname[address.hostId] ?? null
},
get urls() { get urls() {
return hostnames.map(toUrl) return hostnames.map(toUrl)
}, },
@@ -125,16 +130,12 @@ export const filledAddress = (
get allUrls() { get allUrls() {
return hostnames.map(toUrl) return hostnames.map(toUrl)
}, },
get primaryUrl() {
const primaryHostName = mapPrimaryHostname[address.hostId] ?? null
if (primaryHostName == null) return null
return toUrl(primaryHostName)
},
} }
} }
export const networkInterfaceFilled = ( export const networkInterfaceFilled = (
interfaceValue: NetworkInterface, interfaceValue: NetworkInterface,
primaryUrl: UrlString | null,
addresses: FilledAddress[], addresses: FilledAddress[],
): NetworkInterfaceFilled => { ): NetworkInterfaceFilled => {
return { return {
@@ -165,11 +166,8 @@ export const networkInterfaceFilled = (
return unique(addresses.flatMap((x) => x.allHostnames)) return unique(addresses.flatMap((x) => x.allHostnames))
}, },
get primaryHostname() { get primaryHostname() {
for (const address of addresses) { if (primaryUrl == null) return null
const primaryHostname = address.primaryHostname return getHostname(primaryUrl)
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))
@@ -195,13 +193,7 @@ export const networkInterfaceFilled = (
get allUrls() { get allUrls() {
return unique(addresses.flatMap((x) => x.allUrls)) return unique(addresses.flatMap((x) => x.allUrls))
}, },
get primaryUrl() { primaryUrl,
for (const address of addresses) {
const primaryUrl = address.primaryUrl
if (primaryUrl != null) return primaryUrl
}
return null
},
} }
} }
const makeInterfaceFilled = async ({ const makeInterfaceFilled = async ({
@@ -233,27 +225,19 @@ const makeInterfaceFilled = async ({
] as const, ] as const,
), ),
) )
const hostIdPrimaryRecord = Promise.all( const primaryUrl = effects.getPrimaryUrl({
unique(interfaceValue.addresses.map((x) => x.hostId)).map( interfaceId,
async (hostId) =>
[
hostId,
await effects.getPrimaryHostname({
packageId, packageId,
hostId,
callback, callback,
}), })
] as const,
),
).then((xs) => xs.filter(([, x]) => x != null) as Array<[HostId, HostName]>)
const fillAddress = filledAddress.bind( const fillAddress = filledAddress.bind(
null, null,
Object.fromEntries(await hostIdsRecord), Object.fromEntries(await hostIdsRecord),
Object.fromEntries(await hostIdPrimaryRecord),
) )
const interfaceFilled: NetworkInterfaceFilled = networkInterfaceFilled( const interfaceFilled: NetworkInterfaceFilled = networkInterfaceFilled(
interfaceValue, interfaceValue,
await primaryUrl,
interfaceValue.addresses.map(fillAddress), interfaceValue.addresses.map(fillAddress),
) )
return interfaceFilled return interfaceFilled

View File

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