mindoodb 0.1.7 → 0.1.9
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/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts +2 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js +9 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js.map +1 -1
- package/dist/browser/index.mjs +9 -0
- package/dist/core/BaseMindooDB.d.ts +25 -0
- package/dist/core/BaseMindooDB.d.ts.map +1 -1
- package/dist/core/BaseMindooDB.js +187 -9
- package/dist/core/BaseMindooDB.js.map +1 -1
- package/dist/core/BaseMindooTenant.d.ts +39 -0
- package/dist/core/BaseMindooTenant.d.ts.map +1 -1
- package/dist/core/BaseMindooTenant.js +169 -0
- package/dist/core/BaseMindooTenant.js.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.d.ts +25 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.js +140 -3
- package/dist/core/BaseMindooTenantDirectory.js.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.d.ts.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.js +3 -0
- package/dist/core/accesscontrol/directoryProjection.js.map +1 -1
- package/dist/core/accesscontrol/types.d.ts +13 -2
- package/dist/core/accesscontrol/types.d.ts.map +1 -1
- package/dist/core/accesscontrol/types.js +6 -0
- package/dist/core/accesscontrol/types.js.map +1 -1
- package/dist/core/applyChangesInSlices.d.ts +28 -0
- package/dist/core/applyChangesInSlices.d.ts.map +1 -0
- package/dist/core/applyChangesInSlices.js +35 -0
- package/dist/core/applyChangesInSlices.js.map +1 -0
- package/dist/core/builtinDbInvariants.d.ts +13 -2
- package/dist/core/builtinDbInvariants.d.ts.map +1 -1
- package/dist/core/builtinDbInvariants.js +15 -3
- package/dist/core/builtinDbInvariants.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +8 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/personalShares.d.ts +77 -0
- package/dist/core/personalShares.d.ts.map +1 -0
- package/dist/core/personalShares.js +159 -0
- package/dist/core/personalShares.js.map +1 -0
- package/dist/core/types.d.ts +42 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/userkeys/depositKeys.d.ts +60 -0
- package/dist/core/userkeys/depositKeys.d.ts.map +1 -0
- package/dist/core/userkeys/depositKeys.js +80 -0
- package/dist/core/userkeys/depositKeys.js.map +1 -0
- package/dist/core/userkeys/index.d.ts +1 -0
- package/dist/core/userkeys/index.d.ts.map +1 -1
- package/dist/core/userkeys/index.js +6 -1
- package/dist/core/userkeys/index.js.map +1 -1
- package/dist/server/TenantManager.d.ts.map +1 -1
- package/dist/server/TenantManager.js +2 -2
- package/dist/server/TenantManager.js.map +1 -1
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@ exports.BaseMindooDB = exports.CUSTOM_DOC_ID_INITIAL_CHANGE_BYTES = void 0;
|
|
|
6
6
|
// Browser/Node.js: WASM (@automerge/automerge/slim)
|
|
7
7
|
const MindooFilingStore_1 = require("./filing/MindooFilingStore");
|
|
8
8
|
const automerge_adapter_1 = require("./automerge-adapter");
|
|
9
|
+
const applyChangesInSlices_1 = require("./applyChangesInSlices");
|
|
9
10
|
const ChangeDocDraft_1 = require("./ChangeDocDraft");
|
|
10
11
|
const values_1 = require("./values");
|
|
11
12
|
const storeEntryTime_1 = require("./storeEntryTime");
|
|
@@ -38,6 +39,8 @@ const databaseIdValidation_1 = require("./databaseIdValidation");
|
|
|
38
39
|
const builtinDbInvariants_1 = require("./builtinDbInvariants");
|
|
39
40
|
const recipients_1 = require("./userkeys/recipients");
|
|
40
41
|
const sealedTypes_1 = require("./userkeys/sealedTypes");
|
|
42
|
+
const depositKeys_1 = require("./userkeys/depositKeys");
|
|
43
|
+
const fingerprint_1 = require("./userkeys/fingerprint");
|
|
41
44
|
const DocumentSummaryStore_1 = require("./indexing/summary/DocumentSummaryStore");
|
|
42
45
|
const types_6 = require("./indexing/summary/types");
|
|
43
46
|
const DocumentFullTextIndex_1 = require("./indexing/fulltext/DocumentFullTextIndex");
|
|
@@ -645,6 +648,13 @@ class BaseMindooDB {
|
|
|
645
648
|
* next {@link reconcileGroupKeyAccess} re-runs the key visibility reconcile.
|
|
646
649
|
*/
|
|
647
650
|
this.groupKeyAccessChanged = new Set();
|
|
651
|
+
/** Deposit key documents seen in this pass (loaded at its end, see reconcileDepositKeys). */
|
|
652
|
+
this.depositKeyDocsSeen = new Set();
|
|
653
|
+
/** Deposit keys this database registered with the tenant: fingerprint -> group key id. */
|
|
654
|
+
this.depositKeysRegistered = new Map();
|
|
655
|
+
/** Fingerprints without a readable deposit key document, by time of the miss (ms). */
|
|
656
|
+
this.depositKeyMisses = new Map();
|
|
657
|
+
this.loadingDepositKeys = false;
|
|
648
658
|
/** Per document: whether its origin decrypted with the partial series held then. */
|
|
649
659
|
this.partialSeriesProbes = new Map();
|
|
650
660
|
/** Group key series last probed without success, by key id (ms), see ingestGroupKey. */
|
|
@@ -5293,9 +5303,21 @@ class BaseMindooDB {
|
|
|
5293
5303
|
await this.tenant.ingestSealedFromEntries(keyId, entries);
|
|
5294
5304
|
// A document encrypted with a group document's key series (`groupKeyDocId`)
|
|
5295
5305
|
// carries no recipient block of its own: the wraps are on the group document.
|
|
5296
|
-
|
|
5306
|
+
const newestBlock = (0, sealedTypes_1.isSealedKeyId)(keyId) ? (0, recipients_1.newestRecipientBlock)(entries) : undefined;
|
|
5307
|
+
if ((0, sealedTypes_1.isSealedKeyId)(keyId) && !newestBlock) {
|
|
5297
5308
|
await this.ingestGroupKey(keyId);
|
|
5298
5309
|
}
|
|
5310
|
+
// A delivery sealed to a deposit key: open it with the key's private half
|
|
5311
|
+
// when this replica can read the deposit key document (userkeys/depositKeys.ts).
|
|
5312
|
+
if (newestBlock && !(await this.tenant.hasDecryptionKey(keyId))) {
|
|
5313
|
+
if (await this.loadDepositKeysFor(newestBlock)) {
|
|
5314
|
+
await this.tenant.ingestSealedFromEntries(keyId, entries);
|
|
5315
|
+
}
|
|
5316
|
+
}
|
|
5317
|
+
const entryDocId = entries[0]?.docId;
|
|
5318
|
+
if (entryDocId && (0, depositKeys_1.isDepositKeyDocId)(entryDocId)) {
|
|
5319
|
+
this.depositKeyDocsSeen.add(entryDocId);
|
|
5320
|
+
}
|
|
5299
5321
|
// A series that does not reach back to epoch 1 may be completed from the
|
|
5300
5322
|
// key document's older recipient blocks; `entries` is often just a batch.
|
|
5301
5323
|
if ((0, sealedTypes_1.isSealedKeyId)(keyId) && this.tenant.needsDeepSealedIngest?.(keyId)) {
|
|
@@ -5318,6 +5340,121 @@ class BaseMindooDB {
|
|
|
5318
5340
|
this.groupKeyAccessChanged.add(keyId);
|
|
5319
5341
|
}
|
|
5320
5342
|
}
|
|
5343
|
+
/**
|
|
5344
|
+
* For a sealed block this replica could not open: registers the deposit keys
|
|
5345
|
+
* named by its wraps whose documents this replica can read. True when one
|
|
5346
|
+
* was registered (the block should be tried again).
|
|
5347
|
+
*/
|
|
5348
|
+
async loadDepositKeysFor(block) {
|
|
5349
|
+
if (this.loadingDepositKeys)
|
|
5350
|
+
return false;
|
|
5351
|
+
this.loadingDepositKeys = true;
|
|
5352
|
+
try {
|
|
5353
|
+
let registered = false;
|
|
5354
|
+
for (const wrap of (0, sealedTypes_1.allRecipientWraps)(block)) {
|
|
5355
|
+
if (wrap.kind !== "device")
|
|
5356
|
+
continue;
|
|
5357
|
+
const fingerprint = wrap.keyFingerprint;
|
|
5358
|
+
if (this.tenant.hasDepositUnwrapKey(fingerprint))
|
|
5359
|
+
continue;
|
|
5360
|
+
const missed = this.depositKeyMisses.get(fingerprint);
|
|
5361
|
+
if (missed !== undefined && Date.now() - missed < BaseMindooDB.DEPOSIT_KEY_MISS_TTL_MS)
|
|
5362
|
+
continue;
|
|
5363
|
+
if (await this.registerDepositKeyDoc((0, depositKeys_1.depositKeyDocId)(fingerprint))) {
|
|
5364
|
+
registered = true;
|
|
5365
|
+
}
|
|
5366
|
+
else {
|
|
5367
|
+
this.depositKeyMisses.set(fingerprint, Date.now());
|
|
5368
|
+
}
|
|
5369
|
+
}
|
|
5370
|
+
return registered;
|
|
5371
|
+
}
|
|
5372
|
+
finally {
|
|
5373
|
+
this.loadingDepositKeys = false;
|
|
5374
|
+
}
|
|
5375
|
+
}
|
|
5376
|
+
/** Reads a deposit key document and registers its private half; false when not readable. */
|
|
5377
|
+
async registerDepositKeyDoc(docId) {
|
|
5378
|
+
const metas = await this.scanAllMetadata(this.store, { docId });
|
|
5379
|
+
const create = metas.find((meta) => meta.entryType === "doc_create");
|
|
5380
|
+
if (!create)
|
|
5381
|
+
return null;
|
|
5382
|
+
const groupKeyId = create.decryptionKeyId;
|
|
5383
|
+
if (!(0, sealedTypes_1.isSealedKeyId)(groupKeyId))
|
|
5384
|
+
return null;
|
|
5385
|
+
await this.ingestGroupKey(groupKeyId);
|
|
5386
|
+
if (!(await this.tenant.hasDecryptionKey(groupKeyId)))
|
|
5387
|
+
return null;
|
|
5388
|
+
let data;
|
|
5389
|
+
try {
|
|
5390
|
+
const internalDoc = await this.loadDocumentInternal(docId);
|
|
5391
|
+
if (!internalDoc || internalDoc.isDeleted)
|
|
5392
|
+
return null;
|
|
5393
|
+
data = this.wrapDocument(internalDoc).getData();
|
|
5394
|
+
}
|
|
5395
|
+
catch {
|
|
5396
|
+
return null;
|
|
5397
|
+
}
|
|
5398
|
+
// The key must belong to the group whose series encrypts its document.
|
|
5399
|
+
const info = (0, depositKeys_1.depositKeyInfoOf)(docId, data);
|
|
5400
|
+
if (!info || (0, sealedTypes_1.sealedKeyId)(info.groupDocId) !== groupKeyId)
|
|
5401
|
+
return null;
|
|
5402
|
+
const imported = await (0, depositKeys_1.importDepositPrivateKey)(this.tenant.getCryptoAdapter(), docId, data).catch(() => null);
|
|
5403
|
+
if (!imported)
|
|
5404
|
+
return null;
|
|
5405
|
+
this.tenant.registerDepositUnwrapKey(imported.info.fingerprint, imported.privateKey);
|
|
5406
|
+
this.depositKeysRegistered.set(imported.info.fingerprint, groupKeyId);
|
|
5407
|
+
this.depositKeyMisses.delete(imported.info.fingerprint);
|
|
5408
|
+
return imported.info.fingerprint;
|
|
5409
|
+
}
|
|
5410
|
+
/**
|
|
5411
|
+
* End of a pass: registers deposit keys that became readable and gives up
|
|
5412
|
+
* those of groups this replica left, then reveals or hides the deliveries
|
|
5413
|
+
* sealed to them.
|
|
5414
|
+
*/
|
|
5415
|
+
async reconcileDepositKeys() {
|
|
5416
|
+
const changed = new Set();
|
|
5417
|
+
if (this.depositKeyDocsSeen.size > 0) {
|
|
5418
|
+
const seen = [...this.depositKeyDocsSeen];
|
|
5419
|
+
this.depositKeyDocsSeen.clear();
|
|
5420
|
+
for (const docId of seen) {
|
|
5421
|
+
const known = [...this.depositKeysRegistered.keys()].some((fp) => (0, depositKeys_1.depositKeyDocId)(fp) === docId);
|
|
5422
|
+
if (known)
|
|
5423
|
+
continue;
|
|
5424
|
+
const fingerprint = await this.registerDepositKeyDoc(docId);
|
|
5425
|
+
if (fingerprint)
|
|
5426
|
+
changed.add(fingerprint);
|
|
5427
|
+
}
|
|
5428
|
+
}
|
|
5429
|
+
for (const [fingerprint, groupKeyId] of [...this.depositKeysRegistered]) {
|
|
5430
|
+
if (await this.tenant.hasDecryptionKey(groupKeyId))
|
|
5431
|
+
continue;
|
|
5432
|
+
this.depositKeysRegistered.delete(fingerprint);
|
|
5433
|
+
this.tenant.unregisterDepositUnwrapKey(fingerprint);
|
|
5434
|
+
changed.add(fingerprint);
|
|
5435
|
+
}
|
|
5436
|
+
if (changed.size === 0)
|
|
5437
|
+
return;
|
|
5438
|
+
// The deliveries sealed to these keys: their key series follow the deposit key.
|
|
5439
|
+
const keyIds = new Set();
|
|
5440
|
+
for (const meta of await this.scanAllMetadata(this.store)) {
|
|
5441
|
+
if (meta.entryType !== "doc_create" || !(0, sealedTypes_1.isSealedKeyId)(meta.decryptionKeyId))
|
|
5442
|
+
continue;
|
|
5443
|
+
const wraps = (0, sealedTypes_1.allRecipientWraps)(meta.recipients);
|
|
5444
|
+
if (wraps.some((wrap) => changed.has(wrap.keyFingerprint)))
|
|
5445
|
+
keyIds.add(meta.decryptionKeyId);
|
|
5446
|
+
}
|
|
5447
|
+
if (keyIds.size === 0)
|
|
5448
|
+
return;
|
|
5449
|
+
for (const keyId of keyIds) {
|
|
5450
|
+
// Given up: drop the cached series so the wraps decide again.
|
|
5451
|
+
const lost = [...changed].some((fp) => !this.depositKeysRegistered.has(fp));
|
|
5452
|
+
if (lost)
|
|
5453
|
+
this.tenant.forgetSealedSeries(keyId);
|
|
5454
|
+
}
|
|
5455
|
+
this.groupKeyMisses.clear();
|
|
5456
|
+
await this.reconcileKeyVisibilityOf(keyIds);
|
|
5457
|
+
}
|
|
5321
5458
|
/**
|
|
5322
5459
|
* Whether this replica can read a document of key `keyId`. Usually the key id
|
|
5323
5460
|
* alone decides; a reader added to a group without its history
|
|
@@ -5392,6 +5529,8 @@ class BaseMindooDB {
|
|
|
5392
5529
|
* almost every sync batch.
|
|
5393
5530
|
*/
|
|
5394
5531
|
async reconcileGroupKeyAccess() {
|
|
5532
|
+
if (!this.isTimeTravelMode())
|
|
5533
|
+
await this.reconcileDepositKeys();
|
|
5395
5534
|
if (this.groupKeyAccessChanged.size === 0 || this.isTimeTravelMode())
|
|
5396
5535
|
return;
|
|
5397
5536
|
const keyIds = new Set(this.groupKeyAccessChanged);
|
|
@@ -6056,8 +6195,7 @@ class BaseMindooDB {
|
|
|
6056
6195
|
if (changes.length === 0) {
|
|
6057
6196
|
return doc;
|
|
6058
6197
|
}
|
|
6059
|
-
|
|
6060
|
-
return applied;
|
|
6198
|
+
return (0, applyChangesInSlices_1.applyChangesInSlices)(doc, changes);
|
|
6061
6199
|
}
|
|
6062
6200
|
/** Touch the revision-feed LRU, moving `docId` to most-recently-used. */
|
|
6063
6201
|
getRevisionFeedCache(docId) {
|
|
@@ -11106,9 +11244,8 @@ class BaseMindooDB {
|
|
|
11106
11244
|
// This happens when the document has been wrapped and returned to the user
|
|
11107
11245
|
// Automerge marks documents as outdated when they're accessed, preventing direct mutation
|
|
11108
11246
|
const clonedDoc = automerge_adapter_1.Automerge.clone(cachedDoc.doc);
|
|
11109
|
-
// Apply
|
|
11110
|
-
const
|
|
11111
|
-
const updatedDoc = result[0];
|
|
11247
|
+
// Apply in bounded slices (Automerge handles dependency ordering)
|
|
11248
|
+
const updatedDoc = (0, applyChangesInSlices_1.applyChangesInSlices)(clonedDoc, changeBytes);
|
|
11112
11249
|
// Check if document actually changed
|
|
11113
11250
|
const headsAfter = automerge_adapter_1.Automerge.getHeads(updatedDoc);
|
|
11114
11251
|
const headsChanged = JSON.stringify(headsBefore) !== JSON.stringify(headsAfter);
|
|
@@ -11912,6 +12049,45 @@ class BaseMindooDB {
|
|
|
11912
12049
|
async setRecipients(doc, recipients, options) {
|
|
11913
12050
|
return this.mutateRecipients(doc, "set", recipients, options);
|
|
11914
12051
|
}
|
|
12052
|
+
async createDepositKey(groupDocId) {
|
|
12053
|
+
const { info, fields } = await (0, depositKeys_1.generateDepositKey)(this.tenant.getCryptoAdapter(), groupDocId, Date.now());
|
|
12054
|
+
await this.createDocument({
|
|
12055
|
+
id: info.docId,
|
|
12056
|
+
assumeUniqueId: true,
|
|
12057
|
+
groupKeyDocId: groupDocId,
|
|
12058
|
+
initialValues: fields,
|
|
12059
|
+
});
|
|
12060
|
+
const imported = await (0, depositKeys_1.importDepositPrivateKey)(this.tenant.getCryptoAdapter(), info.docId, fields);
|
|
12061
|
+
if (imported) {
|
|
12062
|
+
this.tenant.registerDepositUnwrapKey(info.fingerprint, imported.privateKey);
|
|
12063
|
+
this.depositKeysRegistered.set(info.fingerprint, (0, sealedTypes_1.sealedKeyId)(groupDocId));
|
|
12064
|
+
}
|
|
12065
|
+
return info;
|
|
12066
|
+
}
|
|
12067
|
+
async listDepositKeys(groupDocId) {
|
|
12068
|
+
await this.syncStoreChanges();
|
|
12069
|
+
const keys = [];
|
|
12070
|
+
for (const docId of await this.getAllDocumentIds()) {
|
|
12071
|
+
if (!docId.startsWith(depositKeys_1.DEPOSIT_KEY_DOC_PREFIX))
|
|
12072
|
+
continue;
|
|
12073
|
+
try {
|
|
12074
|
+
const doc = await this.getDocument(docId);
|
|
12075
|
+
const info = (0, depositKeys_1.depositKeyInfoOf)(docId, doc.getData());
|
|
12076
|
+
if (!info || (groupDocId && info.groupDocId !== groupDocId))
|
|
12077
|
+
continue;
|
|
12078
|
+
if (doc.getDecryptionKeyId() !== (0, sealedTypes_1.sealedKeyId)(info.groupDocId))
|
|
12079
|
+
continue;
|
|
12080
|
+
const actual = await (0, fingerprint_1.fingerprintPublicKeyPem)(info.publicKeyPem, this.tenant.getCryptoAdapter().getSubtle());
|
|
12081
|
+
if (actual !== info.fingerprint)
|
|
12082
|
+
continue;
|
|
12083
|
+
keys.push(info);
|
|
12084
|
+
}
|
|
12085
|
+
catch {
|
|
12086
|
+
// not readable
|
|
12087
|
+
}
|
|
12088
|
+
}
|
|
12089
|
+
return keys.sort((left, right) => right.createdAt - left.createdAt || left.docId.localeCompare(right.docId));
|
|
12090
|
+
}
|
|
11915
12091
|
async revealHistory(doc, recipients, options) {
|
|
11916
12092
|
return this.mutateRecipients(doc, "reveal", recipients, options);
|
|
11917
12093
|
}
|
|
@@ -13014,11 +13190,12 @@ class BaseMindooDB {
|
|
|
13014
13190
|
}
|
|
13015
13191
|
// Batch apply all changes at once - Automerge handles dependency ordering
|
|
13016
13192
|
if (changeBytes.length > 0) {
|
|
13017
|
-
this.logger.debug(`Applying ${changeBytes.length} changes to document ${docId} using
|
|
13193
|
+
this.logger.debug(`Applying ${changeBytes.length} changes to document ${docId} using applyChangesInSlices`);
|
|
13018
13194
|
try {
|
|
13019
13195
|
const automergeStartedAt = Date.now();
|
|
13020
|
-
|
|
13021
|
-
|
|
13196
|
+
// In slices: one huge batch can exhaust Automerge's memory
|
|
13197
|
+
// (see applyChangesInSlices).
|
|
13198
|
+
doc = (0, applyChangesInSlices_1.applyChangesInSlices)(doc, changeBytes);
|
|
13022
13199
|
automergeTime += Date.now() - automergeStartedAt;
|
|
13023
13200
|
this.logger.debug(`Successfully applied ${changeBytes.length} changes to document ${docId}`);
|
|
13024
13201
|
if (this.isDebugEnabled())
|
|
@@ -14303,6 +14480,7 @@ class BaseMindooDB {
|
|
|
14303
14480
|
}
|
|
14304
14481
|
}
|
|
14305
14482
|
exports.BaseMindooDB = BaseMindooDB;
|
|
14483
|
+
BaseMindooDB.DEPOSIT_KEY_MISS_TTL_MS = 2000;
|
|
14306
14484
|
BaseMindooDB.GROUP_KEY_MISS_TTL_MS = 2000;
|
|
14307
14485
|
/** The "access control not enforced" allow decision returned by the
|
|
14308
14486
|
* non-throwing `canCreate`/`canChange`/`canDelete` prediction helpers. */
|