@patch-adams/core 1.5.15 → 1.5.17

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,6 +2203,69 @@ 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
  */
@@ -2233,6 +2296,19 @@ function generateLrsBridgeCode(options) {
2233
2296
  LRS.courseInfo.resourceType = docData.resourceType;
2234
2297
  }
2235
2298
 
2299
+ // Update packageName from API document name (matches Bravais Analytics object name)
2300
+ // Only set if not already baked in at wrap time
2301
+ if (docData.name && !LRS.courseInfo.packageName) {
2302
+ LRS.courseInfo.packageName = docData.name;
2303
+ log('Updated packageName from API:', docData.name);
2304
+ }
2305
+
2306
+ // Update documentId from API if we didn't have it
2307
+ if (docData.id && !LRS.courseInfo.documentId) {
2308
+ LRS.courseInfo.documentId = String(docData.id);
2309
+ log('Updated documentId from API:', docData.id);
2310
+ }
2311
+
2236
2312
  log('Course info updated from API:', LRS.courseInfo);
2237
2313
  }
2238
2314
 
@@ -2564,7 +2640,61 @@ function generateLrsBridgeCode(options) {
2564
2640
  info.versionId = paMeta.versionId || info.versionId;
2565
2641
  }
2566
2642
 
2567
- // 8. Build the course ID in Xyleme IRI format
2643
+ // 8. Extract Bravais document name from launch environment (runtime)
2644
+ // This is the name shown in Bravais Analytics and used for search.
2645
+ // Overrides packageName so the statement object name matches the Bravais document.
2646
+ if (!info.packageName) {
2647
+ var bravaisDocName = null;
2648
+
2649
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2650
+ try {
2651
+ var win = window;
2652
+ for (var i = 0; i < 10; i++) {
2653
+ try {
2654
+ if (win.PARAMS) {
2655
+ if (win.PARAMS.documentName && !bravaisDocName) {
2656
+ bravaisDocName = win.PARAMS.documentName;
2657
+ log('Bravais document name from PARAMS:', bravaisDocName);
2658
+ }
2659
+ if (win.PARAMS.did && !info.documentId) {
2660
+ info.documentId = String(win.PARAMS.did);
2661
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2662
+ }
2663
+ }
2664
+ } catch (e) {}
2665
+ if (win === win.parent) break;
2666
+ win = win.parent;
2667
+ }
2668
+ } catch (e) {}
2669
+
2670
+ // Strategy 2: Walk parent frames checking window.name
2671
+ // Bravais sets the iframe name attribute to the document name
2672
+ if (!bravaisDocName) {
2673
+ try {
2674
+ var win = window;
2675
+ for (var i = 0; i < 10; i++) {
2676
+ try {
2677
+ var frameName = win.name;
2678
+ // Document names have hyphens and are long; skip generic frame names
2679
+ if (frameName && frameName.length > 20 && frameName.indexOf('-') > -1 &&
2680
+ frameName.indexOf('scorm') === -1 && frameName.indexOf('API') === -1) {
2681
+ bravaisDocName = frameName;
2682
+ log('Bravais document name from frame name:', bravaisDocName);
2683
+ break;
2684
+ }
2685
+ } catch (e) {}
2686
+ if (win === win.parent) break;
2687
+ win = win.parent;
2688
+ }
2689
+ } catch (e) {}
2690
+ }
2691
+
2692
+ if (bravaisDocName) {
2693
+ info.packageName = bravaisDocName;
2694
+ }
2695
+ }
2696
+
2697
+ // 9. Build the course ID in Xyleme IRI format
2568
2698
  // Native format: http://xyleme.com/bravais/document/{guid}
2569
2699
  if (info.guid) {
2570
2700
  info.id = 'http://xyleme.com/bravais/document/' + info.guid;
@@ -5137,12 +5267,30 @@ function generateLrsBridgeCode(options) {
5137
5267
  tryLaunchEvents();
5138
5268
  }
5139
5269
 
5270
+ // After Bravais API metadata resolves, try our authenticated server as a
5271
+ // final fallback to get the document name for packageName.
5272
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
5273
+ function maybeEnrichAndReady() {
5274
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
5275
+ onDocReady();
5276
+ return;
5277
+ }
5278
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
5279
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
5280
+ if (name) {
5281
+ LRS.courseInfo.packageName = name;
5282
+ log('Set packageName from server lookup:', name);
5283
+ }
5284
+ onDocReady();
5285
+ });
5286
+ }
5287
+
5140
5288
  function fetchDocDataAndReady(docId) {
5141
5289
  fetchDocumentMetadata(docId, function(docData) {
5142
5290
  if (docData) {
5143
5291
  updateCourseInfoFromApi(docData);
5144
5292
  }
5145
- onDocReady();
5293
+ maybeEnrichAndReady();
5146
5294
  });
5147
5295
  }
5148
5296
 
@@ -5162,7 +5310,7 @@ function generateLrsBridgeCode(options) {
5162
5310
  } else {
5163
5311
  warn('Could not fetch document data from shared API - statements may fail aggregation');
5164
5312
  }
5165
- onDocReady();
5313
+ maybeEnrichAndReady();
5166
5314
  });
5167
5315
  } else if (documentId) {
5168
5316
  // No shared link token, try with extracted document ID (requires auth)
@@ -5171,11 +5319,11 @@ function generateLrsBridgeCode(options) {
5171
5319
  } else {
5172
5320
  // No identifiers available
5173
5321
  warn('No shared link token or document ID - statements may fail aggregation');
5174
- onDocReady();
5322
+ maybeEnrichAndReady();
5175
5323
  }
5176
5324
  } else {
5177
- // Already have GUID
5178
- onDocReady();
5325
+ // Already have GUID \u2014 still try to enrich packageName if missing
5326
+ maybeEnrichAndReady();
5179
5327
  }
5180
5328
 
5181
5329
  // Setup beforeunload to send terminated