@sixb/cli 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +275 -0
- package/dist/commands/access-token-utils.d.ts +9 -0
- package/dist/commands/access-token-utils.d.ts.map +1 -0
- package/dist/commands/api.d.ts +13 -0
- package/dist/commands/api.d.ts.map +1 -0
- package/dist/commands/app.d.ts +9 -0
- package/dist/commands/app.d.ts.map +1 -0
- package/dist/commands/atlas.d.ts +9 -0
- package/dist/commands/atlas.d.ts.map +1 -0
- package/dist/commands/auth.d.ts +8 -0
- package/dist/commands/auth.d.ts.map +1 -0
- package/dist/commands/build.d.ts +6 -0
- package/dist/commands/build.d.ts.map +1 -0
- package/dist/commands/check.d.ts +5 -0
- package/dist/commands/check.d.ts.map +1 -0
- package/dist/commands/db-migrate.d.ts +5 -0
- package/dist/commands/db-migrate.d.ts.map +1 -0
- package/dist/commands/dev.d.ts +12 -0
- package/dist/commands/dev.d.ts.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/lake-check.d.ts +5 -0
- package/dist/commands/lake-check.d.ts.map +1 -0
- package/dist/commands/lake-cleanup.d.ts +16 -0
- package/dist/commands/lake-cleanup.d.ts.map +1 -0
- package/dist/commands/orchestrator.d.ts +6 -0
- package/dist/commands/orchestrator.d.ts.map +1 -0
- package/dist/commands/rules.d.ts +6 -0
- package/dist/commands/rules.d.ts.map +1 -0
- package/dist/commands/scheduler.d.ts +6 -0
- package/dist/commands/scheduler.d.ts.map +1 -0
- package/dist/commands/service-account.d.ts +14 -0
- package/dist/commands/service-account.d.ts.map +1 -0
- package/dist/commands/token.d.ts +14 -0
- package/dist/commands/token.d.ts.map +1 -0
- package/dist/commands/typegen.d.ts +5 -0
- package/dist/commands/typegen.d.ts.map +1 -0
- package/dist/commands/worker-group.d.ts +8 -0
- package/dist/commands/worker-group.d.ts.map +1 -0
- package/dist/commands/worker.d.ts +8 -0
- package/dist/commands/worker.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/api-client.d.ts +26 -0
- package/dist/lib/api-client.d.ts.map +1 -0
- package/dist/lib/browser-topology.d.ts +62 -0
- package/dist/lib/browser-topology.d.ts.map +1 -0
- package/dist/lib/errors.d.ts +19 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/health.d.ts +25 -0
- package/dist/lib/health.d.ts.map +1 -0
- package/dist/lib/loadSixb.d.ts +49 -0
- package/dist/lib/loadSixb.d.ts.map +1 -0
- package/dist/lib/production-roles.d.ts +65 -0
- package/dist/lib/production-roles.d.ts.map +1 -0
- package/dist/lib/production.d.ts +29 -0
- package/dist/lib/production.d.ts.map +1 -0
- package/dist/lib/public-origin.d.ts +17 -0
- package/dist/lib/public-origin.d.ts.map +1 -0
- package/dist/lib/runtime.d.ts +76 -0
- package/dist/lib/runtime.d.ts.map +1 -0
- package/dist/lib/shareable-providers.d.ts +20 -0
- package/dist/lib/shareable-providers.d.ts.map +1 -0
- package/dist/lib/storage-migration.d.ts +40 -0
- package/dist/lib/storage-migration.d.ts.map +1 -0
- package/dist/lib/typegen.d.ts +5 -0
- package/dist/lib/typegen.d.ts.map +1 -0
- package/dist/lib/worker-registry.d.ts +30 -0
- package/dist/lib/worker-registry.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/ui/index.d.ts +164 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/commands/access-token-utils.ts +63 -0
- package/src/commands/api.tsx +111 -0
- package/src/commands/app.tsx +91 -0
- package/src/commands/atlas.tsx +75 -0
- package/src/commands/auth.tsx +96 -0
- package/src/commands/build.tsx +90 -0
- package/src/commands/check.tsx +58 -0
- package/src/commands/db-migrate.tsx +27 -0
- package/src/commands/dev.tsx +141 -0
- package/src/commands/init.tsx +18 -0
- package/src/commands/lake-check.tsx +20 -0
- package/src/commands/lake-cleanup.tsx +63 -0
- package/src/commands/orchestrator.tsx +87 -0
- package/src/commands/rules.tsx +81 -0
- package/src/commands/scheduler.tsx +77 -0
- package/src/commands/service-account.tsx +304 -0
- package/src/commands/token.tsx +143 -0
- package/src/commands/typegen.tsx +18 -0
- package/src/commands/worker-group.tsx +120 -0
- package/src/commands/worker.tsx +96 -0
- package/src/index.tsx +390 -0
- package/src/lib/api-client.ts +132 -0
- package/src/lib/browser-topology.ts +292 -0
- package/src/lib/errors.ts +40 -0
- package/src/lib/health.ts +138 -0
- package/src/lib/loadSixb.ts +175 -0
- package/src/lib/production-roles.ts +48 -0
- package/src/lib/production.ts +104 -0
- package/src/lib/public-origin.ts +46 -0
- package/src/lib/runtime.ts +346 -0
- package/src/lib/shareable-providers.ts +67 -0
- package/src/lib/storage-migration.ts +120 -0
- package/src/lib/typegen.ts +12 -0
- package/src/lib/worker-registry.ts +191 -0
- package/src/ui/index.tsx +953 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import type { SixbBrowserOrigin } from "@sixb/server"
|
|
2
|
+
import { configuredOrigin } from "./public-origin"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The process resolving the topology. One answer settles what used to be four options: whether
|
|
6
|
+
* local defaults apply, which browser surfaces the process has to account for, and which one it
|
|
7
|
+
* serves itself.
|
|
8
|
+
*/
|
|
9
|
+
export type BrowserRole = "dev" | "api" | "atlas" | "app"
|
|
10
|
+
|
|
11
|
+
/** The two surfaces a browser loads, and the audience each maps to. */
|
|
12
|
+
export type BrowserSurface = "atlas" | "app"
|
|
13
|
+
|
|
14
|
+
interface BrowserBindOptions {
|
|
15
|
+
readonly host?: string
|
|
16
|
+
/** `--port`: the port this role binds. See {@link resolveBrowserPorts}. */
|
|
17
|
+
readonly port?: string
|
|
18
|
+
/** `--api-host` and `--api-port`, which only the two roles that bind the API accept. */
|
|
19
|
+
readonly apiHost?: string
|
|
20
|
+
readonly apiPort?: string
|
|
21
|
+
readonly apiPublicOrigin?: string
|
|
22
|
+
readonly atlasPublicOrigin?: string
|
|
23
|
+
readonly appPublicOrigin?: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type BrowserTopologyOptions = BrowserBindOptions &
|
|
27
|
+
(
|
|
28
|
+
| {
|
|
29
|
+
/**
|
|
30
|
+
* Hosts the API, so its allowlist has to name every surface the project actually
|
|
31
|
+
* serves — including the custom app, which exists only when the project has one.
|
|
32
|
+
*/
|
|
33
|
+
readonly role: "dev" | "api"
|
|
34
|
+
readonly hasCustomApp: boolean
|
|
35
|
+
}
|
|
36
|
+
| { readonly role: BrowserSurface }
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
export interface BrowserTopology {
|
|
40
|
+
readonly host: string
|
|
41
|
+
readonly apiHost: string
|
|
42
|
+
readonly atlasPort: number
|
|
43
|
+
readonly appPort: number
|
|
44
|
+
readonly apiPort: number
|
|
45
|
+
readonly apiPublicOrigin: string
|
|
46
|
+
readonly atlasPublicOrigin: string | null
|
|
47
|
+
readonly appPublicOrigin: string | null
|
|
48
|
+
readonly allowedBrowserOrigins: readonly SixbBrowserOrigin[]
|
|
49
|
+
/** The surface this process serves itself, or `null` when it serves none. */
|
|
50
|
+
readonly serves: BrowserSurface | null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A topology resolved by a role that serves a surface, so {@link servedUrl} always has one. */
|
|
54
|
+
export interface ServingBrowserTopology extends BrowserTopology {
|
|
55
|
+
readonly serves: BrowserSurface
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface BrowserHosts {
|
|
59
|
+
readonly host: string
|
|
60
|
+
readonly apiHost: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface BrowserPorts {
|
|
64
|
+
readonly atlasPort: number
|
|
65
|
+
readonly appPort: number
|
|
66
|
+
readonly apiPort: number
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface BrowserPublicOrigins {
|
|
70
|
+
readonly apiPublicOrigin: string
|
|
71
|
+
readonly atlasPublicOrigin: string | null
|
|
72
|
+
readonly appPublicOrigin: string | null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A production role sits behind a load balancer and has to accept its traffic. A dev
|
|
77
|
+
* server does not: binding every interface put the project's data — and an API that is
|
|
78
|
+
* usually running with auth disabled — on the local network, which on a café or office
|
|
79
|
+
* network means anyone on it. `--host 0.0.0.0` opts back in.
|
|
80
|
+
*/
|
|
81
|
+
const DEFAULT_PRODUCTION_HOST = "0.0.0.0"
|
|
82
|
+
const DEFAULT_DEVELOPMENT_HOST = "127.0.0.1"
|
|
83
|
+
const DEFAULT_ATLAS_PORT = 3000
|
|
84
|
+
const DEFAULT_APP_PORT_OFFSET = 1
|
|
85
|
+
const DEFAULT_API_PORT_OFFSET = 2
|
|
86
|
+
|
|
87
|
+
export function resolveBrowserTopology(
|
|
88
|
+
options: BrowserBindOptions & { readonly role: BrowserSurface }
|
|
89
|
+
): ServingBrowserTopology
|
|
90
|
+
export function resolveBrowserTopology(options: BrowserTopologyOptions): BrowserTopology
|
|
91
|
+
export function resolveBrowserTopology(options: BrowserTopologyOptions): BrowserTopology {
|
|
92
|
+
const hosts = resolveBrowserHosts(options)
|
|
93
|
+
const ports = resolveBrowserPorts(options)
|
|
94
|
+
const origins = resolveBrowserPublicOrigins(options, ports)
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
...hosts,
|
|
98
|
+
...ports,
|
|
99
|
+
...origins,
|
|
100
|
+
allowedBrowserOrigins: createAllowedBrowserOrigins(origins),
|
|
101
|
+
serves: servedSurface(options.role),
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The surface a role serves itself. For the two roles that serve one, it is the role. */
|
|
106
|
+
function servedSurface(role: BrowserRole): BrowserSurface | null {
|
|
107
|
+
return role === "atlas" || role === "app" ? role : null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The surfaces a role's topology has to resolve an origin for. */
|
|
111
|
+
function surfacesOf(options: BrowserTopologyOptions): Record<BrowserSurface, boolean> {
|
|
112
|
+
switch (options.role) {
|
|
113
|
+
case "dev":
|
|
114
|
+
case "api":
|
|
115
|
+
return { atlas: true, app: options.hasCustomApp }
|
|
116
|
+
case "atlas":
|
|
117
|
+
return { atlas: true, app: false }
|
|
118
|
+
case "app":
|
|
119
|
+
return { atlas: false, app: true }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function resolveBrowserHosts(options: BrowserTopologyOptions): BrowserHosts {
|
|
124
|
+
const host =
|
|
125
|
+
options.host ?? (options.role === "dev" ? DEFAULT_DEVELOPMENT_HOST : DEFAULT_PRODUCTION_HOST)
|
|
126
|
+
return {
|
|
127
|
+
host,
|
|
128
|
+
apiHost: options.apiHost ?? host,
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* `--port` is the port the role binds. `sixb dev` binds all three surfaces, so there it moves the
|
|
134
|
+
* whole block and the others keep their offsets from it. Every other role binds exactly one, and
|
|
135
|
+
* `--port` moves only that one — which is why `sixb app --port 4000` listens on 4000 and not one
|
|
136
|
+
* past it. The ports a role does not bind keep their defaults and are read by nobody.
|
|
137
|
+
*/
|
|
138
|
+
function resolveBrowserPorts(options: BrowserTopologyOptions): BrowserPorts {
|
|
139
|
+
const requested = parsePort(options.port, "port")
|
|
140
|
+
const base = options.role === "dev" ? (requested ?? DEFAULT_ATLAS_PORT) : DEFAULT_ATLAS_PORT
|
|
141
|
+
const bound = options.role === "dev" ? undefined : requested
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
atlasPort: (options.role === "atlas" ? bound : undefined) ?? base,
|
|
145
|
+
appPort: (options.role === "app" ? bound : undefined) ?? base + DEFAULT_APP_PORT_OFFSET,
|
|
146
|
+
apiPort:
|
|
147
|
+
parsePort(options.apiPort, "api-port") ??
|
|
148
|
+
(options.role === "api" ? bound : undefined) ??
|
|
149
|
+
base + DEFAULT_API_PORT_OFFSET,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function resolveBrowserPublicOrigins(
|
|
154
|
+
options: BrowserTopologyOptions,
|
|
155
|
+
ports: BrowserPorts
|
|
156
|
+
): BrowserPublicOrigins {
|
|
157
|
+
const surfaces = surfacesOf(options)
|
|
158
|
+
|
|
159
|
+
// Every browser surface sends its requests here, so this one is never inferred in production.
|
|
160
|
+
const apiPublicOrigin =
|
|
161
|
+
configuredOrigin(options.apiPublicOrigin, "SIXB_API_PUBLIC_ORIGIN", "API public origin") ??
|
|
162
|
+
localOrigin(options.role, ports.apiPort) ??
|
|
163
|
+
refusePublicOrigin("SIXB_API_PUBLIC_ORIGIN")
|
|
164
|
+
|
|
165
|
+
const atlasPublicOrigin = surfaces.atlas
|
|
166
|
+
? resolveSurfaceOrigin(options, "atlas", {
|
|
167
|
+
value: options.atlasPublicOrigin,
|
|
168
|
+
envName: "SIXB_ATLAS_PUBLIC_ORIGIN",
|
|
169
|
+
label: "Atlas public origin",
|
|
170
|
+
port: ports.atlasPort,
|
|
171
|
+
})
|
|
172
|
+
: null
|
|
173
|
+
const appPublicOrigin = surfaces.app
|
|
174
|
+
? resolveSurfaceOrigin(options, "app", {
|
|
175
|
+
value: options.appPublicOrigin,
|
|
176
|
+
envName: "SIXB_APP_PUBLIC_ORIGIN",
|
|
177
|
+
label: "custom app public origin",
|
|
178
|
+
port: ports.appPort,
|
|
179
|
+
})
|
|
180
|
+
: null
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
apiPublicOrigin,
|
|
184
|
+
atlasPublicOrigin,
|
|
185
|
+
appPublicOrigin,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* A browser surface's own public origin. Required wherever it feeds the API's CORS allowlist, and
|
|
191
|
+
* optional on the role that serves the surface, which only prints it — refusing to start over a
|
|
192
|
+
* label is a startup an operator has to debug for nothing.
|
|
193
|
+
*/
|
|
194
|
+
function resolveSurfaceOrigin(
|
|
195
|
+
options: BrowserTopologyOptions,
|
|
196
|
+
surface: BrowserSurface,
|
|
197
|
+
input: {
|
|
198
|
+
readonly value: string | undefined
|
|
199
|
+
readonly envName: string
|
|
200
|
+
readonly label: string
|
|
201
|
+
readonly port: number
|
|
202
|
+
}
|
|
203
|
+
): string | null {
|
|
204
|
+
const configured = configuredOrigin(input.value, input.envName, input.label)
|
|
205
|
+
if (configured) return configured
|
|
206
|
+
|
|
207
|
+
const local = localOrigin(options.role, input.port)
|
|
208
|
+
if (local) return local
|
|
209
|
+
|
|
210
|
+
return options.role === surface ? null : refusePublicOrigin(input.envName)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function createAllowedBrowserOrigins(origins: BrowserPublicOrigins): readonly SixbBrowserOrigin[] {
|
|
214
|
+
const allowedOrigins: SixbBrowserOrigin[] = []
|
|
215
|
+
|
|
216
|
+
if (origins.atlasPublicOrigin) {
|
|
217
|
+
allowedOrigins.push({ origin: origins.atlasPublicOrigin, audience: "atlas" })
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (origins.appPublicOrigin) {
|
|
221
|
+
allowedOrigins.push({ origin: origins.appPublicOrigin, audience: "app" })
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return allowedOrigins
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* A bind address that is not an address anyone can open. The default production host is every
|
|
229
|
+
* interface, so a panel that printed it verbatim answered "where do I go?" with `0.0.0.0`.
|
|
230
|
+
*/
|
|
231
|
+
const WILDCARD_BINDS = new Set(["0.0.0.0", "::", "[::]"])
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* What a browser-serving role shows as its own address. The public origin when one is configured,
|
|
235
|
+
* and otherwise the address it actually bound — which is all the process knows, and better than a
|
|
236
|
+
* startup panel with no URL on it.
|
|
237
|
+
*/
|
|
238
|
+
export function servedUrl(topology: ServingBrowserTopology): string {
|
|
239
|
+
const [origin, port] =
|
|
240
|
+
topology.serves === "atlas"
|
|
241
|
+
? ([topology.atlasPublicOrigin, topology.atlasPort] as const)
|
|
242
|
+
: ([topology.appPublicOrigin, topology.appPort] as const)
|
|
243
|
+
|
|
244
|
+
return origin ?? `http://${displayHost(topology.host)}:${port}`
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function displayHost(host: string): string {
|
|
248
|
+
return WILDCARD_BINDS.has(host) ? "localhost" : host
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function apiUrl(topology: BrowserTopology): string {
|
|
252
|
+
return new URL("/api", topology.apiPublicOrigin).toString().replace(/\/$/, "")
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function apiDocsUrl(topology: BrowserTopology): string {
|
|
256
|
+
return new URL("/docs", topology.apiPublicOrigin).toString()
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function apiEventsUrl(topology: BrowserTopology): string {
|
|
260
|
+
const url = new URL("/ws/events", topology.apiPublicOrigin)
|
|
261
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:"
|
|
262
|
+
return url.toString()
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The local default, or `null` outside `sixb dev`, where an origin behind a proxy is a guess. */
|
|
266
|
+
function localOrigin(role: BrowserRole, port: number): string | null {
|
|
267
|
+
return role === "dev" ? `http://localhost:${port}` : null
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function refusePublicOrigin(envName: string): never {
|
|
271
|
+
throw new Error(
|
|
272
|
+
`[SixbCLI] Production serving requires ${envName} or --${toKebabCase(envName.replace(/^SIXB_/, "").toLowerCase())}.`
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** The port a flag set, or `undefined` when it did not. `label` names the flag in the error. */
|
|
277
|
+
function parsePort(value: string | undefined, label: string): number | undefined {
|
|
278
|
+
if (!value) {
|
|
279
|
+
return undefined
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const port = Number.parseInt(value, 10)
|
|
283
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535 || String(port) !== value.trim()) {
|
|
284
|
+
throw new Error(`[SixbCLI] --${label} must be a valid TCP port.`)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return port
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function toKebabCase(value: string): string {
|
|
291
|
+
return value.replaceAll("_", "-")
|
|
292
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface SixbCliErrorOptions extends ErrorOptions {
|
|
2
|
+
/**
|
|
3
|
+
* What the operator should do next, kept out of the message so the terminal can give the
|
|
4
|
+
* instruction its own place and a caller can log the diagnosis alone.
|
|
5
|
+
*/
|
|
6
|
+
readonly remediation?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A failure the CLI raised itself, as opposed to one it caught from the runtime. */
|
|
10
|
+
export class SixbCliError extends Error {
|
|
11
|
+
readonly remediation: string | undefined
|
|
12
|
+
|
|
13
|
+
constructor(message: string, options: SixbCliErrorOptions = {}) {
|
|
14
|
+
super(message, options)
|
|
15
|
+
this.name = "SixbCliError"
|
|
16
|
+
this.remediation = options.remediation
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function errorMessage(error: unknown): string {
|
|
21
|
+
return error instanceof Error ? error.message : String(error)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Reads a remediation off an error, including through a wrapper's `cause` — wrapping is how the
|
|
26
|
+
* CLI adds context to a provider's failure.
|
|
27
|
+
*/
|
|
28
|
+
export function errorRemediation(error: unknown): string | undefined {
|
|
29
|
+
let current: unknown = error
|
|
30
|
+
|
|
31
|
+
// Bounded: a cause chain is normally one or two deep, and a cycle would otherwise hang.
|
|
32
|
+
for (let depth = 0; depth < 8 && current instanceof Error; depth += 1) {
|
|
33
|
+
if (current instanceof SixbCliError && current.remediation) {
|
|
34
|
+
return current.remediation
|
|
35
|
+
}
|
|
36
|
+
current = current.cause
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return undefined
|
|
40
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { checkStorageSchema } from "@sixb/core"
|
|
2
|
+
import type { LoadedSixb } from "./loadSixb"
|
|
3
|
+
import { findProcessLocalProviders } from "./shareable-providers"
|
|
4
|
+
|
|
5
|
+
/** An unreachable Postgres waits rather than refusing, so every probe is bounded. */
|
|
6
|
+
const PROBE_TIMEOUT_MS = 5_000
|
|
7
|
+
|
|
8
|
+
export interface ProviderCheck {
|
|
9
|
+
readonly ok: boolean
|
|
10
|
+
/** What was found. Shown as-is, so it has to read as an answer on its own. */
|
|
11
|
+
readonly message: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RuntimeCheckOptions {
|
|
15
|
+
/** Per-probe bound. Defaults to {@link PROBE_TIMEOUT_MS}. */
|
|
16
|
+
readonly timeoutMs?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RuntimeCheck {
|
|
20
|
+
readonly storage: ProviderCheck
|
|
21
|
+
readonly timeseries: ProviderCheck
|
|
22
|
+
readonly broker: ProviderCheck
|
|
23
|
+
readonly queues: ProviderCheck
|
|
24
|
+
readonly warnings: readonly string[]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Probes the configured providers with read-only round trips.
|
|
29
|
+
*
|
|
30
|
+
* There is no `events` row because there is no events provider: `EventsRuntime` is built over
|
|
31
|
+
* the broker, so probing both was one round trip reported twice.
|
|
32
|
+
*/
|
|
33
|
+
export async function checkRuntimeHealth(
|
|
34
|
+
sixb: LoadedSixb,
|
|
35
|
+
options: RuntimeCheckOptions = {}
|
|
36
|
+
): Promise<RuntimeCheck> {
|
|
37
|
+
const timeoutMs = options.timeoutMs ?? PROBE_TIMEOUT_MS
|
|
38
|
+
const [storage, timeseries, broker, queues] = await Promise.all([
|
|
39
|
+
probeStorage(sixb, timeoutMs),
|
|
40
|
+
probe(() => probeTimeseries(sixb), providerName(sixb.storage), timeoutMs),
|
|
41
|
+
probe(() => probeBroker(sixb), providerName(sixb.broker), timeoutMs),
|
|
42
|
+
probeQueues(sixb, timeoutMs),
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
storage,
|
|
47
|
+
timeseries,
|
|
48
|
+
broker,
|
|
49
|
+
queues,
|
|
50
|
+
warnings: processLocalWarnings(sixb),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function probeStorage(sixb: LoadedSixb, timeoutMs: number): Promise<ProviderCheck> {
|
|
55
|
+
const name = providerName(sixb.storage)
|
|
56
|
+
const reachable = await probe(() => sixb.storage.ping(), name, timeoutMs)
|
|
57
|
+
if (!reachable.ok) return reachable
|
|
58
|
+
|
|
59
|
+
// Reachable is not usable, so a second round trip checks the schema — shared with `/ready`
|
|
60
|
+
// so both agree on what usable means. `verified: false` means the storage exposes no
|
|
61
|
+
// migrators: usable for want of a schema rather than by having a current one.
|
|
62
|
+
let verified = false
|
|
63
|
+
const schema = await probe(
|
|
64
|
+
async () => {
|
|
65
|
+
const result = await checkStorageSchema(sixb.storage)
|
|
66
|
+
verified = result.verified
|
|
67
|
+
if (!result.ok) throw new Error(result.reason ?? "schema is not usable")
|
|
68
|
+
},
|
|
69
|
+
name,
|
|
70
|
+
timeoutMs
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
return schema.ok && verified
|
|
74
|
+
? { ok: true, message: `${schema.message} · schema current` }
|
|
75
|
+
: schema
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function probeTimeseries(sixb: LoadedSixb): Promise<void> {
|
|
79
|
+
// A read that matches nothing still exercises the telemetry table, which is a
|
|
80
|
+
// different table from the one `ping()` touches.
|
|
81
|
+
await sixb.storage.timeseries.getLatest({
|
|
82
|
+
projectId: sixb.projectId,
|
|
83
|
+
objectTypeId: "__sixb_check",
|
|
84
|
+
objectId: "__sixb_check",
|
|
85
|
+
propertyId: "__sixb_check",
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function probeBroker(sixb: LoadedSixb): Promise<void> {
|
|
90
|
+
// `latestCursor` is the only read in the broker contract that neither writes nor
|
|
91
|
+
// requires the stream to exist — it answers `undefined` for a missing one.
|
|
92
|
+
await sixb.events.latestCursor()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function probeQueues(sixb: LoadedSixb, timeoutMs: number): Promise<ProviderCheck> {
|
|
96
|
+
// Bound to the provider: `probe` calls it as a bare function, and a queues provider
|
|
97
|
+
// that reads its own connection off `this` would see `undefined`.
|
|
98
|
+
return probe(sixb.queues.health.bind(sixb.queues), providerName(sixb.queues), timeoutMs)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function processLocalWarnings(sixb: LoadedSixb): readonly string[] {
|
|
102
|
+
return findProcessLocalProviders(sixb).map(
|
|
103
|
+
(offender) =>
|
|
104
|
+
`${offender.slot} is ${offender.configured}, which only works inside one process. ` +
|
|
105
|
+
`\`sixb dev\` is fine; production roles will refuse to start. Use ${offender.replacements}.`
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** `?.constructor`: the contracts are structural, so a prototype-less object satisfies them. */
|
|
110
|
+
function providerName(provider: object): string {
|
|
111
|
+
return provider.constructor?.name ?? "unknown"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function probe(
|
|
115
|
+
run: () => Promise<unknown>,
|
|
116
|
+
detail: string,
|
|
117
|
+
timeoutMs: number
|
|
118
|
+
): Promise<ProviderCheck> {
|
|
119
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
await Promise.race([
|
|
123
|
+
run(),
|
|
124
|
+
new Promise((_resolve, reject) => {
|
|
125
|
+
timer = setTimeout(() => reject(new Error(`timed out after ${timeoutMs}ms`)), timeoutMs)
|
|
126
|
+
}),
|
|
127
|
+
])
|
|
128
|
+
return { ok: true, message: `ok · ${detail}` }
|
|
129
|
+
} catch (error) {
|
|
130
|
+
// Led by the configured class, because core's reason names the migration adapter
|
|
131
|
+
// (`SixbSqliteStorage`) rather than what the author wrote (`SqliteStorage`).
|
|
132
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
133
|
+
return { ok: false, message: `${detail} · ${message}` }
|
|
134
|
+
} finally {
|
|
135
|
+
// A pending timer keeps the event loop alive and hangs the command after it prints.
|
|
136
|
+
if (timer) clearTimeout(timer)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url"
|
|
2
|
+
import type {
|
|
3
|
+
ActionDefinition,
|
|
4
|
+
Broker,
|
|
5
|
+
ConnectorAdapter,
|
|
6
|
+
ConnectorClient,
|
|
7
|
+
ConnectorDefinition,
|
|
8
|
+
DatasetDefinition,
|
|
9
|
+
LinkProjectionDefinition,
|
|
10
|
+
ObjectProjectionDefinition,
|
|
11
|
+
OntologyMaintenanceHandle,
|
|
12
|
+
OntologyOperationalStatus,
|
|
13
|
+
PipelineDefinition,
|
|
14
|
+
ProjectionDefinition,
|
|
15
|
+
RuleDefinition,
|
|
16
|
+
ScheduleDefinition,
|
|
17
|
+
SixbReadiness,
|
|
18
|
+
SixbRuntimeContext,
|
|
19
|
+
SyncDefinition,
|
|
20
|
+
TelemetryProjectionDefinition,
|
|
21
|
+
} from "@sixb/core"
|
|
22
|
+
import type { AgentsRuntime } from "@sixb/core/internal/agents"
|
|
23
|
+
import type { AuthRuntime } from "@sixb/core/internal/auth"
|
|
24
|
+
import type { WorkflowsRuntime } from "@sixb/core/internal/workflows"
|
|
25
|
+
import type { ObjectRow } from "@sixb/core/storage"
|
|
26
|
+
|
|
27
|
+
export interface LoadedSixb extends SixbRuntimeContext {
|
|
28
|
+
readonly id: string
|
|
29
|
+
readonly broker: Broker
|
|
30
|
+
readonly auth: AuthRuntime
|
|
31
|
+
listObjectTypes(): readonly unknown[]
|
|
32
|
+
listActions(): readonly ActionDefinition[]
|
|
33
|
+
getActionById(actionId: string): ActionDefinition | null
|
|
34
|
+
listSyncs(): readonly SyncDefinition[]
|
|
35
|
+
listPipelines(): readonly PipelineDefinition[]
|
|
36
|
+
getPipelineById(pipelineId: string): PipelineDefinition | null
|
|
37
|
+
listSchedules(): readonly ScheduleDefinition[]
|
|
38
|
+
readonly workflows: WorkflowsRuntime
|
|
39
|
+
readonly agents: AgentsRuntime
|
|
40
|
+
listObjectProjections(): readonly ObjectProjectionDefinition[]
|
|
41
|
+
listLinkProjections(): readonly LinkProjectionDefinition[]
|
|
42
|
+
listTelemetryProjections(): readonly TelemetryProjectionDefinition[]
|
|
43
|
+
listDatasets(): readonly DatasetDefinition[]
|
|
44
|
+
listRules(): readonly RuleDefinition[]
|
|
45
|
+
getDatasetById(datasetId: string): DatasetDefinition | null
|
|
46
|
+
getProjectionById(projectionId: string): ProjectionDefinition | null
|
|
47
|
+
getSyncById(syncId: string): SyncDefinition | null
|
|
48
|
+
getRuleById(ruleId: string): RuleDefinition | null
|
|
49
|
+
listSubTypes(objectTypeId: string): string[]
|
|
50
|
+
connector<TAdapter extends ConnectorAdapter>(
|
|
51
|
+
definition: ConnectorDefinition<string, TAdapter>
|
|
52
|
+
): Promise<ConnectorClient<TAdapter>>
|
|
53
|
+
startScheduler(): Promise<void>
|
|
54
|
+
stopScheduler(): Promise<void>
|
|
55
|
+
startOntologyMaintenance(): Promise<OntologyMaintenanceHandle>
|
|
56
|
+
getOntologyOperationalStatus(): OntologyOperationalStatus
|
|
57
|
+
checkReadiness(): Promise<SixbReadiness>
|
|
58
|
+
disconnectConnectors(): Promise<void>
|
|
59
|
+
closeLogger(): Promise<void>
|
|
60
|
+
closeBroker(): Promise<void>
|
|
61
|
+
list(params: {
|
|
62
|
+
objectTypeIds?: readonly string[]
|
|
63
|
+
limit?: number
|
|
64
|
+
offset?: number
|
|
65
|
+
}): Promise<{ total: number; hasMore: boolean; objects: ObjectRow[] }>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
|
|
69
|
+
return !!value && typeof value === "object" && "then" in value
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const REQUIRED_RUNTIME_PROPERTIES = [
|
|
73
|
+
"ontology",
|
|
74
|
+
"actionRegistry",
|
|
75
|
+
"events",
|
|
76
|
+
"broker",
|
|
77
|
+
"storage",
|
|
78
|
+
"lakeStorage",
|
|
79
|
+
"blobStorage",
|
|
80
|
+
"queues",
|
|
81
|
+
"auth",
|
|
82
|
+
"agents",
|
|
83
|
+
] as const
|
|
84
|
+
|
|
85
|
+
const REQUIRED_DEFINITION_METHODS = [
|
|
86
|
+
"listObjectTypes",
|
|
87
|
+
"listSubTypes",
|
|
88
|
+
"listActions",
|
|
89
|
+
"getActionById",
|
|
90
|
+
"listSyncs",
|
|
91
|
+
"listPipelines",
|
|
92
|
+
"getPipelineById",
|
|
93
|
+
"listSchedules",
|
|
94
|
+
"listObjectProjections",
|
|
95
|
+
"listLinkProjections",
|
|
96
|
+
"listTelemetryProjections",
|
|
97
|
+
"listDatasets",
|
|
98
|
+
"listRules",
|
|
99
|
+
"getDatasetById",
|
|
100
|
+
"getProjectionById",
|
|
101
|
+
"getSyncById",
|
|
102
|
+
"getRuleById",
|
|
103
|
+
] as const
|
|
104
|
+
|
|
105
|
+
const REQUIRED_LIFECYCLE_METHODS = [
|
|
106
|
+
"startScheduler",
|
|
107
|
+
"stopScheduler",
|
|
108
|
+
"startOntologyMaintenance",
|
|
109
|
+
"getOntologyOperationalStatus",
|
|
110
|
+
"checkReadiness",
|
|
111
|
+
"disconnectConnectors",
|
|
112
|
+
"closeLogger",
|
|
113
|
+
"closeBroker",
|
|
114
|
+
] as const
|
|
115
|
+
|
|
116
|
+
function isRecord(value: unknown): value is Record<PropertyKey, unknown> {
|
|
117
|
+
return value !== null && typeof value === "object"
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function hasProperties(
|
|
121
|
+
value: Record<PropertyKey, unknown>,
|
|
122
|
+
properties: readonly PropertyKey[]
|
|
123
|
+
): boolean {
|
|
124
|
+
return properties.every((property) => property in value)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function hasMethods(value: Record<PropertyKey, unknown>, methods: readonly PropertyKey[]): boolean {
|
|
128
|
+
return methods.every((method) => typeof value[method] === "function")
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function hasWorkflows(value: Record<PropertyKey, unknown>): boolean {
|
|
132
|
+
return isRecord(value.workflows) && hasMethods(value.workflows, ["list", "getById"])
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isSixbInstance(value: unknown): value is LoadedSixb {
|
|
136
|
+
if (!isRecord(value)) return false
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
typeof value.id === "string" &&
|
|
140
|
+
typeof value.projectId === "string" &&
|
|
141
|
+
hasProperties(value, REQUIRED_RUNTIME_PROPERTIES) &&
|
|
142
|
+
hasMethods(value, REQUIRED_DEFINITION_METHODS) &&
|
|
143
|
+
hasMethods(value, REQUIRED_LIFECYCLE_METHODS) &&
|
|
144
|
+
hasMethods(value, ["connector", "list"]) &&
|
|
145
|
+
hasWorkflows(value)
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function loadSixbFromEntry(entry: string): Promise<LoadedSixb> {
|
|
150
|
+
const module = (await import(pathToFileURL(entry).href)) as Record<string, unknown>
|
|
151
|
+
const candidate = module.sixb ?? module.default
|
|
152
|
+
|
|
153
|
+
if (isSixbInstance(candidate)) {
|
|
154
|
+
return candidate
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof candidate === "function") {
|
|
158
|
+
const created = (candidate as () => unknown)()
|
|
159
|
+
const resolved = isPromiseLike(created) ? await created : created
|
|
160
|
+
if (isSixbInstance(resolved)) {
|
|
161
|
+
return resolved
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (isPromiseLike(candidate)) {
|
|
166
|
+
const resolved = await candidate
|
|
167
|
+
if (isSixbInstance(resolved)) {
|
|
168
|
+
return resolved
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
throw new Error(
|
|
173
|
+
"Could not load Sixb runtime from entry. Export `sixb` (or default) as a Sixb instance or Promise<Sixb>."
|
|
174
|
+
)
|
|
175
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every long-running production role, and what its startup has to know about itself.
|
|
3
|
+
*
|
|
4
|
+
* Total over the role union on purpose: a new role does not compile until it answers both
|
|
5
|
+
* questions here.
|
|
6
|
+
*/
|
|
7
|
+
const PRODUCTION_ROLES = {
|
|
8
|
+
api: { onEventPlane: true, usesStorageSchema: true },
|
|
9
|
+
rules: { onEventPlane: true, usesStorageSchema: true },
|
|
10
|
+
scheduler: { onEventPlane: true, usesStorageSchema: true },
|
|
11
|
+
orchestrator: { onEventPlane: true, usesStorageSchema: true },
|
|
12
|
+
worker: { onEventPlane: true, usesStorageSchema: true },
|
|
13
|
+
"worker-group": { onEventPlane: true, usesStorageSchema: true },
|
|
14
|
+
atlas: { onEventPlane: false, usesStorageSchema: false },
|
|
15
|
+
app: { onEventPlane: false, usesStorageSchema: false },
|
|
16
|
+
} as const satisfies Record<string, ProductionRoleFacts>
|
|
17
|
+
|
|
18
|
+
interface ProductionRoleFacts {
|
|
19
|
+
/**
|
|
20
|
+
* Whether the role publishes to or claims from the event plane. `atlas` and `app` only serve
|
|
21
|
+
* a browser bundle, so refusing to boot them on a process-local provider would block a valid
|
|
22
|
+
* UI-only container.
|
|
23
|
+
*/
|
|
24
|
+
readonly onEventPlane: boolean
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Whether the role should bring the storage schema up to date at startup. `atlas` and `app`
|
|
28
|
+
* are the tier facing the internet, and a container serving a bundle has no business holding
|
|
29
|
+
* a DDL grant.
|
|
30
|
+
*/
|
|
31
|
+
readonly usesStorageSchema: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ProductionRole = keyof typeof PRODUCTION_ROLES
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The roles that migrate storage at startup, derived from the map rather than restated: a role
|
|
38
|
+
* declared `usesStorageSchema: false` cannot be passed to `migrateStorageForRole` at all.
|
|
39
|
+
*/
|
|
40
|
+
export type StorageSchemaRole = {
|
|
41
|
+
[Role in ProductionRole]: (typeof PRODUCTION_ROLES)[Role]["usesStorageSchema"] extends true
|
|
42
|
+
? Role
|
|
43
|
+
: never
|
|
44
|
+
}[ProductionRole]
|
|
45
|
+
|
|
46
|
+
export function productionRoleFacts(role: ProductionRole): ProductionRoleFacts {
|
|
47
|
+
return PRODUCTION_ROLES[role]
|
|
48
|
+
}
|