@lwrjs/shared-utils 0.13.0-alpha.9 → 0.13.1

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/build/cjs/env.cjs CHANGED
@@ -16,6 +16,9 @@ __export(exports, {
16
16
  isLocalDev: () => isLocalDev,
17
17
  parseRequestDepthHeader: () => parseRequestDepthHeader
18
18
  });
19
+ if (getFeatureFlags().REEVALUATE_MODULES && !getFeatureFlags().LEGACY_LOADER) {
20
+ throw new Error("REEVALUATE_MODULES is only supported with LEGACY_LOADER");
21
+ }
19
22
  function getFeatureFlags() {
20
23
  return {
21
24
  ASSETS_ON_LAMBDA: process.env.ASSETS_ON_LAMBDA !== void 0 && process.env.ASSETS_ON_LAMBDA.toLowerCase() === "true" ? true : false,
@@ -23,7 +26,11 @@ function getFeatureFlags() {
23
26
  SSR_STATIC_BUNDLES: process.env.SSR_STATIC_BUNDLES !== void 0 && process.env.SSR_STATIC_BUNDLES.toLowerCase() === "true" ? true : false,
24
27
  SSR_WITH_CSR_FALLBACK: process.env.SSR_WITH_CSR_FALLBACK !== void 0 && process.env.SSR_WITH_CSR_FALLBACK.toLowerCase() === "true" ? true : false,
25
28
  EXPERIMENTAL_UNVERSIONED_ALIASES: process.env.EXPERIMENTAL_UNVERSIONED_ALIASES !== void 0 && process.env.EXPERIMENTAL_UNVERSIONED_ALIASES.toLowerCase() === "true" ? true : false,
26
- LWR_TRACING: process.env.LWR_TRACING !== void 0 && process.env.LWR_TRACING.toLowerCase() !== "off" ? process.env.LWR_TRACING : false
29
+ LWR_TRACING: process.env.LWR_TRACING !== void 0 && process.env.LWR_TRACING.toLowerCase() !== "off" ? process.env.LWR_TRACING : false,
30
+ ENABLE_NONCE: process.env.ENABLE_NONCE !== void 0 && process.env.ENABLE_NONCE.toLowerCase() === "true" ? true : false,
31
+ SINGLE_RENDER_MODE: process.env.SINGLE_RENDER_MODE !== void 0 && process.env.SINGLE_RENDER_MODE.toLowerCase() === "true" ? true : false,
32
+ REEVALUATE_MODULES: process.env.REEVALUATE_MODULES !== void 0 && process.env.REEVALUATE_MODULES.toLowerCase() === "true" ? true : false,
33
+ MAX_VIEW_CACHE_TTL: process.env.MAX_VIEW_CACHE_TTL
27
34
  };
28
35
  }
29
36
  function isLambdaEnv() {
package/build/cjs/fs.cjs CHANGED
@@ -28,6 +28,7 @@ __export(exports, {
28
28
  PROTOCOL_HTTP: () => PROTOCOL_HTTP,
29
29
  PROTOCOL_HTTPS: () => PROTOCOL_HTTPS,
30
30
  canResolveView: () => canResolveView,
31
+ createIntegrityHash: () => createIntegrityHash,
31
32
  filterProtocolEntries: () => filterProtocolEntries,
32
33
  getViewSourceFromFile: () => getViewSourceFromFile,
33
34
  hashContent: () => hashContent,
@@ -60,6 +61,10 @@ var PROTOCOL_FILE = "file://";
60
61
  function hashContent(source) {
61
62
  return import_crypto.default.createHash("md5").update(source).digest("hex");
62
63
  }
64
+ function createIntegrityHash(source) {
65
+ const hash = import_crypto.default.createHash("sha256").update(source).digest("base64");
66
+ return `sha256-${hash}`;
67
+ }
63
68
  function readFile(filePath) {
64
69
  logMetrics(filePath);
65
70
  return import_fs_extra.default.readFileSync(filePath, "utf8");
@@ -27,7 +27,6 @@ __export(exports, {
27
27
  ASSETS_CACHE_DIR: () => ASSETS_CACHE_DIR,
28
28
  BUNDLE_SIGIL: () => BUNDLE_SIGIL,
29
29
  DEFAULT_LOCKER_TRUSTED_CMP: () => DEFAULT_LOCKER_TRUSTED_CMP,
30
- DEFAULT_LWR_BOOTSTRAP_CONFIG: () => DEFAULT_LWR_BOOTSTRAP_CONFIG,
31
30
  DEFAULT_LWR_LOCKER_CONFIG: () => DEFAULT_LWR_LOCKER_CONFIG,
32
31
  DEFAULT_TITLE: () => DEFAULT_TITLE,
33
32
  ENVIRONMENT_SIGIL: () => ENVIRONMENT_SIGIL,
@@ -40,6 +39,7 @@ __export(exports, {
40
39
  explodeSpecifier: () => explodeSpecifier,
41
40
  explodeSpecifiers: () => explodeSpecifiers,
42
41
  getCacheKeyFromJson: () => import_fast_json_stable_stringify.default,
42
+ getLocalDevOverrideUrl: () => getLocalDevOverrideUrl,
43
43
  getMappingUriPrefix: () => getMappingUriPrefix,
44
44
  getModuleUriPrefix: () => getModuleUriPrefix,
45
45
  getPropFromAttrName: () => getPropFromAttrName,
@@ -58,6 +58,7 @@ __export(exports, {
58
58
  slugify: () => slugify,
59
59
  stringToVariableName: () => stringToVariableName
60
60
  });
61
+ var import_path = __toModule(require("path"));
61
62
  var import_slugify = __toModule(require("slugify"));
62
63
  var import_fast_json_stable_stringify = __toModule(require("fast-json-stable-stringify"));
63
64
  var VERSION_SIGIL = "v";
@@ -75,18 +76,6 @@ var DEFAULT_LWR_LOCKER_CONFIG = {
75
76
  enabled: false,
76
77
  trustedComponents: DEFAULT_LOCKER_TRUSTED_CMP
77
78
  };
78
- var DEFAULT_LWR_BOOTSTRAP_CONFIG = {
79
- autoBoot: true,
80
- syntheticShadow: false,
81
- workers: {},
82
- services: [],
83
- configAsSrc: false,
84
- ssr: false,
85
- preloadData: false,
86
- mixedMode: false,
87
- module: void 0,
88
- preloadModules: []
89
- };
90
79
  function normalizeVersionToUri(version) {
91
80
  return version.replace(/\./g, "_");
92
81
  }
@@ -223,3 +212,10 @@ function stringToVariableName(inputString) {
223
212
  }
224
213
  return cleanedString;
225
214
  }
215
+ function getLocalDevOverrideUrl(cacheDir, specifier, filepath) {
216
+ if (!specifier.startsWith("@view"))
217
+ return filepath;
218
+ const dirname = import_path.default.basename(import_path.default.dirname(filepath));
219
+ const fileName = specifier.split("@view/")[1] + "_view.js";
220
+ return import_path.default.join(cacheDir, "overrides", dirname, fileName);
221
+ }
@@ -62,8 +62,8 @@ async function getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeP
62
62
  }
63
63
  async function toImportMetadata(moduleGraph, existingImportMetadata = {imports: {}, index: {}}, moduleRegistry, runtimeEnvironment, runtimeParams = {}) {
64
64
  const specifier = moduleGraph.graphs[0].specifier;
65
- const uri = moduleGraph.uriMap[specifier];
66
- const definition = moduleGraph.linkedDefinitions[specifier];
65
+ const uri = lookupValueIgnoringVersion(moduleGraph.uriMap, specifier);
66
+ const definition = lookupValueIgnoringVersion(moduleGraph.linkedDefinitions, specifier);
67
67
  if (!uri) {
68
68
  throw new Error("URI was not included in the graph: " + specifier);
69
69
  }
@@ -83,8 +83,8 @@ async function toImportMetadata(moduleGraph, existingImportMetadata = {imports:
83
83
  if (isExternalSpecifier(depSpecifier, moduleRegistry.getConfig().bundleConfig)) {
84
84
  continue;
85
85
  }
86
- const depUri = moduleGraph.uriMap[depSpecifier];
87
- const depDef = moduleGraph.linkedDefinitions[depSpecifier];
86
+ const depUri = lookupValueIgnoringVersion(moduleGraph.uriMap, depSpecifier);
87
+ const depDef = lookupValueIgnoringVersion(moduleGraph.linkedDefinitions, depSpecifier);
88
88
  const depMissing = !depUri || !depDef;
89
89
  if (depMissing && runtimeEnvironment.format !== "esm") {
90
90
  if (!depUri) {
@@ -106,6 +106,18 @@ async function toImportMetadata(moduleGraph, existingImportMetadata = {imports:
106
106
  }
107
107
  return importMetadata;
108
108
  }
109
+ function lookupValueIgnoringVersion(map, specifier) {
110
+ const val = map[specifier];
111
+ if (val) {
112
+ return val;
113
+ }
114
+ const cleanedKey = (0, import_identity.explodeSpecifier)(specifier).specifier;
115
+ for (const mapKey of Object.keys(map)) {
116
+ if ((0, import_identity.explodeSpecifier)(mapKey).specifier === cleanedKey) {
117
+ return map[mapKey];
118
+ }
119
+ }
120
+ }
109
121
  function mergeImportMetadata(existing, newMetadata) {
110
122
  return {
111
123
  imports: {
@@ -66,14 +66,21 @@ async function serializeModuleToJson(code = "", {
66
66
  function replaceStringFromLocation(src, {startOffset, endOffset}, replaceValue) {
67
67
  return src.substr(0, startOffset) + replaceValue + src.substr(endOffset, src.length);
68
68
  }
69
- function shortestTtl(newTtl, oldTtl) {
69
+ function shortestTtl(newTtl, oldTtl, maxTtl) {
70
70
  if (newTtl === void 0 && oldTtl === void 0)
71
71
  return void 0;
72
72
  const newSeconds = typeof newTtl === "string" ? (0, import_ms.default)(newTtl) / 1e3 : newTtl;
73
73
  const oldSeconds = typeof oldTtl === "string" ? (0, import_ms.default)(oldTtl) / 1e3 : oldTtl;
74
- if (newSeconds === void 0)
75
- return oldSeconds;
76
- if (oldSeconds === void 0)
77
- return newSeconds;
78
- return newSeconds < oldSeconds ? newSeconds : oldSeconds;
74
+ const maxSeconds = typeof maxTtl === "string" ? (0, import_ms.default)(maxTtl) / 1e3 : maxTtl;
75
+ let shortest = void 0;
76
+ if (newSeconds !== void 0 && (shortest === void 0 || newSeconds < shortest)) {
77
+ shortest = newSeconds;
78
+ }
79
+ if (oldSeconds !== void 0 && (shortest === void 0 || oldSeconds < shortest)) {
80
+ shortest = oldSeconds;
81
+ }
82
+ if (maxSeconds !== void 0 && (shortest === void 0 || maxSeconds < shortest)) {
83
+ shortest = maxSeconds;
84
+ }
85
+ return shortest;
79
86
  }
package/build/es/env.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare function isLambdaEnv(): boolean;
13
13
  */
14
14
  export declare function isLocalDev(): boolean;
15
15
  /**
16
- * Create a serializable context for the lwr/environment variable
16
+ * Create a serializable context for user-land exposed environment variables
17
17
  */
18
18
  export declare function buildEnvironmentContext(runtimeParams: RuntimeParams): EnvironmentContext;
19
19
  export declare const REQUEST_DEPTH_HEADER = "X-SFDC-Request-Depth";
package/build/es/env.js CHANGED
@@ -1,3 +1,6 @@
1
+ if (getFeatureFlags().REEVALUATE_MODULES && !getFeatureFlags().LEGACY_LOADER) {
2
+ throw new Error('REEVALUATE_MODULES is only supported with LEGACY_LOADER');
3
+ }
1
4
  export function getFeatureFlags() {
2
5
  // Add any new feature flags here to parse from environment variables
3
6
  return {
@@ -28,6 +31,20 @@ export function getFeatureFlags() {
28
31
  LWR_TRACING: process.env.LWR_TRACING !== undefined && process.env.LWR_TRACING.toLowerCase() !== 'off'
29
32
  ? process.env.LWR_TRACING
30
33
  : false,
34
+ ENABLE_NONCE: process.env.ENABLE_NONCE !== undefined && process.env.ENABLE_NONCE.toLowerCase() === 'true'
35
+ ? true
36
+ : false,
37
+ // Forces SSR rendering to render only one page at a time
38
+ SINGLE_RENDER_MODE: process.env.SINGLE_RENDER_MODE !== undefined &&
39
+ process.env.SINGLE_RENDER_MODE.toLowerCase() === 'true'
40
+ ? true
41
+ : false,
42
+ // Forces SSR to re-evaluate modules for every page render. By default, modules are evaluated only once.
43
+ REEVALUATE_MODULES: process.env.REEVALUATE_MODULES !== undefined &&
44
+ process.env.REEVALUATE_MODULES.toLowerCase() === 'true'
45
+ ? true
46
+ : false,
47
+ MAX_VIEW_CACHE_TTL: process.env.MAX_VIEW_CACHE_TTL,
31
48
  };
32
49
  }
33
50
  /**
@@ -48,7 +65,7 @@ export function isLocalDev() {
48
65
  return process.env.MRT_HMR === 'true';
49
66
  }
50
67
  /**
51
- * Create a serializable context for the lwr/environment variable
68
+ * Create a serializable context for user-land exposed environment variables
52
69
  */
53
70
  export function buildEnvironmentContext(runtimeParams) {
54
71
  // The baseBath from the config or set from the request (e.g. /shop)
package/build/es/fs.d.ts CHANGED
@@ -11,6 +11,10 @@ export declare const PROTOCOL_FILE = "file://";
11
11
  * @param source
12
12
  */
13
13
  export declare function hashContent(source: string | Buffer): string;
14
+ /**
15
+ * Create a Sub-resource Integrity Hash https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
16
+ */
17
+ export declare function createIntegrityHash(source: string | Buffer): string;
14
18
  /**
15
19
  * Read in the contents of the file path
16
20
  * @param filePath
package/build/es/fs.js CHANGED
@@ -20,6 +20,13 @@ export const PROTOCOL_FILE = 'file://';
20
20
  export function hashContent(source) {
21
21
  return crypto.createHash('md5').update(source).digest('hex');
22
22
  }
23
+ /**
24
+ * Create a Sub-resource Integrity Hash https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
25
+ */
26
+ export function createIntegrityHash(source) {
27
+ const hash = crypto.createHash('sha256').update(source).digest('base64');
28
+ return `sha256-${hash}`;
29
+ }
23
30
  /**
24
31
  * Read in the contents of the file path
25
32
  * @param filePath
@@ -1,5 +1,5 @@
1
1
  import getCacheKeyFromJson from 'fast-json-stable-stringify';
2
- import type { AbstractModuleId, AssetSource, BundleDefinition, ModuleDefinition, NormalizedLwrAppBootstrapConfig, PublicModuleRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
2
+ import type { AbstractModuleId, AssetSource, BundleDefinition, ModuleDefinition, PublicModuleRegistry, RuntimeEnvironment, RuntimeParams } from '@lwrjs/types';
3
3
  export declare const VERSION_SIGIL = "v";
4
4
  export declare const VERSION_PREFIX: string;
5
5
  export declare const LOCALE_SIGIL = "l";
@@ -15,7 +15,6 @@ export declare const DEFAULT_LWR_LOCKER_CONFIG: {
15
15
  enabled: boolean;
16
16
  trustedComponents: string[];
17
17
  };
18
- export declare const DEFAULT_LWR_BOOTSTRAP_CONFIG: NormalizedLwrAppBootstrapConfig;
19
18
  type ModuleIdentifierPartial = Partial<AbstractModuleId>;
20
19
  /**
21
20
  * Turn the dots in a version into underscores
@@ -148,4 +147,5 @@ export declare function isBundleDefinition(definition: ModuleDefinition | Bundle
148
147
  * @param name - Proposed name
149
148
  */
150
149
  export declare function stringToVariableName(inputString: string): string;
150
+ export declare function getLocalDevOverrideUrl(cacheDir: string, specifier: string, filepath: string): string;
151
151
  //# sourceMappingURL=identity.d.ts.map
@@ -1,3 +1,4 @@
1
+ import path from 'path';
1
2
  import slugifyText from 'slugify';
2
3
  import getCacheKeyFromJson from 'fast-json-stable-stringify';
3
4
  export const VERSION_SIGIL = 'v';
@@ -16,18 +17,6 @@ export const DEFAULT_LWR_LOCKER_CONFIG = {
16
17
  enabled: false,
17
18
  trustedComponents: DEFAULT_LOCKER_TRUSTED_CMP,
18
19
  };
19
- export const DEFAULT_LWR_BOOTSTRAP_CONFIG = {
20
- autoBoot: true,
21
- syntheticShadow: false,
22
- workers: {},
23
- services: [],
24
- configAsSrc: false,
25
- ssr: false,
26
- preloadData: false,
27
- mixedMode: false,
28
- module: undefined,
29
- preloadModules: [],
30
- };
31
20
  /**
32
21
  * Turn the dots in a version into underscores
33
22
  * @param version
@@ -281,4 +270,11 @@ export function stringToVariableName(inputString) {
281
270
  }
282
271
  return cleanedString;
283
272
  }
273
+ export function getLocalDevOverrideUrl(cacheDir, specifier, filepath) {
274
+ if (!specifier.startsWith('@view'))
275
+ return filepath; // only @view modules get overridden
276
+ const dirname = path.basename(path.dirname(filepath));
277
+ const fileName = specifier.split('@view/')[1] + '_view.js';
278
+ return path.join(cacheDir, 'overrides', dirname, fileName);
279
+ }
284
280
  //# sourceMappingURL=identity.js.map
@@ -57,8 +57,8 @@ export async function getImportMetadataMappings(moduleIds, runtimeEnvironment, r
57
57
  export async function toImportMetadata(moduleGraph, existingImportMetadata = { imports: {}, index: {} }, moduleRegistry, runtimeEnvironment, runtimeParams = {}) {
58
58
  // root module specifier
59
59
  const specifier = moduleGraph.graphs[0].specifier;
60
- const uri = moduleGraph.uriMap[specifier];
61
- const definition = moduleGraph.linkedDefinitions[specifier];
60
+ const uri = lookupValueIgnoringVersion(moduleGraph.uriMap, specifier);
61
+ const definition = lookupValueIgnoringVersion(moduleGraph.linkedDefinitions, specifier);
62
62
  if (!uri) {
63
63
  throw new Error('URI was not included in the graph: ' + specifier);
64
64
  }
@@ -86,8 +86,8 @@ export async function toImportMetadata(moduleGraph, existingImportMetadata = { i
86
86
  // Ignore Externals
87
87
  continue;
88
88
  }
89
- const depUri = moduleGraph.uriMap[depSpecifier];
90
- const depDef = moduleGraph.linkedDefinitions[depSpecifier];
89
+ const depUri = lookupValueIgnoringVersion(moduleGraph.uriMap, depSpecifier);
90
+ const depDef = lookupValueIgnoringVersion(moduleGraph.linkedDefinitions, depSpecifier);
91
91
  const depMissing = !depUri || !depDef;
92
92
  if (depMissing && runtimeEnvironment.format !== 'esm') {
93
93
  if (!depUri) {
@@ -112,6 +112,21 @@ export async function toImportMetadata(moduleGraph, existingImportMetadata = { i
112
112
  }
113
113
  return importMetadata;
114
114
  }
115
+ // Function to look up values ignoring the version part
116
+ function lookupValueIgnoringVersion(map, specifier) {
117
+ // early out on exact match
118
+ const val = map[specifier];
119
+ if (val) {
120
+ return val;
121
+ }
122
+ const cleanedKey = explodeSpecifier(specifier).specifier;
123
+ // Iterate over the map to find the first matching key without version
124
+ for (const mapKey of Object.keys(map)) {
125
+ if (explodeSpecifier(mapKey).specifier === cleanedKey) {
126
+ return map[mapKey];
127
+ }
128
+ }
129
+ }
115
130
  function mergeImportMetadata(existing, newMetadata) {
116
131
  // TODO should there be an error if the metadata conflicts?
117
132
  return {
@@ -25,5 +25,5 @@ export declare function replaceStringFromLocation(src: string, { startOffset, en
25
25
  * @param oldTtl - the current shortest TTL (if it exists)
26
26
  * @returns - the shorter of the two TTLs IN SECONDS, undefined if both TTLs are missing
27
27
  */
28
- export declare function shortestTtl(newTtl?: string | number, oldTtl?: string | number): number | undefined;
28
+ export declare function shortestTtl(newTtl?: string | number, oldTtl?: string | number, maxTtl?: string | number): number | undefined;
29
29
  //# sourceMappingURL=serialize.d.ts.map
@@ -49,15 +49,22 @@ export function replaceStringFromLocation(src, { startOffset, endOffset }, repla
49
49
  * @param oldTtl - the current shortest TTL (if it exists)
50
50
  * @returns - the shorter of the two TTLs IN SECONDS, undefined if both TTLs are missing
51
51
  */
52
- export function shortestTtl(newTtl, oldTtl) {
52
+ export function shortestTtl(newTtl, oldTtl, maxTtl) {
53
53
  if (newTtl === undefined && oldTtl === undefined)
54
54
  return undefined;
55
55
  const newSeconds = typeof newTtl === 'string' ? ms(newTtl) / 1000 : newTtl;
56
56
  const oldSeconds = typeof oldTtl === 'string' ? ms(oldTtl) / 1000 : oldTtl;
57
- if (newSeconds === undefined)
58
- return oldSeconds;
59
- if (oldSeconds === undefined)
60
- return newSeconds;
61
- return newSeconds < oldSeconds ? newSeconds : oldSeconds;
57
+ const maxSeconds = typeof maxTtl === 'string' ? ms(maxTtl) / 1000 : maxTtl;
58
+ let shortest = undefined;
59
+ if (newSeconds !== undefined && (shortest === undefined || newSeconds < shortest)) {
60
+ shortest = newSeconds;
61
+ }
62
+ if (oldSeconds !== undefined && (shortest === undefined || oldSeconds < shortest)) {
63
+ shortest = oldSeconds;
64
+ }
65
+ if (maxSeconds !== undefined && (shortest === undefined || maxSeconds < shortest)) {
66
+ shortest = maxSeconds;
67
+ }
68
+ return shortest;
62
69
  }
63
70
  //# sourceMappingURL=serialize.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.9",
7
+ "version": "0.13.1",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -37,25 +37,25 @@
37
37
  "build/**/*.d.ts"
38
38
  ],
39
39
  "dependencies": {
40
- "@lwrjs/diagnostics": "0.13.0-alpha.9",
41
- "es-module-lexer": "^1.5.2",
40
+ "@lwrjs/diagnostics": "0.13.1",
41
+ "es-module-lexer": "^1.5.4",
42
42
  "fast-json-stable-stringify": "^2.1.0",
43
43
  "magic-string": "^0.30.9",
44
44
  "mime-types": "^2.1.33",
45
45
  "ms": "^2.1.3",
46
46
  "parse5-sax-parser": "^6.0.1",
47
- "path-to-regexp": "^6.2.0",
47
+ "path-to-regexp": "^6.2.2",
48
48
  "resolve": "^1.22.8",
49
49
  "rollup": "^2.78.0",
50
50
  "slugify": "^1.4.5"
51
51
  },
52
52
  "devDependencies": {
53
- "@lwrjs/types": "0.13.0-alpha.9",
53
+ "@lwrjs/types": "0.13.1",
54
54
  "@types/mime-types": "2.1.4",
55
55
  "@types/path-to-regexp": "^1.7.0"
56
56
  },
57
57
  "engines": {
58
58
  "node": ">=18.0.0"
59
59
  },
60
- "gitHead": "a2a9e1dbf39a7b04c7a43433e004895b6f4c51a3"
60
+ "gitHead": "548b27ed22f5bfba7031d6b356d64898e15bd609"
61
61
  }