chore: Add the getSsl effects

This commit is contained in:
BluJ
2023-03-06 15:53:42 -07:00
parent b4fc6e891e
commit 8573a483b1
13 changed files with 90 additions and 77 deletions

View File

@@ -40,7 +40,7 @@ export class Backups {
constructor(
private options = DEFAULT_OPTIONS,
private backupSet = [] as BackupSet[],
private backupSet = [] as BackupSet[]
) {}
static volumes(...volumeNames: string[]) {
return new Backups().addSets(
@@ -49,7 +49,7 @@ export class Backups {
srcPath: "./",
dstPath: `./${srcVolume}/`,
dstVolume: Backups.BACKUP,
})),
}))
);
}
static addSets(...options: BackupSet[]) {
@@ -72,12 +72,12 @@ export class Backups {
srcPath: "./",
dstPath: `./${srcVolume}/`,
dstVolume: Backups.BACKUP,
})),
}))
);
}
addSets(...options: BackupSet[]) {
options.forEach((x) =>
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } }),
this.backupSet.push({ ...x, options: { ...this.options, ...x.options } })
);
return this;
}
@@ -98,7 +98,7 @@ export class Backups {
.map((x) => x.dstPath)
.map((x) => x.replace(/\.\/([^]*)\//, "$1"));
const filteredItems = previousItems.filter(
(x) => backupPaths.indexOf(x) === -1,
(x) => backupPaths.indexOf(x) === -1
);
for (const itemToRemove of filteredItems) {
effects.error(`Trying to remove ${itemToRemove}`);
@@ -111,7 +111,7 @@ export class Backups {
effects.removeFile({
volumeId: Backups.BACKUP,
path: itemToRemove,
}),
})
)
.catch(() => {
effects.warn(`Failed to remove ${itemToRemove} from backup volume`);

View File

@@ -489,13 +489,13 @@ export class Config<A extends InputSpec> extends IBuilder<A> {
}
static withValue<K extends string, B extends ValueSpec>(
key: K,
value: Value<B>,
value: Value<B>
) {
return Config.empty().withValue(key, value);
}
static addValue<K extends string, B extends ValueSpec>(
key: K,
value: Value<B>,
value: Value<B>
) {
return Config.empty().withValue(key, value);
}

View File

@@ -38,7 +38,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
Default<string[]> & {
range: string;
spec: StringSpec;
},
}
>(a: A) {
return new List({
type: "list" as const,
@@ -51,7 +51,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
Default<number[]> & {
range: string;
spec: NumberSpec;
},
}
>(a: A) {
return new List({
type: "list" as const,
@@ -69,7 +69,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
[key: string]: string;
};
};
},
}
>(a: A) {
return new List({
type: "list" as const,
@@ -86,7 +86,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
"display-as": null | string;
"unique-by": null | UniqueBy;
};
},
}
>(a: A) {
const { spec: previousSpec, ...rest } = a;
const { spec: previousSpecSpec, ...restSpec } = previousSpec;
@@ -127,7 +127,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
default: string;
};
},
B extends string,
B extends string
>(a: A) {
const { spec: previousSpec, ...rest } = a;
const { variants: previousVariants, ...restSpec } = previousSpec;

View File

@@ -80,7 +80,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
A extends Description &
NullableDefault<DefaultString> &
Nullable &
StringSpec,
StringSpec
>(a: A) {
return new Value({
type: "string" as const,
@@ -88,7 +88,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
} as ValueSpecString);
}
static number<
A extends Description & NullableDefault<number> & Nullable & NumberSpec,
A extends Description & NullableDefault<number> & Nullable & NumberSpec
>(a: A) {
return new Value({
type: "number" as const,
@@ -100,7 +100,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
Default<string> & {
values: readonly string[] | string[];
"value-names": Record<string, string>;
},
}
>(a: A) {
return new Value({
type: "enum" as const,
@@ -117,7 +117,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
"unique-by": null | string;
spec: Config<InputSpec>;
"value-names": Record<string, string>;
},
}
>(a: A) {
const { spec: previousSpec, ...rest } = a;
const spec = previousSpec.build() as BuilderExtract<A["spec"]>;
@@ -143,7 +143,7 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
"display-as": string | null;
"unique-by": UniqueBy;
},
B extends string,
B extends string
>(a: A) {
const { variants: previousVariants, ...rest } = a;
const variants = previousVariants.build() as BuilderExtract<A["variants"]>;

View File

@@ -39,12 +39,12 @@ import { Config } from ".";
```
*/
export class Variants<
A extends { [key: string]: InputSpec },
A extends { [key: string]: InputSpec }
> extends IBuilder<A> {
static of<
A extends {
[key: string]: Config<InputSpec>;
},
}
>(a: A) {
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
for (const key in a) {
@@ -58,7 +58,7 @@ export class Variants<
}
static withVariant<K extends string, B extends InputSpec>(
key: K,
value: Config<B>,
value: Config<B>
) {
return Variants.empty().withVariant(key, value);
}

View File

@@ -10,10 +10,10 @@ import { HealthResult } from "./healthRunner";
*/
export const checkWebUrl: (
url: string,
createSuccess?: null | ((response?: string | null) => string),
createSuccess?: null | ((response?: string | null) => string)
) => (effects: Effects, duration: number) => Promise<HealthResult> = (
url,
createSuccess = null,
createSuccess = null
) => {
return async (effects, duration) => {
const errorValue = guardDurationAboveMinimum({

View File

@@ -4,16 +4,16 @@ import * as fs from "fs";
export async function writeConvertedFile(
file: string,
inputData: Promise<any> | any,
options: Parameters<typeof makeFileContent>[1],
options: Parameters<typeof makeFileContent>[1]
) {
await fs.writeFile(file, await makeFileContent(inputData, options), (err) =>
console.error(err),
console.error(err)
);
}
export default async function makeFileContent(
inputData: Promise<any> | any,
{ startSdk = "start-sdk" } = {},
{ startSdk = "start-sdk" } = {}
) {
const outputLines: string[] = [];
outputLines.push(`
@@ -25,10 +25,10 @@ export default async function makeFileContent(
const configName = newConst("InputSpec", convertInputSpec(data));
const configMatcherName = newConst(
"matchInputSpec",
`${configName}.validator()`,
`${configName}.validator()`
);
outputLines.push(
`export type InputSpec = typeof ${configMatcherName}._TYPE;`,
`export type InputSpec = typeof ${configMatcherName}._TYPE;`
);
return outputLines.join("\n");
@@ -64,7 +64,7 @@ export default async function makeFileContent(
textarea: value.textarea || null,
},
null,
2,
2
)})`;
}
case "number": {
@@ -81,7 +81,7 @@ export default async function makeFileContent(
placeholder: value.placeholder || null,
},
null,
2,
2
)})`;
}
case "boolean": {
@@ -93,7 +93,7 @@ export default async function makeFileContent(
warning: value.warning || null,
},
null,
2,
2
)})`;
}
case "enum": {
@@ -107,13 +107,13 @@ export default async function makeFileContent(
"value-names": value["value-names"] || null,
},
null,
2,
2
)})`;
}
case "object": {
const specName = newConst(
value.name + "_spec",
convertInputSpec(value.spec),
convertInputSpec(value.spec)
);
return `Value.object({
name: ${JSON.stringify(value.name || null)},
@@ -129,7 +129,7 @@ export default async function makeFileContent(
case "union": {
const variants = newConst(
value.name + "_variants",
convertVariants(value.variants),
convertVariants(value.variants)
);
return `Value.union({
name: ${JSON.stringify(value.name || null)},
@@ -147,7 +147,7 @@ export default async function makeFileContent(
"display-as": ${JSON.stringify(value["display-as"] || null)},
"unique-by": ${JSON.stringify(value["unique-by"] || null)},
"variant-names": ${JSON.stringify(
(value["variant-names"] as any) || null,
(value["variant-names"] as any) || null
)},
})`;
}
@@ -182,7 +182,7 @@ export default async function makeFileContent(
warning: value.warning || null,
},
null,
2,
2
)})`;
}
case "number": {
@@ -201,7 +201,7 @@ export default async function makeFileContent(
warning: value.warning || null,
},
null,
2,
2
)})`;
}
case "enum": {
@@ -218,13 +218,13 @@ export default async function makeFileContent(
warning: value.warning || null,
},
null,
2,
2
)})`;
}
case "object": {
const specName = newConst(
value.name + "_spec",
convertInputSpec(value.spec.spec),
convertInputSpec(value.spec.spec)
);
return `List.obj({
name: ${JSON.stringify(value.name || null)},
@@ -232,7 +232,7 @@ export default async function makeFileContent(
spec: {
spec: ${specName},
"display-as": ${JSON.stringify(
value?.spec?.["display-as"] || null,
value?.spec?.["display-as"] || null
)},
"unique-by": ${JSON.stringify(value?.spec?.["unique-by"] || null)},
},
@@ -244,7 +244,7 @@ export default async function makeFileContent(
case "union": {
const variants = newConst(
value.name + "_variants",
convertInputSpec(value.spec.variants),
convertInputSpec(value.spec.variants)
);
return `List.union(
{
@@ -254,24 +254,24 @@ export default async function makeFileContent(
tag: {
"id":${JSON.stringify(value?.spec?.tag?.["id"] || null)},
"name": ${JSON.stringify(
value?.spec?.tag?.["name"] || null,
value?.spec?.tag?.["name"] || null
)},
"description": ${JSON.stringify(
value?.spec?.tag?.["description"] || null,
value?.spec?.tag?.["description"] || null
)},
"warning": ${JSON.stringify(
value?.spec?.tag?.["warning"] || null,
value?.spec?.tag?.["warning"] || null
)},
"variant-names": ${JSON.stringify(
value?.spec?.tag?.["variant-names"] || {},
value?.spec?.tag?.["variant-names"] || {}
)},
},
variants: ${variants},
"display-as": ${JSON.stringify(
value?.spec?.["display-as"] || null,
value?.spec?.["display-as"] || null
)},
"unique-by": ${JSON.stringify(
value?.spec?.["unique-by"] || null,
value?.spec?.["unique-by"] || null
)},
default: ${JSON.stringify(value?.spec?.["default"] || null)},
},

View File

@@ -269,6 +269,19 @@ export type Effects = {
* @param configured
*/
setConfigured(configured: boolean): Promise<void>;
/**
*
* @returns PEM encoded fullchain (ecdsa)
*/
getSslCertificate: (
packageId: string,
algorithm?: "ecdsa" | "ed25519",
) => [string, string, string];
/**
* @returns PEM encoded ssl key (ecdsa)
*/
getSslKey: (packageId: string, algorithm?: "ecdsa" | "ed25519") => string;
};
/* rsync options: https://linux.die.net/man/1/rsync

View File

@@ -111,7 +111,7 @@ export interface ValueSpecListOf<T extends ListValueSpecType>
// sometimes the type checker needs just a little bit of help
export function isValueSpecListOf<S extends ListValueSpecType>(
t: ValueSpecList,
s: S,
s: S
): t is ValueSpecListOf<S> {
return t.subtype === s;
}

View File

@@ -56,7 +56,7 @@ export class FileHelper<A> {
readonly path: string,
readonly volume: string,
readonly writeData: (dataIn: A) => string,
readonly readData: (stringValue: string) => A,
readonly readData: (stringValue: string) => A
) {}
async write(data: A, effects: T.Effects) {
let matched;
@@ -86,21 +86,21 @@ export class FileHelper<A> {
await effects.readFile({
path: this.path,
volumeId: this.volume,
}),
})
);
}
static raw<A>(
path: string,
volume: string,
toFile: (dataIn: A) => string,
fromFile: (rawData: string) => A,
fromFile: (rawData: string) => A
) {
return new FileHelper<A>(path, volume, toFile, fromFile);
}
static json<A>(
path: string,
volume: string,
shape: matches.Validator<unknown, A>,
shape: matches.Validator<unknown, A>
) {
return new FileHelper<A>(
path,
@@ -110,13 +110,13 @@ export class FileHelper<A> {
},
(inString) => {
return shape.unsafeCast(JSON.parse(inString));
},
}
);
}
static toml<A extends Record<string, unknown>>(
path: string,
volume: string,
shape: matches.Validator<unknown, A>,
shape: matches.Validator<unknown, A>
) {
return new FileHelper<A>(
path,
@@ -126,13 +126,13 @@ export class FileHelper<A> {
},
(inString) => {
return shape.unsafeCast(TOML.parse(inString));
},
}
);
}
static yaml<A extends Record<string, unknown>>(
path: string,
volume: string,
shape: matches.Validator<unknown, A>,
shape: matches.Validator<unknown, A>
) {
return new FileHelper<A>(
path,
@@ -142,7 +142,7 @@ export class FileHelper<A> {
},
(inString) => {
return shape.unsafeCast(YAML.parse(inString));
},
}
);
}
}

View File

@@ -7,11 +7,11 @@ export { FileHelper } from "./fileHelper";
/** Used to check if the file exists before hand */
export const exists = (
effects: T.Effects,
props: { path: string; volumeId: string },
props: { path: string; volumeId: string }
) =>
effects.metadata(props).then(
(_) => true,
(_) => false,
(_) => false
);
export const isKnownError = (e: unknown): e is T.KnownError =>

View File

@@ -107,7 +107,7 @@ function charRange(value = "") {
*/
export function generateDefault(
generate: { charset: string; len: number },
{ random = () => Math.random() } = {},
{ random = () => Math.random() } = {}
) {
const validCharSets: number[][] = generate.charset
.split(",")
@@ -118,7 +118,7 @@ export function generateDefault(
}
const max = validCharSets.reduce(
(acc, x) => x.reduce((x, y) => Math.max(x, y), acc),
0,
0
);
let i = 0;
const answer: string[] = Array(generate.len);
@@ -127,7 +127,7 @@ export function generateDefault(
const inRange = validCharSets.reduce(
(acc, [lower, upper]) =>
acc || (nextValue >= lower && nextValue <= upper),
false,
false
);
if (!inRange) continue;
answer[i] = String.fromCharCode(nextValue);
@@ -155,7 +155,7 @@ export function matchNumberWithRange(range: string) {
? "any"
: left === "["
? `greaterThanOrEqualTo${leftValue}`
: `greaterThan${leftValue}`,
: `greaterThan${leftValue}`
)
.validate(
// prettier-ignore
@@ -165,7 +165,7 @@ export function matchNumberWithRange(range: string) {
// prettier-ignore
rightValue === "*" ? "any" :
right === "]" ? `lessThanOrEqualTo${rightValue}` :
`lessThan${rightValue}`,
`lessThan${rightValue}`
);
}
function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) {
@@ -186,12 +186,12 @@ const isGenerator = matches.shape({
}).test;
function defaultNullable<A>(
parser: matches.Parser<unknown, A>,
value: unknown,
value: unknown
) {
if (matchDefault.test(value)) {
if (isGenerator(value.default)) {
return parser.defaultTo(
parser.unsafeCast(generateDefault(value.default)),
parser.unsafeCast(generateDefault(value.default))
);
}
return parser.defaultTo(value.default);
@@ -209,7 +209,7 @@ function defaultNullable<A>(
* @returns
*/
export function guardAll<A extends ValueSpecAny>(
value: A,
value: A
): matches.Parser<unknown, GuardAll<A>> {
if (!isType.test(value)) {
return matches.unknown as any;
@@ -224,7 +224,7 @@ export function guardAll<A extends ValueSpecAny>(
case "number":
return defaultNullable(
withIntegral(withRange(value), value),
value,
value
) as any;
case "object":
@@ -244,14 +244,14 @@ export function guardAll<A extends ValueSpecAny>(
matches
.arrayOf(guardAll({ type: subtype, ...spec } as any))
.validate((x) => rangeValidate(x.length), "valid length"),
value,
value
) as any;
}
case "enum":
if (matchValues.test(value)) {
return defaultNullable(
matches.literals(value.values[0], ...value.values),
value,
value
) as any;
}
return matches.unknown as any;
@@ -261,8 +261,8 @@ export function guardAll<A extends ValueSpecAny>(
...Object.entries(value.variants).map(([variant, spec]) =>
matches
.shape({ [value.tag.id]: matches.literal(variant) })
.concat(typeFromProps(spec)),
),
.concat(typeFromProps(spec))
)
) as any;
}
return matches.unknown as any;
@@ -279,7 +279,7 @@ export function guardAll<A extends ValueSpecAny>(
* @returns
*/
export function typeFromProps<A extends InputSpec>(
valueDictionary: A,
valueDictionary: A
): matches.Parser<unknown, TypeFromProps<A>> {
if (!recordString.test(valueDictionary)) return matches.unknown as any;
return matches.shape(
@@ -287,7 +287,7 @@ export function typeFromProps<A extends InputSpec>(
Object.entries(valueDictionary).map(([key, value]) => [
key,
guardAll(value),
]),
),
])
)
) as any;
}

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "start-sdk",
"version": "0.4.0-lib0.alpha5",
"version": "0.4.0-lib0.alpha6",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "start-sdk",
"version": "0.4.0-lib0.alpha5",
"version": "0.4.0-lib0.alpha6",
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",