@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.cjs CHANGED
@@ -2213,23 +2213,96 @@ function generateLrsBridgeCode(options) {
2213
2213
  }
2214
2214
  }
2215
2215
 
2216
+ /**
2217
+ * Fetch document name from our authenticated server (cdsImporter proxy).
2218
+ * Derives server base URL from LRS_PROXY_ENDPOINT.
2219
+ * Falls back gracefully if CORS blocks or server is unavailable.
2220
+ * This is the final fallback when Bravais API and PARAMS are not available.
2221
+ */
2222
+ function fetchDocumentNameFromServer(documentId, callback) {
2223
+ if (!LRS_PROXY_ENDPOINT || !documentId) {
2224
+ callback(null);
2225
+ return;
2226
+ }
2227
+
2228
+ // Derive server base from LRS proxy endpoint
2229
+ // e.g., https://api.example.com/create/statement \u2192 https://api.example.com/create/
2230
+ var serverBase = LRS_PROXY_ENDPOINT.replace(/\\/statement\\/?$/, '/');
2231
+ if (!serverBase || serverBase === LRS_PROXY_ENDPOINT) {
2232
+ log('Could not derive server base URL from LRS_PROXY_ENDPOINT');
2233
+ callback(null);
2234
+ return;
2235
+ }
2236
+
2237
+ var apiUrl = serverBase + 'cdsImporter/api/documents/' + documentId;
2238
+ log('Fetching document name from server:', apiUrl);
2239
+
2240
+ var xhr = new XMLHttpRequest();
2241
+ xhr.open('GET', apiUrl, true);
2242
+ xhr.withCredentials = true;
2243
+ xhr.setRequestHeader('Accept', 'application/json');
2244
+ xhr.timeout = 5000;
2245
+
2246
+ xhr.onreadystatechange = function() {
2247
+ if (xhr.readyState === 4) {
2248
+ if (xhr.status >= 200 && xhr.status < 300) {
2249
+ try {
2250
+ var data = JSON.parse(xhr.responseText);
2251
+ if (data.name) {
2252
+ log('Document name from server:', data.name);
2253
+ callback(data.name);
2254
+ } else {
2255
+ callback(null);
2256
+ }
2257
+ } catch (e) {
2258
+ callback(null);
2259
+ }
2260
+ } else {
2261
+ log('Server document lookup returned:', xhr.status);
2262
+ callback(null);
2263
+ }
2264
+ }
2265
+ };
2266
+
2267
+ xhr.onerror = function() {
2268
+ log('Server document lookup network error (CORS or connectivity)');
2269
+ callback(null);
2270
+ };
2271
+ xhr.ontimeout = function() {
2272
+ log('Server document lookup timed out');
2273
+ callback(null);
2274
+ };
2275
+
2276
+ try { xhr.send(); } catch (e) { callback(null); }
2277
+ }
2278
+
2216
2279
  /**
2217
2280
  * Update course info with data from document API
2218
2281
  */
