diff --git a/compat/migrations.ts b/compat/migrations.ts index fac5034..98260a3 100644 --- a/compat/migrations.ts +++ b/compat/migrations.ts @@ -16,14 +16,11 @@ export interface NoRepeat { * @param noFail (optional, default:false) whether or not to fail the migration if fn throws an error * @returns a migraion function */ -export function updateConfig< - version extends string, - type extends "up" | "down", - >( - fn: (config: T.Config, effects: T.Effects) => T.Config | Promise, - configured: boolean, - noRepeat?: NoRepeat, - noFail = false, +export function updateConfig( + fn: (config: T.Config, effects: T.Effects) => T.Config | Promise, + configured: boolean, + noRepeat?: NoRepeat, + noFail = false ): M.MigrationFn { return M.migrationFn(async (effects: T.Effects) => { await noRepeatGuard(effects, noRepeat, async () => { @@ -45,20 +42,15 @@ export function updateConfig< }); } -export async function noRepeatGuard< - version extends string, - type extends "up" | "down", - >( - effects: T.Effects, - noRepeat: NoRepeat | undefined, - fn: () => Promise, +export async function noRepeatGuard( + effects: T.Effects, + noRepeat: NoRepeat | undefined, + fn: () => Promise ): Promise { if (!noRepeat) { return fn(); } - if ( - !await util.exists(effects, { path: "start9/migrations", volumeId: "main" }) - ) { + if (!(await util.exists(effects, { path: "start9/migrations", volumeId: "main" }))) { await effects.createDir({ path: "start9/migrations", volumeId: "main" }); } const migrationPath = { @@ -66,7 +58,7 @@ export async function noRepeatGuard< volumeId: "main", }; if (noRepeat.type === "up") { - if (!await util.exists(effects, migrationPath)) { + if (!(await util.exists(effects, migrationPath))) { await fn(); await effects.writeFile({ ...migrationPath, toWrite: "" }); } @@ -81,11 +73,9 @@ export async function noRepeatGuard< export async function initNoRepeat( effects: T.Effects, migrations: M.MigrationMapping, - startingVersion: string, + startingVersion: string ) { - if ( - !await util.exists(effects, { path: "start9/migrations", volumeId: "main" }) - ) { + if (!(await util.exists(effects, { path: "start9/migrations", volumeId: "main" }))) { const starting = EmVer.parse(startingVersion); await effects.createDir({ path: "start9/migrations", volumeId: "main" }); for (const version in migrations) { @@ -103,19 +93,11 @@ export async function initNoRepeat( export function fromMapping( migrations: M.MigrationMapping, - currentVersion: string, + currentVersion: string ): T.ExpectedExports.migration { const inner = M.fromMapping(migrations, currentVersion); - return async ( - effects: T.Effects, - version: string, - direction?: unknown, - ) => { - await initNoRepeat( - effects, - migrations, - direction === "from" ? version : currentVersion, - ); + return async (effects: T.Effects, version: string, direction?: unknown) => { + await initNoRepeat(effects, migrations, direction === "from" ? version : currentVersion); return inner(effects, version, direction); }; } diff --git a/types.ts b/types.ts index 65479eb..8dfe804 100644 --- a/types.ts +++ b/types.ts @@ -1,10 +1,8 @@ // deno-lint-ignore no-namespace export namespace ExpectedExports { + version: 2; /** Set configuration is called after we have modified and saved the configuration in the embassy ui. Use this to make a file for the docker to read from for configuration. */ - export type setConfig = ( - effects: Effects, - input: Config, - ) => Promise>; + export type setConfig = (effects: Effects, input: Config) => Promise>; /** Get configuration returns a shape that describes the format that the embassy ui will generate, and later send to the set config */ export type getConfig = (effects: Effects) => Promise>; /** These are how we make sure the our dependency configurations are valid and if not how to fix them. */ @@ -12,31 +10,17 @@ export namespace ExpectedExports { /** For backing up service data though the embassyOS UI */ export type createBackup = (effects: Effects) => Promise>; /** For restoring service data that was previously backed up using the embassyOS UI create backup flow. Backup restores are also triggered via the embassyOS UI, or doing a system restore flow during setup. */ - export type restoreBackup = ( - effects: Effects, - ) => Promise>; + export type restoreBackup = (effects: Effects) => Promise>; /** Properties are used to get values from the docker, like a username + password, what ports we are hosting from */ - export type properties = ( - effects: Effects, - ) => Promise>; + export type properties = (effects: Effects) => Promise>; export type health = { /** Should be the health check id */ - [id: string]: ( - effects: Effects, - dateMs: number, - ) => Promise>; + [id: string]: (effects: Effects, dateMs: number) => Promise>; }; - export type migration = ( - effects: Effects, - version: string, - ...args: unknown[] - ) => Promise>; + export type migration = (effects: Effects, version: string, ...args: unknown[]) => Promise>; export type action = { - [id: string]: ( - effects: Effects, - config?: Config, - ) => Promise>; + [id: string]: (effects: Effects, config?: Config) => Promise>; }; /** @@ -49,35 +33,32 @@ export namespace ExpectedExports { /** Used to reach out from the pure js runtime */ export type Effects = { /** Usable when not sandboxed */ - writeFile( - input: { path: string; volumeId: string; toWrite: string }, - ): Promise; + writeFile(input: { path: string; volumeId: string; toWrite: string }): Promise; readFile(input: { volumeId: string; path: string }): Promise; metadata(input: { volumeId: string; path: string }): Promise; /** Create a directory. Usable when not sandboxed */ createDir(input: { volumeId: string; path: string }): Promise; + + readDir(input: { volumeId: string; path: string }): Promise; /** Remove a directory. Usable when not sandboxed */ removeDir(input: { volumeId: string; path: string }): Promise; removeFile(input: { volumeId: string; path: string }): Promise; /** Write a json file into an object. Usable when not sandboxed */ - writeJsonFile( - input: { volumeId: string; path: string; toWrite: Record }, - ): Promise; + writeJsonFile(input: { volumeId: string; path: string; toWrite: Record }): Promise; /** Read a json file into an object */ - readJsonFile( - input: { volumeId: string; path: string }, - ): Promise>; + readJsonFile(input: { volumeId: string; path: string }): Promise>; - runCommand( - input: { command: string; args?: string[]; timeoutMillis?: number }, - ): Promise>; + runCommand(input: { command: string; args?: string[]; timeoutMillis?: number }): Promise>; runDaemon(input: { command: string; args?: string[] }): { wait(): Promise>; term(): Promise; }; + chown(input: { volumeId: string; path: string; uid: string }): Promise; + chmod(input: { volumeId: string; path: string; mode: string }): Promise; + sleep(timeMs: number): Promise; /** Log at the trace level */ @@ -95,6 +76,8 @@ export type Effects = { is_sandboxed(): boolean; exists(input: { volumeId: string; path: string }): Promise; + bindLocal(options: { internalPort: number; name: string; externalPort: number }): Promise; + bindTor(options: { internalPort: number; name: string; externalPort: number }): Promise; fetch( url: string, @@ -102,7 +85,7 @@ export type Effects = { method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH"; headers?: Record; body?: string; - }, + } ): Promise<{ method: string; ok: boolean; @@ -217,8 +200,8 @@ export type Target = V & { export type UniqueBy = | { - any: UniqueBy[]; - } + any: UniqueBy[]; + } | string | null; @@ -228,19 +211,19 @@ export type WithNullable = T & { export type DefaultString = | string | { - /** The chars available for the random generation */ - charset?: string; - /** Length that we generate to */ - len: number; - }; + /** The chars available for the random generation */ + charset?: string; + /** Length that we generate to */ + len: number; + }; export type ValueSpecString = // deno-lint-ignore ban-types ( | {} | { - pattern: string; - "pattern-description": string; - } + pattern: string; + "pattern-description": string; + } ) & { copyable?: boolean; masked?: boolean; @@ -257,71 +240,63 @@ export type ValueSpecNumber = { export type ValueSpecBoolean = Record; export type ValueSpecAny = | Tag<"boolean", WithDescription>> + | Tag<"string", WithDescription, DefaultString>>> + | Tag<"number", WithDescription, number>>> | Tag< - "string", - WithDescription< - WithNullableDefault, DefaultString> - > - > - | Tag< - "number", - WithDescription, number>> - > - | Tag< - "enum", - WithDescription< - WithDefault< - { - values: readonly string[] | string[]; - "value-names": { - [key: string]: string; - }; - }, - string + "enum", + WithDescription< + WithDefault< + { + values: readonly string[] | string[]; + "value-names": { + [key: string]: string; + }; + }, + string + > > > - > | Tag<"list", ValueSpecList> | Tag<"object", WithDescription>> | Tag<"union", WithOptionalDescription>> | Tag< - "pointer", - WithDescription< - | Subtype< - "package", - | Target< - "tor-key", - { - "package-id": string; - interface: string; - } - > - | Target< - "tor-address", - { - "package-id": string; - interface: string; - } - > - | Target< - "lan-address", - { - "package-id": string; - interface: string; - } - > - | Target< - "config", - { - "package-id": string; - selector: string; - multi: boolean; - } - > + "pointer", + WithDescription< + | Subtype< + "package", + | Target< + "tor-key", + { + "package-id": string; + interface: string; + } + > + | Target< + "tor-address", + { + "package-id": string; + interface: string; + } + > + | Target< + "lan-address", + { + "package-id": string; + interface: string; + } + > + | Target< + "config", + { + "package-id": string; + selector: string; + multi: boolean; + } + > + > + | Subtype<"system", Record> > - | Subtype<"system", Record> - > - >; + >; export type ValueSpecUnion = { /** What tag for the specification, for tag unions */ tag: { @@ -345,32 +320,12 @@ export type ValueSpecObject = { "unique-by"?: UniqueBy; }; export type ValueSpecList = - | Subtype< - "boolean", - WithDescription, boolean[]>> - > - | Subtype< - "string", - WithDescription, string[]>> - > - | Subtype< - "number", - WithDescription, number[]>> - > - | Subtype< - "enum", - WithDescription, string[]>> - > - | Subtype< - "object", - WithDescription< - WithNullableDefault, Record[]> - > - > - | Subtype< - "union", - WithDescription, string[]>> - >; + | Subtype<"boolean", WithDescription, boolean[]>>> + | Subtype<"string", WithDescription, string[]>>> + | Subtype<"number", WithDescription, number[]>>> + | Subtype<"enum", WithDescription, string[]>>> + | Subtype<"object", WithDescription, Record[]>>> + | Subtype<"union", WithDescription, string[]>>>; export type ValueSpecEnum = { values: string[]; "value-names": { [key: string]: string }; @@ -422,8 +377,8 @@ export type DependsOn = { export type KnownError = | { error: string } | { - "error-code": [number, string] | readonly [number, string]; - }; + "error-code": [number, string] | readonly [number, string]; + }; export type ResultType = KnownError | { result: T }; export type PackagePropertiesV2 = {