@aglyn/tenant-data-admin 1.12.0 → 1.13.0
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/package.json +7 -7
- package/src/lib/server/erase.js +9 -0
- package/src/lib/server/erase.js.map +1 -1
- package/src/lib/server/placement-access.d.ts +38 -0
- package/src/lib/server/placement-access.js +51 -0
- package/src/lib/server/placement-access.js.map +1 -0
- package/src/lib/server/scope-sharing-sweep.d.ts +114 -0
- package/src/lib/server/scope-sharing-sweep.js +380 -0
- package/src/lib/server/scope-sharing-sweep.js.map +1 -0
- package/src/lib/server/serve-media-cdn.js +6 -5
- package/src/lib/server/serve-media-cdn.js.map +1 -1
- package/src/lib/server/transfer-host.js +16 -0
- package/src/lib/server/transfer-host.js.map +1 -1
- package/src/lib/server/visitor-upload.d.ts +28 -0
- package/src/lib/server/visitor-upload.js +81 -9
- package/src/lib/server/visitor-upload.js.map +1 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import type { AglynOrgMember, HostAccessRole } from '@aglyn/aglyn/foundation/definitions/organization.types';
|
|
18
|
+
/**
|
|
19
|
+
* May this member act on a workspace resource placed at `visibleTo` (AGL-4047),
|
|
20
|
+
* by the verdict in `site-placement`? The Admin SDK evaluates no rules, so this
|
|
21
|
+
* repeats the rule for the routes, reading what it reads: the site's stamp, the
|
|
22
|
+
* workspace it belongs to and its suspension. One read of the site, and none for
|
|
23
|
+
* a member the workspace key already admitted.
|
|
24
|
+
*/
|
|
25
|
+
export declare function memberMayActOnPlaced(input: {
|
|
26
|
+
firestore: FirebaseFirestore.Firestore;
|
|
27
|
+
orgId: string;
|
|
28
|
+
uid: string;
|
|
29
|
+
member: Partial<AglynOrgMember>;
|
|
30
|
+
visibleTo: unknown;
|
|
31
|
+
/** The member's answer on the workspace key, which the caller has resolved. */
|
|
32
|
+
workspace: boolean;
|
|
33
|
+
workspaceKey: string;
|
|
34
|
+
siteKey: string;
|
|
35
|
+
sitePreset: Readonly<Partial<Record<HostAccessRole, boolean>>>;
|
|
36
|
+
}): Promise<boolean>;
|
|
37
|
+
/** The key a refusal names: the site's for a resource on one site, else the workspace's. */
|
|
38
|
+
export declare function placedRefusalPermission(visibleTo: unknown, workspaceKey: string, siteKey: string): string;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/ import { oneSitePlacement, placementVerdict, stampedPlacementKey } from "@aglyn/aglyn/app-utils/site-placement";
|
|
17
|
+
import { isLockdownActive } from "@aglyn/aglyn/app-utils/lockdown";
|
|
18
|
+
/**
|
|
19
|
+
* May this member act on a workspace resource placed at `visibleTo` (AGL-4047),
|
|
20
|
+
* by the verdict in `site-placement`? The Admin SDK evaluates no rules, so this
|
|
21
|
+
* repeats the rule for the routes, reading what it reads: the site's stamp, the
|
|
22
|
+
* workspace it belongs to and its suspension. One read of the site, and none for
|
|
23
|
+
* a member the workspace key already admitted.
|
|
24
|
+
*/ export async function memberMayActOnPlaced(input) {
|
|
25
|
+
const { firestore, orgId, uid, member, visibleTo, workspace, workspaceKey, siteKey, sitePreset } = input;
|
|
26
|
+
return placementVerdict({
|
|
27
|
+
member,
|
|
28
|
+
visibleTo,
|
|
29
|
+
workspace,
|
|
30
|
+
site: async (hostId)=>{
|
|
31
|
+
const site = await firestore.collection('hosts').doc(hostId).get();
|
|
32
|
+
if (!site.exists || site.get('orgId') !== orgId) return false;
|
|
33
|
+
const memberRoles = site.get('memberRoles');
|
|
34
|
+
if ((memberRoles == null ? void 0 : memberRoles[uid]) == null) return false;
|
|
35
|
+
if (site.get('suspendedAt') != null && isLockdownActive({
|
|
36
|
+
untilMs: site.get('suspendedUntilMs')
|
|
37
|
+
}, Date.now())) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return stampedPlacementKey({
|
|
41
|
+
memberPermissions: site.get('memberPermissions'),
|
|
42
|
+
memberRoles
|
|
43
|
+
}, uid, siteKey, sitePreset, member, workspaceKey);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** The key a refusal names: the site's for a resource on one site, else the workspace's. */ export function placedRefusalPermission(visibleTo, workspaceKey, siteKey) {
|
|
48
|
+
return oneSitePlacement(visibleTo) ? siteKey : workspaceKey;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=placement-access.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/placement-access.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { oneSitePlacement, placementVerdict, stampedPlacementKey } from '@aglyn/aglyn/app-utils/site-placement'\nimport { isLockdownActive } from '@aglyn/aglyn/app-utils/lockdown'\nimport type { AglynOrgMember, HostAccessRole } from '@aglyn/aglyn/foundation/definitions/organization.types'\n\n/**\n * May this member act on a workspace resource placed at `visibleTo` (AGL-4047),\n * by the verdict in `site-placement`? The Admin SDK evaluates no rules, so this\n * repeats the rule for the routes, reading what it reads: the site's stamp, the\n * workspace it belongs to and its suspension. One read of the site, and none for\n * a member the workspace key already admitted.\n */\nexport async function memberMayActOnPlaced(input: {\n firestore: FirebaseFirestore.Firestore\n orgId: string\n uid: string\n member: Partial<AglynOrgMember>\n visibleTo: unknown\n /** The member's answer on the workspace key, which the caller has resolved. */\n workspace: boolean\n workspaceKey: string\n siteKey: string\n sitePreset: Readonly<Partial<Record<HostAccessRole, boolean>>>\n}): Promise<boolean> {\n const { firestore, orgId, uid, member, visibleTo, workspace, workspaceKey, siteKey, sitePreset } = input\n return placementVerdict<Promise<boolean>>({\n member,\n visibleTo,\n workspace,\n site: async (hostId) => {\n const site = await firestore.collection('hosts').doc(hostId).get()\n if (!site.exists || site.get('orgId') !== orgId) return false\n const memberRoles = site.get('memberRoles') as Record<string, unknown> | undefined\n if (memberRoles?.[uid] == null) return false\n if (site.get('suspendedAt') != null && isLockdownActive({ untilMs: site.get('suspendedUntilMs') }, Date.now())) {\n return false\n }\n return stampedPlacementKey(\n { memberPermissions: site.get('memberPermissions'), memberRoles },\n uid,\n siteKey,\n sitePreset,\n member,\n workspaceKey,\n )\n },\n })\n}\n\n/** The key a refusal names: the site's for a resource on one site, else the workspace's. */\nexport function placedRefusalPermission(visibleTo: unknown, workspaceKey: string, siteKey: string): string {\n return oneSitePlacement(visibleTo) ? siteKey : workspaceKey\n}\n"],"names":["oneSitePlacement","placementVerdict","stampedPlacementKey","isLockdownActive","memberMayActOnPlaced","input","firestore","orgId","uid","member","visibleTo","workspace","workspaceKey","siteKey","sitePreset","site","hostId","collection","doc","get","exists","memberRoles","untilMs","Date","now","memberPermissions","placedRefusalPermission"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,gBAAgB,EAAEC,gBAAgB,EAAEC,mBAAmB,QAAQ,wCAAuC;AAC/G,SAASC,gBAAgB,QAAQ,kCAAiC;AAGlE;;;;;;CAMC,GACD,OAAO,eAAeC,qBAAqBC,KAW1C;IACC,MAAM,EAAEC,SAAS,EAAEC,KAAK,EAAEC,GAAG,EAAEC,MAAM,EAAEC,SAAS,EAAEC,SAAS,EAAEC,YAAY,EAAEC,OAAO,EAAEC,UAAU,EAAE,GAAGT;IACnG,OAAOJ,iBAAmC;QACxCQ;QACAC;QACAC;QACAI,MAAM,OAAOC;YACX,MAAMD,OAAO,MAAMT,UAAUW,UAAU,CAAC,SAASC,GAAG,CAACF,QAAQG,GAAG;YAChE,IAAI,CAACJ,KAAKK,MAAM,IAAIL,KAAKI,GAAG,CAAC,aAAaZ,OAAO,OAAO;YACxD,MAAMc,cAAcN,KAAKI,GAAG,CAAC;YAC7B,IAAIE,CAAAA,+BAAAA,WAAa,CAACb,IAAI,KAAI,MAAM,OAAO;YACvC,IAAIO,KAAKI,GAAG,CAAC,kBAAkB,QAAQhB,iBAAiB;gBAAEmB,SAASP,KAAKI,GAAG,CAAC;YAAoB,GAAGI,KAAKC,GAAG,KAAK;gBAC9G,OAAO;YACT;YACA,OAAOtB,oBACL;gBAAEuB,mBAAmBV,KAAKI,GAAG,CAAC;gBAAsBE;YAAY,GAChEb,KACAK,SACAC,YACAL,QACAG;QAEJ;IACF;AACF;AAEA,0FAA0F,GAC1F,OAAO,SAASc,wBAAwBhB,SAAkB,EAAEE,YAAoB,EAAEC,OAAe;IAC/F,OAAOb,iBAAiBU,aAAaG,UAAUD;AACjD"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { type DocumentReference, type Firestore } from 'firebase-admin/firestore';
|
|
18
|
+
/** Where a sweep with work left records its cursor. */
|
|
19
|
+
export declare const SCOPE_SWEEPS_COLLECTION = "scopeSweeps";
|
|
20
|
+
/** How long a person's site change waits for the sweep. */
|
|
21
|
+
export declare const INLINE_BUDGET_MS = 3000;
|
|
22
|
+
/** How long one job beat spends, across every sweep it takes. */
|
|
23
|
+
export declare const JOB_BUDGET_MS = 20000;
|
|
24
|
+
/** Beats a failing sweep gets. The weekly drift report still counts its documents. */
|
|
25
|
+
export declare const MAX_SWEEP_ATTEMPTS = 5;
|
|
26
|
+
export type ScopeSweepReason = 'site-added' | 'site-removed' | 'drift-repair';
|
|
27
|
+
export interface ScopeSweepCounts {
|
|
28
|
+
/** Marked documents read. */
|
|
29
|
+
marked: number;
|
|
30
|
+
/** Written because they had fallen behind. */
|
|
31
|
+
changed: number;
|
|
32
|
+
/** Past their collection's limit, and so no longer widening. */
|
|
33
|
+
capped: number;
|
|
34
|
+
/** A marker the helper cannot act on: left alone, and reported. */
|
|
35
|
+
invalid: number;
|
|
36
|
+
/** A transaction or patch that threw. */
|
|
37
|
+
failed: number;
|
|
38
|
+
}
|
|
39
|
+
export interface ScopeSweepResult extends ScopeSweepCounts {
|
|
40
|
+
/** Every declared collection was walked to its end. */
|
|
41
|
+
complete: boolean;
|
|
42
|
+
/** Where to carry on from, when `complete` is false. */
|
|
43
|
+
cursor: ScopeSweepCursor | null;
|
|
44
|
+
}
|
|
45
|
+
/** A position in {@link listScopeSharingCollections}. */
|
|
46
|
+
export interface ScopeSweepCursor {
|
|
47
|
+
collection: string;
|
|
48
|
+
afterId: string | null;
|
|
49
|
+
}
|
|
50
|
+
/** The sites `orgs/{orgId}.hosts` names. */
|
|
51
|
+
export declare function orgHostIdsFromDoc(org: Readonly<Record<string, unknown>> | undefined): string[];
|
|
52
|
+
export interface RunScopeSharingSweepInput {
|
|
53
|
+
orgId: string;
|
|
54
|
+
/** Stops starting new work past this; omit to walk to the end. */
|
|
55
|
+
budgetMs?: number;
|
|
56
|
+
/** Resume from here. */
|
|
57
|
+
from?: ScopeSweepCursor | null;
|
|
58
|
+
/**
|
|
59
|
+
* Called the first time a marked document turns up, so a sweep that finds
|
|
60
|
+
* nothing writes nothing. A trigger records its sweep document here.
|
|
61
|
+
*/
|
|
62
|
+
onFirstMarked?: () => Promise<void>;
|
|
63
|
+
firestore?: Firestore;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Walks every declared collection's marked documents and brings each level.
|
|
67
|
+
* Throws only on a failed query; one document failing is counted and the walk
|
|
68
|
+
* goes on, and an incomplete walk answers where it stopped.
|
|
69
|
+
*/
|
|
70
|
+
export declare function runScopeSharingSweep(input: RunScopeSharingSweepInput): Promise<ScopeSweepResult>;
|
|
71
|
+
/**
|
|
72
|
+
* What a change to a workspace's sites runs, once the change has committed.
|
|
73
|
+
*
|
|
74
|
+
* NEVER THROWS, because the site change is done and is what the person asked
|
|
75
|
+
* for. Work it could not finish is left in a sweep document for the job, and
|
|
76
|
+
* if even that write fails the weekly drift report finds what is behind.
|
|
77
|
+
*/
|
|
78
|
+
export declare function sweepScopeSharingAfterSiteChange(input: {
|
|
79
|
+
orgId: string;
|
|
80
|
+
reason: ScopeSweepReason;
|
|
81
|
+
hostId?: string;
|
|
82
|
+
firestore?: Firestore;
|
|
83
|
+
}): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Carries on every unfinished sweep: the job's handler. A sweep document exists
|
|
86
|
+
* only while work is left, so the unfiltered query needs no index and answers
|
|
87
|
+
* nothing on a quiet beat, for one read.
|
|
88
|
+
*/
|
|
89
|
+
export declare function resumeScopeSweeps(options?: {
|
|
90
|
+
budgetMs?: number;
|
|
91
|
+
firestore?: Firestore;
|
|
92
|
+
}): Promise<{
|
|
93
|
+
resumed: number;
|
|
94
|
+
finished: number;
|
|
95
|
+
droppedAfterFailing: number;
|
|
96
|
+
}>;
|
|
97
|
+
/** What the weekly report asks of one workspace. Reads only. */
|
|
98
|
+
export interface ScopeSharingDrift {
|
|
99
|
+
/** Marked documents read. */
|
|
100
|
+
marked: number;
|
|
101
|
+
/** Should read differently from how they read: what a sweep would write. */
|
|
102
|
+
stale: number;
|
|
103
|
+
/** Past their collection's limit. A known state, reported and not alarmed. */
|
|
104
|
+
capped: number;
|
|
105
|
+
/** A marker that cannot be acted on. */
|
|
106
|
+
invalid: number;
|
|
107
|
+
}
|
|
108
|
+
export declare function emptyScopeSharingDrift(): ScopeSharingDrift;
|
|
109
|
+
/**
|
|
110
|
+
* How far one workspace's marked documents are from what a sweep would write,
|
|
111
|
+
* with the same helper the sweep uses. One query per declared collection when
|
|
112
|
+
* nothing is marked.
|
|
113
|
+
*/
|
|
114
|
+
export declare function scopeSharingDriftForOrg(orgRef: DocumentReference, orgData: Readonly<Record<string, unknown>> | undefined): Promise<ScopeSharingDrift>;
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ /**
|
|
18
|
+
* KEEPING "ALL SITES EXCEPT…" TRUE AS SITES COME AND GO (AGL-4047).
|
|
19
|
+
*
|
|
20
|
+
* A document shared this way names its included sites in `visibleTo`, the one
|
|
21
|
+
* field readers enforce. A new site therefore has to be added to it, and a
|
|
22
|
+
* site that left has to come off it. {@link sweepScopeSharingAfterSiteChange}
|
|
23
|
+
* does that after every change to `orgs/{orgId}.hosts`: a site created, moved
|
|
24
|
+
* in or out, or erased.
|
|
25
|
+
*
|
|
26
|
+
* ## What it costs when nothing is marked
|
|
27
|
+
*
|
|
28
|
+
* One query per declared collection, each answering nothing, and not one
|
|
29
|
+
* write. Every workspace pays that on a site change and nothing more, which
|
|
30
|
+
* is why a sweep document is only written once a marked document turns up.
|
|
31
|
+
*
|
|
32
|
+
* ## Each document in its own transaction, reading the org document
|
|
33
|
+
*
|
|
34
|
+
* The transaction reads `orgs/{orgId}` as well as the document, so what it
|
|
35
|
+
* writes is computed from the sites that are true at commit. A site claimed
|
|
36
|
+
* while the sweep runs either serializes before it (and the sweep sees the
|
|
37
|
+
* site) or after it (and the sweep that claim starts sees the document).
|
|
38
|
+
*
|
|
39
|
+
* ## Inline for a few seconds, then the job
|
|
40
|
+
*
|
|
41
|
+
* The caller is a person creating or deleting a site, so a trigger works for
|
|
42
|
+
* {@link INLINE_BUDGET_MS} and leaves the rest to the job, from a cursor in
|
|
43
|
+
* `orgs/{orgId}/scopeSweeps/{sweepId}`. That document exists only while work
|
|
44
|
+
* is left, so the job's query answers nothing on a quiet beat. No rule opens
|
|
45
|
+
* the path: only this server code reads or writes it.
|
|
46
|
+
*
|
|
47
|
+
* Every step is idempotent. A second sweep, or a resumed one going over a
|
|
48
|
+
* document again, reads `current` and writes nothing.
|
|
49
|
+
*/ import { createResourceUid } from "@aglyn/aglyn/app-utils/create-resource-uid";
|
|
50
|
+
import { reconcileAllExcept, SCOPE_MODE_ALL_EXCEPT } from "@aglyn/aglyn/app-utils/scope-tokens";
|
|
51
|
+
import { runPluginDeclarationsRepair } from "@aglyn/aglyn/plugin-manager/plugin-declarations-repair";
|
|
52
|
+
import { listScopeSharingCollections, scopeSharingPatchFor } from "@aglyn/aglyn/plugin-manager/plugin-scope-sharing";
|
|
53
|
+
import { FieldPath, FieldValue } from "firebase-admin/firestore";
|
|
54
|
+
import firebaseAdmin from "./firebase-admin.js";
|
|
55
|
+
/** Where a sweep with work left records its cursor. */ export const SCOPE_SWEEPS_COLLECTION = 'scopeSweeps';
|
|
56
|
+
/** Marked documents read at a time in one collection. */ const PAGE_SIZE = 100;
|
|
57
|
+
/** Transactions running at once. */ const CONCURRENCY = 8;
|
|
58
|
+
/** How long a person's site change waits for the sweep. */ export const INLINE_BUDGET_MS = 3000;
|
|
59
|
+
/** How long one job beat spends, across every sweep it takes. */ export const JOB_BUDGET_MS = 20000;
|
|
60
|
+
/** Sweeps one beat reads. */ const SWEEPS_PER_BEAT = 10;
|
|
61
|
+
/** Beats a failing sweep gets. The weekly drift report still counts its documents. */ export const MAX_SWEEP_ATTEMPTS = 5;
|
|
62
|
+
const emptyCounts = ()=>({
|
|
63
|
+
marked: 0,
|
|
64
|
+
changed: 0,
|
|
65
|
+
capped: 0,
|
|
66
|
+
invalid: 0,
|
|
67
|
+
failed: 0
|
|
68
|
+
});
|
|
69
|
+
const platformFirestore = ()=>firebaseAdmin.app().firestore();
|
|
70
|
+
/** The sites `orgs/{orgId}.hosts` names. */ export function orgHostIdsFromDoc(org) {
|
|
71
|
+
const hosts = org == null ? void 0 : org['hosts'];
|
|
72
|
+
if (!hosts || typeof hosts !== 'object' || Array.isArray(hosts)) return [];
|
|
73
|
+
return Object.entries(hosts).filter(([, on])=>Boolean(on)).map(([hostId])=>hostId);
|
|
74
|
+
}
|
|
75
|
+
/** The sites a document keeps whatever it excludes, per its declaration. */ function keptHostIds(declared, doc) {
|
|
76
|
+
var _declared_keepSiteFields;
|
|
77
|
+
const kept = [];
|
|
78
|
+
for (const field of (_declared_keepSiteFields = declared.keepSiteFields) != null ? _declared_keepSiteFields : []){
|
|
79
|
+
const value = doc[field];
|
|
80
|
+
if (typeof value === 'string' && value) kept.push(value);
|
|
81
|
+
if (Array.isArray(value)) {
|
|
82
|
+
for (const id of value)if (typeof id === 'string' && id) kept.push(id);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return kept;
|
|
86
|
+
}
|
|
87
|
+
/** The marked documents of one collection, in the order a cursor can resume. */ function markedPage(collectionRef, afterId) {
|
|
88
|
+
const ordered = collectionRef.where('scopeMode', '==', SCOPE_MODE_ALL_EXCEPT).orderBy(FieldPath.documentId());
|
|
89
|
+
return (afterId ? ordered.startAfter(afterId) : ordered).limit(PAGE_SIZE).get();
|
|
90
|
+
}
|
|
91
|
+
/** Runs `work` over `items`, at most {@link CONCURRENCY} at a time. */ async function eachConcurrently(items, work) {
|
|
92
|
+
let next = 0;
|
|
93
|
+
await Promise.all(Array.from({
|
|
94
|
+
length: Math.min(CONCURRENCY, items.length)
|
|
95
|
+
}, async ()=>{
|
|
96
|
+
while(next < items.length){
|
|
97
|
+
const item = items[next];
|
|
98
|
+
next += 1;
|
|
99
|
+
await work(item);
|
|
100
|
+
}
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
/** A write's data: a `null` marker field is deleted, never stored. */ function fieldsToWrite(fields) {
|
|
104
|
+
var _fields_scopeMode, _fields_scopeExcept;
|
|
105
|
+
return {
|
|
106
|
+
visibleTo: fields.visibleTo,
|
|
107
|
+
scopeMode: (_fields_scopeMode = fields.scopeMode) != null ? _fields_scopeMode : FieldValue.delete(),
|
|
108
|
+
scopeExcept: (_fields_scopeExcept = fields.scopeExcept) != null ? _fields_scopeExcept : FieldValue.delete()
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** The document as it will read after the write, for a patch to work from. */ function asStored(data, fields) {
|
|
112
|
+
const next = _extends({}, data, {
|
|
113
|
+
visibleTo: fields.visibleTo
|
|
114
|
+
});
|
|
115
|
+
if (fields.scopeMode) {
|
|
116
|
+
next['scopeMode'] = fields.scopeMode;
|
|
117
|
+
next['scopeExcept'] = fields.scopeExcept;
|
|
118
|
+
} else {
|
|
119
|
+
delete next['scopeMode'];
|
|
120
|
+
delete next['scopeExcept'];
|
|
121
|
+
}
|
|
122
|
+
return next;
|
|
123
|
+
}
|
|
124
|
+
/** Brings one marked document level, in its own transaction. */ async function reconcileDocument(db, orgRef, declared, docRef, counts) {
|
|
125
|
+
const status = await db.runTransaction(async (tx)=>{
|
|
126
|
+
var _declared_maxIncluded;
|
|
127
|
+
const [org, doc] = await tx.getAll(orgRef, docRef);
|
|
128
|
+
// The workspace went while this ran, and the document with it.
|
|
129
|
+
if (!org.exists || !doc.exists) return 'current';
|
|
130
|
+
const data = doc.data();
|
|
131
|
+
const answer = reconcileAllExcept(data, orgHostIdsFromDoc(org.data()), {
|
|
132
|
+
maxIncluded: (_declared_maxIncluded = declared.maxIncluded) != null ? _declared_maxIncluded : null,
|
|
133
|
+
keepHostIds: keptHostIds(declared, data)
|
|
134
|
+
});
|
|
135
|
+
if (answer.fields) {
|
|
136
|
+
const patch = declared.patch ? await requiredPatch(declared.name)(declared.name, asStored(data, answer.fields)) : {};
|
|
137
|
+
// Not an edit, so `updatedAt` stays: a list ordered by it must not
|
|
138
|
+
// reshuffle because a site was added somewhere in the workspace.
|
|
139
|
+
tx.update(docRef, _extends({}, fieldsToWrite(answer.fields), patch));
|
|
140
|
+
}
|
|
141
|
+
return answer.status;
|
|
142
|
+
});
|
|
143
|
+
if (status === 'changed') counts.changed += 1;
|
|
144
|
+
if (status === 'capped') counts.capped += 1;
|
|
145
|
+
if (status === 'invalid') counts.invalid += 1;
|
|
146
|
+
}
|
|
147
|
+
function requiredPatch(collection) {
|
|
148
|
+
const patch = scopeSharingPatchFor(collection);
|
|
149
|
+
if (!patch) throw new Error(`no scope-sharing patch is registered for ${collection}`);
|
|
150
|
+
return patch;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A declared patch is missing where the boot step has not run. Runs it once,
|
|
154
|
+
* and only when a marked document needs one, so a sweep that finds nothing
|
|
155
|
+
* never pays for it.
|
|
156
|
+
*/ async function ensurePatches(collections) {
|
|
157
|
+
if (collections.every((row)=>!row.patch || scopeSharingPatchFor(row.name))) return;
|
|
158
|
+
await runPluginDeclarationsRepair().catch((error)=>{
|
|
159
|
+
console.error('[scope-sweep] the declarations repair failed', error);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Walks every declared collection's marked documents and brings each level.
|
|
164
|
+
* Throws only on a failed query; one document failing is counted and the walk
|
|
165
|
+
* goes on, and an incomplete walk answers where it stopped.
|
|
166
|
+
*/ export async function runScopeSharingSweep(input) {
|
|
167
|
+
var _input_firestore;
|
|
168
|
+
const db = (_input_firestore = input.firestore) != null ? _input_firestore : platformFirestore();
|
|
169
|
+
const orgRef = db.collection('orgs').doc(input.orgId);
|
|
170
|
+
const collections = listScopeSharingCollections();
|
|
171
|
+
const startAt = input.from ? Math.max(0, collections.findIndex((row)=>{
|
|
172
|
+
var _input_from;
|
|
173
|
+
return row.name === ((_input_from = input.from) == null ? void 0 : _input_from.collection);
|
|
174
|
+
})) : 0;
|
|
175
|
+
const remaining = collections.slice(startAt);
|
|
176
|
+
const deadline = input.budgetMs == null ? null : Date.now() + input.budgetMs;
|
|
177
|
+
const counts = emptyCounts();
|
|
178
|
+
// Every collection's first page at once, so the common answer of nothing
|
|
179
|
+
// costs one round of queries rather than one after another.
|
|
180
|
+
const pages = new Map();
|
|
181
|
+
const firstAfter = (name)=>input.from && input.from.collection === name ? input.from.afterId : null;
|
|
182
|
+
await eachConcurrently(remaining, async (declared)=>{
|
|
183
|
+
const snapshot = await markedPage(orgRef.collection(declared.name), firstAfter(declared.name));
|
|
184
|
+
pages.set(declared.name, snapshot.docs);
|
|
185
|
+
});
|
|
186
|
+
if ([
|
|
187
|
+
...pages.values()
|
|
188
|
+
].every((docs)=>!docs.length)) {
|
|
189
|
+
return _extends({}, counts, {
|
|
190
|
+
complete: true,
|
|
191
|
+
cursor: null
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
await (input.onFirstMarked == null ? void 0 : input.onFirstMarked.call(input));
|
|
195
|
+
await ensurePatches(remaining);
|
|
196
|
+
for (const declared of remaining){
|
|
197
|
+
var _pages_get;
|
|
198
|
+
let docs = (_pages_get = pages.get(declared.name)) != null ? _pages_get : [];
|
|
199
|
+
let afterId = firstAfter(declared.name);
|
|
200
|
+
while(docs.length){
|
|
201
|
+
if (deadline != null && Date.now() >= deadline) {
|
|
202
|
+
return _extends({}, counts, {
|
|
203
|
+
complete: false,
|
|
204
|
+
cursor: {
|
|
205
|
+
collection: declared.name,
|
|
206
|
+
afterId
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
counts.marked += docs.length;
|
|
211
|
+
await eachConcurrently(docs, async (doc)=>{
|
|
212
|
+
try {
|
|
213
|
+
await reconcileDocument(db, orgRef, declared, doc.ref, counts);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
counts.failed += 1;
|
|
216
|
+
console.error(`[scope-sweep] ${declared.name}/${doc.id} in ${input.orgId} was not brought level`, error);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
afterId = docs[docs.length - 1].id;
|
|
220
|
+
if (docs.length < PAGE_SIZE) break;
|
|
221
|
+
docs = (await markedPage(orgRef.collection(declared.name), afterId)).docs;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return _extends({}, counts, {
|
|
225
|
+
complete: true,
|
|
226
|
+
cursor: null
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* What a change to a workspace's sites runs, once the change has committed.
|
|
231
|
+
*
|
|
232
|
+
* NEVER THROWS, because the site change is done and is what the person asked
|
|
233
|
+
* for. Work it could not finish is left in a sweep document for the job, and
|
|
234
|
+
* if even that write fails the weekly drift report finds what is behind.
|
|
235
|
+
*/ export async function sweepScopeSharingAfterSiteChange(input) {
|
|
236
|
+
const { orgId, reason, hostId } = input;
|
|
237
|
+
let sweepRef = null;
|
|
238
|
+
const leaveForTheJob = async (cursor, attempts)=>{
|
|
239
|
+
var _input_firestore;
|
|
240
|
+
const db = (_input_firestore = input.firestore) != null ? _input_firestore : platformFirestore();
|
|
241
|
+
sweepRef != null ? sweepRef : sweepRef = db.collection('orgs').doc(orgId).collection(SCOPE_SWEEPS_COLLECTION).doc(createResourceUid());
|
|
242
|
+
await sweepRef.set(_extends({
|
|
243
|
+
reason
|
|
244
|
+
}, hostId ? {
|
|
245
|
+
hostId
|
|
246
|
+
} : {}, {
|
|
247
|
+
cursor,
|
|
248
|
+
attempts,
|
|
249
|
+
createdAtMs: Date.now()
|
|
250
|
+
}));
|
|
251
|
+
};
|
|
252
|
+
try {
|
|
253
|
+
const result = await runScopeSharingSweep(_extends({
|
|
254
|
+
orgId,
|
|
255
|
+
budgetMs: INLINE_BUDGET_MS
|
|
256
|
+
}, input.firestore ? {
|
|
257
|
+
firestore: input.firestore
|
|
258
|
+
} : {}, {
|
|
259
|
+
onFirstMarked: ()=>leaveForTheJob(null, 0)
|
|
260
|
+
}));
|
|
261
|
+
if (!result.complete) {
|
|
262
|
+
await leaveForTheJob(result.cursor, 0);
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
if (result.failed) {
|
|
266
|
+
await leaveForTheJob(null, 1);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
await (sweepRef == null ? void 0 : sweepRef.delete());
|
|
270
|
+
} catch (error) {
|
|
271
|
+
console.error(`[scope-sweep] the sweep of ${orgId} after ${reason} did not finish`, error);
|
|
272
|
+
await leaveForTheJob(null, 1).catch((writeError)=>{
|
|
273
|
+
console.error(`[scope-sweep] could not leave ${orgId}'s sweep for the job`, writeError);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Carries on every unfinished sweep: the job's handler. A sweep document exists
|
|
279
|
+
* only while work is left, so the unfiltered query needs no index and answers
|
|
280
|
+
* nothing on a quiet beat, for one read.
|
|
281
|
+
*/ export async function resumeScopeSweeps(options = {}) {
|
|
282
|
+
var _options_firestore, _options_budgetMs;
|
|
283
|
+
const db = (_options_firestore = options.firestore) != null ? _options_firestore : platformFirestore();
|
|
284
|
+
const pending = await db.collectionGroup(SCOPE_SWEEPS_COLLECTION).limit(SWEEPS_PER_BEAT).get();
|
|
285
|
+
const tally = {
|
|
286
|
+
resumed: 0,
|
|
287
|
+
finished: 0,
|
|
288
|
+
droppedAfterFailing: 0
|
|
289
|
+
};
|
|
290
|
+
const deadline = Date.now() + ((_options_budgetMs = options.budgetMs) != null ? _options_budgetMs : JOB_BUDGET_MS);
|
|
291
|
+
for (const sweep of pending.docs){
|
|
292
|
+
var _sweep_get;
|
|
293
|
+
const budgetMs = deadline - Date.now();
|
|
294
|
+
if (budgetMs <= 0) break;
|
|
295
|
+
tally.resumed += 1;
|
|
296
|
+
const orgRef = sweep.ref.parent.parent;
|
|
297
|
+
const attempts = Number((_sweep_get = sweep.get('attempts')) != null ? _sweep_get : 0);
|
|
298
|
+
try {
|
|
299
|
+
var _sweep_get1;
|
|
300
|
+
const org = orgRef ? await orgRef.get() : null;
|
|
301
|
+
// The workspace was erased after this was left. Nothing to bring level.
|
|
302
|
+
if (!(org == null ? void 0 : org.exists)) {
|
|
303
|
+
await sweep.ref.delete();
|
|
304
|
+
tally.finished += 1;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const result = await runScopeSharingSweep({
|
|
308
|
+
orgId: org.id,
|
|
309
|
+
budgetMs,
|
|
310
|
+
from: (_sweep_get1 = sweep.get('cursor')) != null ? _sweep_get1 : null,
|
|
311
|
+
firestore: db
|
|
312
|
+
});
|
|
313
|
+
if (!result.complete) {
|
|
314
|
+
await sweep.ref.update({
|
|
315
|
+
cursor: result.cursor
|
|
316
|
+
});
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (!result.failed) {
|
|
320
|
+
await sweep.ref.delete();
|
|
321
|
+
tally.finished += 1;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
throw new Error(`${result.failed} document(s) failed`);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
console.error(`[scope-sweep] ${sweep.ref.path} failed on attempt ${attempts + 1}`, error);
|
|
327
|
+
if (attempts + 1 >= MAX_SWEEP_ATTEMPTS) {
|
|
328
|
+
// Stops holding a place in the beat. What it left behind is what the
|
|
329
|
+
// weekly drift report counts, and that report goes to staff.
|
|
330
|
+
await sweep.ref.delete().catch(()=>undefined);
|
|
331
|
+
tally.droppedAfterFailing += 1;
|
|
332
|
+
} else {
|
|
333
|
+
await sweep.ref.update({
|
|
334
|
+
attempts: attempts + 1,
|
|
335
|
+
cursor: null
|
|
336
|
+
}).catch(()=>undefined);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return tally;
|
|
341
|
+
}
|
|
342
|
+
export function emptyScopeSharingDrift() {
|
|
343
|
+
return {
|
|
344
|
+
marked: 0,
|
|
345
|
+
stale: 0,
|
|
346
|
+
capped: 0,
|
|
347
|
+
invalid: 0
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* How far one workspace's marked documents are from what a sweep would write,
|
|
352
|
+
* with the same helper the sweep uses. One query per declared collection when
|
|
353
|
+
* nothing is marked.
|
|
354
|
+
*/ export async function scopeSharingDriftForOrg(orgRef, orgData) {
|
|
355
|
+
const drift = emptyScopeSharingDrift();
|
|
356
|
+
const hostIds = orgHostIdsFromDoc(orgData);
|
|
357
|
+
await eachConcurrently(listScopeSharingCollections(), async (declared)=>{
|
|
358
|
+
let afterId = null;
|
|
359
|
+
for(;;){
|
|
360
|
+
const page = await markedPage(orgRef.collection(declared.name), afterId);
|
|
361
|
+
for (const doc of page.docs){
|
|
362
|
+
var _declared_maxIncluded;
|
|
363
|
+
const data = doc.data();
|
|
364
|
+
const answer = reconcileAllExcept(data, hostIds, {
|
|
365
|
+
maxIncluded: (_declared_maxIncluded = declared.maxIncluded) != null ? _declared_maxIncluded : null,
|
|
366
|
+
keepHostIds: keptHostIds(declared, data)
|
|
367
|
+
});
|
|
368
|
+
drift.marked += 1;
|
|
369
|
+
if (answer.status === 'invalid') drift.invalid += 1;
|
|
370
|
+
if (answer.status === 'capped') drift.capped += 1;
|
|
371
|
+
if (answer.fields) drift.stale += 1;
|
|
372
|
+
}
|
|
373
|
+
if (page.docs.length < PAGE_SIZE) return;
|
|
374
|
+
afterId = page.docs[page.docs.length - 1].id;
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
return drift;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
//# sourceMappingURL=scope-sharing-sweep.js.map
|