@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,508 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import type {
|
|
3
|
+
ConnectorAdapter,
|
|
4
|
+
ConnectorClient,
|
|
5
|
+
ConnectorDefinition,
|
|
6
|
+
Logger,
|
|
7
|
+
OntologySource,
|
|
8
|
+
RegisteredWebhook,
|
|
9
|
+
Sixb,
|
|
10
|
+
WebhookDefinition,
|
|
11
|
+
WebhookMetadata,
|
|
12
|
+
WebhookResponse,
|
|
13
|
+
} from "@sixb/core"
|
|
14
|
+
import { reportRunFailure } from "@sixb/core/internal/error-reporting"
|
|
15
|
+
import type {
|
|
16
|
+
FinishWebhookRunStatus,
|
|
17
|
+
WebhookDeliveryClaimResult,
|
|
18
|
+
WebhookDeliveryKey,
|
|
19
|
+
} from "@sixb/core/storage"
|
|
20
|
+
import type { Elysia } from "elysia"
|
|
21
|
+
import { RequestBodyTooLargeError, readRequestBodyWithLimit } from "../utils/request-body"
|
|
22
|
+
|
|
23
|
+
const DEFAULT_WEBHOOK_BODY_LIMIT_BYTES = 1024 * 1024
|
|
24
|
+
|
|
25
|
+
interface ElysiaSet {
|
|
26
|
+
status?: number | string
|
|
27
|
+
headers?: unknown
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface DispatchWebhookOptions {
|
|
31
|
+
readonly sixb: Sixb<readonly OntologySource[]>
|
|
32
|
+
readonly registered: RegisteredWebhook
|
|
33
|
+
readonly request: Request
|
|
34
|
+
readonly set: ElysiaSet
|
|
35
|
+
readonly bodyLimitBytes?: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface WebhookRunFinishInput {
|
|
39
|
+
readonly sixb: Sixb<readonly OntologySource[]>
|
|
40
|
+
readonly runId: string
|
|
41
|
+
readonly status: FinishWebhookRunStatus
|
|
42
|
+
readonly requestBodyBytes?: number
|
|
43
|
+
readonly responseStatus?: number
|
|
44
|
+
readonly idempotencyKey?: string
|
|
45
|
+
readonly deliveryClaimResult?: WebhookDeliveryClaimResult
|
|
46
|
+
readonly error?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
type DeliveryClaimResult =
|
|
50
|
+
| {
|
|
51
|
+
readonly status: "run"
|
|
52
|
+
readonly key: WebhookDeliveryKey | null
|
|
53
|
+
readonly idempotencyKey?: string
|
|
54
|
+
readonly claimResult?: WebhookDeliveryClaimResult
|
|
55
|
+
}
|
|
56
|
+
| {
|
|
57
|
+
readonly status: "skip"
|
|
58
|
+
readonly idempotencyKey: string
|
|
59
|
+
readonly claimResult: WebhookDeliveryClaimResult
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function registerWebhookRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
63
|
+
return app.all(
|
|
64
|
+
"/api/webhooks/:connectorId/:webhookId",
|
|
65
|
+
async ({ params, request, set }) => {
|
|
66
|
+
const registered = sixb.getWebhookById(params.connectorId, params.webhookId)
|
|
67
|
+
|
|
68
|
+
if (!registered) {
|
|
69
|
+
set.status = 404
|
|
70
|
+
return { error: "Webhook not found" }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return dispatchWebhook({ sixb, registered, request, set })
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
detail: {
|
|
77
|
+
hide: true,
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function dispatchWebhook(options: DispatchWebhookOptions): Promise<unknown> {
|
|
84
|
+
const runId = `webhookrun_${randomUUID()}`
|
|
85
|
+
const { sixb, registered } = options
|
|
86
|
+
const logSession = sixb.logs.startExecution({ kind: "webhook", id: runId })
|
|
87
|
+
let failureReported = false
|
|
88
|
+
const reportFailure = (error: unknown) => {
|
|
89
|
+
if (failureReported) return
|
|
90
|
+
|
|
91
|
+
failureReported = true
|
|
92
|
+
reportRunFailure(sixb, error, {
|
|
93
|
+
projectId: sixb.id,
|
|
94
|
+
run: {
|
|
95
|
+
kind: "webhook",
|
|
96
|
+
runId,
|
|
97
|
+
connectorId: registered.connector.id,
|
|
98
|
+
webhookId: registered.webhook.id,
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
await startWebhookRun(sixb, registered, options.request, runId)
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
return await dispatchWebhookRun(
|
|
107
|
+
options,
|
|
108
|
+
runId,
|
|
109
|
+
(phase) => logSession.withContext({ phase }),
|
|
110
|
+
reportFailure
|
|
111
|
+
)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
await finishWebhookRun({
|
|
114
|
+
sixb,
|
|
115
|
+
runId,
|
|
116
|
+
status: "failed",
|
|
117
|
+
responseStatus: 500,
|
|
118
|
+
error: error instanceof Error ? error.message : String(error),
|
|
119
|
+
})
|
|
120
|
+
reportFailure(error)
|
|
121
|
+
throw error
|
|
122
|
+
} finally {
|
|
123
|
+
await logSession.flush()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function dispatchWebhookRun(
|
|
128
|
+
options: DispatchWebhookOptions,
|
|
129
|
+
runId: string,
|
|
130
|
+
loggerForPhase: (phase: string) => Logger,
|
|
131
|
+
reportFailure: (error: unknown) => void
|
|
132
|
+
): Promise<unknown> {
|
|
133
|
+
const { sixb, registered, request, set } = options
|
|
134
|
+
const { connector, webhook, route } = registered
|
|
135
|
+
const requestMethod = request.method.toUpperCase()
|
|
136
|
+
|
|
137
|
+
if (requestMethod !== webhook.method) {
|
|
138
|
+
set.status = 405
|
|
139
|
+
setHeader(set, "allow", webhook.method)
|
|
140
|
+
await finishWebhookRun({
|
|
141
|
+
sixb,
|
|
142
|
+
runId,
|
|
143
|
+
status: "failed",
|
|
144
|
+
responseStatus: 405,
|
|
145
|
+
error: "Method not allowed",
|
|
146
|
+
})
|
|
147
|
+
return { error: "Method not allowed" }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let rawBody: Uint8Array
|
|
151
|
+
try {
|
|
152
|
+
rawBody = await readRawBody(request, options.bodyLimitBytes ?? DEFAULT_WEBHOOK_BODY_LIMIT_BYTES)
|
|
153
|
+
} catch (error) {
|
|
154
|
+
const responseStatus = error instanceof RequestBodyTooLargeError ? 413 : 400
|
|
155
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
156
|
+
set.status = responseStatus
|
|
157
|
+
await finishWebhookRun({
|
|
158
|
+
sixb,
|
|
159
|
+
runId,
|
|
160
|
+
status: "failed",
|
|
161
|
+
responseStatus,
|
|
162
|
+
error: message,
|
|
163
|
+
})
|
|
164
|
+
return { error: message }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const metadata = toWebhookMetadata(webhook, route)
|
|
168
|
+
const baseContext = {
|
|
169
|
+
sixb,
|
|
170
|
+
connector,
|
|
171
|
+
webhook: metadata,
|
|
172
|
+
request,
|
|
173
|
+
rawBody,
|
|
174
|
+
}
|
|
175
|
+
const verifyContext = {
|
|
176
|
+
...baseContext,
|
|
177
|
+
logger: loggerForPhase("verify"),
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
await webhook.verify?.(verifyContext)
|
|
182
|
+
} catch {
|
|
183
|
+
set.status = 401
|
|
184
|
+
await finishWebhookRun({
|
|
185
|
+
sixb,
|
|
186
|
+
runId,
|
|
187
|
+
status: "failed",
|
|
188
|
+
requestBodyBytes: rawBody.byteLength,
|
|
189
|
+
responseStatus: 401,
|
|
190
|
+
error: "Webhook verification failed",
|
|
191
|
+
})
|
|
192
|
+
return { error: "Webhook verification failed" }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let body: unknown
|
|
196
|
+
try {
|
|
197
|
+
body = parseWebhookBody(webhook, rawBody)
|
|
198
|
+
} catch (error) {
|
|
199
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
200
|
+
set.status = 400
|
|
201
|
+
await finishWebhookRun({
|
|
202
|
+
sixb,
|
|
203
|
+
runId,
|
|
204
|
+
status: "failed",
|
|
205
|
+
requestBodyBytes: rawBody.byteLength,
|
|
206
|
+
responseStatus: 400,
|
|
207
|
+
error: message,
|
|
208
|
+
})
|
|
209
|
+
return { error: message }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const handlerContext = {
|
|
213
|
+
...baseContext,
|
|
214
|
+
logger: loggerForPhase("handle"),
|
|
215
|
+
body,
|
|
216
|
+
client: createClientResolver(sixb, connector),
|
|
217
|
+
}
|
|
218
|
+
const idempotencyContext = {
|
|
219
|
+
...handlerContext,
|
|
220
|
+
logger: loggerForPhase("idempotency"),
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Claim before running the handler so duplicate and concurrent provider
|
|
224
|
+
// retries can be acknowledged without repeating side effects.
|
|
225
|
+
let deliveryKey: WebhookDeliveryKey | null = null
|
|
226
|
+
let idempotencyKey: string | undefined
|
|
227
|
+
let deliveryClaimResult: WebhookDeliveryClaimResult | undefined
|
|
228
|
+
try {
|
|
229
|
+
const claim = await claimDeliveryKey(sixb, webhook, idempotencyContext)
|
|
230
|
+
idempotencyKey = claim.idempotencyKey
|
|
231
|
+
deliveryClaimResult = claim.claimResult
|
|
232
|
+
if (claim.status === "skip") {
|
|
233
|
+
const result = accepted(set)
|
|
234
|
+
await finishWebhookRun({
|
|
235
|
+
sixb,
|
|
236
|
+
runId,
|
|
237
|
+
status: "skipped",
|
|
238
|
+
requestBodyBytes: rawBody.byteLength,
|
|
239
|
+
responseStatus: 202,
|
|
240
|
+
idempotencyKey: claim.idempotencyKey,
|
|
241
|
+
deliveryClaimResult: claim.claimResult,
|
|
242
|
+
})
|
|
243
|
+
return result
|
|
244
|
+
}
|
|
245
|
+
deliveryKey = claim.key
|
|
246
|
+
} catch (error) {
|
|
247
|
+
reportFailure(error)
|
|
248
|
+
set.status = 500
|
|
249
|
+
await finishWebhookRun({
|
|
250
|
+
sixb,
|
|
251
|
+
runId,
|
|
252
|
+
status: "failed",
|
|
253
|
+
requestBodyBytes: rawBody.byteLength,
|
|
254
|
+
responseStatus: 500,
|
|
255
|
+
error: "Webhook delivery claim failed",
|
|
256
|
+
})
|
|
257
|
+
return { error: "Webhook delivery claim failed" }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
let response: unknown
|
|
261
|
+
try {
|
|
262
|
+
response = await webhook.handle(handlerContext as never)
|
|
263
|
+
} catch (error) {
|
|
264
|
+
reportFailure(error)
|
|
265
|
+
// Handler failures mark the key failed so the provider's next retry can
|
|
266
|
+
// attempt the delivery again.
|
|
267
|
+
if (deliveryKey) {
|
|
268
|
+
await sixb.storage.webhookDeliveries?.fail({
|
|
269
|
+
...deliveryKey,
|
|
270
|
+
failedAt: new Date().toISOString(),
|
|
271
|
+
error: error instanceof Error ? error.message : String(error),
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
set.status = 500
|
|
276
|
+
await finishWebhookRun({
|
|
277
|
+
sixb,
|
|
278
|
+
runId,
|
|
279
|
+
status: "failed",
|
|
280
|
+
requestBodyBytes: rawBody.byteLength,
|
|
281
|
+
responseStatus: 500,
|
|
282
|
+
idempotencyKey,
|
|
283
|
+
deliveryClaimResult,
|
|
284
|
+
error: "Webhook handler failed",
|
|
285
|
+
})
|
|
286
|
+
return { error: "Webhook handler failed" }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const responseStatus = getWebhookResponseStatus(response)
|
|
290
|
+
const runStatus = responseStatus >= 200 && responseStatus <= 299 ? "succeeded" : "failed"
|
|
291
|
+
const failureMessage = `Webhook handler returned HTTP ${responseStatus}`
|
|
292
|
+
const responseError = runStatus === "failed" ? failureMessage : undefined
|
|
293
|
+
const shouldRetryDelivery =
|
|
294
|
+
runStatus === "failed" && (responseStatus < 400 || responseStatus >= 500)
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
if (deliveryKey) {
|
|
298
|
+
const finalizedAt = new Date().toISOString()
|
|
299
|
+
if (shouldRetryDelivery) {
|
|
300
|
+
await sixb.storage.webhookDeliveries?.fail({
|
|
301
|
+
...deliveryKey,
|
|
302
|
+
failedAt: finalizedAt,
|
|
303
|
+
error: failureMessage,
|
|
304
|
+
})
|
|
305
|
+
} else {
|
|
306
|
+
await sixb.storage.webhookDeliveries?.complete({
|
|
307
|
+
...deliveryKey,
|
|
308
|
+
completedAt: finalizedAt,
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} catch (error) {
|
|
313
|
+
reportFailure(error)
|
|
314
|
+
set.status = 500
|
|
315
|
+
await finishWebhookRun({
|
|
316
|
+
sixb,
|
|
317
|
+
runId,
|
|
318
|
+
status: "failed",
|
|
319
|
+
requestBodyBytes: rawBody.byteLength,
|
|
320
|
+
responseStatus: 500,
|
|
321
|
+
idempotencyKey,
|
|
322
|
+
deliveryClaimResult,
|
|
323
|
+
error: "Webhook delivery completion failed",
|
|
324
|
+
})
|
|
325
|
+
return { error: "Webhook delivery completion failed" }
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (shouldRetryDelivery) {
|
|
329
|
+
reportFailure(new Error(failureMessage))
|
|
330
|
+
}
|
|
331
|
+
await finishWebhookRun({
|
|
332
|
+
sixb,
|
|
333
|
+
runId,
|
|
334
|
+
status: runStatus,
|
|
335
|
+
requestBodyBytes: rawBody.byteLength,
|
|
336
|
+
responseStatus,
|
|
337
|
+
idempotencyKey,
|
|
338
|
+
deliveryClaimResult,
|
|
339
|
+
error: responseError,
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
return applyWebhookResponse(set, response)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function startWebhookRun(
|
|
346
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
347
|
+
registered: RegisteredWebhook,
|
|
348
|
+
request: Request,
|
|
349
|
+
runId: string
|
|
350
|
+
): Promise<void> {
|
|
351
|
+
const storage = sixb.storage.webhookRuns
|
|
352
|
+
if (!storage) {
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
try {
|
|
357
|
+
await storage.start({
|
|
358
|
+
id: runId,
|
|
359
|
+
projectId: sixb.id,
|
|
360
|
+
connectorId: registered.connector.id,
|
|
361
|
+
webhookId: registered.webhook.id,
|
|
362
|
+
method: request.method.toUpperCase(),
|
|
363
|
+
route: registered.route,
|
|
364
|
+
})
|
|
365
|
+
} catch {
|
|
366
|
+
// Webhook run history is observability-only. Logging and dispatch continue
|
|
367
|
+
// even when history storage is unavailable or temporarily failing.
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async function finishWebhookRun(input: WebhookRunFinishInput): Promise<void> {
|
|
372
|
+
const storage = input.sixb.storage.webhookRuns
|
|
373
|
+
if (!storage) {
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
try {
|
|
378
|
+
await storage.finish({
|
|
379
|
+
id: input.runId,
|
|
380
|
+
projectId: input.sixb.id,
|
|
381
|
+
status: input.status,
|
|
382
|
+
finishedAt: new Date(),
|
|
383
|
+
requestBodyBytes: input.requestBodyBytes,
|
|
384
|
+
responseStatus: input.responseStatus,
|
|
385
|
+
idempotencyKey: input.idempotencyKey,
|
|
386
|
+
deliveryClaimResult: input.deliveryClaimResult,
|
|
387
|
+
error: input.error,
|
|
388
|
+
})
|
|
389
|
+
} catch {
|
|
390
|
+
// Webhook run history is observability-only. Do not change provider responses
|
|
391
|
+
// when history storage is unavailable or temporarily failing.
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function readRawBody(request: Request, limitBytes: number): Promise<Uint8Array> {
|
|
396
|
+
return readRequestBodyWithLimit(request, limitBytes, `Webhook body exceeds ${limitBytes} bytes.`)
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function parseWebhookBody(webhook: WebhookDefinition, rawBody: Uint8Array): unknown {
|
|
400
|
+
const decoded =
|
|
401
|
+
webhook.body.format === "raw"
|
|
402
|
+
? rawBody
|
|
403
|
+
: webhook.body.format === "text"
|
|
404
|
+
? new TextDecoder().decode(rawBody)
|
|
405
|
+
: JSON.parse(new TextDecoder().decode(rawBody))
|
|
406
|
+
|
|
407
|
+
return webhook.body.parse(decoded)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function claimDeliveryKey(
|
|
411
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
412
|
+
webhook: WebhookDefinition,
|
|
413
|
+
context: Parameters<NonNullable<WebhookDefinition["idempotencyKey"]>>[0]
|
|
414
|
+
): Promise<DeliveryClaimResult> {
|
|
415
|
+
if (!webhook.idempotencyKey) {
|
|
416
|
+
return { status: "run", key: null }
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const idempotencyKey = await webhook.idempotencyKey(context)
|
|
420
|
+
if (idempotencyKey === null || idempotencyKey === undefined) {
|
|
421
|
+
return { status: "run", key: null }
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const storage = sixb.storage.webhookDeliveries
|
|
425
|
+
if (!storage) {
|
|
426
|
+
throw new Error("Webhook delivery storage is not configured.")
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const deliveryKey = {
|
|
430
|
+
projectId: sixb.id,
|
|
431
|
+
connectorId: context.connector.id,
|
|
432
|
+
webhookId: webhook.id,
|
|
433
|
+
idempotencyKey,
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const result = await storage.claim({
|
|
437
|
+
...deliveryKey,
|
|
438
|
+
receivedAt: new Date().toISOString(),
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
if (result.claimResult === "duplicate" || result.claimResult === "in_progress") {
|
|
442
|
+
return { status: "skip", idempotencyKey, claimResult: result.claimResult }
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (result.claimResult !== "claimed") {
|
|
446
|
+
throw new Error(`Unknown webhook delivery claim result: ${result.claimResult}`)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return { status: "run", key: deliveryKey, idempotencyKey, claimResult: result.claimResult }
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function createClientResolver(
|
|
453
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
454
|
+
connector: ConnectorDefinition
|
|
455
|
+
): () => Promise<ConnectorClient<ConnectorAdapter>> {
|
|
456
|
+
let clientPromise: Promise<ConnectorClient<ConnectorAdapter>> | null = null
|
|
457
|
+
|
|
458
|
+
return () => {
|
|
459
|
+
// Avoid connecting inbound-only handlers or handlers that never need the
|
|
460
|
+
// outbound client, while still reusing one connection within the request.
|
|
461
|
+
clientPromise ??= sixb.connector(connector)
|
|
462
|
+
return clientPromise
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function toWebhookMetadata(webhook: WebhookDefinition, route: string): WebhookMetadata {
|
|
467
|
+
return {
|
|
468
|
+
id: webhook.id,
|
|
469
|
+
method: webhook.method,
|
|
470
|
+
route,
|
|
471
|
+
bodyFormat: webhook.body.format,
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function applyWebhookResponse(set: ElysiaSet, response: unknown): unknown {
|
|
476
|
+
const webhookResponse = isWebhookResponse(response) ? response : undefined
|
|
477
|
+
set.status = getWebhookResponseStatus(response)
|
|
478
|
+
|
|
479
|
+
if (webhookResponse?.headers) {
|
|
480
|
+
for (const [key, value] of new Headers(webhookResponse.headers)) {
|
|
481
|
+
setHeader(set, key, value)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return webhookResponse?.body
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function getWebhookResponseStatus(response: unknown): number {
|
|
489
|
+
const webhookResponse = isWebhookResponse(response) ? response : undefined
|
|
490
|
+
return webhookResponse?.status ?? 202
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function accepted(set: ElysiaSet): undefined {
|
|
494
|
+
set.status = 202
|
|
495
|
+
return undefined
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function setHeader(set: ElysiaSet, key: string, value: string): void {
|
|
499
|
+
if (!set.headers || typeof set.headers !== "object" || Array.isArray(set.headers)) {
|
|
500
|
+
set.headers = {}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
;(set.headers as Record<string, string>)[key] = value
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function isWebhookResponse(value: unknown): value is WebhookResponse {
|
|
507
|
+
return typeof value === "object" && value !== null
|
|
508
|
+
}
|