@patch-adams/core 1.5.16 → 1.5.18

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/cli.js CHANGED
@@ -2203,23 +2203,96 @@ function generateLrsBridgeCode(options) {
2203
2203
  }
2204
2204
  }
2205
2205
 
2206
+ /**
2207
+ * Fetch document name from our authenticated server (cdsImporter proxy).
2208
+ * Derives server base URL from LRS_PROXY_ENDPOINT.
2209
+ * Falls back gracefully if CORS blocks or server is unavailable.
2210
+ * This is the final fallback when Bravais API and PARAMS are not available.
2211
+ */
2212
+ function fetchDocumentNameFromServer(documentId, callback) {
2213
+ if (!LRS_PROXY_ENDPOINT || !documentId) {
2214
+ callback(null);
2215
+ return;
2216
+ }
2217
+
2218
+ // Derive server base from LRS proxy endpoint
2219
+ // e.g., https://api.example.com/create/statement \u2192 https://api.example.com/create/
2220
+ var serverBase = LRS_PROXY_ENDPOINT.replace(/\\/statement\\/?$/, '/');
2221
+ if (!serverBase || serverBase === LRS_PROXY_ENDPOINT) {
2222
+ log('Could not derive server base URL from LRS_PROXY_ENDPOINT');
2223
+ callback(null);
2224
+ return;
2225
+ }
2226
+
2227
+ var apiUrl = serverBase + 'cdsImporter/api/documents/' + documentId;
2228
+ log('Fetching document name from server:', apiUrl);
2229
+
2230
+ var xhr = new XMLHttpRequest();
2231
+ xhr.open('GET', apiUrl, true);
2232
+ xhr.withCredentials = true;
2233
+ xhr.setRequestHeader('Accept', 'application/json');
2234
+ xhr.timeout = 5000;
2235
+
2236
+ xhr.onreadystatechange = function() {
2237
+ if (xhr.readyState === 4) {
2238
+ if (xhr.status >= 200 && xhr.status < 300) {
2239
+ try {
2240
+ var data = JSON.parse(xhr.responseText);
2241
+ if (data.name) {
2242
+ log('Document name from server:', data.name);
2243
+ callback(data.name);
2244
+ } else {
2245
+ callback(null);
2246
+ }
2247
+ } catch (e) {
2248
+ callback(null);
2249
+ }
2250
+ } else {
2251
+ log('Server document lookup returned:', xhr.status);
2252
+ callback(null);
2253
+ }
2254
+ }
2255
+ };
2256
+
2257
+ xhr.onerror = function() {
2258
+ log('Server document lookup network error (CORS or connectivity)');
2259
+ callback(null);
2260
+ };
2261
+ xhr.ontimeout = function() {
2262
+ log('Server document lookup timed out');
2263
+ callback(null);
2264
+ };
2265
+
2266
+ try { xhr.send(); } catch (e) { callback(null); }
2267
+ }
2268
+
2206
2269
  /**
2207
2270
  * Update course info with data from document API
2208
2271
  */
