@lwrjs/shared-utils 0.17.2-alpha.14 → 0.17.2-alpha.16

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
@@ -8,6 +8,9 @@ var __export = (target, all) => {
8
8
  // packages/@lwrjs/shared-utils/src/env.ts
9
9
  __markAsModule(exports);
10
10
  __export(exports, {
11
+ B3_PARENT_ID: () => B3_PARENT_ID,
12
+ B3_SAMPLED: () => B3_SAMPLED,
13
+ B3_SPAN_ID: () => B3_SPAN_ID,
11
14
  B3_TRACE_ID: () => B3_TRACE_ID,
12
15
  CORRELATION_ID: () => CORRELATION_ID,
13
16
  MRT_REQUEST_CLASS: () => MRT_REQUEST_CLASS,
@@ -16,6 +19,7 @@ __export(exports, {
16
19
  TRUE_CLIENT_IP: () => TRUE_CLIENT_IP,
17
20
  buildEnvironmentContext: () => buildEnvironmentContext,
18
21
  getFeatureFlags: () => getFeatureFlags,
22
+ getTraceHeaders: () => getTraceHeaders,
19
23
  isLambdaEnv: () => isLambdaEnv,
20
24
  isLocalDev: () => isLocalDev,
21
25
  parseRequestDepth: () => parseRequestDepth
@@ -31,6 +35,7 @@ function getFeatureFlags() {
31
35
  EXPERIMENTAL_UNVERSIONED_ALIASES: parseBooleanFlag("EXPERIMENTAL_UNVERSIONED_ALIASES"),
32
36
  LEGACY_LOADER: parseBooleanFlag("LEGACY_LOADER"),
33
37
  LWR_TRACING: parseTracingFlag(),
38
+ DISABLE_B3_TRACING: parseBooleanFlag("DISABLE_B3_TRACING"),
34
39
  MAX_VIEW_CACHE_TTL: parseStringFlag("MAX_VIEW_CACHE_TTL"),
35
40
  REEVALUATE_MODULES: parseBooleanFlag("REEVALUATE_MODULES"),
36
41
  SSR_COMPILER_ENABLED: parseBooleanFlag("SSR_COMPILER_ENABLED"),
@@ -69,8 +74,11 @@ function buildEnvironmentContext(runtimeParams) {
69
74
  };
70
75
  }
71
76
  var TRUE_CLIENT_IP = "true-client-ip";
72
- var B3_TRACE_ID = "x-b3-traceid";
73
77
  var CORRELATION_ID = "x-correlation-id";
78
+ var B3_TRACE_ID = "x-b3-traceid";
79
+ var B3_SPAN_ID = "x-b3-spandid";
80
+ var B3_PARENT_ID = "x-b3-parentid";
81
+ var B3_SAMPLED = "x-b3-sampled";
74
82
  var MRT_REQUEST_CLASS = "x-mobify-request-class";
75
83
  var ROUTE_CORE_HEADER = "x-sfdc-route-core";
76
84
  var REQUEST_DEPTH_HEADER = "x-sfdc-request-depth";
@@ -102,3 +110,21 @@ function parseRequestDepth(headers = {}, query = {}) {
102
110
  }
103
111
  return maxDepth;
104
112
  }
113
+ function getTraceHeaders(runtimeParams, span) {
114
+ const headers = {};
115
+ if (runtimeParams.trueClientIP)
116
+ headers[TRUE_CLIENT_IP] = runtimeParams.trueClientIP;
117
+ if (runtimeParams.correlationID)
118
+ headers[CORRELATION_ID] = runtimeParams.correlationID;
119
+ if (!getFeatureFlags().DISABLE_B3_TRACING) {
120
+ if (span?.traceId) {
121
+ headers[B3_TRACE_ID] = span.traceId;
122
+ headers[B3_SAMPLED] = parseTracingFlag() ? "1" : "0";
123
+ }
124
+ if (span?.spanId)
125
+ headers[B3_SPAN_ID] = span.spanId;
126
+ if (span?.parentSpanId)
127
+ headers[B3_PARENT_ID] = span.parentSpanId;
128
+ }
129
+ return headers;
130
+ }
@@ -24,11 +24,14 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/shared-utils/src/serialize.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
+ addHeadMarkup: () => addHeadMarkup,
28
+ createHeadMarkup: () => createHeadMarkup,
27
29
  replaceStringFromLocation: () => replaceStringFromLocation,
28
30
  serializeModuleToJson: () => serializeModuleToJson,
29
31
  shortestTtl: () => shortestTtl
30
32
  });
31
33
  var import_ms = __toModule(require("ms"));
34
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
32
35
  async function createJsonModule(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams) {
33
36
  const {
34
37
  ownHash,
@@ -84,3 +87,52 @@ function shortestTtl(newTtl, oldTtl, maxTtl) {
84
87
  }
85
88
  return shortest;
86
89
  }
90
+ function createMetaTags(meta) {
91
+ return meta.reduce((metaStr, {name, content, httpEquiv}) => {
92
+ if (!name && !content && !httpEquiv)
93
+ return metaStr;
94
+ const nameStr = name ? ` name="${name}"` : "", httpEquivStr = httpEquiv ? ` http-equiv="${httpEquiv}"` : "", contentStr = content ? ` content="${content}"` : "";
95
+ return metaStr + `<meta${nameStr}${httpEquivStr}${contentStr}>
96
+ `;
97
+ }, "");
98
+ }
99
+ function createScriptTags(scripts) {
100
+ return scripts.reduce((scriptStr, {body}) => scriptStr + `<script type="application/ld+json">${body}</script>
101
+ `, "");
102
+ }
103
+ function createLinkTags(links) {
104
+ return links.reduce((linkStr, {href, rel, as, fetchpriority}) => {
105
+ const relStr = rel ? ` rel="${rel}"` : "", asStr = as ? ` as="${as}"` : "", fetchStr = fetchpriority ? ` fetchpriority="${fetchpriority}"` : "";
106
+ return linkStr + `<link href="${href}"${relStr}${asStr}${fetchStr}>
107
+ `;
108
+ }, "");
109
+ }
110
+ function createStyleTags(styles) {
111
+ return styles.reduce((styleStr, {body, id}) => {
112
+ const idStr = id ? ` id="${id}"` : "";
113
+ return styleStr + `<style type="text/css"${idStr}>${body}</style>
114
+ `;
115
+ }, "");
116
+ }
117
+ function createHeadMarkup(markup) {
118
+ let hasTitle = false;
119
+ return markup.reduce((str, {title, scripts = [], meta = [], links = [], styles = []} = {}) => {
120
+ if (title && !hasTitle) {
121
+ hasTitle = true;
122
+ str += `<title>${title}</title>
123
+ `;
124
+ }
125
+ return str + createMetaTags(meta) + createScriptTags(scripts) + createLinkTags(links) + createStyleTags(styles);
126
+ }, "");
127
+ }
128
+ function addHeadMarkup(markup, stringBuilder) {
129
+ const headMarkup = createHeadMarkup(markup);
130
+ if (headMarkup) {
131
+ const headIndex = stringBuilder.original.indexOf("</head>");
132
+ if (headIndex >= 0) {
133
+ stringBuilder.prependLeft(headIndex, headMarkup);
134
+ } else {
135
+ import_diagnostics.logger.error("Adding head markup failed. Could not find the </head> tag.");
136
+ }
137
+ }
138
+ }
package/build/es/env.d.ts CHANGED
@@ -17,10 +17,18 @@ export declare function isLocalDev(): boolean;
17
17
  */
18
18
  export declare function buildEnvironmentContext(runtimeParams: RuntimeParams): EnvironmentContext;
19
19
  export declare const TRUE_CLIENT_IP = "true-client-ip";
20
- export declare const B3_TRACE_ID = "x-b3-traceid";
21
20
  export declare const CORRELATION_ID = "x-correlation-id";
21
+ export declare const B3_TRACE_ID = "x-b3-traceid";
22
+ export declare const B3_SPAN_ID = "x-b3-spandid";
23
+ export declare const B3_PARENT_ID = "x-b3-parentid";
24
+ export declare const B3_SAMPLED = "x-b3-sampled";
22
25
  export declare const MRT_REQUEST_CLASS = "x-mobify-request-class";
23
26
  export declare const ROUTE_CORE_HEADER = "x-sfdc-route-core";
24
27
  export declare const REQUEST_DEPTH_HEADER = "x-sfdc-request-depth";
25
28
  export declare function parseRequestDepth(headers?: Headers, query?: Record<string, string>): number;
29
+ export declare function getTraceHeaders(runtimeParams: RuntimeParams, span?: {
30
+ traceId: string;
31
+ spanId: string;
32
+ parentSpanId?: string;
33
+ }): HeadersInit;
26
34
  //# sourceMappingURL=env.d.ts.map
package/build/es/env.js CHANGED
@@ -14,6 +14,8 @@ export function getFeatureFlags() {
14
14
  LEGACY_LOADER: parseBooleanFlag('LEGACY_LOADER'),
15
15
  // Enable metrics log level 'off', 'default' or 'verbose'
16
16
  LWR_TRACING: parseTracingFlag(),
17
+ // Turn off distributed tracing (B3 headers, traceparent meta tag, MRT span exporting)
18
+ DISABLE_B3_TRACING: parseBooleanFlag('DISABLE_B3_TRACING'),
17
19
  // Max size of ViewDefinition time to live
18
20
  MAX_VIEW_CACHE_TTL: parseStringFlag('MAX_VIEW_CACHE_TTL'),
19
21
  // Forces SSR to re-evaluate modules for every page render. By default, modules are evaluated only once.
@@ -91,8 +93,11 @@ export function buildEnvironmentContext(runtimeParams) {
91
93
  }
92
94
  // headers used by LWR-Node and LWR@MRT
93
95
  export const TRUE_CLIENT_IP = 'true-client-ip'; // public IP of the original request source, eg: 13.123.12.3
94
- export const B3_TRACE_ID = 'x-b3-traceid'; // trace ID shared across service boundaries for a page request, eg: 463ac35c9f6413ad48485a3953bb6124
95
96
  export const CORRELATION_ID = 'x-correlation-id'; // shared correlation ID for the request
97
+ export const B3_TRACE_ID = 'x-b3-traceid'; // trace ID shared across service boundaries for a page request, set to inbound B3 header || lwr.handle.view trace ID
98
+ export const B3_SPAN_ID = 'x-b3-spandid'; // span ID for a trace
99
+ export const B3_PARENT_ID = 'x-b3-parentid'; // parent span ID for a trace
100
+ export const B3_SAMPLED = 'x-b3-sampled'; // "0" = tracing is off, "1" = tracing is on
96
101
  export const MRT_REQUEST_CLASS = 'x-mobify-request-class'; // contains the site base path, eg: basePath=/mysite
97
102
  export const ROUTE_CORE_HEADER = 'x-sfdc-route-core'; // helps route requests to Core efficiently, value: true
98
103
  export const REQUEST_DEPTH_HEADER = 'x-sfdc-request-depth'; // tracks the request depth to prevent cycles to the LWR server, eg: 2
@@ -126,4 +131,22 @@ export function parseRequestDepth(headers = {}, query = {}) {
126
131
  }
127
132
  return maxDepth;
128
133
  }
134
+ export function getTraceHeaders(runtimeParams, span) {
135
+ const headers = {};
136
+ if (runtimeParams.trueClientIP)
137
+ headers[TRUE_CLIENT_IP] = runtimeParams.trueClientIP;
138
+ if (runtimeParams.correlationID)
139
+ headers[CORRELATION_ID] = runtimeParams.correlationID;
140
+ if (!getFeatureFlags().DISABLE_B3_TRACING) {
141
+ if (span?.traceId) {
142
+ headers[B3_TRACE_ID] = span.traceId;
143
+ headers[B3_SAMPLED] = parseTracingFlag() ? '1' : '0';
144
+ }
145
+ if (span?.spanId)
146
+ headers[B3_SPAN_ID] = span.spanId;
147
+ if (span?.parentSpanId)
148
+ headers[B3_PARENT_ID] = span.parentSpanId;
149
+ }
150
+ return headers;
151
+ }
129
152
  //# sourceMappingURL=env.js.map
@@ -1,4 +1,4 @@
1
- import type { LinkedModuleDefinition, ModuleJsonDefinition, ModuleRegistry, RuntimeParams } from '@lwrjs/types';
1
+ import type { HeadMarkup, LinkedModuleDefinition, LwrStringBuilder, ModuleJsonDefinition, ModuleRegistry, RuntimeParams } from '@lwrjs/types';
2
2
  /**
3
3
  * Take a Module Definition and return its JSON serialization
4
4
  *
@@ -26,4 +26,16 @@ export declare function replaceStringFromLocation(src: string, { startOffset, en
26
26
  * @returns - the shorter of the two TTLs IN SECONDS, undefined if both TTLs are missing
27
27
  */
28
28
  export declare function shortestTtl(newTtl?: string | number, oldTtl?: string | number, maxTtl?: string | number): number | undefined;
29
+ /**
30
+ * Serialize head markup metadata into an HTML string
31
+ * @param markup An array of markup metadata objects
32
+ * @returns A string of HTML generated from markup metadata
33
+ */
34
+ export declare function createHeadMarkup(markup: (HeadMarkup | undefined)[]): string;
35
+ /**
36
+ * Serialize HeadMarkup config into HTML, then add it to the <head> of a base doc
37
+ * @param markup An array of markup metadata objects
38
+ * @param stringBuilder The string builder for a base document
39
+ */
40
+ export declare function addHeadMarkup(markup: (HeadMarkup | undefined)[], stringBuilder: LwrStringBuilder): void;
29
41
  //# sourceMappingURL=serialize.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import ms from 'ms';
2
+ import { logger } from '@lwrjs/diagnostics';
2
3
  // Given a Module Identifier, return a JSON entry
3
4
  async function createJsonModule(moduleId, moduleRegistry, runtimeEnvironment, runtimeParams) {
4
5
  const { ownHash, moduleEntry: { version }, } = await moduleRegistry.getModule(moduleId, runtimeParams);
@@ -67,4 +68,69 @@ export function shortestTtl(newTtl, oldTtl, maxTtl) {
67
68
  }
68
69
  return shortest;
69
70
  }
71
+ /** HEAD MARKUP UTILS */
72
+ function createMetaTags(meta) {
73
+ return meta.reduce((metaStr, { name, content, httpEquiv }) => {
74
+ if (!name && !content && !httpEquiv)
75
+ return metaStr; // do not create empty <meta> tags
76
+ const nameStr = name ? ` name="${name}"` : '', httpEquivStr = httpEquiv ? ` http-equiv="${httpEquiv}"` : '', contentStr = content ? ` content="${content}"` : '';
77
+ return metaStr + `<meta${nameStr}${httpEquivStr}${contentStr}>\n`;
78
+ }, '');
79
+ }
80
+ function createScriptTags(scripts) {
81
+ return scripts.reduce((scriptStr, { body }) => scriptStr + `<script type="application/ld+json">${body}</script>\n`, '');
82
+ }
83
+ function createLinkTags(links) {
84
+ return links.reduce((linkStr, { href, rel, as, fetchpriority }) => {
85
+ const relStr = rel ? ` rel="${rel}"` : '', asStr = as ? ` as="${as}"` : '', fetchStr = fetchpriority ? ` fetchpriority="${fetchpriority}"` : '';
86
+ return linkStr + `<link href="${href}"${relStr}${asStr}${fetchStr}>\n`;
87
+ }, '');
88
+ }
89
+ function createStyleTags(styles) {
90
+ return styles.reduce((styleStr, { body, id }) => {
91
+ const idStr = id ? ` id="${id}"` : '';
92
+ return styleStr + `<style type="text/css"${idStr}>${body}</style>\n`;
93
+ }, '');
94
+ }
95
+ /**
96
+ * Serialize head markup metadata into an HTML string
97
+ * @param markup An array of markup metadata objects
98
+ * @returns A string of HTML generated from markup metadata
99
+ */
100
+ export function createHeadMarkup(markup) {
101
+ // Loop through the <title>, <script>, <meta>, and <link> tag information
102
+ // Create an HTML string for each tag
103
+ let hasTitle = false;
104
+ return markup.reduce((str, { title, scripts = [], meta = [], links = [], styles = [] } = {}) => {
105
+ if (title && !hasTitle) {
106
+ // first <title> wins
107
+ hasTitle = true;
108
+ str += `<title>${title}</title>\n`;
109
+ }
110
+ return (str +
111
+ createMetaTags(meta) +
112
+ createScriptTags(scripts) +
113
+ createLinkTags(links) +
114
+ createStyleTags(styles));
115
+ }, '');
116
+ }
117
+ /**
118
+ * Serialize HeadMarkup config into HTML, then add it to the <head> of a base doc
119
+ * @param markup An array of markup metadata objects
120
+ * @param stringBuilder The string builder for a base document
121
+ */
122
+ export function addHeadMarkup(markup, stringBuilder) {
123
+ // Create HTML tags for each item in the SsrDataResponse.markup bag
124
+ const headMarkup = createHeadMarkup(markup);
125
+ if (headMarkup) {
126
+ // Add all the links to the <head> section of the base document
127
+ const headIndex = stringBuilder.original.indexOf('</head>');
128
+ if (headIndex >= 0) {
129
+ stringBuilder.prependLeft(headIndex, headMarkup);
130
+ }
131
+ else {
132
+ logger.error('Adding head markup failed. Could not find the </head> tag.');
133
+ }
134
+ }
135
+ }
70
136
  //# sourceMappingURL=serialize.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.17.2-alpha.14",
7
+ "version": "0.17.2-alpha.16",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -37,7 +37,7 @@
37
37
  "build/**/*.d.ts"
38
38
  ],
39
39
  "dependencies": {
40
- "@lwrjs/diagnostics": "0.17.2-alpha.14",
40
+ "@lwrjs/diagnostics": "0.17.2-alpha.16",
41
41
  "es-module-lexer": "^1.5.4",
42
42
  "fast-json-stable-stringify": "^2.1.0",
43
43
  "magic-string": "^0.30.9",
@@ -50,7 +50,7 @@
50
50
  "slugify": "^1.4.5"
51
51
  },
52
52
  "devDependencies": {
53
- "@lwrjs/types": "0.17.2-alpha.14",
53
+ "@lwrjs/types": "0.17.2-alpha.16",
54
54
  "@types/mime-types": "2.1.4",
55
55
  "@types/path-to-regexp": "^1.7.0",
56
56
  "memfs": "^4.13.0"
@@ -58,5 +58,5 @@
58
58
  "engines": {
59
59
  "node": ">=20.0.0"
60
60
  },
61
- "gitHead": "a7d6d67f5453787a8b2428bd11546c334af3f77b"
61
+ "gitHead": "5a6b24c265145652f9dda60d1d2cb405148c71f3"
62
62
  }