@terreno/api 56.0.0-beta.0 → 56.0.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 (56) hide show
  1. package/CHANGELOG.md +2 -122
  2. package/LICENSE +21 -202
  3. package/dist/api.d.ts +3 -2
  4. package/dist/auth.js +21 -25
  5. package/dist/betterAuthSetup.d.ts +15 -1
  6. package/dist/betterAuthSetup.js +4 -6
  7. package/dist/configurationPlugin.js +2 -2
  8. package/dist/expressServer.js +11 -5
  9. package/dist/githubAuth.js +1 -4
  10. package/dist/plugins.d.ts +7 -6
  11. package/dist/plugins.js +24 -8
  12. package/dist/realtime/realtimeApp.d.ts +1 -1
  13. package/dist/realtime/realtimeApp.js +16 -8
  14. package/dist/realtime/types.d.ts +14 -0
  15. package/dist/sync/executors.js +14 -14
  16. package/dist/sync/models.js +1 -1
  17. package/dist/sync/registry.js +1 -1
  18. package/dist/sync/syncSeqPlugin.js +25 -29
  19. package/dist/tests/models.d.ts +27 -11
  20. package/dist/tests/models.js +5 -3
  21. package/dist/tests/testHelper.d.ts +9 -3
  22. package/package.json +5 -6
  23. package/src/actions.test.ts +19 -12
  24. package/src/actions.ts +2 -2
  25. package/src/api.coverage.test.ts +7 -9
  26. package/src/api.errorPaths.test.ts +47 -1
  27. package/src/api.errors.test.ts +32 -23
  28. package/src/api.ts +22 -12
  29. package/src/auth.test.ts +251 -1
  30. package/src/auth.ts +25 -23
  31. package/src/betterAuthSetup.ts +7 -7
  32. package/src/configuration.test.ts +59 -38
  33. package/src/configurationPlugin.test.ts +72 -42
  34. package/src/configurationPlugin.ts +2 -2
  35. package/src/consentApp.test.ts +12 -12
  36. package/src/expressServer.ts +11 -5
  37. package/src/githubAuth.ts +1 -3
  38. package/src/middleware.test.ts +16 -0
  39. package/src/models/versionConfig.ts +1 -1
  40. package/src/openApiBuilder.test.ts +24 -9
  41. package/src/openApiValidator.test.ts +41 -0
  42. package/src/permissions.test.ts +35 -25
  43. package/src/plugins.test.ts +24 -0
  44. package/src/plugins.ts +29 -11
  45. package/src/populate.test.ts +25 -16
  46. package/src/realtime/realtime.test.ts +239 -0
  47. package/src/realtime/realtimeApp.ts +15 -8
  48. package/src/realtime/realtimeAppSetup.test.ts +159 -0
  49. package/src/realtime/types.ts +14 -0
  50. package/src/secretProvidersGcpClient.test.ts +66 -0
  51. package/src/sync/executors.ts +16 -14
  52. package/src/sync/models.ts +2 -1
  53. package/src/sync/registry.ts +1 -1
  54. package/src/sync/syncFrontier.test.ts +2 -2
  55. package/src/sync/syncSeqPlugin.ts +43 -51
  56. package/src/tests/models.ts +8 -6
@@ -52,11 +52,11 @@ let insertManyProbe: (() => Promise<void>) | null = null;
52
52
 
53
53
  // Registered AFTER syncPlugin so it runs once the batch's seqs are claimed but before
54
54
  // the documents commit — the exact window a snapshot must not page across (C1).
