@open-pencil/cloud 0.16.0 → 0.17.0-beta.2

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 (54) hide show
  1. package/dist/listen.mjs +236 -117
  2. package/dist/listen.mjs.map +1 -1
  3. package/dist/runtime-node.d.mts +2937 -703
  4. package/dist/runtime-node.d.mts.map +1 -1
  5. package/dist/server.d.ts +3985 -941
  6. package/dist/server.d.ts.map +1 -1
  7. package/dist/server.js +833 -376
  8. package/dist/server.js.map +1 -1
  9. package/dist/server.mjs +665 -271
  10. package/dist/server.mjs.map +1 -1
  11. package/package.json +4 -2
  12. package/src/admin/api/routes.ts +44 -33
  13. package/src/runtime/cloudflare/environment.ts +16 -2
  14. package/src/runtime/cloudflare/relay/index.ts +128 -0
  15. package/src/runtime/cloudflare/relay/sockets.ts +112 -0
  16. package/src/runtime/cloudflare/worker.ts +56 -19
  17. package/src/runtime/node/bootstrap.ts +9 -4
  18. package/src/runtime/node/server.ts +40 -8
  19. package/src/runtime/posthog/errors.ts +48 -0
  20. package/src/runtime/s3/objects.ts +26 -0
  21. package/src/runtime/site/server.ts +5 -3
  22. package/src/server/api/router.ts +3 -2
  23. package/src/server/api/types.ts +7 -0
  24. package/src/server/app.ts +4 -0
  25. package/src/server/cleanup/objects.ts +10 -2
  26. package/src/server/cleanup/revisions.ts +8 -2
  27. package/src/server/collaboration/index.ts +3 -0
  28. package/src/server/collaboration/relay/connection.ts +195 -0
  29. package/src/server/collaboration/relay/hub.ts +199 -187
  30. package/src/server/collaboration/relay/index.ts +3 -1
  31. package/src/server/collaboration/relay/log.ts +76 -0
  32. package/src/server/collaboration/relay/persistence.ts +147 -22
  33. package/src/server/collaboration/relay/resume.ts +28 -0
  34. package/src/server/collaboration/routes.ts +27 -16
  35. package/src/server/config/deployment.ts +11 -0
  36. package/src/server/config/schema.ts +24 -2
  37. package/src/server/db/migrate.ts +3 -1
  38. package/src/server/db/migrations/028_collaboration_updates.ts +84 -0
  39. package/src/server/db/tables.ts +18 -4
  40. package/src/server/documents/routes.ts +106 -80
  41. package/src/server/documents/versions/routes.ts +36 -25
  42. package/src/server/documents/versions/service.ts +9 -0
  43. package/src/server/errors/index.ts +56 -0
  44. package/src/server/index.ts +11 -0
  45. package/src/server/internal/routes.ts +3 -8
  46. package/src/server/objects/types.ts +12 -0
  47. package/src/server/pictures/routes.ts +4 -2
  48. package/src/server/rate-limit/index.ts +4 -1
  49. package/src/server/rate-limit/middleware.ts +99 -11
  50. package/src/server/sharing/document-routes.ts +30 -22
  51. package/src/server/sharing/routes.ts +122 -83
  52. package/src/server/validation.ts +58 -2
  53. package/src/server/workspaces/routes.ts +63 -45
  54. package/src/runtime/cloudflare/relay.ts +0 -136
package/dist/server.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { CLOUD_AI_JWKS_PATH, CLOUD_AI_TOKEN_AUDIENCE, CLOUD_DISCOVERY_PATH, CLOUD_PROTOCOL_MAX_UPLOAD_BYTES, COLLAB_ACTIONS, CloudConfigError, MAX_COLLAB_MESSAGE_BYTES, RELAY_CLOSE, RELAY_DOCUMENT_PARAM, RELAY_SERVER_PEER, cloudDeploymentConfigSchema, createRelayClientAssembler, decodeRelayClientFrame, decodeRoomUpdate, defaultRelayURL, documentDownloadSchema, documentPermissionSchema, documentSummarySchema, encodeSyncReply, listen, migrateCloudDatabase, parseCloudDiscovery, parseCloudServerConfig, parseCommitImageUploads, parseCommitUpload, parseCreateAIToken, parseCreateDocument, parseCreateUpload, parseRequestImageLinks, parseRequestImageUploads, presenceName, relayServerMessages, requestImageLinksSchema, staticEntitlementsSchema, validatedJSON, validatedQuery } from "./listen.mjs";
1
+ import { CLOUD_AI_JWKS_PATH, CLOUD_AI_TOKEN_AUDIENCE, CLOUD_DISCOVERY_PATH, CLOUD_PROTOCOL_MAX_UPLOAD_BYTES, COLLAB_ACTIONS, CloudConfigError, MAX_COLLAB_MESSAGE_BYTES, RELAY_CLOSE, RELAY_DOCUMENT_PARAM, RELAY_SERVER_PEER, cloudDeploymentConfigSchema, createRelayClientAssembler, decodeRelayClientFrame, decodeRoomUpdate, defaultRelayURL, documentDownloadSchema, documentInvitationParams, documentParams, documentPermissionSchema, documentShareParams, documentSummarySchema, documentUserParams, documentVersionParams, encodeSyncReply, idParams, invitationContinuationParams, invitationParams, listen, migrateCloudDatabase, parseCloudDiscovery, parseCloudServerConfig, parseCommitImageUploads, parseCommitUpload, parseCreateAIToken, parseCreateDocument, parseCreateUpload, parseRequestImageLinks, parseRequestImageUploads, pictureParams, presenceName, relayServerMessages, requestImageLinksSchema, shareParams, staticEntitlementsSchema, uploadParams, validatedJSON, validatedQuery, workspaceInvitationParams, workspaceMemberParams, workspaceParams } from "./listen.mjs";
2
2
  import { readFile } from "node:fs/promises";
3
- import { isValid, toUint8Array } from "js-base64";
3
+ import { fromUint8Array, isValid, toUint8Array } from "js-base64";
4
4
  import * as v from "valibot";
5
5
  import { clamp, compact, countBy, groupBy, isUndefined, keyBy, limitAsync, maxBy, omitBy, sumBy, uniq, uniqBy, zipObject } from "es-toolkit";
6
6
  import * as decoding from "lib0/decoding";
@@ -15,7 +15,8 @@ import { parse } from "smol-toml";
15
15
  import { CamelCasePlugin, Kysely, NoResultError, PostgresDialect } from "kysely";
16
16
  import { Pool } from "pg";
17
17
  import { rateLimiter } from "hono-rate-limiter";
18
- import { matchedRoutes } from "hono/route";
18
+ import { every } from "hono/combine";
19
+ import { matchedRoutes, routePath } from "hono/route";
19
20
  import { sha256 } from "@noble/hashes/sha2.js";
20
21
  import { bytesToHex, hexToBytes, utf8ToBytes } from "@noble/hashes/utils.js";
21
22
  import { pickBy } from "es-toolkit/object";
@@ -30,6 +31,7 @@ import { admin, bearer, captcha, deviceAuthorization, haveIBeenPwned, twoFactor
30
31
  import { randomHex } from "@open-pencil/scene-graph/random";
31
32
  import { hkdf } from "@noble/hashes/hkdf.js";
32
33
  import { DefaultLogger, TypedInMemoryProvider } from "@openfeature/server-sdk";
34
+ import { HTTPException } from "hono/http-exception";
33
35
  import { bearerAuth } from "hono/bearer-auth";
34
36
  import { render } from "@vue-email/render";
35
37
  import { defineComponent, h } from "vue";
@@ -38,6 +40,7 @@ import { AbortMultipartUploadCommand, CompleteMultipartUploadCommand, CreateMult
38
40
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
39
41
  import nodemailer from "nodemailer";
40
42
  import { WebSocketServer } from "ws";
43
+ import { PostHog } from "posthog-node";
41
44
  import { resolve, sep } from "node:path";
42
45
  import { getMimeType } from "hono/utils/mime";
43
46
  //#region src/contract/account.ts
@@ -732,9 +735,9 @@ function requireTrustedMutationOrigin(trustedOrigins) {
732
735
  }
733
736
  //#endregion
734
737
  //#region src/admin/schema.ts
735
- const uuidSchema$2 = v.pipe(v.string(), v.uuid());
738
+ const uuidSchema$1 = v.pipe(v.string(), v.uuid());
736
739
  /** Accounts and access requests are named by UUID; anything else names no row. */
737
- const isUUID = (value) => v.is(uuidSchema$2, value);
740
+ const isUUID = (value) => v.is(uuidSchema$1, value);
738
741
  const enrollmentStatusSchema = v.picklist([
739
742
  "pending",
740
743
  "approved",
@@ -831,12 +834,14 @@ const CLOUD_RATE_LIMITS = {
831
834
  collaborationTicket: {
832
835
  namespace: "collaboration-ticket",
833
836
  windowMs: 6e4,
834
- limit: 60
837
+ limit: 60,
838
+ actorLimit: 300
835
839
  },
836
840
  invitationMutation: {
837
841
  namespace: "invitation-mutation",
838
842
  windowMs: 6e4,
839
- limit: 30
843
+ limit: 30,
844
+ actorLimit: 60
840
845
  },
841
846
  workspaceCreation: {
842
847
  namespace: "workspace-creation",
@@ -846,12 +851,14 @@ const CLOUD_RATE_LIMITS = {
846
851
  documentCreation: {
847
852
  namespace: "document-creation",
848
853
  windowMs: 6e4,
849
- limit: 60
854
+ limit: 60,
855
+ actorLimit: 120
850
856
  },
851
857
  uploadCreation: {
852
858
  namespace: "upload-creation",
853
859
  windowMs: 6e4,
854
- limit: 120
860
+ limit: 120,
861
+ actorLimit: 600
855
862
  },
856
863
  authenticatedMutation: {
857
864
  namespace: "authenticated-mutation",
@@ -923,11 +930,58 @@ function createActorRateLimiter(database, secret, policy, skip, resource) {
923
930
  });
924
931
  }
925
932
  /**
926
- * A limiter a route mounts for its own budget, such as document creation; general limiters leave
933
+ * A limiter a route mounts for its own budget, such as workspace creation; general limiters leave
927
934
  * the requests it counts to it.
928
935
  */
929
- function createRouteRateLimiter(database, secret, policy, skip, resource) {
930
- return ownBudget(createActorRateLimiter(database, secret, policy, skip, resource), skip);
936
+ function createRouteRateLimiter(database, secret, policy, skip) {
937
+ return ownBudget(createActorRateLimiter(database, secret, policy, skip), skip);
938
+ }
939
+ /**
940
+ * A route's own budget counted per resource, such as uploads per document, and capped per actor
941
+ * across resources by `actorLimit`; general limiters leave the requests it counts to it.
942
+ */
943
+ function createResourceRateLimiter(database, secret, policy, resource, skip) {
944
+ const perResource = createActorRateLimiter(database, secret, policy, skip, resource);
945
+ const perActor = createActorRateLimiter(database, secret, {
946
+ namespace: `${policy.namespace}-actor`,
947
+ windowMs: policy.windowMs,
948
+ limit: policy.actorLimit
949
+ }, skip);
950
+ return ownBudget(every(perResource, keepResourceUse, perActor, reportTighterBudget(policy.windowMs)), skip);
951
+ }
952
+ /** Keeps the resource budget's use, which the actor-wide limiter would record over. */
953
+ const keepResourceUse = async (context, next) => {
954
+ context.set("resourceRateLimit", context.get("rateLimit"));
955
+ await next();
956
+ };
957
+ /**
958
+ * Reports whichever budget has fewer requests left in the `RateLimit` headers, as both limiters
959
+ * write them and the last would otherwise win. A refused request keeps the headers of the budget
960
+ * that refused it.
961
+ */
962
+ function reportTighterBudget(windowMs) {
963
+ return async (context, next) => {
964
+ const resource = context.get("resourceRateLimit");
965
+ const actor = context.get("rateLimit");
966
+ const tighter = resource && actor && resource.remaining < actor.remaining ? resource : actor;
967
+ if (tighter) setBudgetHeaders(context, tighter, windowMs);
968
+ await next();
969
+ };
970
+ }
971
+ /** The draft-7 `RateLimit` headers `hono-rate-limiter` writes, for one budget's use. */
972
+ function setBudgetHeaders(context, use, windowMs) {
973
+ const windowSeconds = Math.ceil(windowMs / 1e3);
974
+ const reset = use.resetTime ? Math.max(0, Math.ceil((use.resetTime.getTime() - Date.now()) / 1e3)) : windowSeconds;
975
+ context.header("RateLimit-Policy", `${use.limit};w=${windowSeconds}`);
976
+ context.header("RateLimit", `limit=${use.limit}, remaining=${use.remaining}, reset=${reset}`);
977
+ }
978
+ /**
979
+ * A limiter's resource named by a route parameter, such as the document a request uploads to. It
980
+ * runs before the route's param validator, and the value only names the limiter's counter, which
981
+ * `rateLimitKey` hashes; it never reaches a query.
982
+ */
983
+ function routeParamResource(name) {
984
+ return (context) => context.req.param(name)?.toLowerCase() ?? "unknown";
931
985
  }
932
986
  //#endregion
933
987
  //#region src/admin/api/routes.ts
@@ -972,9 +1026,8 @@ function createCloudAdminRoutes(services, trustedOrigins, security) {
972
1026
  if (error instanceof v.ValiError) return context.json({ error: { code: "invalid_request" } }, 400);
973
1027
  if (error instanceof NoResultError) return context.json({ error: { code: "not_found" } }, 404);
974
1028
  throw error;
975
- }).get("/enrollments", validatedQuery(parseEnrollmentQuery), async (context) => context.json(await services.enrollment.list(context.req.valid("query")))).post("/enrollments/:id/approve", validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.param("id"), "approved", context.req.valid("json")) })).post("/enrollments/:id/reject", validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.param("id"), "rejected", context.req.valid("json")) })).post("/enrollments/:id/revoke", validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.param("id"), "revoked", context.req.valid("json")) })).get("/users", validatedQuery(parseUserQuery), async (context) => context.json(await services.users.list(context.req.valid("query")))).get("/users/:id", async (context) => {
976
- const id = context.req.param("id");
977
- const detail = v.is(accountIdSchema, id) ? await services.users.detail(id) : null;
1029
+ }).get("/enrollments", validatedQuery(parseEnrollmentQuery), async (context) => context.json(await services.enrollment.list(context.req.valid("query")))).post("/enrollments/:id/approve", idParams, validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.valid("param").id, "approved", context.req.valid("json")) })).post("/enrollments/:id/reject", idParams, validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.valid("param").id, "rejected", context.req.valid("json")) })).post("/enrollments/:id/revoke", idParams, validatedJSON(parseEnrollmentReview), async (context) => context.json({ enrollment: await services.enrollment.review(context.get("actor").userId, context.req.valid("param").id, "revoked", context.req.valid("json")) })).get("/users", validatedQuery(parseUserQuery), async (context) => context.json(await services.users.list(context.req.valid("query")))).get("/users/:id", idParams, async (context) => {
1030
+ const detail = await services.users.detail(context.req.valid("param").id);
978
1031
  if (!detail) return context.json({ error: { code: "not_found" } }, 404);
979
1032
  return context.json(detail);
980
1033
  }).post("/users/reset-two-step", validatedJSON(parseUserMutation), (context) => accountMutation(context, context.req.valid("json").userId, (actorId, userId) => services.users.resetTwoStep(actorId, userId))).post("/users/remove-picture", validatedJSON(parseUserMutation), (context) => accountMutation(context, context.req.valid("json").userId, (actorId, userId) => services.users.removePicture(actorId, userId))).post("/users/ban", validatedJSON(parseUserMutation), async (context) => {
@@ -993,8 +1046,8 @@ function createCloudAdminRoutes(services, trustedOrigins, security) {
993
1046
  const input = context.req.valid("json");
994
1047
  await services.users.setAdmin(context.req.raw.headers, context.get("actor").userId, input.userId, input.enabled);
995
1048
  return context.json({ ok: true });
996
- }).get("/email", validatedQuery(parseEmailQuery), async (context) => context.json(await services.email.list(context.req.valid("query")))).post("/email/:id/regenerate", async (context) => {
997
- await services.email.regenerate(context.get("actor").userId, context.req.param("id"));
1049
+ }).get("/email", validatedQuery(parseEmailQuery), async (context) => context.json(await services.email.list(context.req.valid("query")))).post("/email/:id/regenerate", idParams, async (context) => {
1050
+ await services.email.regenerate(context.get("actor").userId, context.req.valid("param").id);
998
1051
  return context.json({ ok: true });
999
1052
  }).get("/audit", validatedQuery(parseAuditQuery), async (context) => context.json(await services.audit.list(context.req.valid("query")))).get("/operations", async (context) => context.json(await services.operations.summary()));
1000
1053
  }
