@lwrjs/static 0.13.0-alpha.6 → 0.13.0-alpha.8

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.
@@ -0,0 +1,267 @@
1
+ /**
2
+ * A decision tree is used to determine the best static site artifact metadata based on criteria such as specifier, version, isDebug, locale, and additional variants.
3
+ *
4
+ * This tree processes all the metadata for a specific type of artifact (bundle, asset, or resource), finding the best match based on the provided input.
5
+ *
6
+ * It operates on general decision tree principles, using tree nodes to navigate individual choices and identify the most suitable metadata.
7
+ *
8
+ * The tree is populated once by providing a set of possible matching conditions.
9
+ *
10
+ * tree.insert('bundle/foo|v/7_0|l/en', {metadata}, isDebug, {en-MX: [en-MX, en-US, en], en: [en], ...})
11
+ *
12
+ * The tree currently makes decisions in the following order: specifier, isDebug, version, and then locale.
13
+ *
14
+ * To find matching metadata, use the following commands:
15
+ *
16
+ * tree.find('bundle/foo|v/7_0|l/en-MX')
17
+ *
18
+ * tree.find('bundle/foo', isDebug, 'en-US')
19
+ */
20
+ import { logger } from '@lwrjs/diagnostics';
21
+ import { LOCALE_SIGIL, VERSION_NOT_PROVIDED, VERSION_SIGIL, normalizeVersionToUri, } from '@lwrjs/shared-utils';
22
+ import { parseSiteId } from '../site-metadata.js';
23
+ // Choice wildcard means I want to match anything
24
+ // Examples are any locale and match the default locale
25
+ const CHOICE_WILDCARD = '*';
26
+ // Choice empty is explicitly an empty choice
27
+ // This is useful for scenarios like an empty version can match any version
28
+ // This cannot be wildcard since we want an explicit version to also not match this choice.
29
+ const CHOICE_EMPTY = '';
30
+ const CHOICE_PROD = 'prod';
31
+ const CHOICE_DEBUG = 'debug';
32
+ // Tree of decisions to lead you to the right artifact
33
+ export default class DecisionTreeImpl {
34
+ constructor() {
35
+ this.root = new TreeNode(); // Root node does not hold any decision value
36
+ }
37
+ // Insert an artifact into the tree based on a path of decisions
38
+ insert(siteArtifactId, artifact, debug, localeFallbacks) {
39
+ // The decision path is the set of choices needed to get to the right metadata
40
+ // Currently this is hard coded to [specifier, isDebug, version, locale]
41
+ const decisionPath = this.createPossibleArtifactChoices({
42
+ id: siteArtifactId,
43
+ localeFallbacks,
44
+ debug,
45
+ });
46
+ // The set of choices in the root decision (specifier)
47
+ const choices = decisionPath[0];
48
+ // This would only be true if we ever had a decision tree with one choice (for now we expect this to always be false)
49
+ const isLeaf = decisionPath.length == 1;
50
+ // Set of valid choices in the root decision (specifier) only expected choice here is the exact specifier
51
+ for (const [index, key] of choices.entries()) {
52
+ // We will chose the ranked choice on every node along the decision to keep track of the preferred choice at each node
53
+ const rank = [index];
54
+ // If we have note made a node for the root choice (specifier) create one
55
+ if (!this.root.getChild(key, false)) {
56
+ this.root.addChild(key, new TreeNode(key, ''));
57
+ }
58
+ const nextNode = this.root.getChild(key, false);
59
+ // Not expected this would only be a leaf if there we no other decisions to be made
60
+ if (isLeaf) {
61
+ nextNode.setArtifact(artifact, rank);
62
+ }
63
+ else {
64
+ // If it's not a leaf, prepare for the next iteration
65
+ // We need to iterate over each choice separately to maintain distinct paths
66
+ this.deepInsert(1, decisionPath, key, nextNode, artifact, rank);
67
+ }
68
+ }
69
+ }
70
+ /**
71
+ * A method to handle deeper insertions, preserving the unique paths.
72
+ * This will be called for each node in the decision path.
73
+ */
74
+ deepInsert(level, decisionPath, currentPath, currentNode, artifact, rank) {
75
+ // No more choices for you
76
+ if (level >= decisionPath.length)
77
+ return;
78
+ // Get the set of choice for this node
79
+ const choices = decisionPath[level];
80
+ // Is this the last node in the decision path?
81
+ const isLeaf = level === decisionPath.length - 1;
82
+ for (const [index, key] of choices.entries()) {
83
+ // If this is a wild card mark it as WILD_CARD_RANK so we force it to be the last choice
84
+ const nextRank = [...rank, index];
85
+ if (!currentNode.getChild(key, false)) {
86
+ currentNode.addChild(key, new TreeNode(key, currentPath));
87
+ }
88
+ const nextNode = currentNode.getChild(key, false);
89
+ if (isLeaf) {
90
+ nextNode.setArtifact(artifact, nextRank);
91
+ }
92
+ else {
93
+ this.deepInsert(level + 1, decisionPath, `${currentPath}/${key}`, nextNode, artifact, nextRank);
94
+ }
95
+ }
96
+ }
97
+ // Retrieve an artifact from the tree based on a path of decisions
98
+ find(siteArtifactId, debug, localeId) {
99
+ const parsedArtifactId = parseSiteId(siteArtifactId);
100
+ const decisionPath = this.createArtifactChoices({
101
+ specifier: parsedArtifactId.specifier,
102
+ version: parsedArtifactId.variants[VERSION_SIGIL],
103
+ localeId: localeId ?? parsedArtifactId.variants[LOCALE_SIGIL],
104
+ debug,
105
+ });
106
+ let currentNode = this.root;
107
+ for (const key of decisionPath) {
108
+ const lastPath = currentNode.getPath();
109
+ currentNode = currentNode.getChild(key);
110
+ if (!currentNode) {
111
+ logger.debug(`Module ${key} not found at ${lastPath}`);
112
+ return undefined; // Decision path does not lead to an artifact
113
+ }
114
+ }
115
+ if (!currentNode.artifact) {
116
+ logger.debug(`Artifact not found at ${currentNode.getPath()}`);
117
+ }
118
+ return currentNode.artifact;
119
+ }
120
+ /**
121
+ * Create a decision tree patch to look up the most appropriate bundle
122
+ *
123
+ * @param specifier Bundle specifier
124
+ * @param version known version or will add the choice ''
125
+ * @param localeId preferred bundle locale or will add '' for default locale
126
+ * @param debug flag if debug bundle is preferred
127
+ */
128
+ createArtifactChoices({ specifier, version, localeId, debug }) {
129
+ const envChoice = debug ? CHOICE_DEBUG : CHOICE_PROD;
130
+ // Versions are stored in the bundle id in URL normalized form
131
+ const versionChoice = getVersionChoice(version);
132
+ const uriVersion = normalizeVersionToUri(versionChoice);
133
+ return [specifier, envChoice, uriVersion, localeId || CHOICE_WILDCARD];
134
+ }
135
+ createPossibleArtifactChoices({ id, localeFallbacks, debug, }) {
136
+ const match = parseSiteId(id);
137
+ const specifier = match.specifier;
138
+ if (!specifier) {
139
+ // TODO make diagnostic error
140
+ throw new Error(`Unable to parse${debug ? ' debug' : ''} static bundle specifier: ${id}`);
141
+ }
142
+ // Try to parse a version out of the specifier
143
+ const versionChoice = getVersionChoice(match.variants[VERSION_SIGIL]);
144
+ // To make it so that if you ask for a version it will only match an explicit version from the metadata.
145
+ // I think this will cause a breaking change?
146
+ // Un comment to all versioned requests to fall back to *
147
+ // const versions = [...new Set([versionChoice, CHOICE_WILDCARD])];
148
+ const versions = versionChoice === CHOICE_EMPTY
149
+ ? [...new Set([CHOICE_EMPTY, CHOICE_WILDCARD])]
150
+ : [...new Set([versionChoice, CHOICE_EMPTY])];
151
+ const envChoice = debug ? [CHOICE_DEBUG] : [CHOICE_PROD];
152
+ const localeChoice = match.variants[LOCALE_SIGIL];
153
+ const localeId = localeChoice && localeFallbacks ? localeFallbacks[localeChoice] : [CHOICE_WILDCARD];
154
+ return [[specifier], envChoice, versions, localeId];
155
+ }
156
+ }
157
+ /**
158
+ * This represents a single node on the decision path, corresponding to a specific value for a decision criterion (e.g., specifier, isDebug, version, locale).
159
+ * If it's a leaf node, it points to an artifact's metadata.
160
+ * It maintains the rank of all choices at this node, allowing a later node with a higher rank to replace the current one as the best option.
161
+ */
162
+ class TreeNode {
163
+ constructor(value = '', parentPath = '') {
164
+ this.children = new Map(); // Maps a decision key to the next TreeNode
165
+ this.artifact = undefined; // Final artifact at a leaf node
166
+ this.decisionValue = value;
167
+ this.parentPath = parentPath;
168
+ }
169
+ // Adds a child node based on a decision key
170
+ addChild(value, node) {
171
+ this.children.set(value, node);
172
+ }
173
+ // Sets the artifact at a leaf node
174
+ setArtifact(artifact, rank) {
175
+ if (this.artifact && isLowerOrEqualRank(rank, this.rank)) {
176
+ logger.debug({
177
+ label: 'DecisionTree',
178
+ message: `Ignored Artifact ${this.getPath()} ${this.rank} <= ${rank}`,
179
+ });
180
+ return;
181
+ }
182
+ logger.debug({
183
+ label: 'DecisionTree',
184
+ message: `Added artifact at ${this.getPath()}`,
185
+ });
186
+ this.rank = rank;
187
+ this.artifact = artifact;
188
+ }
189
+ // Retrieves a child node based on a decision key
190
+ getChild(key, allowWildcard = true) {
191
+ return allowWildcard
192
+ ? this.children.get(key) || this.children.get(CHOICE_WILDCARD)
193
+ : this.children.get(key);
194
+ }
195
+ getPath() {
196
+ return this.parentPath ? this.parentPath + '|' + this.decisionValue : this.decisionValue;
197
+ }
198
+ }
199
+ // If any choice was lower ranked choose this artifact
200
+ function isLowerOrEqualRank(contender, existing) {
201
+ // If existing path is undefined, we can consider the contender as lower ranked
202
+ // because there's nothing to compare against.
203
+ if (!existing) {
204
+ return true;
205
+ }
206
+ // Should not happen placed here to be sure
207
+ if (existing.length !== contender.length) {
208
+ throw new Error(`Paths must be of the same length ${existing} not found at ${contender}`);
209
+ }
210
+ // Iterate over each decision point to compare choices
211
+ for (let i = 0; i < existing.length; i++) {
212
+ // If the contender has made a choice with a higher index at any decision point,
213
+ // it means the contender is of a lower rank.
214
+ if (contender[i] > existing[i]) {
215
+ return true; // Contender is of a lower rank
216
+ }
217
+ else if (contender[i] < existing[i]) {
218
+ // If the contender has a choice with a lower index at any point, it's not of a lower rank.
219
+ return false;
220
+ }
221
+ // If the choices are the same, continue to the next decision point.
222
+ }
223
+ // If all choices are the same, the contender is equal rank.
224
+ return true;
225
+ }
226
+ /**
227
+ * Returns the version or if empty or undefined it will return a wild card and match
228
+ * an explicity un-versioned or the first match from the bundle metadata
229
+ */
230
+ function getVersionChoice(version) {
231
+ // If the version if empty or explicity version-not-provided
232
+ // return an empty choice to indicate that this an a value not provided
233
+ // so that it can match an explicit empty or wild card choice.
234
+ if (!version || version === VERSION_NOT_PROVIDED) {
235
+ return CHOICE_EMPTY;
236
+ }
237
+ // If there is a version use normalizeVersionToUri to convert it to a how it will be requested
238
+ return normalizeVersionToUri(version);
239
+ }
240
+ export function createFallbackMap(config) {
241
+ const map = {};
242
+ // Helper function to recursively find fallbacks
243
+ function findFallbacks(localeId, visited = new Set()) {
244
+ // Prevent cycles by checking if we've already visited this locale
245
+ if (visited.has(localeId) || localeId === config.defaultLocale) {
246
+ return [];
247
+ }
248
+ visited.add(localeId);
249
+ const locale = config.locales.find((l) => l.id === localeId);
250
+ if (!locale || !locale.fallback) {
251
+ return [localeId];
252
+ }
253
+ // Recursively find fallbacks, adding the current localeId to the start
254
+ return [localeId, ...findFallbacks(locale.fallback, visited)];
255
+ }
256
+ config.locales.forEach((locale) => {
257
+ // default will be wild carded
258
+ if (locale.id !== config.defaultLocale) {
259
+ // Initialize the fallbacks array for each locale, including the default as an implied fallback
260
+ map[locale.id] = [...new Set([...findFallbacks(locale.id)])];
261
+ }
262
+ });
263
+ // Setup a default locale under key '*'
264
+ map[CHOICE_WILDCARD] = [CHOICE_WILDCARD];
265
+ return map;
266
+ }
267
+ //# sourceMappingURL=decision-tree.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.13.0-alpha.6",
7
+ "version": "0.13.0-alpha.8",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -16,6 +16,10 @@
16
16
  },