2209
2272
  function updateCourseInfoFromApi(docData) {
2210
2273
  if (!docData || !LRS.courseInfo) return;
2211
2274
 
2212
- // Document GUID
2213
- if (docData.guid && !LRS.courseInfo.guid) {
2275
+ // Document GUID \u2014 Bravais API GUID ALWAYS overrides baked-in GUID.
2276
+ // The baked-in GUID is a random UUID from wrap time. The Bravais GUID is the
2277
+ // canonical document identifier that Analytics uses to link statements to documents.
2278
+ // Without this override, our statements have a different object.id than cloudplayer
2279
+ // statements, so Analytics treats them as unrelated to the document.
2280
+ if (docData.guid) {
2281
+ if (LRS.courseInfo.guid && LRS.courseInfo.guid !== docData.guid) {
2282
+ log('Overriding baked-in GUID with Bravais GUID:', LRS.courseInfo.guid, '->', docData.guid);
2283
+ }
2214
2284
  LRS.courseInfo.guid = docData.guid;
2215
2285
  LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
2216
- log('Updated course guid from API:', docData.guid);
2286
+ log('Document GUID from API:', docData.guid);
2217
2287
  }
2218
2288
 
2219
- // Version GUID from latestVersion
2220
- if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
2289
+ // Version GUID \u2014 same principle: Bravais version GUID must override baked-in
2290
+ if (docData.latestVersion && docData.latestVersion.guid) {
2291
+ if (LRS.courseInfo.versionGuid && LRS.courseInfo.versionGuid !== docData.latestVersion.guid) {
2292
+ log('Overriding baked-in version GUID:', LRS.courseInfo.versionGuid, '->', docData.latestVersion.guid);
2293
+ }
2221
2294
  LRS.courseInfo.versionGuid = docData.latestVersion.guid;
2222
- log('Updated version guid from API:', docData.latestVersion.guid);
2295
+ log('Version GUID from API:', docData.latestVersion.guid);
2223
2296
  }
2224
2297
 
2225
2298
  // Other metadata
@@ -2233,6 +2306,19 @@ function generateLrsBridgeCode(options) {
2233
2306
  LRS.courseInfo.resourceType = docData.resourceType;
2234
2307
  }
2235
2308
 
2309
+ // Update packageName from API document name (matches Bravais Analytics object name)
2310
+ // Only set if not already baked in at wrap time
2311
+ if (docData.name && !LRS.courseInfo.packageName) {
2312
+ LRS.courseInfo.packageName = docData.name;
2313
+ log('Updated packageName from API:', docData.name);
2314
+ }
2315
+
2316
+ // Update documentId from API if we didn't have it
2317
+ if (docData.id && !LRS.courseInfo.documentId) {
2318
+ LRS.courseInfo.documentId = String(docData.id);
2319
+ log('Updated documentId from API:', docData.id);
2320
+ }
2321
+
2236
2322
  log('Course info updated from API:', LRS.courseInfo);
2237
2323
  }
2238
2324
 
@@ -2570,15 +2656,20 @@ function generateLrsBridgeCode(options) {
2570
2656
  if (!info.packageName) {
2571
2657
  var bravaisDocName = null;
2572
2658
 
2573
- // Strategy 1: Walk parent frames for PARAMS.documentName
2659
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2574
2660
  try {
2575
2661
  var win = window;
2576
2662
  for (var i = 0; i < 10; i++) {
2577
2663
  try {
2578
- if (win.PARAMS && win.PARAMS.documentName) {
2579
- bravaisDocName = win.PARAMS.documentName;
2580
- log('Bravais document name from PARAMS:', bravaisDocName);
2581
- break;
2664
+ if (win.PARAMS) {
2665
+ if (win.PARAMS.documentName && !bravaisDocName) {
2666
+ bravaisDocName = win.PARAMS.documentName;
2667
+ log('Bravais document name from PARAMS:', bravaisDocName);
2668
+ }
2669
+ if (win.PARAMS.did && !info.documentId) {
2670
+ info.documentId = String(win.PARAMS.did);
2671
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2672
+ }
2582
2673
  }
2583
2674
  } catch (e) {}
2584
2675
  if (win === win.parent) break;
@@ -5186,12 +5277,30 @@ function generateLrsBridgeCode(options) {
5186
5277
  tryLaunchEvents();
5187
5278
  }
5188
5279
 
5280
+ // After Bravais API metadata resolves, try our authenticated server as a
5281
+ // final fallback to get the document name for packageName.
5282
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
5283
+ function maybeEnrichAndReady() {
5284
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
5285
+ onDocReady();
5286
+ return;
5287
+ }
5288
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
5289
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
5290
+ if (name) {
5291
+ LRS.courseInfo.packageName = name;
5292
+ log('Set packageName from server lookup:', name);
5293
+ }
5294
+ onDocReady();
5295
+ });
5296
+ }
5297
+
5189
5298
  function fetchDocDataAndReady(docId) {
5190
5299
  fetchDocumentMetadata(docId, function(docData) {
5191
5300
  if (docData) {
5192
5301
  updateCourseInfoFromApi(docData);
5193
5302
  }
5194
- onDocReady();
5303
+ maybeEnrichAndReady();
5195
5304
  });
5196
5305
  }
5197
5306
 
@@ -5211,7 +5320,7 @@ function generateLrsBridgeCode(options) {
5211
5320
  } else {
5212
5321
  warn('Could not fetch document data from shared API - statements may fail aggregation');
5213
5322
  }
5214
- onDocReady();
5323
+ maybeEnrichAndReady();
5215
5324
  });
5216
5325
  } else if (documentId) {
5217
5326
  // No shared link token, try with extracted document ID (requires auth)
@@ -5220,11 +5329,11 @@ function generateLrsBridgeCode(options) {
5220
5329
  } else {
5221
5330
  // No identifiers available
5222
5331
  warn('No shared link token or document ID - statements may fail aggregation');
5223
- onDocReady();
5332
+ maybeEnrichAndReady();
5224
5333
  }
5225
5334
  } else {
5226
- // Already have GUID
5227
- onDocReady();
5335
+ // Already have GUID \u2014 still try to enrich packageName if missing
5336
+ maybeEnrichAndReady();
5228
5337
  }
5229
5338
 
5230
5339
  // Setup beforeunload to send terminated