2219
2282
  function updateCourseInfoFromApi(docData) {
2220
2283
  if (!docData || !LRS.courseInfo) return;
2221
2284
 
2222
- // Document GUID
2223
- if (docData.guid && !LRS.courseInfo.guid) {
2285
+ // Document GUID \u2014 Bravais API GUID ALWAYS overrides baked-in GUID.
2286
+ // The baked-in GUID is a random UUID from wrap time. The Bravais GUID is the
2287
+ // canonical document identifier that Analytics uses to link statements to documents.
2288
+ // Without this override, our statements have a different object.id than cloudplayer
2289
+ // statements, so Analytics treats them as unrelated to the document.
2290
+ if (docData.guid) {
2291
+ if (LRS.courseInfo.guid && LRS.courseInfo.guid !== docData.guid) {
2292
+ log('Overriding baked-in GUID with Bravais GUID:', LRS.courseInfo.guid, '->', docData.guid);
2293
+ }
2224
2294
  LRS.courseInfo.guid = docData.guid;
2225
2295
  LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
2226
- log('Updated course guid from API:', docData.guid);
2296
+ log('Document GUID from API:', docData.guid);
2227
2297
  }
2228
2298
 
2229
- // Version GUID from latestVersion
2230
- if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
2299
+ // Version GUID \u2014 same principle: Bravais version GUID must override baked-in
2300
+ if (docData.latestVersion && docData.latestVersion.guid) {
2301
+ if (LRS.courseInfo.versionGuid && LRS.courseInfo.versionGuid !== docData.latestVersion.guid) {
2302
+ log('Overriding baked-in version GUID:', LRS.courseInfo.versionGuid, '->', docData.latestVersion.guid);
2303
+ }
2231
2304
  LRS.courseInfo.versionGuid = docData.latestVersion.guid;
2232
- log('Updated version guid from API:', docData.latestVersion.guid);
2305
+ log('Version GUID from API:', docData.latestVersion.guid);
2233
2306
  }
2234
2307
 
2235
2308
  // Other metadata
@@ -2243,6 +2316,19 @@ function generateLrsBridgeCode(options) {
2243
2316
  LRS.courseInfo.resourceType = docData.resourceType;
2244
2317
  }
2245
2318
 
2319
+ // Update packageName from API document name (matches Bravais Analytics object name)
2320
+ // Only set if not already baked in at wrap time
2321
+ if (docData.name && !LRS.courseInfo.packageName) {
2322
+ LRS.courseInfo.packageName = docData.name;
2323
+ log('Updated packageName from API:', docData.name);
2324
+ }
2325
+
2326
+ // Update documentId from API if we didn't have it
2327
+ if (docData.id && !LRS.courseInfo.documentId) {
2328
+ LRS.courseInfo.documentId = String(docData.id);
2329
+ log('Updated documentId from API:', docData.id);
2330
+ }
2331
+
2246
2332
  log('Course info updated from API:', LRS.courseInfo);
2247
2333
  }
2248
2334
 
@@ -2580,15 +2666,20 @@ function generateLrsBridgeCode(options) {
2580
2666
  if (!info.packageName) {
2581
2667
  var bravaisDocName = null;
2582
2668
 
2583
- // Strategy 1: Walk parent frames for PARAMS.documentName
2669
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2584
2670
  try {
2585
2671
  var win = window;
2586
2672
  for (var i = 0; i < 10; i++) {
2587
2673
  try {
2588
- if (win.PARAMS && win.PARAMS.documentName) {
2589
- bravaisDocName = win.PARAMS.documentName;
2590
- log('Bravais document name from PARAMS:', bravaisDocName);
2591
- break;
2674
+ if (win.PARAMS) {
2675
+ if (win.PARAMS.documentName && !bravaisDocName) {
2676
+ bravaisDocName = win.PARAMS.documentName;
2677
+ log('Bravais document name from PARAMS:', bravaisDocName);
2678
+ }
2679
+ if (win.PARAMS.did && !info.documentId) {
2680
+ info.documentId = String(win.PARAMS.did);
2681
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2682
+ }
2592
2683
  }
2593
2684
  } catch (e) {}
2594
2685
  if (win === win.parent) break;
@@ -5196,12 +5287,30 @@ function generateLrsBridgeCode(options) {
5196
5287
  tryLaunchEvents();
5197
5288
  }
5198
5289
 
5290
+ // After Bravais API metadata resolves, try our authenticated server as a
5291
+ // final fallback to get the document name for packageName.
5292
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
5293
+ function maybeEnrichAndReady() {
5294
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
5295
+ onDocReady();
5296
+ return;
5297
+ }
5298
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
5299
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
5300
+ if (name) {
5301
+ LRS.courseInfo.packageName = name;
5302
+ log('Set packageName from server lookup:', name);
5303
+ }
5304
+ onDocReady();
5305
+ });
5306
+ }
5307
+
5199
5308
  function fetchDocDataAndReady(docId) {
5200
5309
  fetchDocumentMetadata(docId, function(docData) {
5201
5310
  if (docData) {
5202
5311
  updateCourseInfoFromApi(docData);
5203
5312
  }
5204
- onDocReady();
5313
+ maybeEnrichAndReady();
5205
5314
  });
5206
5315
  }
5207
5316
 
@@ -5221,7 +5330,7 @@ function generateLrsBridgeCode(options) {
5221
5330
  } else {
5222
5331
  warn('Could not fetch document data from shared API - statements may fail aggregation');
5223
5332
  }
5224
- onDocReady();
5333
+ maybeEnrichAndReady();
5225
5334
  });
5226
5335
  } else if (documentId) {
5227
5336
  // No shared link token, try with extracted document ID (requires auth)
@@ -5230,11 +5339,11 @@ function generateLrsBridgeCode(options) {
5230
5339
  } else {
5231
5340
  // No identifiers available
5232
5341
  warn('No shared link token or document ID - statements may fail aggregation');
5233
- onDocReady();
5342
+ maybeEnrichAndReady();
5234
5343
  }
5235
5344
  } else {
5236
- // Already have GUID
5237
- onDocReady();
5345
+ // Already have GUID \u2014 still try to enrich packageName if missing
5346
+ maybeEnrichAndReady();
5238
5347
  }
5239
5348
 
5240
5349
  // Setup beforeunload to send terminated