@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.
- package/CHANGELOG.md +2 -122
- package/LICENSE +21 -202
- package/dist/api.d.ts +3 -2
- package/dist/auth.js +21 -25
- package/dist/betterAuthSetup.d.ts +15 -1
- package/dist/betterAuthSetup.js +4 -6
- package/dist/configurationPlugin.js +2 -2
- package/dist/expressServer.js +11 -5
- package/dist/githubAuth.js +1 -4
- package/dist/plugins.d.ts +7 -6
- package/dist/plugins.js +24 -8
- package/dist/realtime/realtimeApp.d.ts +1 -1
- package/dist/realtime/realtimeApp.js +16 -8
- package/dist/realtime/types.d.ts +14 -0
- package/dist/sync/executors.js +14 -14
- package/dist/sync/models.js +1 -1
- package/dist/sync/registry.js +1 -1
- package/dist/sync/syncSeqPlugin.js +25 -29
- package/dist/tests/models.d.ts +27 -11
- package/dist/tests/models.js +5 -3
- package/dist/tests/testHelper.d.ts +9 -3
- package/package.json +5 -6
- package/src/actions.test.ts +19 -12
- package/src/actions.ts +2 -2
- package/src/api.coverage.test.ts +7 -9
- package/src/api.errorPaths.test.ts +47 -1
- package/src/api.errors.test.ts +32 -23
- package/src/api.ts +22 -12
- package/src/auth.test.ts +251 -1
- package/src/auth.ts +25 -23
- package/src/betterAuthSetup.ts +7 -7
- package/src/configuration.test.ts +59 -38
- package/src/configurationPlugin.test.ts +72 -42
- package/src/configurationPlugin.ts +2 -2
- package/src/consentApp.test.ts +12 -12
- package/src/expressServer.ts +11 -5
- package/src/githubAuth.ts +1 -3
- package/src/middleware.test.ts +16 -0
- package/src/models/versionConfig.ts +1 -1
- package/src/openApiBuilder.test.ts +24 -9
- package/src/openApiValidator.test.ts +41 -0
- package/src/permissions.test.ts +35 -25
- package/src/plugins.test.ts +24 -0
- package/src/plugins.ts +29 -11
- package/src/populate.test.ts +25 -16
- package/src/realtime/realtime.test.ts +239 -0
- package/src/realtime/realtimeApp.ts +15 -8
- package/src/realtime/realtimeAppSetup.test.ts +159 -0
- package/src/realtime/types.ts +14 -0
- package/src/secretProvidersGcpClient.test.ts +66 -0
- package/src/sync/executors.ts +16 -14
- package/src/sync/models.ts +2 -1
- package/src/sync/registry.ts +1 -1
- package/src/sync/syncFrontier.test.ts +2 -2
- package/src/sync/syncSeqPlugin.ts +43 -51
- 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
|
-
|
|
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
|
-
//
|
|
446
|
-
//
|
|
447
|
-
//
|
|
448
|
-
//
|
|
449
|
-
//
|
|
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,
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
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
|
|
package/src/tests/models.ts
CHANGED
|
@@ -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:
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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",
|