From ed56e39f003e523ffeca55b5e36871a543818dac Mon Sep 17 00:00:00 2001 From: BluJ Date: Thu, 11 May 2023 14:50:40 -0600 Subject: [PATCH] feat: Create the setupInterfaces types --- lib/StartSdk.ts | 23 +++-- lib/config/setupConfig.ts | 4 +- lib/health/HealthCheck.ts | 2 +- lib/inits/setupInit.ts | 8 ++ lib/{mainFn => interfaces}/AddressReceipt.ts | 0 lib/{mainFn => interfaces}/Host.ts | 30 ++----- .../NetworkInterfaceBuilder.ts | 8 +- lib/{mainFn => interfaces}/Origin.ts | 0 .../interfaceReceipt.ts | 0 lib/interfaces/setupInterfaces.ts | 27 ++++++ lib/mainFn/Daemons.ts | 1 - lib/mainFn/NetworkBuilder.ts | 13 --- lib/mainFn/ReadyProof.ts | 4 - lib/mainFn/RunningMainRet.ts | 7 -- lib/mainFn/TorBinding.ts | 8 -- lib/mainFn/TorHostname.ts | 17 ---- lib/mainFn/exportInterfaces.ts | 13 --- lib/mainFn/index.ts | 10 +-- lib/test/host.test.ts | 27 ++++++ lib/types.ts | 2 +- lib/util/utils.ts | 86 ++++++++++--------- 21 files changed, 142 insertions(+), 148 deletions(-) rename lib/{mainFn => interfaces}/AddressReceipt.ts (100%) rename lib/{mainFn => interfaces}/Host.ts (87%) rename lib/{mainFn => interfaces}/NetworkInterfaceBuilder.ts (86%) rename lib/{mainFn => interfaces}/Origin.ts (100%) rename lib/{mainFn => interfaces}/interfaceReceipt.ts (100%) create mode 100644 lib/interfaces/setupInterfaces.ts delete mode 100644 lib/mainFn/NetworkBuilder.ts delete mode 100644 lib/mainFn/ReadyProof.ts delete mode 100644 lib/mainFn/RunningMainRet.ts delete mode 100644 lib/mainFn/TorBinding.ts delete mode 100644 lib/mainFn/TorHostname.ts delete mode 100644 lib/mainFn/exportInterfaces.ts create mode 100644 lib/test/host.test.ts diff --git a/lib/StartSdk.ts b/lib/StartSdk.ts index b0365d3..c92b2ec 100644 --- a/lib/StartSdk.ts +++ b/lib/StartSdk.ts @@ -16,9 +16,9 @@ import { ActionMetadata, Effects, ActionResult, - Metadata, BackupOptions, DeepPartial, + Address, } from "./types" import { Utils } from "./util/utils" import { DependencyConfig } from "./dependencyConfig/DependencyConfig" @@ -26,14 +26,11 @@ import { BackupSet, Backups } from "./backup/Backups" import { smtpConfig } from "./config/configConstants" import { Daemons } from "./mainFn/Daemons" import { healthCheck } from "./health/HealthCheck" -import { - checkPortListening, - containsAddress, -} from "./health/checkFns/checkPortListening" +import { checkPortListening } from "./health/checkFns/checkPortListening" import { checkWebUrl, runHealthScript } from "./health/checkFns" import { List } from "./config/builder/list" import { Migration } from "./inits/migrations/Migration" -import { Install, InstallFn, setupInstall } from "./inits/setupInstall" +import { Install, InstallFn } from "./inits/setupInstall" import { setupActions } from "./actions/setupActions" import { setupDependencyConfig } from "./dependencyConfig/setupDependencyConfig" import { SetupBackupsParams, setupBackups } from "./backup/setupBackups" @@ -49,6 +46,9 @@ import { defaultTrigger } from "./trigger/defaultTrigger" import { changeOnFirstSuccess, cooldownTrigger } from "./trigger" import setupConfig, { Read, Save } from "./config/setupConfig" import { setupDependencyMounts } from "./dependency/setupDependencyMounts" +import { SetInterfaces, setupInterfaces } from "./interfaces/setupInterfaces" +import { AddressReceipt } from "./interfaces/AddressReceipt" +import { Host } from "./interfaces/Host" // prettier-ignore type AnyNeverCond = @@ -155,8 +155,17 @@ export class StartSdk { migrations: Migrations, install: Install, uninstall: Uninstall, - ) => setupInit(migrations, install, uninstall), + setInterfaces: SetInterfaces, + ) => + setupInit(migrations, install, uninstall, setInterfaces), setupInstall: (fn: InstallFn) => Install.of(fn), + setupInterfaces: < + ConfigInput extends Record, + Output extends Record, + >( + config: Config, + fn: SetInterfaces, + ) => setupInterfaces(config, fn), setupMain: ( fn: (o: { effects: Effects diff --git a/lib/config/setupConfig.ts b/lib/config/setupConfig.ts index ea3b060..92a49f7 100644 --- a/lib/config/setupConfig.ts +++ b/lib/config/setupConfig.ts @@ -4,7 +4,8 @@ import * as D from "./configDependencies" import { Config, ExtractConfigType } from "./builder/config" import { Utils, utils } from "../util/utils" import nullIfEmpty from "../util/nullIfEmpty" -import { InterfaceReceipt } from "../mainFn/interfaceReceipt" +import { InterfaceReceipt } from "../interfaces/interfaceReceipt" +import { SetInterfacesReceipt } from "../interfaces/setupInterfaces" declare const dependencyProof: unique symbol export type DependenciesReceipt = void & { @@ -27,6 +28,7 @@ export type Save< }) => Promise<{ dependenciesReceipt: DependenciesReceipt interfaceReceipt: InterfaceReceipt + setInterfacesReceipt: SetInterfacesReceipt restart: boolean }> export type Read< diff --git a/lib/health/HealthCheck.ts b/lib/health/HealthCheck.ts index bb13313..8f0bcf8 100644 --- a/lib/health/HealthCheck.ts +++ b/lib/health/HealthCheck.ts @@ -1,4 +1,4 @@ -import { InterfaceReceipt } from "../mainFn/interfaceReceipt" +import { InterfaceReceipt } from "../interfaces/interfaceReceipt" import { Daemon, Effects } from "../types" import { CheckResult } from "./checkFns/CheckResult" import { HealthReceipt } from "./HealthReceipt" diff --git a/lib/inits/setupInit.ts b/lib/inits/setupInit.ts index ad5414b..ec420c6 100644 --- a/lib/inits/setupInit.ts +++ b/lib/inits/setupInit.ts @@ -1,4 +1,6 @@ +import { SetInterfaces } from "../interfaces/setupInterfaces" import { ExpectedExports } from "../types" +import { createUtils } from "../util" import { Migrations } from "./migrations/setupMigrations" import { Install } from "./setupInstall" import { Uninstall } from "./setupUninstall" @@ -7,6 +9,7 @@ export function setupInit( migrations: Migrations, install: Install, uninstall: Uninstall, + setInterfaces: SetInterfaces, ): { init: ExpectedExports.init uninit: ExpectedExports.uninit @@ -15,6 +18,11 @@ export function setupInit( init: async (opts) => { await migrations.init(opts) await install.init(opts) + await setInterfaces({ + ...opts, + input: null, + utils: createUtils(opts.effects), + }) }, uninit: async (opts) => { await migrations.uninit(opts) diff --git a/lib/mainFn/AddressReceipt.ts b/lib/interfaces/AddressReceipt.ts similarity index 100% rename from lib/mainFn/AddressReceipt.ts rename to lib/interfaces/AddressReceipt.ts diff --git a/lib/mainFn/Host.ts b/lib/interfaces/Host.ts similarity index 87% rename from lib/mainFn/Host.ts rename to lib/interfaces/Host.ts index 64012b4..41577cf 100644 --- a/lib/mainFn/Host.ts +++ b/lib/interfaces/Host.ts @@ -1,6 +1,5 @@ import { object, string } from "ts-matches" import { Effects } from "../types" -import { NetworkInterfaceBuilder } from "./NetworkInterfaceBuilder" import { Origin } from "./Origin" const knownProtocols = { @@ -94,9 +93,9 @@ const hasStringProtocal = object({ export class Host { constructor( - readonly kind: "static" | "single" | "multi", readonly options: { effects: Effects + kind: "static" | "single" | "multi" id: string }, ) {} @@ -127,7 +126,7 @@ export class Host { } & { secure: true; ssl: boolean }), ) { await this.options.effects.bind({ - kind: this.kind, + kind: this.options.kind, id: this.options.id, internalPort: internalPort, ...options, @@ -163,7 +162,7 @@ export class Host { } await this.options.effects.bind({ - kind: this.kind, + kind: this.options.kind, id: this.options.id, internalPort, ...newOptions, @@ -189,35 +188,18 @@ export class Host { export class StaticHost extends Host { constructor(options: { effects: Effects; id: string }) { - super("static", options) + super({ ...options, kind: "static" }) } } export class SingleHost extends Host { constructor(options: { effects: Effects; id: string }) { - super("single", options) + super({ ...options, kind: "single" }) } } export class MultiHost extends Host { constructor(options: { effects: Effects; id: string }) { - super("multi", options) + super({ ...options, kind: "multi" }) } } - -async function test(effects: Effects) { - const foo = new MultiHost({ effects, id: "foo" }) - const fooOrigin = await foo.bindPort(80, { protocol: "http" as const }) - const fooInterface = new NetworkInterfaceBuilder({ - effects, - name: "Foo", - id: "foo", - description: "A Foo", - ui: true, - username: "bar", - path: "/baz", - search: { qux: "yes" }, - }) - - await fooInterface.export([fooOrigin]) -} diff --git a/lib/mainFn/NetworkInterfaceBuilder.ts b/lib/interfaces/NetworkInterfaceBuilder.ts similarity index 86% rename from lib/mainFn/NetworkInterfaceBuilder.ts rename to lib/interfaces/NetworkInterfaceBuilder.ts index 66b6de9..5f64f74 100644 --- a/lib/mainFn/NetworkInterfaceBuilder.ts +++ b/lib/interfaces/NetworkInterfaceBuilder.ts @@ -1,4 +1,4 @@ -import { Effects } from "../types" +import { Address, Effects } from "../types" import { AddressReceipt } from "./AddressReceipt" import { Host } from "./Host" import { Origin } from "./Origin" @@ -36,7 +36,9 @@ export class NetworkInterfaceBuilder { * @param addresses * @returns */ - async export(origins: Iterable>) { + async export[]>( + origins: Origins, + ): Promise { const { name, description, id, ui, username, path, search } = this.options const addresses = Array.from(origins).map((o) => @@ -51,6 +53,6 @@ export class NetworkInterfaceBuilder { ui, }) - return {} as AddressReceipt + return addresses as Address[] & AddressReceipt } } diff --git a/lib/mainFn/Origin.ts b/lib/interfaces/Origin.ts similarity index 100% rename from lib/mainFn/Origin.ts rename to lib/interfaces/Origin.ts diff --git a/lib/mainFn/interfaceReceipt.ts b/lib/interfaces/interfaceReceipt.ts similarity index 100% rename from lib/mainFn/interfaceReceipt.ts rename to lib/interfaces/interfaceReceipt.ts diff --git a/lib/interfaces/setupInterfaces.ts b/lib/interfaces/setupInterfaces.ts new file mode 100644 index 0000000..e83b642 --- /dev/null +++ b/lib/interfaces/setupInterfaces.ts @@ -0,0 +1,27 @@ +import { Config } from "../config/builder/config" +import { Address, Effects } from "../types" +import { Utils } from "../util/utils" +import { AddressReceipt } from "./AddressReceipt" + +export type SetInterfacesReceipt = Record +export type SetInterfaces< + Store, + Vault, + ConfigInput extends Record, + Output extends Record, +> = (opts: { + effects: Effects + input: null | ConfigInput + utils: Utils +}) => Promise +export type SetupInterfaces = < + Store, + Vault, + ConfigInput extends Record, + Output extends Record, +>( + config: Config, + fn: SetInterfaces, +) => SetInterfaces +export const NO_INTERFACE_CHANGES = {} as SetInterfacesReceipt +export const setupInterfaces: SetupInterfaces = (_config, fn) => fn diff --git a/lib/mainFn/Daemons.ts b/lib/mainFn/Daemons.ts index 7abbc97..b89ab30 100644 --- a/lib/mainFn/Daemons.ts +++ b/lib/mainFn/Daemons.ts @@ -4,7 +4,6 @@ import { Trigger } from "../trigger" import { TriggerInput } from "../trigger/TriggerInput" import { defaultTrigger } from "../trigger/defaultTrigger" import { DaemonReturned, Effects, ValidIfNoStupidEscape } from "../types" -import { InterfaceReceipt } from "./interfaceReceipt" type Daemon = { id: "" extends Id ? never : Id command: ValidIfNoStupidEscape | [string, ...string[]] diff --git a/lib/mainFn/NetworkBuilder.ts b/lib/mainFn/NetworkBuilder.ts deleted file mode 100644 index 4e49b72..0000000 --- a/lib/mainFn/NetworkBuilder.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Effects } from "../types" -import { TorHostname } from "./TorHostname" - -export class NetworkBuilder { - static of(effects: Effects) { - return new NetworkBuilder(effects) - } - private constructor(private effects: Effects) {} - - getTorHostName(id: string) { - return new TorHostname(this.effects, id) - } -} diff --git a/lib/mainFn/ReadyProof.ts b/lib/mainFn/ReadyProof.ts deleted file mode 100644 index 7fa4631..0000000 --- a/lib/mainFn/ReadyProof.ts +++ /dev/null @@ -1,4 +0,0 @@ -export declare const ReadyProof: unique symbol -export type ReadyReceipt = { - [ReadyProof]: never -} diff --git a/lib/mainFn/RunningMainRet.ts b/lib/mainFn/RunningMainRet.ts deleted file mode 100644 index 69cf1ee..0000000 --- a/lib/mainFn/RunningMainRet.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Daemon } from "../types" -import { ReadyProof } from "./ReadyProof" - -export type RunningMainRet = { - [ReadyProof]: never - daemon: Daemon -} diff --git a/lib/mainFn/TorBinding.ts b/lib/mainFn/TorBinding.ts deleted file mode 100644 index 2821028..0000000 --- a/lib/mainFn/TorBinding.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Origin } from "./Origin" - -export class TorBinding { - constructor(readonly host: string) {} - createOrigin(protocol: string | null) { - return new Origin(protocol, this.host) - } -} diff --git a/lib/mainFn/TorHostname.ts b/lib/mainFn/TorHostname.ts deleted file mode 100644 index ef8b068..0000000 --- a/lib/mainFn/TorHostname.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Effects } from "../types" -import { TorBinding } from "./TorBinding" - -export class TorHostname { - constructor(readonly effects: Effects, readonly id: string) {} - static of(effects: Effects, id: string) { - return new TorHostname(effects, id) - } - async bindTor(internalPort: number, externalPort: number) { - const address = await this.effects.bindTor({ - internalPort, - name: this.id, - externalPort, - }) - return new TorBinding(address) - } -} diff --git a/lib/mainFn/exportInterfaces.ts b/lib/mainFn/exportInterfaces.ts deleted file mode 100644 index d382179..0000000 --- a/lib/mainFn/exportInterfaces.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { AddressReceipt } from "./AddressReceipt" -import { InterfaceReceipt } from "./interfaceReceipt" - -/** - * Takes a list of addressReceipts - * - * Returns an interfaceReceipt, which is needed to create the service daemons - */ -export const exportInterfaces = ( - _firstProof: AddressReceipt, - ..._rest: AddressReceipt[] -) => ({} as InterfaceReceipt) -export default exportInterfaces diff --git a/lib/mainFn/index.ts b/lib/mainFn/index.ts index 833107e..4eefe0f 100644 --- a/lib/mainFn/index.ts +++ b/lib/mainFn/index.ts @@ -2,14 +2,8 @@ import { Effects, ExpectedExports } from "../types" import { createMainUtils } from "../util" import { Utils, utils } from "../util/utils" import { Daemons } from "./Daemons" -import "./exportInterfaces" -import "./LocalBinding" -import "./LocalPort" -import "./NetworkBuilder" -import "./NetworkInterfaceBuilder" -import "./Origin" -import "./TorBinding" -import "./TorHostname" +import "../interfaces/NetworkInterfaceBuilder" +import "../interfaces/Origin" import "./Daemons" diff --git a/lib/test/host.test.ts b/lib/test/host.test.ts new file mode 100644 index 0000000..6e036a1 --- /dev/null +++ b/lib/test/host.test.ts @@ -0,0 +1,27 @@ +import { MultiHost } from "../interfaces/Host" +import { NetworkInterfaceBuilder } from "../interfaces/NetworkInterfaceBuilder" +import { Effects } from "../types" +import { createUtils } from "../util" +import { sdk } from "./output.sdk" + +describe("host", () => { + test("Testing that the types work", () => { + async function test(effects: Effects) { + const utils = createUtils(effects) + const foo = utils.host.multi({ id: "foo" }) + const fooOrigin = await foo.bindPort(80, { protocol: "http" as const }) + const fooInterface = new NetworkInterfaceBuilder({ + effects, + name: "Foo", + id: "foo", + description: "A Foo", + ui: true, + username: "bar", + path: "/baz", + search: { qux: "yes" }, + }) + + await fooInterface.export([fooOrigin]) + } + }) +}) diff --git a/lib/types.ts b/lib/types.ts index 3d131d3..4f26d88 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,7 +1,7 @@ export * as configTypes from "./config/configTypes" import { InputSpec } from "./config/configTypes" import { DependenciesReceipt } from "./config/setupConfig" -import { PortOptions } from "./mainFn/Host" +import { PortOptions } from "./interfaces/Host" export type ExportedAction = (options: { effects: Effects diff --git a/lib/util/utils.ts b/lib/util/utils.ts index 696a80c..6222cef 100644 --- a/lib/util/utils.ts +++ b/lib/util/utils.ts @@ -8,10 +8,6 @@ import { } from "../health/checkFns" import { ExtractStore } from "../types" import { GetSystemSmtp } from "./GetSystemSmtp" -import { LocalBinding } from "../mainFn/LocalBinding" -import { LocalPort } from "../mainFn/LocalPort" -import { NetworkBuilder } from "../mainFn/NetworkBuilder" -import { TorHostname } from "../mainFn/TorHostname" import { DefaultString } from "../config/configTypes" import { getDefaultString } from "./getDefaultString" import { GetStore, getStore } from "../store/getStore" @@ -26,36 +22,9 @@ import { NamedPath, Path, } from "../dependency/setupDependencyMounts" +import { Host, MultiHost, SingleHost, StaticHost } from "../interfaces/Host" export type Utils = { - createOrUpdateVault: (opts: { - key: string - value: string | null | undefined - generator: DefaultString - }) => Promise - readFile: (fileHelper: FileHelper) => ReturnType["read"]> - writeFile: ( - fileHelper: FileHelper, - data: A, - ) => ReturnType["write"]> - getSystemSmtp: () => GetSystemSmtp & WrapperOverWrite - store: { - get: ( - packageId: string, - path: T.EnsureStorePath, - ) => GetStore & WrapperOverWrite - getOwn: ( - path: T.EnsureStorePath, - ) => GetStore & WrapperOverWrite - setOwn: ( - path: T.EnsureStorePath, - value: ExtractStore, - ) => Promise - } - vault: { - get: (key: keyof Vault & string) => GetVault & WrapperOverWrite - set: (key: keyof Vault & string, value: string) => Promise - } checkPortListening( port: number, options: { @@ -73,10 +42,18 @@ export type Utils = { errorMessage?: string }, ): Promise - bindLan: (port: number) => Promise - networkBuilder: () => NetworkBuilder - torHostName: (id: string) => TorHostname - nullIfEmpty: typeof nullIfEmpty + createOrUpdateVault: (opts: { + key: string + value: string | null | undefined + generator: DefaultString + }) => Promise + getSystemSmtp: () => GetSystemSmtp & WrapperOverWrite + host: { + of: (options: { kind: "static" | "single" | "multi"; id: string }) => Host + static: (options: { id: string }) => StaticHost + single: (options: { id: string }) => SingleHost + multi: (options: { id: string }) => MultiHost + } mountDependencies: < In extends | Record>> @@ -86,6 +63,29 @@ export type Utils = { >( value: In, ) => Promise> + nullIfEmpty: typeof nullIfEmpty + readFile: (fileHelper: FileHelper) => ReturnType["read"]> + store: { + get: ( + packageId: string, + path: T.EnsureStorePath, + ) => GetStore & WrapperOverWrite + getOwn: ( + path: T.EnsureStorePath, + ) => GetStore & WrapperOverWrite + setOwn: ( + path: T.EnsureStorePath, + value: ExtractStore, + ) => Promise + } + vault: { + get: (key: keyof Vault & string) => GetVault & WrapperOverWrite + set: (key: keyof Vault & string, value: string) => Promise + } + writeFile: ( + fileHelper: FileHelper, + data: A, + ) => ReturnType["write"]> } export const utils = < Store = never, @@ -116,6 +116,16 @@ export const utils = < }, getSystemSmtp: () => new GetSystemSmtp(effects) as GetSystemSmtp & WrapperOverWrite, + + host: { + of: (options: { kind: "static" | "single" | "multi"; id: string }) => + new Host({ ...options, effects }), + static: (options: { id: string }) => + new StaticHost({ ...options, effects }), + single: (options: { id: string }) => + new SingleHost({ ...options, effects }), + multi: (options: { id: string }) => new MultiHost({ ...options, effects }), + }, readFile: (fileHelper: FileHelper) => fileHelper.read(effects), writeFile: (fileHelper: FileHelper, data: A) => fileHelper.write(data, effects), @@ -137,10 +147,6 @@ export const utils = < }, checkPortListening: checkPortListening.bind(null, effects), checkWebUrl: checkWebUrl.bind(null, effects), - bindLan: async (port: number) => LocalPort.bindLan(effects, port), - networkBuilder: () => NetworkBuilder.of(effects), - torHostName: (id: string) => TorHostname.of(effects, id), - vault: { get: (key: keyof Vault & string) => getVault(effects, key) as GetVault & WrapperOverWrite,