chore: Host with minor refactors

This commit is contained in:
BluJ
2023-05-10 09:46:17 -06:00
parent e5e392392f
commit 78f28f544e

View File

@@ -68,19 +68,45 @@ export type PortOptions = {
preferredExternalPort: number preferredExternalPort: number
addSsl: AddSslOptions | null addSsl: AddSslOptions | null
} & Security } & Security
type PortOptionsByKnownProtocol<T extends KnownProtocol> = type KnownProtocols = typeof knownProtocols
type SslProtocols = {
[K in keyof KnownProtocols]: KnownProtocols[K] extends { ssl: true }
? K
: never
}[keyof KnownProtocols]
type NotSslProtocols = Exclude<keyof KnownProtocols, SslProtocols>
type OldPortOptionsByKnownProtocol<T extends KnownProtocol> =
(typeof knownProtocols)[T] extends { withSsl: KnownProtocol } (typeof knownProtocols)[T] extends { withSsl: KnownProtocol }
? BasePortOptions<T> & ? BasePortOptions<T> &
({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> }) ({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> })
: BasePortOptions<T> & { addSsl?: AddSslOptions | null } : BasePortOptions<T> & { addSsl?: AddSslOptions | null }
type PortOptionsByProtocol<T extends string> = T extends KnownProtocol type OldPortOptionsByProtocol<T extends string> = T extends KnownProtocol
? PortOptionsByKnownProtocol<T> ? OldPortOptionsByKnownProtocol<T>
: PortOptions : PortOptions
type PortOptionsByKnownProtocol =
| ({
protocol: SslProtocols
preferredExternalPort?: number
scheme?: Scheme
} & ({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> }))
| {
protocol: NotSslProtocols
preferredExternalPort?: number
scheme?: Scheme
addSsl?: AddSslOptions | null
}
type PortOptionsByProtocol = PortOptionsByKnownProtocol | PortOptions
function isForKnownProtocol( function isForKnownProtocol(
options: PortOptionsByProtocol<string> | PortOptionsByProtocol<KnownProtocol>, options: PortOptionsByProtocol,
): options is PortOptionsByProtocol<KnownProtocol> { ): options is PortOptionsByKnownProtocol {
return "protocol" in options && (options.protocol as string) in knownProtocols return "protocol" in options && options.protocol in knownProtocols
}
const TRUE_DEFAULT = {
preferredExternalPort: 443,
scheme: "https",
} }
export class Host { export class Host {
@@ -92,60 +118,121 @@ export class Host {
}, },
) {} ) {}
async bindPort<T extends string>( async bindPort(
internalPort: number, internalPort: number,
options: PortOptionsByProtocol<T>, options: PortOptionsByProtocol,
): Promise<Origin<this>> { ): Promise<Origin<this>> {
if (isForKnownProtocol(options)) { if (isForKnownProtocol(options)) {
const scheme = return await this.bindPortForKnown(options, internalPort)
options.scheme === undefined ? options.protocol : options.scheme } else {
const protoInfo = knownProtocols[options.protocol] return await this.bindPortForUnknown(internalPort, options)
const preferredExternalPort = }
options.preferredExternalPort || }
knownProtocols[options.protocol].defaultPort
const defaultAddSsl = private async bindPortForUnknown(
"noAddSsl" in options && options.noAddSsl internalPort: number,
? null options:
: "withSsl" in protoInfo | ({
? { scheme: Scheme
preferredExternalPort: preferredExternalPort: number
knownProtocols[protoInfo.withSsl].defaultPort, addSsl: AddSslOptions | null
scheme: protoInfo.withSsl, } & { secure: false; ssl: false })
} | ({
: null scheme: Scheme
const addSsl = options.addSsl preferredExternalPort: number
addSsl: AddSslOptions | null
} & { secure: true; ssl: boolean }),
) {
await this.options.effects.bind({
kind: this.kind,
id: this.options.id,
internalPort: internalPort,
...options,
})
return new Origin(this, options)
}
private async bindPortForKnown(
options: PortOptionsByKnownProtocol,
internalPort: number,
) {
const scheme =
options.scheme === undefined ? options.protocol : options.scheme
const protoInfo = knownProtocols[options.protocol]
const preferredExternalPort =
options.preferredExternalPort ||
knownProtocols[options.protocol].defaultPort
const defaultAddSsl = this.bindPortForKnownDefaulAddSsl(options, protoInfo)
const addSsl =
"addSsl" in options
? { ...defaultAddSsl, ...options.addSsl } ? { ...defaultAddSsl, ...options.addSsl }
: defaultAddSsl : defaultAddSsl
const security = { const security: Security = !protoInfo.secure
secure: protoInfo.secure, ? {
ssl: protoInfo.ssl, secure: protoInfo.secure,
} as Security ssl: protoInfo.ssl,
}
: { secure: false, ssl: false }
const newOptions = { const newOptions = {
scheme, scheme,
preferredExternalPort, preferredExternalPort,
addSsl, addSsl,
...security, ...security,
}
await this.options.effects.bind({
kind: this.kind,
id: this.options.id,
internalPort: internalPort,
...newOptions,
})
return new Origin(this, newOptions)
} else {
await this.options.effects.bind({
kind: this.kind,
id: this.options.id,
internalPort: internalPort,
...options,
})
return new Origin(this, options)
} }
await this.options.effects.bind({
kind: this.kind,
id: this.options.id,
internalPort,
...newOptions,
})
return new Origin(this, newOptions)
}
private bindPortForKnownDefaulAddSsl(
options: PortOptionsByKnownProtocol,
protoInfo:
| {
readonly secure: false
readonly ssl: false
readonly defaultPort: 80
readonly withSsl: "https"
}
| { readonly secure: true; readonly ssl: true; readonly defaultPort: 443 }
| {
readonly secure: false
readonly ssl: false
readonly defaultPort: 80
readonly withSsl: "wss"
}
| { readonly secure: true; readonly ssl: true; readonly defaultPort: 443 }
| { readonly secure: true; readonly ssl: false; readonly defaultPort: 22 }
| {
readonly secure: true
readonly ssl: false
readonly defaultPort: 8333
}
| {
readonly secure: true
readonly ssl: true
readonly defaultPort: 50051
}
| {
readonly secure: true
readonly ssl: false
readonly defaultPort: 53
},
) {
if ("noAddSsl" in options && options.noAddSsl) return TRUE_DEFAULT
if ("withSsl" in protoInfo)
return {
preferredExternalPort: knownProtocols[protoInfo.withSsl].defaultPort,
scheme: protoInfo.withSsl,
}
return TRUE_DEFAULT
} }
} }