17
17
  "type": "module",
18
18
  "exports": {
19
+ ".": {
20
+ "import": "./build/es/index.js",
21
+ "require": "./build/cjs/index.cjs"
22
+ },
19
23
  "./site-metadata": {
20
24
  "import": "./build/es/site-metadata.js",
21
25
  "require": "./build/cjs/site-metadata.cjs"
@@ -48,11 +52,11 @@
48
52
  "build/**/*.d.ts"
49
53
  ],
50
54
  "dependencies": {
51
- "@lwrjs/diagnostics": "0.13.0-alpha.6",
52
- "@lwrjs/shared-utils": "0.13.0-alpha.6"
55
+ "@lwrjs/diagnostics": "0.13.0-alpha.8",
56
+ "@lwrjs/shared-utils": "0.13.0-alpha.8"
53
57
  },
54
58
  "devDependencies": {
55
- "@lwrjs/types": "0.13.0-alpha.6",
59
+ "@lwrjs/types": "0.13.0-alpha.8",
56
60
  "@types/express": "^4.17.21",
57
61
  "jest": "^26.6.3",
58
62
  "jest-express": "^1.12.0",
@@ -65,5 +69,5 @@
65
69
  "volta": {
66
70
  "extends": "../../../package.json"
67
71
  },
68
- "gitHead": "88d503249ff6b135416208763fae8a33b5e874de"
72
+ "gitHead": "91e731eae1457bbbebeb274fa0ea5574705c51a2"
69
73
  }
@@ -1,12 +0,0 @@
1
- import type { I18NConfig, SiteBundle, SiteBundles } from '@lwrjs/types';
2
- /**
3
- * Return the version for a static module bundle.
4
- *
5
- * Version defined in the metadata > Requested Version > 'version-not-provided'
6
- */
7
- export declare function resolveStaticBundleVersion(metadataVersion?: string, requestedVersion?: string): string;
8
- /**
9
- * Get the most best match for a localized bundle
10
- */
11
- export declare function getLocalizedBundle(specifier: string, siteBundles: SiteBundles, initialLocaleId: string, i18n: I18NConfig): Promise<SiteBundle | undefined>;
12
- //# sourceMappingURL=static-utils.d.ts.map
@@ -1,23 +0,0 @@
1
- /**
2
- * Try to add any code that is mrt specific here so that we can isolate details that would have to be refactored to run lambdas
3
- * on a different platform
4
- */
5
- import { VERSION_NOT_PROVIDED, walkLocaleFallbacks } from '@lwrjs/shared-utils';
6
- /**
7
- * Return the version for a static module bundle.
8
- *
9
- * Version defined in the metadata > Requested Version > 'version-not-provided'
10
- */
11
- export function resolveStaticBundleVersion(metadataVersion, requestedVersion) {
12
- return metadataVersion || requestedVersion || VERSION_NOT_PROVIDED;
13
- }
14
- /**
15
- * Get the most best match for a localized bundle
16
- */
17
- export async function getLocalizedBundle(specifier, siteBundles, initialLocaleId, i18n) {
18
- return walkLocaleFallbacks(initialLocaleId, i18n, async (localeId) => {
19
- const localizedSpecifier = localeId === i18n.defaultLocale ? specifier : `${specifier}|l/${localeId}`;
20
- return siteBundles.bundles[localizedSpecifier];
21
- });
22
- }
23
- //# sourceMappingURL=static-utils.js.map