@sixb/server 0.1.18 → 0.1.19

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.
Files changed (57) hide show
  1. package/README.md +3 -3
  2. package/dist/actions/serialize.d.ts +7 -0
  3. package/dist/actions/serialize.d.ts.map +1 -0
  4. package/dist/index.js +660 -685
  5. package/dist/index.js.map +14 -13
  6. package/dist/routes/action-runs.d.ts +11 -13
  7. package/dist/routes/action-runs.d.ts.map +1 -1
  8. package/dist/routes/actions.d.ts +44 -13
  9. package/dist/routes/actions.d.ts.map +1 -1
  10. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  11. package/dist/routes/agents.d.ts +20 -20
  12. package/dist/routes/connector-connection-runs.d.ts +21 -21
  13. package/dist/routes/connector-connections.d.ts +8 -8
  14. package/dist/routes/connectors.d.ts +2 -2
  15. package/dist/routes/datasets.d.ts +5 -5
  16. package/dist/routes/events.d.ts +4 -4
  17. package/dist/routes/files.d.ts +15 -12
  18. package/dist/routes/files.d.ts.map +1 -1
  19. package/dist/routes/ontology.d.ts +6 -6
  20. package/dist/routes/pipelines.d.ts +16 -16
  21. package/dist/routes/projections.d.ts +49 -49
  22. package/dist/routes/syncs.d.ts +10 -10
  23. package/dist/routes/webhook-runs.d.ts +2 -2
  24. package/dist/routes/workflows.d.ts +48 -48
  25. package/dist/routes/ws/events.d.ts +6 -9
  26. package/dist/routes/ws/events.d.ts.map +1 -1
  27. package/dist/schemas/actions.d.ts +100 -84
  28. package/dist/schemas/actions.d.ts.map +1 -1
  29. package/dist/schemas/agents.d.ts +20 -20
  30. package/dist/schemas/ai-limits.d.ts +10 -10
  31. package/dist/schemas/connectors.d.ts +8 -8
  32. package/dist/schemas/datasets.d.ts +40 -40
  33. package/dist/schemas/events.d.ts +24 -24
  34. package/dist/schemas/events.d.ts.map +1 -1
  35. package/dist/schemas/files.d.ts +4 -4
  36. package/dist/schemas/ontology.d.ts +40 -40
  37. package/dist/schemas/pipelines.d.ts +92 -92
  38. package/dist/schemas/projections.d.ts +290 -290
  39. package/dist/schemas/syncs.d.ts +34 -34
  40. package/dist/schemas/webhook-runs.d.ts +8 -8
  41. package/dist/schemas/workflows.d.ts +160 -160
  42. package/dist/server.d.ts +1 -0
  43. package/dist/server.d.ts.map +1 -1
  44. package/dist/utils/http.d.ts.map +1 -1
  45. package/dist/utils/request-body.d.ts +20 -6
  46. package/dist/utils/request-body.d.ts.map +1 -1
  47. package/package.json +5 -5
  48. package/src/actions/serialize.ts +55 -0
  49. package/src/routes/action-runs.ts +1 -54
  50. package/src/routes/actions.ts +26 -8
  51. package/src/routes/agent-api-gateway.ts +18 -42
  52. package/src/routes/files.ts +15 -63
  53. package/src/routes/ws/events.ts +2 -12
  54. package/src/schemas/actions.ts +21 -18
  55. package/src/server.ts +21 -3
  56. package/src/utils/http.ts +2 -0
  57. package/src/utils/request-body.ts +55 -6
package/src/server.ts CHANGED
@@ -5,7 +5,7 @@ import { CSRF_HEADER_NAME, setRequestClientAddress } from "@sixb/core/internal/a
5
5
  import { bindRequestExecution } from "@sixb/core/internal/request-execution"
6
6
  import { setApiPublicOrigin } from "@sixb/core/internal/runtime"
7
7
  import type { Server } from "bun"
8
- import { Elysia } from "elysia"
8
+ import { Elysia, ParseError } from "elysia"
9
9
  import { websocket as elysiaWebSocket } from "elysia/ws"
