Docker images / prepare-release (push) Successful in 10s
Docker images / backend-test (push) Successful in 10m25s
Docker images / frontend-image (push) Successful in 3m18s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 2m48s
Docker images / publish-release (push) Successful in 10s
Docker images / notify-webhook (push) Successful in 1s
35 lines
1.8 KiB
TypeScript
35 lines
1.8 KiB
TypeScript
import type { FastifyInstance, FastifyRequest } from "fastify"
|
|
import { sharedDatabaseGate } from "../services/shared-database-gate.js"
|
|
import { withConfigurationActor } from "../services/config-mutation-policy.js"
|
|
import { withDatabaseApiRequest } from "../modules/mcp/database-request-lease.js"
|
|
const streams = new WeakMap<FastifyRequest, { cancelled: boolean; abort?: () => void }>()
|
|
|
|
export function registerDatabaseStreamAbort(request: FastifyRequest, abort: () => void): void {
|
|
const state = streams.get(request)
|
|
if (!state) return
|
|
state.abort = abort
|
|
if (state.cancelled) abort()
|
|
}
|
|
|
|
export function installDatabaseRequestGate(app: FastifyInstance, admit = withDatabaseApiRequest): void {
|
|
const gate = sharedDatabaseGate
|
|
app.addHook("onRoute", options => {
|
|
const path = options.url
|
|
if (!path.startsWith("/api/") || path === "/api/auth/config") return
|
|
const original = options.handler
|
|
const operation = path === "/api/system/database/backup" || path === "/api/system/database/restore"
|
|
const stream = path === "/api/logs/live" || path === "/api/traffic/flow/live" || /^\/api\/traffic\/servers\/:id\/live$/.test(path)
|
|
options.handler = async function (request, reply) {
|
|
const run = async () => withConfigurationActor(request.authUser?.id ?? "system", async () => await original.call(this, request, reply))
|
|
// The service owns exclusivity, including calls that do not originate in HTTP.
|
|
if (operation) return run()
|
|
if (!stream) return gate.request(() => admit(run, !["GET", "HEAD", "OPTIONS"].includes(request.method)))
|
|
const state = { cancelled: false, abort: undefined as (() => void) | undefined }
|
|
streams.set(request, state)
|
|
try {
|
|
return await gate.request(run, () => { state.cancelled = true; state.abort?.() })
|
|
} finally { streams.delete(request) }
|
|
}
|
|
})
|
|
}
|