@uniformdev/next-app-router-shared 20.7.1-alpha.118
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/LICENSE.txt +2 -0
- package/dist/index.d.mts +271 -0
- package/dist/index.d.ts +271 -0
- package/dist/index.esm.js +297 -0
- package/dist/index.js +334 -0
- package/dist/index.mjs +297 -0
- package/package.json +53 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// src/placements/evaluatePersonalization.ts
|
|
2
|
+
var evaluatePersonalization = ({
|
|
3
|
+
personalization,
|
|
4
|
+
context,
|
|
5
|
+
compositionContext
|
|
6
|
+
}) => {
|
|
7
|
+
const indexes = [];
|
|
8
|
+
const variantIds = [];
|
|
9
|
+
const componentIds = [];
|
|
10
|
+
const { variations } = context.personalize(personalization);
|
|
11
|
+
for (const v of variations) {
|
|
12
|
+
indexes.push(v.index);
|
|
13
|
+
variantIds.push({
|
|
14
|
+
id: v.id,
|
|
15
|
+
control: v.control
|
|
16
|
+
});
|
|
17
|
+
componentIds.push(v.componentId);
|
|
18
|
+
}
|
|
19
|
+
const event = {
|
|
20
|
+
name: personalization.name,
|
|
21
|
+
variantIds,
|
|
22
|
+
changed: true,
|
|
23
|
+
control: context.storage.data.controlGroup || variantIds.every((v) => v.control),
|
|
24
|
+
compositionMetadata: {
|
|
25
|
+
compositionId: compositionContext._id,
|
|
26
|
+
matchedRoute: compositionContext.matchedRoute,
|
|
27
|
+
dynamicInputs: compositionContext.dynamicInputs
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
indexes,
|
|
32
|
+
event,
|
|
33
|
+
componentIds
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/placements/evaluateTest.ts
|
|
38
|
+
var evaluateTest = ({
|
|
39
|
+
test,
|
|
40
|
+
context,
|
|
41
|
+
compositionContext
|
|
42
|
+
}) => {
|
|
43
|
+
var _a, _b, _c;
|
|
44
|
+
const isTestDefined = Boolean((_a = context == null ? void 0 : context.manifest.data.project.test) == null ? void 0 : _a[test.name]);
|
|
45
|
+
if (!isTestDefined && process.env.NODE_ENV !== "production") {
|
|
46
|
+
console.warn(`Test "${test}" is not defined in Uniform manifest.`);
|
|
47
|
+
return {
|
|
48
|
+
index: null,
|
|
49
|
+
componentId: null,
|
|
50
|
+
event: null
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
let index = null;
|
|
54
|
+
let variantId = null;
|
|
55
|
+
let componentId = null;
|
|
56
|
+
if (context) {
|
|
57
|
+
const { result } = context.test(test);
|
|
58
|
+
if (result) {
|
|
59
|
+
index = result.index;
|
|
60
|
+
variantId = result.id;
|
|
61
|
+
componentId = result.componentId;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (!variantId) {
|
|
65
|
+
const [first] = test.variations;
|
|
66
|
+
if (first) {
|
|
67
|
+
index = first.index;
|
|
68
|
+
variantId = first.id;
|
|
69
|
+
componentId = first.componentId;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const event = {
|
|
73
|
+
name: test.name,
|
|
74
|
+
control: (_c = (_b = test.variations.find((v) => v.id === variantId)) == null ? void 0 : _b.control) != null ? _c : false,
|
|
75
|
+
variantAssigned: true,
|
|
76
|
+
variantId: variantId != null ? variantId : "NO_VARIANTS",
|
|
77
|
+
compositionMetadata: {
|
|
78
|
+
compositionId: compositionContext._id,
|
|
79
|
+
matchedRoute: compositionContext.matchedRoute,
|
|
80
|
+
dynamicInputs: compositionContext.dynamicInputs
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
index,
|
|
85
|
+
componentId,
|
|
86
|
+
event
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/placements/extractPersonalizationName.ts
|
|
91
|
+
var extractPersonalizationName = ({
|
|
92
|
+
component
|
|
93
|
+
}) => {
|
|
94
|
+
var _a;
|
|
95
|
+
const trackingEventNameParameter = (_a = component.parameters) == null ? void 0 : _a.trackingEventName;
|
|
96
|
+
return (trackingEventNameParameter == null ? void 0 : trackingEventNameParameter.value) || "unknown";
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/placements/extractTestName.ts
|
|
100
|
+
var extractTestName = ({ component }) => {
|
|
101
|
+
var _a, _b;
|
|
102
|
+
const testName = (_a = component.parameters) == null ? void 0 : _a.test;
|
|
103
|
+
return (_b = testName == null ? void 0 : testName.value) != null ? _b : "unknown";
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// src/serialization.ts
|
|
107
|
+
import { v5 } from "uuid";
|
|
108
|
+
var ENCODED_RESERVED_CHARACTERS = [
|
|
109
|
+
{
|
|
110
|
+
character: "/",
|
|
111
|
+
replacement: "_"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
character: "+",
|
|
115
|
+
replacement: "-"
|
|
116
|
+
}
|
|
117
|
+
];
|
|
118
|
+
var isNotARegularUuid = (id) => {
|
|
119
|
+
return id.length !== 36;
|
|
120
|
+
};
|
|
121
|
+
var resolveComponentFromPageState = ({
|
|
122
|
+
pageState,
|
|
123
|
+
componentId: providedComponentId
|
|
124
|
+
}) => {
|
|
125
|
+
const componentId = isNotARegularUuid(providedComponentId) ? v5(providedComponentId, NAMESPACE_UUID) : providedComponentId;
|
|
126
|
+
let component = pageState.components[componentId];
|
|
127
|
+
if (!component) {
|
|
128
|
+
const componentIds = Object.keys(pageState.components);
|
|
129
|
+
for (let i = 0; i < componentIds.length; i++) {
|
|
130
|
+
const id = componentIds[i];
|
|
131
|
+
if (componentId.startsWith(id)) {
|
|
132
|
+
component = pageState.components[id];
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return component;
|
|
138
|
+
};
|
|
139
|
+
var getRuleId = (rule) => {
|
|
140
|
+
return v5(JSON.stringify(rule), NAMESPACE_UUID);
|
|
141
|
+
};
|
|
142
|
+
var resolveRuleFromPageState = ({
|
|
143
|
+
pageState,
|
|
144
|
+
rule
|
|
145
|
+
}) => {
|
|
146
|
+
if (!pageState.rules) {
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
const ruleId = getRuleId(rule);
|
|
150
|
+
let value = pageState.rules[ruleId];
|
|
151
|
+
if (typeof value === "undefined") {
|
|
152
|
+
const ruleIds = Object.keys(pageState.rules);
|
|
153
|
+
for (let i = 0; i < ruleIds.length; i++) {
|
|
154
|
+
const id = ruleIds[i];
|
|
155
|
+
if (ruleId.startsWith(id)) {
|
|
156
|
+
value = pageState.rules[id];
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return value;
|
|
162
|
+
};
|
|
163
|
+
var replaceReservedCharacters = (value) => {
|
|
164
|
+
return ENCODED_RESERVED_CHARACTERS.reduce((acc, char) => {
|
|
165
|
+
return acc.replaceAll(char.character, char.replacement);
|
|
166
|
+
}, value);
|
|
167
|
+
};
|
|
168
|
+
var unreplaceReservedCharacters = (value) => {
|
|
169
|
+
return ENCODED_RESERVED_CHARACTERS.reduce((acc, char) => {
|
|
170
|
+
return acc.replaceAll(char.replacement, char.character);
|
|
171
|
+
}, value);
|
|
172
|
+
};
|
|
173
|
+
var encodeWithReplacements = (value) => {
|
|
174
|
+
return replaceReservedCharacters(btoa(value));
|
|
175
|
+
};
|
|
176
|
+
var decodeWithReplacements = (value) => {
|
|
177
|
+
const urlDecoded = decodeURIComponent(value);
|
|
178
|
+
return atob(unreplaceReservedCharacters(urlDecoded));
|
|
179
|
+
};
|
|
180
|
+
var compressIds = (ids) => {
|
|
181
|
+
const uuidV4Length = 36;
|
|
182
|
+
let compressedRegistry;
|
|
183
|
+
for (let i = 0; i < uuidV4Length; i++) {
|
|
184
|
+
const registry = {};
|
|
185
|
+
let success = true;
|
|
186
|
+
for (let j = 0; j < ids.length; j++) {
|
|
187
|
+
const listId = ids[j];
|
|
188
|
+
const id = listId.slice(0, i + 1);
|
|
189
|
+
if (registry[id]) {
|
|
190
|
+
success = false;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
registry[id] = listId;
|
|
194
|
+
}
|
|
195
|
+
if (success) {
|
|
196
|
+
compressedRegistry = registry;
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return compressedRegistry != null ? compressedRegistry : ids.reduce(
|
|
201
|
+
(acc, listId) => {
|
|
202
|
+
acc[listId] = listId;
|
|
203
|
+
return acc;
|
|
204
|
+
},
|
|
205
|
+
{}
|
|
206
|
+
);
|
|
207
|
+
};
|
|
208
|
+
var NAMESPACE_UUID = "00000000-0000-0000-0000-000000000000";
|
|
209
|
+
var serializeEvaluationResult = ({
|
|
210
|
+
payload,
|
|
211
|
+
encode = true
|
|
212
|
+
}) => {
|
|
213
|
+
const compressedPageState = {
|
|
214
|
+
cs: payload.compositionState,
|
|
215
|
+
r: payload.routePath,
|
|
216
|
+
c: {},
|
|
217
|
+
k: payload.keys,
|
|
218
|
+
ri: payload.releaseId,
|
|
219
|
+
dc: typeof payload.defaultConsent !== "undefined" ? payload.defaultConsent ? 1 : 0 : void 0,
|
|
220
|
+
pm: payload.previewMode === "editor" ? "e" : payload.previewMode === "preview" ? "p" : void 0,
|
|
221
|
+
rl: void 0
|
|
222
|
+
};
|
|
223
|
+
const componentKeys = Object.keys(payload.components);
|
|
224
|
+
const sortedKeys = componentKeys.sort();
|
|
225
|
+
const sortedComponents = sortedKeys.map((key) => ({
|
|
226
|
+
_id: isNotARegularUuid(key) ? v5(key, NAMESPACE_UUID) : key,
|
|
227
|
+
...payload.components[key]
|
|
228
|
+
}));
|
|
229
|
+
const compressedComponentIds = compressIds(sortedComponents.map((c) => c._id));
|
|
230
|
+
Object.keys(compressedComponentIds).forEach((compressedId) => {
|
|
231
|
+
var _a;
|
|
232
|
+
const originalId = compressedComponentIds[compressedId];
|
|
233
|
+
const component = sortedComponents.find((c) => c._id === originalId);
|
|
234
|
+
if (!component) {
|
|
235
|
+
throw new Error(`Component ${originalId} not found`);
|
|
236
|
+
}
|
|
237
|
+
compressedPageState.c[compressedId] = {
|
|
238
|
+
i: ((_a = component.indexes) == null ? void 0 : _a.length) ? component.indexes : void 0
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
if (typeof payload.rules !== "undefined") {
|
|
242
|
+
const ruleKeys = Object.keys(payload.rules);
|
|
243
|
+
if (ruleKeys.length !== 0) {
|
|
244
|
+
compressedPageState.rl = {};
|
|
245
|
+
const sortedRuleKeys = ruleKeys.sort();
|
|
246
|
+
const compressedRuleKeys = compressIds(sortedRuleKeys);
|
|
247
|
+
Object.keys(compressedRuleKeys).forEach((compressedId) => {
|
|
248
|
+
const originalId = compressedRuleKeys[compressedId];
|
|
249
|
+
compressedPageState.rl[compressedId] = payload.rules[originalId] ? 1 : 0;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const result = JSON.stringify(compressedPageState);
|
|
254
|
+
return encode ? encodeWithReplacements(result) : result;
|
|
255
|
+
};
|
|
256
|
+
var deserializeEvaluationResult = ({
|
|
257
|
+
input: providedInput,
|
|
258
|
+
decode = true
|
|
259
|
+
}) => {
|
|
260
|
+
var _a, _b;
|
|
261
|
+
const input = decode ? decodeWithReplacements(providedInput) : providedInput;
|
|
262
|
+
const parsed = JSON.parse(input);
|
|
263
|
+
const pageState = {
|
|
264
|
+
compositionState: parsed.cs,
|
|
265
|
+
routePath: parsed.r,
|
|
266
|
+
components: {},
|
|
267
|
+
keys: (_a = parsed.k) != null ? _a : {},
|
|
268
|
+
releaseId: (_b = parsed.ri) != null ? _b : void 0,
|
|
269
|
+
defaultConsent: typeof parsed.dc !== "undefined" ? parsed.dc === 1 : void 0,
|
|
270
|
+
previewMode: parsed.pm === "e" ? "editor" : parsed.pm === "p" ? "preview" : void 0,
|
|
271
|
+
rules: parsed.rl ? Object.fromEntries(Object.entries(parsed.rl).map(([key, value]) => [key, value === 1])) : void 0
|
|
272
|
+
};
|
|
273
|
+
Object.keys(parsed.c).forEach((id) => {
|
|
274
|
+
const component = parsed.c[id];
|
|
275
|
+
pageState.components[id] = {
|
|
276
|
+
indexes: component.i
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
return pageState;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// src/index.ts
|
|
283
|
+
var UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME = "ufsc";
|
|
284
|
+
var UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME = "ufqc";
|
|
285
|
+
export {
|
|
286
|
+
UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME,
|
|
287
|
+
UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME,
|
|
288
|
+
deserializeEvaluationResult,
|
|
289
|
+
evaluatePersonalization,
|
|
290
|
+
evaluateTest,
|
|
291
|
+
extractPersonalizationName,
|
|
292
|
+
extractTestName,
|
|
293
|
+
getRuleId,
|
|
294
|
+
resolveComponentFromPageState,
|
|
295
|
+
resolveRuleFromPageState,
|
|
296
|
+
serializeEvaluationResult
|
|
297
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME: () => UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME,
|
|
24
|
+
UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME: () => UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME,
|
|
25
|
+
deserializeEvaluationResult: () => deserializeEvaluationResult,
|
|
26
|
+
evaluatePersonalization: () => evaluatePersonalization,
|
|
27
|
+
evaluateTest: () => evaluateTest,
|
|
28
|
+
extractPersonalizationName: () => extractPersonalizationName,
|
|
29
|
+
extractTestName: () => extractTestName,
|
|
30
|
+
getRuleId: () => getRuleId,
|
|
31
|
+
resolveComponentFromPageState: () => resolveComponentFromPageState,
|
|
32
|
+
resolveRuleFromPageState: () => resolveRuleFromPageState,
|
|
33
|
+
serializeEvaluationResult: () => serializeEvaluationResult
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/placements/evaluatePersonalization.ts
|
|
38
|
+
var evaluatePersonalization = ({
|
|
39
|
+
personalization,
|
|
40
|
+
context,
|
|
41
|
+
compositionContext
|
|
42
|
+
}) => {
|
|
43
|
+
const indexes = [];
|
|
44
|
+
const variantIds = [];
|
|
45
|
+
const componentIds = [];
|
|
46
|
+
const { variations } = context.personalize(personalization);
|
|
47
|
+
for (const v of variations) {
|
|
48
|
+
indexes.push(v.index);
|
|
49
|
+
variantIds.push({
|
|
50
|
+
id: v.id,
|
|
51
|
+
control: v.control
|
|
52
|
+
});
|
|
53
|
+
componentIds.push(v.componentId);
|
|
54
|
+
}
|
|
55
|
+
const event = {
|
|
56
|
+
name: personalization.name,
|
|
57
|
+
variantIds,
|
|
58
|
+
changed: true,
|
|
59
|
+
control: context.storage.data.controlGroup || variantIds.every((v) => v.control),
|
|
60
|
+
compositionMetadata: {
|
|
61
|
+
compositionId: compositionContext._id,
|
|
62
|
+
matchedRoute: compositionContext.matchedRoute,
|
|
63
|
+
dynamicInputs: compositionContext.dynamicInputs
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
indexes,
|
|
68
|
+
event,
|
|
69
|
+
componentIds
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/placements/evaluateTest.ts
|
|
74
|
+
var evaluateTest = ({
|
|
75
|
+
test,
|
|
76
|
+
context,
|
|
77
|
+
compositionContext
|
|
78
|
+
}) => {
|
|
79
|
+
var _a, _b, _c;
|
|
80
|
+
const isTestDefined = Boolean((_a = context == null ? void 0 : context.manifest.data.project.test) == null ? void 0 : _a[test.name]);
|
|
81
|
+
if (!isTestDefined && process.env.NODE_ENV !== "production") {
|
|
82
|
+
console.warn(`Test "${test}" is not defined in Uniform manifest.`);
|
|
83
|
+
return {
|
|
84
|
+
index: null,
|
|
85
|
+
componentId: null,
|
|
86
|
+
event: null
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
let index = null;
|
|
90
|
+
let variantId = null;
|
|
91
|
+
let componentId = null;
|
|
92
|
+
if (context) {
|
|
93
|
+
const { result } = context.test(test);
|
|
94
|
+
if (result) {
|
|
95
|
+
index = result.index;
|
|
96
|
+
variantId = result.id;
|
|
97
|
+
componentId = result.componentId;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!variantId) {
|
|
101
|
+
const [first] = test.variations;
|
|
102
|
+
if (first) {
|
|
103
|
+
index = first.index;
|
|
104
|
+
variantId = first.id;
|
|
105
|
+
componentId = first.componentId;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const event = {
|
|
109
|
+
name: test.name,
|
|
110
|
+
control: (_c = (_b = test.variations.find((v) => v.id === variantId)) == null ? void 0 : _b.control) != null ? _c : false,
|
|
111
|
+
variantAssigned: true,
|
|
112
|
+
variantId: variantId != null ? variantId : "NO_VARIANTS",
|
|
113
|
+
compositionMetadata: {
|
|
114
|
+
compositionId: compositionContext._id,
|
|
115
|
+
matchedRoute: compositionContext.matchedRoute,
|
|
116
|
+
dynamicInputs: compositionContext.dynamicInputs
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
index,
|
|
121
|
+
componentId,
|
|
122
|
+
event
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/placements/extractPersonalizationName.ts
|
|
127
|
+
var extractPersonalizationName = ({
|
|
128
|
+
component
|
|
129
|
+
}) => {
|
|
130
|
+
var _a;
|
|
131
|
+
const trackingEventNameParameter = (_a = component.parameters) == null ? void 0 : _a.trackingEventName;
|
|
132
|
+
return (trackingEventNameParameter == null ? void 0 : trackingEventNameParameter.value) || "unknown";
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/placements/extractTestName.ts
|
|
136
|
+
var extractTestName = ({ component }) => {
|
|
137
|
+
var _a, _b;
|
|
138
|
+
const testName = (_a = component.parameters) == null ? void 0 : _a.test;
|
|
139
|
+
return (_b = testName == null ? void 0 : testName.value) != null ? _b : "unknown";
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/serialization.ts
|
|
143
|
+
var import_uuid = require("uuid");
|
|
144
|
+
var ENCODED_RESERVED_CHARACTERS = [
|
|
145
|
+
{
|
|
146
|
+
character: "/",
|
|
147
|
+
replacement: "_"
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
character: "+",
|
|
151
|
+
replacement: "-"
|
|
152
|
+
}
|
|
153
|
+
];
|
|
154
|
+
var isNotARegularUuid = (id) => {
|
|
155
|
+
return id.length !== 36;
|
|
156
|
+
};
|
|
157
|
+
var resolveComponentFromPageState = ({
|
|
158
|
+
pageState,
|
|
159
|
+
componentId: providedComponentId
|
|
160
|
+
}) => {
|
|
161
|
+
const componentId = isNotARegularUuid(providedComponentId) ? (0, import_uuid.v5)(providedComponentId, NAMESPACE_UUID) : providedComponentId;
|
|
162
|
+
let component = pageState.components[componentId];
|
|
163
|
+
if (!component) {
|
|
164
|
+
const componentIds = Object.keys(pageState.components);
|
|
165
|
+
for (let i = 0; i < componentIds.length; i++) {
|
|
166
|
+
const id = componentIds[i];
|
|
167
|
+
if (componentId.startsWith(id)) {
|
|
168
|
+
component = pageState.components[id];
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return component;
|
|
174
|
+
};
|
|
175
|
+
var getRuleId = (rule) => {
|
|
176
|
+
return (0, import_uuid.v5)(JSON.stringify(rule), NAMESPACE_UUID);
|
|
177
|
+
};
|
|
178
|
+
var resolveRuleFromPageState = ({
|
|
179
|
+
pageState,
|
|
180
|
+
rule
|
|
181
|
+
}) => {
|
|
182
|
+
if (!pageState.rules) {
|
|
183
|
+
return void 0;
|
|
184
|
+
}
|
|
185
|
+
const ruleId = getRuleId(rule);
|
|
186
|
+
let value = pageState.rules[ruleId];
|
|
187
|
+
if (typeof value === "undefined") {
|
|
188
|
+
const ruleIds = Object.keys(pageState.rules);
|
|
189
|
+
for (let i = 0; i < ruleIds.length; i++) {
|
|
190
|
+
const id = ruleIds[i];
|
|
191
|
+
if (ruleId.startsWith(id)) {
|
|
192
|
+
value = pageState.rules[id];
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return value;
|
|
198
|
+
};
|
|
199
|
+
var replaceReservedCharacters = (value) => {
|
|
200
|
+
return ENCODED_RESERVED_CHARACTERS.reduce((acc, char) => {
|
|
201
|
+
return acc.replaceAll(char.character, char.replacement);
|
|
202
|
+
}, value);
|
|
203
|
+
};
|
|
204
|
+
var unreplaceReservedCharacters = (value) => {
|
|
205
|
+
return ENCODED_RESERVED_CHARACTERS.reduce((acc, char) => {
|
|
206
|
+
return acc.replaceAll(char.replacement, char.character);
|
|
207
|
+
}, value);
|
|
208
|
+
};
|
|
209
|
+
var encodeWithReplacements = (value) => {
|
|
210
|
+
return replaceReservedCharacters(btoa(value));
|
|
211
|
+
};
|
|
212
|
+
var decodeWithReplacements = (value) => {
|
|
213
|
+
const urlDecoded = decodeURIComponent(value);
|
|
214
|
+
return atob(unreplaceReservedCharacters(urlDecoded));
|
|
215
|
+
};
|
|
216
|
+
var compressIds = (ids) => {
|
|
217
|
+
const uuidV4Length = 36;
|
|
218
|
+
let compressedRegistry;
|
|
219
|
+
for (let i = 0; i < uuidV4Length; i++) {
|
|
220
|
+
const registry = {};
|
|
221
|
+
let success = true;
|
|
222
|
+
for (let j = 0; j < ids.length; j++) {
|
|
223
|
+
const listId = ids[j];
|
|
224
|
+
const id = listId.slice(0, i + 1);
|
|
225
|
+
if (registry[id]) {
|
|
226
|
+
success = false;
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
registry[id] = listId;
|
|
230
|
+
}
|
|
231
|
+
if (success) {
|
|
232
|
+
compressedRegistry = registry;
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return compressedRegistry != null ? compressedRegistry : ids.reduce(
|
|
237
|
+
(acc, listId) => {
|
|
238
|
+
acc[listId] = listId;
|
|
239
|
+
return acc;
|
|
240
|
+
},
|
|
241
|
+
{}
|
|
242
|
+
);
|
|
243
|
+
};
|
|
244
|
+
var NAMESPACE_UUID = "00000000-0000-0000-0000-000000000000";
|
|
245
|
+
var serializeEvaluationResult = ({
|
|
246
|
+
payload,
|
|
247
|
+
encode = true
|
|
248
|
+
}) => {
|
|
249
|
+
const compressedPageState = {
|
|
250
|
+
cs: payload.compositionState,
|
|
251
|
+
r: payload.routePath,
|
|
252
|
+
c: {},
|
|
253
|
+
k: payload.keys,
|
|
254
|
+
ri: payload.releaseId,
|
|
255
|
+
dc: typeof payload.defaultConsent !== "undefined" ? payload.defaultConsent ? 1 : 0 : void 0,
|
|
256
|
+
pm: payload.previewMode === "editor" ? "e" : payload.previewMode === "preview" ? "p" : void 0,
|
|
257
|
+
rl: void 0
|
|
258
|
+
};
|
|
259
|
+
const componentKeys = Object.keys(payload.components);
|
|
260
|
+
const sortedKeys = componentKeys.sort();
|
|
261
|
+
const sortedComponents = sortedKeys.map((key) => ({
|
|
262
|
+
_id: isNotARegularUuid(key) ? (0, import_uuid.v5)(key, NAMESPACE_UUID) : key,
|
|
263
|
+
...payload.components[key]
|
|
264
|
+
}));
|
|
265
|
+
const compressedComponentIds = compressIds(sortedComponents.map((c) => c._id));
|
|
266
|
+
Object.keys(compressedComponentIds).forEach((compressedId) => {
|
|
267
|
+
var _a;
|
|
268
|
+
const originalId = compressedComponentIds[compressedId];
|
|
269
|
+
const component = sortedComponents.find((c) => c._id === originalId);
|
|
270
|
+
if (!component) {
|
|
271
|
+
throw new Error(`Component ${originalId} not found`);
|
|
272
|
+
}
|
|
273
|
+
compressedPageState.c[compressedId] = {
|
|
274
|
+
i: ((_a = component.indexes) == null ? void 0 : _a.length) ? component.indexes : void 0
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
if (typeof payload.rules !== "undefined") {
|
|
278
|
+
const ruleKeys = Object.keys(payload.rules);
|
|
279
|
+
if (ruleKeys.length !== 0) {
|
|
280
|
+
compressedPageState.rl = {};
|
|
281
|
+
const sortedRuleKeys = ruleKeys.sort();
|
|
282
|
+
const compressedRuleKeys = compressIds(sortedRuleKeys);
|
|
283
|
+
Object.keys(compressedRuleKeys).forEach((compressedId) => {
|
|
284
|
+
const originalId = compressedRuleKeys[compressedId];
|
|
285
|
+
compressedPageState.rl[compressedId] = payload.rules[originalId] ? 1 : 0;
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const result = JSON.stringify(compressedPageState);
|
|
290
|
+
return encode ? encodeWithReplacements(result) : result;
|
|
291
|
+
};
|
|
292
|
+
var deserializeEvaluationResult = ({
|
|
293
|
+
input: providedInput,
|
|
294
|
+
decode = true
|
|
295
|
+
}) => {
|
|
296
|
+
var _a, _b;
|
|
297
|
+
const input = decode ? decodeWithReplacements(providedInput) : providedInput;
|
|
298
|
+
const parsed = JSON.parse(input);
|
|
299
|
+
const pageState = {
|
|
300
|
+
compositionState: parsed.cs,
|
|
301
|
+
routePath: parsed.r,
|
|
302
|
+
components: {},
|
|
303
|
+
keys: (_a = parsed.k) != null ? _a : {},
|
|
304
|
+
releaseId: (_b = parsed.ri) != null ? _b : void 0,
|
|
305
|
+
defaultConsent: typeof parsed.dc !== "undefined" ? parsed.dc === 1 : void 0,
|
|
306
|
+
previewMode: parsed.pm === "e" ? "editor" : parsed.pm === "p" ? "preview" : void 0,
|
|
307
|
+
rules: parsed.rl ? Object.fromEntries(Object.entries(parsed.rl).map(([key, value]) => [key, value === 1])) : void 0
|
|
308
|
+
};
|
|
309
|
+
Object.keys(parsed.c).forEach((id) => {
|
|
310
|
+
const component = parsed.c[id];
|
|
311
|
+
pageState.components[id] = {
|
|
312
|
+
indexes: component.i
|
|
313
|
+
};
|
|
314
|
+
});
|
|
315
|
+
return pageState;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// src/index.ts
|
|
319
|
+
var UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME = "ufsc";
|
|
320
|
+
var UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME = "ufqc";
|
|
321
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
322
|
+
0 && (module.exports = {
|
|
323
|
+
UNIFORM_MIDDLEWARE_QUIRK_COOKIE_NAME,
|
|
324
|
+
UNIFORM_MIDDLEWARE_SCORE_COOKIE_NAME,
|
|
325
|
+
deserializeEvaluationResult,
|
|
326
|
+
evaluatePersonalization,
|
|
327
|
+
evaluateTest,
|
|
328
|
+
extractPersonalizationName,
|
|
329
|
+
extractTestName,
|
|
330
|
+
getRuleId,
|
|
331
|
+
resolveComponentFromPageState,
|
|
332
|
+
resolveRuleFromPageState,
|
|
333
|
+
serializeEvaluationResult
|
|
334
|
+
});
|