@mistriwork/mistri 1.0.0-rc.51 → 1.0.0-rc.53
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 +16 -0
- package/README.md +1 -1
- package/dist/docs/status.md +3 -3
- package/dist/embed/react/headless.js +1 -1
- package/dist/embed/react/index.js +1 -1
- package/dist/server/app/appSearchRuntime.d.ts +9 -0
- package/dist/server/app/appSearchRuntime.js +21 -3
- package/dist/server/cli/apps.d.ts +2 -0
- package/dist/server/cli/apps.js +14 -0
- package/dist/server/cli/mistriMain.js +13 -1
- package/dist/server/cli/reindex.js +15 -2
- package/dist/server/stores/commandStores.d.ts +6 -1
- package/dist/server/stores/commandStores.js +2 -1
- package/dist/server/tenant/tenantAdministration.js +2 -1
- package/dist/server/tenant/tenantAuthenticatedHttp.js +3 -1
- package/dist/server/tenant/tenantBackstopOwner.d.ts +20 -0
- package/dist/server/tenant/tenantBackstopOwner.js +37 -0
- package/dist/server/tenant/tenantConnections.d.ts +23 -2
- package/dist/server/tenant/tenantConnections.js +39 -3
- package/dist/server/tenant/tenantEnrollment.d.ts +8 -0
- package/dist/server/tenant/tenantEnrollment.js +40 -4
- package/dist/server/tenant/tenantLive.d.ts +6 -0
- package/dist/server/tenant/tenantLive.js +18 -1
- package/dist/server/tenant/tenantLocalAccess.js +16 -2
- package/dist/server/tenant/tenantNames.d.ts +1 -1
- package/dist/server/tenant/tenantPostgresCatalog.d.ts +30 -1
- package/dist/server/tenant/tenantPostgresCatalog.js +138 -3
- package/dist/server/tenant/tenantReadSource.d.ts +4 -187
- package/dist/server/tenant/tenantReadSource.js +110 -693
- package/dist/server/tenant/tenantReadSourceShared.d.ts +213 -0
- package/dist/server/tenant/tenantReadSourceShared.js +176 -0
- package/dist/server/tenant/tenantReadSourceSqlite.d.ts +6 -0
- package/dist/server/tenant/tenantReadSourceSqlite.js +498 -0
- package/dist/server/tenant/tenantSearch.d.ts +2 -30
- package/dist/server/tenant/tenantSearch.js +17 -2
- package/dist/server/tenant/tenantStartup.js +13 -2
- package/dist/server/tenant/tenantSupportOperations.js +2 -1
- package/dist/server/tenant/tenantValidationRecord.js +2 -0
- package/dist/server/tenant/tenantWriteSource.d.ts +8 -1
- package/dist/server/tenant/tenantWriteSource.js +79 -14
- package/package.json +4 -4
|
@@ -1,669 +1,50 @@
|
|
|
1
|
-
import { postgresSandboxSession, sandboxRows, sandboxStorePresent
|
|
1
|
+
import { postgresSandboxSession, sandboxRows, sandboxStorePresent } from './tenantSandboxStore.js';
|
|
2
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { ANSI_STYLE, Refusal, Serial, TransactionOutcomeUnknown, } from '@mistriwork/engine/adapters/adapter.js';
|
|
6
|
-
import { tenantAdapter } from './tenantConnections.js';
|
|
3
|
+
import { TransactionOutcomeUnknown } from '@mistriwork/engine/adapters/adapter.js';
|
|
4
|
+
import { tenantAdapter, tenantBackstop } from './tenantConnections.js';
|
|
7
5
|
import { MAX_REPLICAS, primaryReads, ReplicaSet, ReplicaUnavailable, replicaFailure, replicaReadWanted, } from '@mistriwork/engine/adapters/replicas.js';
|
|
8
6
|
import { StatementStopped } from '@mistriwork/engine/adapters/statementControl.js';
|
|
9
7
|
import { createScopedReads } from '@mistriwork/engine/adapters/scoped/scopedReads.js';
|
|
10
|
-
import { schemaFingerprint, sqliteParams, toJsonSafe, withBytes } from '@mistriwork/engine/adapters/sqlite.js';
|
|
11
|
-
import { readSqliteReadCatalog } from '@mistriwork/engine/adapters/scoped/sqliteScopeCatalog.js';
|
|
12
8
|
import { assertScopeCatalog, captureScopeCatalog, isFixedScopeCatalog, mapScopeCatalog } from '@mistriwork/engine/shared/scopeCatalog.js';
|
|
13
9
|
import { isRowScopeSnapshot, snapshotRowScope } from '@mistriwork/engine/shared/rowScope.js';
|
|
14
10
|
import { mapAppScopedReads } from '../app/appResources.js';
|
|
15
|
-
import { captureTenantData, denyTenant, denyTenantFor,
|
|
16
|
-
import { parseTenantSelector
|
|
17
|
-
import {
|
|
11
|
+
import { captureTenantData, denyTenant, denyTenantFor, TenantContextError } from '@mistriwork/engine/server/evidence.js';
|
|
12
|
+
import { parseTenantSelector } from './tenantMembership.js';
|
|
13
|
+
import { captureTenantOwnership } from './tenantOwnership.js';
|
|
18
14
|
import { assertServiceReadCurrent, captureServiceEvidence, composeServiceScope } from './tenantServiceModel.js';
|
|
19
15
|
import { AppAccessDenied } from '../app/appAccess.js';
|
|
20
16
|
import { composePlatformReadScope, composePublicReadScope, composeTenantReadScope, physicalRowScope } from './tenantReadScope.js';
|
|
21
17
|
import { assertPublicResolved, capturePublicEvidence, capturePublicLookup, intakeVisitor, publicLookupOf, publicResolution, publicTenantOf, } from './tenantPublicRead.js';
|
|
22
18
|
import { assertSupportEvidenceObserved, supportAsMember } from './tenantSupportMembership.js';
|
|
23
|
-
import { observeSqliteSupportMembership } from './tenantSupportMembershipSqlite.js';
|
|
24
19
|
import { observePostgresSupportMembership } from './tenantSupportMembershipPostgres.js';
|
|
25
|
-
import { postgresTenantLocator,
|
|
26
|
-
import {
|
|
20
|
+
import { postgresTenantLocator, registerTenantSourceIdentity } from './tenantSourceIdentity.js';
|
|
21
|
+
import {} from './tenantStore.js';
|
|
27
22
|
import { assertPostgresPlatformMembershipSnapshot, assertPostgresTenantMembershipRows, assertPostgresTenantMembershipSnapshot, assertPostgresTenantStore, attestedPostgresTenantStore, postgresTenantMembershipLookupRows, postgresPlatformMembershipRows, postgresPlatformMembershipStatement, postgresTenantMembershipLookupStatement, postgresTenantMembershipsRows, postgresTenantMembershipsStatement, postgresTenantMembershipStatement, } from './tenantStorePostgres.js';
|
|
28
23
|
import { attestPostgresTenantRelations, pinPostgresTenantRelationsStatement, postgresCatalogFingerprintOf, tenantCatalogTrust, } from './tenantPostgresCatalog.js';
|
|
29
24
|
import { postgresCatalogDigestStatement } from '@mistriwork/engine/adapters/postgresCatalogDigest.js';
|
|
30
25
|
import { schemaMoved } from '@mistriwork/engine/adapters/postgresSchemaEpoch.js';
|
|
31
26
|
import { SYSTEM_SCHEMAS } from '@mistriwork/engine/adapters/postgresCatalog.js';
|
|
32
27
|
import { suspectsStores } from '@mistriwork/engine/adapters/postgresTransactionHandle.js';
|
|
33
|
-
import { captureAddressesLookup, captureIntakeEvidence, captureIntakeLookup, captureIntakeTenantLookup, heldAddress, intakeAddressQuery, intakePlace, intakeTenantOf,
|
|
28
|
+
import { captureAddressesLookup, captureIntakeEvidence, captureIntakeLookup, captureIntakeTenantLookup, heldAddress, intakeAddressQuery, intakePlace, intakeTenantOf, tenantOwnAddress, tenantOwnAddressQuery, ownAddressResolution, captureProfileLookup, activeTenantsZone, activeWithColumn, heldProfile, tenantProfileColumnQuery, tenantProfileQuery, } from './tenantIntakeModel.js';
|
|
34
29
|
import { PLATFORM_TENANT_ID } from './tenantPlatform.js';
|
|
35
|
-
import { findPostgresLink,
|
|
30
|
+
import { findPostgresLink, readPostgresLink } from '../../adapters/commands/commandLinks.js';
|
|
36
31
|
import { assertLinkReadCurrent, assertLinkRowCurrent, captureLinkEvidence, captureLinkLookup, composeLinkScope, linkResolution, } from './tenantLinkModel.js';
|
|
37
|
-
import { readSqliteActiveTenants, readSqliteIntakeAnchor } from './tenantStore.js';
|
|
38
32
|
import { dueTimersPlan } from './tenantDueTimers.js';
|
|
39
|
-
import {
|
|
33
|
+
import { tenantNamesOf, tenantNamesQuery } from './tenantNames.js';
|
|
40
34
|
import { readPostgresActiveTenants, readPostgresIntakeAnchor } from './tenantStorePostgres.js';
|
|
41
35
|
import { assertActivityQuery, assertHistoryQuery, } from '../../adapters/history/recordHistory.js';
|
|
42
|
-
import { readSqliteRecordActivity, readSqliteRecordHistory } from '../../adapters/history/sqliteRecordHistory.js';
|
|
43
36
|
import { readPostgresRecordActivity, readPostgresRecordHistory } from '../../adapters/history/postgresRecordHistory.js';
|
|
44
37
|
import { assertInboxQuery } from '../../adapters/inbox/inbox.js';
|
|
45
38
|
import { assertBrandingLogoQuery, assertBrandingQuery, } from '../../adapters/branding/branding.js';
|
|
46
|
-
import { readSqliteBranding, readSqliteBrandingLogo } from '../../adapters/branding/sqliteBranding.js';
|
|
47
39
|
import { readPostgresBranding, readPostgresBrandingLogo } from '../../adapters/branding/postgresBranding.js';
|
|
48
|
-
import { readSqliteInbox, sqliteInboxMail } from '../../adapters/inbox/sqliteInbox.js';
|
|
49
40
|
import { postgresInboxMail, readPostgresInbox } from '../../adapters/inbox/postgresInbox.js';
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
throw new Refusal('Invalid notification read.');
|
|
54
|
-
}
|
|
55
|
-
/** Private pinned source, not a public credential or session/deployment issuer. */
|
|
56
|
-
/** A discovery's tenants, as a sandbox lookup takes them: tenant ids, at most a discovery's worth. */
|
|
57
|
-
function sandboxLookup(tenants) {
|
|
58
|
-
if (!Array.isArray(tenants) || tenants.length > 256)
|
|
59
|
-
return denyTenant();
|
|
60
|
-
return Object.freeze(tenants.map((id) => parseTenantSelector([id])));
|
|
61
|
-
}
|
|
62
|
-
/** The tenant an address names, when that tenant is active, else the platform.
|
|
63
|
-
* The platform's own row first: while it is not active nothing is taken,
|
|
64
|
-
* whatever the address, so no address is answered differently from another. */
|
|
65
|
-
function intakeResolution(tenantId, anchor) {
|
|
66
|
-
const platform = anchor(PLATFORM_TENANT_ID);
|
|
67
|
-
if (platform === null)
|
|
68
|
-
return denyTenant();
|
|
69
|
-
const version = tenantId === undefined ? null : anchor(tenantId);
|
|
70
|
-
return tenantId !== undefined && version !== null
|
|
71
|
-
? { tenantId, tenantVersion: version, platformVersion: platform }
|
|
72
|
-
: { tenantId: PLATFORM_TENANT_ID, tenantVersion: platform, platformVersion: platform };
|
|
73
|
-
}
|
|
74
|
-
/** The token's tenant when it and the platform are both active; refused otherwise, as no token is triaged. */
|
|
75
|
-
function intakeResolutionByTenant(tenantId, anchor) {
|
|
76
|
-
const platform = anchor(PLATFORM_TENANT_ID);
|
|
77
|
-
const version = platform === null ? null : anchor(tenantId);
|
|
78
|
-
if (platform === null || version === null)
|
|
79
|
-
return denyTenant();
|
|
80
|
-
return { tenantId, tenantVersion: version, platformVersion: platform };
|
|
81
|
-
}
|
|
82
|
-
/** The lookup's plan and its ids, or undefined when there is nothing to read. */
|
|
83
|
-
function namesLookup(owned, lookup) {
|
|
84
|
-
const value = captureTenantData(lookup);
|
|
85
|
-
if (!Array.isArray(value.tenants) ||
|
|
86
|
-
value.tenants.length > TENANT_NAMES_MAX ||
|
|
87
|
-
(value.template !== undefined && typeof value.template !== 'string'))
|
|
88
|
-
return denyTenant();
|
|
89
|
-
const tenants = [...new Set(value.tenants.filter((id) => id !== PLATFORM_TENANT_ID))];
|
|
90
|
-
for (const id of tenants)
|
|
91
|
-
parseTenantSelector([id]);
|
|
92
|
-
const plan = tenantNamePlan(owned, value.template);
|
|
93
|
-
return plan && tenants.length ? { plan, tenants } : undefined;
|
|
94
|
-
}
|
|
95
|
-
const sources = new WeakSet();
|
|
41
|
+
import { assertEmailSwitchQuery, sandboxLookup, intakeResolution, intakeResolutionByTenant, namesLookup, sources, refusedBefore, readBatch, } from './tenantReadSourceShared.js';
|
|
42
|
+
export { advisoryRecords, captureTenantReadSource } from './tenantReadSourceShared.js';
|
|
43
|
+
export { openSqliteTenantReadSource } from './tenantReadSourceSqlite.js';
|
|
96
44
|
/* A whole snapshot begun under an attestation another has replaced since: asked again with the catalog attested. */
|
|
97
45
|
const MOVED = Symbol('moved');
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
export function captureTenantReadSource(input) {
|
|
101
|
-
if (!sources.has(input))
|
|
102
|
-
return denyTenant();
|
|
103
|
-
return input;
|
|
104
|
-
}
|
|
105
|
-
provideTenantSourceProbe('read', captureTenantReadSource);
|
|
106
|
-
/** A batch's reads in the shape a command's records take, for the one caller
|
|
107
|
-
* that asks a command's questions without its transaction (availability): it
|
|
108
|
-
* reads, and every way of writing refuses. */
|
|
109
|
-
export function advisoryRecords(reads) {
|
|
110
|
-
const refuse = async () => {
|
|
111
|
-
throw new Refusal('Availability reads; it never writes.');
|
|
112
|
-
};
|
|
113
|
-
return Object.freeze({
|
|
114
|
-
query: (request) => reads.query(request),
|
|
115
|
-
count: (table, filter) => reads.count(table, filter),
|
|
116
|
-
...(reads.aggregate ? { aggregate: (request) => reads.aggregate(request) } : {}),
|
|
117
|
-
insert: refuse,
|
|
118
|
-
update: refuse,
|
|
119
|
-
remove: refuse,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
/* A read refused before it ran — its arguments not plain data, the source
|
|
123
|
-
closed — is one fixed sentence to its caller, and keeps why for the
|
|
124
|
-
operator (refusalReport.ts), where a fresh denial in its place said
|
|
125
|
-
nothing on stderr at all. */
|
|
126
|
-
const refusedBefore = (error) => Promise.reject(recordRefusalCause(new TenantContextError(), error));
|
|
127
|
-
/** The reader a batch is handed (`withReads`): each operation's arguments
|
|
128
|
-
* captured, run on the batch's snapshot as it is asked, and refused once the
|
|
129
|
-
* batch has returned. Asked together, they leave together: the PostgreSQL
|
|
130
|
-
* reader pipelines, so the `when` of every command a record offers is one
|
|
131
|
-
* round trip, where one after another each was its own (SQLite runs them in
|
|
132
|
-
* turn on its one handle either way). The first failure is every later
|
|
133
|
-
* operation's, as one transaction's is. A database failure is mapped where
|
|
134
|
-
* it happens, so what `work` itself throws stays its own; `own` hears which
|
|
135
|
-
* that was. */
|
|
136
|
-
async function readBatch(bound, active, work, own) {
|
|
137
|
-
const running = new Set();
|
|
138
|
-
let failure;
|
|
139
|
-
const operations = {
|
|
140
|
-
finish: async () => {
|
|
141
|
-
await Promise.allSettled([...running]);
|
|
142
|
-
if (failure)
|
|
143
|
-
throw failure.error;
|
|
144
|
-
},
|
|
145
|
-
};
|
|
146
|
-
let open = true;
|
|
147
|
-
const run = (args, call) => {
|
|
148
|
-
let captured;
|
|
149
|
-
try {
|
|
150
|
-
if (!open)
|
|
151
|
-
throw recordRefusalCause(new TenantContextError(), "A batch's read was asked after the batch had returned.");
|
|
152
|
-
active();
|
|
153
|
-
captured = captureTenantData(args);
|
|
154
|
-
}
|
|
155
|
-
catch (error) {
|
|
156
|
-
return refusedBefore(error);
|
|
157
|
-
}
|
|
158
|
-
if (failure)
|
|
159
|
-
return Promise.reject(failure.error);
|
|
160
|
-
const operation = (async () => {
|
|
161
|
-
try {
|
|
162
|
-
const result = await call(...captured);
|
|
163
|
-
active();
|
|
164
|
-
return result;
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
try {
|
|
168
|
-
return denyTenantFor(error);
|
|
169
|
-
}
|
|
170
|
-
catch (mapped) {
|
|
171
|
-
failure ??= { error: mapped };
|
|
172
|
-
throw mapped;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
})();
|
|
176
|
-
running.add(operation);
|
|
177
|
-
void operation.then(() => running.delete(operation), () => running.delete(operation));
|
|
178
|
-
return operation;
|
|
179
|
-
};
|
|
180
|
-
const reads = Object.freeze({
|
|
181
|
-
query: (request) => run([request], (value) => bound.query(value)),
|
|
182
|
-
count: (table, filter, limit) => run([table, filter, limit], (id, value, boundLimit) => bound.count(id, value, boundLimit)),
|
|
183
|
-
runView: (view, offset) => run([view, offset], (value, boundOffset) => bound.runView(value, boundOffset)),
|
|
184
|
-
countView: (view, limit) => run([view, limit], (value, boundLimit) => bound.countView(value, boundLimit)),
|
|
185
|
-
runBreakdown: (breakdown, offset) => run([breakdown, offset], (value, boundOffset) => bound.runBreakdown(value, boundOffset)),
|
|
186
|
-
countBreakdown: (breakdown, limit) => run([breakdown, limit], (value, boundLimit) => bound.countBreakdown(value, boundLimit)),
|
|
187
|
-
/* A sum or a group, captured and run on the batch's snapshot as `count` is. */
|
|
188
|
-
...(bound.aggregate
|
|
189
|
-
? { aggregate: (request) => run([request], (value) => bound.aggregate(value)) }
|
|
190
|
-
: {}),
|
|
191
|
-
});
|
|
192
|
-
let result;
|
|
193
|
-
try {
|
|
194
|
-
result = await work(reads);
|
|
195
|
-
}
|
|
196
|
-
catch (error) {
|
|
197
|
-
open = false;
|
|
198
|
-
own(error);
|
|
199
|
-
await operations.finish().catch(() => { });
|
|
200
|
-
throw error;
|
|
201
|
-
}
|
|
202
|
-
open = false;
|
|
203
|
-
await operations.finish();
|
|
204
|
-
return result;
|
|
205
|
-
}
|
|
206
|
-
/** Opens once, read-only. Every operation owns a serialized read snapshot on
|
|
207
|
-
* that exact handle; no registry lookup, request-time migration or writer exists. */
|
|
208
|
-
export function openSqliteTenantReadSource(existingPath, ownership, store, now = Date.now) {
|
|
209
|
-
let opened;
|
|
210
|
-
try {
|
|
211
|
-
const captured = captureTenantOwnership(ownership);
|
|
212
|
-
// Ordinary SQLite catalogs omit engine internals. Apply exactly that
|
|
213
|
-
// normalization to both sides, never private/business dependency filtering.
|
|
214
|
-
const owned = captureTenantOwnership({
|
|
215
|
-
...captured,
|
|
216
|
-
physicalSchema: mapScopeCatalog(captured.physicalSchema, new Map(captured.physicalSchema.tables.filter((table) => !/^sqlite_/i.test(table.id)).map((table) => [table.id, table.id]))),
|
|
217
|
-
});
|
|
218
|
-
const owner = captureTenantData(store);
|
|
219
|
-
if (owner.appId !== owned.appId ||
|
|
220
|
-
owner.source.id !== owned.source.id ||
|
|
221
|
-
owner.source.connection !== owned.source.connection ||
|
|
222
|
-
typeof now !== 'function' ||
|
|
223
|
-
typeof existingPath !== 'string' ||
|
|
224
|
-
!statSync(existingPath).isFile())
|
|
225
|
-
return denyTenant();
|
|
226
|
-
const canonicalPath = realpathSync(existingPath);
|
|
227
|
-
const db = (opened = new DatabaseSync(canonicalPath, { readOnly: true, enableForeignKeyConstraints: true }));
|
|
228
|
-
db.exec('PRAGMA query_only = ON; PRAGMA busy_timeout = 1000');
|
|
229
|
-
const physicalIds = new Map(Object.entries(owned.resources).map(([logical, physical]) => [physical, logical]));
|
|
230
|
-
const logicalIds = new Map(Object.entries(owned.resources));
|
|
231
|
-
const toPhysical = (id) => logicalIds.get(id) ?? denyTenant();
|
|
232
|
-
const toLogical = (id) => physicalIds.get(id) ?? denyTenant();
|
|
233
|
-
const physicalSchema = mapScopeCatalog(owned.physicalSchema, new Map([...physicalIds.keys()].map((id) => [id, id])));
|
|
234
|
-
// A snapshot, so every read's composition with it is kept (tenantReadScope.ts).
|
|
235
|
-
const allTrue = snapshotRowScope(owned.schema, Object.fromEntries([...logicalIds.keys()].map((id) => [id, true])));
|
|
236
|
-
let definitions;
|
|
237
|
-
const attestCatalog = () => {
|
|
238
|
-
const actual = readSqliteReadCatalog(db);
|
|
239
|
-
if ([...physicalIds.keys()].some((id) => !actual.readable.has(id)))
|
|
240
|
-
return denyTenant();
|
|
241
|
-
assertTenantOwnership(owned, { ...owned, physicalSchema: actual.schema });
|
|
242
|
-
// Catalog authority intentionally omits collations. Pin actual mapped DDL
|
|
243
|
-
// too: changing a policy column to NOCASE/RTRIM must not widen its filter.
|
|
244
|
-
const ddl = new Map();
|
|
245
|
-
for (const row of db.prepare("SELECT name, sql FROM main.sqlite_schema WHERE type = 'table'").all()) {
|
|
246
|
-
if (typeof row.name === 'string' && physicalIds.has(row.name)) {
|
|
247
|
-
if (typeof row.sql !== 'string')
|
|
248
|
-
return denyTenant();
|
|
249
|
-
ddl.set(row.name, row.sql);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (ddl.size !== physicalIds.size || (definitions && [...definitions].some(([id, sql]) => ddl.get(id) !== sql)))
|
|
253
|
-
return denyTenant();
|
|
254
|
-
definitions ??= ddl;
|
|
255
|
-
};
|
|
256
|
-
/* A field report measured a whole catalog scan inside every read. Any DDL
|
|
257
|
-
on either schema moves its schema_version, so while the pair is the one
|
|
258
|
-
the last full pass judged, nothing it judged can have changed: the scan
|
|
259
|
-
is skipped. Read first, inside the read transaction, it names the
|
|
260
|
-
snapshot the read sees. A refused pass records nothing, so it refuses
|
|
261
|
-
again on every read until the schema moves back to a passing shape. */
|
|
262
|
-
const fingerprint = schemaFingerprint(db);
|
|
263
|
-
let attested;
|
|
264
|
-
const assertCatalog = () => {
|
|
265
|
-
const seen = fingerprint();
|
|
266
|
-
if (seen === attested)
|
|
267
|
-
return;
|
|
268
|
-
attested = undefined;
|
|
269
|
-
attestCatalog();
|
|
270
|
-
attested = seen;
|
|
271
|
-
};
|
|
272
|
-
// Construction must fail before publishing a capability, and close its
|
|
273
|
-
// owned handle even when either catalog or durable store evidence refuses.
|
|
274
|
-
db.exec('BEGIN');
|
|
275
|
-
assertCatalog();
|
|
276
|
-
assertSqliteTenantStore(db, owner);
|
|
277
|
-
db.exec('COMMIT');
|
|
278
|
-
const queue = new Serial();
|
|
279
|
-
let closing = false;
|
|
280
|
-
let closed;
|
|
281
|
-
const active = () => {
|
|
282
|
-
if (closing)
|
|
283
|
-
denyTenant('This tenant read source has closed.');
|
|
284
|
-
};
|
|
285
|
-
const close = () => {
|
|
286
|
-
if (!closed) {
|
|
287
|
-
closing = true;
|
|
288
|
-
closed = queue.run(async () => {
|
|
289
|
-
try {
|
|
290
|
-
db.close();
|
|
291
|
-
}
|
|
292
|
-
catch {
|
|
293
|
-
return denyTenant();
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
return closed;
|
|
298
|
-
};
|
|
299
|
-
const snapshot = async (work) => {
|
|
300
|
-
active();
|
|
301
|
-
if (db.isTransaction)
|
|
302
|
-
return denyTenant();
|
|
303
|
-
db.exec('BEGIN');
|
|
304
|
-
try {
|
|
305
|
-
assertCatalog();
|
|
306
|
-
const result = await work();
|
|
307
|
-
active();
|
|
308
|
-
db.exec('COMMIT');
|
|
309
|
-
return result;
|
|
310
|
-
}
|
|
311
|
-
finally {
|
|
312
|
-
if (db.isTransaction) {
|
|
313
|
-
try {
|
|
314
|
-
db.exec('ROLLBACK');
|
|
315
|
-
}
|
|
316
|
-
catch {
|
|
317
|
-
void close().catch(() => { });
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
// Snapshot all arguments before the first queue wait. Failures are local to
|
|
323
|
-
// this read; the serialized source still drains subsequent accepted work.
|
|
324
|
-
const invoke = (input, work) => {
|
|
325
|
-
let value;
|
|
326
|
-
try {
|
|
327
|
-
active();
|
|
328
|
-
value = captureTenantData(input);
|
|
329
|
-
}
|
|
330
|
-
catch (error) {
|
|
331
|
-
return refusedBefore(error);
|
|
332
|
-
}
|
|
333
|
-
return queue.run(async () => {
|
|
334
|
-
try {
|
|
335
|
-
const result = await snapshot(() => work(value));
|
|
336
|
-
active();
|
|
337
|
-
return result;
|
|
338
|
-
}
|
|
339
|
-
// A refusal inside stays one; a database that failed to answer —
|
|
340
|
-
// busy, gone, a statement that could not run — is a fault.
|
|
341
|
-
catch (error) {
|
|
342
|
-
return denyTenantFor(error);
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
};
|
|
346
|
-
const current = (evidence) => {
|
|
347
|
-
assertSqliteTenantMembershipSnapshot(db, owner, evidence, () => {
|
|
348
|
-
const instant = now();
|
|
349
|
-
active();
|
|
350
|
-
return instant;
|
|
351
|
-
});
|
|
352
|
-
// A trusted clock can retire the source synchronously. Never run SQL or
|
|
353
|
-
// return a newly bound reader after that retirement.
|
|
354
|
-
active();
|
|
355
|
-
};
|
|
356
|
-
const currentPlatform = (evidence) => {
|
|
357
|
-
assertSqlitePlatformMembershipSnapshot(db, owner, evidence, () => {
|
|
358
|
-
const instant = now();
|
|
359
|
-
active();
|
|
360
|
-
return instant;
|
|
361
|
-
});
|
|
362
|
-
active();
|
|
363
|
-
};
|
|
364
|
-
/* A support session's fence: the support row itself, observed with its
|
|
365
|
-
parent platform membership, `@platform` and the dealer. An operator inside
|
|
366
|
-
a dealer holds no member membership there, so the member check above would
|
|
367
|
-
refuse it; this runs inside the same snapshot, which `snapshot()` has
|
|
368
|
-
already opened as a transaction. */
|
|
369
|
-
const currentSupport = (evidence) => {
|
|
370
|
-
const instant = now();
|
|
371
|
-
active();
|
|
372
|
-
assertSupportEvidenceObserved(observeSqliteSupportMembership(db, owner, {
|
|
373
|
-
supportId: evidence.supportId,
|
|
374
|
-
accountId: evidence.accountId,
|
|
375
|
-
tenantId: evidence.tenantId,
|
|
376
|
-
at: instant,
|
|
377
|
-
}), evidence, instant);
|
|
378
|
-
active();
|
|
379
|
-
};
|
|
380
|
-
/* A visitor's placement in this snapshot: the address read by the policy's
|
|
381
|
-
column, byte for byte, then both anchors. Never a refusal of its own: null. */
|
|
382
|
-
const resolvePublicIn = (lookup) => {
|
|
383
|
-
const value = capturePublicLookup(lookup, owner), sql = intakeAddressQuery(owned, value.column);
|
|
384
|
-
const tenantId = value.address === null ? value.tenant : publicTenantOf(db.prepare(sql).all(value.address));
|
|
385
|
-
return publicResolution(tenantId, (id) => readSqliteIntakeAnchor(db, owner, id));
|
|
386
|
-
};
|
|
387
|
-
// A service's anchors as this snapshot finds them, against the versions its attempt read.
|
|
388
|
-
const currentService = (evidence) => {
|
|
389
|
-
const found = {
|
|
390
|
-
tenantVersion: readSqliteIntakeAnchor(db, owner, evidence.tenantId),
|
|
391
|
-
platformVersion: readSqliteIntakeAnchor(db, owner, PLATFORM_TENANT_ID),
|
|
392
|
-
};
|
|
393
|
-
const instant = now();
|
|
394
|
-
active();
|
|
395
|
-
assertServiceReadCurrent(evidence, found, instant);
|
|
396
|
-
active();
|
|
397
|
-
};
|
|
398
|
-
// A link's anchors and its row as this snapshot finds them, against the versions its request read.
|
|
399
|
-
const currentLink = (evidence) => {
|
|
400
|
-
const found = {
|
|
401
|
-
tenantVersion: readSqliteIntakeAnchor(db, owner, evidence.tenantId),
|
|
402
|
-
platformVersion: readSqliteIntakeAnchor(db, owner, PLATFORM_TENANT_ID),
|
|
403
|
-
};
|
|
404
|
-
const instant = now();
|
|
405
|
-
active();
|
|
406
|
-
assertLinkReadCurrent(evidence, found, instant);
|
|
407
|
-
assertLinkRowCurrent(readSqliteLink(db, owner.appId, evidence.tenantId, evidence.linkId, instant), evidence);
|
|
408
|
-
active();
|
|
409
|
-
};
|
|
410
|
-
const currentPublic = (evidence) => {
|
|
411
|
-
const value = capturePublicEvidence(evidence, owned);
|
|
412
|
-
const resolved = resolvePublicIn(publicLookupOf(value));
|
|
413
|
-
const instant = now();
|
|
414
|
-
active();
|
|
415
|
-
assertPublicResolved(value, resolved, instant);
|
|
416
|
-
active();
|
|
417
|
-
};
|
|
418
|
-
// One compiled reader per admitted scope. Every call first revalidates the
|
|
419
|
-
// principal it was bound for, tenant or platform, in its own snapshot.
|
|
420
|
-
const scoped = (scope) => {
|
|
421
|
-
const physicalScope = physicalRowScope(scope, toPhysical);
|
|
422
|
-
return mapAppScopedReads(createScopedReads(physicalSchema, physicalScope, ANSI_STYLE, async (sql, hint) => {
|
|
423
|
-
const started = performance.now();
|
|
424
|
-
const statement = db.prepare(sql.text);
|
|
425
|
-
statement.setReadBigInts(true);
|
|
426
|
-
const rows = statement.all(...sqliteParams(sql.params));
|
|
427
|
-
return {
|
|
428
|
-
columns: statement.columns().map((column) => column.name),
|
|
429
|
-
rows: rows.map(hint?.binary ? withBytes : toJsonSafe),
|
|
430
|
-
ms: Math.round(performance.now() - started),
|
|
431
|
-
sql,
|
|
432
|
-
};
|
|
433
|
-
}), { schema: owned.schema, toPhysical, toLogical });
|
|
434
|
-
};
|
|
435
|
-
const bind = (scope, revalidate) => {
|
|
436
|
-
const reads = scoped(scope);
|
|
437
|
-
const run = (args, work) => invoke(args, (capturedArgs) => {
|
|
438
|
-
revalidate();
|
|
439
|
-
return work(...capturedArgs);
|
|
440
|
-
});
|
|
441
|
-
return Object.freeze({
|
|
442
|
-
query: (request) => run([request], reads.query),
|
|
443
|
-
count: (table, filter, limit) => run([table, filter, limit], reads.count),
|
|
444
|
-
runView: (view, offset) => run([view, offset], reads.runView),
|
|
445
|
-
// The bound reaches the statement: never counted past it.
|
|
446
|
-
countView: (view, limit) => run([view, limit], reads.countView),
|
|
447
|
-
runBreakdown: (breakdown, offset) => run([breakdown, offset], reads.runBreakdown),
|
|
448
|
-
countBreakdown: (breakdown, limit) => run([breakdown, limit], reads.countBreakdown),
|
|
449
|
-
// Revalidated and bounded by the tenant's rows as `count` is.
|
|
450
|
-
...(reads.aggregate ? { aggregate: (request) => run([request], reads.aggregate) } : {}),
|
|
451
|
-
});
|
|
452
|
-
};
|
|
453
|
-
/* A batch is one queued snapshot: its reads run inside it, on the handle
|
|
454
|
-
directly, never through the queue it already holds. */
|
|
455
|
-
const batch = (input, open, work) => {
|
|
456
|
-
if (typeof work !== 'function')
|
|
457
|
-
return Promise.reject(new TenantContextError());
|
|
458
|
-
let own;
|
|
459
|
-
return invoke(input, (captured) => readBatch(scoped(open(captured)), active, work, (error) => {
|
|
460
|
-
own = { error };
|
|
461
|
-
})).catch((error) => {
|
|
462
|
-
throw own ? own.error : error;
|
|
463
|
-
});
|
|
464
|
-
};
|
|
465
|
-
// Arguments captured now, as `invoke` captures them; no snapshot is opened.
|
|
466
|
-
const bindLater = (input, make) => {
|
|
467
|
-
try {
|
|
468
|
-
active();
|
|
469
|
-
return Promise.resolve(make(captureTenantData(input)));
|
|
470
|
-
}
|
|
471
|
-
catch (error) {
|
|
472
|
-
return refusedBefore(error);
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
const source = Object.freeze({
|
|
476
|
-
ownership: owned,
|
|
477
|
-
readMembership: (lookup) => invoke(lookup, (query) => readSqliteTenantMembershipSnapshot(db, owner, query)),
|
|
478
|
-
readMemberships: (lookup) => invoke(lookup, (query) => readSqliteTenantMembershipsSnapshot(db, owner, query, () => {
|
|
479
|
-
const instant = now();
|
|
480
|
-
active();
|
|
481
|
-
return instant;
|
|
482
|
-
})),
|
|
483
|
-
assertCurrent: (evidence) => invoke(evidence, current),
|
|
484
|
-
scopeReads: (evidence, rows) => invoke({ evidence, rows }, (captured) => {
|
|
485
|
-
current(captured.evidence);
|
|
486
|
-
return bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), () => current(captured.evidence));
|
|
487
|
-
}),
|
|
488
|
-
bindReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), () => current(captured.evidence))),
|
|
489
|
-
withReads: (evidence, rows, work) => batch({ evidence, rows }, (captured) => {
|
|
490
|
-
current(captured.evidence);
|
|
491
|
-
return composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue);
|
|
492
|
-
}, work),
|
|
493
|
-
/* One handle, read in turn: every snapshot is already this one connection's, so there is nothing to share. */
|
|
494
|
-
together: async (_evidence, work) => {
|
|
495
|
-
if (typeof work !== 'function')
|
|
496
|
-
return Promise.reject(new TenantContextError());
|
|
497
|
-
active();
|
|
498
|
-
return work();
|
|
499
|
-
},
|
|
500
|
-
togetherFenced: async (_evidence, work) => {
|
|
501
|
-
if (typeof work !== 'function')
|
|
502
|
-
return Promise.reject(new TenantContextError());
|
|
503
|
-
active();
|
|
504
|
-
return { value: await work(), fenced: false };
|
|
505
|
-
},
|
|
506
|
-
readPlatformMembership: (lookup) => invoke(lookup, (query) => readSqlitePlatformMembershipSnapshot(db, owner, query)),
|
|
507
|
-
assertPlatformCurrent: (evidence) => invoke(evidence, currentPlatform),
|
|
508
|
-
// The source recomposes the platform scope itself: whatever a runtime
|
|
509
|
-
// passes, a table the platform policy does not name stays denied.
|
|
510
|
-
scopePlatformReads: (evidence, rows) => invoke({ evidence, rows }, (captured) => {
|
|
511
|
-
currentPlatform(captured.evidence);
|
|
512
|
-
return bind(composePlatformReadScope(owned, captured.evidence, captured.rows), () => currentPlatform(captured.evidence));
|
|
513
|
-
}),
|
|
514
|
-
bindPlatformReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePlatformReadScope(owned, captured.evidence, captured.rows), () => currentPlatform(captured.evidence))),
|
|
515
|
-
withPlatformReads: (evidence, rows, work) => batch({ evidence, rows }, (captured) => {
|
|
516
|
-
currentPlatform(captured.evidence);
|
|
517
|
-
return composePlatformReadScope(owned, captured.evidence, captured.rows);
|
|
518
|
-
}, work),
|
|
519
|
-
assertSupportCurrent: (evidence) => invoke(evidence, currentSupport),
|
|
520
|
-
/* The source recomposes the dealer's scope itself, as above — and through
|
|
521
|
-
composeTenantReadScope on a member view of the support evidence, not a
|
|
522
|
-
rule of its own. One policy rule then governs a dealer's rows for its
|
|
523
|
-
own members and for an operator inside it; a second rule could disagree
|
|
524
|
-
with the first, and that disagreement would show up as a session
|
|
525
|
-
reading rows the policy meant to withhold rather than as a failing
|
|
526
|
-
test. The fence is still the support row (currentSupport). */
|
|
527
|
-
scopeSupportReads: (evidence, rows) => invoke({ evidence, rows }, (captured) => {
|
|
528
|
-
currentSupport(captured.evidence);
|
|
529
|
-
return bind(composeTenantReadScope(owned, supportAsMember(captured.evidence), captured.rows, allTrue), () => currentSupport(captured.evidence));
|
|
530
|
-
}),
|
|
531
|
-
resolvePublic: (lookup) => invoke(lookup, resolvePublicIn),
|
|
532
|
-
assertPublicCurrent: (evidence) => invoke(evidence, currentPublic),
|
|
533
|
-
// The source recomposes the visitor's scope itself: the tenant predicate is never the caller's.
|
|
534
|
-
bindPublicReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePublicReadScope(owned, captured.evidence, captured.rows), () => currentPublic(captured.evidence))),
|
|
535
|
-
bindIntakeReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
536
|
-
const value = captureIntakeEvidence(captured.evidence, owned);
|
|
537
|
-
if (intakePlace(value) !== 'tenant')
|
|
538
|
-
throw new AppAccessDenied();
|
|
539
|
-
const visitor = intakeVisitor(value);
|
|
540
|
-
return bind(composePublicReadScope(owned, visitor, captured.rows), () => currentPublic(visitor));
|
|
541
|
-
}),
|
|
542
|
-
// The source recomposes the service's scope itself, as it does a visitor's.
|
|
543
|
-
bindServiceReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
544
|
-
const value = captureServiceEvidence(captured.evidence, owned);
|
|
545
|
-
if (value.place !== 'tenant')
|
|
546
|
-
throw new AppAccessDenied();
|
|
547
|
-
return bind(composeServiceScope(owned, value, captured.rows), () => currentService(value));
|
|
548
|
-
}),
|
|
549
|
-
// The source recomposes the link's scope itself: its one record, never a caller's predicate.
|
|
550
|
-
bindLinkReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
551
|
-
const value = captureLinkEvidence(captured.evidence, owned);
|
|
552
|
-
return bind(composeLinkScope(owned, value, captured.rows), () => currentLink(value));
|
|
553
|
-
}),
|
|
554
|
-
resolveLink: (lookup) => invoke(lookup, (query) => {
|
|
555
|
-
const { id } = captureLinkLookup(query);
|
|
556
|
-
let row;
|
|
557
|
-
try {
|
|
558
|
-
row = findSqliteLink(db, owner.appId, id, now());
|
|
559
|
-
}
|
|
560
|
-
catch {
|
|
561
|
-
// A store no command has written to yet has no link table: no link, as any other miss.
|
|
562
|
-
return null;
|
|
563
|
-
}
|
|
564
|
-
return linkResolution(row, (tenant) => readSqliteIntakeAnchor(db, owner, tenant));
|
|
565
|
-
}),
|
|
566
|
-
resolveIntake: (lookup) => invoke(lookup, (query) => {
|
|
567
|
-
const value = captureIntakeLookup(query, owner), sql = intakeAddressQuery(owned, value.column);
|
|
568
|
-
const tenantId = value.address === null ? undefined : intakeTenantOf(db.prepare(sql).all(value.address));
|
|
569
|
-
return intakeResolution(tenantId, (id) => readSqliteIntakeAnchor(db, owner, id));
|
|
570
|
-
}),
|
|
571
|
-
resolveIntakeByTenant: (lookup) => invoke(lookup, (query) => {
|
|
572
|
-
const value = captureIntakeTenantLookup(query, owner);
|
|
573
|
-
return intakeResolutionByTenant(value.tenantId, (id) => readSqliteIntakeAnchor(db, owner, id));
|
|
574
|
-
}),
|
|
575
|
-
readTenantNames: (lookup) => invoke(lookup, (query) => {
|
|
576
|
-
const asked = namesLookup(owned, query);
|
|
577
|
-
if (!asked)
|
|
578
|
-
return Object.freeze({});
|
|
579
|
-
const rows = db.prepare(tenantNamesQuery(owned, asked.plan, asked.tenants.length)).all(...asked.tenants);
|
|
580
|
-
return tenantNamesOf(asked.plan, rows, asked.tenants);
|
|
581
|
-
}),
|
|
582
|
-
readTenantAddresses: (lookup) => invoke(lookup, (query) => {
|
|
583
|
-
const { tenantId, columns } = captureAddressesLookup(owned, query);
|
|
584
|
-
const found = {};
|
|
585
|
-
for (const [kind, column] of Object.entries(columns)) {
|
|
586
|
-
if (column === undefined)
|
|
587
|
-
continue;
|
|
588
|
-
const address = heldAddress(db.prepare(tenantAddressQuery(owned, column)).all(tenantId));
|
|
589
|
-
const own = address && tenantOwnAddress(tenantId, address, db.prepare(intakeAddressQuery(owned, column)).all(address));
|
|
590
|
-
if (own)
|
|
591
|
-
found[kind] = own;
|
|
592
|
-
}
|
|
593
|
-
return Object.freeze(found);
|
|
594
|
-
}),
|
|
595
|
-
// Always a tenant's: `@platform` fails the selector.
|
|
596
|
-
readTenantProfile: (lookup) => invoke(lookup, (query) => {
|
|
597
|
-
const { tenantId, columns } = captureProfileLookup(owned, query);
|
|
598
|
-
return heldProfile(owned, columns, db.prepare(tenantProfileQuery(owned, columns)).all(tenantId));
|
|
599
|
-
}),
|
|
600
|
-
readServiceAnchors: (tenantId) => invoke(tenantId, (id) => Object.freeze({
|
|
601
|
-
tenantVersion: id === null ? null : readSqliteIntakeAnchor(db, owner, parseTenantSelector([id])),
|
|
602
|
-
platformVersion: readSqliteIntakeAnchor(db, owner, PLATFORM_TENANT_ID),
|
|
603
|
-
})),
|
|
604
|
-
readActiveTenants: (options) => invoke(options ?? {}, (query) => {
|
|
605
|
-
const zone = activeTenantsZone(owned, query);
|
|
606
|
-
const active = readSqliteActiveTenants(db, owner);
|
|
607
|
-
if (zone === undefined)
|
|
608
|
-
return Object.freeze(active.map((id) => Object.freeze({ id })));
|
|
609
|
-
const rows = db.prepare(tenantProfileColumnQuery(owned, zone)).all();
|
|
610
|
-
return activeWithColumn(owned, zone, active, rows);
|
|
611
|
-
}),
|
|
612
|
-
readSandboxClocks: () => invoke(null, async () => {
|
|
613
|
-
const rows = sqliteSandboxSession(db);
|
|
614
|
-
return (await sandboxStorePresent(rows)) ? sandboxRows(rows, owner.appId).clocks() : Object.freeze([]);
|
|
615
|
-
}),
|
|
616
|
-
readSandboxes: (tenants) => invoke(sandboxLookup(tenants), async (asked) => {
|
|
617
|
-
const rows = sqliteSandboxSession(db);
|
|
618
|
-
return (await sandboxStorePresent(rows)) ? sandboxRows(rows, owner.appId).among(asked) : new Set();
|
|
619
|
-
}),
|
|
620
|
-
readDueTimers: (query) => invoke(query, (value) => {
|
|
621
|
-
const plan = dueTimersPlan(owned, value);
|
|
622
|
-
const active = plan.tenants ? readSqliteActiveTenants(db, owner) : [];
|
|
623
|
-
if (plan.tenants && !active.length)
|
|
624
|
-
return Object.freeze([]);
|
|
625
|
-
return plan.decode(db.prepare(plan.sql).all(...plan.params(active)));
|
|
626
|
-
}),
|
|
627
|
-
readRecordHistory: (query) => invoke(query, (value) => {
|
|
628
|
-
assertHistoryQuery(value);
|
|
629
|
-
return readSqliteRecordHistory(db, value);
|
|
630
|
-
}),
|
|
631
|
-
readRecordActivity: (query) => invoke(query, (value) => {
|
|
632
|
-
assertActivityQuery(value);
|
|
633
|
-
return readSqliteRecordActivity(db, value);
|
|
634
|
-
}),
|
|
635
|
-
readInbox: (query) => invoke(query, (value) => {
|
|
636
|
-
assertInboxQuery(value);
|
|
637
|
-
return readSqliteInbox(db, value);
|
|
638
|
-
}),
|
|
639
|
-
readEmailOff: (query) => invoke(query, (value) => {
|
|
640
|
-
assertEmailSwitchQuery(value);
|
|
641
|
-
return sqliteInboxMail(async (work) => work(db)).emailOff(value.app, value.recipient);
|
|
642
|
-
}),
|
|
643
|
-
readBranding: (query) => invoke(query, (value) => {
|
|
644
|
-
assertBrandingQuery(value);
|
|
645
|
-
return readSqliteBranding(db, value);
|
|
646
|
-
}),
|
|
647
|
-
readBrandingLogo: (query) => invoke(query, (value) => {
|
|
648
|
-
assertBrandingLogoQuery(value);
|
|
649
|
-
return readSqliteBrandingLogo(db, value);
|
|
650
|
-
}),
|
|
651
|
-
close,
|
|
652
|
-
});
|
|
653
|
-
sources.add(source);
|
|
654
|
-
registerTenantSourceIdentity(source, { ...owner, canonicalPath });
|
|
655
|
-
return source;
|
|
656
|
-
}
|
|
657
|
-
catch {
|
|
658
|
-
try {
|
|
659
|
-
opened?.close();
|
|
660
|
-
}
|
|
661
|
-
catch {
|
|
662
|
-
/* Construction never publishes its handle. */
|
|
663
|
-
}
|
|
664
|
-
return denyTenant();
|
|
665
|
-
}
|
|
666
|
-
}
|
|
46
|
+
/* A snapshot's route for what crosses tenants by design (openPostgresTenantReadSource's `snapshot`). */
|
|
47
|
+
const OWNER = Symbol('owner');
|
|
667
48
|
/**
|
|
668
49
|
* The PostgreSQL counterpart of openSqliteTenantReadSource: the same source
|
|
669
50
|
* contract on a dedicated read pool. Every operation is one REPEATABLE READ,
|
|
@@ -750,6 +131,10 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
750
131
|
return target.attested !== undefined && at !== undefined && now >= at && now - at < trustFor ? target.attested : undefined;
|
|
751
132
|
};
|
|
752
133
|
const primary = { url, reader };
|
|
134
|
+
/* Under the backstop the description attests the pinned policy too, on every pool, the owner's included: what
|
|
135
|
+
the runtime role is held to is the database's to say wherever it is read from. */
|
|
136
|
+
const backstop = tenantBackstop(url);
|
|
137
|
+
const security = backstop ? { rowSecurity: { role: backstop.role } } : {};
|
|
753
138
|
const standbys = new ReplicaSet(replicas.map((replica) => ({
|
|
754
139
|
url: replica,
|
|
755
140
|
reader: tenantAdapter(replica, 'Private tenant replica reader', true),
|
|
@@ -782,7 +167,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
782
167
|
assertScopeCatalog(owned.physicalSchema, actual);
|
|
783
168
|
// Catalog authority omits collations, indexes and triggers; pin the
|
|
784
169
|
// description too, as the SQLite reader pins its CREATE text.
|
|
785
|
-
const described = await attestPostgresTenantRelations(snapshot.client, owned);
|
|
170
|
+
const described = await attestPostgresTenantRelations(snapshot.client, owned, security);
|
|
786
171
|
if (definitions !== undefined && described !== definitions)
|
|
787
172
|
return denyTenant();
|
|
788
173
|
await assertPostgresTenantStore(snapshot.client, owner);
|
|
@@ -801,9 +186,15 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
801
186
|
// owned pool even when catalog or durable store evidence refuses.
|
|
802
187
|
await reader.readSnapshot(async (snapshot) => {
|
|
803
188
|
await assertCatalog(snapshot);
|
|
804
|
-
}, false, prologue);
|
|
189
|
+
}, false, prologue, false, false, null);
|
|
805
190
|
// Asked once, at construction: row scopes inline on Postgres 12 and later (rowScope.ts).
|
|
806
191
|
const style = await reader.scopeStyle();
|
|
192
|
+
/* What crosses tenants by design — a public address resolved, the jobs' sweep, every tenant's names — reads on
|
|
193
|
+
the owner's pool under the backstop, which the policy does not bind; on the primary alone, as a write's fence
|
|
194
|
+
does. Attested in full on its first snapshot, as a renewed pool is. Opened once construction has stood. */
|
|
195
|
+
const ownerTarget = backstop
|
|
196
|
+
? { url, reader: tenantAdapter(url, 'Private tenant owner reader', false, 'owner'), owner: true }
|
|
197
|
+
: undefined;
|
|
807
198
|
let closing = false;
|
|
808
199
|
let closed;
|
|
809
200
|
const pending = new Set();
|
|
@@ -818,6 +209,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
818
209
|
await Promise.allSettled([...pending]);
|
|
819
210
|
try {
|
|
820
211
|
await Promise.all([...standbys.pools.map((standby) => standby.reader), ...retired].map((standby) => standby.close().catch(() => { })));
|
|
212
|
+
await ownerTarget?.reader.close();
|
|
821
213
|
await primary.reader.close();
|
|
822
214
|
}
|
|
823
215
|
catch {
|
|
@@ -846,7 +238,9 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
846
238
|
const renew = (target, failed) => {
|
|
847
239
|
if (closing || target.reader !== failed)
|
|
848
240
|
return;
|
|
849
|
-
target.reader =
|
|
241
|
+
target.reader = target.owner
|
|
242
|
+
? tenantAdapter(target.url, 'Private tenant owner reader', false, 'owner')
|
|
243
|
+
: tenantAdapter(target.url, target === primary ? 'Private tenant reader' : 'Private tenant replica reader', target !== primary);
|
|
850
244
|
target.attested = target.checked = undefined;
|
|
851
245
|
if (lent.has(failed))
|
|
852
246
|
retired.add(failed);
|
|
@@ -894,7 +288,10 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
894
288
|
};
|
|
895
289
|
const on = async (target, work, fence, lead,
|
|
896
290
|
/* The fence's membership is the snapshot's whole question (assertCurrent): nothing else is read on it. */
|
|
897
|
-
alone = false
|
|
291
|
+
alone = false,
|
|
292
|
+
/* What the row policy reads under the backstop (tenantConnections.ts): the tenant this snapshot is for, `null`
|
|
293
|
+
where it reads no tenant's rows. Without a backstop nothing is sent for it. */
|
|
294
|
+
tenant) => {
|
|
898
295
|
const pool = target.reader;
|
|
899
296
|
const standing = trusted(target);
|
|
900
297
|
/* Where the snapshot is a member's read, its membership rides in the
|
|
@@ -933,7 +330,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
933
330
|
}));
|
|
934
331
|
active();
|
|
935
332
|
return result;
|
|
936
|
-
}, false, `${pin}; ${question}`, false, true);
|
|
333
|
+
}, false, `${pin}; ${question}`, false, true, tenant);
|
|
937
334
|
if (answered !== MOVED)
|
|
938
335
|
return answered;
|
|
939
336
|
}
|
|
@@ -978,7 +375,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
978
375
|
}));
|
|
979
376
|
active();
|
|
980
377
|
return result;
|
|
981
|
-
}, false, `${standing === undefined ? prologue : pin}; ${membership}`, true);
|
|
378
|
+
}, false, `${standing === undefined ? prologue : pin}; ${membership}`, true, false, tenant);
|
|
982
379
|
/* A membership read that is the snapshot's whole reason (discovery's, an admission's) rides in the message
|
|
983
380
|
that begins it, as a member's fence does above: one round trip where BEGIN and the read were two. Its rows
|
|
984
381
|
reach the work only once the catalog — and the store with it — has stood. */
|
|
@@ -1000,7 +397,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1000
397
|
}));
|
|
1001
398
|
active();
|
|
1002
399
|
return result;
|
|
1003
|
-
}, false, `${standing === undefined ? prologue : pin}; ${lead}
|
|
400
|
+
}, false, `${standing === undefined ? prologue : pin}; ${lead}`, false, false, tenant);
|
|
1004
401
|
}
|
|
1005
402
|
/* Any other snapshot on an attestation that stands is handed over with its BEGIN in flight (`ahead`), as a
|
|
1006
403
|
member's is: its first statements leave behind it, one round trip where BEGIN and they were two, and no
|
|
@@ -1025,13 +422,13 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1025
422
|
await opened;
|
|
1026
423
|
active();
|
|
1027
424
|
return result;
|
|
1028
|
-
}, false, pin, true);
|
|
425
|
+
}, false, pin, true, false, tenant);
|
|
1029
426
|
return await pool.readSnapshot(async (lease) => {
|
|
1030
427
|
const store = await assertCatalog(lease, target, standing, pool);
|
|
1031
428
|
const result = await work(Object.freeze({ client: store, describe: lease.describe, execute: lease.execute }));
|
|
1032
429
|
active();
|
|
1033
430
|
return result;
|
|
1034
|
-
}, false, prologue);
|
|
431
|
+
}, false, prologue, false, false, tenant);
|
|
1035
432
|
}
|
|
1036
433
|
catch (error) {
|
|
1037
434
|
if (schemaMoved(error))
|
|
@@ -1053,9 +450,9 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1053
450
|
};
|
|
1054
451
|
/* A replica's failure is never the answer: the primary decides. Only a
|
|
1055
452
|
stopped statement — the caller cancelled it — is not asked again. */
|
|
1056
|
-
const onReplica = async (target, work, fence, lead, alone = false) => {
|
|
453
|
+
const onReplica = async (target, work, fence, lead, alone = false, tenant) => {
|
|
1057
454
|
try {
|
|
1058
|
-
return await on(target, work, fence, lead, alone);
|
|
455
|
+
return await on(target, work, fence, lead, alone, tenant);
|
|
1059
456
|
}
|
|
1060
457
|
catch (error) {
|
|
1061
458
|
if (error instanceof StatementStopped)
|
|
@@ -1066,16 +463,24 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1066
463
|
throw new ReplicaUnavailable(replicaFailure(error) ?? 'retry', 'The replica could not answer this read.', { cause: error });
|
|
1067
464
|
}
|
|
1068
465
|
};
|
|
1069
|
-
|
|
466
|
+
/* `route`: the tenant the snapshot reads for, `null` for none, or OWNER for what crosses tenants by design, which
|
|
467
|
+
under the backstop reads on the owner's pool, on the primary alone (tenantConnections.ts). Without a backstop
|
|
468
|
+
OWNER is today's read with no tenant, and no tenant is ever sent. */
|
|
469
|
+
const snapshot = async (work, fence, lead, alone = false, route) => {
|
|
1070
470
|
active();
|
|
1071
|
-
|
|
471
|
+
if (route === OWNER && ownerTarget)
|
|
472
|
+
return on(ownerTarget, work, fence, lead, alone, null);
|
|
473
|
+
const tenant = route === OWNER ? null : route;
|
|
474
|
+
return standbys.read((target) => onReplica(target, work, fence, lead, alone, tenant), () => on(primary, work, fence, lead, alone, tenant));
|
|
1072
475
|
};
|
|
1073
476
|
/* `fence`: the member evidence the work revalidates first (`current`), so the snapshot may read it as it begins. */
|
|
1074
477
|
const invoke = (input, work, fence,
|
|
1075
478
|
/* The statement the snapshot begins with, its rows the work's `lease.lead` (`on`). */
|
|
1076
479
|
lead,
|
|
1077
480
|
/* The fence's membership is all the work reads (`on`'s `alone`). */
|
|
1078
|
-
alone = false
|
|
481
|
+
alone = false,
|
|
482
|
+
/* Where the snapshot reads (`snapshot`'s `route`). Left out, a backstop's adapter refuses before anything is sent. */
|
|
483
|
+
route) => {
|
|
1079
484
|
let value;
|
|
1080
485
|
try {
|
|
1081
486
|
active();
|
|
@@ -1091,7 +496,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1091
496
|
pending.add(held);
|
|
1092
497
|
return (async () => {
|
|
1093
498
|
try {
|
|
1094
|
-
const result = await snapshot(async (lease) => work(value, lease), fence?.(value), lead?.(value), alone);
|
|
499
|
+
const result = await snapshot(async (lease) => work(value, lease), fence?.(value), lead?.(value), alone, route?.(value));
|
|
1095
500
|
active();
|
|
1096
501
|
return result;
|
|
1097
502
|
}
|
|
@@ -1244,7 +649,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1244
649
|
await shared.ended;
|
|
1245
650
|
await proved;
|
|
1246
651
|
return shared.fence && (await releaseFence(lease, evidence));
|
|
1247
|
-
}, (evidence) => evidence));
|
|
652
|
+
}, (evidence) => evidence, undefined, false, (evidence) => evidence.tenantId));
|
|
1248
653
|
shared.closed = held;
|
|
1249
654
|
held.catch(reject);
|
|
1250
655
|
});
|
|
@@ -1284,9 +689,12 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1284
689
|
const shared = sharing.getStore();
|
|
1285
690
|
if (shared?.open && !(standbys.pools.length && replicaReadWanted()))
|
|
1286
691
|
return onShared(shared, input, work);
|
|
1287
|
-
|
|
692
|
+
// A store's rows, which no policy binds: no tenant is needed to read them.
|
|
693
|
+
return invoke(input, work, undefined, undefined, false, () => null);
|
|
1288
694
|
};
|
|
1289
|
-
const bind = (scope, revalidate, fence
|
|
695
|
+
const bind = (scope, revalidate, fence,
|
|
696
|
+
/* Where each of its snapshots reads (`snapshot`): the tenant its scope is for, or OWNER for the platform's. */
|
|
697
|
+
route) => {
|
|
1290
698
|
const reads = scoped(scope);
|
|
1291
699
|
const fenceKey = fence === undefined ? undefined : JSON.stringify(fence);
|
|
1292
700
|
const run = (args, work) => {
|
|
@@ -1310,7 +718,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1310
718
|
void answer.catch(() => { });
|
|
1311
719
|
await proved;
|
|
1312
720
|
return answer;
|
|
1313
|
-
}, fence === undefined ? undefined : () => fence);
|
|
721
|
+
}, fence === undefined ? undefined : () => fence, undefined, false, () => route);
|
|
1314
722
|
};
|
|
1315
723
|
return Object.freeze({
|
|
1316
724
|
query: (request) => run([request], (bound, value) => bound.query(value)),
|
|
@@ -1328,7 +736,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1328
736
|
const batch = (input, open, work, fence,
|
|
1329
737
|
/* For a fenced batch whose membership rides with its BEGIN (`fenced.ahead`): the scope, composed from the
|
|
1330
738
|
evidence alone, and the revalidation, asked of the same flight rather than waited on before the reads. */
|
|
1331
|
-
ahead) => {
|
|
739
|
+
ahead, route) => {
|
|
1332
740
|
if (typeof work !== 'function')
|
|
1333
741
|
return Promise.reject(new TenantContextError());
|
|
1334
742
|
let own;
|
|
@@ -1368,7 +776,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1368
776
|
if (answer.status === 'rejected')
|
|
1369
777
|
throw answer.reason;
|
|
1370
778
|
return answer.value;
|
|
1371
|
-
}, fence).catch((error) => {
|
|
779
|
+
}, fence, undefined, false, route).catch((error) => {
|
|
1372
780
|
throw own ? own.error : error;
|
|
1373
781
|
});
|
|
1374
782
|
};
|
|
@@ -1386,17 +794,19 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1386
794
|
ownership: owned,
|
|
1387
795
|
/* Each read in the message that begins its snapshot (`on`'s `lead`), judged as the parameterized read judges its
|
|
1388
796
|
own (tenantStorePostgres.ts). */
|
|
1389
|
-
readMembership: (lookup) => invoke(lookup, (query, lease) => postgresTenantMembershipLookupRows(owner, query, lease.lead ?? denyTenant()), undefined, (query) => postgresTenantMembershipLookupStatement(owner, query)
|
|
797
|
+
readMembership: (lookup) => invoke(lookup, (query, lease) => postgresTenantMembershipLookupRows(owner, query, lease.lead ?? denyTenant()), undefined, (query) => postgresTenantMembershipLookupStatement(owner, query), false,
|
|
798
|
+
// The membership store's rows, which no policy binds.
|
|
799
|
+
() => null),
|
|
1390
800
|
readMemberships: (lookup) => {
|
|
1391
801
|
let asked;
|
|
1392
|
-
return invoke(lookup, (query, lease) => postgresTenantMembershipsRows(owner, query, lease.lead ?? denyTenant(), asked.instant, clock), undefined, (query) => (asked = postgresTenantMembershipsStatement(owner, query, clock)).sql);
|
|
802
|
+
return invoke(lookup, (query, lease) => postgresTenantMembershipsRows(owner, query, lease.lead ?? denyTenant(), asked.instant, clock), undefined, (query) => (asked = postgresTenantMembershipsStatement(owner, query, clock)).sql, false, () => null);
|
|
1393
803
|
},
|
|
1394
|
-
assertCurrent: (evidence) => invoke(evidence, (value, lease) => current(lease, value), (value) => value, undefined, true),
|
|
804
|
+
assertCurrent: (evidence) => invoke(evidence, (value, lease) => current(lease, value), (value) => value, undefined, true, (value) => value.tenantId),
|
|
1395
805
|
scopeReads: (evidence, rows) => invoke({ evidence, rows }, async (captured, lease) => {
|
|
1396
806
|
await current(lease, captured.evidence);
|
|
1397
|
-
return bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), (next) => current(next, captured.evidence), captured.evidence);
|
|
1398
|
-
}, (captured) => captured.evidence),
|
|
1399
|
-
bindReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), (next) => current(next, captured.evidence), captured.evidence)),
|
|
807
|
+
return bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), (next) => current(next, captured.evidence), captured.evidence, captured.evidence.tenantId);
|
|
808
|
+
}, (captured) => captured.evidence, undefined, false, (captured) => captured.evidence.tenantId),
|
|
809
|
+
bindReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue), (next) => current(next, captured.evidence), captured.evidence, captured.evidence.tenantId)),
|
|
1400
810
|
together: async (evidence, work) => {
|
|
1401
811
|
if (typeof work !== 'function')
|
|
1402
812
|
return Promise.reject(new TenantContextError());
|
|
@@ -1481,52 +891,57 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1481
891
|
}, work, (captured) => captured.evidence, {
|
|
1482
892
|
scope: (captured) => composeTenantReadScope(owned, captured.evidence, captured.rows, allTrue),
|
|
1483
893
|
proof: (captured, lease) => current(lease, captured.evidence),
|
|
1484
|
-
}),
|
|
894
|
+
}, (captured) => captured.evidence.tenantId),
|
|
1485
895
|
/* Its statement rides in the message that begins the snapshot (`lead`), as a membership list's does. */
|
|
1486
|
-
readPlatformMembership: (lookup) => invoke(lookup, (query, lease) => postgresPlatformMembershipRows(owner, query, lease.lead ?? denyTenant()), undefined, (query) => postgresPlatformMembershipStatement(owner, query)),
|
|
1487
|
-
|
|
896
|
+
readPlatformMembership: (lookup) => invoke(lookup, (query, lease) => postgresPlatformMembershipRows(owner, query, lease.lead ?? denyTenant()), undefined, (query) => postgresPlatformMembershipStatement(owner, query), false, () => null),
|
|
897
|
+
/* The platform's realm reads every tenant's rows its scope reaches, so it reads as the owner. */
|
|
898
|
+
assertPlatformCurrent: (evidence) => invoke(evidence, (value, lease) => currentPlatform(lease, value), undefined, undefined, false, () => OWNER),
|
|
1488
899
|
// The source recomposes the platform scope itself, as on SQLite.
|
|
1489
900
|
scopePlatformReads: (evidence, rows) => invoke({ evidence, rows }, async (captured, lease) => {
|
|
1490
901
|
await currentPlatform(lease, captured.evidence);
|
|
1491
|
-
return bind(composePlatformReadScope(owned, captured.evidence, captured.rows), (next) => currentPlatform(next, captured.evidence));
|
|
1492
|
-
}),
|
|
1493
|
-
bindPlatformReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePlatformReadScope(owned, captured.evidence, captured.rows), (next) => currentPlatform(next, captured.evidence))),
|
|
902
|
+
return bind(composePlatformReadScope(owned, captured.evidence, captured.rows), (next) => currentPlatform(next, captured.evidence), undefined, OWNER);
|
|
903
|
+
}, undefined, undefined, false, () => OWNER),
|
|
904
|
+
bindPlatformReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePlatformReadScope(owned, captured.evidence, captured.rows), (next) => currentPlatform(next, captured.evidence), undefined, OWNER)),
|
|
1494
905
|
withPlatformReads: (evidence, rows, work) => batch({ evidence, rows }, async (captured, lease) => {
|
|
1495
906
|
await currentPlatform(lease, captured.evidence);
|
|
1496
907
|
return composePlatformReadScope(owned, captured.evidence, captured.rows);
|
|
1497
|
-
}, work),
|
|
1498
|
-
assertSupportCurrent: (evidence) => invoke(evidence, (value, lease) => currentSupport(lease, value)),
|
|
908
|
+
}, work, undefined, undefined, () => OWNER),
|
|
909
|
+
assertSupportCurrent: (evidence) => invoke(evidence, (value, lease) => currentSupport(lease, value), undefined, undefined, false, (value) => value.tenantId),
|
|
1499
910
|
/* The dealer's scope, composed by the same rule its own members get, on a
|
|
1500
911
|
member view of the support evidence — one rule, so a second cannot
|
|
1501
912
|
disagree and quietly widen what a session reads. Fenced on the support
|
|
1502
913
|
row, as on SQLite. */
|
|
1503
914
|
scopeSupportReads: (evidence, rows) => invoke({ evidence, rows }, async (captured, lease) => {
|
|
1504
915
|
await currentSupport(lease, captured.evidence);
|
|
1505
|
-
return bind(composeTenantReadScope(owned, supportAsMember(captured.evidence), captured.rows, allTrue), (next) => currentSupport(next, captured.evidence));
|
|
1506
|
-
}),
|
|
1507
|
-
|
|
1508
|
-
|
|
916
|
+
return bind(composeTenantReadScope(owned, supportAsMember(captured.evidence), captured.rows, allTrue), (next) => currentSupport(next, captured.evidence), undefined, captured.evidence.tenantId);
|
|
917
|
+
}, undefined, undefined, false, (captured) => captured.evidence.tenantId),
|
|
918
|
+
/* An address is resolved among every tenant's, so as the owner; what the visitor then reads is that tenant's
|
|
919
|
+
alone, its placement proved again under the tenant it resolved to (assertPublicCurrent, the bound reads). */
|
|
920
|
+
resolvePublic: (lookup) => invoke(lookup, (value, lease) => resolvePublicIn(lease, value), undefined, undefined, false, () => OWNER),
|
|
921
|
+
assertPublicCurrent: (evidence) => invoke(evidence, (value, lease) => currentPublic(lease, value), undefined, undefined, false, (value) => value.tenantId),
|
|
1509
922
|
// The source recomposes the visitor's scope itself, as on SQLite.
|
|
1510
|
-
bindPublicReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePublicReadScope(owned, captured.evidence, captured.rows), (next) => currentPublic(next, captured.evidence))),
|
|
923
|
+
bindPublicReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => bind(composePublicReadScope(owned, captured.evidence, captured.rows), (next) => currentPublic(next, captured.evidence), undefined, captured.evidence.tenantId)),
|
|
1511
924
|
bindIntakeReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
1512
925
|
const value = captureIntakeEvidence(captured.evidence, owned);
|
|
1513
926
|
if (intakePlace(value) !== 'tenant')
|
|
1514
927
|
throw new AppAccessDenied();
|
|
1515
928
|
const visitor = intakeVisitor(value);
|
|
1516
|
-
return bind(composePublicReadScope(owned, visitor, captured.rows), (next) => currentPublic(next, visitor));
|
|
929
|
+
return bind(composePublicReadScope(owned, visitor, captured.rows), (next) => currentPublic(next, visitor), undefined, visitor.tenantId);
|
|
1517
930
|
}),
|
|
1518
931
|
// The source recomposes the service's scope itself, as on SQLite.
|
|
1519
932
|
bindServiceReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
1520
933
|
const value = captureServiceEvidence(captured.evidence, owned);
|
|
1521
934
|
if (value.place !== 'tenant')
|
|
1522
935
|
throw new AppAccessDenied();
|
|
1523
|
-
|
|
936
|
+
// `place` is 'tenant', so the service has its tenant.
|
|
937
|
+
return bind(composeServiceScope(owned, value, captured.rows), (next) => currentService(next, value), undefined, value.tenantId);
|
|
1524
938
|
}),
|
|
1525
939
|
// The source recomposes the link's scope itself, as on SQLite.
|
|
1526
940
|
bindLinkReads: (evidence, rows) => bindLater({ evidence, rows }, (captured) => {
|
|
1527
941
|
const value = captureLinkEvidence(captured.evidence, owned);
|
|
1528
|
-
return bind(composeLinkScope(owned, value, captured.rows), (next) => currentLink(next, value));
|
|
942
|
+
return bind(composeLinkScope(owned, value, captured.rows), (next) => currentLink(next, value), undefined, value.tenantId);
|
|
1529
943
|
}),
|
|
944
|
+
/* A link, an intake address and the anchors are found among every tenant's rows: as the owner. */
|
|
1530
945
|
resolveLink: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1531
946
|
const { id } = captureLinkLookup(query);
|
|
1532
947
|
const row = await findPostgresLink(lease.client, owner.appId, id, clock());
|
|
@@ -1536,7 +951,7 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1536
951
|
for (const tenant of [PLATFORM_TENANT_ID, row.tenant])
|
|
1537
952
|
anchors.set(tenant, await readPostgresIntakeAnchor(lease.client, owner, tenant));
|
|
1538
953
|
return linkResolution(row, (tenant) => anchors.get(tenant) ?? null);
|
|
1539
|
-
}),
|
|
954
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1540
955
|
resolveIntake: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1541
956
|
const value = captureIntakeLookup(query, owner), sql = intakeAddressQuery(owned, value.column);
|
|
1542
957
|
const tenantId = value.address === null ? undefined : intakeTenantOf((await lease.client.query(sql, [value.address])).rows);
|
|
@@ -1545,21 +960,22 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1545
960
|
anchors.set(id, await readPostgresIntakeAnchor(lease.client, owner, id));
|
|
1546
961
|
}
|
|
1547
962
|
return intakeResolution(tenantId, (id) => anchors.get(id) ?? null);
|
|
1548
|
-
}),
|
|
963
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1549
964
|
resolveIntakeByTenant: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1550
965
|
const value = captureIntakeTenantLookup(query, owner);
|
|
1551
966
|
const anchors = new Map();
|
|
1552
967
|
for (const id of [value.tenantId, PLATFORM_TENANT_ID])
|
|
1553
968
|
anchors.set(id, await readPostgresIntakeAnchor(lease.client, owner, id));
|
|
1554
969
|
return intakeResolutionByTenant(value.tenantId, (id) => anchors.get(id) ?? null);
|
|
1555
|
-
}),
|
|
970
|
+
}, undefined, undefined, false, () => OWNER),
|
|
971
|
+
/* What the platform and the jobs read across tenants by design (docs/postgresql.md): as the owner. */
|
|
1556
972
|
readTenantNames: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1557
973
|
const asked = namesLookup(owned, query);
|
|
1558
974
|
if (!asked)
|
|
1559
975
|
return Object.freeze({});
|
|
1560
976
|
const { rows } = await lease.client.query(tenantNamesQuery(owned, asked.plan, asked.tenants.length), [asked.tenants]);
|
|
1561
977
|
return tenantNamesOf(asked.plan, rows, asked.tenants);
|
|
1562
|
-
}),
|
|
978
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1563
979
|
readTenantAddresses: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1564
980
|
const { tenantId, columns } = captureAddressesLookup(owned, query);
|
|
1565
981
|
const found = {};
|
|
@@ -1574,15 +990,15 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1574
990
|
found[kind] = own;
|
|
1575
991
|
}
|
|
1576
992
|
return Object.freeze(found);
|
|
1577
|
-
}),
|
|
993
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1578
994
|
readTenantProfile: (lookup) => invoke(lookup, async (query, lease) => {
|
|
1579
995
|
const { tenantId, columns } = captureProfileLookup(owned, query);
|
|
1580
996
|
return heldProfile(owned, columns, (await lease.client.query(tenantProfileQuery(owned, columns), [tenantId])).rows);
|
|
1581
|
-
}),
|
|
997
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1582
998
|
readServiceAnchors: (tenantId) => invoke(tenantId, async (id, lease) => Object.freeze({
|
|
1583
999
|
tenantVersion: id === null ? null : await readPostgresIntakeAnchor(lease.client, owner, parseTenantSelector([id])),
|
|
1584
1000
|
platformVersion: await readPostgresIntakeAnchor(lease.client, owner, PLATFORM_TENANT_ID),
|
|
1585
|
-
})),
|
|
1001
|
+
}), undefined, undefined, false, () => OWNER),
|
|
1586
1002
|
readActiveTenants: (options) => invoke(options ?? {}, async (query, lease) => {
|
|
1587
1003
|
const zone = activeTenantsZone(owned, query);
|
|
1588
1004
|
const active = await readPostgresActiveTenants(lease.client, owner);
|
|
@@ -1590,30 +1006,31 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1590
1006
|
return Object.freeze(active.map((id) => Object.freeze({ id })));
|
|
1591
1007
|
const rows = (await lease.client.query(tenantProfileColumnQuery(owned, zone))).rows;
|
|
1592
1008
|
return activeWithColumn(owned, zone, active, rows);
|
|
1593
|
-
}),
|
|
1009
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1594
1010
|
readSandboxClocks: () => invoke(null, async (_none, lease) => {
|
|
1595
1011
|
const rows = postgresSandboxSession(lease.client);
|
|
1596
1012
|
return (await sandboxStorePresent(rows)) ? sandboxRows(rows, owner.appId).clocks() : Object.freeze([]);
|
|
1597
|
-
}),
|
|
1013
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1598
1014
|
readSandboxes: (tenants) => invoke(sandboxLookup(tenants), async (asked, lease) => {
|
|
1599
1015
|
const rows = postgresSandboxSession(lease.client);
|
|
1600
1016
|
return (await sandboxStorePresent(rows)) ? sandboxRows(rows, owner.appId).among(asked) : new Set();
|
|
1601
|
-
}),
|
|
1017
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1602
1018
|
readDueTimers: (query) => invoke(query, async (value, lease) => {
|
|
1603
1019
|
const plan = dueTimersPlan(owned, value);
|
|
1604
1020
|
const active = plan.tenants ? await readPostgresActiveTenants(lease.client, owner) : [];
|
|
1605
1021
|
if (plan.tenants && !active.length)
|
|
1606
1022
|
return Object.freeze([]);
|
|
1607
1023
|
return plan.decode((await lease.client.query(plan.sql, plan.params(active))).rows);
|
|
1608
|
-
}),
|
|
1024
|
+
}, undefined, undefined, false, () => OWNER),
|
|
1025
|
+
// The stores' own rows, which no policy binds: read as the runtime, with no tenant said.
|
|
1609
1026
|
readRecordHistory: (query) => invoke(query, (value, lease) => {
|
|
1610
1027
|
assertHistoryQuery(value);
|
|
1611
1028
|
return attestedStore(lease, 'history', (attested) => readPostgresRecordHistory(lease.client, value, attested));
|
|
1612
|
-
}),
|
|
1029
|
+
}, undefined, undefined, false, () => null),
|
|
1613
1030
|
readRecordActivity: (query) => invoke(query, (value, lease) => {
|
|
1614
1031
|
assertActivityQuery(value);
|
|
1615
1032
|
return attestedStore(lease, 'history', (attested) => readPostgresRecordActivity(lease.client, value, attested));
|
|
1616
|
-
}),
|
|
1033
|
+
}, undefined, undefined, false, () => null),
|
|
1617
1034
|
readInbox: (query) => inRequest(query, (value, lease) => {
|
|
1618
1035
|
assertInboxQuery(value);
|
|
1619
1036
|
return attestedStore(lease, 'inbox', (attested) => readPostgresInbox(lease.client, value, attested));
|
|
@@ -1625,11 +1042,11 @@ export async function openPostgresTenantReadSource(url, ownership, store, now =
|
|
|
1625
1042
|
readBranding: (query) => invoke(query, (value, lease) => {
|
|
1626
1043
|
assertBrandingQuery(value);
|
|
1627
1044
|
return attestedStore(lease, 'branding', (attested) => readPostgresBranding(lease.client, value, attested));
|
|
1628
|
-
}),
|
|
1045
|
+
}, undefined, undefined, false, () => null),
|
|
1629
1046
|
readBrandingLogo: (query) => invoke(query, (value, lease) => {
|
|
1630
1047
|
assertBrandingLogoQuery(value);
|
|
1631
1048
|
return attestedStore(lease, 'branding', (attested) => readPostgresBrandingLogo(lease.client, value, attested));
|
|
1632
|
-
}),
|
|
1049
|
+
}, undefined, undefined, false, () => null),
|
|
1633
1050
|
close,
|
|
1634
1051
|
});
|
|
1635
1052
|
sources.add(source);
|