import FileHelper from "./fileHelper" import nullIfEmpty from "./nullIfEmpty" import { CheckResult, checkPortListening, checkWebUrl, } from "../health/checkFns" import { Effects, EnsureStorePath, ExtractStore, InterfaceId, PackageId, } from "../types" import { GetSystemSmtp } from "./GetSystemSmtp" import { DefaultString } from "../config/configTypes" import { getDefaultString } from "./getDefaultString" import { GetStore, getStore } from "../store/getStore" import { GetVault, getVault } from "./getVault" import { MountDependenciesOut, mountDependencies, } from "../dependency/mountDependencies" import { ManifestId, VolumeName, NamedPath, Path, } from "../dependency/setupDependencyMounts" import { Host, MultiHost, SingleHost, StaticHost } from "../interfaces/Host" import { NetworkInterfaceBuilder } from "../interfaces/NetworkInterfaceBuilder" import { GetNetworkInterface, getNetworkInterface } from "./getNetworkInterface" import { GetNetworkInterfaces, getNetworkInterfaces, } from "./getNetworkInterfaces" export type Utils = { checkPortListening( port: number, options: { errorMessage: string successMessage: string timeoutMessage?: string timeout?: number }, ): Promise checkWebUrl( url: string, options?: { timeout?: number successMessage?: string errorMessage?: string }, ): Promise createInterface: (options: { name: string id: string description: string ui: boolean username: null | string path: string search: Record }) => NetworkInterfaceBuilder 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: (id: string) => StaticHost single: (id: string) => SingleHost multi: (id: string) => MultiHost } mountDependencies: < In extends | Record>> | Record> | Record | Path, >( value: In, ) => Promise> networkInterface: { getOwn: (interfaceId: InterfaceId) => GetNetworkInterface & WrapperOverWrite get: (opts: { interfaceId: InterfaceId packageId: PackageId }) => GetNetworkInterface & WrapperOverWrite getAllOwn: () => GetNetworkInterfaces & WrapperOverWrite getAll: (opts: { packageId: PackageId }) => GetNetworkInterfaces & WrapperOverWrite } nullIfEmpty: typeof nullIfEmpty readFile: (fileHelper: FileHelper) => ReturnType["read"]> store: { get: ( packageId: string, path: EnsureStorePath, ) => GetStore & WrapperOverWrite getOwn: ( path: EnsureStorePath, ) => GetStore & WrapperOverWrite setOwn: ( path: 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, Vault = never, WrapperOverWrite = { const: never }, >( effects: Effects, ): Utils => ({ createOrUpdateVault: async ({ key, value, generator, }: { key: string value: string | null | undefined generator: DefaultString }) => { if (value) { await effects.vault.set({ key, value }) return value } if (await effects.vault.get({ key, callback: noop })) { return null } const newValue = getDefaultString(generator) await effects.vault.set({ key, value: newValue }) return newValue }, createInterface: (options: { name: string id: string description: string ui: boolean username: null | string path: string search: Record }) => new NetworkInterfaceBuilder({ ...options, effects }), getSystemSmtp: () => new GetSystemSmtp(effects) as GetSystemSmtp & WrapperOverWrite, host: { of: (options: { kind: "static" | "single" | "multi"; id: string }) => new Host({ ...options, effects }), static: (id: string) => new StaticHost({ id, effects }), single: (id: string) => new SingleHost({ id, effects }), multi: (id: string) => new MultiHost({ id, effects }), }, readFile: (fileHelper: FileHelper) => fileHelper.read(effects), writeFile: (fileHelper: FileHelper, data: A) => fileHelper.write(data, effects), nullIfEmpty, networkInterface: { getOwn: (interfaceId: InterfaceId) => getNetworkInterface(effects, { interfaceId }) as GetNetworkInterface & WrapperOverWrite, get: (opts: { interfaceId: InterfaceId; packageId: PackageId }) => getNetworkInterface(effects, opts) as GetNetworkInterface & WrapperOverWrite, getAllOwn: () => getNetworkInterfaces(effects, {}) as GetNetworkInterfaces & WrapperOverWrite, getAll: (opts: { packageId: PackageId }) => getNetworkInterfaces(effects, opts) as GetNetworkInterfaces & WrapperOverWrite, }, store: { get: ( packageId: string, path: EnsureStorePath, ) => getStore(effects, path as any, { packageId, }) as any, getOwn: (path: EnsureStorePath) => getStore(effects, path as any) as any, setOwn: ( path: EnsureStorePath, value: ExtractStore, ) => effects.store.set({ value, path: path as any }), }, checkPortListening: checkPortListening.bind(null, effects), checkWebUrl: checkWebUrl.bind(null, effects), vault: { get: (key: keyof Vault & string) => getVault(effects, key) as GetVault & WrapperOverWrite, set: (key: keyof Vault & string, value: string) => effects.vault.set({ key, value }), }, mountDependencies: < In extends | Record>> | Record> | Record | Path, >( value: In, ) => mountDependencies(effects, value), }) function noop(): void {}