2022-05-19 13:33:48 +02:00
|
|
|
import {ArrayField, Field, RawJsonField} from "sparkson";
|
|
|
|
|
2022-02-22 15:39:39 +01:00
|
|
|
// Types shared with the client.
|
2022-05-26 13:58:01 +02:00
|
|
|
export type TypeGuard<T> = (arg: unknown) => arg is T;
|
2022-02-22 15:39:39 +01:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function parseJSON(str: string): JSONValue {
|
|
|
|
const json = JSON.parse(str);
|
|
|
|
if (!isJSONValue(json)) {
|
|
|
|
throw new Error("Invalid JSON returned. Should never happen.");
|
|
|
|
}
|
|
|
|
return json;
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type JSONValue =
|
|
|
|
| null
|
|
|
|
| string
|
|
|
|
| number
|
|
|
|
| boolean
|
|
|
|
| JSONObject
|
|
|
|
| JSONArray;
|
2022-07-14 20:06:05 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function isJSONValue(arg: unknown): arg is JSONValue {
|
|
|
|
return (
|
|
|
|
arg === null ||
|
|
|
|
isString(arg) ||
|
|
|
|
isNumber(arg) ||
|
|
|
|
isBoolean(arg) ||
|
|
|
|
isJSONObject(arg) ||
|
|
|
|
isJSONArray(arg)
|
|
|
|
);
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export interface JSONObject {
|
|
|
|
[x: string]: JSONValue;
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function isJSONObject(arg: unknown): arg is JSONObject {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-22 15:39:39 +01:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const obj = arg as object;
|
|
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
|
|
if (!isString(key) || !isJSONValue(value)) {
|
2022-07-14 20:06:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 17:49:42 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface JSONArray extends Array<JSONValue> {
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isJSONArray = toIsArray(isJSONValue);
|
|
|
|
|
|
|
|
export type EnumValue<E> = E[keyof E];
|
|
|
|
export type EnumTypeGuard<E> = TypeGuard<EnumValue<E>>;
|
|
|
|
|
|
|
|
export function unhandledEnumField(field: never): never {
|
|
|
|
throw new Error(`Unhandled enum field: ${field}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isObject(arg: unknown): arg is object {
|
|
|
|
return arg !== null && typeof arg === "object";
|
2022-07-14 20:06:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:58:01 +02:00
|
|
|
export function isArray<T>(arg: unknown, isT: TypeGuard<T>): arg is Array<T> {
|
2022-05-19 13:33:48 +02:00
|
|
|
if (!Array.isArray(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const element of arg) {
|
|
|
|
if (!isT(element)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:30:29 +02:00
|
|
|
export function isMap(arg: unknown): arg is Map<any, any> {
|
|
|
|
return arg instanceof Map;
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:33:48 +02:00
|
|
|
export function isString(arg: unknown): arg is string {
|
|
|
|
return typeof arg === "string"
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:22:57 +02:00
|
|
|
export function toIsNewtype<
|
|
|
|
Type extends Value & { readonly __tag: symbol },
|
|
|
|
Value,
|
|
|
|
>(isValue: TypeGuard<Value>, _example: Type): TypeGuard<Type> {
|
|
|
|
return (arg: unknown): arg is Type => isValue(arg);
|
|
|
|
}
|
|
|
|
|
2022-06-12 14:10:00 +02:00
|
|
|
export function isNumber(arg: unknown): arg is number {
|
|
|
|
return typeof arg === "number"
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isBoolean(arg: unknown): arg is boolean {
|
|
|
|
return typeof arg === "boolean"
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:11:08 +02:00
|
|
|
export function isUndefined(arg: unknown): arg is undefined {
|
|
|
|
return arg === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isNull(arg: unknown): arg is null {
|
|
|
|
return arg === null;
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:58:01 +02:00
|
|
|
export function toIsArray<T>(isT: TypeGuard<T>): TypeGuard<T[]> {
|
|
|
|
return (arg): arg is T[] => isArray(arg, isT);
|
|
|
|
}
|
2022-02-22 15:39:39 +01:00
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function toIsEnum<E>(enumDef: E): EnumTypeGuard<E> {
|
|
|
|
return (arg): arg is EnumValue<E> => Object.values(enumDef).includes(arg as [keyof E]);
|
2022-02-22 15:39:39 +01:00
|
|
|
}
|
|
|
|
|
2022-06-12 14:10:00 +02:00
|
|
|
export function isOptional<T>(arg: unknown, isT: TypeGuard<T>): arg is (T | undefined) {
|
|
|
|
return arg === undefined || isT(arg);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type Url = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isUrl = toIsNewtype(isString, "" as Url);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type Version = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isVersion = toIsNewtype(isString, "" as Version);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type EmailAddress = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isEmailAddress = toIsNewtype(isString, "" as EmailAddress);
|
2022-07-18 17:49:42 +02:00
|
|
|
|
2022-02-22 15:39:39 +01:00
|
|
|
export type NodeStatistics = {
|
|
|
|
registered: number;
|
|
|
|
withVPN: number;
|
|
|
|
withCoords: number;
|
|
|
|
monitoring: {
|
|
|
|
active: number;
|
|
|
|
pending: number;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export function isNodeStatistics(arg: unknown): arg is NodeStatistics {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const stats = arg as NodeStatistics;
|
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isNumber(stats.registered) &&
|
|
|
|
isNumber(stats.withVPN) &&
|
|
|
|
isNumber(stats.withCoords) &&
|
|
|
|
isObject(stats.monitoring) &&
|
|
|
|
isNumber(stats.monitoring.active) &&
|
|
|
|
isNumber(stats.monitoring.pending)
|
2022-02-22 15:39:39 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export type Statistics = {
|
2022-02-22 15:39:39 +01:00
|
|
|
nodes: NodeStatistics;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isStatistics(arg: unknown): arg is Statistics {
|
|
|
|
return isObject(arg) && isNodeStatistics((arg as Statistics).nodes);
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
|
|
|
|
export class CommunityConfig {
|
|
|
|
constructor(
|
|
|
|
@Field("name") public name: string,
|
|
|
|
@Field("domain") public domain: string,
|
2022-07-18 17:49:42 +02:00
|
|
|
@Field("contactEmail") public contactEmail: EmailAddress,
|
|
|
|
@ArrayField("sites", String) public sites: Site[],
|
|
|
|
@ArrayField("domains", String) public domains: Domain[],
|
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isCommunityConfig(arg: unknown): arg is CommunityConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as CommunityConfig;
|
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isString(cfg.name) &&
|
|
|
|
isString(cfg.domain) &&
|
2022-07-18 17:49:42 +02:00
|
|
|
isEmailAddress(cfg.contactEmail) &&
|
|
|
|
isArray(cfg.sites, isSite) &&
|
|
|
|
isArray(cfg.domains, isDomain)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LegalConfig {
|
|
|
|
constructor(
|
2022-07-18 17:49:42 +02:00
|
|
|
@Field("privacyUrl", true) public privacyUrl?: Url,
|
|
|
|
@Field("imprintUrl", true) public imprintUrl?: Url,
|
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isLegalConfig(arg: unknown): arg is LegalConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as LegalConfig;
|
|
|
|
return (
|
2022-07-18 17:49:42 +02:00
|
|
|
isOptional(cfg.privacyUrl, isUrl) &&
|
|
|
|
isOptional(cfg.imprintUrl, isUrl)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ClientMapConfig {
|
|
|
|
constructor(
|
2022-07-18 17:49:42 +02:00
|
|
|
@Field("mapUrl") public mapUrl: Url,
|
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isClientMapConfig(arg: unknown): arg is ClientMapConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as ClientMapConfig;
|
2022-07-18 17:49:42 +02:00
|
|
|
return isUrl(cfg.mapUrl);
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MonitoringConfig {
|
|
|
|
constructor(
|
|
|
|
@Field("enabled") public enabled: boolean,
|
2022-07-18 17:49:42 +02:00
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isMonitoringConfig(arg: unknown): arg is MonitoringConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as MonitoringConfig;
|
2022-06-12 14:10:00 +02:00
|
|
|
return isBoolean(cfg.enabled);
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export class CoordinatesConfig {
|
2022-05-19 13:33:48 +02:00
|
|
|
constructor(
|
|
|
|
@Field("lat") public lat: number,
|
|
|
|
@Field("lng") public lng: number,
|
2022-07-18 17:49:42 +02:00
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function isCoordinatesConfig(arg: unknown): arg is CoordinatesConfig {
|
2022-05-19 13:33:48 +02:00
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-18 17:49:42 +02:00
|
|
|
const coords = arg as CoordinatesConfig;
|
2022-05-19 13:33:48 +02:00
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isNumber(coords.lat) &&
|
|
|
|
isNumber(coords.lng)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export class CoordinatesSelectorConfig {
|
2022-05-19 13:33:48 +02:00
|
|
|
constructor(
|
|
|
|
@Field("lat") public lat: number,
|
|
|
|
@Field("lng") public lng: number,
|
|
|
|
@Field("defaultZoom") public defaultZoom: number,
|
2022-07-18 17:49:42 +02:00
|
|
|
@RawJsonField("layers") public layers: JSONObject,
|
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export function isCoordinatesSelectorConfig(arg: unknown): arg is CoordinatesSelectorConfig {
|
2022-05-19 13:33:48 +02:00
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-18 17:49:42 +02:00
|
|
|
const cfg = arg as CoordinatesSelectorConfig;
|
2022-05-19 13:33:48 +02:00
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isNumber(cfg.lat) &&
|
|
|
|
isNumber(cfg.lng) &&
|
|
|
|
isNumber(cfg.defaultZoom) &&
|
2022-07-18 17:49:42 +02:00
|
|
|
isJSONObject(cfg.layers)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OtherCommunityInfoConfig {
|
|
|
|
constructor(
|
|
|
|
@Field("showInfo") public showInfo: boolean,
|
|
|
|
@Field("showBorderForDebugging") public showBorderForDebugging: boolean,
|
2022-07-18 17:49:42 +02:00
|
|
|
@ArrayField("localCommunityPolygon", CoordinatesConfig) public localCommunityPolygon: CoordinatesConfig[],
|
|
|
|
) {
|
|
|
|
}
|
2022-05-19 13:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isOtherCommunityInfoConfig(arg: unknown): arg is OtherCommunityInfoConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as OtherCommunityInfoConfig;
|
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isBoolean(cfg.showInfo) &&
|
|
|
|
isBoolean(cfg.showBorderForDebugging) &&
|
2022-07-18 17:49:42 +02:00
|
|
|
isArray(cfg.localCommunityPolygon, isCoordinatesConfig)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ClientConfig {
|
|
|
|
constructor(
|
|
|
|
@Field("community") public community: CommunityConfig,
|
|
|
|
@Field("legal") public legal: LegalConfig,
|
|
|
|
@Field("map") public map: ClientMapConfig,
|
|
|
|
@Field("monitoring") public monitoring: MonitoringConfig,
|
2022-07-18 17:49:42 +02:00
|
|
|
@Field("coordsSelector") public coordsSelector: CoordinatesSelectorConfig,
|
2022-05-19 13:33:48 +02:00
|
|
|
@Field("otherCommunityInfo") public otherCommunityInfo: OtherCommunityInfoConfig,
|
|
|
|
@Field("rootPath", true, undefined, "/") public rootPath: string,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isClientConfig(arg: unknown): arg is ClientConfig {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cfg = arg as ClientConfig;
|
|
|
|
return (
|
|
|
|
isCommunityConfig(cfg.community) &&
|
|
|
|
isLegalConfig(cfg.legal) &&
|
|
|
|
isClientMapConfig(cfg.map) &&
|
|
|
|
isMonitoringConfig(cfg.monitoring) &&
|
2022-07-18 17:49:42 +02:00
|
|
|
isCoordinatesSelectorConfig(cfg.coordsSelector) &&
|
2022-05-19 13:33:48 +02:00
|
|
|
isOtherCommunityInfoConfig(cfg.otherCommunityInfo) &&
|
2022-06-12 14:10:00 +02:00
|
|
|
isString(cfg.rootPath)
|
2022-05-19 13:33:48 +02:00
|
|
|
);
|
|
|
|
}
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type Token = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isToken = toIsNewtype(isString, "" as Token);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type FastdKey = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isFastdKey = toIsNewtype(isString, "" as FastdKey);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type MAC = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isMAC = toIsNewtype(isString, "" as MAC);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export type DurationSeconds = number & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isDurationSeconds = toIsNewtype(isNumber, NaN as DurationSeconds);
|
2022-07-21 18:39:33 +02:00
|
|
|
|
2022-07-18 12:55:19 +02:00
|
|
|
export type UnixTimestampSeconds = number & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isUnixTimestampSeconds = toIsNewtype(isNumber, NaN as UnixTimestampSeconds);
|
2022-07-18 17:49:42 +02:00
|
|
|
|
2022-07-18 12:55:19 +02:00
|
|
|
export type UnixTimestampMilliseconds = number & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isUnixTimestampMilliseconds = toIsNewtype(isNumber, NaN as UnixTimestampMilliseconds);
|
2022-07-18 12:55:19 +02:00
|
|
|
|
|
|
|
export function toUnixTimestampSeconds(ms: UnixTimestampMilliseconds): UnixTimestampSeconds {
|
|
|
|
return Math.floor(ms) as UnixTimestampSeconds;
|
|
|
|
}
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type MonitoringToken = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isMonitoringToken = toIsNewtype(isString, "" as MonitoringToken);
|
2022-07-14 20:06:05 +02:00
|
|
|
|
2022-05-26 13:58:01 +02:00
|
|
|
export enum MonitoringState {
|
|
|
|
ACTIVE = "active",
|
|
|
|
PENDING = "pending",
|
|
|
|
DISABLED = "disabled",
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isMonitoringState = toIsEnum(MonitoringState);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type NodeId = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isNodeId = toIsNewtype(isString, "" as NodeId);
|
2022-07-18 17:49:42 +02:00
|
|
|
|
2022-07-28 12:22:57 +02:00
|
|
|
export type Hostname = string & { readonly __tag: unique symbol }
|
|
|
|
export const isHostname = toIsNewtype(isString, "" as Hostname);
|
2022-07-18 17:49:42 +02:00
|
|
|
|
|
|
|
export type Nickname = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isNickname = toIsNewtype(isString, "" as Nickname);
|
2022-07-18 17:49:42 +02:00
|
|
|
|
|
|
|
export type Coordinates = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isCoordinates = toIsNewtype(isString, "" as Coordinates);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
/**
|
|
|
|
* Basic node data.
|
|
|
|
*/
|
|
|
|
export type BaseNode = {
|
2022-07-18 17:49:42 +02:00
|
|
|
nickname: Nickname;
|
|
|
|
email: EmailAddress;
|
|
|
|
hostname: Hostname;
|
|
|
|
coords?: Coordinates;
|
2022-05-26 13:58:01 +02:00
|
|
|
key?: FastdKey;
|
|
|
|
mac: MAC;
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export function isBaseNode(arg: unknown): arg is BaseNode {
|
2022-05-26 13:58:01 +02:00
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = arg as BaseNode;
|
2022-05-26 13:58:01 +02:00
|
|
|
return (
|
2022-07-18 17:49:42 +02:00
|
|
|
isNickname(node.nickname) &&
|
|
|
|
isEmailAddress(node.email) &&
|
|
|
|
isHostname(node.hostname) &&
|
|
|
|
isOptional(node.coords, isCoordinates) &&
|
2022-06-12 14:10:00 +02:00
|
|
|
isOptional(node.key, isFastdKey) &&
|
2022-07-21 18:39:33 +02:00
|
|
|
isMAC(node.mac)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Node data used for creating or updating a node.
|
|
|
|
*/
|
|
|
|
export type CreateOrUpdateNode = BaseNode & {
|
|
|
|
monitoring: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCreateOrUpdateNode(arg: unknown): arg is CreateOrUpdateNode {
|
|
|
|
if (!isBaseNode(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const node = arg as CreateOrUpdateNode;
|
|
|
|
return (
|
|
|
|
isBoolean(node.monitoring)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Representation of a stored node.
|
|
|
|
*/
|
|
|
|
export type StoredNode = BaseNode & {
|
|
|
|
token: Token;
|
|
|
|
monitoringState: MonitoringState;
|
|
|
|
modifiedAt: UnixTimestampSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isStoredNode(arg: unknown): arg is StoredNode {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const node = arg as StoredNode;
|
|
|
|
return (
|
|
|
|
isBaseNode(node) &&
|
|
|
|
isToken(node.token) &&
|
2022-05-26 13:58:01 +02:00
|
|
|
isMonitoringState(node.monitoringState) &&
|
2022-07-18 17:49:42 +02:00
|
|
|
isUnixTimestampSeconds(node.modifiedAt)
|
2022-05-26 13:58:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export type NodeResponse = StoredNode & {
|
|
|
|
monitoring: boolean;
|
|
|
|
monitoringConfirmed: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isNodeResponse(arg: unknown): arg is NodeResponse {
|
|
|
|
if (!isStoredNode(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const node = arg as NodeResponse;
|
|
|
|
return (
|
|
|
|
isBoolean(node.monitoring) &&
|
|
|
|
isBoolean(node.monitoringConfirmed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export type NodeTokenResponse = {
|
|
|
|
token: Token;
|
|
|
|
node: NodeResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isNodeTokenResponse(arg: unknown): arg is NodeTokenResponse {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const response = arg as NodeTokenResponse;
|
|
|
|
return (
|
|
|
|
isToken(response.token) &&
|
|
|
|
isNodeResponse(response.node) &&
|
|
|
|
response.token === response.node.token
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:58:01 +02:00
|
|
|
export enum OnlineState {
|
|
|
|
ONLINE = "ONLINE",
|
|
|
|
OFFLINE = "OFFLINE",
|
|
|
|
}
|
2022-07-14 20:06:05 +02:00
|
|
|
|
2022-05-26 13:58:01 +02:00
|
|
|
export const isOnlineState = toIsEnum(OnlineState);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type Site = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isSite = toIsNewtype(isString, "" as Site);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export type Domain = string & { readonly __tag: unique symbol };
|
2022-07-28 12:22:57 +02:00
|
|
|
export const isDomain = toIsNewtype(isString, "" as Domain);
|
2022-05-26 13:58:01 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
/**
|
|
|
|
* Represents a node in the context of a Freifunk site and domain.
|
|
|
|
*/
|
2022-07-22 17:11:08 +02:00
|
|
|
export type DomainSpecificNodeResponse = Record<NodeSortField, any> & NodeResponse & {
|
2022-05-26 13:58:01 +02:00
|
|
|
site?: Site,
|
|
|
|
domain?: Domain,
|
|
|
|
onlineState?: OnlineState,
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export function isDomainSpecificNodeResponse(arg: unknown): arg is DomainSpecificNodeResponse {
|
|
|
|
if (!isNodeResponse(arg)) {
|
2022-05-26 13:58:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
const node = arg as DomainSpecificNodeResponse;
|
2022-05-26 13:58:01 +02:00
|
|
|
return (
|
2022-06-12 14:10:00 +02:00
|
|
|
isOptional(node.site, isSite) &&
|
|
|
|
isOptional(node.domain, isDomain) &&
|
|
|
|
isOptional(node.onlineState, isOnlineState)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export type MonitoringResponse = {
|
|
|
|
hostname: Hostname,
|
|
|
|
mac: MAC,
|
|
|
|
email: EmailAddress,
|
|
|
|
monitoring: boolean,
|
|
|
|
monitoringConfirmed: boolean,
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isMonitoringResponse(arg: unknown): arg is MonitoringResponse {
|
|
|
|
if (!Object(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const response = arg as MonitoringResponse;
|
|
|
|
return (
|
|
|
|
isHostname(response.hostname) &&
|
|
|
|
isMAC(response.mac) &&
|
|
|
|
isEmailAddress(response.email) &&
|
|
|
|
isBoolean(response.monitoring) &&
|
|
|
|
isBoolean(response.monitoringConfirmed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export enum NodeSortField {
|
|
|
|
HOSTNAME = 'hostname',
|
|
|
|
NICKNAME = 'nickname',
|
|
|
|
EMAIL = 'email',
|
|
|
|
TOKEN = 'token',
|
|
|
|
MAC = 'mac',
|
|
|
|
KEY = 'key',
|
|
|
|
SITE = 'site',
|
|
|
|
DOMAIN = 'domain',
|
|
|
|
COORDS = 'coords',
|
|
|
|
ONLINE_STATE = 'onlineState',
|
|
|
|
MONITORING_STATE = 'monitoringState',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isNodeSortField = toIsEnum(NodeSortField);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export type NodesFilter = {
|
2022-06-12 14:10:00 +02:00
|
|
|
hasKey?: boolean;
|
|
|
|
hasCoords?: boolean;
|
|
|
|
monitoringState?: MonitoringState;
|
2022-06-17 15:30:29 +02:00
|
|
|
site?: Site;
|
|
|
|
domain?: Domain;
|
|
|
|
onlineState?: OnlineState;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NODES_FILTER_FIELDS = {
|
|
|
|
hasKey: Boolean,
|
|
|
|
hasCoords: Boolean,
|
|
|
|
monitoringState: MonitoringState,
|
|
|
|
site: String,
|
|
|
|
domain: String,
|
|
|
|
onlineState: OnlineState,
|
|
|
|
};
|
2022-06-12 14:10:00 +02:00
|
|
|
|
|
|
|
export function isNodesFilter(arg: unknown): arg is NodesFilter {
|
|
|
|
if (!isObject(arg)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const filter = arg as NodesFilter;
|
|
|
|
return (
|
|
|
|
isOptional(filter.hasKey, isBoolean) &&
|
|
|
|
isOptional(filter.hasCoords, isBoolean) &&
|
2022-06-17 15:30:29 +02:00
|
|
|
isOptional(filter.monitoringState, isMonitoringState) &&
|
|
|
|
isOptional(filter.site, isSite) &&
|
|
|
|
isOptional(filter.domain, isDomain) &&
|
|
|
|
isOptional(filter.onlineState, isOnlineState)
|
2022-05-26 13:58:01 +02:00
|
|
|
);
|
|
|
|
}
|
2022-06-23 14:26:15 +02:00
|
|
|
|
|
|
|
export enum MonitoringSortField {
|
|
|
|
ID = 'id',
|
|
|
|
HOSTNAME = 'hostname',
|
|
|
|
MAC = 'mac',
|
|
|
|
SITE = 'site',
|
|
|
|
DOMAIN = 'domain',
|
|
|
|
MONITORING_STATE = 'monitoring_state',
|
|
|
|
STATE = 'state',
|
|
|
|
LAST_SEEN = 'last_seen',
|
|
|
|
IMPORT_TIMESTAMP = 'import_timestamp',
|
|
|
|
LAST_STATUS_MAIL_TYPE = 'last_status_mail_type',
|
|
|
|
LAST_STATUS_MAIL_SENT = 'last_status_mail_sent',
|
|
|
|
CREATED_AT = 'created_at',
|
|
|
|
MODIFIED_AT = 'modified_at',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isMonitoringSortField = toIsEnum(MonitoringSortField);
|
|
|
|
|
|
|
|
export enum TaskSortField {
|
|
|
|
ID = 'id',
|
|
|
|
NAME = 'name',
|
|
|
|
SCHEDULE = 'schedule',
|
|
|
|
STATE = 'state',
|
|
|
|
RUNNING_SINCE = 'runningSince',
|
|
|
|
LAST_RUN_STARTED = 'lastRunStarted',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isTaskSortField = toIsEnum(TaskSortField);
|
|
|
|
|
|
|
|
export enum MailSortField {
|
|
|
|
ID = 'id',
|
|
|
|
FAILURES = 'failures',
|
|
|
|
SENDER = 'sender',
|
|
|
|
RECIPIENT = 'recipient',
|
|
|
|
EMAIL = 'email',
|
|
|
|
CREATED_AT = 'created_at',
|
|
|
|
MODIFIED_AT = 'modified_at',
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isMailSortField = toIsEnum(MailSortField);
|
|
|
|
|
2022-07-14 20:06:05 +02:00
|
|
|
export type GenericSortField = {
|
|
|
|
value: string;
|
|
|
|
readonly __tag: unique symbol
|
|
|
|
};
|
2022-06-23 14:26:15 +02:00
|
|
|
|
|
|
|
export enum SortDirection {
|
|
|
|
ASCENDING = "ASC",
|
|
|
|
DESCENDING = "DESC",
|
|
|
|
}
|
|
|
|
|
|
|
|
export const isSortDirection = toIsEnum(SortDirection);
|