@sixb/server 0.1.19 → 0.1.21

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 (103) hide show
  1. package/README.md +13 -22
  2. package/dist/artifacts/frame.d.ts +13 -0
  3. package/dist/artifacts/frame.d.ts.map +1 -0
  4. package/dist/auth/browser-origin.d.ts +9 -26
  5. package/dist/auth/browser-origin.d.ts.map +1 -1
  6. package/dist/auth/experience.d.ts +0 -3
  7. package/dist/auth/experience.d.ts.map +1 -1
  8. package/dist/auth/guard.d.ts +1 -1
  9. package/dist/auth/guard.d.ts.map +1 -1
  10. package/dist/auth/public-routes.d.ts +0 -5
  11. package/dist/auth/public-routes.d.ts.map +1 -1
  12. package/dist/auth/responses.d.ts +0 -2
  13. package/dist/auth/responses.d.ts.map +1 -1
  14. package/dist/auth/session-cookies.d.ts.map +1 -1
  15. package/dist/files/content.d.ts +2 -3
  16. package/dist/files/content.d.ts.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +3241 -2445
  20. package/dist/index.js.map +31 -26
  21. package/dist/openapi/tags.d.ts +10 -2
  22. package/dist/openapi/tags.d.ts.map +1 -1
  23. package/dist/registerRoutes.d.ts +3 -0
  24. package/dist/registerRoutes.d.ts.map +1 -1
  25. package/dist/routes/action-runs.d.ts +0 -2
  26. package/dist/routes/action-runs.d.ts.map +1 -1
  27. package/dist/routes/activity.d.ts +35 -0
  28. package/dist/routes/activity.d.ts.map +1 -0
  29. package/dist/routes/agents.d.ts +18 -29
  30. package/dist/routes/agents.d.ts.map +1 -1
  31. package/dist/routes/artifact-frames.d.ts +98 -0
  32. package/dist/routes/artifact-frames.d.ts.map +1 -0
  33. package/dist/routes/artifacts.d.ts +852 -0
  34. package/dist/routes/artifacts.d.ts.map +1 -0
  35. package/dist/routes/auth.d.ts +9 -18
  36. package/dist/routes/auth.d.ts.map +1 -1
  37. package/dist/routes/connector-connection-runs.d.ts +59 -59
  38. package/dist/routes/connector-connections.d.ts +2 -2
  39. package/dist/routes/datasets.d.ts +7 -7
  40. package/dist/routes/events.d.ts +1 -1
  41. package/dist/routes/objects.d.ts +0 -2
  42. package/dist/routes/objects.d.ts.map +1 -1
  43. package/dist/routes/pipelines.d.ts +31 -31
  44. package/dist/routes/projections.d.ts +10 -10
  45. package/dist/routes/share-grants.d.ts +5 -5
  46. package/dist/routes/syncs.d.ts +38 -38
  47. package/dist/routes/workflows.d.ts +30 -34
  48. package/dist/routes/workflows.d.ts.map +1 -1
  49. package/dist/routes/ws/events.d.ts.map +1 -1
  50. package/dist/routes/ws/subscription-hub.d.ts +8 -3
  51. package/dist/routes/ws/subscription-hub.d.ts.map +1 -1
  52. package/dist/schemas/activity.d.ts +1278 -0
  53. package/dist/schemas/activity.d.ts.map +1 -0
  54. package/dist/schemas/agents.d.ts +96 -144
  55. package/dist/schemas/agents.d.ts.map +1 -1
  56. package/dist/schemas/ai-accounting.d.ts +24 -24
  57. package/dist/schemas/ai-limits.d.ts +26 -26
  58. package/dist/schemas/artifacts.d.ts +1305 -0
  59. package/dist/schemas/artifacts.d.ts.map +1 -0
  60. package/dist/schemas/auth.d.ts +34 -69
  61. package/dist/schemas/auth.d.ts.map +1 -1
  62. package/dist/schemas/connectors.d.ts +30 -30
  63. package/dist/schemas/datasets.d.ts +10 -10
  64. package/dist/schemas/events.d.ts +6 -6
  65. package/dist/schemas/files.d.ts +0 -3
  66. package/dist/schemas/files.d.ts.map +1 -1
  67. package/dist/schemas/pipelines.d.ts +35 -35
  68. package/dist/schemas/projections.d.ts +40 -40
  69. package/dist/schemas/share-grants.d.ts +16 -16
  70. package/dist/schemas/syncs.d.ts +36 -36
  71. package/dist/schemas/workflows.d.ts +84 -84
  72. package/dist/server.d.ts +1 -2
  73. package/dist/server.d.ts.map +1 -1
  74. package/package.json +6 -6
  75. package/src/artifacts/frame.ts +46 -0
  76. package/src/auth/browser-origin.ts +40 -240
  77. package/src/auth/experience.ts +1 -4
  78. package/src/auth/guard.ts +4 -32
  79. package/src/auth/public-routes.ts +11 -10
  80. package/src/auth/responses.ts +0 -5
  81. package/src/auth/session-cookies.ts +1 -3
  82. package/src/auth/shared-access.ts +1 -1
  83. package/src/files/content.ts +9 -13
  84. package/src/index.ts +1 -1
  85. package/src/openapi/tags.ts +8 -0
  86. package/src/registerRoutes.ts +9 -0
  87. package/src/routes/activity.ts +327 -0
  88. package/src/routes/agents.ts +1 -1
  89. package/src/routes/artifact-frames.ts +138 -0
  90. package/src/routes/artifacts.ts +442 -0
  91. package/src/routes/auth.ts +32 -111
  92. package/src/routes/file-downloads.ts +1 -3
  93. package/src/routes/ws/events.ts +3 -2
  94. package/src/routes/ws/subscription-hub.ts +53 -20
  95. package/src/schemas/activity.ts +285 -0
  96. package/src/schemas/agents.ts +1 -2
  97. package/src/schemas/artifacts.ts +158 -0
  98. package/src/schemas/auth.ts +2 -15
  99. package/src/schemas/files.ts +0 -6
  100. package/src/server.ts +27 -24
  101. package/dist/auth/application-access.d.ts +0 -4
  102. package/dist/auth/application-access.d.ts.map +0 -1
  103. package/src/auth/application-access.ts +0 -15
