feat: initialize unidesk platform
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "@unidesk/shared",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "src/index.ts",
|
||||
"types": "src/index.ts"
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
|
||||
|
||||
export type ProviderLabels = Record<string, JsonValue>;
|
||||
|
||||
export interface ProviderRegisterMessage {
|
||||
type: "register";
|
||||
providerId: string;
|
||||
name: string;
|
||||
labels: ProviderLabels;
|
||||
startedAt: string;
|
||||
capabilities: string[];
|
||||
}
|
||||
|
||||
export interface ProviderHeartbeatMessage {
|
||||
type: "heartbeat";
|
||||
providerId: string;
|
||||
labels: ProviderLabels;
|
||||
at: string;
|
||||
}
|
||||
|
||||
export interface ProviderTaskStatusMessage {
|
||||
type: "task_status";
|
||||
providerId: string;
|
||||
taskId: string;
|
||||
status: "accepted" | "running" | "succeeded" | "failed";
|
||||
message: string;
|
||||
at: string;
|
||||
result?: JsonValue;
|
||||
}
|
||||
|
||||
export interface CoreDispatchMessage {
|
||||
type: "dispatch";
|
||||
taskId: string;
|
||||
command: "docker.ps" | "echo";
|
||||
payload: Record<string, JsonValue>;
|
||||
}
|
||||
|
||||
export interface CoreAcknowledgeMessage {
|
||||
type: "ack";
|
||||
requestId: string;
|
||||
ok: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export type ProviderToCoreMessage =
|
||||
| ProviderRegisterMessage
|
||||
| ProviderHeartbeatMessage
|
||||
| ProviderTaskStatusMessage;
|
||||
|
||||
export type CoreToProviderMessage = CoreDispatchMessage | CoreAcknowledgeMessage;
|
||||
|
||||
export interface ApiNode {
|
||||
providerId: string;
|
||||
name: string;
|
||||
status: "online" | "offline";
|
||||
labels: ProviderLabels;
|
||||
connectedAt: string | null;
|
||||
lastHeartbeat: string | null;
|
||||
}
|
||||
|
||||
export interface ApiTask {
|
||||
id: string;
|
||||
providerId: string;
|
||||
command: string;
|
||||
status: string;
|
||||
payload: JsonValue;
|
||||
result: JsonValue;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface ApiEvent {
|
||||
id: number;
|
||||
type: string;
|
||||
source: string;
|
||||
payload: JsonValue;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface ServiceLogEntry {
|
||||
ts: string;
|
||||
service: string;
|
||||
level: "debug" | "info" | "warn" | "error";
|
||||
message: string;
|
||||
data?: JsonValue;
|
||||
}
|
||||
|
||||
export function parseJsonObject(value: string, name: string): Record<string, JsonValue> {
|
||||
const parsed = JSON.parse(value) as unknown;
|
||||
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
||||
throw new Error(`${name} must be a JSON object`);
|
||||
}
|
||||
return parsed as Record<string, JsonValue>;
|
||||
}
|
||||
|
||||
export function isProviderToCoreMessage(value: unknown): value is ProviderToCoreMessage {
|
||||
if (typeof value !== "object" || value === null || !("type" in value)) return false;
|
||||
const msg = value as { type?: unknown; providerId?: unknown };
|
||||
return (
|
||||
(msg.type === "register" || msg.type === "heartbeat" || msg.type === "task_status") &&
|
||||
typeof msg.providerId === "string" &&
|
||||
msg.providerId.length > 0
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"types": ["bun", "node"],
|
||||
"strict": true,
|
||||
"declaration": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"outDir": "dist",
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user