@contractspec/lib.overlay-engine 1.56.1 → 1.58.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/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +535 -8
- package/dist/merger.d.ts +5 -9
- package/dist/merger.d.ts.map +1 -1
- package/dist/merger.js +120 -101
- package/dist/node/index.js +534 -0
- package/dist/node/merger.js +125 -0
- package/dist/node/react.js +21 -0
- package/dist/node/registry.js +262 -0
- package/dist/node/runtime.js +176 -0
- package/dist/node/signer.js +81 -0
- package/dist/node/spec.js +15 -0
- package/dist/node/types.js +0 -0
- package/dist/node/validator.js +131 -0
- package/dist/react.d.ts +6 -9
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +17 -18
- package/dist/registry.d.ts +22 -25
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +254 -98
- package/dist/runtime.d.ts +19 -23
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +174 -51
- package/dist/signer.d.ts +13 -17
- package/dist/signer.d.ts.map +1 -1
- package/dist/signer.js +71 -52
- package/dist/spec.d.ts +65 -69
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js +13 -12
- package/dist/types.d.ts +25 -28
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/dist/validator.d.ts +12 -16
- package/dist/validator.d.ts.map +1 -1
- package/dist/validator.js +123 -92
- package/package.json +75 -31
- package/dist/merger.js.map +0 -1
- package/dist/react.js.map +0 -1
- package/dist/registry.js.map +0 -1
- package/dist/runtime.js.map +0 -1
- package/dist/signer.js.map +0 -1
- package/dist/spec.js.map +0 -1
- package/dist/validator.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
export * from './spec';
|
|
2
|
+
export * from './types';
|
|
3
|
+
export * from './registry';
|
|
4
|
+
export * from './validator';
|
|
5
|
+
export * from './runtime';
|
|
6
|
+
export * from './merger';
|
|
7
|
+
export * from './signer';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,535 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/spec.ts
|
|
3
|
+
var OVERLAY_SCOPE_ORDER = [
|
|
4
|
+
"tenantId",
|
|
5
|
+
"role",
|
|
6
|
+
"userId",
|
|
7
|
+
"device",
|
|
8
|
+
"tags"
|
|
9
|
+
];
|
|
10
|
+
function defineOverlay(spec) {
|
|
11
|
+
return spec;
|
|
12
|
+
}
|
|
13
|
+
// src/validator.ts
|
|
14
|
+
var TARGET_KEYS = [
|
|
15
|
+
"capability",
|
|
16
|
+
"workflow",
|
|
17
|
+
"dataView",
|
|
18
|
+
"presentation",
|
|
19
|
+
"operation"
|
|
20
|
+
];
|
|
21
|
+
var defaultOverlayValidator = (spec) => validateOverlaySpec(spec);
|
|
22
|
+
function validateOverlaySpec(spec) {
|
|
23
|
+
const issues = [];
|
|
24
|
+
if (!spec.overlayId?.trim()) {
|
|
25
|
+
issues.push({
|
|
26
|
+
code: "overlay.id",
|
|
27
|
+
message: "overlayId is required",
|
|
28
|
+
path: ["overlayId"]
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (!spec.version?.trim()) {
|
|
32
|
+
issues.push({
|
|
33
|
+
code: "overlay.version",
|
|
34
|
+
message: "version is required",
|
|
35
|
+
path: ["version"]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const hasTarget = TARGET_KEYS.some((key) => {
|
|
39
|
+
const value = spec.appliesTo?.[key];
|
|
40
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
41
|
+
});
|
|
42
|
+
if (!hasTarget) {
|
|
43
|
+
issues.push({
|
|
44
|
+
code: "overlay.target",
|
|
45
|
+
message: "Overlay must specify at least one target (capability, workflow, dataView, presentation, or operation).",
|
|
46
|
+
path: ["appliesTo"]
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (!spec.modifications?.length) {
|
|
50
|
+
issues.push({
|
|
51
|
+
code: "overlay.modifications.empty",
|
|
52
|
+
message: "Overlay must include at least one modification.",
|
|
53
|
+
path: ["modifications"]
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
spec.modifications.forEach((mod, idx) => {
|
|
57
|
+
const path = ["modifications", String(idx)];
|
|
58
|
+
validateModification(mod, path, issues);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
valid: issues.length === 0,
|
|
63
|
+
issues
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function validateModification(modification, path, issues) {
|
|
67
|
+
const push = (code, message, extraPath) => {
|
|
68
|
+
issues.push({
|
|
69
|
+
code,
|
|
70
|
+
message,
|
|
71
|
+
path: extraPath ? [...path, ...extraPath] : path
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
if (isFieldModification(modification)) {
|
|
75
|
+
if (!modification.field?.trim()) {
|
|
76
|
+
push("overlay.mod.field", "field is required for this modification", [
|
|
77
|
+
"field"
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
switch (modification.type) {
|
|
82
|
+
case "renameLabel": {
|
|
83
|
+
if (!modification.newLabel?.trim()) {
|
|
84
|
+
push("overlay.mod.renameLabel.newLabel", "newLabel is required", [
|
|
85
|
+
"newLabel"
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case "reorderFields": {
|
|
91
|
+
if (!modification.fields?.length) {
|
|
92
|
+
push("overlay.mod.reorderFields.fields", "fields list cannot be empty", ["fields"]);
|
|
93
|
+
}
|
|
94
|
+
const seen = new Set;
|
|
95
|
+
for (const field of modification.fields ?? []) {
|
|
96
|
+
if (!field?.trim()) {
|
|
97
|
+
push("overlay.mod.reorderFields.fields.blank", "fields entries must be non-empty");
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
if (seen.has(field)) {
|
|
101
|
+
push("overlay.mod.reorderFields.fields.duplicate", `field "${field}" was listed multiple times`);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
seen.add(field);
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "setDefault": {
|
|
109
|
+
if (modification.value === undefined) {
|
|
110
|
+
push("overlay.mod.setDefault.value", "value is required", ["value"]);
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "addHelpText": {
|
|
115
|
+
if (!modification.text?.trim()) {
|
|
116
|
+
push("overlay.mod.addHelpText.text", "text is required", ["text"]);
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case "makeRequired":
|
|
121
|
+
case "hideField":
|
|
122
|
+
break;
|
|
123
|
+
default: {
|
|
124
|
+
const exhaustive = modification;
|
|
125
|
+
throw new Error(`Unsupported overlay modification ${exhaustive?.type ?? "unknown"}`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function isFieldModification(mod) {
|
|
130
|
+
return "field" in mod;
|
|
131
|
+
}
|
|
132
|
+
function assertOverlayValid(spec, validator = defaultOverlayValidator) {
|
|
133
|
+
const result = validator(spec);
|
|
134
|
+
if (!result.valid) {
|
|
135
|
+
const message = result.issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
136
|
+
throw new Error(`Invalid OverlaySpec "${spec.overlayId}": ${message}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/registry.ts
|
|
141
|
+
var TARGET_KEYS2 = [
|
|
142
|
+
"capability",
|
|
143
|
+
"workflow",
|
|
144
|
+
"dataView",
|
|
145
|
+
"presentation",
|
|
146
|
+
"operation"
|
|
147
|
+
];
|
|
148
|
+
var SCOPE_WEIGHTS = {
|
|
149
|
+
tenantId: 8,
|
|
150
|
+
role: 4,
|
|
151
|
+
userId: 16,
|
|
152
|
+
device: 2,
|
|
153
|
+
tags: 1
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
class OverlayRegistry {
|
|
157
|
+
options;
|
|
158
|
+
overlays = new Map;
|
|
159
|
+
constructor(options = {}) {
|
|
160
|
+
this.options = options;
|
|
161
|
+
}
|
|
162
|
+
register(overlay, options) {
|
|
163
|
+
if (!options?.skipValidation) {
|
|
164
|
+
const validator = this.options.validator ?? defaultOverlayValidator;
|
|
165
|
+
const result = validator(overlay);
|
|
166
|
+
if (!result.valid) {
|
|
167
|
+
const reason = result.issues.map((issue) => `${issue.code}: ${issue.message}`).join("; ");
|
|
168
|
+
throw new Error(`Overlay "${overlay.overlayId}" failed validation: ${reason}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const normalized = this.ensureSigned(overlay);
|
|
172
|
+
const key = this.getKey(normalized.overlayId, normalized.version);
|
|
173
|
+
const stored = {
|
|
174
|
+
overlay: normalized,
|
|
175
|
+
specificity: computeSpecificity(normalized.appliesTo),
|
|
176
|
+
registeredAt: Date.now()
|
|
177
|
+
};
|
|
178
|
+
this.overlays.set(key, stored);
|
|
179
|
+
return normalized;
|
|
180
|
+
}
|
|
181
|
+
unregister(overlayId, version) {
|
|
182
|
+
if (version) {
|
|
183
|
+
this.overlays.delete(this.getKey(overlayId, version));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (const key of Array.from(this.overlays.keys())) {
|
|
187
|
+
if (key.startsWith(`${overlayId}@`)) {
|
|
188
|
+
this.overlays.delete(key);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
list() {
|
|
193
|
+
return Array.from(this.overlays.values()).map((entry) => entry.overlay);
|
|
194
|
+
}
|
|
195
|
+
get(overlayId, version) {
|
|
196
|
+
return this.overlays.get(this.getKey(overlayId, version))?.overlay;
|
|
197
|
+
}
|
|
198
|
+
forContext(query) {
|
|
199
|
+
return Array.from(this.overlays.values()).filter((entry) => matches(entry.overlay.appliesTo, query)).sort((a, b) => {
|
|
200
|
+
if (a.specificity !== b.specificity) {
|
|
201
|
+
return a.specificity - b.specificity;
|
|
202
|
+
}
|
|
203
|
+
return a.registeredAt - b.registeredAt;
|
|
204
|
+
}).map((entry) => entry.overlay);
|
|
205
|
+
}
|
|
206
|
+
clear() {
|
|
207
|
+
this.overlays.clear();
|
|
208
|
+
}
|
|
209
|
+
size() {
|
|
210
|
+
return this.overlays.size;
|
|
211
|
+
}
|
|
212
|
+
ensureSigned(input) {
|
|
213
|
+
if (isSignedOverlay(input)) {
|
|
214
|
+
if (!input.signature?.signature && !this.options.allowUnsigned) {
|
|
215
|
+
throw new Error(`Overlay "${input.overlayId}" is missing a signature.`);
|
|
216
|
+
}
|
|
217
|
+
return input;
|
|
218
|
+
}
|
|
219
|
+
if (!this.options.allowUnsigned) {
|
|
220
|
+
throw new Error(`Overlay "${input.overlayId}" must be signed before registration.`);
|
|
221
|
+
}
|
|
222
|
+
return input;
|
|
223
|
+
}
|
|
224
|
+
getKey(overlayId, version) {
|
|
225
|
+
return `${overlayId}@${version}`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function isSignedOverlay(spec) {
|
|
229
|
+
return Boolean(spec.signature);
|
|
230
|
+
}
|
|
231
|
+
function computeSpecificity(appliesTo) {
|
|
232
|
+
let score = 0;
|
|
233
|
+
Object.keys(SCOPE_WEIGHTS).forEach((key) => {
|
|
234
|
+
const hasValue = key === "tags" ? Array.isArray(appliesTo.tags) && appliesTo.tags.length > 0 : Boolean(appliesTo[key]);
|
|
235
|
+
if (hasValue) {
|
|
236
|
+
score += SCOPE_WEIGHTS[key];
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
return score;
|
|
240
|
+
}
|
|
241
|
+
function matches(appliesTo, ctx) {
|
|
242
|
+
for (const key of TARGET_KEYS2) {
|
|
243
|
+
const expected = appliesTo[key];
|
|
244
|
+
if (expected && expected !== ctx[key]) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (appliesTo.tenantId && appliesTo.tenantId !== ctx.tenantId) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
if (appliesTo.role && appliesTo.role !== ctx.role) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
if (appliesTo.userId && appliesTo.userId !== ctx.userId) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (appliesTo.device && appliesTo.device !== ctx.device) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
if (appliesTo.tags?.length) {
|
|
261
|
+
if (!ctx.tags?.length) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
const ctxTags = new Set(ctx.tags);
|
|
265
|
+
const satisfies = appliesTo.tags.every((tag) => ctxTags.has(tag));
|
|
266
|
+
if (!satisfies) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/merger.ts
|
|
274
|
+
function applyOverlayModifications(target, overlays, options = {}) {
|
|
275
|
+
if (!overlays.length) {
|
|
276
|
+
return target;
|
|
277
|
+
}
|
|
278
|
+
const states = target.fields.map((field) => ({
|
|
279
|
+
key: field.key,
|
|
280
|
+
field: { ...field },
|
|
281
|
+
hidden: field.visible === false
|
|
282
|
+
}));
|
|
283
|
+
const fieldMap = new Map(states.map((state) => [state.key, state]));
|
|
284
|
+
let orderSequence = target.fields.map((field) => field.key);
|
|
285
|
+
const handleMissing = (field, overlayId) => {
|
|
286
|
+
if (options.strict) {
|
|
287
|
+
throw new Error(`Overlay "${overlayId}" referenced unknown field "${field}".`);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
overlays.forEach((overlay) => {
|
|
291
|
+
overlay.modifications.forEach((modification) => {
|
|
292
|
+
switch (modification.type) {
|
|
293
|
+
case "hideField": {
|
|
294
|
+
const state = fieldMap.get(modification.field);
|
|
295
|
+
if (!state)
|
|
296
|
+
return handleMissing(modification.field, overlay.overlayId);
|
|
297
|
+
state.hidden = true;
|
|
298
|
+
state.field.visible = false;
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case "renameLabel": {
|
|
302
|
+
const state = fieldMap.get(modification.field);
|
|
303
|
+
if (!state)
|
|
304
|
+
return handleMissing(modification.field, overlay.overlayId);
|
|
305
|
+
state.field.label = modification.newLabel;
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
case "setDefault": {
|
|
309
|
+
const state = fieldMap.get(modification.field);
|
|
310
|
+
if (!state)
|
|
311
|
+
return handleMissing(modification.field, overlay.overlayId);
|
|
312
|
+
state.field.defaultValue = modification.value;
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case "addHelpText": {
|
|
316
|
+
const state = fieldMap.get(modification.field);
|
|
317
|
+
if (!state)
|
|
318
|
+
return handleMissing(modification.field, overlay.overlayId);
|
|
319
|
+
state.field.helpText = modification.text;
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
case "makeRequired": {
|
|
323
|
+
const state = fieldMap.get(modification.field);
|
|
324
|
+
if (!state)
|
|
325
|
+
return handleMissing(modification.field, overlay.overlayId);
|
|
326
|
+
state.field.required = modification.required ?? true;
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
case "reorderFields": {
|
|
330
|
+
const { filtered, missing } = normalizeOrderList(modification.fields, fieldMap);
|
|
331
|
+
if (missing.length && options.strict) {
|
|
332
|
+
missing.forEach((field) => handleMissing(field, overlay.overlayId));
|
|
333
|
+
}
|
|
334
|
+
orderSequence = applyReorder(orderSequence, filtered);
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
default:
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
const visibleFields = [];
|
|
343
|
+
const seen = new Set;
|
|
344
|
+
orderSequence.forEach((key) => {
|
|
345
|
+
const state = fieldMap.get(key);
|
|
346
|
+
if (!state || state.hidden) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
seen.add(key);
|
|
350
|
+
visibleFields.push(state.field);
|
|
351
|
+
});
|
|
352
|
+
states.forEach((state) => {
|
|
353
|
+
if (state.hidden || seen.has(state.key)) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
visibleFields.push(state.field);
|
|
357
|
+
});
|
|
358
|
+
visibleFields.forEach((field, index) => {
|
|
359
|
+
field.order = index;
|
|
360
|
+
field.visible = true;
|
|
361
|
+
});
|
|
362
|
+
return {
|
|
363
|
+
...target,
|
|
364
|
+
fields: visibleFields
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function normalizeOrderList(fields, fieldMap) {
|
|
368
|
+
const filtered = [];
|
|
369
|
+
const missing = [];
|
|
370
|
+
const seen = new Set;
|
|
371
|
+
fields.forEach((field) => {
|
|
372
|
+
if (!field?.trim()) {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (!fieldMap.has(field)) {
|
|
376
|
+
missing.push(field);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (seen.has(field)) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
seen.add(field);
|
|
383
|
+
filtered.push(field);
|
|
384
|
+
});
|
|
385
|
+
return { filtered, missing };
|
|
386
|
+
}
|
|
387
|
+
function applyReorder(sequence, orderedFields) {
|
|
388
|
+
if (!orderedFields.length) {
|
|
389
|
+
return sequence;
|
|
390
|
+
}
|
|
391
|
+
const orderedSet = new Set(orderedFields);
|
|
392
|
+
const remainder = sequence.filter((key) => !orderedSet.has(key));
|
|
393
|
+
return [...orderedFields, ...remainder];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// src/runtime.ts
|
|
397
|
+
class OverlayEngine {
|
|
398
|
+
registry;
|
|
399
|
+
audit;
|
|
400
|
+
constructor(options) {
|
|
401
|
+
this.registry = options.registry;
|
|
402
|
+
this.audit = options.audit;
|
|
403
|
+
}
|
|
404
|
+
apply(params) {
|
|
405
|
+
const overlays = params.overlays ?? this.registry.forContext({
|
|
406
|
+
capability: params.capability,
|
|
407
|
+
workflow: params.workflow,
|
|
408
|
+
dataView: params.dataView,
|
|
409
|
+
presentation: params.presentation,
|
|
410
|
+
operation: params.operation,
|
|
411
|
+
tenantId: params.tenantId,
|
|
412
|
+
role: params.role,
|
|
413
|
+
userId: params.userId,
|
|
414
|
+
device: params.device,
|
|
415
|
+
tags: params.tags
|
|
416
|
+
});
|
|
417
|
+
const merged = applyOverlayModifications(params.target, overlays, {
|
|
418
|
+
strict: params.strict
|
|
419
|
+
});
|
|
420
|
+
const context = extractContext(params);
|
|
421
|
+
overlays.forEach((overlay) => {
|
|
422
|
+
this.audit?.({
|
|
423
|
+
overlay: {
|
|
424
|
+
overlayId: overlay.overlayId,
|
|
425
|
+
version: overlay.version
|
|
426
|
+
},
|
|
427
|
+
context,
|
|
428
|
+
timestamp: new Date().toISOString()
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
return {
|
|
432
|
+
target: merged,
|
|
433
|
+
overlaysApplied: overlays
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function extractContext(params) {
|
|
438
|
+
return {
|
|
439
|
+
tenantId: params.tenantId,
|
|
440
|
+
role: params.role,
|
|
441
|
+
userId: params.userId,
|
|
442
|
+
device: params.device,
|
|
443
|
+
tags: params.tags
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// src/signer.ts
|
|
448
|
+
import stringify from "fast-json-stable-stringify";
|
|
449
|
+
import {
|
|
450
|
+
constants,
|
|
451
|
+
createPrivateKey,
|
|
452
|
+
createPublicKey,
|
|
453
|
+
sign,
|
|
454
|
+
verify
|
|
455
|
+
} from "crypto";
|
|
456
|
+
function signOverlay(spec, privateKey, options = {}) {
|
|
457
|
+
const algorithm = options.algorithm ?? "ed25519";
|
|
458
|
+
const keyObject = typeof privateKey === "string" || Buffer.isBuffer(privateKey) ? createPrivateKey(privateKey) : privateKey;
|
|
459
|
+
const payload = Buffer.from(canonicalizeOverlay(spec), "utf8");
|
|
460
|
+
let rawSignature;
|
|
461
|
+
if (algorithm === "ed25519") {
|
|
462
|
+
rawSignature = sign(null, payload, keyObject);
|
|
463
|
+
} else if (algorithm === "rsa-pss-sha256") {
|
|
464
|
+
rawSignature = sign("sha256", payload, {
|
|
465
|
+
key: keyObject,
|
|
466
|
+
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
467
|
+
saltLength: 32
|
|
468
|
+
});
|
|
469
|
+
} else {
|
|
470
|
+
throw new Error(`Unsupported overlay signature algorithm: ${algorithm}`);
|
|
471
|
+
}
|
|
472
|
+
const publicKey = options.publicKey ?? createPublicKey(keyObject).export({ format: "pem", type: "spki" }).toString();
|
|
473
|
+
return {
|
|
474
|
+
...spec,
|
|
475
|
+
signature: {
|
|
476
|
+
algorithm,
|
|
477
|
+
signature: rawSignature.toString("base64"),
|
|
478
|
+
publicKey,
|
|
479
|
+
keyId: options.keyId,
|
|
480
|
+
issuedAt: toIso(options.issuedAt) ?? new Date().toISOString(),
|
|
481
|
+
expiresAt: toIso(options.expiresAt),
|
|
482
|
+
metadata: options.metadata
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
function verifyOverlaySignature(overlay) {
|
|
487
|
+
if (!overlay.signature?.signature) {
|
|
488
|
+
throw new Error(`Overlay "${overlay.overlayId}" is missing signature metadata.`);
|
|
489
|
+
}
|
|
490
|
+
const payload = Buffer.from(canonicalizeOverlay(overlay), "utf8");
|
|
491
|
+
const signature = Buffer.from(overlay.signature.signature, "base64");
|
|
492
|
+
const publicKey = createPublicKey(overlay.signature.publicKey);
|
|
493
|
+
if (overlay.signature.algorithm === "ed25519") {
|
|
494
|
+
return verify(null, payload, publicKey, signature);
|
|
495
|
+
}
|
|
496
|
+
if (overlay.signature.algorithm === "rsa-pss-sha256") {
|
|
497
|
+
return verify("sha256", payload, {
|
|
498
|
+
key: publicKey,
|
|
499
|
+
padding: constants.RSA_PKCS1_PSS_PADDING,
|
|
500
|
+
saltLength: 32
|
|
501
|
+
}, signature);
|
|
502
|
+
}
|
|
503
|
+
throw new Error(`Unsupported overlay signature algorithm: ${overlay.signature.algorithm}`);
|
|
504
|
+
}
|
|
505
|
+
function canonicalizeOverlay(spec) {
|
|
506
|
+
const { signature, ...rest } = spec;
|
|
507
|
+
return stringify(rest);
|
|
508
|
+
}
|
|
509
|
+
function stripSignature(spec) {
|
|
510
|
+
const { signature, ...rest } = spec;
|
|
511
|
+
return { ...rest };
|
|
512
|
+
}
|
|
513
|
+
function toIso(value) {
|
|
514
|
+
if (!value) {
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
if (typeof value === "string") {
|
|
518
|
+
return new Date(value).toISOString();
|
|
519
|
+
}
|
|
520
|
+
return value.toISOString();
|
|
521
|
+
}
|
|
522
|
+
export {
|
|
523
|
+
verifyOverlaySignature,
|
|
524
|
+
validateOverlaySpec,
|
|
525
|
+
stripSignature,
|
|
526
|
+
signOverlay,
|
|
527
|
+
defineOverlay,
|
|
528
|
+
defaultOverlayValidator,
|
|
529
|
+
canonicalizeOverlay,
|
|
530
|
+
assertOverlayValid,
|
|
531
|
+
applyOverlayModifications,
|
|
532
|
+
OverlayRegistry,
|
|
533
|
+
OverlayEngine,
|
|
534
|
+
OVERLAY_SCOPE_ORDER
|
|
535
|
+
};
|
package/dist/merger.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { SignedOverlaySpec } from
|
|
2
|
-
import { OverlayRenderable } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface ApplyOverlayOptions {
|
|
6
|
-
strict?: boolean;
|
|
1
|
+
import type { SignedOverlaySpec } from './spec';
|
|
2
|
+
import type { OverlayRenderable } from './types';
|
|
3
|
+
export interface ApplyOverlayOptions {
|
|
4
|
+
strict?: boolean;
|
|
7
5
|
}
|
|
8
|
-
declare function applyOverlayModifications<T extends OverlayRenderable>(target: T, overlays: SignedOverlaySpec[], options?: ApplyOverlayOptions): T;
|
|
9
|
-
//#endregion
|
|
10
|
-
export { ApplyOverlayOptions, applyOverlayModifications };
|
|
6
|
+
export declare function applyOverlayModifications<T extends OverlayRenderable>(target: T, overlays: SignedOverlaySpec[], options?: ApplyOverlayOptions): T;
|
|
11
7
|
//# sourceMappingURL=merger.d.ts.map
|
package/dist/merger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merger.d.ts","
|
|
1
|
+
{"version":3,"file":"merger.d.ts","sourceRoot":"","sources":["../src/merger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,KAAK,EAAE,iBAAiB,EAA0B,MAAM,SAAS,CAAC;AAEzE,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAQD,wBAAgB,yBAAyB,CAAC,CAAC,SAAS,iBAAiB,EACnE,MAAM,EAAE,CAAC,EACT,QAAQ,EAAE,iBAAiB,EAAE,EAC7B,OAAO,GAAE,mBAAwB,GAChC,CAAC,CA4GH"}
|