@sfxcode/nuxt-ui-mongocamp 1.3.5 → 1.5.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/README.md +1 -1
- package/dist/module.d.mts +8 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -0
- package/dist/runtime/components/MongocampAggregationBuilder.d.vue.ts +1 -0
- package/dist/runtime/components/MongocampAggregationBuilder.vue +331 -5
- package/dist/runtime/components/MongocampAggregationBuilder.vue.d.ts +1 -0
- package/dist/runtime/components/MongocampAggregationChart.d.vue.ts +6 -0
- package/dist/runtime/components/MongocampAggregationChart.vue +93 -0
- package/dist/runtime/components/MongocampAggregationChart.vue.d.ts +6 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.d.vue.ts +10 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.vue +149 -0
- package/dist/runtime/components/MongocampAggregationStagePreview.vue.d.ts +10 -0
- package/dist/runtime/components/MongocampBucketFiles.vue +230 -48
- package/dist/runtime/components/MongocampBulkDelete.d.vue.ts +9 -0
- package/dist/runtime/components/MongocampBulkDelete.vue +94 -25
- package/dist/runtime/components/MongocampBulkDelete.vue.d.ts +9 -0
- package/dist/runtime/components/MongocampBulkEdit.d.vue.ts +5 -0
- package/dist/runtime/components/MongocampBulkEdit.vue +41 -3
- package/dist/runtime/components/MongocampBulkEdit.vue.d.ts +5 -0
- package/dist/runtime/components/MongocampChart.d.vue.ts +29 -0
- package/dist/runtime/components/MongocampChart.vue +129 -0
- package/dist/runtime/components/MongocampChart.vue.d.ts +29 -0
- package/dist/runtime/components/MongocampCollectionCharts.d.vue.ts +17 -0
- package/dist/runtime/components/MongocampCollectionCharts.vue +200 -0
- package/dist/runtime/components/MongocampCollectionCharts.vue.d.ts +17 -0
- package/dist/runtime/components/MongocampCollectionCopy.d.vue.ts +23 -0
- package/dist/runtime/components/MongocampCollectionCopy.vue +206 -0
- package/dist/runtime/components/MongocampCollectionCopy.vue.d.ts +23 -0
- package/dist/runtime/components/MongocampCollectionData.d.vue.ts +6 -0
- package/dist/runtime/components/MongocampCollectionData.vue +423 -138
- package/dist/runtime/components/MongocampCollectionData.vue.d.ts +6 -0
- package/dist/runtime/components/MongocampCollectionInfos.vue +106 -238
- package/dist/runtime/components/MongocampCollectionRename.d.vue.ts +22 -0
- package/dist/runtime/components/MongocampCollectionRename.vue +116 -0
- package/dist/runtime/components/MongocampCollectionRename.vue.d.ts +22 -0
- package/dist/runtime/components/MongocampCollections.vue +93 -12
- package/dist/runtime/components/MongocampCommandPalette.d.vue.ts +40 -0
- package/dist/runtime/components/MongocampCommandPalette.vue +135 -0
- package/dist/runtime/components/MongocampCommandPalette.vue.d.ts +40 -0
- package/dist/runtime/components/MongocampConfirmByName.vue +2 -2
- package/dist/runtime/components/MongocampDashboard.d.vue.ts +3 -1
- package/dist/runtime/components/MongocampDashboard.vue +39 -5
- package/dist/runtime/components/MongocampDashboard.vue.d.ts +3 -1
- package/dist/runtime/components/MongocampDatabases.vue +35 -2
- package/dist/runtime/components/MongocampDocumentCompare.d.vue.ts +20 -0
- package/dist/runtime/components/MongocampDocumentCompare.vue +146 -0
- package/dist/runtime/components/MongocampDocumentCompare.vue.d.ts +20 -0
- package/dist/runtime/components/MongocampDocumentView.d.vue.ts +22 -0
- package/dist/runtime/components/MongocampDocumentView.vue +202 -0
- package/dist/runtime/components/MongocampDocumentView.vue.d.ts +22 -0
- package/dist/runtime/components/MongocampErrorAlert.vue +12 -4
- package/dist/runtime/components/MongocampExportAll.d.vue.ts +18 -0
- package/dist/runtime/components/MongocampExportAll.vue +191 -0
- package/dist/runtime/components/MongocampExportAll.vue.d.ts +18 -0
- package/dist/runtime/components/MongocampFilePreview.d.vue.ts +16 -0
- package/dist/runtime/components/MongocampFilePreview.vue +162 -0
- package/dist/runtime/components/MongocampFilePreview.vue.d.ts +16 -0
- package/dist/runtime/components/MongocampFilterBuilder.vue +11 -0
- package/dist/runtime/components/MongocampImport.vue +171 -13
- package/dist/runtime/components/MongocampIndexes.d.vue.ts +18 -0
- package/dist/runtime/components/MongocampIndexes.vue +496 -0
- package/dist/runtime/components/MongocampIndexes.vue.d.ts +18 -0
- package/dist/runtime/components/MongocampJobs.d.vue.ts +6 -1
- package/dist/runtime/components/MongocampJobs.vue +36 -5
- package/dist/runtime/components/MongocampJobs.vue.d.ts +6 -1
- package/dist/runtime/components/MongocampPermissionMatrix.d.vue.ts +12 -0
- package/dist/runtime/components/MongocampPermissionMatrix.vue +301 -0
- package/dist/runtime/components/MongocampPermissionMatrix.vue.d.ts +12 -0
- package/dist/runtime/components/MongocampQueryBar.d.vue.ts +26 -0
- package/dist/runtime/components/MongocampQueryBar.vue +398 -0
- package/dist/runtime/components/MongocampQueryBar.vue.d.ts +26 -0
- package/dist/runtime/components/MongocampRoleGrants.vue +18 -5
- package/dist/runtime/components/MongocampRoles.vue +16 -5
- package/dist/runtime/components/MongocampServerOverview.d.vue.ts +20 -0
- package/dist/runtime/components/MongocampServerOverview.vue +420 -0
- package/dist/runtime/components/MongocampServerOverview.vue.d.ts +20 -0
- package/dist/runtime/components/MongocampSettings.d.vue.ts +28 -1
- package/dist/runtime/components/MongocampSettings.vue +83 -10
- package/dist/runtime/components/MongocampSettings.vue.d.ts +28 -1
- package/dist/runtime/components/MongocampUsers.vue +19 -6
- package/dist/runtime/composables/useMongocampAdmin.d.ts +2 -1
- package/dist/runtime/composables/useMongocampAdmin.js +6 -1
- package/dist/runtime/composables/useMongocampAdminGate.d.ts +12 -0
- package/dist/runtime/composables/useMongocampAdminGate.js +12 -0
- package/dist/runtime/composables/useMongocampAggregation.d.ts +23 -1
- package/dist/runtime/composables/useMongocampAggregation.js +41 -26
- package/dist/runtime/composables/useMongocampAutoRefresh.d.ts +26 -0
- package/dist/runtime/composables/useMongocampAutoRefresh.js +78 -0
- package/dist/runtime/composables/useMongocampBucket.d.ts +13 -1
- package/dist/runtime/composables/useMongocampBucket.js +34 -0
- package/dist/runtime/composables/useMongocampBulkByFilter.d.ts +15 -0
- package/dist/runtime/composables/useMongocampBulkByFilter.js +32 -0
- package/dist/runtime/composables/useMongocampClientApi.js +2 -1
- package/dist/runtime/composables/useMongocampCollectionCopy.d.ts +45 -0
- package/dist/runtime/composables/useMongocampCollectionCopy.js +48 -0
- package/dist/runtime/composables/useMongocampCollectionStats.d.ts +10 -0
- package/dist/runtime/composables/useMongocampCollectionStats.js +17 -0
- package/dist/runtime/composables/useMongocampError.d.ts +2 -1
- package/dist/runtime/composables/useMongocampError.js +16 -1
- package/dist/runtime/composables/useMongocampExportAll.d.ts +53 -0
- package/dist/runtime/composables/useMongocampExportAll.js +57 -0
- package/dist/runtime/composables/useMongocampExtendedJson.d.ts +7 -2
- package/dist/runtime/composables/useMongocampExtendedJson.js +44 -11
- package/dist/runtime/composables/useMongocampFetchAll.d.ts +25 -0
- package/dist/runtime/composables/useMongocampFetchAll.js +27 -0
- package/dist/runtime/composables/useMongocampFieldTypes.d.ts +24 -0
- package/dist/runtime/composables/useMongocampFieldTypes.js +44 -0
- package/dist/runtime/composables/useMongocampImport.d.ts +4 -0
- package/dist/runtime/composables/useMongocampImport.js +5 -2
- package/dist/runtime/composables/useMongocampIndex.d.ts +6 -1
- package/dist/runtime/composables/useMongocampIndex.js +7 -1
- package/dist/runtime/composables/useMongocampPermissions.d.ts +17 -0
- package/dist/runtime/composables/useMongocampPermissions.js +43 -0
- package/dist/runtime/composables/useMongocampPersistedState.d.ts +15 -0
- package/dist/runtime/composables/useMongocampPersistedState.js +21 -0
- package/dist/runtime/composables/useMongocampProxyApi.js +2 -1
- package/dist/runtime/composables/useMongocampQuery.d.ts +50 -2
- package/dist/runtime/composables/useMongocampQuery.js +72 -1
- package/dist/runtime/composables/useMongocampSavedPipelines.d.ts +25 -0
- package/dist/runtime/composables/useMongocampSavedPipelines.js +23 -0
- package/dist/runtime/composables/useMongocampSavedQueries.d.ts +37 -0
- package/dist/runtime/composables/useMongocampSavedQueries.js +50 -0
- package/dist/runtime/composables/useMongocampServerOverview.d.ts +35 -0
- package/dist/runtime/composables/useMongocampServerOverview.js +51 -0
- package/dist/runtime/composables/useMongocampSession.d.ts +2 -0
- package/dist/runtime/composables/useMongocampSession.js +16 -2
- package/dist/runtime/composables/useMongocampSystem.d.ts +7 -2
- package/dist/runtime/composables/useMongocampSystem.js +6 -2
- package/dist/runtime/locales/de.json +509 -25
- package/dist/runtime/locales/en.json +509 -25
- package/dist/runtime/plugin.js +6 -3
- package/dist/runtime/utils/aggregationErrors.d.ts +4 -0
- package/dist/runtime/utils/aggregationErrors.js +7 -0
- package/dist/runtime/utils/aggregationPipeline.d.ts +5 -4
- package/dist/runtime/utils/aggregationPipeline.js +23 -9
- package/dist/runtime/utils/aggregationPreview.d.ts +13 -0
- package/dist/runtime/utils/aggregationPreview.js +10 -0
- package/dist/runtime/utils/aggregationResult.d.ts +5 -0
- package/dist/runtime/utils/aggregationResult.js +4 -0
- package/dist/runtime/utils/aggregationTemplates.d.ts +34 -0
- package/dist/runtime/utils/aggregationTemplates.js +47 -0
- package/dist/runtime/utils/analyzeSamples.d.ts +1 -0
- package/dist/runtime/utils/analyzeSamples.js +1 -1
- package/dist/runtime/utils/autoRefresh.d.ts +14 -0
- package/dist/runtime/utils/autoRefresh.js +15 -0
- package/dist/runtime/utils/batchInsert.d.ts +6 -0
- package/dist/runtime/utils/batchInsert.js +31 -3
- package/dist/runtime/utils/bigIntJson.d.ts +15 -0
- package/dist/runtime/utils/bigIntJson.js +55 -0
- package/dist/runtime/utils/bsonTypes.d.ts +10 -0
- package/dist/runtime/utils/bsonTypes.js +42 -0
- package/dist/runtime/utils/bulkDelete.d.ts +9 -1
- package/dist/runtime/utils/bulkDelete.js +15 -3
- package/dist/runtime/utils/bulkEdit.d.ts +6 -0
- package/dist/runtime/utils/bulkEdit.js +7 -0
- package/dist/runtime/utils/chartData.d.ts +11 -0
- package/dist/runtime/utils/chartData.js +9 -0
- package/dist/runtime/utils/collectionChart.d.ts +36 -0
- package/dist/runtime/utils/collectionChart.js +73 -0
- package/dist/runtime/utils/collectionCopy.d.ts +24 -0
- package/dist/runtime/utils/collectionCopy.js +37 -0
- package/dist/runtime/utils/collectionStats.d.ts +31 -0
- package/dist/runtime/utils/collectionStats.js +42 -0
- package/dist/runtime/utils/commandPalette.d.ts +48 -0
- package/dist/runtime/utils/commandPalette.js +39 -0
- package/dist/runtime/utils/configRows.d.ts +7 -0
- package/dist/runtime/utils/configRows.js +4 -0
- package/dist/runtime/utils/documentCompare.d.ts +31 -0
- package/dist/runtime/utils/documentCompare.js +71 -0
- package/dist/runtime/utils/documentDiff.d.ts +19 -0
- package/dist/runtime/utils/documentDiff.js +71 -0
- package/dist/runtime/utils/documentPaths.d.ts +41 -0
- package/dist/runtime/utils/documentPaths.js +139 -0
- package/dist/runtime/utils/documentTree.d.ts +40 -0
- package/dist/runtime/utils/documentTree.js +102 -0
- package/dist/runtime/utils/exportDocuments.d.ts +33 -0
- package/dist/runtime/utils/exportDocuments.js +53 -0
- package/dist/runtime/utils/exportRows.d.ts +2 -0
- package/dist/runtime/utils/exportRows.js +6 -3
- package/dist/runtime/utils/fieldDetails.d.ts +51 -0
- package/dist/runtime/utils/fieldDetails.js +113 -0
- package/dist/runtime/utils/filePreview.d.ts +27 -0
- package/dist/runtime/utils/filePreview.js +50 -0
- package/dist/runtime/utils/filterBuilder.d.ts +6 -1
- package/dist/runtime/utils/filterBuilder.js +6 -5
- package/dist/runtime/utils/formatBytes.d.ts +2 -0
- package/dist/runtime/utils/formatBytes.js +11 -0
- package/dist/runtime/utils/grantMatching.d.ts +8 -0
- package/dist/runtime/utils/grantMatching.js +15 -0
- package/dist/runtime/utils/importRows.js +12 -2
- package/dist/runtime/utils/indexForm.d.ts +83 -0
- package/dist/runtime/utils/indexForm.js +74 -0
- package/dist/runtime/utils/luceneExpression.d.ts +15 -0
- package/dist/runtime/utils/luceneExpression.js +74 -0
- package/dist/runtime/utils/pagedFetch.d.ts +46 -0
- package/dist/runtime/utils/pagedFetch.js +58 -0
- package/dist/runtime/utils/paginationHeaders.d.ts +4 -0
- package/dist/runtime/utils/paginationHeaders.js +4 -0
- package/dist/runtime/utils/permissionDenied.d.ts +10 -0
- package/dist/runtime/utils/permissionDenied.js +23 -0
- package/dist/runtime/utils/permissionMatrix.d.ts +55 -0
- package/dist/runtime/utils/permissionMatrix.js +85 -0
- package/dist/runtime/utils/persistedState.d.ts +25 -0
- package/dist/runtime/utils/persistedState.js +47 -0
- package/dist/runtime/utils/queryState.d.ts +45 -0
- package/dist/runtime/utils/queryState.js +75 -0
- package/dist/runtime/utils/requestLimits.d.ts +2 -0
- package/dist/runtime/utils/requestLimits.js +4 -0
- package/dist/runtime/utils/resultChart.d.ts +32 -0
- package/dist/runtime/utils/resultChart.js +59 -0
- package/dist/runtime/utils/savedPipelines.d.ts +22 -0
- package/dist/runtime/utils/savedPipelines.js +46 -0
- package/dist/runtime/utils/savedQueries.d.ts +60 -0
- package/dist/runtime/utils/savedQueries.js +143 -0
- package/dist/runtime/utils/schemaAnalysis.d.ts +16 -0
- package/dist/runtime/utils/schemaAnalysis.js +57 -0
- package/dist/runtime/utils/serverError.d.ts +19 -0
- package/dist/runtime/utils/serverError.js +19 -0
- package/dist/runtime/utils/serverOverview.d.ts +47 -0
- package/dist/runtime/utils/serverOverview.js +40 -0
- package/dist/runtime/utils/sessionGuard.d.ts +3 -0
- package/dist/runtime/utils/sessionGuard.js +18 -1
- package/package.json +15 -14
|
@@ -64,8 +64,8 @@ export function buildCondition(condition, type) {
|
|
|
64
64
|
if (type === "objectId") {
|
|
65
65
|
const ids = condition.operator === "oneOf" ? splitList(value) : [value];
|
|
66
66
|
if (ids.length === 0 || !ids.every(isObjectIdHex)) return "";
|
|
67
|
-
if (condition.operator === "oneOf") return h.
|
|
68
|
-
return condition.operator === "notEquals" ? h.notEquals(field, value) : h.
|
|
67
|
+
if (condition.operator === "oneOf") return h.byIds(ids);
|
|
68
|
+
return condition.operator === "notEquals" ? h.notEquals(field, value) : h.byId(value);
|
|
69
69
|
}
|
|
70
70
|
switch (condition.operator) {
|
|
71
71
|
case "isEmpty":
|
|
@@ -73,9 +73,9 @@ export function buildCondition(condition, type) {
|
|
|
73
73
|
case "isNotEmpty":
|
|
74
74
|
return h.exists(field, existsType(type));
|
|
75
75
|
case "isTrue":
|
|
76
|
-
return
|
|
76
|
+
return h.isTrue(field);
|
|
77
77
|
case "isFalse":
|
|
78
|
-
return
|
|
78
|
+
return h.isFalse(field);
|
|
79
79
|
case "gte": {
|
|
80
80
|
const from = parseRangeBound(type, value);
|
|
81
81
|
return from === null ? "" : h.inRange(field, from, null);
|
|
@@ -121,7 +121,8 @@ export function buildCondition(condition, type) {
|
|
|
121
121
|
export function buildFilterQuery(group, fieldTypes) {
|
|
122
122
|
const h = useMongocampQuery();
|
|
123
123
|
const parts = group.conditions.map((c) => buildCondition(c, fieldTypes[c.field] ?? "string"));
|
|
124
|
-
|
|
124
|
+
const combined = group.match === "any" ? h.or(...parts) : h.and(...parts);
|
|
125
|
+
return group.negate ? h.not(combined) : combined;
|
|
125
126
|
}
|
|
126
127
|
export function countActiveConditions(group, fieldTypes) {
|
|
127
128
|
return group.conditions.filter((c) => buildCondition(c, fieldTypes[c.field] ?? "string") !== "").length;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function formatBytes(bytes) {
|
|
2
|
+
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
|
|
3
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
4
|
+
let value = bytes;
|
|
5
|
+
let unit = 0;
|
|
6
|
+
while (value >= 1024 && unit < units.length - 1) {
|
|
7
|
+
value /= 1024;
|
|
8
|
+
unit++;
|
|
9
|
+
}
|
|
10
|
+
return `${unit === 0 ? value : value.toFixed(value >= 10 ? 0 : 1)} ${units[unit]}`;
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type GrantPermission = 'read' | 'write' | 'administrate';
|
|
2
|
+
/**
|
|
3
|
+
* Whether the grants allow `permission` on `collection`: `true` / `false` when they decide, `undefined` when they cannot
|
|
4
|
+
* — no grant list at all, or a GridFS bucket member (`<bucket>.files` / `.chunks`), whose grant name and type were not
|
|
5
|
+
* verified. Callers show the action on `undefined` and let the server refuse (it answers a missing grant clearly).
|
|
6
|
+
* Several grants for the same name add up: any one that allows it is enough.
|
|
7
|
+
*/
|
|
8
|
+
export declare function permissionFromGrants(grants: unknown, collection: string, permission: GrantPermission): boolean | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isBucketMemberCollection } from "./collectionProtection.js";
|
|
2
|
+
const normalise = (value) => typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
3
|
+
function isObject(value) {
|
|
4
|
+
return typeof value === "object" && value !== null;
|
|
5
|
+
}
|
|
6
|
+
function isCollectionGrant(grant) {
|
|
7
|
+
const type = normalise(grant.grantType);
|
|
8
|
+
return type === "" || type === "collection";
|
|
9
|
+
}
|
|
10
|
+
export function permissionFromGrants(grants, collection, permission) {
|
|
11
|
+
if (!Array.isArray(grants)) return void 0;
|
|
12
|
+
if (isBucketMemberCollection(collection)) return void 0;
|
|
13
|
+
const wanted = normalise(collection);
|
|
14
|
+
return grants.some((grant) => isObject(grant) && isCollectionGrant(grant) && normalise(grant.name) === wanted && grant[permission] === true);
|
|
15
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseJsonKeepingLongs } from "./bigIntJson.js";
|
|
1
2
|
import { isIsoDateTimeString } from "./isoDate.js";
|
|
2
3
|
import { isObjectIdHex } from "./objectId.js";
|
|
3
4
|
export const DEFAULT_IMPORT_LIMITS = { maxRows: 1e4, maxBytes: 5 * 1024 * 1024 };
|
|
@@ -11,18 +12,27 @@ function stripBom(text) {
|
|
|
11
12
|
function fileError(code, message) {
|
|
12
13
|
return { rows: [], errors: [{ rowIndex: -1, code, message }], truncated: false };
|
|
13
14
|
}
|
|
15
|
+
function containsUnsafeLong(value) {
|
|
16
|
+
if (Array.isArray(value)) return value.some(containsUnsafeLong);
|
|
17
|
+
if (!isPlainObject(value)) return false;
|
|
18
|
+
const keys = Object.keys(value);
|
|
19
|
+
if (keys.length === 1 && keys[0] === "$numberLong" && typeof value.$numberLong === "string" && /^-?\d+$/.test(value.$numberLong) && !Number.isSafeInteger(Number(value.$numberLong))) return true;
|
|
20
|
+
return Object.values(value).some(containsUnsafeLong);
|
|
21
|
+
}
|
|
14
22
|
export function parseJsonRows(text, limits = DEFAULT_IMPORT_LIMITS) {
|
|
15
23
|
if (text.length > limits.maxBytes) return fileError("file-too-large", `File is larger than ${limits.maxBytes} bytes`);
|
|
16
24
|
let parsed;
|
|
17
25
|
try {
|
|
18
|
-
parsed =
|
|
26
|
+
parsed = parseJsonKeepingLongs(stripBom(text));
|
|
19
27
|
} catch (e) {
|
|
20
28
|
return fileError("invalid-json", `Invalid JSON: ${e instanceof Error ? e.message : String(e)}`);
|
|
21
29
|
}
|
|
22
30
|
if (!Array.isArray(parsed)) return fileError("not-array", "Expected a JSON array of objects");
|
|
23
31
|
const report = { rows: [], errors: [], truncated: false, sourceIndexes: [] };
|
|
24
32
|
parsed.forEach((item, rowIndex) => {
|
|
25
|
-
if (isPlainObject(item)) {
|
|
33
|
+
if (isPlainObject(item) && containsUnsafeLong(item)) {
|
|
34
|
+
report.errors.push({ rowIndex, code: "unsafe-integer", message: "Integer is beyond \xB1(2^53 \u2212 1) and would lose precision; import it as text" });
|
|
35
|
+
} else if (isPlainObject(item)) {
|
|
26
36
|
report.rows.push(item);
|
|
27
37
|
report.sourceIndexes.push(rowIndex);
|
|
28
38
|
} else report.errors.push({ rowIndex, code: "not-object", message: "Row is not an object" });
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** An index as `GET …/index/details` lists it (the parts this module reads). */
|
|
2
|
+
export interface IndexDetailLike {
|
|
3
|
+
name: string;
|
|
4
|
+
keys?: Array<{
|
|
5
|
+
field: string;
|
|
6
|
+
direction: string;
|
|
7
|
+
}>;
|
|
8
|
+
unique?: boolean;
|
|
9
|
+
sparse?: boolean;
|
|
10
|
+
hidden?: boolean;
|
|
11
|
+
partialFilterExpression?: unknown;
|
|
12
|
+
collation?: unknown;
|
|
13
|
+
weights?: Record<string, unknown>;
|
|
14
|
+
defaultLanguage?: string;
|
|
15
|
+
expireAfterSeconds?: number | null;
|
|
16
|
+
}
|
|
17
|
+
export interface IndexKey {
|
|
18
|
+
field: string;
|
|
19
|
+
/** 1, -1, or `text` for a field of a text index. */
|
|
20
|
+
direction: 1 | -1 | 'text' | 'other';
|
|
21
|
+
}
|
|
22
|
+
export declare const MAX_INDEX_FIELDS = 8;
|
|
23
|
+
/** The fields of an index in index order, straight from the server's ordered listing. */
|
|
24
|
+
export declare function indexKeys(index: Pick<IndexDetailLike, 'keys'>): IndexKey[];
|
|
25
|
+
/** Whether the index is a text index (any key is `text`). */
|
|
26
|
+
export declare function isTextIndex(index: Pick<IndexDetailLike, 'keys'>): boolean;
|
|
27
|
+
/** Whether the index expires documents (a TTL index). */
|
|
28
|
+
export declare function isExpiringIndex(index: Pick<IndexDetailLike, 'expireAfterSeconds'>): boolean;
|
|
29
|
+
export declare function describeKey(key: IndexKey): string;
|
|
30
|
+
export { formatBytes } from './formatBytes.js';
|
|
31
|
+
export declare function isValidExpireAfter(text: string): boolean;
|
|
32
|
+
export type IndexKind = 'standard' | 'unique' | 'text' | 'expiring';
|
|
33
|
+
export interface IndexForm {
|
|
34
|
+
kind: IndexKind;
|
|
35
|
+
/** One row per field; an expiring index uses the first, a text index all of them (the direction is ignored). */
|
|
36
|
+
fields: Array<{
|
|
37
|
+
field: string;
|
|
38
|
+
descending: boolean;
|
|
39
|
+
}>;
|
|
40
|
+
/** Optional name (every kind but expiring). */
|
|
41
|
+
name: string;
|
|
42
|
+
expireAfter: string;
|
|
43
|
+
/** Index only the documents that have the field (not together with a partial filter). */
|
|
44
|
+
sparse: boolean;
|
|
45
|
+
/** Created but not used by queries until unhidden. */
|
|
46
|
+
hidden: boolean;
|
|
47
|
+
/** A MongoDB filter as JSON text: only the documents it matches are indexed. Empty = none. */
|
|
48
|
+
partialFilter: string;
|
|
49
|
+
}
|
|
50
|
+
export type IndexFormProblem = 'no-field' | 'empty-field' | 'duplicate-field' | 'too-many-fields' | 'bad-duration' | 'bad-name' | 'bad-partial-filter' | 'sparse-and-partial';
|
|
51
|
+
/** The partial filter text as an object: `undefined` for empty, `null` when it is not a JSON object. */
|
|
52
|
+
export declare function parsePartialFilter(text: string): Record<string, unknown> | null | undefined;
|
|
53
|
+
export declare function validateIndexForm(form: IndexForm): IndexFormProblem[];
|
|
54
|
+
export interface IndexCreateOptions {
|
|
55
|
+
unique?: boolean;
|
|
56
|
+
name?: string;
|
|
57
|
+
sparse?: boolean;
|
|
58
|
+
hidden?: boolean;
|
|
59
|
+
partialFilterExpression?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
export type IndexRequest = {
|
|
62
|
+
call: 'createOrderedIndex';
|
|
63
|
+
keys: Array<{
|
|
64
|
+
field: string;
|
|
65
|
+
direction: '1' | '-1' | 'text';
|
|
66
|
+
}>;
|
|
67
|
+
options?: IndexCreateOptions;
|
|
68
|
+
} | {
|
|
69
|
+
call: 'createExpiringIndex';
|
|
70
|
+
field: string;
|
|
71
|
+
duration: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The call a valid form stands for. Every kind but expiring goes through the ordered create, which keeps the field order of
|
|
75
|
+
* a compound index and accepts a text index over several fields (server 1.8.1, verified live).
|
|
76
|
+
*/
|
|
77
|
+
export declare function buildIndexRequest(form: IndexForm): IndexRequest;
|
|
78
|
+
export type IndexErrorKind = 'duplicate-values' | 'conflict' | 'empty-keys' | 'other';
|
|
79
|
+
/** What a create/drop failure means, from the server's message (MongoDB error codes 11000, 85/86 and 67). */
|
|
80
|
+
export declare function classifyIndexError(message: string): IndexErrorKind;
|
|
81
|
+
/** Never used since the counter started — a hint, not proof (counters restart with the server). `_id_` is never flagged. */
|
|
82
|
+
export declare function isUnusedIndex(name: string, ops: number | undefined): boolean;
|
|
83
|
+
export declare function canDropIndex(name: string): boolean;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export const MAX_INDEX_FIELDS = 8;
|
|
2
|
+
export function indexKeys(index) {
|
|
3
|
+
return (index.keys ?? []).map(({ field, direction }) => ({
|
|
4
|
+
field,
|
|
5
|
+
direction: direction === "1" ? 1 : direction === "-1" ? -1 : direction === "text" ? "text" : "other"
|
|
6
|
+
}));
|
|
7
|
+
}
|
|
8
|
+
export function isTextIndex(index) {
|
|
9
|
+
return indexKeys(index).some((key) => key.direction === "text");
|
|
10
|
+
}
|
|
11
|
+
export function isExpiringIndex(index) {
|
|
12
|
+
return typeof index.expireAfterSeconds === "number";
|
|
13
|
+
}
|
|
14
|
+
export function describeKey(key) {
|
|
15
|
+
if (key.direction === "text") return `${key.field} (text)`;
|
|
16
|
+
return `${key.field}: ${key.direction === 1 ? "\u2191" : key.direction === -1 ? "\u2193" : "?"}`;
|
|
17
|
+
}
|
|
18
|
+
export { formatBytes } from "./formatBytes.js";
|
|
19
|
+
const DURATION = /^[1-9]\d{0,8}[smhd]$/;
|
|
20
|
+
export function isValidExpireAfter(text) {
|
|
21
|
+
return DURATION.test(text.trim());
|
|
22
|
+
}
|
|
23
|
+
export function parsePartialFilter(text) {
|
|
24
|
+
if (text.trim() === "") return void 0;
|
|
25
|
+
try {
|
|
26
|
+
const value = JSON.parse(text);
|
|
27
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length > 0 ? value : null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const takesOptions = (kind) => kind !== "expiring";
|
|
33
|
+
export function validateIndexForm(form) {
|
|
34
|
+
const problems = [];
|
|
35
|
+
const rows = form.kind === "expiring" ? form.fields.slice(0, 1) : form.fields;
|
|
36
|
+
if (rows.length === 0) return ["no-field"];
|
|
37
|
+
const names = rows.map((r) => r.field.trim());
|
|
38
|
+
if (names.includes("")) problems.push("empty-field");
|
|
39
|
+
if (new Set(names.filter(Boolean)).size !== names.filter(Boolean).length) problems.push("duplicate-field");
|
|
40
|
+
if (rows.length > MAX_INDEX_FIELDS) problems.push("too-many-fields");
|
|
41
|
+
if (form.kind === "expiring" && !isValidExpireAfter(form.expireAfter)) problems.push("bad-duration");
|
|
42
|
+
if (takesOptions(form.kind) && form.name.trim().length > 100) problems.push("bad-name");
|
|
43
|
+
if (takesOptions(form.kind)) {
|
|
44
|
+
const partial = parsePartialFilter(form.partialFilter);
|
|
45
|
+
if (partial === null) problems.push("bad-partial-filter");
|
|
46
|
+
else if (partial !== void 0 && form.sparse) problems.push("sparse-and-partial");
|
|
47
|
+
}
|
|
48
|
+
return problems;
|
|
49
|
+
}
|
|
50
|
+
export function buildIndexRequest(form) {
|
|
51
|
+
const rows = form.fields.map((r) => ({ field: r.field.trim(), descending: r.descending }));
|
|
52
|
+
if (form.kind === "expiring") return { call: "createExpiringIndex", field: rows[0].field, duration: form.expireAfter.trim() };
|
|
53
|
+
const keys = rows.map((row) => ({ field: row.field, direction: form.kind === "text" ? "text" : row.descending ? "-1" : "1" }));
|
|
54
|
+
const options = {};
|
|
55
|
+
if (form.kind === "unique") options.unique = true;
|
|
56
|
+
if (form.name.trim() !== "") options.name = form.name.trim();
|
|
57
|
+
if (form.sparse) options.sparse = true;
|
|
58
|
+
if (form.hidden) options.hidden = true;
|
|
59
|
+
const partial = parsePartialFilter(form.partialFilter);
|
|
60
|
+
if (partial) options.partialFilterExpression = partial;
|
|
61
|
+
return Object.keys(options).length > 0 ? { call: "createOrderedIndex", keys, options } : { call: "createOrderedIndex", keys };
|
|
62
|
+
}
|
|
63
|
+
export function classifyIndexError(message) {
|
|
64
|
+
if (/11000|duplicate key|E11000/i.test(message)) return "duplicate-values";
|
|
65
|
+
if (/equivalent index already exists|IndexOptionsConflict|IndexKeySpecsConflict|already exists with a different name/i.test(message)) return "conflict";
|
|
66
|
+
if (/keys cannot be empty/i.test(message)) return "empty-keys";
|
|
67
|
+
return "other";
|
|
68
|
+
}
|
|
69
|
+
export function isUnusedIndex(name, ops) {
|
|
70
|
+
return name !== "_id_" && ops === 0;
|
|
71
|
+
}
|
|
72
|
+
export function canDropIndex(name) {
|
|
73
|
+
return name !== "_id_";
|
|
74
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type LuceneNode = {
|
|
2
|
+
kind: 'clause';
|
|
3
|
+
text: string;
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'and' | 'or';
|
|
6
|
+
parts: LuceneNode[];
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'not';
|
|
9
|
+
node: LuceneNode;
|
|
10
|
+
};
|
|
11
|
+
export declare class UnsupportedExpressionError extends Error {
|
|
12
|
+
constructor(expression: string, reason: string);
|
|
13
|
+
}
|
|
14
|
+
/** `null` for an empty expression; throws {@link UnsupportedExpressionError} for anything it cannot read. */
|
|
15
|
+
export declare function parseLuceneExpression(expression: string): LuceneNode | null;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class UnsupportedExpressionError extends Error {
|
|
2
|
+
constructor(expression, reason) {
|
|
3
|
+
super(`Cannot negate this expression (${reason}): ${expression}`);
|
|
4
|
+
this.name = "UnsupportedExpressionError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const OPEN = /* @__PURE__ */ new Set(["(", "[", "{"]);
|
|
8
|
+
const CLOSE = /* @__PURE__ */ new Set([")", "]", "}"]);
|
|
9
|
+
function scan(text, visit) {
|
|
10
|
+
let depth = 0;
|
|
11
|
+
let inQuote = false;
|
|
12
|
+
for (let i = 0; i < text.length; i++) {
|
|
13
|
+
const char = text[i];
|
|
14
|
+
if (char === "\\") {
|
|
15
|
+
i++;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (char === '"') {
|
|
19
|
+
inQuote = !inQuote;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (inQuote) continue;
|
|
23
|
+
if (CLOSE.has(char)) depth--;
|
|
24
|
+
const found = visit(char, i, depth);
|
|
25
|
+
if (found !== void 0) return found;
|
|
26
|
+
if (OPEN.has(char)) depth++;
|
|
27
|
+
}
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
function matchingClose(text, start) {
|
|
31
|
+
const index = scan(text.slice(start), (char, i, depth) => char === ")" && depth === 0 ? i : void 0);
|
|
32
|
+
return index === void 0 ? -1 : start + index;
|
|
33
|
+
}
|
|
34
|
+
function splitTopLevel(text) {
|
|
35
|
+
const parts = [];
|
|
36
|
+
const operators = /* @__PURE__ */ new Set();
|
|
37
|
+
let last = 0;
|
|
38
|
+
scan(text, (char, i, depth) => {
|
|
39
|
+
if (char !== " " || depth !== 0) return void 0;
|
|
40
|
+
for (const operator of ["AND", "OR"]) {
|
|
41
|
+
const token = ` ${operator} `;
|
|
42
|
+
if (text.startsWith(token, i)) {
|
|
43
|
+
parts.push(text.slice(last, i));
|
|
44
|
+
operators.add(operator);
|
|
45
|
+
last = i + token.length;
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return void 0;
|
|
50
|
+
});
|
|
51
|
+
parts.push(text.slice(last));
|
|
52
|
+
return { parts: parts.map((part) => part.trim()), operators };
|
|
53
|
+
}
|
|
54
|
+
export function parseLuceneExpression(expression) {
|
|
55
|
+
const text = expression.trim();
|
|
56
|
+
if (text === "" || text === "()") return null;
|
|
57
|
+
return parse(text, expression);
|
|
58
|
+
}
|
|
59
|
+
function parse(text, original) {
|
|
60
|
+
if (text.startsWith("NOT ") || text.startsWith("!")) throw new UnsupportedExpressionError(original, "NOT is not supported");
|
|
61
|
+
if (text.startsWith("-(") && matchingClose(text, 1) === text.length - 1) {
|
|
62
|
+
return { kind: "not", node: parse(text.slice(2, -1).trim(), original) };
|
|
63
|
+
}
|
|
64
|
+
if (text.startsWith("(")) {
|
|
65
|
+
const close = matchingClose(text, 0);
|
|
66
|
+
if (close === -1) throw new UnsupportedExpressionError(original, "unbalanced parentheses");
|
|
67
|
+
if (close === text.length - 1) return parse(text.slice(1, -1).trim(), original);
|
|
68
|
+
}
|
|
69
|
+
const { parts, operators } = splitTopLevel(text);
|
|
70
|
+
if (operators.size === 0) return { kind: "clause", text };
|
|
71
|
+
if (operators.size > 1) throw new UnsupportedExpressionError(original, "AND and OR mixed without parentheses");
|
|
72
|
+
if (parts.includes("")) throw new UnsupportedExpressionError(original, "empty operand");
|
|
73
|
+
return { kind: operators.has("AND") ? "and" : "or", parts: parts.map((part) => parse(part, original)) };
|
|
74
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare const DEFAULT_PAGE_SIZE = 500;
|
|
2
|
+
/** Callers raise this on purpose; an unbounded read of a large collection is never the default. */
|
|
3
|
+
export declare const DEFAULT_MAX_ROWS = 10000;
|
|
4
|
+
export interface PageResult<T> {
|
|
5
|
+
rows: T[];
|
|
6
|
+
/** Total number of rows the server reports for the whole list, when it does (the pagination header). */
|
|
7
|
+
total?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface FetchAllProgress {
|
|
10
|
+
loaded: number;
|
|
11
|
+
/** The server's total as of the latest page; `undefined` when it never reported one. */
|
|
12
|
+
total?: number;
|
|
13
|
+
/** How many rows will be read at most: the total, capped at `maxRows`. */
|
|
14
|
+
target?: number;
|
|
15
|
+
page: number;
|
|
16
|
+
}
|
|
17
|
+
export interface FetchAllOptions<T> {
|
|
18
|
+
/** Rows per request (default 500; the server accepts far more). */
|
|
19
|
+
pageSize?: number;
|
|
20
|
+
/** Stop after this many rows and report `truncated` (default 10 000). */
|
|
21
|
+
maxRows?: number;
|
|
22
|
+
onProgress?: (progress: FetchAllProgress) => void;
|
|
23
|
+
/** Aborting rejects with an `AbortError` — a half-read result is never returned as if it were complete. */
|
|
24
|
+
signal?: AbortSignal;
|
|
25
|
+
/** Identity of a row, to drop a row that appears twice because the list shifted while reading. */
|
|
26
|
+
keyOf?: (row: T) => string;
|
|
27
|
+
}
|
|
28
|
+
export interface FetchAllResult<T> {
|
|
29
|
+
rows: T[];
|
|
30
|
+
/** The server's total as of the latest page. */
|
|
31
|
+
total?: number;
|
|
32
|
+
/** More rows exist than were read: the cap was hit. */
|
|
33
|
+
truncated: boolean;
|
|
34
|
+
/** The reported total was not the same on every page — rows were added or removed during the read. */
|
|
35
|
+
totalChanged: boolean;
|
|
36
|
+
/** Rows skipped because `keyOf` had already seen them. */
|
|
37
|
+
duplicates: number;
|
|
38
|
+
pages: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Calls `fetchPage(page, pageSize)` for page 1, 2, … until everything is read, the cap is reached or `signal` aborts.
|
|
42
|
+
* Stops when the rows read reach the server's latest total, on an empty page, or — only when the server never reports a
|
|
43
|
+
* total — on a page shorter than asked for (a server may cap the page size, so a short page alone proves nothing).
|
|
44
|
+
* Sequential by design: the pages are cheap to compute and the page size is what makes it fast.
|
|
45
|
+
*/
|
|
46
|
+
export declare function fetchAllPages<T>(fetchPage: (page: number, pageSize: number) => Promise<PageResult<T>>, options?: FetchAllOptions<T>): Promise<FetchAllResult<T>>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export const DEFAULT_PAGE_SIZE = 500;
|
|
2
|
+
export const DEFAULT_MAX_ROWS = 1e4;
|
|
3
|
+
function abortError(signal) {
|
|
4
|
+
return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
5
|
+
}
|
|
6
|
+
export async function fetchAllPages(fetchPage, options = {}) {
|
|
7
|
+
const pageSize = Math.max(1, Math.floor(options.pageSize ?? DEFAULT_PAGE_SIZE) || DEFAULT_PAGE_SIZE);
|
|
8
|
+
const maxRows = Math.max(1, Math.floor(options.maxRows ?? DEFAULT_MAX_ROWS) || DEFAULT_MAX_ROWS);
|
|
9
|
+
const { signal, onProgress, keyOf } = options;
|
|
10
|
+
const rows = [];
|
|
11
|
+
const seen = keyOf ? /* @__PURE__ */ new Set() : void 0;
|
|
12
|
+
let total;
|
|
13
|
+
let totalChanged = false;
|
|
14
|
+
let duplicates = 0;
|
|
15
|
+
let truncated = false;
|
|
16
|
+
let page = 0;
|
|
17
|
+
for (; ; ) {
|
|
18
|
+
if (signal?.aborted) throw abortError(signal);
|
|
19
|
+
page++;
|
|
20
|
+
const result = await fetchPage(page, pageSize);
|
|
21
|
+
if (signal?.aborted) throw abortError(signal);
|
|
22
|
+
if (typeof result.total === "number" && Number.isFinite(result.total)) {
|
|
23
|
+
if (total !== void 0 && result.total !== total) totalChanged = true;
|
|
24
|
+
total = result.total;
|
|
25
|
+
}
|
|
26
|
+
let added = 0;
|
|
27
|
+
for (const row of result.rows) {
|
|
28
|
+
if (seen && keyOf) {
|
|
29
|
+
const key = keyOf(row);
|
|
30
|
+
if (seen.has(key)) {
|
|
31
|
+
duplicates++;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
seen.add(key);
|
|
35
|
+
}
|
|
36
|
+
if (rows.length >= maxRows) {
|
|
37
|
+
truncated = true;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
rows.push(row);
|
|
41
|
+
added++;
|
|
42
|
+
}
|
|
43
|
+
onProgress?.({ loaded: rows.length, total, target: total === void 0 ? void 0 : Math.min(total, maxRows), page });
|
|
44
|
+
if (truncated) break;
|
|
45
|
+
if (rows.length >= maxRows) {
|
|
46
|
+
truncated = total === void 0 ? result.rows.length >= pageSize : total > rows.length + duplicates;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (result.rows.length === 0) break;
|
|
50
|
+
if (keyOf && added === 0) break;
|
|
51
|
+
if (total !== void 0) {
|
|
52
|
+
if (rows.length + duplicates >= total) break;
|
|
53
|
+
} else if (result.rows.length < pageSize) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { rows, total, truncated, totalChanged, duplicates, pages: page };
|
|
58
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Middleware } from '@sfxcode/nuxt-mongocamp-server/api';
|
|
2
|
+
/** `true` for the body of a 401 that names what the user may not do, `false` for a dead token or anything else. */
|
|
3
|
+
export declare function isPermissionDeniedBody(status: number, body: unknown): boolean;
|
|
4
|
+
export declare const permissionDeniedMiddleware: Middleware;
|
|
5
|
+
/**
|
|
6
|
+
* Puts the middleware first in the list the dependency's API instances share (they are all built from one
|
|
7
|
+
* `Configuration`, so one array). Safe to call on anything: a test double without a `middleware` array is left alone,
|
|
8
|
+
* and a list that already has it is not changed.
|
|
9
|
+
*/
|
|
10
|
+
export declare function installPermissionDeniedMiddleware<T extends object>(apis: T): T;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const NOT_AUTHORIZED_CODE = 4011;
|
|
2
|
+
export function isPermissionDeniedBody(status, body) {
|
|
3
|
+
if (status !== 401 || typeof body !== "object" || body === null) return false;
|
|
4
|
+
const { code, msg } = body;
|
|
5
|
+
return code === NOT_AUTHORIZED_CODE && typeof msg === "string" && /^user not authorized for\b/i.test(msg.trim());
|
|
6
|
+
}
|
|
7
|
+
export const permissionDeniedMiddleware = {
|
|
8
|
+
post: async ({ response }) => {
|
|
9
|
+
if (response.status !== 401) return void 0;
|
|
10
|
+
try {
|
|
11
|
+
const text = await response.clone().text();
|
|
12
|
+
if (!isPermissionDeniedBody(401, JSON.parse(text))) return void 0;
|
|
13
|
+
return new Response(text, { status: 403, statusText: "Forbidden", headers: new Headers(response.headers) });
|
|
14
|
+
} catch {
|
|
15
|
+
return void 0;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export function installPermissionDeniedMiddleware(apis) {
|
|
20
|
+
const list = apis.adminApi?.middleware;
|
|
21
|
+
if (Array.isArray(list) && !list.includes(permissionDeniedMiddleware)) list.unshift(permissionDeniedMiddleware);
|
|
22
|
+
return apis;
|
|
23
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Role, UserProfile } from '@sfxcode/nuxt-mongocamp-server';
|
|
2
|
+
import type { GrantPermission } from './grantMatching.js';
|
|
3
|
+
export declare const PERMISSIONS: readonly GrantPermission[];
|
|
4
|
+
/** What a user may do on one collection. `undefined` = not known (the grants of a GridFS bucket's `.files` / `.chunks` were never verified). */
|
|
5
|
+
export interface Cell {
|
|
6
|
+
read: boolean | undefined;
|
|
7
|
+
write: boolean | undefined;
|
|
8
|
+
administrate: boolean | undefined;
|
|
9
|
+
/** An admin user: everything is allowed whatever the grants say. */
|
|
10
|
+
admin: boolean;
|
|
11
|
+
/** The roles whose grants allow at least one permission here (empty for an admin and for a user without a grant). */
|
|
12
|
+
via: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface MatrixUser {
|
|
15
|
+
name: string;
|
|
16
|
+
admin: boolean;
|
|
17
|
+
roles: string[];
|
|
18
|
+
/** Roles the user has that the role list does not know (deleted meanwhile): their grants cannot count. */
|
|
19
|
+
unknownRoles: string[];
|
|
20
|
+
}
|
|
21
|
+
export interface PermissionMatrix {
|
|
22
|
+
users: MatrixUser[];
|
|
23
|
+
collections: string[];
|
|
24
|
+
/** `cells[userName][collection]` */
|
|
25
|
+
cells: Record<string, Record<string, Cell>>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The matrix for `users` and `collections`. Roles are looked up by name (case-sensitive, as the server keeps them). A user's own
|
|
29
|
+
* `grants` field (what the server lists with the user) counts as well, so a grant that is not in any role still shows.
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildPermissionMatrix(users: readonly UserProfile[], roles: readonly Role[], collections: readonly string[]): PermissionMatrix;
|
|
32
|
+
/** The letters of a cell, `R W A` with `·` for what is missing; `?` for an unknown one; `admin` for an administrator. */
|
|
33
|
+
export declare function cellLetters(cell: Cell): string;
|
|
34
|
+
/** The short form used in the CSV: `R`, `RW`, `RWA`, `admin`, empty for nothing and `?` for unknown. */
|
|
35
|
+
export declare function cellCode(cell: Cell): string;
|
|
36
|
+
export declare function hasAnyPermission(cell: Cell): boolean;
|
|
37
|
+
export type QuickView = 'all' | 'users-without-roles' | 'collections-nobody-writes' | 'collections-nobody-reads';
|
|
38
|
+
export declare const QUICK_VIEWS: readonly QuickView[];
|
|
39
|
+
export interface MatrixFilter {
|
|
40
|
+
/** Text matched against user and collection names, ignoring case. */
|
|
41
|
+
text?: string;
|
|
42
|
+
/** Only users that have this role. */
|
|
43
|
+
role?: string;
|
|
44
|
+
view?: QuickView;
|
|
45
|
+
}
|
|
46
|
+
/** Collections no non-admin user may write (admins always can); bucket members are never listed — their grants are unknown. */
|
|
47
|
+
export declare function collectionsNobodyCan(matrix: PermissionMatrix, permission: GrantPermission): string[];
|
|
48
|
+
export declare function usersWithoutRoles(matrix: PermissionMatrix): string[];
|
|
49
|
+
/** The matrix narrowed to what the filter asks for; the cells are shared, nothing is copied. */
|
|
50
|
+
export declare function filterMatrix(matrix: PermissionMatrix, filter: MatrixFilter): PermissionMatrix;
|
|
51
|
+
/** Rows for `rowsToCsv`: one per user, `user`, `roles`, then one column per collection with `cellCode`. */
|
|
52
|
+
export declare function matrixToCsvRows(matrix: PermissionMatrix): {
|
|
53
|
+
keys: string[];
|
|
54
|
+
rows: Record<string, unknown>[];
|
|
55
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { isBucketMemberCollection } from "./collectionProtection.js";
|
|
2
|
+
import { permissionFromGrants } from "./grantMatching.js";
|
|
3
|
+
export const PERMISSIONS = ["read", "write", "administrate"];
|
|
4
|
+
function grantsOf(role) {
|
|
5
|
+
return Array.isArray(role?.collectionGrants) ? role.collectionGrants : [];
|
|
6
|
+
}
|
|
7
|
+
export function buildPermissionMatrix(users, roles, collections) {
|
|
8
|
+
const roleByName = new Map(roles.map((role) => [role.name, role]));
|
|
9
|
+
const matrixUsers = [];
|
|
10
|
+
const cells = {};
|
|
11
|
+
for (const user of users) {
|
|
12
|
+
const roleNames = Array.isArray(user.roles) ? user.roles : [];
|
|
13
|
+
const known = roleNames.filter((name) => roleByName.has(name));
|
|
14
|
+
const admin = user.isAdmin === true || known.some((name) => roleByName.get(name).isAdmin === true);
|
|
15
|
+
matrixUsers.push({ name: user.user, admin, roles: roleNames, unknownRoles: roleNames.filter((name) => !roleByName.has(name)) });
|
|
16
|
+
const own = Array.isArray(user.grants) ? user.grants : [];
|
|
17
|
+
const all = [...known.flatMap((name) => grantsOf(roleByName.get(name))), ...own];
|
|
18
|
+
const row = {};
|
|
19
|
+
for (const collection of collections) {
|
|
20
|
+
if (admin) {
|
|
21
|
+
row[collection] = { read: true, write: true, administrate: true, admin: true, via: [] };
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const cell = {
|
|
25
|
+
read: permissionFromGrants(all, collection, "read"),
|
|
26
|
+
write: permissionFromGrants(all, collection, "write"),
|
|
27
|
+
administrate: permissionFromGrants(all, collection, "administrate"),
|
|
28
|
+
admin: false,
|
|
29
|
+
via: known.filter((name) => PERMISSIONS.some((p) => permissionFromGrants(grantsOf(roleByName.get(name)), collection, p) === true))
|
|
30
|
+
};
|
|
31
|
+
row[collection] = cell;
|
|
32
|
+
}
|
|
33
|
+
cells[user.user] = row;
|
|
34
|
+
}
|
|
35
|
+
return { users: matrixUsers, collections: [...collections], cells };
|
|
36
|
+
}
|
|
37
|
+
export function cellLetters(cell) {
|
|
38
|
+
if (cell.admin) return "admin";
|
|
39
|
+
const letter = (value, on) => value === void 0 ? "?" : value ? on : "\xB7";
|
|
40
|
+
return `${letter(cell.read, "R")}${letter(cell.write, "W")}${letter(cell.administrate, "A")}`;
|
|
41
|
+
}
|
|
42
|
+
export function cellCode(cell) {
|
|
43
|
+
if (cell.admin) return "admin";
|
|
44
|
+
if (cell.read === void 0 && cell.write === void 0 && cell.administrate === void 0) return "?";
|
|
45
|
+
return `${cell.read ? "R" : ""}${cell.write ? "W" : ""}${cell.administrate ? "A" : ""}`;
|
|
46
|
+
}
|
|
47
|
+
export function hasAnyPermission(cell) {
|
|
48
|
+
return cell.admin || PERMISSIONS.some((p) => cell[p] === true);
|
|
49
|
+
}
|
|
50
|
+
export const QUICK_VIEWS = ["all", "users-without-roles", "collections-nobody-writes", "collections-nobody-reads"];
|
|
51
|
+
export function collectionsNobodyCan(matrix, permission) {
|
|
52
|
+
const regular = matrix.users.filter((u) => !u.admin);
|
|
53
|
+
return matrix.collections.filter((collection) => !isBucketMemberCollection(collection) && regular.every((u) => matrix.cells[u.name][collection][permission] !== true));
|
|
54
|
+
}
|
|
55
|
+
export function usersWithoutRoles(matrix) {
|
|
56
|
+
return matrix.users.filter((u) => u.roles.length === 0 && !u.admin).map((u) => u.name);
|
|
57
|
+
}
|
|
58
|
+
export function filterMatrix(matrix, filter) {
|
|
59
|
+
const text = (filter.text ?? "").trim().toLowerCase();
|
|
60
|
+
let users = matrix.users;
|
|
61
|
+
let collections = matrix.collections;
|
|
62
|
+
if (filter.role) users = users.filter((u) => u.roles.includes(filter.role));
|
|
63
|
+
if (filter.view === "users-without-roles") {
|
|
64
|
+
const names = new Set(usersWithoutRoles(matrix));
|
|
65
|
+
users = users.filter((u) => names.has(u.name));
|
|
66
|
+
}
|
|
67
|
+
if (filter.view === "collections-nobody-writes") collections = collections.filter((c) => collectionsNobodyCan(matrix, "write").includes(c));
|
|
68
|
+
if (filter.view === "collections-nobody-reads") collections = collections.filter((c) => collectionsNobodyCan(matrix, "read").includes(c));
|
|
69
|
+
if (text) {
|
|
70
|
+
const userHit = users.filter((u) => u.name.toLowerCase().includes(text));
|
|
71
|
+
const collectionHit = collections.filter((c) => c.toLowerCase().includes(text));
|
|
72
|
+
if (userHit.length > 0 || collectionHit.length === 0) users = userHit;
|
|
73
|
+
if (collectionHit.length > 0 || userHit.length === 0) collections = collectionHit;
|
|
74
|
+
}
|
|
75
|
+
return { users, collections, cells: matrix.cells };
|
|
76
|
+
}
|
|
77
|
+
export function matrixToCsvRows(matrix) {
|
|
78
|
+
const keys = ["user", "roles", ...matrix.collections];
|
|
79
|
+
const rows = matrix.users.map((user) => {
|
|
80
|
+
const row = { user: user.name, roles: user.roles.join(" ") };
|
|
81
|
+
for (const collection of matrix.collections) row[collection] = cellCode(matrix.cells[user.name][collection]);
|
|
82
|
+
return row;
|
|
83
|
+
});
|
|
84
|
+
return { keys, rows };
|
|
85
|
+
}
|