mindoodb 0.0.97 → 0.0.99
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/README-server.md +15 -0
- package/README.md +1 -0
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.d.ts +7 -0
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.js +17 -9
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.js.map +1 -1
- package/dist/appendonlystores/network/HttpTransport.d.ts.map +1 -1
- package/dist/appendonlystores/network/HttpTransport.js +1 -0
- package/dist/appendonlystores/network/HttpTransport.js.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts +18 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js +41 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js.map +1 -1
- package/dist/browser/index.mjs +76 -0
- package/dist/core/BaseMindooDB.d.ts +2 -0
- package/dist/core/BaseMindooDB.d.ts.map +1 -1
- package/dist/core/BaseMindooDB.js +73 -53
- package/dist/core/BaseMindooDB.js.map +1 -1
- package/dist/core/BaseMindooTenant.d.ts +44 -1
- package/dist/core/BaseMindooTenant.d.ts.map +1 -1
- package/dist/core/BaseMindooTenant.js +176 -42
- package/dist/core/BaseMindooTenant.js.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.d.ts +11 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.js +137 -92
- package/dist/core/BaseMindooTenantDirectory.js.map +1 -1
- package/dist/core/MindooDBServerAdmin.d.ts +1 -1
- package/dist/core/MindooDBServerAdmin.d.ts.map +1 -1
- package/dist/core/appendonlystores/migration/StoreMigration.d.ts +41 -1
- package/dist/core/appendonlystores/migration/StoreMigration.d.ts.map +1 -1
- package/dist/core/appendonlystores/migration/StoreMigration.js +126 -0
- package/dist/core/appendonlystores/migration/StoreMigration.js.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteContentAddressedStore.d.ts.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteContentAddressedStore.js +29 -0
- package/dist/core/appendonlystores/sqlite/SqliteContentAddressedStore.js.map +1 -1
- package/dist/core/appendonlystores/syncStores.d.ts +6 -0
- package/dist/core/appendonlystores/syncStores.d.ts.map +1 -1
- package/dist/core/appendonlystores/syncStores.js +21 -0
- package/dist/core/appendonlystores/syncStores.js.map +1 -1
- package/dist/core/appendonlystores/types.d.ts +8 -0
- package/dist/core/appendonlystores/types.d.ts.map +1 -1
- package/dist/core/appendonlystores/types.js.map +1 -1
- package/dist/core/builtinDbInvariants.d.ts +26 -1
- package/dist/core/builtinDbInvariants.d.ts.map +1 -1
- package/dist/core/builtinDbInvariants.js +52 -2
- package/dist/core/builtinDbInvariants.js.map +1 -1
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/joinrequests/crypto.d.ts +110 -0
- package/dist/core/joinrequests/crypto.d.ts.map +1 -0
- package/dist/core/joinrequests/crypto.js +340 -0
- package/dist/core/joinrequests/crypto.js.map +1 -0
- package/dist/core/joinrequests/index.d.ts +4 -0
- package/dist/core/joinrequests/index.d.ts.map +1 -0
- package/dist/core/joinrequests/index.js +29 -0
- package/dist/core/joinrequests/index.js.map +1 -0
- package/dist/core/joinrequests/invitations.d.ts +257 -0
- package/dist/core/joinrequests/invitations.d.ts.map +1 -0
- package/dist/core/joinrequests/invitations.js +703 -0
- package/dist/core/joinrequests/invitations.js.map +1 -0
- package/dist/core/joinrequests/types.d.ts +208 -0
- package/dist/core/joinrequests/types.d.ts.map +1 -0
- package/dist/core/joinrequests/types.js +43 -0
- package/dist/core/joinrequests/types.js.map +1 -0
- package/dist/core/recovery/PaperRescueDevice.d.ts +101 -0
- package/dist/core/recovery/PaperRescueDevice.d.ts.map +1 -0
- package/dist/core/recovery/PaperRescueDevice.js +205 -0
- package/dist/core/recovery/PaperRescueDevice.js.map +1 -0
- package/dist/core/recovery/RecoveryContactsDocument.d.ts +86 -0
- package/dist/core/recovery/RecoveryContactsDocument.d.ts.map +1 -0
- package/dist/core/recovery/RecoveryContactsDocument.js +91 -0
- package/dist/core/recovery/RecoveryContactsDocument.js.map +1 -0
- package/dist/core/recovery/RecoveryContactsManager.d.ts +176 -0
- package/dist/core/recovery/RecoveryContactsManager.d.ts.map +1 -0
- package/dist/core/recovery/RecoveryContactsManager.js +455 -0
- package/dist/core/recovery/RecoveryContactsManager.js.map +1 -0
- package/dist/core/recovery/index.d.ts +9 -0
- package/dist/core/recovery/index.d.ts.map +1 -0
- package/dist/core/recovery/index.js +48 -0
- package/dist/core/recovery/index.js.map +1 -0
- package/dist/core/recovery/recoverWithContacts.d.ts +56 -0
- package/dist/core/recovery/recoverWithContacts.d.ts.map +1 -0
- package/dist/core/recovery/recoverWithContacts.js +142 -0
- package/dist/core/recovery/recoverWithContacts.js.map +1 -0
- package/dist/core/recovery/recoveryPackage.d.ts +80 -0
- package/dist/core/recovery/recoveryPackage.d.ts.map +1 -0
- package/dist/core/recovery/recoveryPackage.js +182 -0
- package/dist/core/recovery/recoveryPackage.js.map +1 -0
- package/dist/core/recovery/rescueApproval.d.ts +39 -0
- package/dist/core/recovery/rescueApproval.d.ts.map +1 -0
- package/dist/core/recovery/rescueApproval.js +52 -0
- package/dist/core/recovery/rescueApproval.js.map +1 -0
- package/dist/core/recovery/rescueDevice.d.ts +51 -0
- package/dist/core/recovery/rescueDevice.d.ts.map +1 -0
- package/dist/core/recovery/rescueDevice.js +201 -0
- package/dist/core/recovery/rescueDevice.js.map +1 -0
- package/dist/core/recovery/shamir.d.ts +30 -0
- package/dist/core/recovery/shamir.d.ts.map +1 -0
- package/dist/core/recovery/shamir.js +133 -0
- package/dist/core/recovery/shamir.js.map +1 -0
- package/dist/core/types.d.ts +73 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/userkeys/UserKeyManager.d.ts +45 -0
- package/dist/core/userkeys/UserKeyManager.d.ts.map +1 -1
- package/dist/core/userkeys/UserKeyManager.js +121 -8
- package/dist/core/userkeys/UserKeyManager.js.map +1 -1
- package/dist/core/userkeys/generationSignature.d.ts +6 -0
- package/dist/core/userkeys/generationSignature.d.ts.map +1 -1
- package/dist/core/userkeys/generationSignature.js +1 -0
- package/dist/core/userkeys/generationSignature.js.map +1 -1
- package/dist/core/userkeys/resolveUserKeyDocument.d.ts.map +1 -1
- package/dist/core/userkeys/resolveUserKeyDocument.js +6 -6
- package/dist/core/userkeys/resolveUserKeyDocument.js.map +1 -1
- package/dist/core/userkeys/validateUserKeyDocument.d.ts +11 -1
- package/dist/core/userkeys/validateUserKeyDocument.d.ts.map +1 -1
- package/dist/core/userkeys/validateUserKeyDocument.js +18 -5
- package/dist/core/userkeys/validateUserKeyDocument.js.map +1 -1
- package/dist/node/appendonlystores/sqlite/FsBlobContentStore.d.ts +23 -0
- package/dist/node/appendonlystores/sqlite/FsBlobContentStore.d.ts.map +1 -0
- package/dist/node/appendonlystores/sqlite/FsBlobContentStore.js +119 -0
- package/dist/node/appendonlystores/sqlite/FsBlobContentStore.js.map +1 -0
- package/dist/node/appendonlystores/sqlite/NodeSqliteStore.d.ts +43 -0
- package/dist/node/appendonlystores/sqlite/NodeSqliteStore.d.ts.map +1 -0
- package/dist/node/appendonlystores/sqlite/NodeSqliteStore.js +121 -0
- package/dist/node/appendonlystores/sqlite/NodeSqliteStore.js.map +1 -0
- package/dist/node/appendonlystores/sqlite/ServerSqliteStore.d.ts +43 -0
- package/dist/node/appendonlystores/sqlite/ServerSqliteStore.d.ts.map +1 -0
- package/dist/node/appendonlystores/sqlite/ServerSqliteStore.js +279 -0
- package/dist/node/appendonlystores/sqlite/ServerSqliteStore.js.map +1 -0
- package/dist/node/server/MindooDBServer.d.ts +2 -0
- package/dist/node/server/MindooDBServer.d.ts.map +1 -1
- package/dist/node/server/MindooDBServer.js +46 -0
- package/dist/node/server/MindooDBServer.js.map +1 -1
- package/dist/node/server/StoreFactory.d.ts +5 -2
- package/dist/node/server/StoreFactory.d.ts.map +1 -1
- package/dist/node/server/StoreFactory.js +21 -4
- package/dist/node/server/StoreFactory.js.map +1 -1
- package/dist/node/server/TenantManager.d.ts +6 -0
- package/dist/node/server/TenantManager.d.ts.map +1 -1
- package/dist/node/server/TenantManager.js +17 -0
- package/dist/node/server/TenantManager.js.map +1 -1
- package/dist/node/server/server.js +2 -0
- package/dist/node/server/server.js.map +1 -1
- package/dist/server/JoinRequestService.d.ts +108 -0
- package/dist/server/JoinRequestService.d.ts.map +1 -0
- package/dist/server/JoinRequestService.js +454 -0
- package/dist/server/JoinRequestService.js.map +1 -0
- package/dist/server/TenantManager.d.ts +57 -1
- package/dist/server/TenantManager.d.ts.map +1 -1
- package/dist/server/TenantManager.js +273 -34
- package/dist/server/TenantManager.js.map +1 -1
- package/dist/server/http/MindooDBServerCore.d.ts +18 -0
- package/dist/server/http/MindooDBServerCore.d.ts.map +1 -1
- package/dist/server/http/MindooDBServerCore.js +230 -137
- package/dist/server/http/MindooDBServerCore.js.map +1 -1
- package/dist/server/serverConfig.d.ts.map +1 -1
- package/dist/server/serverConfig.js +17 -0
- package/dist/server/serverConfig.js.map +1 -1
- package/dist/server/types.d.ts +16 -1
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js +6 -0
- package/dist/server/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -23,17 +23,19 @@ exports.lazyContentAddressedStore = lazyContentAddressedStore;
|
|
|
23
23
|
const AuthenticationService_1 = require("../core/appendonlystores/network/AuthenticationService");
|
|
24
24
|
const ServerNetworkContentAddressedStore_1 = require("../appendonlystores/network/ServerNetworkContentAddressedStore");
|
|
25
25
|
const PurgedDocRegistry_1 = require("./PurgedDocRegistry");
|
|
26
|
+
const types_1 = require("../core/joinrequests/types");
|
|
26
27
|
const purgeDocumentHistory_1 = require("../core/appendonlystores/purgeDocumentHistory");
|
|
27
28
|
const Ed25519WitnessProvider_1 = require("../core/accesscontrol/timestamp/Ed25519WitnessProvider");
|
|
28
29
|
const BaseMindooTenantFactory_1 = require("../core/BaseMindooTenantFactory");
|
|
29
30
|
const RSAEncryption_1 = require("../core/crypto/RSAEncryption");
|
|
30
31
|
const privateKeyEncryption_1 = require("../core/crypto/privateKeyEncryption");
|
|
31
32
|
const KeyBag_1 = require("../core/keys/KeyBag");
|
|
32
|
-
const
|
|
33
|
+
const types_2 = require("../core/types");
|
|
33
34
|
const builtinDbInvariants_1 = require("../core/builtinDbInvariants");
|
|
34
35
|
const DirectoryStateNode_1 = require("../core/accesscontrol/DirectoryStateNode");
|
|
35
36
|
const tier1Evaluator_1 = require("../core/accesscontrol/tier1Evaluator");
|
|
36
37
|
const base64_1 = require("../core/utils/base64");
|
|
38
|
+
const LogLevel_1 = require("../core/logging/LogLevel");
|
|
37
39
|
// ---------------------------------------------------------------------------
|
|
38
40
|
// StoreFactoryAdapter
|
|
39
41
|
// ---------------------------------------------------------------------------
|
|
@@ -43,8 +45,8 @@ class StoreFactoryAdapter {
|
|
|
43
45
|
}
|
|
44
46
|
createStore(dbId, _options) {
|
|
45
47
|
return {
|
|
46
|
-
docStore: this.storeFactory.getStore(dbId,
|
|
47
|
-
attachmentStore: this.storeFactory.getStore(dbId,
|
|
48
|
+
docStore: this.storeFactory.getStore(dbId, types_2.StoreKind.docs),
|
|
49
|
+
attachmentStore: this.storeFactory.getStore(dbId, types_2.StoreKind.attachments),
|
|
48
50
|
};
|
|
49
51
|
}
|
|
50
52
|
}
|
|
@@ -427,6 +429,21 @@ const SERVER_KEYBAG_KEY = "server.keybag";
|
|
|
427
429
|
class TenantManager {
|
|
428
430
|
constructor(options) {
|
|
429
431
|
this.loadedTenants = new Map();
|
|
432
|
+
/**
|
|
433
|
+
* Tenant loads in progress. Parallel first requests for one tenant share a
|
|
434
|
+
* single load: two loads would open two store instances on the same
|
|
435
|
+
* directories, and the receipt-order write lock only holds per instance.
|
|
436
|
+
*/
|
|
437
|
+
this.loadingTenants = new Map();
|
|
438
|
+
/** Last use and running store calls per tenant (kept across unloads). */
|
|
439
|
+
this.tenantActivity = new Map();
|
|
440
|
+
/** Tracked store handles handed out per loaded tenant, so callers get stable identities. */
|
|
441
|
+
this.trackedStores = new WeakMap();
|
|
442
|
+
/**
|
|
443
|
+
* Token secret per tenant when no {@link TenantManagerOptions.jwtSecretForTenant}
|
|
444
|
+
* is configured. Kept across unloads and reloads so tokens stay valid.
|
|
445
|
+
*/
|
|
446
|
+
this.tenantJwtSecrets = new Map();
|
|
430
447
|
this.serverIdentity = null;
|
|
431
448
|
this.trustedServers = [];
|
|
432
449
|
/** Peer wiring handed down to every server store; see setPeerAccessControl. */
|
|
@@ -441,6 +458,7 @@ class TenantManager {
|
|
|
441
458
|
this.cryptoAdapter = options.cryptoAdapter;
|
|
442
459
|
this.jwtSecretForTenant = options.jwtSecretForTenant;
|
|
443
460
|
this.publicInfosKeysResolver = options.publicInfosKeysResolver;
|
|
461
|
+
this.defaultStoreTypeForNewTenants = options.defaultStoreTypeForNewTenants ?? "file";
|
|
444
462
|
// Load global server identity
|
|
445
463
|
const identityJson = this.dataStore.readText(SERVER_IDENTITY_KEY);
|
|
446
464
|
if (identityJson !== null) {
|
|
@@ -704,17 +722,17 @@ class TenantManager {
|
|
|
704
722
|
this.serverPassword &&
|
|
705
723
|
this.dataStore.exists(SERVER_KEYBAG_KEY)) {
|
|
706
724
|
const keyBag = await this.loadServerKeyBag();
|
|
707
|
-
keys.push(...(await keyBag.getAllKeys("doc", tenantId,
|
|
725
|
+
keys.push(...(await keyBag.getAllKeys("doc", tenantId, types_2.PUBLIC_INFOS_KEY_ID)));
|
|
708
726
|
}
|
|
709
727
|
return this.mergeUniqueKeys(keys);
|
|
710
728
|
}
|
|
711
729
|
async persistTenantPublicInfosKey(tenantId, key) {
|
|
712
730
|
const keyBag = await this.loadServerKeyBag();
|
|
713
|
-
const existingKeys = await keyBag.getAllKeys("doc", tenantId,
|
|
731
|
+
const existingKeys = await keyBag.getAllKeys("doc", tenantId, types_2.PUBLIC_INFOS_KEY_ID);
|
|
714
732
|
const incomingSignature = this.bytesToBase64(key);
|
|
715
733
|
const alreadyStored = existingKeys.some((entry) => this.bytesToBase64(entry) === incomingSignature);
|
|
716
734
|
if (!alreadyStored) {
|
|
717
|
-
await keyBag.set("doc", tenantId,
|
|
735
|
+
await keyBag.set("doc", tenantId, types_2.PUBLIC_INFOS_KEY_ID, key, Date.now());
|
|
718
736
|
await this.saveServerKeyBag(keyBag);
|
|
719
737
|
}
|
|
720
738
|
}
|
|
@@ -761,7 +779,7 @@ class TenantManager {
|
|
|
761
779
|
adminUsername: request.adminUsername,
|
|
762
780
|
adminSigningPublicKey: request.adminSigningPublicKey,
|
|
763
781
|
adminEncryptionPublicKey: request.adminEncryptionPublicKey,
|
|
764
|
-
defaultStoreType: request.defaultStoreType ||
|
|
782
|
+
defaultStoreType: request.defaultStoreType || this.defaultStoreTypeForNewTenants,
|
|
765
783
|
users: [],
|
|
766
784
|
};
|
|
767
785
|
try {
|
|
@@ -780,13 +798,170 @@ class TenantManager {
|
|
|
780
798
|
}
|
|
781
799
|
async getTenant(tenantId) {
|
|
782
800
|
const normalizedId = tenantId.toLowerCase();
|
|
801
|
+
this.touchTenant(normalizedId);
|
|
783
802
|
const cached = this.loadedTenants.get(normalizedId);
|
|
784
803
|
if (cached) {
|
|
785
804
|
return cached;
|
|
786
805
|
}
|
|
787
|
-
const
|
|
788
|
-
|
|
789
|
-
|
|
806
|
+
const inFlight = this.loadingTenants.get(normalizedId);
|
|
807
|
+
if (inFlight) {
|
|
808
|
+
return inFlight;
|
|
809
|
+
}
|
|
810
|
+
const loading = this.loadTenant(normalizedId).then((loaded) => {
|
|
811
|
+
// Only cache if nobody invalidated the tenant while it was loading
|
|
812
|
+
// (config update, removal, reload, clearCache).
|
|
813
|
+
if (this.loadingTenants.get(normalizedId) === loading) {
|
|
814
|
+
this.loadingTenants.delete(normalizedId);
|
|
815
|
+
this.loadedTenants.set(normalizedId, loaded);
|
|
816
|
+
}
|
|
817
|
+
return loaded;
|
|
818
|
+
}, (error) => {
|
|
819
|
+
if (this.loadingTenants.get(normalizedId) === loading) {
|
|
820
|
+
this.loadingTenants.delete(normalizedId);
|
|
821
|
+
}
|
|
822
|
+
throw error;
|
|
823
|
+
});
|
|
824
|
+
this.loadingTenants.set(normalizedId, loading);
|
|
825
|
+
return loading;
|
|
826
|
+
}
|
|
827
|
+
/** Drop a tenant from the cache, including a load that is still running. */
|
|
828
|
+
forgetTenant(normalizedId) {
|
|
829
|
+
this.loadedTenants.delete(normalizedId);
|
|
830
|
+
this.loadingTenants.delete(normalizedId);
|
|
831
|
+
}
|
|
832
|
+
// =======================================================================
|
|
833
|
+
// Unloading idle tenants
|
|
834
|
+
// =======================================================================
|
|
835
|
+
touchTenant(normalizedId) {
|
|
836
|
+
let activity = this.tenantActivity.get(normalizedId);
|
|
837
|
+
if (!activity) {
|
|
838
|
+
activity = { lastUsedAt: Date.now(), inFlight: 0 };
|
|
839
|
+
this.tenantActivity.set(normalizedId, activity);
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
activity.lastUsedAt = Date.now();
|
|
843
|
+
}
|
|
844
|
+
return activity;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Wrap a store of a loaded tenant so that its use counts as tenant activity
|
|
848
|
+
* and a running call keeps the tenant loaded. A caller that keeps the handle
|
|
849
|
+
* after the tenant was unloaded or reloaded is routed to the current
|
|
850
|
+
* instance (`reopen`), so there is never more than one live store instance
|
|
851
|
+
* per store directory.
|
|
852
|
+
*/
|
|
853
|
+
trackStore(normalizedId, owner, cacheKey, target, reopen) {
|
|
854
|
+
let handles = this.trackedStores.get(owner);
|
|
855
|
+
if (!handles) {
|
|
856
|
+
handles = new Map();
|
|
857
|
+
this.trackedStores.set(owner, handles);
|
|
858
|
+
}
|
|
859
|
+
const existing = handles.get(cacheKey);
|
|
860
|
+
if (existing) {
|
|
861
|
+
return existing;
|
|
862
|
+
}
|
|
863
|
+
const proxy = new Proxy(target, {
|
|
864
|
+
get: (obj, prop) => {
|
|
865
|
+
const value = Reflect.get(obj, prop, obj);
|
|
866
|
+
if (typeof value !== "function") {
|
|
867
|
+
return value;
|
|
868
|
+
}
|
|
869
|
+
const fn = value;
|
|
870
|
+
if (prop === "getId" || prop === "getStoreKind" || prop === "constructor") {
|
|
871
|
+
return fn.bind(obj);
|
|
872
|
+
}
|
|
873
|
+
return (...args) => {
|
|
874
|
+
const activity = this.touchTenant(normalizedId);
|
|
875
|
+
if (this.loadedTenants.get(normalizedId) !== owner) {
|
|
876
|
+
// Unloaded or reloaded since this handle was handed out.
|
|
877
|
+
return reopen().then((fresh) => Reflect.get(fresh, prop, fresh).apply(fresh, args));
|
|
878
|
+
}
|
|
879
|
+
activity.inFlight++;
|
|
880
|
+
let result;
|
|
881
|
+
try {
|
|
882
|
+
result = fn.apply(obj, args);
|
|
883
|
+
}
|
|
884
|
+
catch (error) {
|
|
885
|
+
activity.inFlight--;
|
|
886
|
+
throw error;
|
|
887
|
+
}
|
|
888
|
+
if (result && typeof result.then === "function") {
|
|
889
|
+
return result.finally(() => {
|
|
890
|
+
activity.inFlight--;
|
|
891
|
+
activity.lastUsedAt = Date.now();
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
activity.inFlight--;
|
|
895
|
+
return result;
|
|
896
|
+
};
|
|
897
|
+
},
|
|
898
|
+
});
|
|
899
|
+
handles.set(cacheKey, proxy);
|
|
900
|
+
return proxy;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Unload every tenant that was not used for `idleMs`: its stores (with
|
|
904
|
+
* their in-memory metadata indexes), its directory and its caches. The next
|
|
905
|
+
* request loads it again. A tenant with running store calls or purges stays.
|
|
906
|
+
* Returns the ids of the unloaded tenants.
|
|
907
|
+
*/
|
|
908
|
+
evictIdleTenants(idleMs, now = Date.now()) {
|
|
909
|
+
const evicted = [];
|
|
910
|
+
for (const [tenantId, tenant] of this.loadedTenants) {
|
|
911
|
+
const activity = this.tenantActivity.get(tenantId);
|
|
912
|
+
if (activity && (activity.inFlight > 0 || now - activity.lastUsedAt < idleMs)) {
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
if (this.hasPurgeInFlight(tenantId)) {
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
this.loadedTenants.delete(tenantId);
|
|
919
|
+
evicted.push(tenantId);
|
|
920
|
+
void tenant.mindooTenant
|
|
921
|
+
?.disposeCacheManager?.()
|
|
922
|
+
.catch(() => undefined);
|
|
923
|
+
}
|
|
924
|
+
if (evicted.length > 0) {
|
|
925
|
+
console.log(`[TenantManager] Unloaded ${evicted.length} idle tenant(s): ${evicted.join(", ")} (${this.loadedTenants.size} still loaded)`);
|
|
926
|
+
}
|
|
927
|
+
return evicted;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Periodically unload tenants idle for `idleMs` (see {@link evictIdleTenants}).
|
|
931
|
+
* `idleMs <= 0` turns it off. The timer does not keep the process alive.
|
|
932
|
+
*/
|
|
933
|
+
startIdleEviction(idleMs, intervalMs = Math.min(idleMs, 60000)) {
|
|
934
|
+
this.stopIdleEviction();
|
|
935
|
+
if (!(idleMs > 0)) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
this.idleEvictionTimer = setInterval(() => {
|
|
939
|
+
try {
|
|
940
|
+
this.evictIdleTenants(idleMs);
|
|
941
|
+
}
|
|
942
|
+
catch (error) {
|
|
943
|
+
console.error("[TenantManager] Unloading idle tenants failed:", error);
|
|
944
|
+
}
|
|
945
|
+
}, Math.max(1000, intervalMs));
|
|
946
|
+
this.idleEvictionTimer.unref?.();
|
|
947
|
+
}
|
|
948
|
+
stopIdleEviction() {
|
|
949
|
+
if (this.idleEvictionTimer !== undefined) {
|
|
950
|
+
clearInterval(this.idleEvictionTimer);
|
|
951
|
+
this.idleEvictionTimer = undefined;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
/** Number of tenants currently held in memory. */
|
|
955
|
+
getLoadedTenantCount() {
|
|
956
|
+
return this.loadedTenants.size;
|
|
957
|
+
}
|
|
958
|
+
hasPurgeInFlight(normalizedId) {
|
|
959
|
+
for (const key of this.purgeInFlight.keys()) {
|
|
960
|
+
if (key === normalizedId || key.startsWith(`${normalizedId}\u0000`)) {
|
|
961
|
+
return true;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return false;
|
|
790
965
|
}
|
|
791
966
|
tenantExists(tenantId) {
|
|
792
967
|
const normalizedId = tenantId.toLowerCase();
|
|
@@ -857,7 +1032,7 @@ class TenantManager {
|
|
|
857
1032
|
config.defaultStoreType = updates.defaultStoreType;
|
|
858
1033
|
}
|
|
859
1034
|
this.writeTenantConfig(normalizedId, config);
|
|
860
|
-
this.
|
|
1035
|
+
this.forgetTenant(normalizedId);
|
|
861
1036
|
console.log(`[TenantManager] Updated tenant config: ${normalizedId}`);
|
|
862
1037
|
}
|
|
863
1038
|
async removeTenant(tenantId) {
|
|
@@ -866,23 +1041,30 @@ class TenantManager {
|
|
|
866
1041
|
if (!this.dataStore.exists(tenantKey)) {
|
|
867
1042
|
throw new Error(`Tenant ${normalizedId} does not exist`);
|
|
868
1043
|
}
|
|
869
|
-
this.
|
|
1044
|
+
this.forgetTenant(normalizedId);
|
|
870
1045
|
if (this.serverIdentity &&
|
|
871
1046
|
this.serverPassword &&
|
|
872
1047
|
this.dataStore.exists(SERVER_KEYBAG_KEY)) {
|
|
873
1048
|
const keyBag = await this.loadServerKeyBag();
|
|
874
|
-
await keyBag.deleteKey("doc", normalizedId,
|
|
1049
|
+
await keyBag.deleteKey("doc", normalizedId, types_2.PUBLIC_INFOS_KEY_ID);
|
|
875
1050
|
await this.saveServerKeyBag(keyBag);
|
|
876
1051
|
}
|
|
877
1052
|
this.dataStore.deleteTree(tenantKey);
|
|
878
1053
|
await this.storeProvider.removeTenantStores?.(normalizedId);
|
|
1054
|
+
this.tenantJwtSecrets.delete(normalizedId);
|
|
1055
|
+
this.tenantActivity.delete(normalizedId);
|
|
879
1056
|
console.log(`[TenantManager] Removed tenant: ${normalizedId}`);
|
|
880
1057
|
}
|
|
881
1058
|
// =======================================================================
|
|
882
1059
|
// Server stores
|
|
883
1060
|
// =======================================================================
|
|
884
1061
|
async getServerStore(tenantId, dbId, storeKind) {
|
|
885
|
-
const
|
|
1062
|
+
const normalizedId = tenantId.toLowerCase();
|
|
1063
|
+
const tenant = await this.getTenant(normalizedId);
|
|
1064
|
+
const raw = await this.getRawServerStore(tenant, tenantId, dbId, storeKind);
|
|
1065
|
+
return this.trackStore(normalizedId, tenant, `server:${dbId}:${storeKind}`, raw, () => this.getServerStore(tenantId, dbId, storeKind));
|
|
1066
|
+
}
|
|
1067
|
+
async getRawServerStore(tenant, tenantId, dbId, storeKind) {
|
|
886
1068
|
const cacheKey = `${dbId}:${storeKind}`;
|
|
887
1069
|
const cached = tenant.serverStores.get(cacheKey);
|
|
888
1070
|
if (cached) {
|
|
@@ -925,20 +1107,20 @@ class TenantManager {
|
|
|
925
1107
|
: () => this.getPurgedDocRegistry(tenantId).getPurgedDocIds(dbId);
|
|
926
1108
|
// Author-scoped read (§6.12). Never for the directory databases. The
|
|
927
1109
|
// attachments store finds a document's creator in the docs store.
|
|
928
|
-
const readScopeResolver = dbId === "directory" || dbId ===
|
|
1110
|
+
const readScopeResolver = dbId === "directory" || dbId === types_2.USER_DIRECTORY_DB_ID
|
|
929
1111
|
? undefined
|
|
930
1112
|
: this.buildReadScopeResolver(directory, dbId);
|
|
931
|
-
const docCreatorResolver = storeKind ===
|
|
1113
|
+
const docCreatorResolver = storeKind === types_2.StoreKind.attachments
|
|
932
1114
|
? this.buildDocCreatorResolver(
|
|
933
1115
|
// Lazy as well: a refused request must not create the docs store.
|
|
934
|
-
lazyContentAddressedStore(dbId,
|
|
1116
|
+
lazyContentAddressedStore(dbId, types_2.StoreKind.docs, () => tenant.storeFactory.getStore(dbId, types_2.StoreKind.docs)))
|
|
935
1117
|
: undefined;
|
|
936
1118
|
// Purge requests in personal databases (§6.11): checked on push (only in
|
|
937
1119
|
// the docs store, where the request documents live).
|
|
938
|
-
const purgeRequestValidator = storeKind ===
|
|
1120
|
+
const purgeRequestValidator = storeKind === types_2.StoreKind.docs && (0, builtinDbInvariants_1.personalDatabaseOwner)(dbId)
|
|
939
1121
|
? this.buildPurgeRequestValidator(tenant, directory, localStore, dbId)
|
|
940
1122
|
: undefined;
|
|
941
|
-
const builtinWriteContext = dbId ===
|
|
1123
|
+
const builtinWriteContext = dbId === types_2.USER_DIRECTORY_DB_ID
|
|
942
1124
|
? this.buildUserdirectoryWriteContext(tenant, localStore)
|
|
943
1125
|
: undefined;
|
|
944
1126
|
// Directory writes only by administrators at the server's clock, and
|
|
@@ -949,6 +1131,16 @@ class TenantManager {
|
|
|
949
1131
|
isAdminAt: (signingKey, T) => this.isTenantAdminKeyAt(tenant, signingKey, T),
|
|
950
1132
|
}
|
|
951
1133
|
: undefined;
|
|
1134
|
+
// Join requests (docs/invitations.md): server-created, admin-decided.
|
|
1135
|
+
const joinRequestsWriteCheck = dbId === types_1.JOIN_REQUESTS_DB_ID
|
|
1136
|
+
? {
|
|
1137
|
+
isAdminAt: (signingKey, T) => this.isTenantAdminKeyAt(tenant, signingKey, T),
|
|
1138
|
+
isJoinRequestServerKey: async (signingKey) => {
|
|
1139
|
+
const base = tenant.mindooTenant;
|
|
1140
|
+
return typeof base?.isJoinRequestServerKey === "function" ? base.isJoinRequestServerKey(signingKey) : false;
|
|
1141
|
+
},
|
|
1142
|
+
}
|
|
1143
|
+
: undefined;
|
|
952
1144
|
const serverStore = new ServerNetworkContentAddressedStore_1.ServerNetworkContentAddressedStore(localStore, directory, tenant.authService, this.cryptoAdapter, undefined, {
|
|
953
1145
|
timestampProvider,
|
|
954
1146
|
witnessDbid: dbId,
|
|
@@ -963,10 +1155,19 @@ class TenantManager {
|
|
|
963
1155
|
purgeRequestValidator,
|
|
964
1156
|
builtinWriteContext,
|
|
965
1157
|
directoryAdminCheck,
|
|
1158
|
+
joinRequestsWriteCheck,
|
|
966
1159
|
...this.peerAccessControl,
|
|
967
1160
|
});
|
|
1161
|
+
// A parallel request for the same store may have finished first while
|
|
1162
|
+
// this one awaited above; keep a single instance per store.
|
|
1163
|
+
const raced = tenant.serverStores.get(cacheKey);
|
|
1164
|
+
if (raced) {
|
|
1165
|
+
return raced;
|
|
1166
|
+
}
|
|
968
1167
|
tenant.serverStores.set(cacheKey, serverStore);
|
|
969
|
-
|
|
1168
|
+
if ((0, LogLevel_1.getDefaultLogLevel)() >= LogLevel_1.LogLevel.DEBUG) {
|
|
1169
|
+
console.debug(`[TenantManager] Created server store for ${tenantId}/${dbId}/${storeKind}`);
|
|
1170
|
+
}
|
|
970
1171
|
return serverStore;
|
|
971
1172
|
}
|
|
972
1173
|
/**
|
|
@@ -1155,8 +1356,9 @@ class TenantManager {
|
|
|
1155
1356
|
};
|
|
1156
1357
|
}
|
|
1157
1358
|
async getStore(tenantId, dbId, storeKind) {
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1359
|
+
const normalizedId = tenantId.toLowerCase();
|
|
1360
|
+
const tenant = await this.getTenant(normalizedId);
|
|
1361
|
+
return this.trackStore(normalizedId, tenant, `local:${dbId}:${storeKind}`, tenant.storeFactory.getStore(dbId, storeKind), () => this.getStore(tenantId, dbId, storeKind));
|
|
1160
1362
|
}
|
|
1161
1363
|
/**
|
|
1162
1364
|
* Documents purged from `dbId` on this server (the persistent purge
|
|
@@ -1165,6 +1367,29 @@ class TenantManager {
|
|
|
1165
1367
|
getPurgedDocIds(tenantId, dbId) {
|
|
1166
1368
|
return this.getPurgedDocRegistry(tenantId).getPurgedDocIds(dbId);
|
|
1167
1369
|
}
|
|
1370
|
+
/**
|
|
1371
|
+
* What the join-request inbox needs from this manager (docs/invitations.md).
|
|
1372
|
+
*/
|
|
1373
|
+
joinRequestAccess() {
|
|
1374
|
+
return {
|
|
1375
|
+
tenantExists: (tenantId) => this.tenantExists(tenantId),
|
|
1376
|
+
getServerTenant: async (tenantId) => (await this.getTenant(tenantId)).mindooTenant,
|
|
1377
|
+
getServerSigningPublicKey: () => this.getServerPublicInfo()?.signingPublicKey ?? null,
|
|
1378
|
+
isTenantMemberKey: async (tenantId, signingKey) => {
|
|
1379
|
+
const tenant = await this.getTenant(tenantId);
|
|
1380
|
+
if (await this.isTenantAdminKeyAt(tenant, signingKey, Date.now()))
|
|
1381
|
+
return true;
|
|
1382
|
+
const directory = tenant.directory;
|
|
1383
|
+
return typeof directory.getUserBySigningPublicKey === "function"
|
|
1384
|
+
? (await directory.getUserBySigningPublicKey(signingKey)) !== null
|
|
1385
|
+
: false;
|
|
1386
|
+
},
|
|
1387
|
+
recordPurgedDocument: (tenantId, dbId, docId) => this.getPurgedDocRegistry(tenantId).recordPurgedDoc(dbId, docId),
|
|
1388
|
+
dataStore: this.dataStore,
|
|
1389
|
+
tenantDataKey: (tenantId, name) => `${this.tenantKey(tenantId.toLowerCase())}/${name}`,
|
|
1390
|
+
cryptoAdapter: this.cryptoAdapter,
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1168
1393
|
/** The (lazily-loaded, cached) purge registry for a tenant. */
|
|
1169
1394
|
getPurgedDocRegistry(tenantId) {
|
|
1170
1395
|
const normalizedId = tenantId.toLowerCase();
|
|
@@ -1285,7 +1510,7 @@ class TenantManager {
|
|
|
1285
1510
|
if (entry.entryType !== "doc_create") {
|
|
1286
1511
|
return "a purge request can only be created, not changed or deleted";
|
|
1287
1512
|
}
|
|
1288
|
-
if (entry.decryptionKeyId !==
|
|
1513
|
+
if (entry.decryptionKeyId !== types_2.PUBLIC_INFOS_KEY_ID) {
|
|
1289
1514
|
return "a purge request must be encrypted with $publicinfos";
|
|
1290
1515
|
}
|
|
1291
1516
|
if (!mindooTenant || typeof dir.evaluateDbAccessModeForSigningKey !== "function") {
|
|
@@ -1297,7 +1522,7 @@ class TenantManager {
|
|
|
1297
1522
|
}
|
|
1298
1523
|
let docIds = null;
|
|
1299
1524
|
try {
|
|
1300
|
-
docIds = (0, builtinDbInvariants_1.purgeRequestDocIdsFromCreateBytes)(await mindooTenant.decryptPayload(entry.encryptedData,
|
|
1525
|
+
docIds = (0, builtinDbInvariants_1.purgeRequestDocIdsFromCreateBytes)(await mindooTenant.decryptPayload(entry.encryptedData, types_2.PUBLIC_INFOS_KEY_ID));
|
|
1301
1526
|
}
|
|
1302
1527
|
catch {
|
|
1303
1528
|
docIds = null;
|
|
@@ -1343,7 +1568,7 @@ class TenantManager {
|
|
|
1343
1568
|
const mindooTenant = tenant.mindooTenant;
|
|
1344
1569
|
if (!mindooTenant)
|
|
1345
1570
|
return;
|
|
1346
|
-
const docsStore = await this.getStore(normalizedId, dbId,
|
|
1571
|
+
const docsStore = await this.getStore(normalizedId, dbId, types_2.StoreKind.docs);
|
|
1347
1572
|
const creates = (await docsStore.findEntries("doc_create", null, null)).filter((m) => (0, builtinDbInvariants_1.isPurgeRequestDocId)(m.docId));
|
|
1348
1573
|
if (creates.length === 0)
|
|
1349
1574
|
return;
|
|
@@ -1382,8 +1607,8 @@ class TenantManager {
|
|
|
1382
1607
|
async purgeDocumentFromDatabase(tenantId, dbId, docId) {
|
|
1383
1608
|
try {
|
|
1384
1609
|
await (0, purgeDocumentHistory_1.purgeDocumentHistoryFromStores)({
|
|
1385
|
-
docsStore: await this.getStore(tenantId, dbId,
|
|
1386
|
-
attachmentStore: await this.getStore(tenantId, dbId,
|
|
1610
|
+
docsStore: await this.getStore(tenantId, dbId, types_2.StoreKind.docs),
|
|
1611
|
+
attachmentStore: await this.getStore(tenantId, dbId, types_2.StoreKind.attachments),
|
|
1387
1612
|
}, docId);
|
|
1388
1613
|
}
|
|
1389
1614
|
catch (error) {
|
|
@@ -1399,11 +1624,12 @@ class TenantManager {
|
|
|
1399
1624
|
}
|
|
1400
1625
|
async reloadTenant(tenantId) {
|
|
1401
1626
|
const normalizedId = tenantId.toLowerCase();
|
|
1402
|
-
this.
|
|
1627
|
+
this.forgetTenant(normalizedId);
|
|
1403
1628
|
return this.getTenant(normalizedId);
|
|
1404
1629
|
}
|
|
1405
1630
|
clearCache() {
|
|
1406
1631
|
this.loadedTenants.clear();
|
|
1632
|
+
this.loadingTenants.clear();
|
|
1407
1633
|
console.log(`[TenantManager] Cleared tenant cache`);
|
|
1408
1634
|
}
|
|
1409
1635
|
// -----------------------------------------------------------------------
|
|
@@ -1444,16 +1670,18 @@ class TenantManager {
|
|
|
1444
1670
|
innerDirectory = new SimpleMindooDirectory(config);
|
|
1445
1671
|
}
|
|
1446
1672
|
// Wrap with CompositeMindooDirectory so trusted servers are also recognized
|
|
1447
|
-
const directory = new CompositeMindooDirectory(innerDirectory, this.trustedServers,
|
|
1673
|
+
const directory = new CompositeMindooDirectory(innerDirectory, this.trustedServers,
|
|
1674
|
+
// Without a configured name the root key still authenticates by key, as a
|
|
1675
|
+
// principal named after it (like a delegated admin, §6.13): admin-only
|
|
1676
|
+
// databases such as `joinrequests` (docs/invitations.md) need that.
|
|
1677
|
+
config.adminSigningPublicKey && config.adminEncryptionPublicKey
|
|
1448
1678
|
? {
|
|
1449
|
-
username: config.adminUsername,
|
|
1679
|
+
username: config.adminUsername ?? delegatedAdminPrincipal(config.adminSigningPublicKey),
|
|
1450
1680
|
signingPublicKey: config.adminSigningPublicKey,
|
|
1451
1681
|
encryptionPublicKey: config.adminEncryptionPublicKey,
|
|
1452
1682
|
}
|
|
1453
1683
|
: undefined);
|
|
1454
|
-
const authService = new AuthenticationService_1.AuthenticationService(this.cryptoAdapter, directory, tenantId, this.
|
|
1455
|
-
? { jwtSecret: await this.jwtSecretForTenant(tenantId) }
|
|
1456
|
-
: undefined);
|
|
1684
|
+
const authService = new AuthenticationService_1.AuthenticationService(this.cryptoAdapter, directory, tenantId, { jwtSecret: await this.getJwtSecret(tenantId) });
|
|
1457
1685
|
console.log(`[TenantManager] Loaded tenant: ${tenantId}`);
|
|
1458
1686
|
return {
|
|
1459
1687
|
context,
|
|
@@ -1464,6 +1692,17 @@ class TenantManager {
|
|
|
1464
1692
|
serverStores: new Map(),
|
|
1465
1693
|
};
|
|
1466
1694
|
}
|
|
1695
|
+
async getJwtSecret(tenantId) {
|
|
1696
|
+
if (this.jwtSecretForTenant) {
|
|
1697
|
+
return this.jwtSecretForTenant(tenantId);
|
|
1698
|
+
}
|
|
1699
|
+
let secret = this.tenantJwtSecrets.get(tenantId);
|
|
1700
|
+
if (!secret) {
|
|
1701
|
+
secret = this.cryptoAdapter.getRandomValues(new Uint8Array(32));
|
|
1702
|
+
this.tenantJwtSecrets.set(tenantId, secret);
|
|
1703
|
+
}
|
|
1704
|
+
return secret;
|
|
1705
|
+
}
|
|
1467
1706
|
/**
|
|
1468
1707
|
* Create a real BaseMindooTenant for directory reading using the global
|
|
1469
1708
|
* server identity.
|
|
@@ -1475,7 +1714,7 @@ class TenantManager {
|
|
|
1475
1714
|
// Create a KeyBag using the server user's encryption key
|
|
1476
1715
|
const keyBag = await this.createServerKeyBag(serverUser, this.serverPassword);
|
|
1477
1716
|
for (const publicInfosKey of this.mergeUniqueKeys(publicInfosKeys)) {
|
|
1478
|
-
await keyBag.set("doc", tenantId,
|
|
1717
|
+
await keyBag.set("doc", tenantId, types_2.PUBLIC_INFOS_KEY_ID, publicInfosKey);
|
|
1479
1718
|
}
|
|
1480
1719
|
// Collect trusted server signing keys for additionalTrustedKeys
|
|
1481
1720
|
const openOptions = {};
|