@@ -1525,6 +1578,15 @@ function serviceConfig(config, environment) {
1525
1578
  internalServiceToken: config.internal_api ? requiredSecret(environment, config.internal_api.service_token) : void 0
1526
1579
  };
1527
1580
  }
1581
+ function errorTrackingConfig(config, environment) {
1582
+ const tracking = config.error_tracking;
1583
+ if (!tracking) return void 0;
1584
+ return {
1585
+ provider: tracking.provider,
1586
+ host: tracking.host,
1587
+ projectAPIKey: configuredText(environment, tracking.project_api_key)
1588
+ };
1589
+ }
1528
1590
  /** Hard limits, whatever a plan allows; each lives in the section it limits. */
1529
1591
  function technicalLimits(config) {
1530
1592
  return {
@@ -1581,6 +1643,7 @@ function parseCloudDeploymentConfig(input, environment) {
1581
1643
  ...emailConfig(config, environment),
1582
1644
  ...workerConfig(config),
1583
1645
  ...serviceConfig(config, environment),
1646
+ errorTracking: errorTrackingConfig(config, environment),
1584
1647
  technicalLimits: technicalLimits(config)
1585
1648
  });
1586
1649
  }
@@ -2246,8 +2309,9 @@ function createUserPictures(database, options) {
2246
2309
  * documents and workspaces with, and the desktop app shows them from another origin.
2247
2310
  */
2248
2311
  function createPictureRoutes(pictures) {
2249
- return new Hono().get(`${PICTURE_PATH}/:userId/:version`, async (context) => {
2250
- const picture = await pictures.read(context.req.param("userId"), context.req.param("version"));
2312
+ return new Hono().get(`${PICTURE_PATH}/:userId/:version`, pictureParams, async (context) => {
2313
+ const { userId, version } = context.req.valid("param");
2314
+ const picture = await pictures.read(userId, version);
2251
2315
  if (!picture) return context.json({ error: { code: "not_found" } }, 404);
2252
2316
  context.header("Cache-Control", "public, max-age=86400");
2253
2317
  context.header("Content-Type", `image/${picture.format}`);
@@ -3241,62 +3305,46 @@ async function documentResponse(context, respond) {
3241
3305
  function createDocumentRoutes(service, rateLimit) {
3242
3306
  const router = new Hono();
3243
3307
  if (rateLimit) {
3244
- router.use("/workspaces/:workspaceId/documents", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.documentCreation, (context) => context.req.method !== "POST", (context) => context.req.param("workspaceId") ?? "unknown"));
3245
- router.use("/documents/:documentId/images", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.uploadCreation, (context) => context.req.method !== "POST", (context) => context.req.param("documentId") ?? "unknown"));
3246
- router.use("/documents/:documentId/uploads", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.uploadCreation, (context) => context.req.method !== "POST", (context) => context.req.param("documentId") ?? "unknown"));
3308
+ router.use("/workspaces/:workspaceId/documents", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.documentCreation, routeParamResource("workspaceId"), (context) => context.req.method !== "POST"));
3309
+ router.use("/documents/:documentId/images", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.uploadCreation, routeParamResource("documentId"), (context) => context.req.method !== "POST"));
3310
+ router.use("/documents/:documentId/uploads", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.uploadCreation, routeParamResource("documentId"), (context) => context.req.method !== "POST"));
3247
3311
  }
3248
- return router.get("/shared-documents", async (context) => context.json({ documents: await service.listShared(context.get("actor").userId) })).get("/workspaces/:workspaceId/documents", async (context) => {
3249
- const documents = await service.list(context.get("actor").userId, context.req.param("workspaceId"));
3312
+ return router.get("/shared-documents", async (context) => context.json({ documents: await service.listShared(context.get("actor").userId) })).get("/workspaces/:workspaceId/documents", workspaceParams, async (context) => {
3313
+ const documents = await service.list(context.get("actor").userId, context.req.valid("param").workspaceId);
3250
3314
  if (!documents) return context.json({ error: { code: "not_found" } }, 404);
3251
3315
  return context.json({ documents });
3252
- }).get("/workspaces/:workspaceId/usage", async (context) => {
3316
+ }).get("/workspaces/:workspaceId/usage", workspaceParams, async (context) => {
3253
3317
  try {
3254
- return context.json({ usage: await service.usage(context.get("actor").userId, context.req.param("workspaceId")) });
3318
+ return context.json({ usage: await service.usage(context.get("actor").userId, context.req.valid("param").workspaceId) });
3255
3319
  } catch (error) {
3256
3320
  const response = documentDomainError(context, error);
3257
3321
  if (response) return response;
3258
3322
  throw error;
3259
3323
  }
3260
- }).post("/workspaces/:workspaceId/documents", validatedJSON(parseCreateDocument), async (context) => {
3324
+ }).post("/workspaces/:workspaceId/documents", workspaceParams, validatedJSON(parseCreateDocument), async (context) => {
3261
3325
  try {
3262
- const document = await service.create(context.get("actor").userId, context.req.param("workspaceId"), context.req.valid("json"));
3326
+ const document = await service.create(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("json"));
3263
3327
  return context.json({ document }, 201);
3264
3328
  } catch (error) {
3265
3329
  const response = documentDomainError(context, error);
3266
3330
  if (response) return response;
3267
3331
  throw error;
3268
3332
  }
3269
- }).delete("/documents/:documentId", async (context) => {
3333
+ }).delete("/documents/:documentId", documentParams, async (context) => {
3270
3334
  try {
3271
- await service.remove(context.get("actor").userId, context.req.param("documentId"));
3335
+ await service.remove(context.get("actor").userId, context.req.valid("param").documentId);
3272
3336
  return context.body(null, 204);
3273
3337
  } catch (error) {
3274
3338
  const response = documentDomainError(context, error);
3275
3339
  if (response) return response;
3276
3340
  throw error;
3277
3341
  }
3278
- }).get("/documents/:documentId/access", async (context) => {
3279
- try {
3280
- return context.json({ access: await service.access(context.get("actor").userId, context.req.param("documentId")) });
3281
- } catch (error) {
3282
- const response = documentDomainError(context, error);
3283
- if (response) return response;
3284
- throw error;
3285
- }
3286
- }).get("/documents/:documentId", async (context) => {
3287
- try {
3288
- return context.json({ document: await service.download(context.get("actor").userId, context.req.param("documentId")) });
3289
- } catch (error) {
3290
- const response = documentDomainError(context, error);
3291
- if (response) return response;
3292
- throw error;
3293
- }
3294
- }).post("/documents/:documentId/uploads", validatedJSON(parseCreateUpload), (context) => documentResponse(context, async () => context.json(await service.createUpload(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")), 201))).post("/documents/:documentId/images/commit", validatedJSON(parseCommitImageUploads), (context) => documentResponse(context, async () => {
3295
- await service.commitImageUploads(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json").uploads);
3342
+ }).get("/documents/:documentId/access", documentParams, (context) => documentResponse(context, async () => context.json({ access: await service.access(context.get("actor").userId, context.req.valid("param").documentId) }))).get("/documents/:documentId", documentParams, (context) => documentResponse(context, async () => context.json({ document: await service.download(context.get("actor").userId, context.req.valid("param").documentId) }))).post("/documents/:documentId/uploads", documentParams, validatedJSON(parseCreateUpload), (context) => documentResponse(context, async () => context.json(await service.createUpload(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")), 201))).post("/documents/:documentId/images/commit", documentParams, validatedJSON(parseCommitImageUploads), (context) => documentResponse(context, async () => {
3343
+ await service.commitImageUploads(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json").uploads);
3296
3344
  return context.body(null, 204);
3297
- })).post("/documents/:documentId/images/links", validatedJSON(parseRequestImageLinks), (context) => documentResponse(context, async () => context.json(await service.imageLinks(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json").checksums)))).post("/documents/:documentId/images", validatedJSON(parseRequestImageUploads), (context) => documentResponse(context, async () => context.json(await service.requestImageUploads(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")), 201))).post("/uploads/:uploadId/commit", validatedJSON(parseCommitUpload), async (context) => {
3345
+ })).post("/documents/:documentId/images/links", documentParams, validatedJSON(parseRequestImageLinks), (context) => documentResponse(context, async () => context.json(await service.imageLinks(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json").checksums)))).post("/documents/:documentId/images", documentParams, validatedJSON(parseRequestImageUploads), (context) => documentResponse(context, async () => context.json(await service.requestImageUploads(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")), 201))).post("/uploads/:uploadId/commit", uploadParams, validatedJSON(parseCommitUpload), async (context) => {
3298
3346
  try {
3299
- const document = await service.commitUpload(context.get("actor").userId, context.req.param("uploadId"), context.req.valid("json"));
3347
+ const document = await service.commitUpload(context.get("actor").userId, context.req.valid("param").uploadId, context.req.valid("json"));
3300
3348
  return context.json({ document });
3301
3349
  } catch (error) {
3302
3350
  const response = documentDomainError(context, error);
@@ -3308,11 +3356,15 @@ function createDocumentRoutes(service, rateLimit) {
3308
3356
  //#endregion
3309
3357
  //#region src/server/cleanup/objects.ts
3310
3358
  /**
3311
- * No revision refers to the stored file any more, and no workspace keeps it as an image, so it
3312
- * can leave the object store.
3359
+ * No revision refers to the stored file any more, no workspace keeps it as an image, and no
3360
+ * collaboration room keeps it as its snapshot, so it can leave the object store.
3313
3361
  */
3314
3362
  function unreferencedObject(expression) {
3315
- return expression.and([expression.not(expression.exists(expression.selectFrom("documentRevision").select("documentRevision.id").whereRef("documentRevision.storageObjectId", "=", "storageObject.id"))), expression.not(expression.exists(expression.selectFrom("workspaceImage").select("workspaceImage.checksum").whereRef("workspaceImage.storageObjectId", "=", "storageObject.id")))]);
3363
+ return expression.and([
3364
+ expression.not(expression.exists(expression.selectFrom("documentRevision").select("documentRevision.id").whereRef("documentRevision.storageObjectId", "=", "storageObject.id"))),
3365
+ expression.not(expression.exists(expression.selectFrom("workspaceImage").select("workspaceImage.checksum").whereRef("workspaceImage.storageObjectId", "=", "storageObject.id"))),
3366
+ expression.not(expression.exists(expression.selectFrom("documentRoomSnapshot").select("documentRoomSnapshot.documentId").whereRef("documentRoomSnapshot.snapshotObjectId", "=", "storageObject.id")))
3367
+ ]);
3316
3368
  }
3317
3369
  //#endregion
3318
3370
  //#region src/server/cleanup/documents.ts
@@ -3653,6 +3705,11 @@ async function historyStart(policy, deploymentMode, workspaceId, now) {
3653
3705
  /** How long a save between versions is kept before compaction may delete it. */
3654
3706
  const REVISION_COMPACTION_GRACE_MS = 864e5;
3655
3707
  /**
3708
+ * How long a stored file nothing refers to is left alone: a collaboration room records its next
3709
+ * snapshot's file before writing it and points the room at it moments later.
3710
+ */
3711
+ const OBJECT_SWEEP_GRACE_MS = 9e5;
3712
+ /**
3656
3713
  * Keeps a document's stored history to its versions. A save that is neither the document's
3657
3714
  * current state nor kept by a version is deleted once it is older than the grace period, and its
3658
3715
  * file with it when nothing else uses it; a workspace without full history first loses the
@@ -3708,8 +3765,8 @@ function createRevisionCompactionService(database, objects, options = {}) {
3708
3765
  });
3709
3766
  }
3710
3767
  /** Deletes freed files from the object store, then their rows; a failure stays for next time. */
3711
- async function sweepObjects(batchSize, result) {
3712
- const freed = await unreferencedObjects(database).select(["id", "objectKey"]).limit(batchSize).execute();
3768
+ async function sweepObjects(now, batchSize, result) {
3769
+ const freed = await unreferencedObjects(database).select(["id", "objectKey"]).where("createdAt", "<", /* @__PURE__ */ new Date(now.getTime() - OBJECT_SWEEP_GRACE_MS)).limit(batchSize).execute();
3713
3770
  for (const object of freed) try {
3714
3771
  await objects.delete(object.objectKey);
3715
3772
  await database.deleteFrom("storageObject").where("id", "=", object.id).where(unreferencedObject).execute();
@@ -3750,7 +3807,7 @@ function createRevisionCompactionService(database, objects, options = {}) {
3750
3807
  } catch {
3751
3808
  result.failed++;
3752
3809
  }
3753
- await sweepObjects(input.batchSize, result);
3810
+ await sweepObjects(now, input.batchSize, result);
3754
3811
  return result;
3755
3812
  } };
3756
3813
  }
@@ -3977,7 +4034,7 @@ async function writableDocument(database, options, userId, documentId) {
3977
4034
  }
3978
4035
  //#endregion
3979
4036
  //#region src/server/documents/repository.ts
3980
- const uuidSchema$1 = v.pipe(v.string(), v.uuid());
4037
+ const uuidSchema = v.pipe(v.string(), v.uuid());
3981
4038
  async function workspaceRole(database, userId, workspaceId) {
3982
4039
  return (await database.selectFrom("workspaceMember").select("role").where("workspaceId", "=", workspaceId).where("userId", "=", userId).executeTakeFirst())?.role;
3983
4040
  }
@@ -4010,7 +4067,7 @@ async function listSharedDocuments(database, userId) {
4010
4067
  "documentGrant.createdAt as sharedAt",
4011
4068
  "documentGrant.createdBy"
4012
4069
  ]).where("documentGrant.userId", "=", userId).where("documentGrant.revokedAt", "is", null).where("document.deletedAt", "is", null).where((expression) => expression.not(expression.exists(expression.selectFrom("workspaceMember").select("workspaceMember.userId").whereRef("workspaceMember.workspaceId", "=", "document.workspaceId").where("workspaceMember.userId", "=", userId)))).orderBy("document.updatedAt", "desc").execute();
4013
- const granters = uniq(rows.map((row) => row.createdBy)).filter((id) => v.is(uuidSchema$1, id));
4070
+ const granters = uniq(rows.map((row) => row.createdBy)).filter((id) => v.is(uuidSchema, id));
4014
4071
  const names = new Map(granters.length === 0 ? [] : (await database.selectFrom("user").select(["id", "name"]).where("id", "in", granters).execute()).map((user) => [user.id, user.name]));
4015
4072
  return rows.map(({ workspaceName, permission, sharedAt, createdBy, ...row }) => {
4016
4073
  const sharedByName = names.get(createdBy);
@@ -5452,15 +5509,15 @@ async function shareActor(request, resolveSession) {
5452
5509
  //#endregion
5453
5510
  //#region src/server/sharing/document-routes.ts
5454
5511
  function createPublicDocumentRoutes(sharing, documents, resolveSession) {
5455
- return new Hono().post("/shares/:shareId/document", validatedJSON(parseResolveDocumentShare), (context) => sharingRoute(context, async () => {
5456
- const resolution = await sharing.resolveShare(context.req.param("shareId"), context.req.valid("json"), await shareActor(context.req.raw, resolveSession));
5512
+ return new Hono().post("/shares/:shareId/document", shareParams, validatedJSON(parseResolveDocumentShare), (context) => sharingRoute(context, async () => {
5513
+ const resolution = await sharing.resolveShare(context.req.valid("param").shareId, context.req.valid("json"), await shareActor(context.req.raw, resolveSession));
5457
5514
  return context.json({
5458
5515
  resolution,
5459
5516
  document: await documents.downloadShared(resolution.documentId)
5460
5517
  });
5461
- })).post("/shares/:shareId/images", validatedJSON(parseRequestSharedImageLinks), (context) => sharingRoute(context, async () => {
5518
+ })).post("/shares/:shareId/images", shareParams, validatedJSON(parseRequestSharedImageLinks), (context) => sharingRoute(context, async () => {
5462
5519
  const { checksums, ...share } = context.req.valid("json");
5463
- const resolution = await sharing.resolveShare(context.req.param("shareId"), share, await shareActor(context.req.raw, resolveSession));
5520
+ const resolution = await sharing.resolveShare(context.req.valid("param").shareId, share, await shareActor(context.req.raw, resolveSession));
5464
5521
  return context.json(await documents.imageLinksShared(resolution.documentId, checksums));
5465
5522
  }));
5466
5523
  }
@@ -5627,20 +5684,20 @@ function createInvitationService(database, options) {
5627
5684
  //#region src/server/sharing/routes.ts
5628
5685
  function createDocumentSharingRoutes(service, rateLimit) {
5629
5686
  const router = new Hono();
5630
- if (rateLimit) router.use("/documents/:documentId/invitations", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, (context) => context.req.method !== "POST", (context) => context.req.param("documentId") ?? "unknown"));
5631
- return router.post("/documents/:documentId/users/lookup", validatedJSON(parseLookupCloudUser), (context) => sharingRoute(context, async () => context.json({ user: await service.lookupUser(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")) }))).get("/documents/:documentId/users/:userId", (context) => sharingRoute(context, async () => context.json({ user: await service.userProfile(context.get("actor").userId, context.req.param("documentId"), context.req.param("userId")) }))).get("/documents/:documentId/shares", (context) => sharingRoute(context, async () => context.json({ shares: await service.listShares(context.get("actor").userId, context.req.param("documentId")) }))).post("/documents/:documentId/shares", validatedJSON(parseCreateDocumentShare), (context) => sharingRoute(context, async () => context.json(await service.createShare(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")), 201))).patch("/documents/:documentId/shares/:shareId", validatedJSON(parseUpdateDocumentShare), (context) => sharingRoute(context, async () => context.json({ share: await service.updateShare(context.get("actor").userId, context.req.param("documentId"), context.req.param("shareId"), context.req.valid("json")) }))).post("/documents/:documentId/shares/:shareId/rotate", (context) => sharingRoute(context, async () => context.json(await service.rotateShare(context.get("actor").userId, context.req.param("documentId"), context.req.param("shareId"))))).delete("/documents/:documentId/shares/:shareId", (context) => sharingRoute(context, async () => {
5632
- await service.revokeShare(context.get("actor").userId, context.req.param("documentId"), context.req.param("shareId"));
5687
+ if (rateLimit) router.use("/documents/:documentId/invitations", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, routeParamResource("documentId"), (context) => context.req.method !== "POST"));
5688
+ return router.post("/documents/:documentId/users/lookup", documentParams, validatedJSON(parseLookupCloudUser), (context) => sharingRoute(context, async () => context.json({ user: await service.lookupUser(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")) }))).get("/documents/:documentId/users/:userId", documentUserParams, (context) => sharingRoute(context, async () => context.json({ user: await service.userProfile(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").userId) }))).get("/documents/:documentId/shares", documentParams, (context) => sharingRoute(context, async () => context.json({ shares: await service.listShares(context.get("actor").userId, context.req.valid("param").documentId) }))).post("/documents/:documentId/shares", documentParams, validatedJSON(parseCreateDocumentShare), (context) => sharingRoute(context, async () => context.json(await service.createShare(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")), 201))).patch("/documents/:documentId/shares/:shareId", documentShareParams, validatedJSON(parseUpdateDocumentShare), (context) => sharingRoute(context, async () => context.json({ share: await service.updateShare(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").shareId, context.req.valid("json")) }))).post("/documents/:documentId/shares/:shareId/rotate", documentShareParams, (context) => sharingRoute(context, async () => context.json(await service.rotateShare(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").shareId)))).delete("/documents/:documentId/shares/:shareId", documentShareParams, (context) => sharingRoute(context, async () => {
5689
+ await service.revokeShare(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").shareId);
5633
5690
  return context.body(null, 204);
5634
- })).get("/documents/:documentId/grants", (context) => sharingRoute(context, async () => context.json({ grants: await service.listGrants(context.get("actor").userId, context.req.param("documentId")) }))).put("/documents/:documentId/grants/:userId", validatedJSON(parsePutDocumentGrant), (context) => sharingRoute(context, async () => context.json({ grant: await service.putGrant(context.get("actor").userId, context.req.param("documentId"), context.req.param("userId"), context.req.valid("json")) }))).delete("/documents/:documentId/grants/:userId", (context) => sharingRoute(context, async () => {
5635
- await service.revokeGrant(context.get("actor").userId, context.req.param("documentId"), context.req.param("userId"));
5691
+ })).get("/documents/:documentId/grants", documentParams, (context) => sharingRoute(context, async () => context.json({ grants: await service.listGrants(context.get("actor").userId, context.req.valid("param").documentId) }))).put("/documents/:documentId/grants/:userId", documentUserParams, validatedJSON(parsePutDocumentGrant), (context) => sharingRoute(context, async () => context.json({ grant: await service.putGrant(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").userId, context.req.valid("json")) }))).delete("/documents/:documentId/grants/:userId", documentUserParams, (context) => sharingRoute(context, async () => {
5692
+ await service.revokeGrant(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").userId);
5636
5693
  return context.body(null, 204);
5637
- })).get("/documents/:documentId/invitations", (context) => sharingRoute(context, async () => context.json({ invitations: await service.listInvitations(context.get("actor").userId, context.req.param("documentId")) }))).post("/documents/:documentId/invitations", validatedJSON(parseCreateDocumentInvitation), (context) => sharingRoute(context, async () => context.json(await service.createInvitation(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")), 201))).post("/invitations/:invitationId/accept", validatedJSON(parseAcceptDocumentInvitation), (context) => sharingRoute(context, async () => context.json({ accepted: await service.acceptInvitation(context.get("actor"), context.req.param("invitationId"), context.req.valid("json")) }))).delete("/documents/:documentId/invitations/:invitationId", (context) => sharingRoute(context, async () => {
5638
- await service.revokeInvitation(context.get("actor").userId, context.req.param("documentId"), context.req.param("invitationId"));
5694
+ })).get("/documents/:documentId/invitations", documentParams, (context) => sharingRoute(context, async () => context.json({ invitations: await service.listInvitations(context.get("actor").userId, context.req.valid("param").documentId) }))).post("/documents/:documentId/invitations", documentParams, validatedJSON(parseCreateDocumentInvitation), (context) => sharingRoute(context, async () => context.json(await service.createInvitation(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")), 201))).post("/invitations/:invitationId/accept", invitationParams, validatedJSON(parseAcceptDocumentInvitation), (context) => sharingRoute(context, async () => context.json({ accepted: await service.acceptInvitation(context.get("actor"), context.req.valid("param").invitationId, context.req.valid("json")) }))).delete("/documents/:documentId/invitations/:invitationId", documentInvitationParams, (context) => sharingRoute(context, async () => {
5695
+ await service.revokeInvitation(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").invitationId);
5639
5696
  return context.body(null, 204);
5640
5697
  }));
5641
5698
  }
5642
5699
  function createPublicSharingRoutes(service, resolveSession) {
5643
- return new Hono().post("/invitations/continuations", validatedJSON(parseCreateInvitationContinuation), (context) => sharingRoute(context, async () => context.json(await service.createInvitationContinuation(context.req.valid("json")), 201))).post("/invitations/continuations/:continuationId/consume", (context) => sharingRoute(context, async () => context.json(await service.consumeInvitationContinuation(context.req.param("continuationId"))))).post("/invitations/:invitationId/preview", validatedJSON(parseAcceptDocumentInvitation), (context) => sharingRoute(context, async () => context.json({ invitation: await service.previewInvitation(context.req.param("invitationId"), context.req.valid("json")) }))).post("/shares/:shareId/resolve", validatedJSON(parseResolveDocumentShare), (context) => sharingRoute(context, async () => context.json({ resolution: await service.resolveShare(context.req.param("shareId"), context.req.valid("json"), await shareActor(context.req.raw, resolveSession)) })));
5700
+ return new Hono().post("/invitations/continuations", validatedJSON(parseCreateInvitationContinuation), (context) => sharingRoute(context, async () => context.json(await service.createInvitationContinuation(context.req.valid("json")), 201))).post("/invitations/continuations/:continuationId/consume", invitationContinuationParams, (context) => sharingRoute(context, async () => context.json(await service.consumeInvitationContinuation(context.req.valid("param").continuationId)))).post("/invitations/:invitationId/preview", invitationParams, validatedJSON(parseAcceptDocumentInvitation), (context) => sharingRoute(context, async () => context.json({ invitation: await service.previewInvitation(context.req.valid("param").invitationId, context.req.valid("json")) }))).post("/shares/:shareId/resolve", shareParams, validatedJSON(parseResolveDocumentShare), (context) => sharingRoute(context, async () => context.json({ resolution: await service.resolveShare(context.req.valid("param").shareId, context.req.valid("json"), await shareActor(context.req.raw, resolveSession)) })));
5644
5701
  }
5645
5702
  //#endregion
5646
5703
  //#region src/server/sharing/service.ts
@@ -5687,10 +5744,10 @@ function notFound(context, error) {
5687
5744
  }
5688
5745
  function createCollaborationRoutes(service, rateLimit) {
5689
5746
  const router = new Hono();
5690
- if (rateLimit) router.use("/documents/:documentId/collaboration-ticket", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.collaborationTicket, void 0, (context) => context.req.param("documentId") ?? "unknown-document"));
5691
- return router.post("/documents/:documentId/collaboration-ticket", async (context) => {
5747
+ if (rateLimit) router.use("/documents/:documentId/collaboration-ticket", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.collaborationTicket, routeParamResource("documentId")));
5748
+ return router.post("/documents/:documentId/collaboration-ticket", documentParams, async (context) => {
5692
5749
  try {
5693
- return context.json({ ticket: await service.issueUserTicket(context.get("actor"), context.req.param("documentId")) });
5750
+ return context.json({ ticket: await service.issueUserTicket(context.get("actor"), context.req.valid("param").documentId) });
5694
5751
  } catch (error) {
5695
5752
  const response = notFound(context, error);
5696
5753
  if (response) return response;
@@ -5699,9 +5756,9 @@ function createCollaborationRoutes(service, rateLimit) {
5699
5756
  });
5700
5757
  }
5701
5758
  function createPublicCollaborationRoutes(service, resolveSession) {
5702
- return new Hono().post("/shares/:shareId/collaboration-ticket", validatedJSON(parseResolveDocumentShare), async (context) => {
5759
+ return new Hono().post("/shares/:shareId/collaboration-ticket", shareParams, validatedJSON(parseResolveDocumentShare), async (context) => {
5703
5760
  try {
5704
- return context.json({ ticket: await service.issueShareTicket(context.req.param("shareId"), context.req.valid("json"), resolveSession ? await resolveSession(context.req.raw) ?? void 0 : void 0) });
5761
+ return context.json({ ticket: await service.issueShareTicket(context.req.valid("param").shareId, context.req.valid("json"), resolveSession ? await resolveSession(context.req.raw) ?? void 0 : void 0) });
5705
5762
  } catch (error) {
5706
5763
  const response = notFound(context, error);
5707
5764
  if (response) return response;
@@ -5935,6 +5992,172 @@ function relayDocumentLookups(database, policy, deploymentMode) {
5935
5992
  };
5936
5993
  }
5937
5994
  //#endregion
5995
+ //#region src/server/collaboration/relay/connection.ts
5996
+ /**
5997
+ * A socket's connection: a new one, which presents a ticket before it joins, or one resumed with
5998
+ * the peer it already is. It joins fragmented frames, holds what arrives while a ticket is checked
5999
+ * or the room's document loads, and refreshes tickets.
6000
+ */
6001
+ function openRelayConnection(socket, resumed, relay) {
6002
+ let peer = resumed;
6003
+ /** Read through an object, since it changes across awaits and between messages. */
6004
+ const state = {
6005
+ closed: false,
6006
+ busy: false
6007
+ };
6008
+ const isClosed = () => state.closed;
6009
+ const queued = [];
6010
+ let queuedBytes = 0;
6011
+ const assembler = createRelayClientAssembler(() => peer?.authorization.permission === "edit" ? MAX_COLLAB_MESSAGE_BYTES : relay.maximumMessageBytes);
6012
+ const authTimer = resumed ? void 0 : setTimeout(() => socket.close(RELAY_CLOSE.unauthorized, "Expected a collaboration ticket"), relay.authTimeoutMs);
6013
+ async function authenticate(token) {
6014
+ let authorization;
6015
+ try {
6016
+ authorization = await authorizeRelayTicket(token, relay.authSecret);
6017
+ } catch {
6018
+ socket.close(RELAY_CLOSE.unauthorized, "Invalid collaboration ticket");
6019
+ return;
6020
+ }
6021
+ if (relay.documentId !== void 0 && authorization.documentId !== relay.documentId) {
6022
+ socket.close(RELAY_CLOSE.unauthorized, "Ticket is for a different document");
6023
+ return;
6024
+ }
6025
+ if (state.closed) return;
6026
+ if (peer) {
6027
+ const current = peer.authorization;
6028
+ if (authorization.roomId !== current.roomId || principalKey(authorization.principal) !== principalKey(current.principal)) {
6029
+ socket.close(RELAY_CLOSE.unauthorized, "Ticket is for a different room or person");
6030
+ return;
6031
+ }
6032
+ peer.authorization = authorization;
6033
+ relay.refreshed(peer);
6034
+ return;
6035
+ }
6036
+ clearTimeout(authTimer);
6037
+ const admitted = await relay.admit(socket, authorization);
6038
+ if (admitted && isClosed()) relay.depart(admitted);
6039
+ else peer = admitted;
6040
+ }
6041
+ function process(data) {
6042
+ if (data.byteLength > relay.maximumMessageBytes) {
6043
+ socket.close(RELAY_CLOSE.messageTooLarge, "Collaboration message is too large");
6044
+ return;
6045
+ }
6046
+ const assembled = assembler.push(data);
6047
+ if (assembled.type === "partial") return;
6048
+ if (assembled.type === "too-large") {
6049
+ socket.close(RELAY_CLOSE.messageTooLarge, "Collaboration message is too large");
6050
+ return;
6051
+ }
6052
+ const frame = assembled.type === "frame" ? decodeRelayClientFrame(assembled.data) : null;
6053
+ if (!frame) {
6054
+ socket.close(RELAY_CLOSE.protocol, "Malformed collaboration frame");
6055
+ return;
6056
+ }
6057
+ if (frame.type === "auth") {
6058
+ state.busy = true;
6059
+ authenticate(frame.token).catch((error) => {
6060
+ relay.report("Admitting a collaboration peer failed:", error);
6061
+ socket.close(RELAY_CLOSE.protocol, "Could not open the collaboration room");
6062
+ }).finally(() => {
6063
+ state.busy = false;
6064
+ flush();
6065
+ });
6066
+ return;
6067
+ }
6068
+ if (!peer) {
6069
+ socket.close(RELAY_CLOSE.unauthorized, "Expected a collaboration ticket");
6070
+ return;
6071
+ }
6072
+ const delivered = relay.deliver(peer, frame);
6073
+ if (!delivered) return;
6074
+ state.busy = true;
6075
+ delivered.finally(() => {
6076
+ state.busy = false;
6077
+ flush();
6078
+ });
6079
+ }
6080
+ function flush() {
6081
+ while (!state.busy && !state.closed && queued.length > 0) {
6082
+ const next = queued.shift();
6083
+ if (!next) continue;
6084
+ queuedBytes -= next.byteLength;
6085
+ process(next);
6086
+ }
6087
+ }
6088
+ return {
6089
+ receive(data) {
6090
+ if (state.closed) return;
6091
+ queuedBytes += data.byteLength;
6092
+ if (!peer && queuedBytes > relay.maximumMessageBytes) {
6093
+ queued.length = 0;
6094
+ queuedBytes = 0;
6095
+ socket.close(RELAY_CLOSE.messageTooLarge, "Too much sent before joining the room");
6096
+ return;
6097
+ }
6098
+ queued.push(data);
6099
+ flush();
6100
+ },
6101
+ closed() {
6102
+ state.closed = true;
6103
+ queued.length = 0;
6104
+ queuedBytes = 0;
6105
+ clearTimeout(authTimer);
6106
+ if (peer) relay.depart(peer);
6107
+ }
6108
+ };
6109
+ }
6110
+ //#endregion
6111
+ //#region src/server/collaboration/relay/log.ts
6112
+ /**
6113
+ * A room's document as its store saved it, its snapshot with the updates logged after it, and the
6114
+ * log its saves go to. Each room has one writer, the relay holding it, so sequences only grow.
6115
+ */
6116
+ async function openRoomLog(store, room, limits) {
6117
+ const saved = await store.load(room);
6118
+ const document = new Y.Doc();
6119
+ if (saved.snapshot) Y.applyUpdate(document, saved.snapshot);
6120
+ for (const update of saved.updates) Y.applyUpdate(document, update);
6121
+ /** Updates since the last save, which the next one logs. */
6122
+ let pending = [];
6123
+ document.on("update", (update) => pending.push(update));
6124
+ /** The last sequence a save may have written: an append that failed may still have landed. */
6125
+ let sequence = saved.sequence;
6126
+ let loggedCount = saved.updates.length;
6127
+ let loggedBytes = saved.updates.reduce((total, update) => total + update.byteLength, 0);
6128
+ /** Set by a failed save, after which only a snapshot is sure to hold the room. */
6129
+ let compactNext = false;
6130
+ /** The save in flight; the next one waits for it, so two never claim one sequence. */
6131
+ let saving = Promise.resolve();
6132
+ async function write(closing) {
6133
+ const compact = compactNext || loggedCount >= limits.updates || loggedBytes >= limits.bytes || closing && (loggedCount > 0 || pending.length > 0);
6134
+ const batch = pending;
6135
+ pending = [];
6136
+ if (compact) {
6137
+ await store.compact(room, sequence, Y.encodeStateAsUpdate(document));
6138
+ loggedCount = 0;
6139
+ loggedBytes = 0;
6140
+ compactNext = false;
6141
+ } else if (batch.length > 0) {
6142
+ const update = Y.mergeUpdates(batch);
6143
+ sequence += 1;
6144
+ await store.append(room, sequence, update);
6145
+ loggedCount += 1;
6146
+ loggedBytes += update.byteLength;
6147
+ }
6148
+ }
6149
+ return {
6150
+ document,
6151
+ log: { save({ closing = false } = {}) {
6152
+ const run = saving.then(() => write(closing));
6153
+ saving = run.catch(() => {
6154
+ compactNext = true;
6155
+ });
6156
+ return run;
6157
+ } }
6158
+ };
6159
+ }
6160
+ //#endregion
5938
6161
  //#region src/server/collaboration/relay/hub.ts
5939
6162
  /** Messages a viewer may send; their document writes never leave the relay. */
5940
6163
  const VIEWER_ACTIONS = /* @__PURE__ */ new Set([COLLAB_ACTIONS.syncStep1, COLLAB_ACTIONS.awareness]);
@@ -5952,6 +6175,10 @@ function createCollaborationRelay(options) {
5952
6175
  let shuttingDown = false;
5953
6176
  const persistDelayMs = options.persistDelayMs ?? 2e3;
5954
6177
  const persistRetryMs = options.persistRetryMs ?? 3e4;
6178
+ const logLimits = {
6179
+ updates: options.compactAfterUpdates ?? 500,
6180
+ bytes: options.compactAfterBytes ?? 4194304
6181
+ };
5955
6182
  const authTimeoutMs = options.authTimeoutMs ?? 1e4;
5956
6183
  const report = options.onError ?? ((message, error) => console.error(`[Cloud] ${message}`, error));
5957
6184
  function send(socket, frame) {
@@ -5972,20 +6199,9 @@ function createCollaborationRelay(options) {
5972
6199
  };
5973
6200
  const created = (async () => {
5974
6201
  await closing.get(authorization.roomId);
5975
- const kept = rooms.get(authorization.roomId);
5976
- if (kept) return kept;
5977
- const document = new Y.Doc();
5978
- const state = await options.store.load(identity);
5979
- if (state) Y.applyUpdate(document, state);
5980
- const room = {
5981
- id: authorization.roomId,
5982
- identity,
5983
- document,
5984
- peers: /* @__PURE__ */ new Map(),
5985
- persistTimer: null
5986
- };
5987
- document.on("update", () => schedulePersist(room));
5988
- rooms.set(room.id, room);
6202
+ const room = rooms.get(authorization.roomId) ?? newRoom(authorization.roomId, identity);
6203
+ await contentOf(room);
6204
+ if (!rooms.has(room.id)) rooms.set(room.id, room);
5989
6205
  return room;
5990
6206
  })();
5991
6207
  opening.set(authorization.roomId, created);
@@ -5995,15 +6211,41 @@ function createCollaborationRelay(options) {
5995
6211
  opening.delete(authorization.roomId);
5996
6212
  }
5997
6213
  }
5998
- /** Saves the room's document; false when the store refused it. */
5999
- async function persist(room) {
6214
+ function newRoom(id, identity) {
6215
+ return {
6216
+ id,
6217
+ identity,
6218
+ peers: /* @__PURE__ */ new Map(),
6219
+ persistTimer: null,
6220
+ content: null,
6221
+ loading: null
6222
+ };
6223
+ }
6224
+ /** The room's document and log, loaded from the store the first time something needs them. */
6225
+ function contentOf(room) {
6226
+ if (room.content) return Promise.resolve(room.content);
6227
+ room.loading ??= openRoomLog(options.store, room.identity, logLimits).then((content) => {
6228
+ content.document.on("update", () => schedulePersist(room));
6229
+ room.content = content;
6230
+ return content;
6231
+ }, (error) => {
6232
+ room.loading = null;
6233
+ throw error;
6234
+ });
6235
+ return room.loading;
6236
+ }
6237
+ /** Saves what changed in the room since its last save; false when the store refused it. */
6238
+ async function persist(room, closing = false) {
6000
6239
  if (room.persistTimer) clearTimeout(room.persistTimer);
6001
6240
  room.persistTimer = null;
6241
+ if (!room.content && !closing) return true;
6002
6242
  try {
6003
- await options.store.store(room.identity, Y.encodeStateAsUpdate(room.document));
6243
+ const { log } = await contentOf(room);
6244
+ await log.save({ closing });
6004
6245
  return true;
6005
6246
  } catch (error) {
6006
6247
  report("Saving a collaboration room failed:", error);
6248
+ if (!closing && rooms.get(room.id) === room) room.persistTimer ??= setTimeout(() => void persist(room), persistRetryMs);
6007
6249
  return false;
6008
6250
  }
6009
6251
  }
@@ -6014,7 +6256,7 @@ function createCollaborationRelay(options) {
6014
6256
  * Applies a peer's V2 update to the room, inflated first for a sync reply. Rooms are saved as
6015
6257
  * V1 updates; only what peers exchange is V2.
6016
6258
  */
6017
- function applyToRoom(room, peer, namespace, payload) {
6259
+ function applyToRoom(document, peer, namespace, payload) {
6018
6260
  try {
6019
6261
  const update = decodeRoomUpdate(payload, {
6020
6262
  limit: MAX_COLLAB_MESSAGE_BYTES,
@@ -6024,7 +6266,7 @@ function createCollaborationRelay(options) {
6024
6266
  peer.socket.close(RELAY_CLOSE.messageTooLarge, "Collaboration update is too large");
6025
6267
  return false;
6026
6268
  }
6027
- Y.applyUpdateV2(room.document, update, peer.id);
6269
+ Y.applyUpdateV2(document, update, peer.id);
6028
6270
  return true;
6029
6271
  } catch {
6030
6272
  peer.socket.close(RELAY_CLOSE.protocol, "Malformed document update");
@@ -6038,13 +6280,13 @@ function createCollaborationRelay(options) {
6038
6280
  from: from.id,
6039
6281
  payload
6040
6282
  };
6041
- sendAll((target ? [room.peers.get(target)] : [...room.peers.values()].filter((peer) => peer !== from)).flatMap((peer) => peer ? [peer.socket] : []), frame);
6283
+ sendAll(deliverable((target ? [room.peers.get(target)] : [...room.peers.values()].filter((peer) => peer !== from)).flatMap((peer) => peer ? [peer] : [])), frame);
6042
6284
  }
6043
- function handleServerMessage(room, peer, namespace, payload) {
6285
+ function handleServerMessage(document, peer, namespace, payload) {
6044
6286
  if (namespace === COLLAB_ACTIONS.syncStep1) {
6045
6287
  let reply;
6046
6288
  try {
6047
- reply = encodeSyncReply(room.document, payload);
6289
+ reply = encodeSyncReply(document, payload);
6048
6290
  } catch {
6049
6291
  peer.socket.close(RELAY_CLOSE.protocol, "Malformed state vector");
6050
6292
  return;
@@ -6055,28 +6297,73 @@ function createCollaborationRelay(options) {
6055
6297
  from: RELAY_SERVER_PEER,
6056
6298
  payload: reply
6057
6299
  });
6058
- } else if (namespace === COLLAB_ACTIONS.syncReply) applyToRoom(room, peer, namespace, payload);
6300
+ } else if (namespace === COLLAB_ACTIONS.syncReply) applyToRoom(document, peer, namespace, payload);
6301
+ }
6302
+ /** Whether handling a message reads or writes the room's document. */
6303
+ function needsDocument(namespace, target) {
6304
+ if (namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply) return true;
6305
+ return target === "server" && namespace === COLLAB_ACTIONS.syncStep1;
6059
6306
  }
6060
- function handleMessage(room, peer, namespace, target, payload) {
6307
+ function handleMessage(room, document, peer, namespace, target, payload) {
6061
6308
  if (peer.authorization.permission === "view" && !VIEWER_ACTIONS.has(namespace)) return;
6062
6309
  if (namespace === COLLAB_ACTIONS.versions) return;
6063
6310
  let forwarded = payload;
6064
6311
  if (namespace === COLLAB_ACTIONS.awareness) {
6065
6312
  const stamped = stampAwareness(payload, peer.authorization, (clientId) => [...room.peers.values()].some((other) => other !== peer && other.presenceClients.has(clientId)));
6313
+ const known = peer.presenceClients.size;
6066
6314
  for (const clientId of stamped.claimed) peer.presenceClients.add(clientId);
6315
+ if (peer.presenceClients.size !== known) remember(peer);
6067
6316
  if (!stamped.update) return;
6068
6317
  forwarded = stamped.update;
6069
6318
  }
6070
6319
  if (target === "server") {
6071
- handleServerMessage(room, peer, namespace, payload);
6320
+ if (document) handleServerMessage(document, peer, namespace, payload);
6072
6321
  return;
6073
6322
  }
6074
- if ((namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply) && !applyToRoom(room, peer, namespace, payload)) return;
6323
+ if ((namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply) && !(document && applyToRoom(document, peer, namespace, payload))) return;
6075
6324
  forward(room, peer, namespace, target, forwarded);
6076
6325
  }
6077
- function scheduleExpiry(peer) {
6078
- clearTimeout(peer.expiry);
6079
- peer.expiry = setTimeout(() => peer.socket.close(RELAY_CLOSE.expired, "Collaboration ticket expired"), Math.max(0, peer.authorization.expiresAt - Date.now()));
6326
+ /**
6327
+ * Whether the peer's ticket still holds, closing its socket when it lapsed. It is checked when
6328
+ * the relay hears from the peer and before it sends the peer anything, never by a timer, so a
6329
+ * runtime can set an idle relay aside while sockets stay open.
6330
+ */
6331
+ function current(peer) {
6332
+ if (Date.now() < peer.authorization.expiresAt) return true;
6333
+ peer.socket.close(RELAY_CLOSE.expired, "Collaboration ticket expired");
6334
+ return false;
6335
+ }
6336
+ /** The sockets of peers whose tickets still hold. */
6337
+ function deliverable(peers) {
6338
+ return [...peers].filter(current).map((peer) => peer.socket);
6339
+ }
6340
+ function remember(peer) {
6341
+ peer.socket.remember?.({
6342
+ peerId: peer.id,
6343
+ authorization: peer.authorization,
6344
+ presenceClients: [...peer.presenceClients]
6345
+ });
6346
+ }
6347
+ /** Handles a room message from an admitted peer; a promise while the room's document loads. */
6348
+ function deliver(peer, frame) {
6349
+ const room = rooms.get(peer.authorization.roomId);
6350
+ if (!room) {
6351
+ peer.socket.close(RELAY_CLOSE.unauthorized, "Expected a collaboration ticket");
6352
+ return;
6353
+ }
6354
+ if (!current(peer)) return void 0;
6355
+ const { namespace, target, payload } = frame;
6356
+ if (room.content || !needsDocument(namespace, target)) {
6357
+ handleMessage(room, room.content?.document ?? null, peer, namespace, target, payload);
6358
+ return;
6359
+ }
6360
+ return contentOf(room).then(({ document }) => {
6361
+ if (room.peers.get(peer.id) !== peer) return void 0;
6362
+ handleMessage(room, document, peer, namespace, target, payload);
6363
+ }, (error) => {
6364
+ report("Loading a collaboration room failed:", error);
6365
+ peer.socket.close(RELAY_CLOSE.protocol, "Could not open the collaboration room");
6366
+ });
6080
6367
  }
6081
6368
  async function admit(socket, authorization) {
6082
6369
  const limit = await options.maximumParticipants?.(authorization.documentId);
@@ -6103,28 +6390,31 @@ function createCollaborationRelay(options) {
6103
6390
  authorization,
6104
6391
  presenceClients: /* @__PURE__ */ new Set()
6105
6392
  };
6106
- scheduleExpiry(peer);
6393
+ remember(peer);
6107
6394
  send(socket, {
6108
6395
  type: "welcome",
6109
6396
  peerId: peer.id,
6110
6397
  peers: [RELAY_SERVER_PEER, ...room.peers.keys()]
6111
6398
  });
6112
- for (const other of room.peers.values()) send(other.socket, {
6399
+ sendAll(deliverable(room.peers.values()), {
6113
6400
  type: "join",
6114
6401
  peerId: peer.id
6115
6402
  });
6116
6403
  room.peers.set(peer.id, peer);
6117
- if (authorization.permission === "edit") send(socket, {
6118
- type: "message",
6119
- namespace: COLLAB_ACTIONS.syncStep1,
6120
- from: RELAY_SERVER_PEER,
6121
- payload: Y.encodeStateVector(room.document)
6122
- });
6404
+ if (authorization.permission === "edit") {
6405
+ const { document } = await contentOf(room);
6406
+ send(socket, {
6407
+ type: "message",
6408
+ namespace: COLLAB_ACTIONS.syncStep1,
6409
+ from: RELAY_SERVER_PEER,
6410
+ payload: Y.encodeStateVector(document)
6411
+ });
6412
+ }
6123
6413
  return peer;
6124
6414
  }
6125
6415
  async function saveAndClose(room) {
6126
- if (await persist(room) || shuttingDown) {
6127
- room.document.destroy();
6416
+ if (await persist(room, true) || shuttingDown) {
6417
+ room.content?.document.destroy();
6128
6418
  return;
6129
6419
  }
6130
6420
  rooms.set(room.id, room);
@@ -6144,119 +6434,59 @@ function createCollaborationRelay(options) {
6144
6434
  }
6145
6435
  }
6146
6436
  function depart(peer) {
6147
- clearTimeout(peer.expiry);
6148
6437
  const room = rooms.get(peer.authorization.roomId);
6149
6438
  if (!room?.peers.delete(peer.id)) return;
6150
- for (const other of room.peers.values()) send(other.socket, {
6439
+ sendAll(deliverable(room.peers.values()), {
6151
6440
  type: "leave",
6152
6441
  peerId: peer.id
6153
6442
  });
6154
6443
  leaveRoom(room);
6155
6444
  }
6445
+ /**
6446
+ * A socket's connection: a new one, which presents a ticket before it joins, or one resumed with
6447
+ * the peer it already is.
6448
+ */
6449
+ const connectionRelay = {
6450
+ authSecret: options.authSecret,
6451
+ documentId: options.documentId,
6452
+ maximumMessageBytes: options.maximumMessageBytes,
6453
+ authTimeoutMs,
6454
+ admit,
6455
+ refreshed: remember,
6456
+ deliver,
6457
+ depart,
6458
+ report
6459
+ };
6156
6460
  return {
6157
6461
  connect(socket) {
6158
- let peer = null;
6159
- /** Read through an object, since it changes across awaits and between messages. */
6160
- const state = {
6161
- closed: false,
6162
- authenticating: false
6462
+ return openRelayConnection(socket, null, connectionRelay);
6463
+ },
6464
+ /**
6465
+ * Takes back a socket whose peer was admitted before the runtime set the relay aside, as the
6466
+ * peer it was, without loading the room's document until a message needs it. A peer whose
6467
+ * ticket lapsed meanwhile is closed.
6468
+ */
6469
+ resume(socket, saved) {
6470
+ const { authorization } = saved;
6471
+ const identity = {
6472
+ documentId: authorization.documentId,
6473
+ roomEpoch: authorization.roomEpoch
6163
6474
  };
6164
- const isClosed = () => state.closed;
6165
- const queued = [];
6166
- let queuedBytes = 0;
6167
- const assembler = createRelayClientAssembler(() => peer?.authorization.permission === "edit" ? MAX_COLLAB_MESSAGE_BYTES : options.maximumMessageBytes);
6168
- const authTimer = setTimeout(() => socket.close(RELAY_CLOSE.unauthorized, "Expected a collaboration ticket"), authTimeoutMs);
6169
- async function authenticate(token) {
6170
- let authorization;
6171
- try {
6172
- authorization = await authorizeRelayTicket(token, options.authSecret);
6173
- } catch {
6174
- socket.close(RELAY_CLOSE.unauthorized, "Invalid collaboration ticket");
6175
- return;
6176
- }
6177
- if (options.documentId !== void 0 && authorization.documentId !== options.documentId) {
6178
- socket.close(RELAY_CLOSE.unauthorized, "Ticket is for a different document");
6179
- return;
6180
- }
6181
- if (state.closed) return;
6182
- if (peer) {
6183
- const current = peer.authorization;
6184
- if (authorization.roomId !== current.roomId || principalKey(authorization.principal) !== principalKey(current.principal)) {
6185
- socket.close(RELAY_CLOSE.unauthorized, "Ticket is for a different room or person");
6186
- return;
6187
- }
6188
- peer.authorization = authorization;
6189
- scheduleExpiry(peer);
6190
- return;
6191
- }
6192
- clearTimeout(authTimer);
6193
- const admitted = await admit(socket, authorization);
6194
- if (admitted && isClosed()) depart(admitted);
6195
- else peer = admitted;
6196
- }
6197
- function process(data) {
6198
- if (data.byteLength > options.maximumMessageBytes) {
6199
- socket.close(RELAY_CLOSE.messageTooLarge, "Collaboration message is too large");
6200
- return;
6201
- }
6202
- const assembled = assembler.push(data);
6203
- if (assembled.type === "partial") return;
6204
- if (assembled.type === "too-large") {
6205
- socket.close(RELAY_CLOSE.messageTooLarge, "Collaboration message is too large");
6206
- return;
6207
- }
6208
- const frame = assembled.type === "frame" ? decodeRelayClientFrame(assembled.data) : null;
6209
- if (!frame) {
6210
- socket.close(RELAY_CLOSE.protocol, "Malformed collaboration frame");
6211
- return;
6212
- }
6213
- if (frame.type === "auth") {
6214
- state.authenticating = true;
6215
- authenticate(frame.token).catch((error) => {
6216
- report("Admitting a collaboration peer failed:", error);
6217
- socket.close(RELAY_CLOSE.protocol, "Could not open the collaboration room");
6218
- }).finally(() => {
6219
- state.authenticating = false;
6220
- flush();
6221
- });
6222
- return;
6223
- }
6224
- const room = peer && rooms.get(peer.authorization.roomId);
6225
- if (!peer || !room) {
6226
- socket.close(RELAY_CLOSE.unauthorized, "Expected a collaboration ticket");
6227
- return;
6228
- }
6229
- handleMessage(room, peer, frame.namespace, frame.target, frame.payload);
6230
- }
6231
- function flush() {
6232
- while (!state.authenticating && !state.closed && queued.length > 0) {
6233
- const next = queued.shift();
6234
- if (!next) continue;
6235
- queuedBytes -= next.byteLength;
6236
- process(next);
6237
- }
6475
+ let room = rooms.get(authorization.roomId);
6476
+ if (!room) {
6477
+ room = newRoom(authorization.roomId, identity);
6478
+ rooms.set(room.id, room);
6238
6479
  }
6239
- return {
6240
- receive(data) {
6241
- if (state.closed) return;
6242
- queuedBytes += data.byteLength;
6243
- if (!peer && queuedBytes > options.maximumMessageBytes) {
6244
- queued.length = 0;
6245
- queuedBytes = 0;
6246
- socket.close(RELAY_CLOSE.messageTooLarge, "Too much sent before joining the room");
6247
- return;
6248
- }
6249
- queued.push(data);
6250
- flush();
6251
- },
6252
- closed() {
6253
- state.closed = true;
6254
- queued.length = 0;
6255
- queuedBytes = 0;
6256
- clearTimeout(authTimer);
6257
- if (peer) depart(peer);
6258
- }
6480
+ const peer = {
6481
+ id: saved.peerId,
6482
+ socket,
6483
+ authorization,
6484
+ presenceClients: new Set(saved.presenceClients)
6259
6485
  };
6486
+ room.peers.set(peer.id, peer);
6487
+ const connection = openRelayConnection(socket, peer, connectionRelay);
6488
+ queueMicrotask(() => current(peer));
6489
+ return connection;
6260
6490
  },
6261
6491
  /**
6262
6492
  * Ends a restored document's rooms: everyone in them is told the document was restored, and
@@ -6267,12 +6497,9 @@ function createCollaborationRelay(options) {
6267
6497
  if (room.identity.documentId !== documentId) continue;
6268
6498
  rooms.delete(room.id);
6269
6499
  if (room.persistTimer) clearTimeout(room.persistTimer);
6270
- for (const peer of room.peers.values()) {
6271
- clearTimeout(peer.expiry);
6272
- peer.socket.close(RELAY_CLOSE.restored, "The document was restored");
6273
- }
6500
+ for (const peer of room.peers.values()) peer.socket.close(RELAY_CLOSE.restored, "The document was restored");
6274
6501
  room.peers.clear();
6275
- room.document.destroy();
6502
+ room.content?.document.destroy();
6276
6503
  }
6277
6504
  },
6278
6505
  /** Tells everyone in a document's rooms that its version history changed. */
@@ -6283,16 +6510,13 @@ function createCollaborationRelay(options) {
6283
6510
  from: RELAY_SERVER_PEER,
6284
6511
  payload: /* @__PURE__ */ new Uint8Array()
6285
6512
  };
6286
- sendAll([...rooms.values()].filter((room) => room.identity.documentId === documentId).flatMap((room) => [...room.peers.values()].map((peer) => peer.socket)), frame);
6513
+ sendAll(deliverable([...rooms.values()].filter((room) => room.identity.documentId === documentId).flatMap((room) => [...room.peers.values()])), frame);
6287
6514
  },
6288
6515
  /** Saves every open room and closes its sockets, as the runtime shuts down. */
6289
6516
  async close() {
6290
6517
  shuttingDown = true;
6291
6518
  await Promise.all([...rooms.values()].map(async (room) => {
6292
- for (const peer of room.peers.values()) {
6293
- clearTimeout(peer.expiry);
6294
- peer.socket.close(1001, "Server shutting down");
6295
- }
6519
+ for (const peer of room.peers.values()) peer.socket.close(1001, "Server shutting down");
6296
6520
  room.peers.clear();
6297
6521
  await leaveRoom(room);
6298
6522
  }));
@@ -6302,23 +6526,84 @@ function createCollaborationRelay(options) {
6302
6526
  }
6303
6527
  //#endregion
6304
6528
  //#region src/server/collaboration/relay/persistence.ts
6305
- function createCollaborationStateStore(database) {
6529
+ /**
6530
+ * Whether the document is still at the room's epoch, holding its row so a restore, which advances
6531
+ * the epoch and deletes the replaced rooms' rows, waits for this write or this write sees it.
6532
+ */
6533
+ async function holdsEpoch(transaction, room) {
6534
+ return (await transaction.selectFrom("document").select("collaborationEpoch").where("id", "=", room.documentId).forShare().executeTakeFirst())?.collaborationEpoch === room.roomEpoch;
6535
+ }
6536
+ /** A snapshot is a Yjs update, as the store hands it back. */
6537
+ const SNAPSHOT_CONTENT_TYPE = "application/octet-stream";
6538
+ /**
6539
+ * Where a room's snapshots go. Each compaction writes a new file and points the room at it; the one
6540
+ * it replaced is no longer referenced, and the object sweep removes it as it does a deleted
6541
+ * revision's file.
6542
+ */
6543
+ function snapshotKey(room) {
6544
+ return `collaboration/${room.documentId}/${room.roomEpoch}/${crypto.randomUUID()}.yjs`;
6545
+ }
6546
+ /**
6547
+ * A store keeping each room's snapshot as a file in the object store and the updates logged after
6548
+ * it in the database, so the database holds only what changed since the last snapshot.
6549
+ */
6550
+ function createCollaborationStateStore(database, objects) {
6306
6551
  return {
6307
6552
  async load(room) {
6308
- const row = await database.selectFrom("documentCollaborationState").select("state").where("documentId", "=", room.documentId).where("roomEpoch", "=", room.roomEpoch).executeTakeFirst();
6309
- return row ? new Uint8Array(row.state) : null;
6310
- },
6311
- async store(room, state) {
6312
- await database.insertInto("documentCollaborationState").values({
6313
- documentId: room.documentId,
6314
- roomEpoch: room.roomEpoch,
6315
- state,
6316
- updatedAt: /* @__PURE__ */ new Date()
6317
- }).onConflict((conflict) => conflict.columns(["documentId", "roomEpoch"]).doUpdateSet({
6318
- state,
6319
- version: (expression) => expression("documentCollaborationState.version", "+", 1),
6320
- updatedAt: /* @__PURE__ */ new Date()
6321
- })).execute();
6553
+ const snapshot = await database.selectFrom("documentRoomSnapshot").innerJoin("storageObject", "storageObject.id", "documentRoomSnapshot.snapshotObjectId").select(["storageObject.objectKey", "documentRoomSnapshot.throughSequence"]).where("documentRoomSnapshot.documentId", "=", room.documentId).where("documentRoomSnapshot.roomEpoch", "=", room.roomEpoch).executeTakeFirst();
6554
+ const state = snapshot ? await objects.read(snapshot.objectKey) : null;
6555
+ if (snapshot && !state) throw new Error(`The snapshot of room ${room.documentId}:${room.roomEpoch} is missing`);
6556
+ const through = snapshot?.throughSequence ?? 0;
6557
+ const updates = await database.selectFrom("documentRoomUpdate").select(["sequence", "data"]).where("documentId", "=", room.documentId).where("roomEpoch", "=", room.roomEpoch).where("sequence", ">", through).orderBy("sequence").execute();
6558
+ return {
6559
+ snapshot: state,
6560
+ updates: updates.map((row) => new Uint8Array(row.data)),
6561
+ sequence: updates.at(-1)?.sequence ?? through
6562
+ };
6563
+ },
6564
+ async append(room, sequence, update) {
6565
+ await database.transaction().execute(async (transaction) => {
6566
+ if (!await holdsEpoch(transaction, room)) return;
6567
+ await transaction.insertInto("documentRoomUpdate").values({
6568
+ documentId: room.documentId,
6569
+ roomEpoch: room.roomEpoch,
6570
+ sequence,
6571
+ data: update
6572
+ }).execute();
6573
+ });
6574
+ },
6575
+ async compact(room, sequence, state) {
6576
+ const snapshotObjectId = crypto.randomUUID();
6577
+ const objectKey = snapshotKey(room);
6578
+ const checksum = fromUint8Array(sha256(state));
6579
+ await database.insertInto("storageObject").values({
6580
+ id: snapshotObjectId,
6581
+ objectKey,
6582
+ checksum,
6583
+ byteSize: state.byteLength,
6584
+ contentType: SNAPSHOT_CONTENT_TYPE
6585
+ }).execute();
6586
+ await objects.write({
6587
+ key: objectKey,
6588
+ bytes: state,
6589
+ contentType: SNAPSHOT_CONTENT_TYPE,
6590
+ checksum
6591
+ });
6592
+ await database.transaction().execute(async (transaction) => {
6593
+ if (!await holdsEpoch(transaction, room)) return;
6594
+ await transaction.insertInto("documentRoomSnapshot").values({
6595
+ documentId: room.documentId,
6596
+ roomEpoch: room.roomEpoch,
6597
+ snapshotObjectId,
6598
+ throughSequence: sequence,
6599
+ updatedAt: /* @__PURE__ */ new Date()
6600
+ }).onConflict((conflict) => conflict.columns(["documentId", "roomEpoch"]).doUpdateSet({
6601
+ snapshotObjectId,
6602
+ throughSequence: sequence,
6603
+ updatedAt: /* @__PURE__ */ new Date()
6604
+ })).execute();
6605
+ await transaction.deleteFrom("documentRoomUpdate").where("documentId", "=", room.documentId).where("roomEpoch", "=", room.roomEpoch).where("sequence", "<=", sequence).execute();
6606
+ });
6322
6607
  }
6323
6608
  };
6324
6609
  }
@@ -6340,7 +6625,7 @@ async function answer(context, run) {
6340
6625
  }
6341
6626
  /** A document's version history: listing, naming, opening, restoring, and duplicating versions. */
6342
6627
  function createDocumentVersionRoutes(service) {
6343
- return new Hono().get("/documents/:documentId/versions", validatedQuery(parsePage), (context) => answer(context, async () => context.json(await service.list(context.get("actor").userId, context.req.param("documentId"), context.req.valid("query"))))).post("/documents/:documentId/versions", validatedJSON(parseSaveDocumentVersion), (context) => answer(context, async () => context.json({ version: await service.save(context.get("actor").userId, context.req.param("documentId"), context.req.valid("json")) }, 201))).patch("/documents/:documentId/versions/:versionId", validatedJSON(parseUpdateDocumentVersion), (context) => answer(context, async () => context.json({ version: await service.update(context.get("actor").userId, context.req.param("documentId"), context.req.param("versionId"), context.req.valid("json")) }))).get("/documents/:documentId/versions/:versionId", (context) => answer(context, async () => context.json(await service.download(context.get("actor").userId, context.req.param("documentId"), context.req.param("versionId"))))).post("/documents/:documentId/versions/:versionId/restore", (context) => answer(context, async () => context.json(await service.restore(context.get("actor").userId, context.req.param("documentId"), context.req.param("versionId"))))).post("/documents/:documentId/versions/:versionId/duplicate", validatedJSON(parseDuplicateDocumentVersion), (context) => answer(context, async () => context.json({ document: await service.duplicate(context.get("actor").userId, context.req.param("documentId"), context.req.param("versionId"), context.req.valid("json")) }, 201)));
6628
+ return new Hono().get("/documents/:documentId/versions", documentParams, validatedQuery(parsePage), (context) => answer(context, async () => context.json(await service.list(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("query"))))).post("/documents/:documentId/versions", documentParams, validatedJSON(parseSaveDocumentVersion), (context) => answer(context, async () => context.json({ version: await service.save(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("json")) }, 201))).patch("/documents/:documentId/versions/:versionId", documentVersionParams, validatedJSON(parseUpdateDocumentVersion), (context) => answer(context, async () => context.json({ version: await service.update(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").versionId, context.req.valid("json")) }))).get("/documents/:documentId/versions/:versionId", documentVersionParams, (context) => answer(context, async () => context.json(await service.download(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").versionId)))).post("/documents/:documentId/versions/:versionId/restore", documentVersionParams, (context) => answer(context, async () => context.json(await service.restore(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").versionId)))).post("/documents/:documentId/versions/:versionId/duplicate", documentVersionParams, validatedJSON(parseDuplicateDocumentVersion), (context) => answer(context, async () => context.json({ document: await service.duplicate(context.get("actor").userId, context.req.valid("param").documentId, context.req.valid("param").versionId, context.req.valid("json")) }, 201)));
6344
6629
  }
6345
6630
  //#endregion
6346
6631
  //#region src/server/documents/versions/service.ts
@@ -6527,6 +6812,8 @@ function createDocumentVersionService(database, objects, options = {}) {
6527
6812
  version: (expression) => expression("version", "+", 1),
6528
6813
  collaborationEpoch: (expression) => expression("collaborationEpoch", "+", 1)
6529
6814
  }).where("id", "=", documentId).execute();
6815
+ await transaction.deleteFrom("documentRoomUpdate").where("documentId", "=", documentId).execute();
6816
+ await transaction.deleteFrom("documentRoomSnapshot").where("documentId", "=", documentId).execute();
6530
6817
  await keepRevision(transaction, {
6531
6818
  documentId,
6532
6819
  revisionId,
@@ -6675,8 +6962,8 @@ function createWorkspaceRoutes(service, members, rateLimit) {
6675
6962
  const router = new Hono();
6676
6963
  if (rateLimit) {
6677
6964
  router.use("/", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.workspaceCreation, (context) => context.req.method !== "POST"));
6678
- router.use("/:workspaceId/invitations/:invitationId/resend", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, () => false, (context) => context.req.param("workspaceId") ?? "unknown"));
6679
- router.use("/:workspaceId/invitations", createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, (context) => context.req.method !== "POST", (context) => context.req.param("workspaceId") ?? "unknown"));
6965
+ router.use("/:workspaceId/invitations/:invitationId/resend", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, routeParamResource("workspaceId"), () => false));
6966
+ router.use("/:workspaceId/invitations", createResourceRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.invitationMutation, routeParamResource("workspaceId"), (context) => context.req.method !== "POST"));
6680
6967
  }
6681
6968
  return router.get("/", async (context) => {
6682
6969
  const workspaces = await service.list(context.get("actor").userId);
@@ -6689,21 +6976,21 @@ function createWorkspaceRoutes(service, members, rateLimit) {
6689
6976
  if (error instanceof WorkspaceSlugConflictError) return context.json({ error: { code: "slug_conflict" } }, 409);
6690
6977
  throw error;
6691
6978
  }
6692
- }).get("/:workspaceId", async (context) => {
6693
- const workspace = await service.get(context.get("actor").userId, context.req.param("workspaceId"));
6979
+ }).get("/:workspaceId", workspaceParams, async (context) => {
6980
+ const workspace = await service.get(context.get("actor").userId, context.req.valid("param").workspaceId);
6694
6981
  if (!workspace) return context.json({ error: { code: "not_found" } }, 404);
6695
6982
  return context.json({ workspace });
6696
- }).patch("/:workspaceId", validatedJSON(parseUpdateWorkspace), (context) => memberRoute(context, async () => context.json({ workspace: await members.rename(context.get("actor").userId, context.req.param("workspaceId"), context.req.valid("json").name) }))).delete("/:workspaceId", (context) => memberRoute(context, async () => {
6697
- await members.delete(context.get("actor").userId, context.req.param("workspaceId"));
6983
+ }).patch("/:workspaceId", workspaceParams, validatedJSON(parseUpdateWorkspace), (context) => memberRoute(context, async () => context.json({ workspace: await members.rename(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("json").name) }))).delete("/:workspaceId", workspaceParams, (context) => memberRoute(context, async () => {
6984
+ await members.delete(context.get("actor").userId, context.req.valid("param").workspaceId);
6698
6985
  return context.body(null, 204);
6699
- })).get("/:workspaceId/members", (context) => memberRoute(context, async () => context.json(await members.list(context.get("actor").userId, context.req.param("workspaceId"))))).post("/:workspaceId/invitations", validatedJSON(parseCreateWorkspaceInvitation), (context) => memberRoute(context, async () => context.json(await members.invite(context.get("actor").userId, context.req.param("workspaceId"), context.req.valid("json")), 201))).post("/:workspaceId/invitations/:invitationId/resend", (context) => memberRoute(context, async () => context.json(await members.resendInvitation(context.get("actor").userId, context.req.param("workspaceId"), context.req.param("invitationId"))))).delete("/:workspaceId/invitations/:invitationId", (context) => memberRoute(context, async () => {
6700
- await members.revokeInvitation(context.get("actor").userId, context.req.param("workspaceId"), context.req.param("invitationId"));
6986
+ })).get("/:workspaceId/members", workspaceParams, (context) => memberRoute(context, async () => context.json(await members.list(context.get("actor").userId, context.req.valid("param").workspaceId)))).post("/:workspaceId/invitations", workspaceParams, validatedJSON(parseCreateWorkspaceInvitation), (context) => memberRoute(context, async () => context.json(await members.invite(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("json")), 201))).post("/:workspaceId/invitations/:invitationId/resend", workspaceInvitationParams, (context) => memberRoute(context, async () => context.json(await members.resendInvitation(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("param").invitationId)))).delete("/:workspaceId/invitations/:invitationId", workspaceInvitationParams, (context) => memberRoute(context, async () => {
6987
+ await members.revokeInvitation(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("param").invitationId);
6701
6988
  return context.body(null, 204);
6702
- })).patch("/:workspaceId/members/:userId", validatedJSON(parseUpdateWorkspaceMember), (context) => memberRoute(context, async () => {
6703
- await members.updateRole(context.get("actor").userId, context.req.param("workspaceId"), context.req.param("userId"), context.req.valid("json").role);
6989
+ })).patch("/:workspaceId/members/:userId", workspaceMemberParams, validatedJSON(parseUpdateWorkspaceMember), (context) => memberRoute(context, async () => {
6990
+ await members.updateRole(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("param").userId, context.req.valid("json").role);
6704
6991
  return context.body(null, 204);
6705
- })).delete("/:workspaceId/members/:userId", (context) => memberRoute(context, async () => {
6706
- await members.remove(context.get("actor").userId, context.req.param("workspaceId"), context.req.param("userId"));
6992
+ })).delete("/:workspaceId/members/:userId", workspaceMemberParams, (context) => memberRoute(context, async () => {
6993
+ await members.remove(context.get("actor").userId, context.req.valid("param").workspaceId, context.req.valid("param").userId);
6707
6994
  return context.body(null, 204);
6708
6995
  }));
6709
6996
  }
@@ -6713,9 +7000,9 @@ function createPublicCloudAPIRouter(services) {
6713
7000
  return new Hono().route("/", createPublicSharingRoutes(services.sharing, services.resolveSession)).route("/", createPublicDocumentRoutes(services.sharing, services.documents, services.resolveSession)).route("/", createPublicCollaborationRoutes(services.collaboration, services.resolveSession));
6714
7001
  }
6715
7002
  function createCloudAPIRouter(services) {
6716
- return new Hono().route("/public", createPublicCloudAPIRouter(services)).get("/workspaces/:workspaceId/entitlements", async (context) => {
7003
+ return new Hono().route("/public", createPublicCloudAPIRouter(services)).get("/workspaces/:workspaceId/entitlements", workspaceParams, async (context) => {
6717
7004
  if (!services.entitlements) return context.json({ error: { code: "not_found" } }, 404);
6718
- return context.json({ entitlements: await services.entitlements.workspace(context.get("actor").userId, context.req.param("workspaceId")) });
7005
+ return context.json({ entitlements: await services.entitlements.workspace(context.get("actor").userId, context.req.valid("param").workspaceId) });
6719
7006
  }).get("/session", (context) => context.json({ user: context.get("actor") })).route("/", createAIRoutes(services.aiTokens, services.database && services.rateLimitSecret ? {
6720
7007
  database: services.database,
6721
7008
  secret: services.rateLimitSecret
@@ -6892,6 +7179,31 @@ function createAccountRoutes(services) {
6892
7179
  });
6893
7180
  }
6894
7181
  //#endregion
7182
+ //#region src/server/errors/index.ts
7183
+ /** Logs a caught error and reports it, when the deployment reports errors. */
7184
+ function reportCloudError(reporter, message, error, context) {
7185
+ console.error(`[Cloud] ${message}`, error);
7186
+ return reporter ? reporter(error, context) : Promise.resolve();
7187
+ }
7188
+ function requestContext(context) {
7189
+ return {
7190
+ source: "request",
7191
+ method: context.req.method,
7192
+ route: routePath(context)
7193
+ };
7194
+ }
7195
+ /**
7196
+ * The app's last error handler, as Hono's default: an `HTTPException` answers with its response,
7197
+ * anything else is logged, reported, and answered with a 500.
7198
+ */
7199
+ function cloudErrorHandler(reporter) {
7200
+ return (error, context) => {
7201
+ if (error instanceof HTTPException) return error.getResponse();
7202
+ reportCloudError(reporter, "Request failed:", error, requestContext(context));
7203
+ return context.text("Internal Server Error", 500);
7204
+ };
7205
+ }
7206
+ //#endregion
6895
7207
  //#region src/server/internal/schema.ts
6896
7208
  const setWorkspaceEntitlementsSchema = v.object({
6897
7209
  values: staticEntitlementsSchema,
@@ -6928,7 +7240,6 @@ async setWorkspaceEntitlements(workspaceId, input) {
6928
7240
  }
6929
7241
  //#endregion
6930
7242
  //#region src/server/internal/routes.ts
6931
- const uuidSchema = v.pipe(v.string(), v.uuid());
6932
7243
  /** The internal API on HTTP, for runtimes without a private transport to their services. */
6933
7244
  function createInternalRoutes(service, serviceToken) {
6934
7245
  const digest = hashCapability(serviceToken);
@@ -6938,9 +7249,8 @@ function createInternalRoutes(service, serviceToken) {
6938
7249
  noAuthenticationHeader: { message: unauthorized },
6939
7250
  invalidAuthenticationHeader: { message: unauthorized },
6940
7251
  invalidToken: { message: unauthorized }
6941
- })).put("/workspaces/:workspaceId/entitlements", validatedJSON(parseSetWorkspaceEntitlements), async (context) => {
6942
- const workspaceId = context.req.param("workspaceId");
6943
- if (!v.is(uuidSchema, workspaceId)) return context.json({ error: { code: "not_found" } }, 404);
7252
+ })).put("/workspaces/:workspaceId/entitlements", workspaceParams, validatedJSON(parseSetWorkspaceEntitlements), async (context) => {
7253
+ const { workspaceId } = context.req.valid("param");
6944
7254
  const result = await service.setWorkspaceEntitlements(workspaceId, context.req.valid("json"));
6945
7255
  if (!result.found) return context.json({ error: { code: "not_found" } }, 404);
6946
7256
  return context.json({ applied: result.applied });
@@ -7099,7 +7409,7 @@ function createCloudApp(services) {
7099
7409
  resolveSession
7100
7410
  });
7101
7411
  const internalAPI = services.config.internalServiceToken ? createInternalRoutes(createInternalService(services.database), services.config.internalServiceToken) : new Hono();
7102
- return new Hono().use(CLOUD_DISCOVERY_PATH, cloudCORS).use("/api/*", cloudCORS).use("*", async (context, next) => {
7412
+ return new Hono().onError(cloudErrorHandler(services.reportError)).use(CLOUD_DISCOVERY_PATH, cloudCORS).use("/api/*", cloudCORS).use("*", async (context, next) => {
7103
7413
  await next();
7104
7414
  if (shouldPreventIndexing(context.req.path, services.config.indexingPolicy)) context.header("X-Robots-Tag", "noindex, nofollow, noarchive");
7105
7415
  }).get("/robots.txt", (context) => {
@@ -7808,8 +8118,8 @@ async function loadNodeCloudServerConfig(environment) {
7808
8118
  }
7809
8119
  //#endregion
7810
8120
  //#region src/runtime/node/bootstrap.ts
7811
- async function createMigratedNodeCloudDatabase(environment) {
7812
- const config = await loadNodeCloudServerConfig(environment);
8121
+ /** The deployment's database, migrated to this version before anything uses it. */
8122
+ async function migratedNodeCloudDatabase(config) {
7813
8123
  const database = createPostgresCloudDatabase({ connectionString: config.databaseURL });
7814
8124
  const auth = createBetterAuthAdapter(config, database);
7815
8125
  await migrateCloudDatabase(database, {
@@ -7817,11 +8127,17 @@ async function createMigratedNodeCloudDatabase(environment) {
7817
8127
  schemaVersion: auth.schemaVersion
7818
8128
  });
7819
8129
  return {
7820
- config,
7821
8130
  database,
7822
8131
  auth
7823
8132
  };
7824
8133
  }
8134
+ async function createMigratedNodeCloudDatabase(environment) {
8135
+ const config = await loadNodeCloudServerConfig(environment);
8136
+ return {
8137
+ config,
8138
+ ...await migratedNodeCloudDatabase(config)
8139
+ };
8140
+ }
7825
8141
  //#endregion
7826
8142
  //#region src/runtime/s3/objects.ts
7827
8143
  const CHECKSUM_METADATA_KEY = "openpencil-sha256";
@@ -8005,6 +8321,29 @@ function createS3ObjectStore(config) {
8005
8321
  Bucket: config.s3Bucket,
8006
8322
  Key: key
8007
8323
  }));
8324
+ },
8325
+ async write(input) {
8326
+ await client.send(new PutObjectCommand({
8327
+ Bucket: config.s3Bucket,
8328
+ Key: input.key,
8329
+ Body: input.bytes,
8330
+ ContentType: input.contentType,
8331
+ ChecksumSHA256: nativeSHA256 ? input.checksum : void 0,
8332
+ Metadata: { [CHECKSUM_METADATA_KEY]: input.checksum },
8333
+ ...encryption(config)
8334
+ }));
8335
+ },
8336
+ async read(key) {
8337
+ try {
8338
+ const object = await client.send(new GetObjectCommand({
8339
+ Bucket: config.s3Bucket,
8340
+ Key: key
8341
+ }));
8342
+ return object.Body ? await object.Body.transformToByteArray() : null;
8343
+ } catch (error) {
8344
+ if (isNotFoundError(error)) return null;
8345
+ throw error;
8346
+ }
8008
8347
  }
8009
8348
  };
8010
8349
  }
@@ -8139,6 +8478,43 @@ function attachCollaborationRelay(server, relay, maximumMessageBytes) {
8139
8478
  } };
8140
8479
  }
8141
8480
  //#endregion
8481
+ //#region src/runtime/posthog/errors.ts
8482
+ /**
8483
+ * Reports server errors to the PostHog project the deployment's `[error_tracking]` names, or
8484
+ * returns nothing when it names none. Each error is sent as it is caught, as a `$exception` with
8485
+ * no person profile: reports carry the runtime, the deployment, and where the error was caught,
8486
+ * never who made the request or what it carried.
8487
+ */
8488
+ function createPostHogErrorReporter(config, runtime, options = {}) {
8489
+ const tracking = config.errorTracking;
8490
+ if (!tracking) return void 0;
8491
+ const client = new PostHog(tracking.projectAPIKey, {
8492
+ host: tracking.host,
8493
+ disableGeoip: true,
8494
+ ...options
8495
+ });
8496
+ client.on("error", (failure) => {
8497
+ console.error("[Cloud] PostHog refused an error report:", failure);
8498
+ });
8499
+ const deployment = {
8500
+ cloud_runtime: runtime,
8501
+ cloud_deployment: config.deployment,
8502
+ cloud_host: new URL(config.publicURL).host
8503
+ };
8504
+ return async (error, context) => {
8505
+ try {
8506
+ await client.captureExceptionImmediate(error, void 0, {
8507
+ ...deployment,
8508
+ cloud_source: context.source,
8509
+ http_method: context.method,
8510
+ http_route: context.route
8511
+ });
8512
+ } catch (failure) {
8513
+ console.error("[Cloud] Reporting an error failed:", failure);
8514
+ }
8515
+ };
8516
+ }
8517
+ //#endregion
8142
8518
  //#region src/runtime/site/server.ts
8143
8519
  const PORTAL_PAGE = "/cloud.html";
8144
8520
  const EDITOR_PAGE = "/index.html";
@@ -8158,8 +8534,8 @@ function cacheControl(path) {
8158
8534
  * everywhere else, and, where the API has nothing, the site's files, or the editor's
8159
8535
  * `index.html` for any other page, whose router knows the editor's own addresses.
8160
8536
  */
8161
- function createCloudServer(api, { site, indexingPolicy }) {
8162
- const server = new Hono();
8537
+ function createCloudServer(api, { site, indexingPolicy, reportError }) {
8538
+ const server = new Hono().onError(cloudErrorHandler(reportError));
8163
8539
  server.use(async (c, next) => {
8164
8540
  await next();
8165
8541
  c.res = withIndexingPolicy(c.res, c.req.path, indexingPolicy);
@@ -8256,9 +8632,24 @@ function siteFiles(environment) {
8256
8632
  if (siteDirectory) return nodeSiteFiles(siteDirectory);
8257
8633
  console.warn("[Cloud] OPENPENCIL_CLOUD_SITE_DIR is not set; the editor and sign-in pages are off.");
8258
8634
  }
8635
+ /**
8636
+ * Starts the server. Once the config parses, a startup that fails, such as a migration, a signing
8637
+ * key, or a port that cannot be bound, is reported before it is thrown; a config that does not
8638
+ * parse has no error tracking to report to.
8639
+ */
8259
8640
  async function startNodeCloudServer(options = {}) {
8260
8641
  const environment = options.environment ?? process.env;
8261
- const { config, database } = await createMigratedNodeCloudDatabase(environment);
8642
+ const config = await loadNodeCloudServerConfig(environment);
8643
+ const reportError = createPostHogErrorReporter(config, "node");
8644
+ try {
8645
+ return await startConfiguredServer(config, environment, options, reportError);
8646
+ } catch (error) {
8647
+ await reportCloudError(reportError, "Starting the server failed:", error, { source: "setup" });
8648
+ throw error;
8649
+ }
8650
+ }
8651
+ async function startConfiguredServer(config, environment, options, reportError) {
8652
+ const { database } = await migratedNodeCloudDatabase(config);
8262
8653
  const aiSigningKey = await loadAISigningKey(config).catch(async (error) => {
8263
8654
  await database.destroy();
8264
8655
  throw error;
@@ -8270,13 +8661,14 @@ async function startNodeCloudServer(options = {}) {
8270
8661
  const cleanup = config.cleanupEnabled ? startCleanupWorker(createCleanupServices(config, database, objects, policy), {
8271
8662
  ...cleanupPassOptions(config),
8272
8663
  intervalMs: config.cleanupIntervalMs,
8273
- onError: (error) => console.error("[Cloud] Cleanup worker failed:", error)
8664
+ onError: (error) => void reportCloudError(reportError, "Cleanup worker failed:", error, { source: "cleanup" })
8274
8665
  }) : void 0;
8275
8666
  const relay = createCollaborationRelay({
8276
8667
  authSecret: config.authSecret,
8277
- store: createCollaborationStateStore(database),
8668
+ store: createCollaborationStateStore(database, objects),
8278
8669
  maximumMessageBytes: config.technicalLimits.maximumCollaborationMessageBytes,
8279
8670
  maximumConnectionsPerRoom: config.technicalLimits.maximumConnectionsPerRoom,
8671
+ onError: (message, error) => void reportCloudError(reportError, message, error, { source: "relay" }),
8280
8672
  ...relayDocumentLookups(database, policy, config.deployment)
8281
8673
  });
8282
8674
  const app = createCloudApp({
@@ -8290,18 +8682,20 @@ async function startNodeCloudServer(options = {}) {
8290
8682
  aiSigningKey,
8291
8683
  relayURL: config.relayURL ?? defaultRelayURL(config.publicURL),
8292
8684
  onDocumentRestored: (documentId) => relay.startOver(documentId),
8293
- onDocumentVersionsChanged: (documentId) => relay.announceVersions(documentId)
8685
+ onDocumentVersionsChanged: (documentId) => relay.announceVersions(documentId),
8686
+ reportError
8294
8687
  });
8295
8688
  const emailWorker = emailTransport ? startTransactionalEmailWorker(email, {
8296
8689
  batchSize: config.emailBatchSize,
8297
8690
  intervalMs: config.emailIntervalMs,
8298
8691
  leaseDurationMs: config.emailLeaseDurationMs,
8299
8692
  maximumAttempts: config.emailMaximumAttempts,
8300
- onError: (error) => console.error("[Cloud] Email worker failed:", error)
8693
+ onError: (error) => void reportCloudError(reportError, "Email worker failed:", error, { source: "email" })
8301
8694
  }) : void 0;
8302
8695
  const cloudServer = createCloudServer(app, {
8303
8696
  site: siteFiles(environment),
8304
- indexingPolicy: config.indexingPolicy
8697
+ indexingPolicy: config.indexingPolicy,
8698
+ reportError
8305
8699
  });
8306
8700
  const server = await listen(async (request) => cloudServer.fetch(request), options.port ?? Number(environment.PORT ?? 8787), environment.HOST ?? "0.0.0.0");
8307
8701
  const relayListener = attachCollaborationRelay(server.http, relay, config.technicalLimits.maximumCollaborationMessageBytes);