@sixb/server 0.2.5 → 0.2.7
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/dist/auth/run-token.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +596 -92
- package/dist/index.js.map +36 -35
- package/dist/registerRoutes.d.ts +1 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +2 -0
- package/dist/routes/actions.d.ts.map +1 -1
- package/dist/routes/agent-approvals.d.ts +23 -1
- package/dist/routes/agent-approvals.d.ts.map +1 -1
- package/dist/routes/agent-computers.d.ts +36 -0
- package/dist/routes/agent-computers.d.ts.map +1 -1
- package/dist/routes/agent-thread-reads.d.ts +82 -0
- package/dist/routes/agent-thread-reads.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +795 -13
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/connectors.d.ts +2 -0
- package/dist/routes/connectors.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +2 -0
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/model-generations.d.ts +6 -0
- package/dist/routes/model-generations.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +24 -2
- package/dist/routes/observed-agent-threads.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +2 -0
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +2 -0
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/projections.d.ts +6 -0
- package/dist/routes/projections.d.ts.map +1 -1
- package/dist/routes/rules.d.ts +2 -0
- package/dist/routes/rules.d.ts.map +1 -1
- package/dist/routes/status.d.ts +5 -2
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/syncs.d.ts +177 -12
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +8 -6
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/schemas/actions.d.ts +6 -0
- package/dist/schemas/actions.d.ts.map +1 -1
- package/dist/schemas/activity.d.ts +4 -4
- package/dist/schemas/agents.d.ts +2657 -61
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +3 -0
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/common.d.ts +5 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/connectors.d.ts +3 -0
- package/dist/schemas/connectors.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +3 -0
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/models.d.ts +46 -0
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +3 -0
- package/dist/schemas/ontology.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +3 -0
- package/dist/schemas/pipelines.d.ts.map +1 -1
- package/dist/schemas/projections.d.ts +33 -0
- package/dist/schemas/projections.d.ts.map +1 -1
- package/dist/schemas/rules.d.ts +3 -0
- package/dist/schemas/rules.d.ts.map +1 -1
- package/dist/schemas/status.d.ts +2 -2
- package/dist/schemas/syncs.d.ts +727 -25
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +25 -22
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +16 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/auth/run-token.ts +59 -9
- package/src/index.ts +6 -1
- package/src/registerRoutes.ts +4 -1
- package/src/routes/actions.ts +7 -3
- package/src/routes/agent-computers.ts +7 -6
- package/src/routes/agent-thread-reads.ts +67 -0
- package/src/routes/agents.ts +129 -16
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/connectors.ts +1 -0
- package/src/routes/datasets.ts +11 -3
- package/src/routes/model-generations.ts +1 -1
- package/src/routes/observed-agent-threads.ts +3 -4
- package/src/routes/ontology.ts +17 -3
- package/src/routes/pipelines.ts +13 -3
- package/src/routes/projections.ts +14 -9
- package/src/routes/rules.ts +9 -3
- package/src/routes/status.ts +18 -1
- package/src/routes/syncs.ts +139 -8
- package/src/routes/workflows.ts +13 -3
- package/src/routes/ws/agents.ts +43 -3
- package/src/schemas/actions.ts +7 -1
- package/src/schemas/agents.ts +80 -10
- package/src/schemas/ai-accounting.ts +12 -0
- package/src/schemas/common.ts +24 -9
- package/src/schemas/connectors.ts +2 -1
- package/src/schemas/datasets.ts +2 -1
- package/src/schemas/models.ts +10 -0
- package/src/schemas/ontology.ts +2 -0
- package/src/schemas/pipelines.ts +2 -1
- package/src/schemas/projections.ts +4 -1
- package/src/schemas/rules.ts +2 -0
- package/src/schemas/syncs.ts +65 -2
- package/src/schemas/workflows.ts +7 -1
- package/src/server.ts +205 -13
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SixbFailure } from "@sixb/core"
|
|
2
2
|
import { PROJECTION_RUN_FAILURE_CODES, type ProjectionRunFailureCode } from "@sixb/core/storage"
|
|
3
3
|
import { z } from "zod"
|
|
4
|
-
import { sixbFailureSchema } from "./common"
|
|
4
|
+
import { DefinitionFolderSchema, sixbFailureSchema } from "./common"
|
|
5
5
|
|
|
6
6
|
const ProjectionRunFailureSchema: z.ZodType<SixbFailure<ProjectionRunFailureCode>> =
|
|
7
7
|
sixbFailureSchema(PROJECTION_RUN_FAILURE_CODES)
|
|
@@ -93,6 +93,7 @@ export const SourceEditConflictResolutionSchema = z.union([
|
|
|
93
93
|
export const ObjectProjectionSchema = z.object({
|
|
94
94
|
_tag: z.literal("ObjectProjectionDefinition"),
|
|
95
95
|
id: z.string(),
|
|
96
|
+
folder: DefinitionFolderSchema,
|
|
96
97
|
objectTypeId: z.string(),
|
|
97
98
|
datasetId: z.string(),
|
|
98
99
|
properties: z.record(z.string()),
|
|
@@ -107,6 +108,7 @@ export const ObjectProjectionSchema = z.object({
|
|
|
107
108
|
export const LinkProjectionSchema = z.object({
|
|
108
109
|
_tag: z.literal("LinkProjectionDefinition"),
|
|
109
110
|
id: z.string(),
|
|
111
|
+
folder: DefinitionFolderSchema,
|
|
110
112
|
linkId: z.string(),
|
|
111
113
|
sourceObjectTypeId: z.string(),
|
|
112
114
|
targetObjectTypeId: z.string(),
|
|
@@ -119,6 +121,7 @@ export const LinkProjectionSchema = z.object({
|
|
|
119
121
|
export const TelemetryProjectionSchema = z.object({
|
|
120
122
|
_tag: z.literal("TelemetryProjectionDefinition"),
|
|
121
123
|
id: z.string(),
|
|
124
|
+
folder: DefinitionFolderSchema,
|
|
122
125
|
objectTypeId: z.string(),
|
|
123
126
|
datasetId: z.string(),
|
|
124
127
|
objectIdField: z.string(),
|
package/src/schemas/rules.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RulePredicate } from "@sixb/core"
|
|
2
2
|
import { z } from "zod"
|
|
3
|
+
import { DefinitionFolderSchema } from "./common"
|
|
3
4
|
|
|
4
5
|
export const RuleParamsSchema = z.object({
|
|
5
6
|
ruleId: z.string().min(1),
|
|
@@ -79,6 +80,7 @@ const RuleEventDependencySchema = z.union([
|
|
|
79
80
|
export const RuleSchema = z.object({
|
|
80
81
|
kind: z.literal("rule"),
|
|
81
82
|
id: z.string(),
|
|
83
|
+
folder: DefinitionFolderSchema,
|
|
82
84
|
subject: RuleSubjectSchema,
|
|
83
85
|
predicate: RulePredicateSchema,
|
|
84
86
|
dependencies: z.array(RuleEventDependencySchema),
|
package/src/schemas/syncs.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
type SyncRunFailureCode,
|
|
5
5
|
} from "@sixb/core/storage"
|
|
6
6
|
import { z } from "zod"
|
|
7
|
-
import { sixbFailureSchema } from "./common"
|
|
7
|
+
import { DefinitionFolderSchema, sixbFailureSchema } from "./common"
|
|
8
8
|
import { DatasetDefinitionSchema } from "./datasets"
|
|
9
9
|
|
|
10
10
|
const SyncRunFailureSchema: z.ZodType<SixbFailure<SyncRunFailureCode>> =
|
|
@@ -19,6 +19,7 @@ const SyncModeSchema = z.enum(["snapshot", "append", "merge"])
|
|
|
19
19
|
|
|
20
20
|
export const SyncRunsQuerySchema = z.object({
|
|
21
21
|
syncId: z.string().optional(),
|
|
22
|
+
connectionId: z.string().optional(),
|
|
22
23
|
datasetId: z.string().optional(),
|
|
23
24
|
status: SyncRunStatusSchema.optional(),
|
|
24
25
|
startedAfter: z.string().optional(),
|
|
@@ -29,6 +30,8 @@ export const SyncRunsQuerySchema = z.object({
|
|
|
29
30
|
})
|
|
30
31
|
|
|
31
32
|
export const RequestSyncRunBodySchema = z.object({
|
|
33
|
+
/** One connection to run, on a sync of an OAuth connector. Without it, every one you can run. */
|
|
34
|
+
connectionId: z.string().optional(),
|
|
32
35
|
expectedLatestVersionId: z.string().optional(),
|
|
33
36
|
commitMessage: z.string().optional(),
|
|
34
37
|
})
|
|
@@ -42,6 +45,8 @@ export const SyncRunSchema = z.object({
|
|
|
42
45
|
id: z.string(),
|
|
43
46
|
projectId: z.string(),
|
|
44
47
|
syncId: z.string(),
|
|
48
|
+
/** The connection the run reads, on a sync of an OAuth connector. */
|
|
49
|
+
connectionId: z.string().optional(),
|
|
45
50
|
datasetId: z.string(),
|
|
46
51
|
mode: SyncModeSchema,
|
|
47
52
|
status: SyncRunStatusSchema,
|
|
@@ -62,8 +67,48 @@ export const SyncRunSchema = z.object({
|
|
|
62
67
|
error: SyncRunFailureSchema.optional(),
|
|
63
68
|
})
|
|
64
69
|
|
|
70
|
+
const SyncAccountConnectionSchema = z.object({
|
|
71
|
+
id: z.string(),
|
|
72
|
+
owner: z.discriminatedUnion("type", [
|
|
73
|
+
z.object({ type: z.literal("project") }),
|
|
74
|
+
z.object({ type: z.literal("user"), id: z.string() }),
|
|
75
|
+
]),
|
|
76
|
+
slot: z.string(),
|
|
77
|
+
account: z.object({ id: z.string(), label: z.string() }),
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
/** One connected account of an OAuth connector, and the latest run of each of its syncs. */
|
|
81
|
+
export const ConnectorAccountSchema = z.object({
|
|
82
|
+
connection: SyncAccountConnectionSchema,
|
|
83
|
+
latestRuns: z.array(SyncRunSchema),
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
export const ConnectorAccountListResponseSchema = z.object({
|
|
87
|
+
accounts: z.array(ConnectorAccountSchema),
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
export const ConnectorAccountsParamsSchema = z.object({
|
|
91
|
+
connectorId: z.string().min(1),
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
/** Where a sync of an OAuth connector's accounts stand, by the latest run of each. */
|
|
95
|
+
const SyncAccountSummarySchema = z.object({
|
|
96
|
+
total: z.number(),
|
|
97
|
+
/** Their latest run is queued or running. */
|
|
98
|
+
running: z.number(),
|
|
99
|
+
/** Their latest run failed because the account needs reconnecting. */
|
|
100
|
+
needsReconnecting: z.number(),
|
|
101
|
+
/** Their latest run failed for any other reason. */
|
|
102
|
+
failing: z.number(),
|
|
103
|
+
/** Their latest run succeeded. */
|
|
104
|
+
synced: z.number(),
|
|
105
|
+
/** When the most recent of those successful runs finished. */
|
|
106
|
+
lastSyncedAt: z.string().optional(),
|
|
107
|
+
})
|
|
108
|
+
|
|
65
109
|
export const SyncSchema = z.object({
|
|
66
110
|
id: z.string(),
|
|
111
|
+
folder: DefinitionFolderSchema,
|
|
67
112
|
mode: SyncModeSchema,
|
|
68
113
|
connector: z.object({
|
|
69
114
|
id: z.string(),
|
|
@@ -75,6 +120,17 @@ export const SyncSchema = z.object({
|
|
|
75
120
|
}),
|
|
76
121
|
triggers: z.array(SyncTriggerSchema),
|
|
77
122
|
latestRun: SyncRunSchema.nullable(),
|
|
123
|
+
/** On a sync of an OAuth connector: the accounts the caller may see. */
|
|
124
|
+
accounts: SyncAccountSummarySchema.optional(),
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
export const ConnectionSyncRunsParamsSchema = z.object({
|
|
128
|
+
connectorId: z.string().min(1),
|
|
129
|
+
connectionId: z.string().min(1),
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
export const ConnectionSyncRunsResponseSchema = z.object({
|
|
133
|
+
runs: z.array(SyncRunSchema),
|
|
78
134
|
})
|
|
79
135
|
|
|
80
136
|
export const SyncRunListResponseSchema = z.object({
|
|
@@ -83,9 +139,16 @@ export const SyncRunListResponseSchema = z.object({
|
|
|
83
139
|
total: z.number(),
|
|
84
140
|
})
|
|
85
141
|
|
|
86
|
-
|
|
142
|
+
const RequestedSyncRunSchema = z.object({
|
|
87
143
|
runId: z.string(),
|
|
88
144
|
jobId: z.string(),
|
|
89
145
|
syncId: z.string(),
|
|
146
|
+
/** The connection the run reads, on a sync of an OAuth connector. */
|
|
147
|
+
connectionId: z.string().optional(),
|
|
90
148
|
queuedAt: z.string(),
|
|
91
149
|
})
|
|
150
|
+
|
|
151
|
+
/** One run for a static connector's sync; one per connection for an OAuth connector's sync. */
|
|
152
|
+
export const RequestSyncRunResponseSchema = z.object({
|
|
153
|
+
runs: z.array(RequestedSyncRunSchema),
|
|
154
|
+
})
|
package/src/schemas/workflows.ts
CHANGED
|
@@ -7,7 +7,12 @@ import { z } from "zod"
|
|
|
7
7
|
import { AgentMessagePartSchema, AgentRunFailureSchema } from "./agents"
|
|
8
8
|
import { AiCostSummarySchema } from "./ai-accounting"
|
|
9
9
|
import { AiUsageSummarySchema } from "./ai-usage"
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DefinitionFolderSchema,
|
|
12
|
+
JsonValueSchema,
|
|
13
|
+
PrincipalSchema,
|
|
14
|
+
sixbFailureSchema,
|
|
15
|
+
} from "./common"
|
|
11
16
|
|
|
12
17
|
export const WorkflowIOSnapshotSchema = z.record(JsonValueSchema)
|
|
13
18
|
|
|
@@ -204,6 +209,7 @@ export const WorkflowInterventionSchema = z.object({
|
|
|
204
209
|
|
|
205
210
|
export const WorkflowSchema = z.object({
|
|
206
211
|
id: z.string(),
|
|
212
|
+
folder: DefinitionFolderSchema,
|
|
207
213
|
input: WorkflowIOSnapshotSchema,
|
|
208
214
|
triggers: z.array(WorkflowTriggerSchema),
|
|
209
215
|
nodes: z.array(WorkflowNodeSchema),
|
package/src/server.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { cors } from "@elysiajs/cors"
|
|
2
2
|
import { openapi } from "@elysiajs/openapi"
|
|
3
3
|
import type { OntologyMaintenanceHandle, SixbHostView } from "@sixb/core"
|
|
4
|
+
import { ACTION_RUN_DRAIN_TIMEOUT_MS } from "@sixb/core/internal/actions"
|
|
4
5
|
import { bindAgentRunRequestExecution } from "@sixb/core/internal/agent-execution"
|
|
5
6
|
import { ARTIFACT_FRAME_ROUTE_PREFIX } from "@sixb/core/internal/artifacts/frames"
|
|
6
7
|
import { CSRF_HEADER_NAME, setRequestClientAddress } from "@sixb/core/internal/auth"
|
|
7
8
|
import { bindRequestExecution } from "@sixb/core/internal/request-execution"
|
|
8
9
|
import { findDatasetIngestRuntime, setApiPublicOrigin } from "@sixb/core/internal/runtime"
|
|
9
10
|
import { checkPipelineMarkings, warmAppliedMarkings } from "@sixb/core/internal/security"
|
|
10
|
-
import type { Server } from "bun"
|
|
11
|
+
import type { Server, ServerWebSocket } from "bun"
|
|
11
12
|
import { Elysia, ParseError } from "elysia"
|
|
12
13
|
import { websocket as elysiaWebSocket } from "elysia/ws"
|
|
13
14
|
import {
|
|
@@ -70,6 +71,8 @@ import {
|
|
|
70
71
|
|
|
71
72
|
// Clients send only subscription messages; Bun's default would buffer up to 16 MB for one.
|
|
72
73
|
export const MAX_WEBSOCKET_MESSAGE_BYTES = 64 * 1024
|
|
74
|
+
/** RFC 6455 Service Restart: tells clients to hand off immediately to the new release. */
|
|
75
|
+
const SERVICE_RESTART_CLOSE_CODE = 1012
|
|
73
76
|
|
|
74
77
|
export interface SixbServerOptions {
|
|
75
78
|
host: SixbHostView
|
|
@@ -100,6 +103,15 @@ export interface SixbServerOptions {
|
|
|
100
103
|
ingestFlush?: boolean
|
|
101
104
|
}
|
|
102
105
|
|
|
106
|
+
export interface SixbServerStopOptions {
|
|
107
|
+
/**
|
|
108
|
+
* How long in-flight HTTP requests may take to finish before remaining connections are closed.
|
|
109
|
+
* Defaults to 65 seconds, matching the Action drain window; `0` closes them at once. A stop
|
|
110
|
+
* called while another one drains joins it, and a shorter timeout brings its deadline closer.
|
|
111
|
+
*/
|
|
112
|
+
readonly timeoutMs?: number
|
|
113
|
+
}
|
|
114
|
+
|
|
103
115
|
export function createSixbServer(options: SixbServerOptions): SixbServer {
|
|
104
116
|
return new SixbServer(options)
|
|
105
117
|
}
|
|
@@ -118,10 +130,13 @@ export class SixbServer {
|
|
|
118
130
|
private readonly timing: SixbRequestTimingOptions
|
|
119
131
|
private app: SixbApp | null = null
|
|
120
132
|
private bunServer: ReturnType<typeof Bun.serve> | null = null
|
|
133
|
+
private closeWebSockets: (() => void) | null = null
|
|
134
|
+
private stopping = false
|
|
121
135
|
private readonly hostsOntologyMaintenance: boolean
|
|
122
136
|
private readonly flushesIngestion: boolean
|
|
123
137
|
private maintenance: OntologyMaintenanceHandle | null = null
|
|
124
138
|
private stopSweeper: (() => void) | null = null
|
|
139
|
+
private inProgressStop: InProgressStop | null = null
|
|
125
140
|
|
|
126
141
|
constructor(options: SixbServerOptions) {
|
|
127
142
|
this.hostRuntime = options.host
|
|
@@ -190,10 +205,20 @@ export class SixbServer {
|
|
|
190
205
|
return this.sharedAccessOptions
|
|
191
206
|
}
|
|
192
207
|
|
|
208
|
+
/** Marks the server as stopping so `/ready` answers `503` before connections close. */
|
|
209
|
+
markStopping(): void {
|
|
210
|
+
this.stopping = true
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
isStopping(): boolean {
|
|
214
|
+
return this.stopping
|
|
215
|
+
}
|
|
216
|
+
|
|
193
217
|
async start(): Promise<void> {
|
|
194
218
|
if (this.app !== null || this.bunServer !== null || this.maintenance !== null) {
|
|
195
219
|
throw new Error("[SixbServer] Server is already running.")
|
|
196
220
|
}
|
|
221
|
+
this.stopping = false
|
|
197
222
|
|
|
198
223
|
// Startup cannot see through SQL steps: check what they derive before serving any request.
|
|
199
224
|
await checkPipelineMarkings({
|
|
@@ -215,15 +240,18 @@ export class SixbServer {
|
|
|
215
240
|
|
|
216
241
|
try {
|
|
217
242
|
this.app = createSixbApi(this)
|
|
218
|
-
|
|
243
|
+
const started = startApiServer(this.app, {
|
|
219
244
|
host: this.hostname,
|
|
220
245
|
port: this.port,
|
|
221
246
|
resolveClientAddress: this.resolveClientAddress,
|
|
222
247
|
timing: this.timing,
|
|
223
248
|
})
|
|
249
|
+
this.bunServer = started.bunServer
|
|
250
|
+
this.closeWebSockets = started.closeWebSockets
|
|
224
251
|
} catch (error) {
|
|
225
252
|
this.app = null
|
|
226
253
|
this.bunServer = null
|
|
254
|
+
this.closeWebSockets = null
|
|
227
255
|
this.maintenance = null
|
|
228
256
|
await maintenance?.stop()
|
|
229
257
|
const message = error instanceof Error ? error.message : String(error)
|
|
@@ -245,18 +273,49 @@ export class SixbServer {
|
|
|
245
273
|
if (ingest) ingest.writesInline = inline
|
|
246
274
|
}
|
|
247
275
|
|
|
248
|
-
async stop(): Promise<void> {
|
|
276
|
+
async stop(options: SixbServerStopOptions = {}): Promise<void> {
|
|
277
|
+
const timeoutMs = resolveStopTimeoutMs(options.timeoutMs, ACTION_RUN_DRAIN_TIMEOUT_MS)
|
|
278
|
+
|
|
279
|
+
// A stop that arrives while another one drains joins it, and can only bring its deadline
|
|
280
|
+
// closer: `sixb api` passes `timeoutMs: 0` when SIGINT follows SIGTERM.
|
|
281
|
+
if (this.inProgressStop) {
|
|
282
|
+
this.inProgressStop.deadline.bringForward(timeoutMs)
|
|
283
|
+
return this.inProgressStop.done
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const deadline = new StopDeadline(timeoutMs)
|
|
287
|
+
const done = this.stopUntil(deadline).finally(() => {
|
|
288
|
+
this.inProgressStop = null
|
|
289
|
+
})
|
|
290
|
+
this.inProgressStop = { deadline, done }
|
|
291
|
+
return done
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private async stopUntil(deadline: StopDeadline): Promise<void> {
|
|
295
|
+
this.stopping = true
|
|
249
296
|
this.setIngestFlush(false)
|
|
250
297
|
this.stopSweeper?.()
|
|
251
298
|
this.stopSweeper = null
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
299
|
+
|
|
300
|
+
const bunServer = this.bunServer
|
|
301
|
+
const closeWebSockets = this.closeWebSockets
|
|
302
|
+
const app = this.app
|
|
303
|
+
this.bunServer = null
|
|
304
|
+
this.closeWebSockets = null
|
|
305
|
+
this.app = null
|
|
306
|
+
|
|
307
|
+
const stopAppAndSockets = async () => {
|
|
308
|
+
try {
|
|
309
|
+
if (app) await stopElysiaHooks(app)
|
|
310
|
+
} finally {
|
|
311
|
+
closeWebSockets?.()
|
|
312
|
+
}
|
|
255
313
|
}
|
|
256
314
|
|
|
257
|
-
if (
|
|
258
|
-
await
|
|
259
|
-
|
|
315
|
+
if (bunServer) {
|
|
316
|
+
await drainBunServer(bunServer, deadline, stopAppAndSockets)
|
|
317
|
+
} else {
|
|
318
|
+
await stopAppAndSockets()
|
|
260
319
|
}
|
|
261
320
|
|
|
262
321
|
const maintenance = this.maintenance
|
|
@@ -517,6 +576,7 @@ export function createSixbApi(server: SixbServer) {
|
|
|
517
576
|
registerHttpRoutes(app, host, {
|
|
518
577
|
sharedAccess,
|
|
519
578
|
browserPolicy: apiBrowserPolicy,
|
|
579
|
+
isStopping: () => server.isStopping(),
|
|
520
580
|
connectorConnections: {
|
|
521
581
|
resolveReturnTo: (request, returnTo) =>
|
|
522
582
|
server.resolveAuthRedirectContext(request, { returnTo }).returnTo,
|
|
@@ -608,6 +668,7 @@ function startApiServer(
|
|
|
608
668
|
readonly timing: SixbRequestTimingOptions
|
|
609
669
|
}
|
|
610
670
|
) {
|
|
671
|
+
const openSockets = new Set<ServerWebSocket<unknown>>()
|
|
611
672
|
const bunServer = Bun.serve({
|
|
612
673
|
port: options.port,
|
|
613
674
|
hostname: options.host,
|
|
@@ -624,22 +685,153 @@ function startApiServer(
|
|
|
624
685
|
}
|
|
625
686
|
return timeRequest(request, options.timing, () => app.fetch(request))
|
|
626
687
|
},
|
|
627
|
-
websocket: getElysiaWsHandler(app),
|
|
688
|
+
websocket: getElysiaWsHandler(app, openSockets),
|
|
628
689
|
} as Parameters<typeof Bun.serve>[0])
|
|
629
690
|
|
|
630
691
|
attachBunServer(app, bunServer)
|
|
631
|
-
return
|
|
692
|
+
return {
|
|
693
|
+
bunServer,
|
|
694
|
+
closeWebSockets: () => {
|
|
695
|
+
for (const ws of [...openSockets]) {
|
|
696
|
+
try {
|
|
697
|
+
ws.close(SERVICE_RESTART_CLOSE_CODE, "Service Restart")
|
|
698
|
+
} catch {
|
|
699
|
+
// Already closed.
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
openSockets.clear()
|
|
703
|
+
},
|
|
704
|
+
}
|
|
632
705
|
}
|
|
633
706
|
|
|
634
|
-
function getElysiaWsHandler(app: SixbApp) {
|
|
707
|
+
function getElysiaWsHandler(app: SixbApp, openSockets: Set<ServerWebSocket<unknown>>) {
|
|
635
708
|
const cfg = (app as unknown as { config?: { websocket?: Record<string, unknown> } }).config
|
|
636
|
-
|
|
709
|
+
const base = {
|
|
637
710
|
...elysiaWebSocket,
|
|
638
711
|
...(cfg?.websocket ?? {}),
|
|
712
|
+
} as NonNullable<Parameters<typeof Bun.serve>[0]["websocket"]>
|
|
713
|
+
return {
|
|
714
|
+
...base,
|
|
639
715
|
maxPayloadLength: MAX_WEBSOCKET_MESSAGE_BYTES,
|
|
716
|
+
open(ws: ServerWebSocket<unknown>) {
|
|
717
|
+
openSockets.add(ws)
|
|
718
|
+
return base.open?.(ws)
|
|
719
|
+
},
|
|
720
|
+
close(ws: ServerWebSocket<unknown>, code: number, reason: string) {
|
|
721
|
+
openSockets.delete(ws)
|
|
722
|
+
return base.close?.(ws, code, reason)
|
|
723
|
+
},
|
|
640
724
|
} as Parameters<typeof Bun.serve>[0]["websocket"]
|
|
641
725
|
}
|
|
642
726
|
|
|
727
|
+
function resolveStopTimeoutMs(timeoutMs: number | undefined, fallbackMs: number): number {
|
|
728
|
+
const resolved = timeoutMs ?? fallbackMs
|
|
729
|
+
if (!Number.isFinite(resolved) || resolved < 0) {
|
|
730
|
+
throw new Error("[SixbServer] Stop timeout must be a non-negative finite number.")
|
|
731
|
+
}
|
|
732
|
+
return resolved
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/** A stop under way: a later `stop()` joins `done` and may bring `deadline` closer. */
|
|
736
|
+
interface InProgressStop {
|
|
737
|
+
readonly deadline: StopDeadline
|
|
738
|
+
readonly done: Promise<void>
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** When a stopping server gives up on in-flight requests. A later `stop()` can only bring it closer. */
|
|
742
|
+
class StopDeadline {
|
|
743
|
+
private at: number
|
|
744
|
+
|
|
745
|
+
constructor(timeoutMs: number) {
|
|
746
|
+
this.at = Date.now() + timeoutMs
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
remainingMs(): number {
|
|
750
|
+
return this.at - Date.now()
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
bringForward(timeoutMs: number): void {
|
|
754
|
+
this.at = Math.min(this.at, Date.now() + timeoutMs)
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
async function drainBunServer(
|
|
759
|
+
server: ReturnType<typeof Bun.serve>,
|
|
760
|
+
deadline: StopDeadline,
|
|
761
|
+
beforeWait: () => Promise<void>
|
|
762
|
+
): Promise<void> {
|
|
763
|
+
if (deadline.remainingMs() <= 0) {
|
|
764
|
+
try {
|
|
765
|
+
await beforeWait()
|
|
766
|
+
} finally {
|
|
767
|
+
await server.stop(true)
|
|
768
|
+
}
|
|
769
|
+
return
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const draining = server.stop(false)
|
|
773
|
+
try {
|
|
774
|
+
await beforeWait()
|
|
775
|
+
} catch (error) {
|
|
776
|
+
await server.stop(true)
|
|
777
|
+
await draining
|
|
778
|
+
throw error
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Bun's `stop(false)` can stay pending on an already-answered 413 socket whose unread upload
|
|
782
|
+
// body has not hit its idle timeout yet, even though `pendingRequests` is already 0.
|
|
783
|
+
await waitUntilIdleOrTimeout(server, draining, deadline)
|
|
784
|
+
await server.stop(true)
|
|
785
|
+
await draining
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/** Polls every 25 ms, so a deadline brought forward takes effect within one poll. */
|
|
789
|
+
async function waitUntilIdleOrTimeout(
|
|
790
|
+
server: ReturnType<typeof Bun.serve>,
|
|
791
|
+
draining: Promise<void>,
|
|
792
|
+
deadline: StopDeadline
|
|
793
|
+
): Promise<void> {
|
|
794
|
+
let settled = false
|
|
795
|
+
void draining.then(
|
|
796
|
+
() => {
|
|
797
|
+
settled = true
|
|
798
|
+
},
|
|
799
|
+
() => {
|
|
800
|
+
settled = true
|
|
801
|
+
}
|
|
802
|
+
)
|
|
803
|
+
|
|
804
|
+
while (!settled && (server.pendingRequests > 0 || server.pendingWebSockets > 0)) {
|
|
805
|
+
const remaining = deadline.remainingMs()
|
|
806
|
+
if (remaining <= 0) return
|
|
807
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
808
|
+
try {
|
|
809
|
+
await Promise.race([
|
|
810
|
+
draining,
|
|
811
|
+
new Promise<void>((resolve) => {
|
|
812
|
+
timer = setTimeout(resolve, Math.min(remaining, 25))
|
|
813
|
+
}),
|
|
814
|
+
])
|
|
815
|
+
} finally {
|
|
816
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
643
821
|
function attachBunServer(app: SixbApp, bunServer: ReturnType<typeof Bun.serve>) {
|
|
644
822
|
;(app as unknown as { server: typeof bunServer }).server = bunServer
|
|
645
823
|
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Runs Elysia's `onStop` hooks (such as `LiveRooms.close()`) and awaits their promises without
|
|
827
|
+
* waiting on `bunServer.stop(false)` first, which stays pending while WebSockets are still open.
|
|
828
|
+
*/
|
|
829
|
+
async function stopElysiaHooks(app: SixbApp): Promise<void> {
|
|
830
|
+
const internal = app as unknown as {
|
|
831
|
+
server: unknown
|
|
832
|
+
event?: { stop?: ReadonlyArray<{ fn: (instance: SixbApp) => unknown }> }
|
|
833
|
+
}
|
|
834
|
+
const hooks = internal.event?.stop ?? []
|
|
835
|
+
internal.server = null
|
|
836
|
+
await Promise.all(hooks.map((hook) => Promise.resolve(hook.fn(app))))
|
|
837
|
+
}
|