chore: Update the types

This commit is contained in:
BluJ
2023-05-10 11:09:10 -06:00
parent 7e74c4bfd1
commit b07fda724b

View File

@@ -1,5 +1,5 @@
import { object, string } from "ts-matches"
import { Effects } from "../types" import { Effects } from "../types"
import { AddressReceipt } from "./AddressReceipt"
import { NetworkInterfaceBuilder } from "./NetworkInterfaceBuilder" import { NetworkInterfaceBuilder } from "./NetworkInterfaceBuilder"
import { Origin } from "./Origin" import { Origin } from "./Origin"
@@ -48,15 +48,8 @@ const knownProtocols = {
}, },
} as const } as const
type KnownProtocol = keyof typeof knownProtocols
type Scheme = string | null type Scheme = string | null
type BasePortOptions<T extends KnownProtocol> = {
protocol: T
preferredExternalPort?: number
scheme?: Scheme
}
type AddSslOptions = { type AddSslOptions = {
preferredExternalPort: number preferredExternalPort: number
scheme: Scheme scheme: Scheme
@@ -69,37 +62,35 @@ export type PortOptions = {
addSsl: AddSslOptions | null addSsl: AddSslOptions | null
} & Security } & Security
type KnownProtocols = typeof knownProtocols type KnownProtocols = typeof knownProtocols
type SslProtocols = { type ProtocolsWithSslVariants = {
[K in keyof KnownProtocols]: KnownProtocols[K] extends { ssl: true } [K in keyof KnownProtocols]: KnownProtocols[K] extends {
withSsl: string
}
? K ? K
: never : never
}[keyof KnownProtocols] }[keyof KnownProtocols]
type NotSslProtocols = Exclude<keyof KnownProtocols, SslProtocols> type NotProtocolsWithSslVariants = Exclude<
keyof KnownProtocols,
ProtocolsWithSslVariants
>
type PortOptionsByKnownProtocol = type PortOptionsByKnownProtocol =
| ({ | ({
protocol: SslProtocols protocol: ProtocolsWithSslVariants
preferredExternalPort?: number preferredExternalPort?: number
scheme?: Scheme scheme?: Scheme
} & ({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> })) } & ({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> }))
| { | {
protocol: NotSslProtocols protocol: NotProtocolsWithSslVariants
preferredExternalPort?: number preferredExternalPort?: number
scheme?: Scheme scheme?: Scheme
addSsl?: AddSslOptions | null addSsl?: AddSslOptions | null
} }
type PortOptionsByProtocol = PortOptionsByKnownProtocol | PortOptions type PortOptionsByProtocol = PortOptionsByKnownProtocol | PortOptions
function isForKnownProtocol( const hasStringProtocal = object({
options: PortOptionsByProtocol, protocol: string,
): options is PortOptionsByKnownProtocol { }).test
return "protocol" in options && options.protocol in knownProtocols
}
const TRUE_DEFAULT = {
preferredExternalPort: 443,
scheme: "https",
}
export class Host { export class Host {
constructor( constructor(
@@ -114,7 +105,7 @@ export class Host {
internalPort: number, internalPort: number,
options: PortOptionsByProtocol, options: PortOptionsByProtocol,
): Promise<Origin<this>> { ): Promise<Origin<this>> {
if (isForKnownProtocol(options)) { if (hasStringProtocal(options)) {
return await this.bindPortForKnown(options, internalPort) return await this.bindPortForKnown(options, internalPort)
} else { } else {
return await this.bindPortForUnknown(internalPort, options) return await this.bindPortForUnknown(internalPort, options)
@@ -155,11 +146,8 @@ export class Host {
const preferredExternalPort = const preferredExternalPort =
options.preferredExternalPort || options.preferredExternalPort ||
knownProtocols[options.protocol].defaultPort knownProtocols[options.protocol].defaultPort
const defaultAddSsl = this.bindPortForKnownDefaulAddSsl(options, protoInfo) const addSsl = this.getAddSsl(options, protoInfo)
const addSsl =
"addSsl" in options
? { ...defaultAddSsl, ...options.addSsl }
: defaultAddSsl
const security: Security = !protoInfo.secure const security: Security = !protoInfo.secure
? { ? {
secure: protoInfo.secure, secure: protoInfo.secure,
@@ -184,17 +172,18 @@ export class Host {
return new Origin(this, newOptions) return new Origin(this, newOptions)
} }
private bindPortForKnownDefaulAddSsl( private getAddSsl(
options: PortOptionsByKnownProtocol, options: PortOptionsByKnownProtocol,
protoInfo: KnownProtocols[keyof KnownProtocols], protoInfo: KnownProtocols[keyof KnownProtocols],
) { ): AddSslOptions | null {
if ("noAddSsl" in options && options.noAddSsl) return TRUE_DEFAULT if ("noAddSsl" in options && options.noAddSsl) return null
if ("withSsl" in protoInfo && protoInfo.withSsl) if ("withSsl" in protoInfo && protoInfo.withSsl)
return { return {
preferredExternalPort: knownProtocols[protoInfo.withSsl].defaultPort, preferredExternalPort: knownProtocols[protoInfo.withSsl].defaultPort,
scheme: protoInfo.withSsl, scheme: protoInfo.withSsl,
...("addSsl" in options ? options.addSsl : null),
} }
return TRUE_DEFAULT return null
} }
} }