@@ -1,10 +1,5 @@
1
1
  import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto"
2
- import {
3
- type AuthSessionAudience,
4
- DEFAULT_AUTH_SESSION_AUDIENCE,
5
- type GroupDefinition,
6
- type SixbHostView,
7
- } from "@sixb/core"
2
+ import type { GroupDefinition, SixbHostView } from "@sixb/core"
8
3
  import {
9
4
  type AuthenticatedAuthSession,
10
5
  type AuthenticatedUserRequestSession,
@@ -38,15 +33,12 @@ import {
38
33
  } from "@sixb/core/storage"
39
34
  import { type Elysia, t } from "elysia"
40
35
  import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
41
- import { sessionCanAccessApplication } from "../auth/application-access"
42
36
  import {
43
- type AuthInvitationDestinationOptions,
44
37
  type AuthInvitationRedirectContext,
45
38
  type AuthInvitationRedirectInput,
46
39
  type AuthRedirectContext,
47
40
  BrowserOriginError,
48
41
  type ResolveAuthRedirectContext,
49
- type ResolveRequestAuthContext,
50
42
  } from "../auth/browser-origin"
51
43
  import { CSRF_TOKEN_RESPONSE_HEADER_NAME } from "../auth/csrf"
52
44
  import {
@@ -117,10 +109,6 @@ type ResolvedCookieOptions = Parameters<typeof createCsrfCookieHeader>[0]["optio
117
109
  type AuthenticatedAuthSessionResponse = {
118
110
  readonly authenticated: true
119
111
  readonly csrfToken: string
120
- readonly applicationAccess: {
121
- readonly allowed: boolean
122
- readonly audience: AuthSessionAudience
123
- }
124
112
  readonly user: {
125
113
  readonly id: string
126
114
  readonly email: string
@@ -135,9 +123,7 @@ type AuthenticatedAuthSessionResponse = {
135
123
  }
136
124
 
137
125
  export interface AuthRoutesOptions {
138
- readonly resolveAuthContext: ResolveRequestAuthContext
139
126
  readonly resolveAuthRedirectContext: ResolveAuthRedirectContext
140
- readonly getInvitationDestinationOptions: (request: Request) => AuthInvitationDestinationOptions
141
127
  readonly resolveAuthRequestOrigin: (request: Request) => string
142
128
  readonly resolveInvitationRedirectContext: (
143
129
  request: Request,
@@ -341,10 +327,9 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
341
327
  ) {
342
328
  return authPageResponse("<h1>This authorization has expired</h1>", 400)
343
329
  }
344
- const authOptions = resolveAuthOptions(options, request)
345
- const session = await host.auth.getSession(request, authOptions)
330
+ const session = await host.auth.getSession(request)
346
331
  if (!session.authenticated) return authPageResponse("<h1>Authentication required</h1>", 401)
347
- const cookieOptions = host.auth.getCookieOptions(authOptions)
332
+ const cookieOptions = host.auth.getCookieOptions()
348
333
  const csrf = resolveSessionCsrfToken({
349
334
  request,
350
335
  cookieOptions,
@@ -377,11 +362,10 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
377
362
  "/auth/device",
378
363
  async ({ body, request }) => {
379
364
  const parsed = DeviceAuthorizationDecisionBodySchema.parse(body)
380
- const authOptions = resolveAuthOptions(options, request)
381
- const session = await host.auth.getSession(request, authOptions)
382
- // Every signed-in user may approve their own CLI login, whichever web app they may open.
365
+ const session = await host.auth.getSession(request)
366
+ // Every signed-in user may approve their own CLI login.
383
367
  if (!session.authenticated) return authPageResponse("<h1>Authentication required</h1>", 401)
384
- const csrfCookieName = host.auth.getCookieOptions(authOptions).csrfCookieName
368
+ const csrfCookieName = host.auth.getCookieOptions().csrfCookieName
385
369
  if (!verifyCsrfToken(request, { cookieName: csrfCookieName, token: parsed.csrfToken })) {
386
370
  return authPageResponse("<h1>Invalid authorization request</h1>", 403)
387
371
  }
@@ -434,30 +418,23 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
434
418
  .get(
435
419
  "/api/auth/session",
436
420
  async ({ request }) => {
437
- const authOptions = resolveAuthOptions(options, request)
438
- const caller = await host.auth.getSession(request, {
439
- ...authOptions,
440
- credentialSource: "any",
441
- })
421
+ const caller = await host.auth.getSession(request, { credentialSource: "any" })
442
422
  // Access tokens are not sessions; a native client's session has no cookies to renew.
443
423
  if (!caller.authenticated || caller.credentialSource !== "session") {
444
424
  return signedOutSessionResponse(host)
445
425
  }
446
- // A native client belongs to no web app; tell it whether its user may open Atlas.
447
426
  if (caller.session.bearer) {
448
- return jsonResponse(authSessionBody(host, caller, DEFAULT_AUTH_SESSION_AUDIENCE), 200)
427
+ return jsonResponse(authSessionBody(caller), 200)
449
428
  }
450
429
 
451
- const session =
452
- hasForegroundSessionActivity(request) &&
453
- sessionCanAccessApplication(host, caller, authOptions.audience)
454
- ? await host.auth.getSession(request, { ...authOptions, sessionActivity: "foreground" })
455
- : caller
430
+ const session = hasForegroundSessionActivity(request)
431
+ ? await host.auth.getSession(request, { sessionActivity: "foreground" })
432
+ : caller
456
433
  if (!session.authenticated) {
457
434
  return signedOutSessionResponse(host)
458
435
  }
459
436
 
460
- const cookieOptions = host.auth.getCookieOptions(authOptions)
437
+ const cookieOptions = host.auth.getCookieOptions()
461
438
  const csrf = resolveSessionCsrfToken({
462
439
  request,
463
440
  cookieOptions,
@@ -473,7 +450,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
473
450
  })
474
451
  : null
475
452
  return authSessionJsonResponse(
476
- { ...authSessionBody(host, session, authOptions.audience), csrfToken: csrf.token },
453
+ { ...authSessionBody(session), csrfToken: csrf.token },
477
454
  renewal?.headers ?? (csrf.setCookie ? [csrf.setCookie] : [])
478
455
  )
479
456
  },
@@ -490,15 +467,11 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
490
467
  .post(
491
468
  "/api/auth/sign-out",
492
469
  async ({ request }) => {
493
- const authOptions = resolveAuthOptions(options, request)
494
- const result = await host.auth.getSession(request, {
495
- ...authOptions,
496
- credentialSource: "any",
497
- })
470
+ const result = await host.auth.getSession(request, { credentialSource: "any" })
498
471
  // Access tokens are not sessions; revoke those through their own route.
499
472
  const session =
500
473
  result.authenticated && result.credentialSource === "session" ? result : null
501
- const cookieOptions = host.auth.getCookieOptions(authOptions)
474
+ const cookieOptions = host.auth.getCookieOptions()
502
475
  if (
503
476
  session &&
504
477
  !session.session.bearer &&
@@ -541,7 +514,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
541
514
  {
542
515
  sessions: sessions.map((entry) => ({
543
516
  id: entry.id,
544
- audience: entry.bearer ? undefined : entry.audience,
545
517
  current: entry.id === session.session.id,
546
518
  createdAt: toIsoString(entry.createdAt),
547
519
  expiresAt: toIsoString(entry.expiresAt),
@@ -582,7 +554,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
582
554
  if (sessionId !== session.session.id) {
583
555
  return jsonResponse({ success: true }, 200)
584
556
  }
585
- const cookieOptions = host.auth.getCookieOptions(resolveAuthOptions(options, request))
557
+ const cookieOptions = host.auth.getCookieOptions()
586
558
  return signedOutResponse(request, cookieOptions, { success: true })
587
559
  },
588
560
  {
@@ -609,10 +581,10 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
609
581
  return session
610
582
  }
611
583
 
612
- // Other audiences' cookies stay in their browsers, but their sessions are revoked, so the
613
- // next request there re-authenticates.
584
+ // Other browsers keep their cookies, but their sessions are revoked, so the next request
585
+ // there re-authenticates.
614
586
  const revokedCount = await host.auth.revokeAllSessions(session)
615
- const cookieOptions = host.auth.getCookieOptions(resolveAuthOptions(options, request))
587
+ const cookieOptions = host.auth.getCookieOptions()
616
588
  return signedOutResponse(request, cookieOptions, { success: true, revokedCount })
617
589
  },
618
590
  {
@@ -716,7 +688,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
716
688
  })
717
689
 
718
690
  // The raw token is only returned on creation. Storage keeps a hash, so
719
- // Atlas can never reveal it again after this response leaves the page.
691
+ // nothing can reveal it again after this response leaves the page.
720
692
  return jsonResponse(
721
693
  {
722
694
  accessToken: serializeAccessToken(result.accessToken, {
@@ -1030,7 +1002,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1030
1002
  const caller = requireUserCaller(context)
1031
1003
  const parsed = CreateAuthInvitationBodySchema.parse(body)
1032
1004
  const deliveryContext = resolveInvitationDeliveryContext(options, request, {
1033
- destinationId: parsed.destinationId,
1034
1005
  returnTo: parsed.returnTo,
1035
1006
  })
1036
1007
  if (deliveryContext instanceof Response) {
@@ -1083,13 +1054,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1083
1054
  async ({ request, ...context }) => {
1084
1055
  try {
1085
1056
  const caller = requireUserCaller(context)
1086
- return jsonResponse(
1087
- {
1088
- ...(await host.auth.getInvitationOptions(caller)),
1089
- ...options.getInvitationDestinationOptions(request),
1090
- },
1091
- 200
1092
- )
1057
+ return jsonResponse(await host.auth.getInvitationOptions(caller), 200)
1093
1058
  } catch (error) {
1094
1059
  return authRouteErrorResponse(error)
1095
1060
  }
@@ -1351,7 +1316,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1351
1316
  async ({ body, request }) => {
1352
1317
  const strategy = host.auth.getStrategy()
1353
1318
  const authRedirect = resolveAuthRedirectContext(options, request, {
1354
- audience: body.audience,
1355
1319
  returnTo: body.returnTo,
1356
1320
  })
1357
1321
  if (authRedirect instanceof Response) {
@@ -1366,7 +1330,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1366
1330
  projectId: host.id,
1367
1331
  authStorage,
1368
1332
  email: body.email ?? "",
1369
- audience: authRedirect.audience,
1370
1333
  returnTo: authRedirect.returnTo,
1371
1334
  requestOrigin: authRedirect.requestOrigin,
1372
1335
  requesterHash: pending.hash,
@@ -1399,7 +1362,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1399
1362
  const result = await strategy.startOidcSignIn({
1400
1363
  projectId: host.id,
1401
1364
  authStorage,
1402
- audience: authRedirect.audience,
1403
1365
  returnTo: authRedirect.returnTo,
1404
1366
  requestOrigin: authRedirect.requestOrigin,
1405
1367
  })
@@ -1410,7 +1372,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1410
1372
  },
1411
1373
  {
1412
1374
  body: t.Object({
1413
- audience: t.Optional(t.String()),
1414
1375
  email: t.Optional(t.String()),
1415
1376
  returnTo: t.Optional(t.String()),
1416
1377
  }),
@@ -1424,7 +1385,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1424
1385
  const strategy = host.auth.getStrategy()
1425
1386
  const url = new URL(request.url)
1426
1387
  const authRedirect = resolveAuthRedirectContext(options, request, {
1427
- audience: url.searchParams.get("audience"),
1428
1388
  returnTo: url.searchParams.get("returnTo"),
1429
1389
  })
1430
1390
  if (authRedirect instanceof Response) {
@@ -1439,7 +1399,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1439
1399
  const result = await strategy.startOidcSignIn({
1440
1400
  projectId: host.id,
1441
1401
  authStorage: requireAuthStorage(host),
1442
- audience: authRedirect.audience,
1443
1402
  returnTo: authRedirect.returnTo,
1444
1403
  requestOrigin: authRedirect.requestOrigin,
1445
1404
  })
@@ -1459,11 +1418,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1459
1418
  const url = new URL(request.url)
1460
1419
  const magicLinkId = url.searchParams.get("magicLinkId")?.trim()
1461
1420
  const token = url.searchParams.get("token")?.trim()
1462
- const authRedirectHint = resolveAuthExperienceRedirectHint(
1463
- options,
1464
- request,
1465
- url.searchParams.get("audience")
1466
- )
1421
+ const authRedirectHint = resolveAuthExperienceRedirectHint(options, request)
1467
1422
 
1468
1423
  if (!magicLinkId || !token) {
1469
1424
  return await invalidMagicLinkResponse(options, authRedirectHint)
@@ -1539,7 +1494,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1539
1494
  "[SixbServer] OIDC callback reached a browser that did not start this sign-in."
1540
1495
  )
1541
1496
  }
1542
- const authOptions = resolveAuthOptions(options, request)
1543
1497
  const result = await strategy.completeOidcSignIn({
1544
1498
  projectId: host.id,
1545
1499
  authStorage: requireAuthStorage(host),
@@ -1547,7 +1501,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1547
1501
  requestOrigin: options.resolveAuthRequestOrigin(request),
1548
1502
  session: {
1549
1503
  id: sessionCredential.sessionId,
1550
- audience: authOptions.audience,
1551
1504
  tokenHash: sessionCredential.tokenHash,
1552
1505
  createdAt: now,
1553
1506
  ...host.auth.createSessionDeadlines(now),
@@ -1560,7 +1513,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1560
1513
  request,
1561
1514
  apiOrigin: options.resolveAuthRequestOrigin(request),
1562
1515
  sessionCredential,
1563
- audience: result.session.audience,
1564
1516
  expiresAt: result.session.expiresAt,
1565
1517
  returnTo: result.returnTo,
1566
1518
  })
@@ -1602,7 +1554,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1602
1554
 
1603
1555
  const magicLinkId = body.magicLinkId?.trim()
1604
1556
  const token = body.token?.trim()
1605
- const authRedirectHint = resolveAuthExperienceRedirectHint(options, request, body.audience)
1557
+ const authRedirectHint = resolveAuthExperienceRedirectHint(options, request)
1606
1558
  if (!magicLinkId || !token) {
1607
1559
  return await invalidMagicLinkResponse(options, authRedirectHint)
1608
1560
  }
@@ -1621,7 +1573,6 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
1621
1573
  body: t.Object({
1622
1574
  magicLinkId: t.Optional(t.String()),
1623
1575
  token: t.Optional(t.String()),
1624
- audience: t.Optional(t.String()),
1625
1576
  }),
1626
1577
  parse: "urlencoded",
1627
1578
  detail: { hide: true },
@@ -1727,7 +1678,6 @@ async function completeMagicLinkCallback(input: {
1727
1678
  readonly clearPendingCookie?: boolean
1728
1679
  readonly authRedirectHint?: AuthRedirectContext
1729
1680
  }): Promise<Response> {
1730
- const authOptions = resolveAuthOptions(input.options, input.request)
1731
1681
  const now = new Date()
1732
1682
  const sessionCredential = createSessionCredential()
1733
1683
  const device = resolveSessionDevice(input.request)
@@ -1740,7 +1690,6 @@ async function completeMagicLinkCallback(input: {
1740
1690
  token: input.token,
1741
1691
  session: {
1742
1692
  id: sessionCredential.sessionId,
1743
- audience: authOptions.audience,
1744
1693
  tokenHash: sessionCredential.tokenHash,
1745
1694
  createdAt: now,
1746
1695
  ...input.host.auth.createSessionDeadlines(now),
@@ -1753,7 +1702,6 @@ async function completeMagicLinkCallback(input: {
1753
1702
  request: input.request,
1754
1703
  apiOrigin: input.options.resolveAuthRequestOrigin(input.request),
1755
1704
  sessionCredential,
1756
- audience: result.session.audience,
1757
1705
  expiresAt: result.session.expiresAt,
1758
1706
  returnTo: result.returnTo,
1759
1707
  })
@@ -1838,11 +1786,10 @@ function sessionCallbackCompletionResponse(input: {
1838
1786
  readonly request: Request
1839
1787
  readonly apiOrigin: string
1840
1788
  readonly sessionCredential: ReturnType<typeof createSessionCredential>
1841
- readonly audience: AuthSessionAudience
1842
1789
  readonly expiresAt: Date
1843
1790
  readonly returnTo: string
1844
1791
  }): Response {
1845
- const cookieOptions = input.host.auth.getCookieOptions({ audience: input.audience })
1792
+ const cookieOptions = input.host.auth.getCookieOptions()
1846
1793
  const headers = new Headers({
1847
1794
  "cache-control": "no-store",
1848
1795
  })
@@ -1928,13 +1875,6 @@ function authCallbackCompletionDocumentResponse(returnTo: string, headers: Heade
1928
1875
  )
1929
1876
  }
1930
1877
 
1931
- function resolveAuthOptions(
1932
- options: AuthRoutesOptions,
1933
- request: Request
1934
- ): ReturnType<ResolveRequestAuthContext> {
1935
- return options.resolveAuthContext(request)
1936
- }
1937
-
1938
1878
  function resolveAuthRedirectContext(
1939
1879
  options: AuthRoutesOptions,
1940
1880
  request: Request,
@@ -1960,7 +1900,7 @@ function resolveInvitationDeliveryContext(
1960
1900
  return options.resolveInvitationRedirectContext(request, input)
1961
1901
  } catch (error) {
1962
1902
  if (error instanceof BrowserOriginError) {
1963
- return jsonResponse({ error: "Invitation destination is not allowed" }, 400)
1903
+ return jsonResponse({ error: "Invitation return target is not allowed" }, 400)
1964
1904
  }
1965
1905
 
1966
1906
  throw error
@@ -1990,13 +1930,10 @@ function resolveSessionCsrfToken(input: {
1990
1930
  }
1991
1931
 
1992
1932
  function authSessionBody(
1993
- host: SixbHostView,
1994
- session: AuthenticatedAuthSession,
1995
- audience: AuthSessionAudience
1933
+ session: AuthenticatedAuthSession
1996
1934
  ): Omit<AuthenticatedAuthSessionResponse, "csrfToken"> {
1997
1935
  return {
1998
1936
  authenticated: true,
1999
- applicationAccess: { allowed: sessionCanAccessApplication(host, session, audience), audience },
2000
1937
  user: {
2001
1938
  id: session.user.id,
2002
1939
  email: session.user.email,
@@ -2045,7 +1982,7 @@ function strategyNotImplementedResponse(message: string): Response {
2045
1982
  })
2046
1983
  }
2047
1984
 
2048
- // Mirrors the Atlas design tokens (packages/ui/src/styles/globals.css) so the
1985
+ // Mirrors the app's design tokens (packages/ui/src/styles/globals.css) so the
2049
1986
  // server-rendered auth pages feel familiar. Self-contained: no external CSS, and dark mode
2050
1987
  // follows the system preference since these standalone pages have no client theme runtime.
2051
1988
  const AUTH_PAGE_STYLE = `<style>
@@ -2204,13 +2141,12 @@ async function signInFormResponse(
2204
2141
  ): Promise<Response> {
2205
2142
  return (
2206
2143
  (await customAuthExperienceResponse(options.authExperience, {
2207
- audience: context.audience,
2208
2144
  state: { kind: "signIn" },
2209
2145
  signInUrl: authExperienceSignInUrl(context),
2210
2146
  submission: {
2211
2147
  kind: "requestMagicLink",
2212
2148
  action: "/auth/sign-in",
2213
- fields: { audience: context.audience, returnTo: context.returnTo },
2149
+ fields: { returnTo: context.returnTo },
2214
2150
  },
2215
2151
  // Native POST forms inherit the document's referrer policy when
2216
2152
  // constructing their Origin header. `no-referrer` produces
@@ -2222,7 +2158,6 @@ async function signInFormResponse(
2222
2158
  "<h1>Sign in</h1>",
2223
2159
  "<p>We'll email you a sign-in link.</p>",
2224
2160
  '<form method="post" action="/auth/sign-in">',
2225
- `<input type="hidden" name="audience" value="${escapeHtml(context.audience)}">`,
2226
2161
  `<input type="hidden" name="returnTo" value="${escapeHtml(context.returnTo)}">`,
2227
2162
  '<input name="email" type="email" autocomplete="email" placeholder="you@example.com" aria-label="Email address" required autofocus>',
2228
2163
  '<button type="submit">Send sign-in link</button>',
@@ -2238,7 +2173,6 @@ async function magicLinkRequestedResponse(
2238
2173
  ): Promise<Response> {
2239
2174
  return (
2240
2175
  (await customAuthExperienceResponse(options.authExperience, {
2241
- audience: context.audience,
2242
2176
  state: { kind: "checkEmail" },
2243
2177
  signInUrl: authExperienceSignInUrl(context),
2244
2178
  })) ??
@@ -2256,7 +2190,6 @@ async function invalidMagicLinkResponse(
2256
2190
  ): Promise<Response> {
2257
2191
  if (context) {
2258
2192
  const custom = await customAuthExperienceResponse(options.authExperience, {
2259
- audience: context.audience,
2260
2193
  state: { kind: "invalidLink" },
2261
2194
  signInUrl: authExperienceSignInUrl(context),
2262
2195
  status: 400,
@@ -2273,7 +2206,6 @@ async function authExperienceErrorResponse(
2273
2206
  ): Promise<Response> {
2274
2207
  if (context) {
2275
2208
  const custom = await customAuthExperienceResponse(options.authExperience, {
2276
- audience: context.audience,
2277
2209
  state: { kind: "error" },
2278
2210
  signInUrl: authExperienceSignInUrl(context),
2279
2211
  status: 500,
@@ -2285,20 +2217,16 @@ async function authExperienceErrorResponse(
2285
2217
  }
2286
2218
 
2287
2219
  function authExperienceSignInUrl(context: AuthRedirectContext): string {
2288
- const params = new URLSearchParams({ audience: context.audience, returnTo: context.returnTo })
2220
+ const params = new URLSearchParams({ returnTo: context.returnTo })
2289
2221
  return `/auth/sign-in?${params.toString()}`
2290
2222
  }
2291
2223
 
2224
+ // A magic link's own page carries no return target, so the app's custom pages get the app's home.
2292
2225
  function resolveAuthExperienceRedirectHint(
2293
2226
  options: AuthRoutesOptions,
2294
- request: Request,
2295
- audience: string | null | undefined
2227
+ request: Request
2296
2228
  ): AuthRedirectContext | undefined {
2297
- if (audience !== "app" && audience !== "atlas") {
2298
- return undefined
2299
- }
2300
-
2301
- const context = resolveInvitationDeliveryContext(options, request, { destinationId: audience })
2229
+ const context = resolveInvitationDeliveryContext(options, request, {})
2302
2230
  return context instanceof Response ? undefined : context
2303
2231
  }
2304
2232
 
@@ -2322,7 +2250,6 @@ async function magicLinkConfirmResponse(
2322
2250
  ): Promise<Response> {
2323
2251
  if (input.authRedirect) {
2324
2252
  const custom = await customAuthExperienceResponse(options.authExperience, {
2325
- audience: input.authRedirect.audience,
2326
2253
  state: { kind: "confirm", ...(input.email ? { email: input.email } : {}) },
2327
2254
  signInUrl: authExperienceSignInUrl(input.authRedirect),
2328
2255
  submission: {
@@ -2331,7 +2258,6 @@ async function magicLinkConfirmResponse(
2331
2258
  fields: {
2332
2259
  magicLinkId: input.magicLinkId,
2333
2260
  token: input.token,
2334
- audience: input.authRedirect.audience,
2335
2261
  },
2336
2262
  },
2337
2263
  referrerPolicy: "same-origin",
@@ -2352,11 +2278,6 @@ async function magicLinkConfirmResponse(
2352
2278
  '<form method="post" action="/auth/callback" id="confirm">',
2353
2279
  `<input type="hidden" name="magicLinkId" value="${escapeHtml(input.magicLinkId)}">`,
2354
2280
  `<input type="hidden" name="token" value="${escapeHtml(input.token)}">`,
2355
- ...(input.authRedirect
2356
- ? [
2357
- `<input type="hidden" name="audience" value="${escapeHtml(input.authRedirect.audience)}">`,
2358
- ]
2359
- : []),
2360
2281
  '<button type="submit" id="confirm-button">Continue</button>',
2361
2282
  "</form>",
2362
2283
  "<script>",
@@ -36,9 +36,7 @@ async function fileDownloadResponse(
36
36
  blobStorage: host.blobStorage,
37
37
  fileRef,
38
38
  head: options.head,
39
- rangeHeader: request.headers.get("range"),
40
- ifNoneMatchHeader: request.headers.get("if-none-match"),
41
- ifRangeHeader: request.headers.get("if-range"),
39
+ request,
42
40
  }))
43
41
  if (!response) {
44
42
  set.status = 404
@@ -156,8 +156,9 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
156
156
  (!filter.types?.length || filter.types.includes(event.type)) &&
157
157
  eventMatchesScope(event, filter),
158
158
  // The execution facade owns per-event visibility: it withholds events the caller
159
- // cannot view and omits marked properties the caller is not cleared for.
160
- view: (event) => sixb.events.readable(event),
159
+ // cannot view and omits marked properties the caller is not cleared for, as markings
160
+ // are when each batch is sent.
161
+ view: (batch) => sixb.events.visible(batch),
161
162
  replay: replayFrom
162
163
  ? {
163
164
  afterCursor: replayFrom.afterCursor,
@@ -30,10 +30,13 @@ export interface HubSource<T extends HubRecord> {
30
30
  export interface HubSubscription<T extends HubRecord> {
31
31
  matches(record: T): boolean
32
32
  /**
33
- * The matching record as this socket may read it, or `undefined` to withhold it. It keeps the
34
- * record's cursor. Omit to deliver matching records unchanged.
33
+ * The matching records of one outgoing batch as this socket may read them, in order: withheld
34
+ * records are left out, the others keep their cursor. It is applied as the batch is sent, so it
35
+ * reflects what the socket may read then, and may wait for what decides it. A batch it fails on
36
+ * is never sent unviewed: the stream closes there, so the client resumes from the last cursor it
37
+ * received. Omit to deliver matching records unchanged.
35
38
  */
36
- view?(record: T): T | undefined
39
+ view?(records: readonly T[]): readonly T[] | Promise<readonly T[]>
37
40
  /** Replays retained records after `afterCursor` (or from the oldest) before live ones. */
38
41
  readonly replay?: {
39
42
  readonly afterCursor: string | undefined
@@ -56,6 +59,8 @@ interface ClientState<T extends HubRecord> {
56
59
  queuedBytes: number
57
60
  pendingBytes: number
58
61
  flushTimer: ReturnType<typeof setTimeout> | null
62
+ /** A batch is waiting for its view: the next one is sent after it, never before. */
63
+ sending: boolean
59
64
  readonly queueProgressWaiters: Set<() => void>
60
65
  catchingUp: boolean
61
66
  closed: boolean
@@ -108,6 +113,7 @@ export class SubscriptionHub<T extends HubRecord> {
108
113
  queuedBytes: 0,
109
114
  pendingBytes: 0,
110
115
  flushTimer: null,
116
+ sending: false,
111
117
  queueProgressWaiters: new Set(),
112
118
  catchingUp: subscription.replay !== undefined,
113
119
  closed: false,
@@ -179,10 +185,9 @@ export class SubscriptionHub<T extends HubRecord> {
179
185
  private deliverLive(records: readonly T[]): void {
180
186
  for (const state of this.clients.values()) {
181
187
  for (const record of records) {
182
- const visible = visibleRecord(state.subscription, record)
183
- if (!visible) continue
184
- if (state.catchingUp) this.enqueuePendingLive(state, visible)
185
- else this.enqueue(state, visible)
188
+ if (!state.subscription.matches(record)) continue
189
+ if (state.catchingUp) this.enqueuePendingLive(state, record)
190
+ else this.enqueue(state, record)
186
191
  }
187
192
  }
188
193
  }
@@ -203,9 +208,8 @@ export class SubscriptionHub<T extends HubRecord> {
203
208
  if (state.pendingCursors.has(record.cursor)) {
204
209
  state.replayedPendingCursors.add(record.cursor)
205
210
  }
206
- const visible = visibleRecord(state.subscription, record)
207
- if (!visible) continue
208
- if (!(await this.enqueueReplay(state, visible))) return
211
+ if (!state.subscription.matches(record)) continue
212
+ if (!(await this.enqueueReplay(state, record))) return
209
213
  }
210
214
  if (state.closed) return
211
215
  afterCursor = page.cursor ?? afterCursor
@@ -311,7 +315,7 @@ export class SubscriptionHub<T extends HubRecord> {
311
315
  }
312
316
 
313
317
  private flush(state: ClientState<T>): void {
314
- if (state.closed || state.queue.length === 0) return
318
+ if (state.closed || state.sending || state.queue.length === 0) return
315
319
  if (socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
316
320
  this.scheduleFlush(state)
317
321
  return
@@ -323,8 +327,45 @@ export class SubscriptionHub<T extends HubRecord> {
323
327
  state.queuedBytes -= encodedBytes(record)
324
328
  }
325
329
  this.notifyQueueProgress(state)
330
+
331
+ const view = state.subscription.view
332
+ if (!view) {
333
+ this.sendFrames(state, records)
334
+ if (state.queue.length > 0) this.scheduleFlush(state)
335
+ return
336
+ }
337
+
338
+ state.sending = true
339
+ void this.sendViewed(state, records, view).finally(() => {
340
+ state.sending = false
341
+ if (state.queue.length > 0) this.scheduleFlush(state)
342
+ })
343
+ }
344
+
345
+ private async sendViewed(
346
+ state: ClientState<T>,
347
+ records: readonly T[],
348
+ view: NonNullable<HubSubscription<T>["view"]>
349
+ ): Promise<void> {
350
+ let visible: readonly T[]
351
+ try {
352
+ visible = await view(records)
353
+ } catch (error) {
354
+ // Fail closed, and close: a client resumes after the last cursor it received, so delivering
355
+ // later batches would skip this one for good. Reconnecting reads it again.
356
+ const message = error instanceof Error ? error.message : String(error)
357
+ console.warn(
358
+ `[SixbServer] Closed a stream subscription at cursor '${records[0]?.cursor}': ${records.length} record(s) could not be viewed: ${message}`
359
+ )
360
+ this.fail(state, "Stream records could not be viewed; reconnect from the last cursor.", 1011)
361
+ return
362
+ }
363
+
364
+ if (visible.length > 0) this.sendFrames(state, visible)
365
+ }
366
+
367
+ private sendFrames(state: ClientState<T>, records: readonly T[]): void {
326
368
  for (const frame of this.source.frames(records)) this.send(state, frame)
327
- if (state.queue.length > 0) this.scheduleFlush(state)
328
369
  }
329
370
 
330
371
  private waitForQueueProgress(state: ClientState<T>): Promise<void> {
@@ -367,14 +408,6 @@ export class SubscriptionHub<T extends HubRecord> {
367
408
  }
368
409
  }
369
410
 
370
- function visibleRecord<T extends HubRecord>(
371
- subscription: HubSubscription<T>,
372
- record: T
373
- ): T | undefined {
374
- if (!subscription.matches(record)) return undefined
375
- return subscription.view ? subscription.view(record) : record
376
- }
377
-
378
411
  function hasQueueCapacity(state: ClientState<HubRecord>, bytes: number): boolean {
379
412
  return (
380
413
  state.queue.length + state.pendingLive.length < MAX_CLIENT_RECORDS &&