mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
chore: Some fixes
This commit is contained in:
3
Makefile
3
Makefile
@@ -3,11 +3,12 @@ version = $(shell git tag --sort=committerdate | tail -1)
|
|||||||
test: $(TS_FILES)
|
test: $(TS_FILES)
|
||||||
npm test
|
npm test
|
||||||
|
|
||||||
|
make clean:
|
||||||
|
rm -rf dist
|
||||||
# utils/test/output.ts: utils/test/config.json scripts/oldSpecToBuilder.ts
|
# utils/test/output.ts: utils/test/config.json scripts/oldSpecToBuilder.ts
|
||||||
# cat utils/test/config.json | deno run scripts/oldSpecToBuilder.ts "../../mod" |deno fmt - > utils/test/output.ts
|
# cat utils/test/config.json | deno run scripts/oldSpecToBuilder.ts "../../mod" |deno fmt - > utils/test/output.ts
|
||||||
|
|
||||||
bundle: fmt $(TS_FILES) .FORCE node_modules
|
bundle: fmt $(TS_FILES) .FORCE node_modules
|
||||||
rm -rf dist || true
|
|
||||||
npx tsc-multi
|
npx tsc-multi
|
||||||
npx tsc --emitDeclarationOnly
|
npx tsc --emitDeclarationOnly
|
||||||
|
|
||||||
|
|||||||
@@ -39,12 +39,12 @@ import { Config } from ".";
|
|||||||
```
|
```
|
||||||
*/
|
*/
|
||||||
export class Variants<
|
export class Variants<
|
||||||
A extends { [key: string]: ConfigSpec },
|
A extends { [key: string]: ConfigSpec }
|
||||||
> extends IBuilder<A> {
|
> extends IBuilder<A> {
|
||||||
static of<
|
static of<
|
||||||
A extends {
|
A extends {
|
||||||
[key: string]: Config<ConfigSpec>;
|
[key: string]: Config<ConfigSpec>;
|
||||||
},
|
}
|
||||||
>(a: A) {
|
>(a: A) {
|
||||||
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
|
const variants: { [K in keyof A]: BuilderExtract<A[K]> } = {} as any;
|
||||||
for (const key in a) {
|
for (const key in a) {
|
||||||
@@ -58,14 +58,14 @@ export class Variants<
|
|||||||
}
|
}
|
||||||
static withVariant<K extends string, B extends ConfigSpec>(
|
static withVariant<K extends string, B extends ConfigSpec>(
|
||||||
key: K,
|
key: K,
|
||||||
value: Config<B>,
|
value: Config<B>
|
||||||
) {
|
) {
|
||||||
return Variants.empty().withVariant(key, value);
|
return Variants.empty().withVariant(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
withVariant<K extends string, B extends ConfigSpec>(
|
withVariant<K extends string, B extends ConfigSpec>(
|
||||||
key: K,
|
key: K,
|
||||||
value: Config<B>,
|
value: Config<B>
|
||||||
) {
|
) {
|
||||||
return new Variants({
|
return new Variants({
|
||||||
...this.a,
|
...this.a,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { object, string } from "ts-matches";
|
|||||||
|
|
||||||
export type HealthCheck = (
|
export type HealthCheck = (
|
||||||
effects: Types.Effects,
|
effects: Types.Effects,
|
||||||
dateMs: number,
|
dateMs: number
|
||||||
) => Promise<HealthResult>;
|
) => Promise<HealthResult>;
|
||||||
export type HealthResult =
|
export type HealthResult =
|
||||||
| { success: string }
|
| { success: string }
|
||||||
@@ -23,7 +23,7 @@ function safelyStringify(e: unknown) {
|
|||||||
}
|
}
|
||||||
async function timeoutHealth(
|
async function timeoutHealth(
|
||||||
effects: Types.Effects,
|
effects: Types.Effects,
|
||||||
timeMs: number,
|
timeMs: number
|
||||||
): Promise<HealthResult> {
|
): Promise<HealthResult> {
|
||||||
await effects.sleep(timeMs);
|
await effects.sleep(timeMs);
|
||||||
return { failure: "Timed out " };
|
return { failure: "Timed out " };
|
||||||
@@ -39,7 +39,7 @@ async function timeoutHealth(
|
|||||||
export default function healthRunner(
|
export default function healthRunner(
|
||||||
name: string,
|
name: string,
|
||||||
fn: HealthCheck,
|
fn: HealthCheck,
|
||||||
{ defaultIntervalS = 60 } = {},
|
{ defaultIntervalS = 60 } = {}
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
create(effects: Types.Effects, defaultIntervalCreatedS = defaultIntervalS) {
|
create(effects: Types.Effects, defaultIntervalCreatedS = defaultIntervalS) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Effects, ResultType } from "../types";
|
import { Effects, ResultType } from "../types";
|
||||||
import { error, errorCode, isKnownError, ok, okOf } from "../util";
|
import { isKnownError } from "../util";
|
||||||
import { HealthResult } from "./healthRunner";
|
import { HealthResult } from "./healthRunner";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,32 +10,30 @@ import { HealthResult } from "./healthRunner";
|
|||||||
*/
|
*/
|
||||||
export const checkWebUrl: (
|
export const checkWebUrl: (
|
||||||
url: string,
|
url: string,
|
||||||
createSuccess?: null | ((response?: string | null) => string),
|
createSuccess?: null | ((response?: string | null) => string)
|
||||||
) => (
|
) => (effects: Effects, duration: number) => Promise<HealthResult> = (
|
||||||
effects: Effects,
|
url,
|
||||||
duration: number,
|
createSuccess = null
|
||||||
) => Promise<ResultType<HealthResult>> = (url, createSuccess = null) => {
|
) => {
|
||||||
return async (effects, duration) => {
|
return async (effects, duration) => {
|
||||||
let errorValue;
|
const errorValue = guardDurationAboveMinimum({
|
||||||
if (
|
duration,
|
||||||
(errorValue = guardDurationAboveMinimum({ duration, minimumTime: 5000 }))
|
minimumTime: 5000,
|
||||||
) {
|
});
|
||||||
|
if (!!errorValue) {
|
||||||
return errorValue;
|
return errorValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await effects
|
return await effects
|
||||||
.fetch(url)
|
.fetch(url)
|
||||||
.then((x) =>
|
.then((x) => ({
|
||||||
okOf({
|
success: createSuccess?.(x.body) ?? `Successfully fetched URL: ${url}`,
|
||||||
success:
|
}))
|
||||||
createSuccess?.(x.body) ?? `Successfully fetched URL: ${url}`,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
effects.warn(`Error while fetching URL: ${url}`);
|
effects.warn(`Error while fetching URL: ${url}`);
|
||||||
effects.error(JSON.stringify(e));
|
effects.error(JSON.stringify(e));
|
||||||
effects.error(e.toString());
|
effects.error(e.toString());
|
||||||
return error(`Error while fetching URL: ${url}`);
|
return { failure: `Error while fetching URL: ${url}` };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -59,7 +57,7 @@ export const runHealthScript =
|
|||||||
}) =>
|
}) =>
|
||||||
async (
|
async (
|
||||||
effects: Effects,
|
effects: Effects,
|
||||||
_duration: number,
|
_duration: number
|
||||||
): Promise<ResultType<HealthResult>> => {
|
): Promise<ResultType<HealthResult>> => {
|
||||||
const res = await effects.runCommand({ command, args });
|
const res = await effects.runCommand({ command, args });
|
||||||
if ("result" in res) {
|
if ("result" in res) {
|
||||||
@@ -79,10 +77,11 @@ export const runHealthScript =
|
|||||||
export const guardDurationAboveMinimum = (input: {
|
export const guardDurationAboveMinimum = (input: {
|
||||||
duration: number;
|
duration: number;
|
||||||
minimumTime: number;
|
minimumTime: number;
|
||||||
}) => (input.duration <= input.minimumTime ? errorCode(60, "Starting") : null);
|
}): null | HealthResult =>
|
||||||
|
input.duration <= input.minimumTime ? { starting: null } : null;
|
||||||
|
|
||||||
export const catchError = (effects: Effects) => (e: unknown) => {
|
export const catchError = (effects: Effects) => (e: unknown) => {
|
||||||
if (isKnownError(e)) return e;
|
if (isKnownError(e)) return e;
|
||||||
effects.error(`Health check failed: ${e}`);
|
effects.error(`Health check failed: ${e}`);
|
||||||
return error("Error while running health check");
|
return { failure: "Error while running health check" };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ export type Effects = {
|
|||||||
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH";
|
method?: "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "PATCH";
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
body?: string;
|
body?: string;
|
||||||
},
|
}
|
||||||
): Promise<{
|
): Promise<{
|
||||||
method: string;
|
method: string;
|
||||||
ok: boolean;
|
ok: boolean;
|
||||||
@@ -382,12 +382,12 @@ export type Dependencies = {
|
|||||||
/** Checks are called to make sure that our dependency is in the correct shape. If a known error is returned we know that the dependency needs modification */
|
/** Checks are called to make sure that our dependency is in the correct shape. If a known error is returned we know that the dependency needs modification */
|
||||||
check(
|
check(
|
||||||
effects: Effects,
|
effects: Effects,
|
||||||
input: ConfigSpec,
|
input: ConfigSpec
|
||||||
): Promise<ResultType<void | null>>;
|
): Promise<ResultType<void | null>>;
|
||||||
/** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */
|
/** This is called after we know that the dependency package needs a new configuration, this would be a transform for defaults */
|
||||||
autoConfigure(
|
autoConfigure(
|
||||||
effects: Effects,
|
effects: Effects,
|
||||||
input: ConfigSpec,
|
input: ConfigSpec
|
||||||
): Promise<ResultType<ConfigSpec>>;
|
): Promise<ResultType<ConfigSpec>>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ function charRange(value = "") {
|
|||||||
*/
|
*/
|
||||||
export function generateDefault(
|
export function generateDefault(
|
||||||
generate: { charset: string; len: number },
|
generate: { charset: string; len: number },
|
||||||
{ random = () => Math.random() } = {},
|
{ random = () => Math.random() } = {}
|
||||||
) {
|
) {
|
||||||
const validCharSets: number[][] = generate.charset
|
const validCharSets: number[][] = generate.charset
|
||||||
.split(",")
|
.split(",")
|
||||||
@@ -118,7 +118,7 @@ export function generateDefault(
|
|||||||
}
|
}
|
||||||
const max = validCharSets.reduce(
|
const max = validCharSets.reduce(
|
||||||
(acc, x) => x.reduce((x, y) => Math.max(x, y), acc),
|
(acc, x) => x.reduce((x, y) => Math.max(x, y), acc),
|
||||||
0,
|
0
|
||||||
);
|
);
|
||||||
let i = 0;
|
let i = 0;
|
||||||
const answer: string[] = Array(generate.len);
|
const answer: string[] = Array(generate.len);
|
||||||
@@ -127,7 +127,7 @@ export function generateDefault(
|
|||||||
const inRange = validCharSets.reduce(
|
const inRange = validCharSets.reduce(
|
||||||
(acc, [lower, upper]) =>
|
(acc, [lower, upper]) =>
|
||||||
acc || (nextValue >= lower && nextValue <= upper),
|
acc || (nextValue >= lower && nextValue <= upper),
|
||||||
false,
|
false
|
||||||
);
|
);
|
||||||
if (!inRange) continue;
|
if (!inRange) continue;
|
||||||
answer[i] = String.fromCharCode(nextValue);
|
answer[i] = String.fromCharCode(nextValue);
|
||||||
@@ -155,7 +155,7 @@ export function matchNumberWithRange(range: string) {
|
|||||||
? "any"
|
? "any"
|
||||||
: left === "["
|
: left === "["
|
||||||
? `greaterThanOrEqualTo${leftValue}`
|
? `greaterThanOrEqualTo${leftValue}`
|
||||||
: `greaterThan${leftValue}`,
|
: `greaterThan${leftValue}`
|
||||||
)
|
)
|
||||||
.validate(
|
.validate(
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
@@ -165,7 +165,7 @@ export function matchNumberWithRange(range: string) {
|
|||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
rightValue === "*" ? "any" :
|
rightValue === "*" ? "any" :
|
||||||
right === "]" ? `lessThanOrEqualTo${rightValue}` :
|
right === "]" ? `lessThanOrEqualTo${rightValue}` :
|
||||||
`lessThan${rightValue}`,
|
`lessThan${rightValue}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) {
|
function withIntegral(parser: matches.Parser<unknown, number>, value: unknown) {
|
||||||
@@ -186,12 +186,12 @@ const isGenerator = matches.shape({
|
|||||||
}).test;
|
}).test;
|
||||||
function defaultNullable<A>(
|
function defaultNullable<A>(
|
||||||
parser: matches.Parser<unknown, A>,
|
parser: matches.Parser<unknown, A>,
|
||||||
value: unknown,
|
value: unknown
|
||||||
) {
|
) {
|
||||||
if (matchDefault.test(value)) {
|
if (matchDefault.test(value)) {
|
||||||
if (isGenerator(value.default)) {
|
if (isGenerator(value.default)) {
|
||||||
return parser.defaultTo(
|
return parser.defaultTo(
|
||||||
parser.unsafeCast(generateDefault(value.default)),
|
parser.unsafeCast(generateDefault(value.default))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return parser.defaultTo(value.default);
|
return parser.defaultTo(value.default);
|
||||||
@@ -209,7 +209,7 @@ function defaultNullable<A>(
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function guardAll<A extends ValueSpecAny>(
|
export function guardAll<A extends ValueSpecAny>(
|
||||||
value: A,
|
value: A
|
||||||
): matches.Parser<unknown, GuardAll<A>> {
|
): matches.Parser<unknown, GuardAll<A>> {
|
||||||
if (!isType.test(value)) {
|
if (!isType.test(value)) {
|
||||||
return matches.unknown as any;
|
return matches.unknown as any;
|
||||||
@@ -224,7 +224,7 @@ export function guardAll<A extends ValueSpecAny>(
|
|||||||
case "number":
|
case "number":
|
||||||
return defaultNullable(
|
return defaultNullable(
|
||||||
withIntegral(withRange(value), value),
|
withIntegral(withRange(value), value),
|
||||||
value,
|
value
|
||||||
) as any;
|
) as any;
|
||||||
|
|
||||||
case "object":
|
case "object":
|
||||||
@@ -244,14 +244,14 @@ export function guardAll<A extends ValueSpecAny>(
|
|||||||
matches
|
matches
|
||||||
.arrayOf(guardAll({ type: subtype, ...spec } as any))
|
.arrayOf(guardAll({ type: subtype, ...spec } as any))
|
||||||
.validate((x) => rangeValidate(x.length), "valid length"),
|
.validate((x) => rangeValidate(x.length), "valid length"),
|
||||||
value,
|
value
|
||||||
) as any;
|
) as any;
|
||||||
}
|
}
|
||||||
case "enum":
|
case "enum":
|
||||||
if (matchValues.test(value)) {
|
if (matchValues.test(value)) {
|
||||||
return defaultNullable(
|
return defaultNullable(
|
||||||
matches.literals(value.values[0], ...value.values),
|
matches.literals(value.values[0], ...value.values),
|
||||||
value,
|
value
|
||||||
) as any;
|
) as any;
|
||||||
}
|
}
|
||||||
return matches.unknown as any;
|
return matches.unknown as any;
|
||||||
@@ -261,8 +261,8 @@ export function guardAll<A extends ValueSpecAny>(
|
|||||||
...Object.entries(value.variants).map(([variant, spec]) =>
|
...Object.entries(value.variants).map(([variant, spec]) =>
|
||||||
matches
|
matches
|
||||||
.shape({ [value.tag.id]: matches.literal(variant) })
|
.shape({ [value.tag.id]: matches.literal(variant) })
|
||||||
.concat(typeFromProps(spec)),
|
.concat(typeFromProps(spec))
|
||||||
),
|
)
|
||||||
) as any;
|
) as any;
|
||||||
}
|
}
|
||||||
return matches.unknown as any;
|
return matches.unknown as any;
|
||||||
@@ -279,7 +279,7 @@ export function guardAll<A extends ValueSpecAny>(
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function typeFromProps<A extends ConfigSpec>(
|
export function typeFromProps<A extends ConfigSpec>(
|
||||||
valueDictionary: A,
|
valueDictionary: A
|
||||||
): matches.Parser<unknown, TypeFromProps<A>> {
|
): matches.Parser<unknown, TypeFromProps<A>> {
|
||||||
if (!recordString.test(valueDictionary)) return matches.unknown as any;
|
if (!recordString.test(valueDictionary)) return matches.unknown as any;
|
||||||
return matches.shape(
|
return matches.shape(
|
||||||
@@ -287,7 +287,7 @@ export function typeFromProps<A extends ConfigSpec>(
|
|||||||
Object.entries(valueDictionary).map(([key, value]) => [
|
Object.entries(valueDictionary).map(([key, value]) => [
|
||||||
key,
|
key,
|
||||||
guardAll(value),
|
guardAll(value),
|
||||||
]),
|
])
|
||||||
),
|
)
|
||||||
) as any;
|
) as any;
|
||||||
}
|
}
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "start-sdk",
|
"name": "start-sdk",
|
||||||
"version": "0.4.0-lib0.alpha3",
|
"version": "0.4.0-lib0.alpha4",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "start-sdk",
|
"name": "start-sdk",
|
||||||
"version": "0.4.0-lib0.alpha3",
|
"version": "0.4.0-lib0.alpha4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@iarna/toml": "^2.2.5",
|
"@iarna/toml": "^2.2.5",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "start-sdk",
|
"name": "start-sdk",
|
||||||
"version": "0.4.0-lib0.alpha4",
|
"version": "0.4.0-lib0.alpha5",
|
||||||
"description": "For making the patterns that are wanted in making services for the startOS.",
|
"description": "For making the patterns that are wanted in making services for the startOS.",
|
||||||
"main": "./index.cjs",
|
"main": "./index.cjs",
|
||||||
"types": "./index.d.ts",
|
"types": "./index.d.ts",
|
||||||
|
|||||||
Reference in New Issue
Block a user