@happyvertical/smrt-features 0.30.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/AGENTS.md +28 -0
- package/CLAUDE.md +1 -0
- package/LICENSE +7 -0
- package/README.md +60 -0
- package/dist/index.d.ts +203 -0
- package/dist/index.js +727 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.json +763 -0
- package/dist/smrt-knowledge.json +508 -0
- package/dist/types.d.ts +111 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +61 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,727 @@
|
|
|
1
|
+
import { ObjectRegistry, isQualifiedName, parseQualifiedName, createQualifiedName, smrt, SmrtObject, SmrtCollection } from "@happyvertical/smrt-core";
|
|
2
|
+
import { GLOBAL_FEATURE_SCOPE_ID, FeatureOverrideEffect } from "./types.js";
|
|
3
|
+
import { importWorkspaceModule } from "@happyvertical/smrt-core/utils/import-workspace-module";
|
|
4
|
+
ObjectRegistry.registerPackageManifest(
|
|
5
|
+
new URL("./manifest.json", import.meta.url)
|
|
6
|
+
);
|
|
7
|
+
function createFeatureKey(qualifiedClassName, localId) {
|
|
8
|
+
if (!isQualifiedName(qualifiedClassName)) {
|
|
9
|
+
throw new Error(
|
|
10
|
+
`Feature keys require a qualified class name. Received "${qualifiedClassName}".`
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
if (!localId) {
|
|
14
|
+
throw new Error("Feature localId is required.");
|
|
15
|
+
}
|
|
16
|
+
if (localId.includes("#")) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Feature localId "${localId}" cannot contain "#". This character is reserved for canonical feature keys.`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
return `${qualifiedClassName}#${localId}`;
|
|
22
|
+
}
|
|
23
|
+
function parseFeatureKey(featureKey) {
|
|
24
|
+
const separatorIndex = featureKey.lastIndexOf("#");
|
|
25
|
+
if (separatorIndex <= 0 || separatorIndex === featureKey.length - 1) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Invalid feature key "${featureKey}". Expected format "<qualifiedClassName>#<localId>".`
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
const qualifiedClassName = featureKey.slice(0, separatorIndex);
|
|
31
|
+
const localId = featureKey.slice(separatorIndex + 1);
|
|
32
|
+
if (!isQualifiedName(qualifiedClassName)) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`Invalid feature key "${featureKey}". "${qualifiedClassName}" is not a qualified class name.`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return { qualifiedClassName, localId };
|
|
38
|
+
}
|
|
39
|
+
function serializeFeatureMetadata(metadata) {
|
|
40
|
+
if (!metadata) {
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
if (typeof metadata === "string") {
|
|
44
|
+
const parsed = parseFeatureMetadataString(metadata);
|
|
45
|
+
return JSON.stringify(parsed);
|
|
46
|
+
}
|
|
47
|
+
if (!isFeatureMetadataRecord(metadata)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Feature metadata must be a plain object or a JSON string representing a plain object."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return JSON.stringify(metadata);
|
|
53
|
+
}
|
|
54
|
+
function parseFeatureMetadata(metadata) {
|
|
55
|
+
if (!metadata) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
return parseFeatureMetadataString(metadata);
|
|
60
|
+
} catch {
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function parseFeatureMetadataString(metadata) {
|
|
65
|
+
let parsed;
|
|
66
|
+
try {
|
|
67
|
+
parsed = JSON.parse(metadata);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Feature metadata must be a plain object or a JSON string representing a plain object.",
|
|
71
|
+
{ cause: error }
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (!isFeatureMetadataRecord(parsed)) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Feature metadata must be a plain object or a JSON string representing a plain object."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return parsed;
|
|
80
|
+
}
|
|
81
|
+
function isFeatureMetadataRecord(value) {
|
|
82
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const prototype = Object.getPrototypeOf(value);
|
|
86
|
+
return prototype === Object.prototype || prototype === null;
|
|
87
|
+
}
|
|
88
|
+
function getQualifiedClassNameFromRegistry(registration) {
|
|
89
|
+
if (registration.qualifiedName && isQualifiedName(registration.qualifiedName)) {
|
|
90
|
+
return registration.qualifiedName;
|
|
91
|
+
}
|
|
92
|
+
if (registration.packageName) {
|
|
93
|
+
return createQualifiedName(registration.packageName, registration.name);
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
function getPackageNameFromRegistry(registration) {
|
|
98
|
+
if (registration.packageName) {
|
|
99
|
+
return registration.packageName;
|
|
100
|
+
}
|
|
101
|
+
const qualifiedClassName = getQualifiedClassNameFromRegistry(registration);
|
|
102
|
+
if (!qualifiedClassName) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return parseQualifiedName(qualifiedClassName).packageName;
|
|
106
|
+
}
|
|
107
|
+
function extractFeatureSeedsFromRegistryEntry(registration) {
|
|
108
|
+
if (registration.visibility === "test") {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
const qualifiedClassName = getQualifiedClassNameFromRegistry(registration);
|
|
112
|
+
if (!qualifiedClassName) {
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
const featureConfig = registration.config.features;
|
|
116
|
+
if (!featureConfig) {
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
const packageName = registration.packageName || parseQualifiedName(qualifiedClassName).packageName;
|
|
120
|
+
return Object.entries(featureConfig).map(([localId, feature]) => ({
|
|
121
|
+
featureKey: createFeatureKey(qualifiedClassName, localId),
|
|
122
|
+
packageName,
|
|
123
|
+
qualifiedClassName,
|
|
124
|
+
className: registration.name,
|
|
125
|
+
localId,
|
|
126
|
+
defaultEnabled: feature.defaultEnabled,
|
|
127
|
+
label: feature.label,
|
|
128
|
+
description: feature.description,
|
|
129
|
+
metadata: feature.metadata,
|
|
130
|
+
visibility: registration.visibility
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
function extractFeatureSeedsFromManifest(manifest) {
|
|
134
|
+
const seeds = [];
|
|
135
|
+
for (const [objectKey, objectDef] of Object.entries(manifest.objects)) {
|
|
136
|
+
seeds.push(...extractFeatureSeedsFromManifestObject(objectKey, objectDef));
|
|
137
|
+
}
|
|
138
|
+
return seeds;
|
|
139
|
+
}
|
|
140
|
+
function extractTouchedPackagesFromManifest(manifest) {
|
|
141
|
+
const packages = /* @__PURE__ */ new Set();
|
|
142
|
+
if (manifest.packageName) {
|
|
143
|
+
packages.add(manifest.packageName);
|
|
144
|
+
}
|
|
145
|
+
for (const [objectKey, objectDef] of Object.entries(manifest.objects)) {
|
|
146
|
+
if (objectDef.visibility === "test") {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (objectDef.packageName) {
|
|
150
|
+
packages.add(objectDef.packageName);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const qualifiedClassName = resolveManifestQualifiedClassName(
|
|
154
|
+
objectKey,
|
|
155
|
+
objectDef
|
|
156
|
+
);
|
|
157
|
+
if (qualifiedClassName) {
|
|
158
|
+
packages.add(parseQualifiedName(qualifiedClassName).packageName);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return packages;
|
|
162
|
+
}
|
|
163
|
+
function extractFeatureSeedsFromManifestObject(objectKey, objectDef) {
|
|
164
|
+
if (objectDef.visibility === "test") {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
const features = objectDef.decoratorConfig.features;
|
|
168
|
+
if (!features) {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
const qualifiedClassName = resolveManifestQualifiedClassName(
|
|
172
|
+
objectKey,
|
|
173
|
+
objectDef
|
|
174
|
+
);
|
|
175
|
+
if (!qualifiedClassName) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
const packageName = objectDef.packageName || parseQualifiedName(qualifiedClassName).packageName;
|
|
179
|
+
return Object.entries(features).map(([localId, feature]) => ({
|
|
180
|
+
featureKey: createFeatureKey(qualifiedClassName, localId),
|
|
181
|
+
packageName,
|
|
182
|
+
qualifiedClassName,
|
|
183
|
+
className: objectDef.className,
|
|
184
|
+
localId,
|
|
185
|
+
defaultEnabled: feature.defaultEnabled,
|
|
186
|
+
label: feature.label,
|
|
187
|
+
description: feature.description,
|
|
188
|
+
metadata: feature.metadata && typeof feature.metadata === "object" ? feature.metadata : void 0,
|
|
189
|
+
visibility: objectDef.visibility
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
function resolveManifestQualifiedClassName(objectKey, objectDef) {
|
|
193
|
+
if (objectDef.qualifiedName && isQualifiedName(objectDef.qualifiedName)) {
|
|
194
|
+
return objectDef.qualifiedName;
|
|
195
|
+
}
|
|
196
|
+
if (isQualifiedName(objectKey)) {
|
|
197
|
+
return objectKey;
|
|
198
|
+
}
|
|
199
|
+
if (objectDef.packageName) {
|
|
200
|
+
return createQualifiedName(objectDef.packageName, objectDef.className);
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
function resolveFeatureKeyForTarget(classOrInstance, localId) {
|
|
205
|
+
const ctor = typeof classOrInstance === "function" ? classOrInstance : classOrInstance.constructor;
|
|
206
|
+
const registration = ObjectRegistry.getClassByConstructor(ctor);
|
|
207
|
+
if (!registration) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
`Cannot resolve feature "${localId}" because ${ctor?.name || "the target class"} is not registered with @smrt().`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
const qualifiedClassName = getQualifiedClassNameFromRegistry(registration);
|
|
213
|
+
if (!qualifiedClassName) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Cannot resolve feature "${localId}" for ${registration.name} because the class has no qualified package identity.`
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const feature = registration.config.features?.[localId];
|
|
219
|
+
if (!feature) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`Feature "${localId}" is not declared on ${qualifiedClassName}.`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
featureKey: createFeatureKey(qualifiedClassName, localId),
|
|
226
|
+
defaultEnabled: feature.defaultEnabled
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function findFeatureDefaultInRegistry(featureKey) {
|
|
230
|
+
const { qualifiedClassName, localId } = parseFeatureKey(featureKey);
|
|
231
|
+
const registration = ObjectRegistry.getClassByQualifiedName(qualifiedClassName) || ObjectRegistry.getClass(qualifiedClassName);
|
|
232
|
+
const feature = registration?.config.features?.[localId];
|
|
233
|
+
return feature?.defaultEnabled;
|
|
234
|
+
}
|
|
235
|
+
var __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor;
|
|
236
|
+
var __decorateClass$1 = (decorators, target, key, kind) => {
|
|
237
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target;
|
|
238
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
239
|
+
if (decorator = decorators[i])
|
|
240
|
+
result = decorator(result) || result;
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
let FeatureDefinition = class extends SmrtObject {
|
|
244
|
+
featureKey = "";
|
|
245
|
+
packageName = "";
|
|
246
|
+
qualifiedClassName = "";
|
|
247
|
+
className = "";
|
|
248
|
+
localId = "";
|
|
249
|
+
defaultEnabled = false;
|
|
250
|
+
label = "";
|
|
251
|
+
description = "";
|
|
252
|
+
metadata = "";
|
|
253
|
+
visibility = "public";
|
|
254
|
+
constructor(options = {}) {
|
|
255
|
+
super(options);
|
|
256
|
+
if (options.featureKey !== void 0) this.featureKey = options.featureKey;
|
|
257
|
+
if (options.packageName !== void 0)
|
|
258
|
+
this.packageName = options.packageName;
|
|
259
|
+
if (options.qualifiedClassName !== void 0) {
|
|
260
|
+
this.qualifiedClassName = options.qualifiedClassName;
|
|
261
|
+
}
|
|
262
|
+
if (options.className !== void 0) this.className = options.className;
|
|
263
|
+
if (options.localId !== void 0) this.localId = options.localId;
|
|
264
|
+
if (options.defaultEnabled !== void 0) {
|
|
265
|
+
this.defaultEnabled = options.defaultEnabled;
|
|
266
|
+
}
|
|
267
|
+
if (options.label !== void 0) this.label = options.label;
|
|
268
|
+
if (options.description !== void 0)
|
|
269
|
+
this.description = options.description;
|
|
270
|
+
if (options.visibility !== void 0) this.visibility = options.visibility;
|
|
271
|
+
if (options.metadata !== void 0) {
|
|
272
|
+
this.metadata = serializeFeatureMetadata(options.metadata);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
getMetadata() {
|
|
276
|
+
return parseFeatureMetadata(this.metadata);
|
|
277
|
+
}
|
|
278
|
+
setMetadata(metadata) {
|
|
279
|
+
this.metadata = serializeFeatureMetadata(metadata);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
FeatureDefinition = __decorateClass$1([
|
|
283
|
+
smrt({
|
|
284
|
+
tableName: "_smrt_feature_definitions",
|
|
285
|
+
api: { include: ["list", "get"] },
|
|
286
|
+
cli: { include: ["list", "get"], exclude: ["getMetadata", "setMetadata"] },
|
|
287
|
+
mcp: { include: ["list", "get"], exclude: ["getMetadata", "setMetadata"] },
|
|
288
|
+
conflictColumns: ["feature_key"]
|
|
289
|
+
})
|
|
290
|
+
], FeatureDefinition);
|
|
291
|
+
class FeatureDefinitionCollection extends SmrtCollection {
|
|
292
|
+
static _itemClass = FeatureDefinition;
|
|
293
|
+
async findByFeatureKey(featureKey) {
|
|
294
|
+
const results = await this.list({
|
|
295
|
+
where: { featureKey },
|
|
296
|
+
limit: 1
|
|
297
|
+
});
|
|
298
|
+
return results[0] ?? null;
|
|
299
|
+
}
|
|
300
|
+
async findByPackageName(packageName) {
|
|
301
|
+
return this.list({
|
|
302
|
+
where: { packageName }
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
async upsertDefinition(seed) {
|
|
306
|
+
const existing = await this.findByFeatureKey(seed.featureKey);
|
|
307
|
+
if (!existing) {
|
|
308
|
+
const created = await this.create({
|
|
309
|
+
...seed,
|
|
310
|
+
metadata: serializeFeatureMetadata(seed.metadata)
|
|
311
|
+
});
|
|
312
|
+
await created.save();
|
|
313
|
+
return { definition: created, status: "created" };
|
|
314
|
+
}
|
|
315
|
+
const nextMetadata = serializeFeatureMetadata(seed.metadata);
|
|
316
|
+
const changed = existing.packageName !== seed.packageName || existing.qualifiedClassName !== seed.qualifiedClassName || existing.className !== seed.className || existing.localId !== seed.localId || existing.defaultEnabled !== seed.defaultEnabled || existing.label !== (seed.label ?? "") || existing.description !== (seed.description ?? "") || existing.metadata !== nextMetadata || existing.visibility !== (seed.visibility ?? "public");
|
|
317
|
+
if (!changed) {
|
|
318
|
+
return { definition: existing, status: "unchanged" };
|
|
319
|
+
}
|
|
320
|
+
existing.packageName = seed.packageName;
|
|
321
|
+
existing.qualifiedClassName = seed.qualifiedClassName;
|
|
322
|
+
existing.className = seed.className;
|
|
323
|
+
existing.localId = seed.localId;
|
|
324
|
+
existing.defaultEnabled = seed.defaultEnabled;
|
|
325
|
+
existing.label = seed.label ?? "";
|
|
326
|
+
existing.description = seed.description ?? "";
|
|
327
|
+
existing.metadata = nextMetadata;
|
|
328
|
+
existing.visibility = seed.visibility ?? "public";
|
|
329
|
+
await existing.save();
|
|
330
|
+
return { definition: existing, status: "updated" };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
334
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
335
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
336
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
337
|
+
if (decorator = decorators[i])
|
|
338
|
+
result = decorator(result) || result;
|
|
339
|
+
return result;
|
|
340
|
+
};
|
|
341
|
+
let FeatureOverride = class extends SmrtObject {
|
|
342
|
+
featureKey = "";
|
|
343
|
+
scopeType = "global";
|
|
344
|
+
scopeId = GLOBAL_FEATURE_SCOPE_ID;
|
|
345
|
+
effect = FeatureOverrideEffect.INHERIT;
|
|
346
|
+
constructor(options = {}) {
|
|
347
|
+
super(options);
|
|
348
|
+
if (options.featureKey !== void 0) this.featureKey = options.featureKey;
|
|
349
|
+
if (options.scopeType !== void 0) this.scopeType = options.scopeType;
|
|
350
|
+
if (options.scopeId !== void 0) this.scopeId = options.scopeId;
|
|
351
|
+
if (options.effect !== void 0) this.effect = options.effect;
|
|
352
|
+
}
|
|
353
|
+
isInherit() {
|
|
354
|
+
return this.effect === FeatureOverrideEffect.INHERIT;
|
|
355
|
+
}
|
|
356
|
+
isEnabled() {
|
|
357
|
+
return this.effect === FeatureOverrideEffect.ENABLE;
|
|
358
|
+
}
|
|
359
|
+
isDisabled() {
|
|
360
|
+
return this.effect === FeatureOverrideEffect.DISABLE;
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
FeatureOverride = __decorateClass([
|
|
364
|
+
smrt({
|
|
365
|
+
tableName: "_smrt_feature_overrides",
|
|
366
|
+
api: { include: ["list", "get", "create", "update", "delete"] },
|
|
367
|
+
cli: {
|
|
368
|
+
exclude: ["isInherit", "isEnabled", "isDisabled"]
|
|
369
|
+
},
|
|
370
|
+
mcp: {
|
|
371
|
+
exclude: ["isInherit", "isEnabled", "isDisabled"]
|
|
372
|
+
},
|
|
373
|
+
conflictColumns: ["feature_key", "scope_type", "scope_id"]
|
|
374
|
+
})
|
|
375
|
+
], FeatureOverride);
|
|
376
|
+
class FeatureOverrideCollection extends SmrtCollection {
|
|
377
|
+
static _itemClass = FeatureOverride;
|
|
378
|
+
async findByFeatureKey(featureKey) {
|
|
379
|
+
return this.list({
|
|
380
|
+
where: { featureKey }
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
async findByFeatureAndScope(featureKey, scopeType, scopeId) {
|
|
384
|
+
const results = await this.list({
|
|
385
|
+
where: { featureKey, scopeType, scopeId },
|
|
386
|
+
limit: 1
|
|
387
|
+
});
|
|
388
|
+
return results[0] ?? null;
|
|
389
|
+
}
|
|
390
|
+
async getGlobalOverride(featureKey) {
|
|
391
|
+
return this.findByFeatureAndScope(
|
|
392
|
+
featureKey,
|
|
393
|
+
"global",
|
|
394
|
+
GLOBAL_FEATURE_SCOPE_ID
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
async getTenantOverride(featureKey, tenantId) {
|
|
398
|
+
return this.findByFeatureAndScope(featureKey, "tenant", tenantId);
|
|
399
|
+
}
|
|
400
|
+
async getOverrideMap(featureKey, scopeType, scopeIds) {
|
|
401
|
+
const result = /* @__PURE__ */ new Map();
|
|
402
|
+
if (scopeIds.length === 0) {
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
const overrides = await this.list({
|
|
406
|
+
where: { featureKey, scopeType, scopeId: scopeIds }
|
|
407
|
+
});
|
|
408
|
+
for (const override of overrides) {
|
|
409
|
+
result.set(override.scopeId, override);
|
|
410
|
+
}
|
|
411
|
+
return result;
|
|
412
|
+
}
|
|
413
|
+
async setOverride(featureKey, scopeType, scopeId, effect) {
|
|
414
|
+
const existing = await this.findByFeatureAndScope(
|
|
415
|
+
featureKey,
|
|
416
|
+
scopeType,
|
|
417
|
+
scopeId
|
|
418
|
+
);
|
|
419
|
+
if (existing) {
|
|
420
|
+
existing.effect = effect;
|
|
421
|
+
await existing.save();
|
|
422
|
+
return existing;
|
|
423
|
+
}
|
|
424
|
+
const created = await this.create({
|
|
425
|
+
featureKey,
|
|
426
|
+
scopeType,
|
|
427
|
+
scopeId,
|
|
428
|
+
effect
|
|
429
|
+
});
|
|
430
|
+
await created.save();
|
|
431
|
+
return created;
|
|
432
|
+
}
|
|
433
|
+
async setGlobalOverride(featureKey, effect) {
|
|
434
|
+
return this.setOverride(
|
|
435
|
+
featureKey,
|
|
436
|
+
"global",
|
|
437
|
+
GLOBAL_FEATURE_SCOPE_ID,
|
|
438
|
+
effect
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
async setTenantOverride(featureKey, tenantId, effect) {
|
|
442
|
+
return this.setOverride(featureKey, "tenant", tenantId, effect);
|
|
443
|
+
}
|
|
444
|
+
async removeOverride(featureKey, scopeType, scopeId) {
|
|
445
|
+
const existing = await this.findByFeatureAndScope(
|
|
446
|
+
featureKey,
|
|
447
|
+
scopeType,
|
|
448
|
+
scopeId
|
|
449
|
+
);
|
|
450
|
+
if (!existing) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
await existing.delete();
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
class FeatureResolver {
|
|
458
|
+
options;
|
|
459
|
+
resolverOptions;
|
|
460
|
+
featureDefinitions;
|
|
461
|
+
featureOverrides;
|
|
462
|
+
initializationPromise = null;
|
|
463
|
+
tenantHierarchyPromise = null;
|
|
464
|
+
constructor(options = {}, resolverOptions = {}) {
|
|
465
|
+
this.options = options;
|
|
466
|
+
this.resolverOptions = resolverOptions;
|
|
467
|
+
}
|
|
468
|
+
async isEnabled(featureKey, context = {}) {
|
|
469
|
+
await this.ensureInitialized();
|
|
470
|
+
const baseState = await this.resolveBaseState(featureKey);
|
|
471
|
+
const globalOverride = await this.featureOverrides.getGlobalOverride(featureKey);
|
|
472
|
+
const globalState = this.applyOverride(baseState, globalOverride?.effect);
|
|
473
|
+
if (!context.tenantId) {
|
|
474
|
+
return globalState;
|
|
475
|
+
}
|
|
476
|
+
const tenantHierarchy = await this.getTenantHierarchy();
|
|
477
|
+
if (!tenantHierarchy) {
|
|
478
|
+
const directOverride = await this.featureOverrides.getTenantOverride(
|
|
479
|
+
featureKey,
|
|
480
|
+
context.tenantId
|
|
481
|
+
);
|
|
482
|
+
return this.applyOverride(globalState, directOverride?.effect);
|
|
483
|
+
}
|
|
484
|
+
const chain = await tenantHierarchy.getChain(context.tenantId);
|
|
485
|
+
if (chain.length === 0) {
|
|
486
|
+
const directOverride = await this.featureOverrides.getTenantOverride(
|
|
487
|
+
featureKey,
|
|
488
|
+
context.tenantId
|
|
489
|
+
);
|
|
490
|
+
return this.applyOverride(globalState, directOverride?.effect);
|
|
491
|
+
}
|
|
492
|
+
const overrides = await this.featureOverrides.getOverrideMap(
|
|
493
|
+
featureKey,
|
|
494
|
+
"tenant",
|
|
495
|
+
chain.map((node) => node.id)
|
|
496
|
+
);
|
|
497
|
+
let inheritedState = globalState;
|
|
498
|
+
for (let index = 0; index < chain.length; index++) {
|
|
499
|
+
const current = chain[index];
|
|
500
|
+
const previous = index > 0 ? chain[index - 1] : null;
|
|
501
|
+
const shouldInherit = index === 0 || !!previous?.cascadePermissions && current.inheritPermissions;
|
|
502
|
+
const baseline = shouldInherit ? inheritedState : globalState;
|
|
503
|
+
const override = overrides.get(current.id);
|
|
504
|
+
inheritedState = this.applyOverride(baseline, override?.effect);
|
|
505
|
+
}
|
|
506
|
+
return inheritedState;
|
|
507
|
+
}
|
|
508
|
+
async isEnabledFor(classOrInstance, localId, context = {}) {
|
|
509
|
+
const { featureKey } = resolveFeatureKeyForTarget(classOrInstance, localId);
|
|
510
|
+
return this.isEnabled(featureKey, context);
|
|
511
|
+
}
|
|
512
|
+
async ensureInitialized() {
|
|
513
|
+
if (!this.initializationPromise) {
|
|
514
|
+
this.initializationPromise = (async () => {
|
|
515
|
+
this.featureDefinitions = await FeatureDefinitionCollection.create(this.options);
|
|
516
|
+
this.featureOverrides = await FeatureOverrideCollection.create(
|
|
517
|
+
this.options
|
|
518
|
+
);
|
|
519
|
+
})();
|
|
520
|
+
}
|
|
521
|
+
await this.initializationPromise;
|
|
522
|
+
}
|
|
523
|
+
async resolveBaseState(featureKey) {
|
|
524
|
+
const registryDefault = findFeatureDefaultInRegistry(featureKey);
|
|
525
|
+
if (registryDefault !== void 0) {
|
|
526
|
+
return registryDefault;
|
|
527
|
+
}
|
|
528
|
+
const definition = await this.featureDefinitions.findByFeatureKey(featureKey);
|
|
529
|
+
if (!definition) {
|
|
530
|
+
throw new Error(
|
|
531
|
+
`Feature "${featureKey}" is not declared in the registry or synced feature definitions.`
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
return definition.defaultEnabled;
|
|
535
|
+
}
|
|
536
|
+
applyOverride(currentState, effect) {
|
|
537
|
+
switch (effect) {
|
|
538
|
+
case FeatureOverrideEffect.ENABLE:
|
|
539
|
+
return true;
|
|
540
|
+
case FeatureOverrideEffect.DISABLE:
|
|
541
|
+
return false;
|
|
542
|
+
default:
|
|
543
|
+
return currentState;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
async getTenantHierarchy() {
|
|
547
|
+
if (!this.tenantHierarchyPromise) {
|
|
548
|
+
const loader = this.resolverOptions.tenantHierarchyLoader || defaultTenantHierarchyLoader;
|
|
549
|
+
this.tenantHierarchyPromise = loader(this.options);
|
|
550
|
+
}
|
|
551
|
+
return this.tenantHierarchyPromise;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
async function defaultTenantHierarchyLoader(options) {
|
|
555
|
+
try {
|
|
556
|
+
const usersModule = await importWorkspaceModule({
|
|
557
|
+
packageName: "@happyvertical/smrt-users",
|
|
558
|
+
sourceEntry: "packages/users/src/collections/index.ts",
|
|
559
|
+
purpose: "tenant-aware feature flag resolution"
|
|
560
|
+
});
|
|
561
|
+
const tenantCollection = await usersModule.TenantCollection.create(options);
|
|
562
|
+
return {
|
|
563
|
+
async getChain(tenantId) {
|
|
564
|
+
const tenant = await tenantCollection.get({ id: tenantId });
|
|
565
|
+
if (!tenant) {
|
|
566
|
+
return [];
|
|
567
|
+
}
|
|
568
|
+
const ancestors = await tenantCollection.getAncestorsFromRoot(tenantId);
|
|
569
|
+
return [...ancestors, tenant].map((node) => ({
|
|
570
|
+
id: String(node.id),
|
|
571
|
+
inheritPermissions: Boolean(node.inheritPermissions),
|
|
572
|
+
cascadePermissions: Boolean(node.cascadePermissions)
|
|
573
|
+
}));
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
} catch (error) {
|
|
577
|
+
if (isMissingUsersDependency(error)) {
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function isMissingUsersDependency(error) {
|
|
584
|
+
if (!(error instanceof Error)) {
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
const causeMessage = error.cause instanceof Error ? ` ${error.cause.message}` : "";
|
|
588
|
+
const text = `${error.message}${causeMessage}`;
|
|
589
|
+
return text.includes("@happyvertical/smrt-users") && (text.includes("Cannot find module") || text.includes("Failed to load") || text.includes("could not find"));
|
|
590
|
+
}
|
|
591
|
+
class FeatureSyncService {
|
|
592
|
+
options;
|
|
593
|
+
featureDefinitions;
|
|
594
|
+
initializationPromise = null;
|
|
595
|
+
constructor(options = {}) {
|
|
596
|
+
this.options = options;
|
|
597
|
+
}
|
|
598
|
+
async syncDefinitions(options = {}) {
|
|
599
|
+
await this.ensureInitialized();
|
|
600
|
+
const registrations = this.collectRegistrations(options);
|
|
601
|
+
const definitions = this.collectDefinitionsFromRegistrations(registrations);
|
|
602
|
+
const touchedPackages = this.collectTouchedPackagesFromRegistrations(registrations);
|
|
603
|
+
const isFilteredSync = Boolean(options.classNames?.length) || Boolean(options.constructors?.length);
|
|
604
|
+
const pruneStale = isFilteredSync ? false : options.pruneStale ?? true;
|
|
605
|
+
return this.applyDefinitions(definitions, touchedPackages, pruneStale);
|
|
606
|
+
}
|
|
607
|
+
async syncManifest(manifest, options = {}) {
|
|
608
|
+
await this.ensureInitialized();
|
|
609
|
+
const definitions = extractFeatureSeedsFromManifest(manifest);
|
|
610
|
+
const touchedPackages = extractTouchedPackagesFromManifest(manifest);
|
|
611
|
+
return this.applyDefinitions(
|
|
612
|
+
definitions,
|
|
613
|
+
touchedPackages,
|
|
614
|
+
options.pruneStale ?? true
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
async ensureInitialized() {
|
|
618
|
+
if (!this.initializationPromise) {
|
|
619
|
+
this.initializationPromise = (async () => {
|
|
620
|
+
this.featureDefinitions = await FeatureDefinitionCollection.create(this.options);
|
|
621
|
+
})();
|
|
622
|
+
}
|
|
623
|
+
await this.initializationPromise;
|
|
624
|
+
}
|
|
625
|
+
collectDefinitionsFromRegistrations(registrations) {
|
|
626
|
+
const definitions = [];
|
|
627
|
+
for (const registration of registrations) {
|
|
628
|
+
definitions.push(...extractFeatureSeedsFromRegistryEntry(registration));
|
|
629
|
+
}
|
|
630
|
+
return definitions;
|
|
631
|
+
}
|
|
632
|
+
collectTouchedPackagesFromRegistrations(registrations) {
|
|
633
|
+
const packages = /* @__PURE__ */ new Set();
|
|
634
|
+
for (const registration of registrations) {
|
|
635
|
+
if (registration.visibility === "test") {
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
const packageName = getPackageNameFromRegistry(registration);
|
|
639
|
+
if (packageName) {
|
|
640
|
+
packages.add(packageName);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
return packages;
|
|
644
|
+
}
|
|
645
|
+
collectRegistrations(options) {
|
|
646
|
+
if (options.classNames?.length) {
|
|
647
|
+
return options.classNames.map((name) => ObjectRegistry.getClass(name)).filter((value) => !!value);
|
|
648
|
+
}
|
|
649
|
+
if (options.constructors?.length) {
|
|
650
|
+
return options.constructors.map((ctor) => ObjectRegistry.getClassByConstructor(ctor)).filter((value) => !!value);
|
|
651
|
+
}
|
|
652
|
+
const registrations = [];
|
|
653
|
+
const seen = /* @__PURE__ */ new Set();
|
|
654
|
+
for (const registration of ObjectRegistry.getAllClasses().values()) {
|
|
655
|
+
if (seen.has(registration)) {
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
seen.add(registration);
|
|
659
|
+
registrations.push(registration);
|
|
660
|
+
}
|
|
661
|
+
return registrations;
|
|
662
|
+
}
|
|
663
|
+
async applyDefinitions(definitions, touchedPackages, pruneStale) {
|
|
664
|
+
const deduped = /* @__PURE__ */ new Map();
|
|
665
|
+
for (const definition of definitions) {
|
|
666
|
+
deduped.set(definition.featureKey, definition);
|
|
667
|
+
}
|
|
668
|
+
let created = 0;
|
|
669
|
+
let updated = 0;
|
|
670
|
+
let unchanged = 0;
|
|
671
|
+
let deleted = 0;
|
|
672
|
+
const dedupedDefinitions = Array.from(deduped.values());
|
|
673
|
+
const currentKeys = new Set(
|
|
674
|
+
dedupedDefinitions.map((definition) => definition.featureKey)
|
|
675
|
+
);
|
|
676
|
+
const upsertResults = await Promise.all(
|
|
677
|
+
dedupedDefinitions.map(
|
|
678
|
+
(definition) => this.featureDefinitions.upsertDefinition(definition)
|
|
679
|
+
)
|
|
680
|
+
);
|
|
681
|
+
for (const result of upsertResults) {
|
|
682
|
+
if (result.status === "created") created++;
|
|
683
|
+
if (result.status === "updated") updated++;
|
|
684
|
+
if (result.status === "unchanged") unchanged++;
|
|
685
|
+
}
|
|
686
|
+
if (pruneStale) {
|
|
687
|
+
const existingByPackage = await Promise.all(
|
|
688
|
+
Array.from(
|
|
689
|
+
touchedPackages,
|
|
690
|
+
(packageName) => this.featureDefinitions.findByPackageName(packageName)
|
|
691
|
+
)
|
|
692
|
+
);
|
|
693
|
+
const staleDefinitions = existingByPackage.flatMap(
|
|
694
|
+
(existing) => existing.filter(
|
|
695
|
+
(definition) => !currentKeys.has(definition.featureKey)
|
|
696
|
+
)
|
|
697
|
+
);
|
|
698
|
+
await Promise.all(
|
|
699
|
+
staleDefinitions.map((definition) => definition.delete())
|
|
700
|
+
);
|
|
701
|
+
deleted = staleDefinitions.length;
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
total: deduped.size,
|
|
705
|
+
created,
|
|
706
|
+
updated,
|
|
707
|
+
unchanged,
|
|
708
|
+
deleted,
|
|
709
|
+
featureKeys: Array.from(currentKeys).sort()
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
export {
|
|
714
|
+
FeatureDefinition,
|
|
715
|
+
FeatureDefinitionCollection,
|
|
716
|
+
FeatureOverride,
|
|
717
|
+
FeatureOverrideCollection,
|
|
718
|
+
FeatureOverrideEffect,
|
|
719
|
+
FeatureResolver,
|
|
720
|
+
FeatureSyncService,
|
|
721
|
+
GLOBAL_FEATURE_SCOPE_ID,
|
|
722
|
+
createFeatureKey,
|
|
723
|
+
findFeatureDefaultInRegistry,
|
|
724
|
+
parseFeatureKey,
|
|
725
|
+
resolveFeatureKeyForTarget
|
|
726
|
+
};
|
|
727
|
+
//# sourceMappingURL=index.js.map
|