10
10
  import { zodToJsonSchema } from "zod-to-json-schema"
11
11
  import {
@@ -56,10 +56,15 @@ import {
56
56
  import { OPENAPI_TAG_METADATA } from "./openapi/tags"
57
57
  import { registerHttpRoutes } from "./registerRoutes"
58
58
  import { registerAuthRoutes } from "./routes/auth"
59
+ import { DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES } from "./routes/files"
59
60
  import { registerWebhookRoutes } from "./routes/webhooks"
60
61
  import { registerWebSocketRoutes } from "./routes/ws"
61
62
  import { jsonValueOpenApiOverride } from "./schemas/common"
62
63
  import { ObjectQueryOpenApiSchemas } from "./schemas/objects"
64
+ import { limitRequestBody, RequestBodyTooLargeError } from "./utils/request-body"
65
+
66
+ // Clients send only subscription messages; Bun's default would buffer up to 16 MB for one.
67
+ export const MAX_WEBSOCKET_MESSAGE_BYTES = 64 * 1024
63
68
 
64
69
  export interface SixbServerOptions {
65
70
  host: SixbHostView
@@ -263,6 +268,13 @@ export function createSixbApi(server: SixbServer) {
263
268
  }
264
269
  return shared.kind === "deny" ? shared.response : undefined
265
270
  })
271
+ // A body over its limit fails while Elysia parses it, before the auth guard or handler runs.
272
+ app.onError(({ error, set }) => {
273
+ if (error instanceof ParseError && error.cause instanceof RequestBodyTooLargeError) {
274
+ set.status = 413
275
+ return { error: error.cause.message }
276
+ }
277
+ })
266
278
 
267
279
  // Resolve authentication and bind one execution SDK at the request boundary. Protected routes
268
280
  // never choose between a principal SDK and the ambient runtime themselves.
@@ -532,8 +544,13 @@ function startApiServer(
532
544
  const bunServer = Bun.serve({
533
545
  port: options.port,
534
546
  hostname: options.host,
535
- fetch: (request: Request, server: Server<unknown>) => {
536
- const socketAddress = server.requestIP(request)?.address
547
+ // No route reads more than a simple upload form, so Bun rejects anything larger with 413.
548
+ maxRequestBodySize: DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES,
549
+ fetch: (incoming: Request, server: Server<unknown>) => {
550
+ const socketAddress = server.requestIP(incoming)?.address
551
+ // Elysia parses a body before the auth guard runs, so cap it here, as it streams. The capped
552
+ // request replaces this one before any state is keyed to it.
553
+ const request = limitRequestBody(incoming)
537
554
  if (socketAddress) {
538
555
  const forwardedFor = request.headers.get("x-forwarded-for")
539
556
  setRequestClientAddress(request, options.resolveClientAddress(socketAddress, forwardedFor))
@@ -552,6 +569,7 @@ function getElysiaWsHandler(app: SixbApp) {
552
569
  return {
553
570
  ...elysiaWebSocket,
554
571
  ...(cfg?.websocket ?? {}),
572
+ maxPayloadLength: MAX_WEBSOCKET_MESSAGE_BYTES,
555
573
  } as Parameters<typeof Bun.serve>[0]["websocket"]
556
574
  }
557
575
 
package/src/utils/http.ts CHANGED
@@ -15,6 +15,7 @@ import { RequestBodyTooLargeError } from "./request-body"
15
15
 
16
16
  /** Explicit transport policy for coded failures that are safe to surface as non-500 responses. */
17
17
  const HTTP_STATUS_BY_ERROR_CODE: Partial<Record<SixbErrorCode, number>> = {
18
+ "action.run_in_progress": 409,
18
19
  "connector.adapter_invalid": 502,
19
20
  "connector.authorization_invalid": 400,
20
21
  "connector.authorization_required": 409,
@@ -29,6 +30,7 @@ const HTTP_STATUS_BY_ERROR_CODE: Partial<Record<SixbErrorCode, number>> = {
29
30
  "connector.revocation_pending": 409,
30
31
  "dataset.not_found": 404,
31
32
  "dataset.version_not_found": 404,
33
+ "runtime.stopping": 503,
32
34
  "ai.usage_limit_exceeded": 429,
33
35
  "ai.usage_limit_unavailable": 429,
34
36
  }
@@ -1,11 +1,16 @@
1
1
  /**
2
- * Reads a request body fully into memory while enforcing a hard byte ceiling.
2
+ * Request body limits for the API.
3
3
  *
4
- * The cap is applied DURING streaming (not after buffering), so an oversized or
5
- * chunked/unknown-length body is rejected before it is fully read — the
6
- * `content-length` header, when present, only provides an early fast-path. This
7
- * is the single bounded-reader used by request handlers that need the raw bytes.
4
+ * Elysia parses a JSON or form body before the auth guard runs, so the server caps every body it
5
+ * receives at `DEFAULT_REQUEST_BODY_LIMIT_BYTES` (`limitRequestBody`). A route that takes more,
6
+ * such as a file upload, reads its own body with `readRequestBodyWithLimit`, whose limit replaces
7
+ * that cap. Both apply the limit DURING streaming (not after buffering), so an oversized or
8
+ * chunked/unknown-length body is rejected before it is fully read.
8
9
  */
10
+
11
+ /** The most a request body may be, unless the route reading it allows more. */
12
+ export const DEFAULT_REQUEST_BODY_LIMIT_BYTES = 1024 * 1024
13
+
9
14
  export class RequestBodyTooLargeError extends Error {
10
15
  readonly name = "RequestBodyTooLargeError"
11
16
 
@@ -17,11 +22,55 @@ export class RequestBodyTooLargeError extends Error {
17
22
  }
18
23
  }
19
24
 
25
+ const requestBodyCaps = new WeakMap<Request, { lifted: boolean }>()
26
+
27
+ /**
28
+ * Returns `request` with its body capped at `DEFAULT_REQUEST_BODY_LIMIT_BYTES`. Reading past the
29
+ * cap fails with `RequestBodyTooLargeError`, which Elysia surfaces as a parse error.
30
+ */
31
+ export function limitRequestBody(request: Request): Request {
32
+ if (!request.body) {
33
+ return request
34
+ }
35
+
36
+ const cap = { lifted: false }
37
+ let total = 0
38
+ const body = request.body.pipeThrough(
39
+ new TransformStream<Uint8Array, Uint8Array>({
40
+ transform(chunk, controller) {
41
+ total += chunk.byteLength
42
+ if (!cap.lifted && total > DEFAULT_REQUEST_BODY_LIMIT_BYTES) {
43
+ controller.error(new RequestBodyTooLargeError(DEFAULT_REQUEST_BODY_LIMIT_BYTES))
44
+ return
45
+ }
46
+ controller.enqueue(chunk)
47
+ },
48
+ })
49
+ )
50
+ const limited = new Request(request, { body, duplex: "half" } as RequestInit & {
51
+ duplex: "half"
52
+ })
53
+ requestBodyCaps.set(limited, cap)
54
+ return limited
55
+ }
56
+
57
+ /**
58
+ * Reads a request body fully into memory, failing once it passes `limitBytes`. That limit
59
+ * replaces the server's default cap for this request, so a route reading more than the default
60
+ * must authenticate the caller first. The `content-length` header, when present, only provides an
61
+ * early fast-path.
62
+ */
20
63
  export async function readRequestBodyWithLimit(
21
64
  request: Request,
22
65
  limitBytes: number,
23
66
  tooLargeMessage?: string
24
- ): Promise<Uint8Array> {
67
+ ): Promise<Uint8Array<ArrayBuffer>> {
68
+ // The loop below enforces `limitBytes` in place of the server's cap.
69
+ const cap = requestBodyCaps.get(request)
70
+ if (cap) {
71
+ cap.lifted = true
72
+ }
73
+
25
74
  const contentLength = request.headers.get("content-length")
26
75
  if (contentLength !== null) {
27
76
  const declared = Number(contentLength)