@sixb/server 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 +123 -0
- package/dist/auth/access-token-boundary.d.ts +23 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -0
- package/dist/auth/application-access.d.ts +4 -0
- package/dist/auth/application-access.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +62 -0
- package/dist/auth/browser-origin.d.ts.map +1 -0
- package/dist/auth/csrf.d.ts +2 -0
- package/dist/auth/csrf.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +35 -0
- package/dist/auth/guard.d.ts.map +1 -0
- package/dist/auth/public-routes.d.ts +8 -0
- package/dist/auth/public-routes.d.ts.map +1 -0
- package/dist/auth/responses.d.ts +11 -0
- package/dist/auth/responses.d.ts.map +1 -0
- package/dist/auth/return-to.d.ts +3 -0
- package/dist/auth/return-to.d.ts.map +1 -0
- package/dist/auth/scope.d.ts +43 -0
- package/dist/auth/scope.d.ts.map +1 -0
- package/dist/auth/session-activity.d.ts +4 -0
- package/dist/auth/session-activity.d.ts.map +1 -0
- package/dist/auth/session-cookies.d.ts +12 -0
- package/dist/auth/session-cookies.d.ts.map +1 -0
- package/dist/files/content.d.ts +141 -0
- package/dist/files/content.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11522 -0
- package/dist/index.js.map +78 -0
- package/dist/openapi/security.d.ts +26 -0
- package/dist/openapi/security.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +151 -0
- package/dist/openapi/tags.d.ts.map +1 -0
- package/dist/registerRoutes.d.ts +31 -0
- package/dist/registerRoutes.d.ts.map +1 -0
- package/dist/routes/action-runs.d.ts +244 -0
- package/dist/routes/action-runs.d.ts.map +1 -0
- package/dist/routes/actions.d.ts +183 -0
- package/dist/routes/actions.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts +4 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1061 -0
- package/dist/routes/agents.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +1366 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +108 -0
- package/dist/routes/connectors.d.ts.map +1 -0
- package/dist/routes/datasets.d.ts +339 -0
- package/dist/routes/datasets.d.ts.map +1 -0
- package/dist/routes/events.d.ts +116 -0
- package/dist/routes/events.d.ts.map +1 -0
- package/dist/routes/files.d.ts +340 -0
- package/dist/routes/files.d.ts.map +1 -0
- package/dist/routes/links.d.ts +175 -0
- package/dist/routes/links.d.ts.map +1 -0
- package/dist/routes/logs.d.ts +88 -0
- package/dist/routes/logs.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +450 -0
- package/dist/routes/objects.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +240 -0
- package/dist/routes/ontology.d.ts.map +1 -0
- package/dist/routes/pipelines.d.ts +399 -0
- package/dist/routes/pipelines.d.ts.map +1 -0
- package/dist/routes/project.d.ts +56 -0
- package/dist/routes/project.d.ts.map +1 -0
- package/dist/routes/projections.d.ts +916 -0
- package/dist/routes/projections.d.ts.map +1 -0
- package/dist/routes/rules.d.ts +165 -0
- package/dist/routes/rules.d.ts.map +1 -0
- package/dist/routes/status.d.ts +81 -0
- package/dist/routes/status.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +294 -0
- package/dist/routes/syncs.d.ts.map +1 -0
- package/dist/routes/telemetry.d.ts +241 -0
- package/dist/routes/telemetry.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +89 -0
- package/dist/routes/webhook-runs.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +60 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +973 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/routes/ws/agents.d.ts +99 -0
- package/dist/routes/ws/agents.d.ts.map +1 -0
- package/dist/routes/ws/events.d.ts +89 -0
- package/dist/routes/ws/events.d.ts.map +1 -0
- package/dist/routes/ws/index.d.ts +31 -0
- package/dist/routes/ws/index.d.ts.map +1 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +47 -0
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -0
- package/dist/routes/ws/logs.d.ts +110 -0
- package/dist/routes/ws/logs.d.ts.map +1 -0
- package/dist/schemas/actions.d.ts +644 -0
- package/dist/schemas/actions.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +3157 -0
- package/dist/schemas/agents.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +2023 -0
- package/dist/schemas/auth.d.ts.map +1 -0
- package/dist/schemas/common.d.ts +30 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/connectors.d.ts +82 -0
- package/dist/schemas/connectors.d.ts.map +1 -0
- package/dist/schemas/datasets.d.ts +744 -0
- package/dist/schemas/datasets.d.ts.map +1 -0
- package/dist/schemas/event-payloads.d.ts +477 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -0
- package/dist/schemas/events.d.ts +665 -0
- package/dist/schemas/events.d.ts.map +1 -0
- package/dist/schemas/files.d.ts +241 -0
- package/dist/schemas/files.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +90 -0
- package/dist/schemas/links.d.ts.map +1 -0
- package/dist/schemas/logs.d.ts +231 -0
- package/dist/schemas/logs.d.ts.map +1 -0
- package/dist/schemas/objects.d.ts +288 -0
- package/dist/schemas/objects.d.ts.map +1 -0
- package/dist/schemas/ontology.d.ts +778 -0
- package/dist/schemas/ontology.d.ts.map +1 -0
- package/dist/schemas/pipelines.d.ts +1373 -0
- package/dist/schemas/pipelines.d.ts.map +1 -0
- package/dist/schemas/project.d.ts +9 -0
- package/dist/schemas/project.d.ts.map +1 -0
- package/dist/schemas/projections.d.ts +6992 -0
- package/dist/schemas/projections.d.ts.map +1 -0
- package/dist/schemas/rules.d.ts +204 -0
- package/dist/schemas/rules.d.ts.map +1 -0
- package/dist/schemas/status.d.ts +189 -0
- package/dist/schemas/status.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +538 -0
- package/dist/schemas/syncs.d.ts.map +1 -0
- package/dist/schemas/telemetry.d.ts +265 -0
- package/dist/schemas/telemetry.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +169 -0
- package/dist/schemas/webhook-runs.d.ts.map +1 -0
- package/dist/schemas/workflows.d.ts +1720 -0
- package/dist/schemas/workflows.d.ts.map +1 -0
- package/dist/server.d.ts +64 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/utils/http.d.ts +23 -0
- package/dist/utils/http.d.ts.map +1 -0
- package/dist/utils/request-body.d.ts +15 -0
- package/dist/utils/request-body.d.ts.map +1 -0
- package/dist/utils/ws.d.ts +10 -0
- package/dist/utils/ws.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/auth/access-token-boundary.ts +68 -0
- package/src/auth/application-access.ts +16 -0
- package/src/auth/browser-origin.ts +367 -0
- package/src/auth/csrf.ts +1 -0
- package/src/auth/guard.ts +169 -0
- package/src/auth/public-routes.ts +87 -0
- package/src/auth/responses.ts +54 -0
- package/src/auth/return-to.ts +21 -0
- package/src/auth/scope.ts +64 -0
- package/src/auth/session-activity.ts +19 -0
- package/src/auth/session-cookies.ts +43 -0
- package/src/files/content.ts +419 -0
- package/src/index.ts +2 -0
- package/src/openapi/security.ts +37 -0
- package/src/openapi/tags.ts +151 -0
- package/src/registerRoutes.ts +49 -0
- package/src/routes/action-runs.ts +238 -0
- package/src/routes/actions.ts +126 -0
- package/src/routes/agent-api-gateway.ts +260 -0
- package/src/routes/agents.ts +943 -0
- package/src/routes/auth.ts +2233 -0
- package/src/routes/connectors.ts +69 -0
- package/src/routes/datasets.ts +402 -0
- package/src/routes/events.ts +55 -0
- package/src/routes/files.ts +675 -0
- package/src/routes/links.ts +143 -0
- package/src/routes/logs.ts +65 -0
- package/src/routes/objects.ts +838 -0
- package/src/routes/ontology.ts +138 -0
- package/src/routes/pipelines.ts +332 -0
- package/src/routes/project.ts +16 -0
- package/src/routes/projections.ts +251 -0
- package/src/routes/rules.ts +124 -0
- package/src/routes/status.ts +53 -0
- package/src/routes/syncs.ts +243 -0
- package/src/routes/telemetry.ts +207 -0
- package/src/routes/webhook-runs.ts +81 -0
- package/src/routes/webhooks.ts +508 -0
- package/src/routes/workflows.ts +1333 -0
- package/src/routes/ws/agents.ts +301 -0
- package/src/routes/ws/events.ts +278 -0
- package/src/routes/ws/index.ts +12 -0
- package/src/routes/ws/log-subscription-hub.ts +375 -0
- package/src/routes/ws/logs.ts +117 -0
- package/src/schemas/actions.ts +123 -0
- package/src/schemas/agents.ts +293 -0
- package/src/schemas/auth.ts +317 -0
- package/src/schemas/common.ts +60 -0
- package/src/schemas/connectors.ts +21 -0
- package/src/schemas/datasets.ts +107 -0
- package/src/schemas/event-payloads.ts +79 -0
- package/src/schemas/events.ts +117 -0
- package/src/schemas/files.ts +82 -0
- package/src/schemas/links.ts +38 -0
- package/src/schemas/logs.ts +76 -0
- package/src/schemas/objects.ts +759 -0
- package/src/schemas/ontology.ts +77 -0
- package/src/schemas/pipelines.ts +150 -0
- package/src/schemas/project.ts +5 -0
- package/src/schemas/projections.ts +144 -0
- package/src/schemas/rules.ts +98 -0
- package/src/schemas/status.ts +51 -0
- package/src/schemas/syncs.ts +83 -0
- package/src/schemas/telemetry.ts +59 -0
- package/src/schemas/webhook-runs.ts +40 -0
- package/src/schemas/workflows.ts +229 -0
- package/src/server.ts +383 -0
- package/src/utils/http.ts +108 -0
- package/src/utils/request-body.ts +62 -0
- package/src/utils/ws.ts +59 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
4
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
5
|
+
import { ConnectorParamsSchema, ConnectorSchema } from "../schemas/connectors"
|
|
6
|
+
|
|
7
|
+
function serializeConnector(
|
|
8
|
+
connector: ReturnType<Sixb<readonly OntologySource[]>["listConnectors"]>[number],
|
|
9
|
+
sixb: Sixb<readonly OntologySource[]>
|
|
10
|
+
) {
|
|
11
|
+
return {
|
|
12
|
+
id: connector.id,
|
|
13
|
+
type: connector.adapter.type,
|
|
14
|
+
syncIds: sixb
|
|
15
|
+
.listSyncs()
|
|
16
|
+
.filter((sync) => sync.connector.id === connector.id)
|
|
17
|
+
.map((sync) => sync.id),
|
|
18
|
+
webhooks: sixb
|
|
19
|
+
.listWebhooks()
|
|
20
|
+
.filter((registered) => registered.connector.id === connector.id)
|
|
21
|
+
.map(({ webhook, route }) => ({
|
|
22
|
+
id: webhook.id,
|
|
23
|
+
method: webhook.method,
|
|
24
|
+
route,
|
|
25
|
+
bodyFormat: webhook.body.format,
|
|
26
|
+
hasVerify: typeof webhook.verify === "function",
|
|
27
|
+
hasIdempotency: typeof webhook.idempotencyKey === "function",
|
|
28
|
+
})),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function registerConnectorRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
33
|
+
return app
|
|
34
|
+
.get(
|
|
35
|
+
"/api/connectors",
|
|
36
|
+
() => {
|
|
37
|
+
return sixb.listConnectors().map((connector) => serializeConnector(connector, sixb))
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
response: { 200: ConnectorSchema.array() },
|
|
41
|
+
detail: {
|
|
42
|
+
summary: "List registered connectors",
|
|
43
|
+
tags: [OPENAPI_TAGS.connectors.name],
|
|
44
|
+
operationId: "listConnectors",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
.get(
|
|
49
|
+
"/api/connectors/:connectorId",
|
|
50
|
+
({ params, set }) => {
|
|
51
|
+
const connector = sixb.getConnectorById(params.connectorId)
|
|
52
|
+
if (!connector) {
|
|
53
|
+
set.status = 404
|
|
54
|
+
return { error: "Connector not found" }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return serializeConnector(connector, sixb)
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
params: ConnectorParamsSchema,
|
|
61
|
+
response: { 200: ConnectorSchema, 404: ErrorResponseSchema },
|
|
62
|
+
detail: {
|
|
63
|
+
summary: "Get connector metadata",
|
|
64
|
+
tags: [OPENAPI_TAGS.connectors.name],
|
|
65
|
+
operationId: "getConnector",
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type { DatasetDefinition, OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import type {
|
|
3
|
+
DatasetCatalogState,
|
|
4
|
+
DatasetLatestVersionSummary,
|
|
5
|
+
DatasetVersion,
|
|
6
|
+
} from "@sixb/core/lake-storage"
|
|
7
|
+
import type { Elysia } from "elysia"
|
|
8
|
+
import { requestAuthState } from "../auth/scope"
|
|
9
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
10
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
11
|
+
import {
|
|
12
|
+
DatasetCatalogItemSchema,
|
|
13
|
+
DatasetParamsSchema,
|
|
14
|
+
DatasetRowsQuerySchema,
|
|
15
|
+
DatasetRowsResponseSchema,
|
|
16
|
+
DatasetVersionListResponseSchema,
|
|
17
|
+
DatasetVersionParamsSchema,
|
|
18
|
+
DatasetVersionSchema,
|
|
19
|
+
DatasetVersionsQuerySchema,
|
|
20
|
+
} from "../schemas/datasets"
|
|
21
|
+
import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
|
|
22
|
+
|
|
23
|
+
const DEFAULT_VERSION_LIMIT = 20
|
|
24
|
+
const MAX_VERSION_LIMIT = 100
|
|
25
|
+
const DEFAULT_ROW_LIMIT = 100
|
|
26
|
+
const MAX_ROW_LIMIT = 1_000
|
|
27
|
+
|
|
28
|
+
function parseLimit(value: string | undefined, fallback: number, max: number): number {
|
|
29
|
+
const parsed = parseOptionalInt(value) ?? fallback
|
|
30
|
+
if (parsed < 0) {
|
|
31
|
+
throw new Error("Limit must be greater than or equal to 0")
|
|
32
|
+
}
|
|
33
|
+
return Math.min(parsed, max)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseOffset(value: string | undefined): number {
|
|
37
|
+
const parsed = parseOptionalInt(value) ?? 0
|
|
38
|
+
if (parsed < 0) {
|
|
39
|
+
throw new Error("Offset must be greater than or equal to 0")
|
|
40
|
+
}
|
|
41
|
+
return parsed
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function serializeDatasetVersion(version: DatasetVersion) {
|
|
45
|
+
return DatasetVersionSchema.parse({
|
|
46
|
+
datasetId: version.datasetId,
|
|
47
|
+
versionId: version.versionId,
|
|
48
|
+
parentVersionId: version.parentVersionId,
|
|
49
|
+
mode: version.mode,
|
|
50
|
+
createdAt: toIsoString(version.createdAt),
|
|
51
|
+
schema: version.schema,
|
|
52
|
+
producer: version.producer,
|
|
53
|
+
inputs: version.inputs,
|
|
54
|
+
rowCount: version.rowCount,
|
|
55
|
+
sizeBytes: version.sizeBytes,
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface DatasetReferences {
|
|
60
|
+
readonly syncIds: string[]
|
|
61
|
+
readonly sourcePipelineIds: string[]
|
|
62
|
+
readonly targetPipelineIds: string[]
|
|
63
|
+
readonly projectionIds: string[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const EMPTY_DATASET_REFERENCES: DatasetReferences = {
|
|
67
|
+
syncIds: [],
|
|
68
|
+
sourcePipelineIds: [],
|
|
69
|
+
targetPipelineIds: [],
|
|
70
|
+
projectionIds: [],
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Index dataset references in a single pass so the catalog routes never rescan
|
|
75
|
+
* syncs, pipelines, and projections once per dataset.
|
|
76
|
+
*/
|
|
77
|
+
function buildDatasetReferenceIndex(
|
|
78
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
79
|
+
scoped: ReturnType<typeof requestAuthState>["scoped"] = null
|
|
80
|
+
): Map<string, DatasetReferences> {
|
|
81
|
+
const index = new Map<string, DatasetReferences>()
|
|
82
|
+
const referencesFor = (datasetId: string): DatasetReferences => {
|
|
83
|
+
let references = index.get(datasetId)
|
|
84
|
+
if (!references) {
|
|
85
|
+
references = { syncIds: [], sourcePipelineIds: [], targetPipelineIds: [], projectionIds: [] }
|
|
86
|
+
index.set(datasetId, references)
|
|
87
|
+
}
|
|
88
|
+
return references
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const sync of scoped ? scoped.listSyncs() : sixb.listSyncs()) {
|
|
92
|
+
referencesFor(sync.target.dataset.id).syncIds.push(sync.id)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const pipeline of scoped ? scoped.listPipelines() : sixb.listPipelines()) {
|
|
96
|
+
const sourceDatasetIds = new Set<string>()
|
|
97
|
+
const targetDatasetIds = new Set<string>()
|
|
98
|
+
for (const node of pipeline.graph.nodes) {
|
|
99
|
+
for (const input of Object.values(node.step.inputs)) {
|
|
100
|
+
sourceDatasetIds.add(input.id)
|
|
101
|
+
}
|
|
102
|
+
targetDatasetIds.add(node.step.output.id)
|
|
103
|
+
}
|
|
104
|
+
for (const datasetId of sourceDatasetIds) {
|
|
105
|
+
referencesFor(datasetId).sourcePipelineIds.push(pipeline.id)
|
|
106
|
+
}
|
|
107
|
+
for (const datasetId of targetDatasetIds) {
|
|
108
|
+
referencesFor(datasetId).targetPipelineIds.push(pipeline.id)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Projections inherit dataset visibility: a scoped caller sees a
|
|
113
|
+
// projection's lineage only when it can view the projection's source
|
|
114
|
+
// dataset. Privileged callers (no scoped runtime) see them all.
|
|
115
|
+
for (const projection of [
|
|
116
|
+
...sixb.listObjectProjections(),
|
|
117
|
+
...sixb.listLinkProjections(),
|
|
118
|
+
...sixb.listTelemetryProjections(),
|
|
119
|
+
]) {
|
|
120
|
+
if (!scoped || scoped.getDatasetById(projection.datasetId)) {
|
|
121
|
+
referencesFor(projection.datasetId).projectionIds.push(projection.id)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return index
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function serializeLatestVersionSummary(summary: DatasetLatestVersionSummary) {
|
|
129
|
+
return {
|
|
130
|
+
datasetId: summary.datasetId,
|
|
131
|
+
versionId: summary.versionId,
|
|
132
|
+
mode: summary.mode,
|
|
133
|
+
createdAt: toIsoString(summary.createdAt),
|
|
134
|
+
rowCount: summary.rowCount,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function serializeDatasetCatalogItem(
|
|
139
|
+
dataset: DatasetDefinition,
|
|
140
|
+
state: DatasetCatalogState | undefined,
|
|
141
|
+
references: DatasetReferences
|
|
142
|
+
) {
|
|
143
|
+
return DatasetCatalogItemSchema.parse({
|
|
144
|
+
...dataset,
|
|
145
|
+
materialized: state?.materialized ?? false,
|
|
146
|
+
latestVersion: state?.latestVersion ? serializeLatestVersionSummary(state.latestVersion) : null,
|
|
147
|
+
syncIds: references.syncIds,
|
|
148
|
+
sourcePipelineIds: references.sourcePipelineIds,
|
|
149
|
+
targetPipelineIds: references.targetPipelineIds,
|
|
150
|
+
projectionIds: references.projectionIds,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function serializeDatasetCatalogItems(
|
|
155
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
156
|
+
definitions: readonly DatasetDefinition[],
|
|
157
|
+
scoped: ReturnType<typeof requestAuthState>["scoped"] = null
|
|
158
|
+
) {
|
|
159
|
+
const references = buildDatasetReferenceIndex(sixb, scoped)
|
|
160
|
+
const states = await sixb.lakeStorage.listDatasetCatalogState(definitions.map((d) => d.id))
|
|
161
|
+
const stateByDatasetId = new Map(states.map((state) => [state.datasetId, state]))
|
|
162
|
+
|
|
163
|
+
return definitions.map((definition) =>
|
|
164
|
+
serializeDatasetCatalogItem(
|
|
165
|
+
definition,
|
|
166
|
+
stateByDatasetId.get(definition.id),
|
|
167
|
+
references.get(definition.id) ?? EMPTY_DATASET_REFERENCES
|
|
168
|
+
)
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function requireDataset(
|
|
173
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
174
|
+
scoped: ReturnType<typeof requestAuthState>["scoped"],
|
|
175
|
+
datasetId: string
|
|
176
|
+
) {
|
|
177
|
+
const dataset = scoped ? scoped.getDatasetById(datasetId) : sixb.getDatasetById(datasetId)
|
|
178
|
+
if (!dataset) {
|
|
179
|
+
throw new Error("Dataset not found")
|
|
180
|
+
}
|
|
181
|
+
return dataset
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function parseColumns(value: string | undefined): readonly string[] | undefined {
|
|
185
|
+
if (!value) return undefined
|
|
186
|
+
|
|
187
|
+
const columns = value
|
|
188
|
+
.split(",")
|
|
189
|
+
.map((column) => column.trim())
|
|
190
|
+
.filter(Boolean)
|
|
191
|
+
|
|
192
|
+
return columns.length > 0 ? columns : undefined
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function resolveColumns(version: DatasetVersion, requested: readonly string[] | undefined) {
|
|
196
|
+
const available = new Set(version.schema.columns.map((column) => column.name))
|
|
197
|
+
const selected = requested ?? version.schema.columns.map((column) => column.name)
|
|
198
|
+
|
|
199
|
+
for (const column of selected) {
|
|
200
|
+
if (!available.has(column)) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Dataset '${version.datasetId}' does not have column '${column}' at version '${version.versionId}'`
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return selected
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function collectRows(rows: AsyncIterable<Readonly<Record<string, unknown>>>) {
|
|
211
|
+
const collected: Record<string, unknown>[] = []
|
|
212
|
+
for await (const row of rows) {
|
|
213
|
+
collected.push({ ...row })
|
|
214
|
+
}
|
|
215
|
+
return collected
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function registerDatasetRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
219
|
+
return app
|
|
220
|
+
.get(
|
|
221
|
+
"/api/datasets",
|
|
222
|
+
async (context) => {
|
|
223
|
+
const { set } = context
|
|
224
|
+
const { scoped } = requestAuthState(context)
|
|
225
|
+
try {
|
|
226
|
+
const definitions = scoped ? scoped.listDatasets() : sixb.listDatasets()
|
|
227
|
+
return await serializeDatasetCatalogItems(sixb, definitions, scoped)
|
|
228
|
+
} catch (error) {
|
|
229
|
+
set.status = 400
|
|
230
|
+
return { error: error instanceof Error ? error.message : String(error) }
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
response: { 200: DatasetCatalogItemSchema.array(), 400: ErrorResponseSchema },
|
|
235
|
+
detail: {
|
|
236
|
+
summary: "List registered datasets",
|
|
237
|
+
tags: [OPENAPI_TAGS.datasets.name],
|
|
238
|
+
operationId: "listDatasets",
|
|
239
|
+
},
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
.get(
|
|
243
|
+
"/api/datasets/:datasetId",
|
|
244
|
+
async (context) => {
|
|
245
|
+
const { params, set } = context
|
|
246
|
+
const { scoped } = requestAuthState(context)
|
|
247
|
+
try {
|
|
248
|
+
const dataset = requireDataset(sixb, scoped, params.datasetId)
|
|
249
|
+
const [state] = await sixb.lakeStorage.listDatasetCatalogState([dataset.id])
|
|
250
|
+
const references = buildDatasetReferenceIndex(sixb, scoped).get(dataset.id)
|
|
251
|
+
return serializeDatasetCatalogItem(dataset, state, references ?? EMPTY_DATASET_REFERENCES)
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return handleRouteError(error, set)
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
params: DatasetParamsSchema,
|
|
258
|
+
response: {
|
|
259
|
+
200: DatasetCatalogItemSchema,
|
|
260
|
+
400: ErrorResponseSchema,
|
|
261
|
+
404: ErrorResponseSchema,
|
|
262
|
+
},
|
|
263
|
+
detail: {
|
|
264
|
+
summary: "Get dataset metadata",
|
|
265
|
+
tags: [OPENAPI_TAGS.datasets.name],
|
|
266
|
+
operationId: "getDataset",
|
|
267
|
+
},
|
|
268
|
+
}
|
|
269
|
+
)
|
|
270
|
+
.get(
|
|
271
|
+
"/api/datasets/:datasetId/versions",
|
|
272
|
+
async (context) => {
|
|
273
|
+
const { params, query, set } = context
|
|
274
|
+
const { scoped } = requestAuthState(context)
|
|
275
|
+
try {
|
|
276
|
+
requireDataset(sixb, scoped, params.datasetId)
|
|
277
|
+
const parsed = DatasetVersionsQuerySchema.parse(query)
|
|
278
|
+
const limit = parseLimit(parsed.limit, DEFAULT_VERSION_LIMIT, MAX_VERSION_LIMIT)
|
|
279
|
+
const versions = await sixb.lakeStorage.listVersions(params.datasetId, limit)
|
|
280
|
+
|
|
281
|
+
return DatasetVersionListResponseSchema.parse({
|
|
282
|
+
versions: versions.map(serializeDatasetVersion),
|
|
283
|
+
count: versions.length,
|
|
284
|
+
})
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return handleRouteError(error, set)
|
|
287
|
+
}
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
params: DatasetParamsSchema,
|
|
291
|
+
query: DatasetVersionsQuerySchema,
|
|
292
|
+
response: {
|
|
293
|
+
200: DatasetVersionListResponseSchema,
|
|
294
|
+
400: ErrorResponseSchema,
|
|
295
|
+
404: ErrorResponseSchema,
|
|
296
|
+
},
|
|
297
|
+
detail: {
|
|
298
|
+
summary: "List dataset versions",
|
|
299
|
+
tags: [OPENAPI_TAGS.datasetVersions.name],
|
|
300
|
+
operationId: "listDatasetVersions",
|
|
301
|
+
},
|
|
302
|
+
}
|
|
303
|
+
)
|
|
304
|
+
.get(
|
|
305
|
+
"/api/datasets/:datasetId/versions/:versionId",
|
|
306
|
+
async (context) => {
|
|
307
|
+
const { params, set } = context
|
|
308
|
+
const { scoped } = requestAuthState(context)
|
|
309
|
+
try {
|
|
310
|
+
requireDataset(sixb, scoped, params.datasetId)
|
|
311
|
+
const version = await sixb.lakeStorage.getVersion(params.datasetId, params.versionId)
|
|
312
|
+
if (!version) {
|
|
313
|
+
set.status = 404
|
|
314
|
+
return { error: "Dataset version not found" }
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return serializeDatasetVersion(version)
|
|
318
|
+
} catch (error) {
|
|
319
|
+
return handleRouteError(error, set)
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
params: DatasetVersionParamsSchema,
|
|
324
|
+
response: {
|
|
325
|
+
200: DatasetVersionSchema,
|
|
326
|
+
400: ErrorResponseSchema,
|
|
327
|
+
404: ErrorResponseSchema,
|
|
328
|
+
},
|
|
329
|
+
detail: {
|
|
330
|
+
summary: "Get dataset version",
|
|
331
|
+
tags: [OPENAPI_TAGS.datasetVersions.name],
|
|
332
|
+
operationId: "getDatasetVersion",
|
|
333
|
+
},
|
|
334
|
+
}
|
|
335
|
+
)
|
|
336
|
+
.get(
|
|
337
|
+
"/api/datasets/:datasetId/rows",
|
|
338
|
+
async (context) => {
|
|
339
|
+
const { params, query, set } = context
|
|
340
|
+
const { scoped } = requestAuthState(context)
|
|
341
|
+
try {
|
|
342
|
+
requireDataset(sixb, scoped, params.datasetId)
|
|
343
|
+
const parsed = DatasetRowsQuerySchema.parse(query)
|
|
344
|
+
const limit = parseLimit(parsed.limit, DEFAULT_ROW_LIMIT, MAX_ROW_LIMIT)
|
|
345
|
+
const offset = parseOffset(parsed.offset)
|
|
346
|
+
const version = parsed.versionId
|
|
347
|
+
? await sixb.lakeStorage.getVersion(params.datasetId, parsed.versionId)
|
|
348
|
+
: await sixb.lakeStorage.getLatestVersion(params.datasetId)
|
|
349
|
+
|
|
350
|
+
if (!version) {
|
|
351
|
+
set.status = 404
|
|
352
|
+
return { error: "Dataset version not found" }
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const requestedColumns = parseColumns(parsed.columns)
|
|
356
|
+
const columns = resolveColumns(version, requestedColumns)
|
|
357
|
+
const rows = await collectRows(
|
|
358
|
+
sixb.lakeStorage.readRows({
|
|
359
|
+
datasetId: params.datasetId,
|
|
360
|
+
versionId: version.versionId,
|
|
361
|
+
columns: requestedColumns,
|
|
362
|
+
limit,
|
|
363
|
+
offset,
|
|
364
|
+
})
|
|
365
|
+
)
|
|
366
|
+
const hasMore =
|
|
367
|
+
version.rowCount === undefined
|
|
368
|
+
? rows.length === limit
|
|
369
|
+
: offset + rows.length < version.rowCount
|
|
370
|
+
|
|
371
|
+
return DatasetRowsResponseSchema.parse({
|
|
372
|
+
datasetId: params.datasetId,
|
|
373
|
+
versionId: version.versionId,
|
|
374
|
+
version: serializeDatasetVersion(version),
|
|
375
|
+
columns: [...columns],
|
|
376
|
+
rows,
|
|
377
|
+
count: rows.length,
|
|
378
|
+
limit,
|
|
379
|
+
offset,
|
|
380
|
+
total: version.rowCount,
|
|
381
|
+
hasMore,
|
|
382
|
+
})
|
|
383
|
+
} catch (error) {
|
|
384
|
+
return handleRouteError(error, set)
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
params: DatasetParamsSchema,
|
|
389
|
+
query: DatasetRowsQuerySchema,
|
|
390
|
+
response: {
|
|
391
|
+
200: DatasetRowsResponseSchema,
|
|
392
|
+
400: ErrorResponseSchema,
|
|
393
|
+
404: ErrorResponseSchema,
|
|
394
|
+
},
|
|
395
|
+
detail: {
|
|
396
|
+
summary: "Preview dataset rows",
|
|
397
|
+
tags: [OPENAPI_TAGS.datasetRows.name],
|
|
398
|
+
operationId: "listDatasetRows",
|
|
399
|
+
},
|
|
400
|
+
}
|
|
401
|
+
)
|
|
402
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { AuthorizationError, type OntologySource, type Sixb } from "@sixb/core"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
4
|
+
import { requestAuthState } from "../auth/scope"
|
|
5
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
6
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
7
|
+
import { EventsQuerySchema, EventsResponseSchema } from "../schemas/events"
|
|
8
|
+
import { parseOptionalInt } from "../utils/http"
|
|
9
|
+
export function registerEventRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
10
|
+
return app.get(
|
|
11
|
+
"/api/events",
|
|
12
|
+
async (context) => {
|
|
13
|
+
const { query, set } = context
|
|
14
|
+
const { scoped } = requestAuthState(context)
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const parsed = EventsQuerySchema.parse(query)
|
|
18
|
+
const input = {
|
|
19
|
+
topics: parsed.topic ? [parsed.topic] : undefined,
|
|
20
|
+
types: parsed.type ? [parsed.type] : undefined,
|
|
21
|
+
afterCursor: parsed.afterCursor,
|
|
22
|
+
limit: parseOptionalInt(parsed.limit),
|
|
23
|
+
}
|
|
24
|
+
const events = scoped ? await scoped.readEvents(input) : await sixb.events.read(input)
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
count: events.length,
|
|
28
|
+
events: [...events],
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error instanceof AuthorizationError) {
|
|
32
|
+
set.status = 403
|
|
33
|
+
return { error: error.message }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
set.status = 400
|
|
37
|
+
return { error: error instanceof Error ? error.message : String(error) }
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
query: EventsQuerySchema,
|
|
42
|
+
response: {
|
|
43
|
+
200: EventsResponseSchema,
|
|
44
|
+
400: ErrorResponseSchema,
|
|
45
|
+
403: ErrorResponseSchema,
|
|
46
|
+
},
|
|
47
|
+
detail: {
|
|
48
|
+
summary: "Read domain events",
|
|
49
|
+
tags: [OPENAPI_TAGS.events.name],
|
|
50
|
+
operationId: "listEvents",
|
|
51
|
+
security: bearerSecurityRequirement("listEvents"),
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
}
|