mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-04-04 14:29:47 +00:00
Merge pull request #10 from Start9Labs/for/feature/multi-host/refactor
chore: Host with minor refactors
This commit is contained in:
@@ -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
|
||||||
@@ -68,20 +61,36 @@ export type PortOptions = {
|
|||||||
preferredExternalPort: number
|
preferredExternalPort: number
|
||||||
addSsl: AddSslOptions | null
|
addSsl: AddSslOptions | null
|
||||||
} & Security
|
} & Security
|
||||||
type PortOptionsByKnownProtocol<T extends KnownProtocol> =
|
type KnownProtocols = typeof knownProtocols
|
||||||
(typeof knownProtocols)[T] extends { withSsl: KnownProtocol }
|
type ProtocolsWithSslVariants = {
|
||||||
? BasePortOptions<T> &
|
[K in keyof KnownProtocols]: KnownProtocols[K] extends {
|
||||||
({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> })
|
withSsl: string
|
||||||
: BasePortOptions<T> & { addSsl?: AddSslOptions | null }
|
}
|
||||||
type PortOptionsByProtocol<T extends string> = T extends KnownProtocol
|
? K
|
||||||
? PortOptionsByKnownProtocol<T>
|
: never
|
||||||
: PortOptions
|
}[keyof KnownProtocols]
|
||||||
|
type NotProtocolsWithSslVariants = Exclude<
|
||||||
|
keyof KnownProtocols,
|
||||||
|
ProtocolsWithSslVariants
|
||||||
|
>
|
||||||
|
|
||||||
function isForKnownProtocol(
|
type PortOptionsByKnownProtocol =
|
||||||
options: PortOptionsByProtocol<string> | PortOptionsByProtocol<KnownProtocol>,
|
| ({
|
||||||
): options is PortOptionsByProtocol<KnownProtocol> {
|
protocol: ProtocolsWithSslVariants
|
||||||
return "protocol" in options && (options.protocol as string) in knownProtocols
|
preferredExternalPort?: number
|
||||||
}
|
scheme?: Scheme
|
||||||
|
} & ({ noAddSsl: true } | { addSsl?: Partial<AddSslOptions> }))
|
||||||
|
| {
|
||||||
|
protocol: NotProtocolsWithSslVariants
|
||||||
|
preferredExternalPort?: number
|
||||||
|
scheme?: Scheme
|
||||||
|
addSsl?: AddSslOptions | null
|
||||||
|
}
|
||||||
|
type PortOptionsByProtocol = PortOptionsByKnownProtocol | PortOptions
|
||||||
|
|
||||||
|
const hasStringProtocal = object({
|
||||||
|
protocol: string,
|
||||||
|
}).test
|
||||||
|
|
||||||
export class Host {
|
export class Host {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -92,61 +101,90 @@ 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 (hasStringProtocal(options)) {
|
||||||
const scheme =
|
return await this.bindPortForKnown(options, internalPort)
|
||||||
options.scheme === undefined ? options.protocol : options.scheme
|
|
||||||
const protoInfo = knownProtocols[options.protocol]
|
|
||||||
const preferredExternalPort =
|
|
||||||
options.preferredExternalPort ||
|
|
||||||
knownProtocols[options.protocol].defaultPort
|
|
||||||
const defaultAddSsl =
|
|
||||||
"noAddSsl" in options && options.noAddSsl
|
|
||||||
? null
|
|
||||||
: "withSsl" in protoInfo
|
|
||||||
? {
|
|
||||||
preferredExternalPort:
|
|
||||||
knownProtocols[protoInfo.withSsl].defaultPort,
|
|
||||||
scheme: protoInfo.withSsl,
|
|
||||||
}
|
|
||||||
: null
|
|
||||||
const addSsl = options.addSsl
|
|
||||||
? { ...defaultAddSsl, ...options.addSsl }
|
|
||||||
: defaultAddSsl
|
|
||||||
const security = {
|
|
||||||
secure: protoInfo.secure,
|
|
||||||
ssl: protoInfo.ssl,
|
|
||||||
} as Security
|
|
||||||
|
|
||||||
const newOptions = {
|
|
||||||
scheme,
|
|
||||||
preferredExternalPort,
|
|
||||||
addSsl,
|
|
||||||
...security,
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.options.effects.bind({
|
|
||||||
kind: this.kind,
|
|
||||||
id: this.options.id,
|
|
||||||
internalPort: internalPort,
|
|
||||||
...newOptions,
|
|
||||||
})
|
|
||||||
|
|
||||||
return new Origin(this, newOptions)
|
|
||||||
} else {
|
} else {
|
||||||
await this.options.effects.bind({
|
return await this.bindPortForUnknown(internalPort, options)
|
||||||
kind: this.kind,
|
|
||||||
id: this.options.id,
|
|
||||||
internalPort: internalPort,
|
|
||||||
...options,
|
|
||||||
})
|
|
||||||
|
|
||||||
return new Origin(this, options)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async bindPortForUnknown(
|
||||||
|
internalPort: number,
|
||||||
|
options:
|
||||||
|
| ({
|
||||||
|
scheme: Scheme
|
||||||
|
preferredExternalPort: number
|
||||||
|
addSsl: AddSslOptions | null
|
||||||
|
} & { secure: false; ssl: false })
|
||||||
|
| ({
|
||||||
|
scheme: Scheme
|
||||||
|
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 addSsl = this.getAddSsl(options, protoInfo)
|
||||||
|
|
||||||
|
const security: Security = !protoInfo.secure
|
||||||
|
? {
|
||||||
|
secure: protoInfo.secure,
|
||||||
|
ssl: protoInfo.ssl,
|
||||||
|
}
|
||||||
|
: { secure: false, ssl: false }
|
||||||
|
|
||||||
|
const newOptions = {
|
||||||
|
scheme,
|
||||||
|
preferredExternalPort,
|
||||||
|
addSsl,
|
||||||
|
...security,
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.options.effects.bind({
|
||||||
|
kind: this.kind,
|
||||||
|
id: this.options.id,
|
||||||
|
internalPort,
|
||||||
|
...newOptions,
|
||||||
|
})
|
||||||
|
|
||||||
|
return new Origin(this, newOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
private getAddSsl(
|
||||||
|
options: PortOptionsByKnownProtocol,
|
||||||
|
protoInfo: KnownProtocols[keyof KnownProtocols],
|
||||||
|
): AddSslOptions | null {
|
||||||
|
if ("noAddSsl" in options && options.noAddSsl) return null
|
||||||
|
if ("withSsl" in protoInfo && protoInfo.withSsl)
|
||||||
|
return {
|
||||||
|
preferredExternalPort: knownProtocols[protoInfo.withSsl].defaultPort,
|
||||||
|
scheme: protoInfo.withSsl,
|
||||||
|
...("addSsl" in options ? options.addSsl : null),
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class StaticHost extends Host {
|
export class StaticHost extends Host {
|
||||||
|
|||||||
Reference in New Issue
Block a user