mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 04:23:40 +00:00
Refactor/actions (#2733)
* store, properties, manifest * interfaces * init and backups * fix init and backups * file models * more versions * dependencies * config except dynamic types * clean up config * remove disabled from non-dynamic vaues * actions * standardize example code block formats * wip: actions refactor Co-authored-by: Jade <Blu-J@users.noreply.github.com> * commit types * fix types * update types * update action request type * update apis * add description to actionrequest * clean up imports * revert package json * chore: Remove the recursive to the index * chore: Remove the other thing I was testing * flatten action requests * update container runtime with new config paradigm * new actions strategy * seems to be working * misc backend fixes * fix fe bugs * only show breakages if breakages * only show success modal if result * don't panic on failed removal * hide config from actions page * polyfill autoconfig * use metadata strategy for actions instead of prev * misc fixes * chore: split the sdk into 2 libs (#2736) * follow sideload progress (#2718) * follow sideload progress * small bugfix * shareReplay with no refcount false * don't wrap sideload progress in RPCResult * dont present toast --------- Co-authored-by: Aiden McClelland <me@drbonez.dev> * chore: Add the initial of the creation of the two sdk * chore: Add in the baseDist * chore: Add in the baseDist * chore: Get the web and the runtime-container running * chore: Remove the empty file * chore: Fix it so the container-runtime works --------- Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com> Co-authored-by: Aiden McClelland <me@drbonez.dev> * misc fixes * update todos * minor clean up * fix link script * update node version in CI test * fix node version syntax in ci build * wip: fixing callbacks * fix sdk makefile dependencies * add support for const outside of main * update apis * don't panic! * Chore: Capture weird case on rpc, and log that * fix procedure id issue * pass input value for dep auto config * handle disabled and warning for actions * chore: Fix for link not having node_modules * sdk fixes * fix build * fix build * fix build --------- Co-authored-by: Matt Hill <mattnine@protonmail.com> Co-authored-by: Jade <Blu-J@users.noreply.github.com> Co-authored-by: J H <dragondef@gmail.com> Co-authored-by: Jade <2364004+Blu-J@users.noreply.github.com> Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com>
This commit is contained in:
203
sdk/package/lib/version/VersionGraph.ts
Normal file
203
sdk/package/lib/version/VersionGraph.ts
Normal file
@@ -0,0 +1,203 @@
|
||||
import { ExtendedVersion, VersionRange } from "../../../base/lib/exver"
|
||||
import * as T from "../../../base/lib/types"
|
||||
import { Graph, Vertex, once } from "../util"
|
||||
import { IMPOSSIBLE, VersionInfo } from "./VersionInfo"
|
||||
|
||||
export class VersionGraph<CurrentVersion extends string> {
|
||||
private readonly graph: () => Graph<
|
||||
ExtendedVersion | VersionRange,
|
||||
((opts: { effects: T.Effects }) => Promise<void>) | undefined
|
||||
>
|
||||
private constructor(
|
||||
readonly current: VersionInfo<CurrentVersion>,
|
||||
versions: Array<VersionInfo<any>>,
|
||||
) {
|
||||
this.graph = once(() => {
|
||||
const graph = new Graph<
|
||||
ExtendedVersion | VersionRange,
|
||||
((opts: { effects: T.Effects }) => Promise<void>) | undefined
|
||||
>()
|
||||
const flavorMap: Record<
|
||||
string,
|
||||
[
|
||||
ExtendedVersion,
|
||||
VersionInfo<any>,
|
||||
Vertex<
|
||||
ExtendedVersion | VersionRange,
|
||||
((opts: { effects: T.Effects }) => Promise<void>) | undefined
|
||||
>,
|
||||
][]
|
||||
> = {}
|
||||
for (let version of [current, ...versions]) {
|
||||
const v = ExtendedVersion.parse(version.options.version)
|
||||
const vertex = graph.addVertex(v, [], [])
|
||||
const flavor = v.flavor || ""
|
||||
if (!flavorMap[flavor]) {
|
||||
flavorMap[flavor] = []
|
||||
}
|
||||
flavorMap[flavor].push([v, version, vertex])
|
||||
}
|
||||
for (let flavor in flavorMap) {
|
||||
flavorMap[flavor].sort((a, b) => a[0].compareForSort(b[0]))
|
||||
let prev:
|
||||
| [
|
||||
ExtendedVersion,
|
||||
VersionInfo<any>,
|
||||
Vertex<
|
||||
ExtendedVersion | VersionRange,
|
||||
(opts: { effects: T.Effects }) => Promise<void>
|
||||
>,
|
||||
]
|
||||
| undefined = undefined
|
||||
for (let [v, version, vertex] of flavorMap[flavor]) {
|
||||
if (version.options.migrations.up !== IMPOSSIBLE) {
|
||||
let range
|
||||
if (prev) {
|
||||
graph.addEdge(version.options.migrations.up, prev[2], vertex)
|
||||
range = VersionRange.anchor(">=", prev[0]).and(
|
||||
VersionRange.anchor("<", v),
|
||||
)
|
||||
} else {
|
||||
range = VersionRange.anchor("<", v)
|
||||
}
|
||||
const vRange = graph.addVertex(range, [], [])
|
||||
graph.addEdge(version.options.migrations.up, vRange, vertex)
|
||||
}
|
||||
|
||||
if (version.options.migrations.down !== IMPOSSIBLE) {
|
||||
let range
|
||||
if (prev) {
|
||||
graph.addEdge(version.options.migrations.down, vertex, prev[2])
|
||||
range = VersionRange.anchor(">=", prev[0]).and(
|
||||
VersionRange.anchor("<", v),
|
||||
)
|
||||
} else {
|
||||
range = VersionRange.anchor("<", v)
|
||||
}
|
||||
const vRange = graph.addVertex(range, [], [])
|
||||
graph.addEdge(version.options.migrations.down, vertex, vRange)
|
||||
}
|
||||
|
||||
if (version.options.migrations.other) {
|
||||
for (let rangeStr in version.options.migrations.other) {
|
||||
const range = VersionRange.parse(rangeStr)
|
||||
const vRange = graph.addVertex(range, [], [])
|
||||
graph.addEdge(
|
||||
version.options.migrations.other[rangeStr],
|
||||
vRange,
|
||||
vertex,
|
||||
)
|
||||
for (let matching of graph.findVertex(
|
||||
(v) =>
|
||||
v.metadata instanceof ExtendedVersion &&
|
||||
v.metadata.satisfies(range),
|
||||
)) {
|
||||
graph.addEdge(
|
||||
version.options.migrations.other[rangeStr],
|
||||
matching,
|
||||
vertex,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return graph
|
||||
})
|
||||
}
|
||||
currentVersion = once(() =>
|
||||
ExtendedVersion.parse(this.current.options.version),
|
||||
)
|
||||
/**
|
||||
* Each exported `VersionInfo.of()` should be imported and provided as an argument to this function.
|
||||
*
|
||||
* ** The current version must be the FIRST argument. **
|
||||
*/
|
||||
static of<
|
||||
CurrentVersion extends string,
|
||||
OtherVersions extends Array<VersionInfo<any>>,
|
||||
>(
|
||||
currentVersion: VersionInfo<CurrentVersion>,
|
||||
...other: EnsureUniqueId<OtherVersions, OtherVersions, CurrentVersion>
|
||||
) {
|
||||
return new VersionGraph(currentVersion, other as Array<VersionInfo<any>>)
|
||||
}
|
||||
async migrate({
|
||||
effects,
|
||||
from,
|
||||
to,
|
||||
}: {
|
||||
effects: T.Effects
|
||||
from: ExtendedVersion
|
||||
to: ExtendedVersion
|
||||
}) {
|
||||
const graph = this.graph()
|
||||
if (from && to) {
|
||||
const path = graph.shortestPath(
|
||||
(v) =>
|
||||
(v.metadata instanceof VersionRange &&
|
||||
v.metadata.satisfiedBy(from)) ||
|
||||
(v.metadata instanceof ExtendedVersion && v.metadata.equals(from)),
|
||||
(v) =>
|
||||
(v.metadata instanceof VersionRange && v.metadata.satisfiedBy(to)) ||
|
||||
(v.metadata instanceof ExtendedVersion && v.metadata.equals(to)),
|
||||
)
|
||||
if (path) {
|
||||
for (let edge of path) {
|
||||
if (edge.metadata) {
|
||||
await edge.metadata({ effects })
|
||||
}
|
||||
await effects.setDataVersion({ version: edge.to.metadata.toString() })
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
throw new Error()
|
||||
}
|
||||
canMigrateFrom = once(() =>
|
||||
Array.from(
|
||||
this.graph().reverseBreadthFirstSearch(
|
||||
(v) =>
|
||||
(v.metadata instanceof VersionRange &&
|
||||
v.metadata.satisfiedBy(this.currentVersion())) ||
|
||||
(v.metadata instanceof ExtendedVersion &&
|
||||
v.metadata.equals(this.currentVersion())),
|
||||
),
|
||||
).reduce(
|
||||
(acc, x) =>
|
||||
acc.or(
|
||||
x.metadata instanceof VersionRange
|
||||
? x.metadata
|
||||
: VersionRange.anchor("=", x.metadata),
|
||||
),
|
||||
VersionRange.none(),
|
||||
),
|
||||
)
|
||||
canMigrateTo = once(() =>
|
||||
Array.from(
|
||||
this.graph().breadthFirstSearch(
|
||||
(v) =>
|
||||
(v.metadata instanceof VersionRange &&
|
||||
v.metadata.satisfiedBy(this.currentVersion())) ||
|
||||
(v.metadata instanceof ExtendedVersion &&
|
||||
v.metadata.equals(this.currentVersion())),
|
||||
),
|
||||
).reduce(
|
||||
(acc, x) =>
|
||||
acc.or(
|
||||
x.metadata instanceof VersionRange
|
||||
? x.metadata
|
||||
: VersionRange.anchor("=", x.metadata),
|
||||
),
|
||||
VersionRange.none(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
export type EnsureUniqueId<A, B = A, OtherVersions = never> =
|
||||
B extends [] ? A :
|
||||
B extends [VersionInfo<infer Version>, ...infer Rest] ? (
|
||||
Version extends OtherVersions ? "One or more versions are not unique"[] :
|
||||
EnsureUniqueId<A, Rest, Version | OtherVersions>
|
||||
) : "There exists a migration that is not a Migration"[]
|
||||
83
sdk/package/lib/version/VersionInfo.ts
Normal file
83
sdk/package/lib/version/VersionInfo.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { ValidateExVer } from "../../../base/lib/exver"
|
||||
import * as T from "../../../base/lib/types"
|
||||
|
||||
export const IMPOSSIBLE = Symbol("IMPOSSIBLE")
|
||||
|
||||
export type VersionOptions<Version extends string> = {
|
||||
/** The exver-compliant version number */
|
||||
version: Version & ValidateExVer<Version>
|
||||
/** The release notes for this version */
|
||||
releaseNotes: string
|
||||
/** Data migrations for this version */
|
||||
migrations: {
|
||||
/**
|
||||
* A migration from the previous version. Leave empty to indicate no migration is necessary.
|
||||
* Set to `IMPOSSIBLE` to indicate migrating from the previous version is not possible.
|
||||
*/
|
||||
up?: ((opts: { effects: T.Effects }) => Promise<void>) | typeof IMPOSSIBLE
|
||||
/**
|
||||
* A migration to the previous version. Leave blank to indicate no migration is necessary.
|
||||
* Set to `IMPOSSIBLE` to indicate downgrades are prohibited
|
||||
*/
|
||||
down?: ((opts: { effects: T.Effects }) => Promise<void>) | typeof IMPOSSIBLE
|
||||
/**
|
||||
* Additional migrations, such as fast-forward migrations, or migrations from other flavors.
|
||||
*/
|
||||
other?: Record<string, (opts: { effects: T.Effects }) => Promise<void>>
|
||||
}
|
||||
}
|
||||
|
||||
export class VersionInfo<Version extends string> {
|
||||
private _version: null | Version = null
|
||||
private constructor(
|
||||
readonly options: VersionOptions<Version> & { satisfies: string[] },
|
||||
) {}
|
||||
/**
|
||||
* @description Use this function to define a new version of the service. By convention, each version should receive its own file.
|
||||
* @property {string} version
|
||||
* @property {string} releaseNotes
|
||||
* @property {object} migrations
|
||||
* @returns A VersionInfo class instance that is exported, then imported into versions/index.ts.
|
||||
*/
|
||||
static of<Version extends string>(options: VersionOptions<Version>) {
|
||||
return new VersionInfo<Version>({ ...options, satisfies: [] })
|
||||
}
|
||||
/** Specify a version that this version is 100% backwards compatible to */
|
||||
satisfies<V extends string>(
|
||||
version: V & ValidateExVer<V>,
|
||||
): VersionInfo<Version> {
|
||||
return new VersionInfo({
|
||||
...this.options,
|
||||
satisfies: [...this.options.satisfies, version],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function __type_tests() {
|
||||
const version: VersionInfo<"1.0.0:0"> = VersionInfo.of({
|
||||
version: "1.0.0:0",
|
||||
releaseNotes: "",
|
||||
migrations: {},
|
||||
})
|
||||
.satisfies("#other:1.0.0:0")
|
||||
.satisfies("#other:2.0.0:0")
|
||||
// @ts-expect-error
|
||||
.satisfies("#other:2.f.0:0")
|
||||
|
||||
let a: VersionInfo<"1.0.0:0"> = version
|
||||
// @ts-expect-error
|
||||
let b: VersionInfo<"1.0.0:3"> = version
|
||||
|
||||
VersionInfo.of({
|
||||
// @ts-expect-error
|
||||
version: "test",
|
||||
releaseNotes: "",
|
||||
migrations: {},
|
||||
})
|
||||
VersionInfo.of({
|
||||
// @ts-expect-error
|
||||
version: "test" as string,
|
||||
releaseNotes: "",
|
||||
migrations: {},
|
||||
})
|
||||
}
|
||||
2
sdk/package/lib/version/index.ts
Normal file
2
sdk/package/lib/version/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./VersionGraph"
|
||||
export * from "./VersionInfo"
|
||||
Reference in New Issue
Block a user