55
- frontierSchema.pre("insertMany", async (next: any) => {
55
+ // Mongoose 9: pre hooks no longer receive `next` — use async/await only.
56
+ frontierSchema.pre("insertMany", async () => {
56
57
  if (insertManyProbe) {
57
58
  await insertManyProbe();
58
59
  }
59
- next();
60
60
  });
61
61
 
62
62
  // Registered AFTER syncPlugin so a save can fail once the seq (and, before the fix, the
@@ -1,14 +1,7 @@
1
1
  // noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
2
2
  // biome-ignore-all lint/suspicious/noExplicitAny: Schema/Query generics must be loose to accept arbitrary consumer schemas
3
- import type {
4
- CallbackWithoutResultAndOptionalError,
5
- ClientSession,
6
- Model,
7
- Query,
8
- Schema,
9
- } from "mongoose";
3
+ import type {ClientSession, Model, Query, Schema} from "mongoose";
10
4
  import {logger} from "../logger";
11
- import {isVersionError} from "./executors";
12
5
  import {claimSyncSeqs, confirmSyncSeqs, releaseSyncSeqs, SyncScopeMove} from "./models";
13
6
  import {findSyncEntryByModelName, type SyncRegistryEntry} from "./registry";
14
7
  import {
@@ -442,70 +435,69 @@ export const syncPlugin = (schema: Schema<any, any, any, any>): void => {
442
435
  this.$locals[INITIAL_STREAM_KEY] = pending.currentStream;
443
436
  });
444
437
 
445
- // Task 9.13: an optimistic-concurrency loss (`VersionError`) matched no document, so the
446
- // seq claimed in pre('save') will never be stamped on anything. Cancel it instead of
447
- // letting it hold the stable frontier down for the full pending-claim lease — a lost
448
- // update race is now an EXPECTED outcome, not a crash, and must not stall catch-up for
449
- // every other client on the stream.
438
+ // Any save that claimed a seq in pre('save') but then failed (VersionError, E11000,
439
+ // simulated/hook failure, etc.) never stamped that seq. Release it so the stable
440
+ // frontier is not held for the full pending-claim lease.
441
+ //
442
+ // Mongoose 9 / Kareem 3: async error middleware must `throw error` (or return a
443
+ // rejected promise). Calling `next(error)` after an async hook returns a Promise
444
+ // leaves an orphaned rejection that Bun reports as an unhandled error between tests,
445
+ // even when the caller correctly awaited `save().catch(...)`.
446
+ // Keep arity ≥ 3 so Kareem still classifies this as error middleware.
450
447
  schema.post(
451
448
  "save",
452
- async function (error: any, doc: any, next: (err?: Error) => void): Promise<void> {
449
+ async function (error: any, doc: any, _next: (err?: Error) => void): Promise<void> {
453
450
  const locals = (this?.$locals ?? doc?.$locals) as Record<string, unknown> | undefined;
454
451
  const pending = locals?.[PENDING_COMMIT_KEY] as PendingSaveCommit | undefined;
455
452
  if (locals) {
456
453
  locals[PENDING_COMMIT_KEY] = undefined;
457
454
  }
458
- if (pending?.claim.registered && isVersionError(error)) {
455
+ if (pending?.claim.registered) {
459
456
  await releaseSyncSeqs({seqs: pending.claim.seqs, stream: pending.currentStream}).catch(
460
457
  (releaseError: unknown) => {
461
- logger.error("[sync] Failed to release the seq claim of a version-conflicted save", {
458
+ logger.error("[sync] Failed to release seq claim after failed save", {
462
459
  error: String(releaseError),
463
460
  stream: pending.currentStream,
464
461
  });
465
462
  }
466
463
  );
467
464
  }
468
- next(error);
465
+ throw error;
469
466
  }
470
467
  );
471
468
 
469
+ // Mongoose 9 insertMany pre hooks are async (no `next` callback) and receive docs + options.
472
470
  schema.pre(
473
471
  "insertMany",
474
- async function (
475
- next: CallbackWithoutResultAndOptionalError,
476
- docs: Record<string, unknown>[],
477
- options?: {session?: ClientSession | null}
478
- ) {
479
- try {
480
- const model = this as unknown as Model<any>;
481
- const entry = findSyncEntryByModelName(model.modelName);
482
- if (!entry || !Array.isArray(docs) || docs.length === 0) {
483
- return next();
484
- }
485
- const session = options?.session ?? null;
486
- const byStream = new Map<string, Record<string, unknown>[]>();
487
- for (const doc of docs) {
488
- const stream = writableStreamForObject(entry, doc);
489
- const group = byStream.get(stream) ?? [];
490
- group.push(doc);
491
- byStream.set(stream, group);
492
- }
493
- for (const [stream, group] of byStream) {
494
- // The claim joins the caller's session so a transactional insertMany gets true
495
- // counter+write atomicity (and registers no pending entry to confirm).
496
- const claim = await claimSyncSeqs({count: group.length, session, stream});
497
- group.forEach((doc, index) => {
498
- doc._syncPrevStream = null;
499
- doc._syncSeq = claim.seqs[index];
500
- });
501
- }
502
- // C1: the confirm belongs in post('insertMany') — confirming here would let the
503
- // stable frontier (and therefore a snapshot cursor) advance past seqs whose
504
- // documents have not committed yet, stranding them below every catch-up cursor.
505
- return next();
506
- } catch (error: unknown) {
507
- return next(error as Error);
472
+ async function (docs: any | Array<any>, options?: {session?: ClientSession | null}) {
473
+ const docsArray = Array.isArray(docs)
474
+ ? (docs as Record<string, unknown>[])
475
+ : [docs as Record<string, unknown>];
476
+ const model = this as unknown as Model<any>;
477
+ const entry = findSyncEntryByModelName(model.modelName);
478
+ if (!entry || docsArray.length === 0) {
479
+ return;
480
+ }
481
+ const session = options?.session ?? null;
482
+ const byStream = new Map<string, Record<string, unknown>[]>();
483
+ for (const doc of docsArray) {
484
+ const stream = writableStreamForObject(entry, doc);
485
+ const group = byStream.get(stream) ?? [];
486
+ group.push(doc);
487
+ byStream.set(stream, group);
488
+ }
489
+ for (const [stream, group] of byStream) {
490
+ // The claim joins the caller's session so a transactional insertMany gets true
491
+ // counter+write atomicity (and registers no pending entry to confirm).
492
+ const claim = await claimSyncSeqs({count: group.length, session, stream});
493
+ group.forEach((doc, index) => {
494
+ doc._syncPrevStream = null;
495
+ doc._syncSeq = claim.seqs[index];
496
+ });
508
497
  }
498
+ // C1: the confirm belongs in post('insertMany') — confirming here would let the
499
+ // stable frontier (and therefore a snapshot cursor) advance past seqs whose
500
+ // documents have not committed yet, stranding them below every catch-up cursor.
509
501
  }
510
502
  );
511
503
 
@@ -1,4 +1,4 @@
1
- import mongoose, {type Model, model, Schema} from "mongoose";
1
+ import mongoose, {type Model, model, Schema, type StringSchemaDefinition} from "mongoose";
2
2
  import passportLocalMongoose from "passport-local-mongoose";
3
3
 
4
4
  import {createdUpdatedPlugin, DateOnly, isDisabledPlugin} from "../plugins";
@@ -41,11 +41,11 @@ export interface Food {
41
41
  dateAdded?: string;
42
42
  };
43
43
  tags: string[];
44
- eatenBy: [Schema.Types.ObjectId | User];
44
+ eatenBy: Array<mongoose.Types.ObjectId | User>;
45
45
  lastEatenWith: {[name: string]: Date};
46
46
  categories: FoodCategory[];
47
47
  expiration: string;
48
- likesIds: {userId: string; likes: boolean}[];
48
+ likesIds: {userId: mongoose.Types.ObjectId | string; likes: boolean}[];
49
49
  }
50
50
 
51
51
  export interface RequiredField {
@@ -126,9 +126,11 @@ const foodSchema = new Schema<Food>(
126
126
  type: Schema.Types.ObjectId,
127
127
  },
128
128
  ],
129
- // noExplicitAny: DateOnly is a custom SchemaType not recognized by Mongoose's built-in type definitions
130
- // biome-ignore lint/suspicious/noExplicitAny: DateOnly is a custom SchemaType not recognized by Mongoose's built-in type definitions
131
- expiration: {description: "Expiration date of the food", type: DateOnly as any},
129
+ expiration: {
130
+ description: "Expiration date of the food",
131
+ // DateOnly is a custom SchemaType registered on Schema.Types; it stores an ISO date string.
132
+ type: DateOnly as unknown as StringSchemaDefinition,
133
+ },
132
134
  hidden: {
133
135
  default: false,
134
136
  description: "Whether this food is hidden from listings",