@sixb/server 0.1.16 → 0.1.17
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/README.md +6 -0
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts.map +1 -1
- package/dist/index.js +406 -327
- package/dist/index.js.map +17 -16
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +8 -0
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +1 -1
- package/dist/routes/file-downloads.d.ts +98 -0
- package/dist/routes/file-downloads.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +3 -0
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +4 -4
- package/dist/routes/status.d.ts +0 -4
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +3 -0
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +11 -0
- package/dist/routes/ws/logs.d.ts.map +1 -1
- package/dist/routes/ws/subscription-hub.d.ts +66 -0
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +33 -0
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +224 -100
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-usage.d.ts +3 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +2 -2
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +18 -18
- package/dist/schemas/status.d.ts +0 -26
- package/dist/schemas/status.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +35 -0
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/auth/public-routes.ts +10 -0
- package/src/auth/shared-access.ts +48 -6
- package/src/registerRoutes.ts +2 -0
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/file-downloads.ts +48 -0
- package/src/routes/objects.ts +13 -3
- package/src/routes/ws/events.ts +96 -133
- package/src/routes/ws/logs.ts +67 -5
- package/src/routes/ws/{log-subscription-hub.ts → subscription-hub.ts} +137 -127
- package/src/schemas/ai-accounting.ts +5 -0
- package/src/schemas/ai-usage.ts +3 -0
- package/src/schemas/objects.ts +28 -2
- package/src/schemas/status.ts +0 -6
- package/src/server.ts +5 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +0 -50
- package/dist/routes/ws/log-subscription-hub.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import { cors } from "@elysiajs/cors";
|
|
|
4
4
|
import { openapi } from "@elysiajs/openapi";
|
|
5
5
|
import { CSRF_HEADER_NAME as CSRF_HEADER_NAME2, setRequestClientAddress } from "@sixb/core/internal/auth";
|
|
6
6
|
import { bindRequestExecution } from "@sixb/core/internal/request-execution";
|
|
7
|
+
import { setApiPublicOrigin } from "@sixb/core/internal/runtime";
|
|
7
8
|
import { Elysia } from "elysia";
|
|
8
9
|
import { websocket as elysiaWebSocket } from "elysia/ws";
|
|
9
10
|
import { zodToJsonSchema as zodToJsonSchema2 } from "zod-to-json-schema";
|
|
@@ -317,11 +318,13 @@ function isSessionTerminationRequest(request) {
|
|
|
317
318
|
|
|
318
319
|
// src/auth/shared-access.ts
|
|
319
320
|
var SHARED_ACCESS_GRANT_HEADER_NAME = "x-sixb-share-grant";
|
|
321
|
+
var SHARED_ACCESS_GRANT_QUERY_PARAM = "shareGrant";
|
|
320
322
|
var SHARED_SESSION_COOKIE_PREFIX = "sixb_share_session_";
|
|
321
323
|
var SHARED_CSRF_COOKIE_PREFIX = "sixb_share_csrf_";
|
|
322
324
|
var SHARED_COOKIE_PATH = "/api";
|
|
323
325
|
var STORED_IDENTIFIER_MAX_LENGTH = 128;
|
|
324
326
|
var SHARED_SESSION_ROUTES = SIXB_API_ROUTES2.filter((route) => route.sharedSession);
|
|
327
|
+
var SHARED_FILE_CONTENT_ROUTES = SHARED_SESSION_ROUTES.filter((route) => (route.method === "GET" || route.method === "HEAD") && route.path.endsWith("/files/content"));
|
|
325
328
|
|
|
326
329
|
class SharedAccessBoundary {
|
|
327
330
|
host;
|
|
@@ -334,24 +337,28 @@ class SharedAccessBoundary {
|
|
|
334
337
|
preflightResponse(request) {
|
|
335
338
|
if (isSharedAccessPublicRoute(request))
|
|
336
339
|
return;
|
|
337
|
-
const
|
|
338
|
-
if (
|
|
340
|
+
const selection = selectSharedAccessGrant(request);
|
|
341
|
+
if (selection.kind === "absent")
|
|
339
342
|
return;
|
|
340
|
-
if (request.headers.has("authorization") || !isSharedSessionRoute(request)) {
|
|
343
|
+
if (selection.kind === "conflict" || request.headers.has("authorization") || !isSharedSessionRoute(request)) {
|
|
341
344
|
return sharedAccessForbiddenResponse();
|
|
342
345
|
}
|
|
343
|
-
if (!isStoredIdentifier(
|
|
346
|
+
if (!isStoredIdentifier(selection.grantId))
|
|
344
347
|
return sharedAccessUnauthenticatedResponse();
|
|
345
348
|
}
|
|
346
349
|
async resolveRequest(request) {
|
|
347
350
|
if (isSharedAccessPublicRoute(request))
|
|
348
351
|
return { kind: "absent" };
|
|
349
|
-
const
|
|
350
|
-
if (
|
|
352
|
+
const selection = selectSharedAccessGrant(request);
|
|
353
|
+
if (selection.kind === "absent")
|
|
351
354
|
return { kind: "absent" };
|
|
355
|
+
if (selection.kind === "conflict") {
|
|
356
|
+
return { kind: "deny", response: sharedAccessForbiddenResponse() };
|
|
357
|
+
}
|
|
352
358
|
const preflight = this.preflightResponse(request);
|
|
353
359
|
if (preflight)
|
|
354
360
|
return { kind: "deny", response: preflight };
|
|
361
|
+
const selectedGrantId = selection.grantId;
|
|
355
362
|
let resolved;
|
|
356
363
|
try {
|
|
357
364
|
resolved = await this.resolveSession(request, selectedGrantId);
|
|
@@ -510,6 +517,20 @@ function isSharedAccessPublicPath(pathname, requestMethod) {
|
|
|
510
517
|
return false;
|
|
511
518
|
return matchesPathPattern2(normalizedPathname, `/api/shared-access/:grantId/${suffix}`);
|
|
512
519
|
}
|
|
520
|
+
function selectSharedAccessGrant(request) {
|
|
521
|
+
const header = request.headers.get(SHARED_ACCESS_GRANT_HEADER_NAME);
|
|
522
|
+
const url = new URL(request.url);
|
|
523
|
+
const method = request.method.toUpperCase();
|
|
524
|
+
const pathname = normalizeRoutePath2(url.pathname);
|
|
525
|
+
const queryValues = SHARED_FILE_CONTENT_ROUTES.some((route) => route.method === method && matchesPathPattern2(pathname, route.path)) ? url.searchParams.getAll(SHARED_ACCESS_GRANT_QUERY_PARAM) : [];
|
|
526
|
+
const [query, ...extra] = queryValues;
|
|
527
|
+
if (query === undefined) {
|
|
528
|
+
return header === null ? { kind: "absent" } : { kind: "selected", grantId: header };
|
|
529
|
+
}
|
|
530
|
+
if (extra.length > 0 || header !== null && header !== query)
|
|
531
|
+
return { kind: "conflict" };
|
|
532
|
+
return { kind: "selected", grantId: query };
|
|
533
|
+
}
|
|
513
534
|
function isSharedSessionRoute(request) {
|
|
514
535
|
const method = request.method.toUpperCase();
|
|
515
536
|
const pathname = normalizeRoutePath2(new URL(request.url).pathname);
|
|
@@ -664,6 +685,7 @@ function sessionCanAccessApplication(host, session, audience) {
|
|
|
664
685
|
// src/auth/public-routes.ts
|
|
665
686
|
import { AGENT_API_GATEWAY_PREFIX } from "@sixb/core/internal/agents";
|
|
666
687
|
import { isCsrfExemptMethod as isCsrfExemptMethod3 } from "@sixb/core/internal/auth";
|
|
688
|
+
import { FILE_DOWNLOAD_ROUTE_PREFIX } from "@sixb/core/internal/blob-storage";
|
|
667
689
|
var CONNECTOR_OAUTH_CALLBACK_PATH = "/auth/connectors/callback";
|
|
668
690
|
function classifyRoute(request) {
|
|
669
691
|
const url = new URL(request.url);
|
|
@@ -702,6 +724,9 @@ function isPublicRoute(pathname, method) {
|
|
|
702
724
|
if (pathname.startsWith(`${AGENT_API_GATEWAY_PREFIX}/`)) {
|
|
703
725
|
return true;
|
|
704
726
|
}
|
|
727
|
+
if (pathname.startsWith(FILE_DOWNLOAD_ROUTE_PREFIX) && (normalizedMethod === "GET" || normalizedMethod === "HEAD")) {
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
705
730
|
if (pathname.startsWith("/api/webhooks/") && normalizedMethod === "POST") {
|
|
706
731
|
return true;
|
|
707
732
|
}
|
|
@@ -3038,6 +3063,7 @@ var AiUsageSummarySchema = z6.object({
|
|
|
3038
3063
|
cacheWriteInputTokens: TokenCountSchema.optional(),
|
|
3039
3064
|
textOutputTokens: TokenCountSchema.optional(),
|
|
3040
3065
|
reasoningOutputTokens: TokenCountSchema.optional(),
|
|
3066
|
+
audioDurationMs: TokenCountSchema.optional().describe("Audio content duration in milliseconds; not billed duration."),
|
|
3041
3067
|
reportingStatus: z6.enum(["complete", "partial", "unavailable"])
|
|
3042
3068
|
});
|
|
3043
3069
|
|
|
@@ -3120,7 +3146,8 @@ var AiAccountingAggregateSchema = z8.object({
|
|
|
3120
3146
|
cacheReadInputTokens: z8.number().int().nonnegative(),
|
|
3121
3147
|
cacheWriteInputTokens: z8.number().int().nonnegative(),
|
|
3122
3148
|
textOutputTokens: z8.number().int().nonnegative(),
|
|
3123
|
-
reasoningOutputTokens: z8.number().int().nonnegative()
|
|
3149
|
+
reasoningOutputTokens: z8.number().int().nonnegative(),
|
|
3150
|
+
audioDurationMs: z8.number().int().nonnegative()
|
|
3124
3151
|
})
|
|
3125
3152
|
}),
|
|
3126
3153
|
costs: AiCostSummarySchema
|
|
@@ -3171,7 +3198,8 @@ var AiCostComponentSchema = z8.object({
|
|
|
3171
3198
|
"tokens.input.cacheWrite1h",
|
|
3172
3199
|
"tokens.output.total",
|
|
3173
3200
|
"tokens.output.text",
|
|
3174
|
-
"tokens.output.reasoning"
|
|
3201
|
+
"tokens.output.reasoning",
|
|
3202
|
+
"audio.input.milliseconds"
|
|
3175
3203
|
]),
|
|
3176
3204
|
quantity: IntegerStringSchema,
|
|
3177
3205
|
rateAmountNanosPerMillion: IntegerStringSchema,
|
|
@@ -3239,6 +3267,7 @@ var AiModelCallUsageRecordSchema = z8.object({
|
|
|
3239
3267
|
callId: z8.string(),
|
|
3240
3268
|
providerId: z8.string(),
|
|
3241
3269
|
requestedModelId: z8.string(),
|
|
3270
|
+
modelKind: z8.enum(["language", "image", "video", "embedding", "decision", "transcription", "reranking"]).optional(),
|
|
3242
3271
|
requestedReasoning: ModelReasoningSchema.optional(),
|
|
3243
3272
|
responseModelId: z8.string().optional(),
|
|
3244
3273
|
responseId: z8.string(),
|
|
@@ -4347,6 +4376,7 @@ function serializeModelCall(item) {
|
|
|
4347
4376
|
callId: item.usage.callId,
|
|
4348
4377
|
providerId: item.usage.providerId,
|
|
4349
4378
|
requestedModelId: item.usage.requestedModelId,
|
|
4379
|
+
modelKind: item.usage.modelKind,
|
|
4350
4380
|
providerIds: item.usage.providerIds ?? null,
|
|
4351
4381
|
requestedReasoning: item.usage.requestedReasoning,
|
|
4352
4382
|
responseModelId: item.usage.responseModelId,
|
|
@@ -6021,6 +6051,31 @@ function registerEventRoutes(app, _host) {
|
|
|
6021
6051
|
});
|
|
6022
6052
|
}
|
|
6023
6053
|
|
|
6054
|
+
// src/routes/file-downloads.ts
|
|
6055
|
+
import { FILE_DOWNLOAD_ROUTE_PREFIX as FILE_DOWNLOAD_ROUTE_PREFIX2, resolveFileDownload } from "@sixb/core/internal/blob-storage";
|
|
6056
|
+
function registerFileDownloadRoutes(app, host) {
|
|
6057
|
+
const path = `${FILE_DOWNLOAD_ROUTE_PREFIX2}:token`;
|
|
6058
|
+
return app.get(path, (context) => fileDownloadResponse(host, context), { detail: { hide: true } }).head(path, (context) => fileDownloadResponse(host, context, { head: true }), {
|
|
6059
|
+
detail: { hide: true }
|
|
6060
|
+
});
|
|
6061
|
+
}
|
|
6062
|
+
async function fileDownloadResponse(host, { params, request, set }, options = {}) {
|
|
6063
|
+
const fileRef = await resolveFileDownload(host, params.token);
|
|
6064
|
+
const response = fileRef && await createFileContentResponse({
|
|
6065
|
+
blobStorage: host.blobStorage,
|
|
6066
|
+
fileRef,
|
|
6067
|
+
head: options.head,
|
|
6068
|
+
rangeHeader: request.headers.get("range"),
|
|
6069
|
+
ifNoneMatchHeader: request.headers.get("if-none-match"),
|
|
6070
|
+
ifRangeHeader: request.headers.get("if-range")
|
|
6071
|
+
});
|
|
6072
|
+
if (!response) {
|
|
6073
|
+
set.status = 404;
|
|
6074
|
+
return { error: "File not found" };
|
|
6075
|
+
}
|
|
6076
|
+
return response;
|
|
6077
|
+
}
|
|
6078
|
+
|
|
6024
6079
|
// src/schemas/links.ts
|
|
6025
6080
|
import { z as z16 } from "zod";
|
|
6026
6081
|
var LinkSourceParamsSchema = z16.object({
|
|
@@ -6486,6 +6541,11 @@ var ObjectQuerySchema = z18.lazy(() => z18.discriminatedUnion("kind", [
|
|
|
6486
6541
|
profile: z18.string().min(1),
|
|
6487
6542
|
k: z18.number().int().positive()
|
|
6488
6543
|
}).strict(),
|
|
6544
|
+
z18.object({
|
|
6545
|
+
kind: z18.literal("rerank"),
|
|
6546
|
+
input: ObjectQuerySchema,
|
|
6547
|
+
model: z18.object({ provider: z18.string().min(1), modelId: z18.string().min(1) }).strict()
|
|
6548
|
+
}).strict(),
|
|
6489
6549
|
z18.object({
|
|
6490
6550
|
kind: z18.literal("traverse"),
|
|
6491
6551
|
input: ObjectQuerySchema,
|
|
@@ -6623,7 +6683,7 @@ var ObjectQueryOpenApiSchemas = {
|
|
|
6623
6683
|
required: ["mode", "providerIssues", "fallbackIssues", "issues"],
|
|
6624
6684
|
additionalProperties: false,
|
|
6625
6685
|
properties: {
|
|
6626
|
-
mode: { type: "string", enum: ["pushdown", "fallback", "rejected"] },
|
|
6686
|
+
mode: { type: "string", enum: ["pushdown", "fallback", "pipeline", "rejected"] },
|
|
6627
6687
|
providerIssues: {
|
|
6628
6688
|
type: "array",
|
|
6629
6689
|
items: { $ref: "#/components/schemas/ObjectQueryIssue" }
|
|
@@ -6703,9 +6763,10 @@ var ObjectQueryOpenApiSchemas = {
|
|
|
6703
6763
|
},
|
|
6704
6764
|
ObjectQueryFacetsResponse: {
|
|
6705
6765
|
type: "object",
|
|
6706
|
-
required: ["facets", "plan"],
|
|
6766
|
+
required: ["total", "facets", "plan"],
|
|
6707
6767
|
additionalProperties: false,
|
|
6708
6768
|
properties: {
|
|
6769
|
+
total: { type: "number" },
|
|
6709
6770
|
facets: {
|
|
6710
6771
|
type: "array",
|
|
6711
6772
|
items: { $ref: "#/components/schemas/ObjectQueryFacetResult" }
|
|
@@ -6898,6 +6959,24 @@ var ObjectQueryOpenApiSchemas = {
|
|
|
6898
6959
|
k: { type: "integer", minimum: 1 }
|
|
6899
6960
|
}
|
|
6900
6961
|
},
|
|
6962
|
+
{
|
|
6963
|
+
type: "object",
|
|
6964
|
+
required: ["kind", "input", "model"],
|
|
6965
|
+
additionalProperties: false,
|
|
6966
|
+
properties: {
|
|
6967
|
+
kind: { type: "string", enum: ["rerank"] },
|
|
6968
|
+
input: objectQueryRef,
|
|
6969
|
+
model: {
|
|
6970
|
+
type: "object",
|
|
6971
|
+
required: ["provider", "modelId"],
|
|
6972
|
+
additionalProperties: false,
|
|
6973
|
+
properties: {
|
|
6974
|
+
provider: { type: "string", minLength: 1 },
|
|
6975
|
+
modelId: { type: "string", minLength: 1 }
|
|
6976
|
+
}
|
|
6977
|
+
}
|
|
6978
|
+
}
|
|
6979
|
+
},
|
|
6901
6980
|
{
|
|
6902
6981
|
type: "object",
|
|
6903
6982
|
required: ["kind", "input", "linkId", "direction"],
|
|
@@ -7077,7 +7156,10 @@ var ObjectQueryOpenApiSchemas = {
|
|
|
7077
7156
|
};
|
|
7078
7157
|
|
|
7079
7158
|
// src/routes/objects.ts
|
|
7080
|
-
var
|
|
7159
|
+
var ObjectFileContentRouteQuerySchema = FileContentQuerySchema.extend({
|
|
7160
|
+
shareGrant: z19.string().optional().describe("Share grant selector for elements that cannot send the x-sixb-share-grant header")
|
|
7161
|
+
});
|
|
7162
|
+
var ObjectFileContentQuerySchema = ObjectFileContentRouteQuerySchema.extend({
|
|
7081
7163
|
path: z19.string().min(1).regex(/^\/properties(?:\/|$)/, "Object file content paths must start with /properties/")
|
|
7082
7164
|
});
|
|
7083
7165
|
function serializeObject(row) {
|
|
@@ -7628,6 +7710,7 @@ function registerObjectRoutes(app, host) {
|
|
|
7628
7710
|
facets: parsed.facets
|
|
7629
7711
|
});
|
|
7630
7712
|
return {
|
|
7713
|
+
total: result.total,
|
|
7631
7714
|
facets: result.facets,
|
|
7632
7715
|
plan: serializePlan(result.plan)
|
|
7633
7716
|
};
|
|
@@ -7685,7 +7768,7 @@ function registerObjectRoutes(app, host) {
|
|
|
7685
7768
|
}
|
|
7686
7769
|
}).get("/api/objects/:objectTypeId/:objectId/files/content", (context) => objectFileContentResponse(host, context), {
|
|
7687
7770
|
params: ObjectParamsSchema,
|
|
7688
|
-
query:
|
|
7771
|
+
query: ObjectFileContentRouteQuerySchema,
|
|
7689
7772
|
detail: {
|
|
7690
7773
|
summary: "Get object file content",
|
|
7691
7774
|
tags: [OPENAPI_TAGS.objectFiles.name],
|
|
@@ -7695,7 +7778,7 @@ function registerObjectRoutes(app, host) {
|
|
|
7695
7778
|
}
|
|
7696
7779
|
}).head("/api/objects/:objectTypeId/:objectId/files/content", (context) => objectFileContentResponse(host, context, { head: true }), {
|
|
7697
7780
|
params: ObjectParamsSchema,
|
|
7698
|
-
query:
|
|
7781
|
+
query: ObjectFileContentRouteQuerySchema,
|
|
7699
7782
|
detail: {
|
|
7700
7783
|
summary: "Head object file content",
|
|
7701
7784
|
tags: [OPENAPI_TAGS.objectFiles.name],
|
|
@@ -9138,10 +9221,6 @@ var MaintenanceSummarySchema = z26.object({
|
|
|
9138
9221
|
retryingCount: z26.number().int().nonnegative(),
|
|
9139
9222
|
maxAttempts: z26.number().int().nonnegative()
|
|
9140
9223
|
}).nullable(),
|
|
9141
|
-
terminalSources: z26.object({
|
|
9142
|
-
count: z26.number().int().nonnegative(),
|
|
9143
|
-
oldestTerminalAt: z26.string().nullable()
|
|
9144
|
-
}).nullable(),
|
|
9145
9224
|
cleanup: z26.object({
|
|
9146
9225
|
publishedOutboxRowsDeleted: z26.number().int().nonnegative(),
|
|
9147
9226
|
terminalSourceRowsDeleted: z26.number().int().nonnegative(),
|
|
@@ -10984,6 +11063,7 @@ function registerHttpRoutes(app, host, options) {
|
|
|
10984
11063
|
registerObjectRoutes(app, host);
|
|
10985
11064
|
registerActionRoutes(app, host);
|
|
10986
11065
|
registerFileRoutes(app, host);
|
|
11066
|
+
registerFileDownloadRoutes(app, host);
|
|
10987
11067
|
registerActionRunRoutes(app, host);
|
|
10988
11068
|
registerAgentRoutes(app, host);
|
|
10989
11069
|
registerModelRoutes(app, host);
|
|
@@ -13952,248 +14032,45 @@ function errorMessage(error) {
|
|
|
13952
14032
|
// src/routes/ws/events.ts
|
|
13953
14033
|
import { scopeKeysForEvent } from "@sixb/core/internal/event-scope";
|
|
13954
14034
|
import { z as z34 } from "zod";
|
|
13955
|
-
var SubscribeSchema2 = z34.object({
|
|
13956
|
-
type: z34.literal("subscribe"),
|
|
13957
|
-
topic: z34.enum(EVENT_TOPICS).optional(),
|
|
13958
|
-
types: z34.array(z34.enum(EVENT_TYPES)).optional(),
|
|
13959
|
-
objectTypeId: z34.string().optional(),
|
|
13960
|
-
primaryId: z34.string().optional(),
|
|
13961
|
-
actionId: z34.string().optional(),
|
|
13962
|
-
runId: z34.string().optional(),
|
|
13963
|
-
afterCursor: z34.string().optional(),
|
|
13964
|
-
limit: z34.number().int().positive().max(500).optional()
|
|
13965
|
-
});
|
|
13966
|
-
var UnsubscribeSchema2 = z34.object({
|
|
13967
|
-
type: z34.literal("unsubscribe")
|
|
13968
|
-
});
|
|
13969
|
-
var SubscriptionMessageSchema = z34.union([SubscribeSchema2, UnsubscribeSchema2]);
|
|
13970
|
-
function parseSubscriptionMessage(payload) {
|
|
13971
|
-
if (!payload || typeof payload !== "object") {
|
|
13972
|
-
return { ok: false, error: "Message must be a JSON object." };
|
|
13973
|
-
}
|
|
13974
|
-
const parsed = SubscriptionMessageSchema.safeParse(payload);
|
|
13975
|
-
if (!parsed.success) {
|
|
13976
|
-
return {
|
|
13977
|
-
ok: false,
|
|
13978
|
-
error: parsed.error.issues[0]?.message ?? "Invalid websocket message."
|
|
13979
|
-
};
|
|
13980
|
-
}
|
|
13981
|
-
return { ok: true, data: parsed.data };
|
|
13982
|
-
}
|
|
13983
|
-
async function resolveLatestCursor(server) {
|
|
13984
|
-
return server.getHost().events.latestCursor();
|
|
13985
|
-
}
|
|
13986
|
-
function createDefaultState() {
|
|
13987
|
-
return {
|
|
13988
|
-
topics: undefined,
|
|
13989
|
-
types: undefined,
|
|
13990
|
-
objectTypeId: undefined,
|
|
13991
|
-
primaryId: undefined,
|
|
13992
|
-
actionId: undefined,
|
|
13993
|
-
runId: undefined,
|
|
13994
|
-
afterCursor: undefined,
|
|
13995
|
-
limit: 200,
|
|
13996
|
-
polling: false,
|
|
13997
|
-
timer: null,
|
|
13998
|
-
initialized: Promise.resolve(),
|
|
13999
|
-
initializationFailureHandled: false
|
|
14000
|
-
};
|
|
14001
|
-
}
|
|
14002
|
-
function registerEventStreamRoutes(app, server) {
|
|
14003
|
-
const states = new WeakMap;
|
|
14004
|
-
const awaitInitialization = async (ws, state) => {
|
|
14005
|
-
try {
|
|
14006
|
-
await state.initialized;
|
|
14007
|
-
return true;
|
|
14008
|
-
} catch (error) {
|
|
14009
|
-
if (state.initializationFailureHandled)
|
|
14010
|
-
return false;
|
|
14011
|
-
state.initializationFailureHandled = true;
|
|
14012
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
14013
|
-
safeSend(ws, {
|
|
14014
|
-
type: "error",
|
|
14015
|
-
message: `[SixbServer] Failed to initialize event websocket: ${message}`
|
|
14016
|
-
});
|
|
14017
|
-
ws.close();
|
|
14018
|
-
return false;
|
|
14019
|
-
}
|
|
14020
|
-
};
|
|
14021
|
-
const stopPolling = (ws) => {
|
|
14022
|
-
const state = states.get(wsStateKey(ws));
|
|
14023
|
-
if (!state) {
|
|
14024
|
-
return;
|
|
14025
|
-
}
|
|
14026
|
-
if (state.timer) {
|
|
14027
|
-
clearInterval(state.timer);
|
|
14028
|
-
state.timer = null;
|
|
14029
|
-
}
|
|
14030
|
-
};
|
|
14031
|
-
const startPolling = (ws) => {
|
|
14032
|
-
const state = states.get(wsStateKey(ws));
|
|
14033
|
-
if (!state || state.timer) {
|
|
14034
|
-
return;
|
|
14035
|
-
}
|
|
14036
|
-
const sixb = wsRequestSixb(ws);
|
|
14037
|
-
if (!sixb) {
|
|
14038
|
-
safeSend(ws, { type: "error", message: "Execution scope is not available." });
|
|
14039
|
-
return;
|
|
14040
|
-
}
|
|
14041
|
-
const tick = async () => {
|
|
14042
|
-
if (state.polling) {
|
|
14043
|
-
return;
|
|
14044
|
-
}
|
|
14045
|
-
state.polling = true;
|
|
14046
|
-
try {
|
|
14047
|
-
const p = server.getHost();
|
|
14048
|
-
const events = await p.events.read({
|
|
14049
|
-
afterCursor: state.afterCursor,
|
|
14050
|
-
limit: state.limit,
|
|
14051
|
-
topics: state.topics,
|
|
14052
|
-
types: state.types
|
|
14053
|
-
});
|
|
14054
|
-
for (const event of events) {
|
|
14055
|
-
if (!eventMatchesScope(event, state)) {
|
|
14056
|
-
continue;
|
|
14057
|
-
}
|
|
14058
|
-
if (sixb.events.canRead(event)) {
|
|
14059
|
-
safeSend(ws, { type: "event", event });
|
|
14060
|
-
}
|
|
14061
|
-
}
|
|
14062
|
-
const last = events[events.length - 1];
|
|
14063
|
-
if (last) {
|
|
14064
|
-
state.afterCursor = last.cursor;
|
|
14065
|
-
}
|
|
14066
|
-
} catch (error) {
|
|
14067
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
14068
|
-
safeSend(ws, { type: "error", message });
|
|
14069
|
-
} finally {
|
|
14070
|
-
state.polling = false;
|
|
14071
|
-
}
|
|
14072
|
-
};
|
|
14073
|
-
state.timer = setInterval(() => {
|
|
14074
|
-
tick();
|
|
14075
|
-
}, 500);
|
|
14076
|
-
tick();
|
|
14077
|
-
};
|
|
14078
|
-
return app.ws("/ws/events", {
|
|
14079
|
-
async open(ws) {
|
|
14080
|
-
const state = createDefaultState();
|
|
14081
|
-
states.set(wsStateKey(ws), state);
|
|
14082
|
-
state.initialized = resolveLatestCursor(server).then((cursor) => {
|
|
14083
|
-
state.afterCursor = cursor;
|
|
14084
|
-
});
|
|
14085
|
-
if (!await awaitInitialization(ws, state))
|
|
14086
|
-
return;
|
|
14087
|
-
safeSend(ws, { type: "connected", channel: "events" });
|
|
14088
|
-
},
|
|
14089
|
-
async message(ws, message) {
|
|
14090
|
-
const decoded = await decodeWsMessage(message);
|
|
14091
|
-
const parsed = parseSubscriptionMessage(decoded);
|
|
14092
|
-
if (!parsed.ok) {
|
|
14093
|
-
safeSend(ws, { type: "error", message: parsed.error });
|
|
14094
|
-
return;
|
|
14095
|
-
}
|
|
14096
|
-
const state = states.get(wsStateKey(ws));
|
|
14097
|
-
if (!state) {
|
|
14098
|
-
safeSend(ws, { type: "error", message: "Subscription state not found." });
|
|
14099
|
-
return;
|
|
14100
|
-
}
|
|
14101
|
-
if (!await awaitInitialization(ws, state))
|
|
14102
|
-
return;
|
|
14103
|
-
if (parsed.data.type === "unsubscribe") {
|
|
14104
|
-
stopPolling(ws);
|
|
14105
|
-
state.topics = undefined;
|
|
14106
|
-
state.types = undefined;
|
|
14107
|
-
state.objectTypeId = undefined;
|
|
14108
|
-
state.primaryId = undefined;
|
|
14109
|
-
state.actionId = undefined;
|
|
14110
|
-
state.runId = undefined;
|
|
14111
|
-
state.afterCursor = await resolveLatestCursor(server);
|
|
14112
|
-
safeSend(ws, { type: "unsubscribed" });
|
|
14113
|
-
return;
|
|
14114
|
-
}
|
|
14115
|
-
state.topics = parsed.data.topic ? [parsed.data.topic] : undefined;
|
|
14116
|
-
state.types = parsed.data.types;
|
|
14117
|
-
state.objectTypeId = parsed.data.objectTypeId;
|
|
14118
|
-
state.primaryId = parsed.data.primaryId;
|
|
14119
|
-
state.actionId = parsed.data.actionId;
|
|
14120
|
-
state.runId = parsed.data.runId;
|
|
14121
|
-
state.limit = parsed.data.limit ?? state.limit;
|
|
14122
|
-
state.afterCursor = parsed.data.afterCursor ?? state.afterCursor;
|
|
14123
|
-
startPolling(ws);
|
|
14124
|
-
safeSend(ws, {
|
|
14125
|
-
type: "subscribed",
|
|
14126
|
-
topic: state.topics?.[0] ?? null,
|
|
14127
|
-
types: state.types ?? null,
|
|
14128
|
-
afterCursor: state.afterCursor ?? null
|
|
14129
|
-
});
|
|
14130
|
-
},
|
|
14131
|
-
close(ws) {
|
|
14132
|
-
stopPolling(ws);
|
|
14133
|
-
states.delete(wsStateKey(ws));
|
|
14134
|
-
}
|
|
14135
|
-
});
|
|
14136
|
-
}
|
|
14137
|
-
function eventMatchesScope(event, filter) {
|
|
14138
|
-
const { objectTypeId, primaryId, actionId, runId } = filter;
|
|
14139
|
-
if (objectTypeId === undefined && primaryId === undefined && actionId === undefined && runId === undefined) {
|
|
14140
|
-
return true;
|
|
14141
|
-
}
|
|
14142
|
-
const scope = scopeKeysForEvent(event);
|
|
14143
|
-
if (objectTypeId !== undefined && scope.objectTypeId !== objectTypeId) {
|
|
14144
|
-
return false;
|
|
14145
|
-
}
|
|
14146
|
-
if (primaryId !== undefined && scope.primaryId !== primaryId) {
|
|
14147
|
-
return false;
|
|
14148
|
-
}
|
|
14149
|
-
if (actionId !== undefined && scope.actionId !== actionId) {
|
|
14150
|
-
return false;
|
|
14151
|
-
}
|
|
14152
|
-
if (runId !== undefined && scope.runId !== runId) {
|
|
14153
|
-
return false;
|
|
14154
|
-
}
|
|
14155
|
-
return true;
|
|
14156
|
-
}
|
|
14157
|
-
|
|
14158
|
-
// src/routes/ws/logs.ts
|
|
14159
|
-
import { z as z35 } from "zod";
|
|
14160
14035
|
|
|
14161
|
-
// src/routes/ws/
|
|
14036
|
+
// src/routes/ws/subscription-hub.ts
|
|
14162
14037
|
import { BrokerCursorExpiredError as BrokerCursorExpiredError2 } from "@sixb/core/broker";
|
|
14163
14038
|
var MAX_CLIENT_RECORDS = 1000;
|
|
14164
14039
|
var MAX_CLIENT_BYTES = 1048576;
|
|
14165
14040
|
var MAX_SOCKET_BUFFERED_BYTES = 1048576;
|
|
14166
14041
|
var MAX_BATCH_RECORDS = 100;
|
|
14167
14042
|
var FLUSH_DELAY_MS = 10;
|
|
14168
|
-
var READ_PAGE_SIZE = 500;
|
|
14169
14043
|
|
|
14170
|
-
class
|
|
14171
|
-
|
|
14044
|
+
class SubscriptionHub {
|
|
14045
|
+
source;
|
|
14172
14046
|
clients = new Map;
|
|
14173
14047
|
subscriptionGenerations = new WeakMap;
|
|
14174
|
-
|
|
14175
|
-
unsubscribeBroker = null;
|
|
14048
|
+
started = null;
|
|
14176
14049
|
closed = false;
|
|
14177
|
-
constructor(
|
|
14178
|
-
this.
|
|
14050
|
+
constructor(source) {
|
|
14051
|
+
this.source = source;
|
|
14179
14052
|
}
|
|
14180
|
-
async subscribe(key, ws,
|
|
14053
|
+
async subscribe(key, ws, subscription, onSubscribed) {
|
|
14181
14054
|
if (this.closed)
|
|
14182
|
-
throw new Error("
|
|
14055
|
+
throw new Error("Subscription hub is closed");
|
|
14183
14056
|
const generation = this.nextSubscriptionGeneration(key);
|
|
14184
14057
|
this.removeClient(key);
|
|
14185
|
-
|
|
14186
|
-
|
|
14187
|
-
|
|
14058
|
+
let started;
|
|
14059
|
+
do {
|
|
14060
|
+
started = this.start();
|
|
14061
|
+
try {
|
|
14062
|
+
await started;
|
|
14063
|
+
} catch (error) {
|
|
14064
|
+
if (this.closed || !this.isCurrentSubscription(key, generation))
|
|
14065
|
+
return;
|
|
14066
|
+
throw error;
|
|
14067
|
+
}
|
|
14188
14068
|
if (this.closed || !this.isCurrentSubscription(key, generation))
|
|
14189
14069
|
return;
|
|
14190
|
-
|
|
14191
|
-
}
|
|
14192
|
-
if (this.closed || !this.isCurrentSubscription(key, generation))
|
|
14193
|
-
return;
|
|
14070
|
+
} while (started !== this.started);
|
|
14194
14071
|
const state = {
|
|
14195
14072
|
ws,
|
|
14196
|
-
|
|
14073
|
+
subscription,
|
|
14197
14074
|
queue: [],
|
|
14198
14075
|
queuedCursors: new Set,
|
|
14199
14076
|
pendingLive: [],
|
|
@@ -14203,13 +14080,13 @@ class LogSubscriptionHub {
|
|
|
14203
14080
|
pendingBytes: 0,
|
|
14204
14081
|
flushTimer: null,
|
|
14205
14082
|
queueProgressWaiters: new Set,
|
|
14206
|
-
catchingUp:
|
|
14083
|
+
catchingUp: subscription.replay !== undefined,
|
|
14207
14084
|
closed: false
|
|
14208
14085
|
};
|
|
14209
14086
|
this.clients.set(key, state);
|
|
14210
14087
|
onSubscribed();
|
|
14211
|
-
if (
|
|
14212
|
-
this.catchUp(key, state);
|
|
14088
|
+
if (subscription.replay) {
|
|
14089
|
+
this.catchUp(key, state, subscription.replay);
|
|
14213
14090
|
}
|
|
14214
14091
|
}
|
|
14215
14092
|
unsubscribe(key) {
|
|
@@ -14224,7 +14101,12 @@ class LogSubscriptionHub {
|
|
|
14224
14101
|
if (state.flushTimer)
|
|
14225
14102
|
clearTimeout(state.flushTimer);
|
|
14226
14103
|
this.notifyQueueProgress(state);
|
|
14104
|
+
this.deleteClient(key);
|
|
14105
|
+
}
|
|
14106
|
+
deleteClient(key) {
|
|
14227
14107
|
this.clients.delete(key);
|
|
14108
|
+
if (this.clients.size === 0)
|
|
14109
|
+
this.release();
|
|
14228
14110
|
}
|
|
14229
14111
|
nextSubscriptionGeneration(key) {
|
|
14230
14112
|
const generation = (this.subscriptionGenerations.get(key) ?? 0) + 1;
|
|
@@ -14236,58 +14118,57 @@ class LogSubscriptionHub {
|
|
|
14236
14118
|
}
|
|
14237
14119
|
async close() {
|
|
14238
14120
|
this.closed = true;
|
|
14239
|
-
this.unsubscribeBroker?.();
|
|
14240
|
-
this.unsubscribeBroker = null;
|
|
14241
14121
|
for (const key of this.clients.keys())
|
|
14242
14122
|
this.unsubscribe(key);
|
|
14123
|
+
this.release();
|
|
14243
14124
|
}
|
|
14244
|
-
|
|
14125
|
+
start() {
|
|
14245
14126
|
if (this.closed)
|
|
14246
|
-
|
|
14247
|
-
if (!this.
|
|
14248
|
-
|
|
14249
|
-
|
|
14250
|
-
|
|
14251
|
-
|
|
14252
|
-
this.unsubscribeBroker = unsubscribe;
|
|
14253
|
-
}).catch((error) => {
|
|
14254
|
-
this.startPromise = null;
|
|
14255
|
-
throw error;
|
|
14127
|
+
return Promise.reject(new Error("Subscription hub is closed"));
|
|
14128
|
+
if (!this.started) {
|
|
14129
|
+
const started = this.source.subscribe((records) => this.deliverLive(records));
|
|
14130
|
+
started.catch(() => {
|
|
14131
|
+
if (this.started === started)
|
|
14132
|
+
this.started = null;
|
|
14256
14133
|
});
|
|
14134
|
+
this.started = started;
|
|
14257
14135
|
}
|
|
14258
|
-
|
|
14136
|
+
return this.started;
|
|
14259
14137
|
}
|
|
14260
|
-
|
|
14138
|
+
release() {
|
|
14139
|
+
const started = this.started;
|
|
14140
|
+
this.started = null;
|
|
14141
|
+
started?.then((unsubscribe) => unsubscribe(), () => {
|
|
14142
|
+
return;
|
|
14143
|
+
});
|
|
14144
|
+
}
|
|
14145
|
+
deliverLive(records) {
|
|
14261
14146
|
for (const state of this.clients.values()) {
|
|
14262
|
-
for (const
|
|
14263
|
-
if (!matches(
|
|
14147
|
+
for (const record of records) {
|
|
14148
|
+
if (!state.subscription.matches(record))
|
|
14264
14149
|
continue;
|
|
14265
14150
|
if (state.catchingUp)
|
|
14266
|
-
this.enqueuePendingLive(state,
|
|
14151
|
+
this.enqueuePendingLive(state, record);
|
|
14267
14152
|
else
|
|
14268
|
-
this.enqueue(state,
|
|
14153
|
+
this.enqueue(state, record);
|
|
14269
14154
|
}
|
|
14270
14155
|
}
|
|
14271
14156
|
}
|
|
14272
|
-
async catchUp(key, state) {
|
|
14273
|
-
let afterCursor =
|
|
14157
|
+
async catchUp(key, state, replay) {
|
|
14158
|
+
let afterCursor = replay.afterCursor;
|
|
14274
14159
|
try {
|
|
14275
14160
|
let hasMore = true;
|
|
14276
14161
|
while (hasMore) {
|
|
14277
|
-
const page = await
|
|
14278
|
-
afterCursor,
|
|
14279
|
-
limit: READ_PAGE_SIZE,
|
|
14280
|
-
kinds: state.filter.kinds,
|
|
14281
|
-
levels: state.filter.levels,
|
|
14282
|
-
run: state.filter.run
|
|
14283
|
-
});
|
|
14162
|
+
const page = await replay.read(afterCursor);
|
|
14284
14163
|
if (state.closed || this.clients.get(key) !== state)
|
|
14285
14164
|
return;
|
|
14286
|
-
for (const
|
|
14287
|
-
if (state.pendingCursors.has(
|
|
14288
|
-
state.replayedPendingCursors.add(
|
|
14165
|
+
for (const record of page.records) {
|
|
14166
|
+
if (state.pendingCursors.has(record.cursor)) {
|
|
14167
|
+
state.replayedPendingCursors.add(record.cursor);
|
|
14289
14168
|
}
|
|
14290
|
-
if (!
|
|
14169
|
+
if (!state.subscription.matches(record))
|
|
14170
|
+
continue;
|
|
14171
|
+
if (!await this.enqueueReplay(state, record))
|
|
14291
14172
|
return;
|
|
14292
14173
|
}
|
|
14293
14174
|
if (state.closed)
|
|
@@ -14295,7 +14176,7 @@ class LogSubscriptionHub {
|
|
|
14295
14176
|
afterCursor = page.cursor ?? afterCursor;
|
|
14296
14177
|
hasMore = page.hasMore;
|
|
14297
14178
|
if (hasMore && !page.cursor) {
|
|
14298
|
-
throw new Error("
|
|
14179
|
+
throw new Error("Broker returned hasMore without a cursor");
|
|
14299
14180
|
}
|
|
14300
14181
|
}
|
|
14301
14182
|
state.catchingUp = false;
|
|
@@ -14305,9 +14186,9 @@ class LogSubscriptionHub {
|
|
|
14305
14186
|
state.pendingCursors.clear();
|
|
14306
14187
|
state.replayedPendingCursors.clear();
|
|
14307
14188
|
state.pendingBytes = 0;
|
|
14308
|
-
for (const
|
|
14309
|
-
if (!replayedPendingCursors.has(
|
|
14310
|
-
this.enqueue(state,
|
|
14189
|
+
for (const record of pendingLive) {
|
|
14190
|
+
if (!replayedPendingCursors.has(record.cursor))
|
|
14191
|
+
this.enqueue(state, record);
|
|
14311
14192
|
}
|
|
14312
14193
|
} catch (error) {
|
|
14313
14194
|
if (state.closed || this.clients.get(key) !== state)
|
|
@@ -14319,12 +14200,7 @@ class LogSubscriptionHub {
|
|
|
14319
14200
|
state.pendingBytes = 0;
|
|
14320
14201
|
if (error instanceof BrokerCursorExpiredError2) {
|
|
14321
14202
|
try {
|
|
14322
|
-
|
|
14323
|
-
this.send(state, {
|
|
14324
|
-
type: "reset",
|
|
14325
|
-
reason: "cursor_expired",
|
|
14326
|
-
cursor: latest.lines.at(-1)?.cursor ?? latest.cursor
|
|
14327
|
-
});
|
|
14203
|
+
this.send(state, await this.source.expired());
|
|
14328
14204
|
} catch (resetError) {
|
|
14329
14205
|
this.fail(state, resetError instanceof Error ? resetError.message : String(resetError), 1011);
|
|
14330
14206
|
}
|
|
@@ -14333,25 +14209,25 @@ class LogSubscriptionHub {
|
|
|
14333
14209
|
this.fail(state, error instanceof Error ? error.message : String(error), 1011);
|
|
14334
14210
|
}
|
|
14335
14211
|
}
|
|
14336
|
-
enqueue(state,
|
|
14337
|
-
if (state.closed || state.queuedCursors.has(
|
|
14212
|
+
enqueue(state, record) {
|
|
14213
|
+
if (state.closed || state.queuedCursors.has(record.cursor))
|
|
14338
14214
|
return;
|
|
14339
|
-
const bytes = encodedBytes(
|
|
14215
|
+
const bytes = encodedBytes(record);
|
|
14340
14216
|
if (!hasQueueCapacity(state, bytes)) {
|
|
14341
|
-
this.fail(state, "
|
|
14217
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013);
|
|
14342
14218
|
return;
|
|
14343
14219
|
}
|
|
14344
|
-
this.
|
|
14220
|
+
this.pushQueuedRecord(state, record, bytes);
|
|
14345
14221
|
}
|
|
14346
|
-
async enqueueReplay(state,
|
|
14222
|
+
async enqueueReplay(state, record) {
|
|
14347
14223
|
if (state.closed)
|
|
14348
14224
|
return false;
|
|
14349
|
-
if (state.queuedCursors.has(
|
|
14225
|
+
if (state.queuedCursors.has(record.cursor))
|
|
14350
14226
|
return true;
|
|
14351
|
-
const bytes = encodedBytes(
|
|
14227
|
+
const bytes = encodedBytes(record);
|
|
14352
14228
|
while (!hasQueueCapacity(state, bytes)) {
|
|
14353
14229
|
if (state.queue.length === 0 || socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
|
|
14354
|
-
this.fail(state, "
|
|
14230
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013);
|
|
14355
14231
|
return false;
|
|
14356
14232
|
}
|
|
14357
14233
|
this.scheduleFlush(state);
|
|
@@ -14359,25 +14235,25 @@ class LogSubscriptionHub {
|
|
|
14359
14235
|
if (state.closed)
|
|
14360
14236
|
return false;
|
|
14361
14237
|
}
|
|
14362
|
-
this.
|
|
14238
|
+
this.pushQueuedRecord(state, record, bytes);
|
|
14363
14239
|
return !state.closed;
|
|
14364
14240
|
}
|
|
14365
|
-
|
|
14366
|
-
state.queue.push(
|
|
14367
|
-
state.queuedCursors.add(
|
|
14241
|
+
pushQueuedRecord(state, record, bytes) {
|
|
14242
|
+
state.queue.push(record);
|
|
14243
|
+
state.queuedCursors.add(record.cursor);
|
|
14368
14244
|
state.queuedBytes += bytes;
|
|
14369
14245
|
this.scheduleFlush(state);
|
|
14370
14246
|
}
|
|
14371
|
-
enqueuePendingLive(state,
|
|
14372
|
-
if (state.closed || state.pendingCursors.has(
|
|
14247
|
+
enqueuePendingLive(state, record) {
|
|
14248
|
+
if (state.closed || state.pendingCursors.has(record.cursor))
|
|
14373
14249
|
return;
|
|
14374
|
-
const bytes = encodedBytes(
|
|
14250
|
+
const bytes = encodedBytes(record);
|
|
14375
14251
|
if (!hasQueueCapacity(state, bytes)) {
|
|
14376
|
-
this.fail(state, "
|
|
14252
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013);
|
|
14377
14253
|
return;
|
|
14378
14254
|
}
|
|
14379
|
-
state.pendingLive.push(
|
|
14380
|
-
state.pendingCursors.add(
|
|
14255
|
+
state.pendingLive.push(record);
|
|
14256
|
+
state.pendingCursors.add(record.cursor);
|
|
14381
14257
|
state.pendingBytes += bytes;
|
|
14382
14258
|
}
|
|
14383
14259
|
scheduleFlush(state) {
|
|
@@ -14395,13 +14271,14 @@ class LogSubscriptionHub {
|
|
|
14395
14271
|
this.scheduleFlush(state);
|
|
14396
14272
|
return;
|
|
14397
14273
|
}
|
|
14398
|
-
const
|
|
14399
|
-
for (const
|
|
14400
|
-
state.queuedCursors.delete(
|
|
14401
|
-
state.queuedBytes -= encodedBytes(
|
|
14274
|
+
const records = state.queue.splice(0, MAX_BATCH_RECORDS);
|
|
14275
|
+
for (const record of records) {
|
|
14276
|
+
state.queuedCursors.delete(record.cursor);
|
|
14277
|
+
state.queuedBytes -= encodedBytes(record);
|
|
14402
14278
|
}
|
|
14403
14279
|
this.notifyQueueProgress(state);
|
|
14404
|
-
|
|
14280
|
+
for (const frame of this.source.frames(records))
|
|
14281
|
+
this.send(state, frame);
|
|
14405
14282
|
if (state.queue.length > 0)
|
|
14406
14283
|
this.scheduleFlush(state);
|
|
14407
14284
|
}
|
|
@@ -14435,28 +14312,16 @@ class LogSubscriptionHub {
|
|
|
14435
14312
|
this.notifyQueueProgress(state);
|
|
14436
14313
|
for (const [key, candidate] of this.clients) {
|
|
14437
14314
|
if (candidate === state)
|
|
14438
|
-
this.
|
|
14315
|
+
this.deleteClient(key);
|
|
14439
14316
|
}
|
|
14440
14317
|
try {
|
|
14441
|
-
state.ws.close(closeCode, closeCode === 1013 ? "
|
|
14318
|
+
state.ws.close(closeCode, closeCode === 1013 ? "Stream backpressure" : "Stream failure");
|
|
14442
14319
|
} catch {}
|
|
14443
14320
|
}
|
|
14444
14321
|
}
|
|
14445
14322
|
function hasQueueCapacity(state, bytes) {
|
|
14446
14323
|
return state.queue.length + state.pendingLive.length < MAX_CLIENT_RECORDS && state.queuedBytes + state.pendingBytes + bytes <= MAX_CLIENT_BYTES;
|
|
14447
14324
|
}
|
|
14448
|
-
function matches(line, filter) {
|
|
14449
|
-
if (filter.run) {
|
|
14450
|
-
if (line.context.run.kind !== filter.run.kind || line.context.run.id !== filter.run.id) {
|
|
14451
|
-
return false;
|
|
14452
|
-
}
|
|
14453
|
-
}
|
|
14454
|
-
if (filter.kinds && !filter.kinds.includes(line.context.run.kind))
|
|
14455
|
-
return false;
|
|
14456
|
-
if (filter.levels && !filter.levels.includes(line.level))
|
|
14457
|
-
return false;
|
|
14458
|
-
return true;
|
|
14459
|
-
}
|
|
14460
14325
|
function encodedBytes(value) {
|
|
14461
14326
|
return new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
14462
14327
|
}
|
|
@@ -14468,7 +14333,173 @@ function socketBufferedAmount(ws) {
|
|
|
14468
14333
|
return typeof bufferedAmount === "number" ? bufferedAmount : 0;
|
|
14469
14334
|
}
|
|
14470
14335
|
|
|
14336
|
+
// src/routes/ws/events.ts
|
|
14337
|
+
var DEFAULT_READ_LIMIT = 200;
|
|
14338
|
+
var SubscribeSchema2 = z34.object({
|
|
14339
|
+
type: z34.literal("subscribe"),
|
|
14340
|
+
topic: z34.enum(EVENT_TOPICS).optional(),
|
|
14341
|
+
types: z34.array(z34.enum(EVENT_TYPES)).optional(),
|
|
14342
|
+
objectTypeId: z34.string().optional(),
|
|
14343
|
+
primaryId: z34.string().optional(),
|
|
14344
|
+
actionId: z34.string().optional(),
|
|
14345
|
+
runId: z34.string().optional(),
|
|
14346
|
+
afterCursor: z34.string().optional(),
|
|
14347
|
+
limit: z34.number().int().positive().max(500).optional()
|
|
14348
|
+
});
|
|
14349
|
+
var UnsubscribeSchema2 = z34.object({
|
|
14350
|
+
type: z34.literal("unsubscribe")
|
|
14351
|
+
});
|
|
14352
|
+
var SubscriptionMessageSchema = z34.union([SubscribeSchema2, UnsubscribeSchema2]);
|
|
14353
|
+
function parseSubscriptionMessage(payload) {
|
|
14354
|
+
if (!payload || typeof payload !== "object") {
|
|
14355
|
+
return { ok: false, error: "Message must be a JSON object." };
|
|
14356
|
+
}
|
|
14357
|
+
const parsed = SubscriptionMessageSchema.safeParse(payload);
|
|
14358
|
+
if (!parsed.success) {
|
|
14359
|
+
return {
|
|
14360
|
+
ok: false,
|
|
14361
|
+
error: parsed.error.issues[0]?.message ?? "Invalid websocket message."
|
|
14362
|
+
};
|
|
14363
|
+
}
|
|
14364
|
+
return { ok: true, data: parsed.data };
|
|
14365
|
+
}
|
|
14366
|
+
function registerEventStreamRoutes(app, server) {
|
|
14367
|
+
const events = server.getHost().events;
|
|
14368
|
+
const states = new WeakMap;
|
|
14369
|
+
const hub = new SubscriptionHub({
|
|
14370
|
+
subscribe: (deliver) => events.subscribe({ from: "latest" }, deliver),
|
|
14371
|
+
frames: (batch) => batch.map((event) => ({ type: "event", event })),
|
|
14372
|
+
expired: async () => ({
|
|
14373
|
+
type: "error",
|
|
14374
|
+
message: "[SixbServer] Event cursor expired; refetch current state. Live events continue."
|
|
14375
|
+
})
|
|
14376
|
+
});
|
|
14377
|
+
const awaitInitialization = async (ws, state) => {
|
|
14378
|
+
try {
|
|
14379
|
+
await state.initialized;
|
|
14380
|
+
return true;
|
|
14381
|
+
} catch (error) {
|
|
14382
|
+
if (state.initializationFailureHandled)
|
|
14383
|
+
return false;
|
|
14384
|
+
state.initializationFailureHandled = true;
|
|
14385
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
14386
|
+
safeSend(ws, {
|
|
14387
|
+
type: "error",
|
|
14388
|
+
message: `[SixbServer] Failed to initialize event websocket: ${message}`
|
|
14389
|
+
});
|
|
14390
|
+
ws.close();
|
|
14391
|
+
return false;
|
|
14392
|
+
}
|
|
14393
|
+
};
|
|
14394
|
+
app.onStop(() => hub.close());
|
|
14395
|
+
return app.ws("/ws/events", {
|
|
14396
|
+
async open(ws) {
|
|
14397
|
+
const state = {
|
|
14398
|
+
replayFrom: null,
|
|
14399
|
+
initialized: Promise.resolve(),
|
|
14400
|
+
initializationFailureHandled: false
|
|
14401
|
+
};
|
|
14402
|
+
states.set(wsStateKey(ws), state);
|
|
14403
|
+
state.initialized = events.latestCursor().then((afterCursor) => {
|
|
14404
|
+
state.replayFrom = { afterCursor };
|
|
14405
|
+
});
|
|
14406
|
+
if (!await awaitInitialization(ws, state))
|
|
14407
|
+
return;
|
|
14408
|
+
safeSend(ws, { type: "connected", channel: "events" });
|
|
14409
|
+
},
|
|
14410
|
+
async message(ws, message) {
|
|
14411
|
+
const decoded = await decodeWsMessage(message);
|
|
14412
|
+
const parsed = parseSubscriptionMessage(decoded);
|
|
14413
|
+
if (!parsed.ok) {
|
|
14414
|
+
safeSend(ws, { type: "error", message: parsed.error });
|
|
14415
|
+
return;
|
|
14416
|
+
}
|
|
14417
|
+
const key = wsStateKey(ws);
|
|
14418
|
+
const state = states.get(key);
|
|
14419
|
+
if (!state) {
|
|
14420
|
+
safeSend(ws, { type: "error", message: "Subscription state not found." });
|
|
14421
|
+
return;
|
|
14422
|
+
}
|
|
14423
|
+
if (!await awaitInitialization(ws, state))
|
|
14424
|
+
return;
|
|
14425
|
+
if (parsed.data.type === "unsubscribe") {
|
|
14426
|
+
hub.unsubscribe(key);
|
|
14427
|
+
safeSend(ws, { type: "unsubscribed" });
|
|
14428
|
+
return;
|
|
14429
|
+
}
|
|
14430
|
+
const sixb = wsRequestSixb(ws);
|
|
14431
|
+
if (!sixb) {
|
|
14432
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." });
|
|
14433
|
+
return;
|
|
14434
|
+
}
|
|
14435
|
+
const filter = parsed.data;
|
|
14436
|
+
const topics = filter.topic ? [filter.topic] : undefined;
|
|
14437
|
+
const limit = filter.limit ?? DEFAULT_READ_LIMIT;
|
|
14438
|
+
const replayFrom = filter.afterCursor === undefined ? state.replayFrom : { afterCursor: filter.afterCursor };
|
|
14439
|
+
state.replayFrom = null;
|
|
14440
|
+
try {
|
|
14441
|
+
await hub.subscribe(key, ws, {
|
|
14442
|
+
matches: (event) => (!filter.topic || event.topic === filter.topic) && (!filter.types?.length || filter.types.includes(event.type)) && eventMatchesScope(event, filter) && sixb.events.canRead(event),
|
|
14443
|
+
replay: replayFrom ? {
|
|
14444
|
+
afterCursor: replayFrom.afterCursor,
|
|
14445
|
+
read: async (afterCursor) => {
|
|
14446
|
+
const records = await events.read({
|
|
14447
|
+
afterCursor,
|
|
14448
|
+
limit,
|
|
14449
|
+
topics,
|
|
14450
|
+
types: filter.types
|
|
14451
|
+
});
|
|
14452
|
+
return {
|
|
14453
|
+
records,
|
|
14454
|
+
cursor: records.at(-1)?.cursor,
|
|
14455
|
+
hasMore: records.length === limit
|
|
14456
|
+
};
|
|
14457
|
+
}
|
|
14458
|
+
} : undefined
|
|
14459
|
+
}, () => safeSend(ws, {
|
|
14460
|
+
type: "subscribed",
|
|
14461
|
+
topic: filter.topic ?? null,
|
|
14462
|
+
types: filter.types ?? null,
|
|
14463
|
+
afterCursor: replayFrom?.afterCursor ?? null
|
|
14464
|
+
}));
|
|
14465
|
+
} catch (error) {
|
|
14466
|
+
safeSend(ws, {
|
|
14467
|
+
type: "error",
|
|
14468
|
+
message: error instanceof Error ? error.message : String(error)
|
|
14469
|
+
});
|
|
14470
|
+
ws.close(1011, "Event stream setup failed");
|
|
14471
|
+
}
|
|
14472
|
+
},
|
|
14473
|
+
close(ws) {
|
|
14474
|
+
hub.unsubscribe(wsStateKey(ws));
|
|
14475
|
+
states.delete(wsStateKey(ws));
|
|
14476
|
+
}
|
|
14477
|
+
});
|
|
14478
|
+
}
|
|
14479
|
+
function eventMatchesScope(event, filter) {
|
|
14480
|
+
const { objectTypeId, primaryId, actionId, runId } = filter;
|
|
14481
|
+
if (objectTypeId === undefined && primaryId === undefined && actionId === undefined && runId === undefined) {
|
|
14482
|
+
return true;
|
|
14483
|
+
}
|
|
14484
|
+
const scope = scopeKeysForEvent(event);
|
|
14485
|
+
if (objectTypeId !== undefined && scope.objectTypeId !== objectTypeId) {
|
|
14486
|
+
return false;
|
|
14487
|
+
}
|
|
14488
|
+
if (primaryId !== undefined && scope.primaryId !== primaryId) {
|
|
14489
|
+
return false;
|
|
14490
|
+
}
|
|
14491
|
+
if (actionId !== undefined && scope.actionId !== actionId) {
|
|
14492
|
+
return false;
|
|
14493
|
+
}
|
|
14494
|
+
if (runId !== undefined && scope.runId !== runId) {
|
|
14495
|
+
return false;
|
|
14496
|
+
}
|
|
14497
|
+
return true;
|
|
14498
|
+
}
|
|
14499
|
+
|
|
14471
14500
|
// src/routes/ws/logs.ts
|
|
14501
|
+
import { z as z35 } from "zod";
|
|
14502
|
+
var READ_PAGE_SIZE = 500;
|
|
14472
14503
|
var SubscribeSchema3 = z35.object({
|
|
14473
14504
|
type: z35.literal("subscribe"),
|
|
14474
14505
|
kinds: z35.array(z35.enum(SIXB_RUN_KINDS)).min(1).optional(),
|
|
@@ -14502,8 +14533,41 @@ function parseLogSubscriptionMessage(payload) {
|
|
|
14502
14533
|
}
|
|
14503
14534
|
return { ok: true, data: parsed.data };
|
|
14504
14535
|
}
|
|
14536
|
+
function createLogSubscriptionHub(logging) {
|
|
14537
|
+
return new SubscriptionHub({
|
|
14538
|
+
subscribe: (deliver) => logging.subscribe({ from: "latest" }, deliver),
|
|
14539
|
+
frames: (logs) => [{ type: "logs", logs }],
|
|
14540
|
+
expired: async () => {
|
|
14541
|
+
const latest = await logging.tail({ limit: 1 });
|
|
14542
|
+
return {
|
|
14543
|
+
type: "reset",
|
|
14544
|
+
reason: "cursor_expired",
|
|
14545
|
+
cursor: latest.lines.at(-1)?.cursor ?? latest.cursor
|
|
14546
|
+
};
|
|
14547
|
+
}
|
|
14548
|
+
});
|
|
14549
|
+
}
|
|
14550
|
+
function logSubscription(logging, filter) {
|
|
14551
|
+
return {
|
|
14552
|
+
matches: (line) => matchesLogFilter(line, filter),
|
|
14553
|
+
replay: filter.afterCursor === undefined ? undefined : {
|
|
14554
|
+
afterCursor: filter.afterCursor,
|
|
14555
|
+
read: async (afterCursor) => {
|
|
14556
|
+
const page = await logging.read({
|
|
14557
|
+
afterCursor,
|
|
14558
|
+
limit: READ_PAGE_SIZE,
|
|
14559
|
+
kinds: filter.kinds,
|
|
14560
|
+
levels: filter.levels,
|
|
14561
|
+
run: filter.run
|
|
14562
|
+
});
|
|
14563
|
+
return { records: page.lines, cursor: page.cursor, hasMore: page.hasMore };
|
|
14564
|
+
}
|
|
14565
|
+
}
|
|
14566
|
+
};
|
|
14567
|
+
}
|
|
14505
14568
|
function registerLogStreamRoutes(app, server) {
|
|
14506
|
-
const
|
|
14569
|
+
const logging = server.getHost().logging;
|
|
14570
|
+
const hub = createLogSubscriptionHub(logging);
|
|
14507
14571
|
app.onStop(() => hub.close());
|
|
14508
14572
|
return app.ws("/ws/logs", {
|
|
14509
14573
|
open(ws) {
|
|
@@ -14542,12 +14606,12 @@ function registerLogStreamRoutes(app, server) {
|
|
|
14542
14606
|
const subscription = parsed.data;
|
|
14543
14607
|
const levels = subscription.levels;
|
|
14544
14608
|
try {
|
|
14545
|
-
await hub.subscribe(key, ws, {
|
|
14609
|
+
await hub.subscribe(key, ws, logSubscription(logging, {
|
|
14546
14610
|
kinds: subscription.kinds,
|
|
14547
14611
|
levels,
|
|
14548
14612
|
run: subscription.run,
|
|
14549
14613
|
afterCursor: subscription.afterCursor
|
|
14550
|
-
}, () => safeSend(ws, {
|
|
14614
|
+
}), () => safeSend(ws, {
|
|
14551
14615
|
type: "subscribed",
|
|
14552
14616
|
kinds: subscription.kinds ?? null,
|
|
14553
14617
|
levels: levels ?? null,
|
|
@@ -14567,6 +14631,18 @@ function registerLogStreamRoutes(app, server) {
|
|
|
14567
14631
|
}
|
|
14568
14632
|
});
|
|
14569
14633
|
}
|
|
14634
|
+
function matchesLogFilter(line, filter) {
|
|
14635
|
+
if (filter.run) {
|
|
14636
|
+
if (line.context.run.kind !== filter.run.kind || line.context.run.id !== filter.run.id) {
|
|
14637
|
+
return false;
|
|
14638
|
+
}
|
|
14639
|
+
}
|
|
14640
|
+
if (filter.kinds && !filter.kinds.includes(line.context.run.kind))
|
|
14641
|
+
return false;
|
|
14642
|
+
if (filter.levels && !filter.levels.includes(line.level))
|
|
14643
|
+
return false;
|
|
14644
|
+
return true;
|
|
14645
|
+
}
|
|
14570
14646
|
|
|
14571
14647
|
// src/routes/ws/index.ts
|
|
14572
14648
|
function registerWebSocketRoutes(app, server) {
|
|
@@ -14601,6 +14677,9 @@ class SixbServer {
|
|
|
14601
14677
|
this.hostname = options.hostname ?? "0.0.0.0";
|
|
14602
14678
|
this.quiet = options.quiet ?? false;
|
|
14603
14679
|
this.apiBrowserPolicy = resolveApiBrowserPolicy(options.browser);
|
|
14680
|
+
if (this.apiBrowserPolicy.publicOrigin) {
|
|
14681
|
+
setApiPublicOrigin(this.hostRuntime, this.apiBrowserPolicy.publicOrigin);
|
|
14682
|
+
}
|
|
14604
14683
|
this.authContextResolver = createApiBrowserAuthContextResolver(this.apiBrowserPolicy);
|
|
14605
14684
|
this.authRedirectContextResolver = createApiBrowserAuthRedirectContextResolver(this.apiBrowserPolicy);
|
|
14606
14685
|
this.authExperience = options.authExperience;
|
|
@@ -14949,4 +15028,4 @@ export {
|
|
|
14949
15028
|
createSixbServer
|
|
14950
15029
|
};
|
|
14951
15030
|
|
|
14952
|
-
//# debugId=
|
|
15031
|
+
//# debugId=A2358A8A15C4EC4564756E2164756E21
|