@patch-adams/core 1.5.16 → 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
 
@@ -2570,15 +2646,20 @@ function generateLrsBridgeCode(options) {
2570
2646
  if (!info.packageName) {
2571
2647
  var bravaisDocName = null;
2572
2648
 
2573
- // Strategy 1: Walk parent frames for PARAMS.documentName
2649
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2574
2650
  try {
2575
2651
  var win = window;
2576
2652
  for (var i = 0; i < 10; i++) {
2577
2653
  try {
2578
- if (win.PARAMS && win.PARAMS.documentName) {
2579
- bravaisDocName = win.PARAMS.documentName;
2580
- log('Bravais document name from PARAMS:', bravaisDocName);
2581
- break;
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
+ }
2582
2663
  }
2583
2664
  } catch (e) {}
2584
2665
  if (win === win.parent) break;
@@ -5186,12 +5267,30 @@ function generateLrsBridgeCode(options) {
5186
5267
  tryLaunchEvents();
5187
5268
  }
5188
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
+
5189
5288
  function fetchDocDataAndReady(docId) {
5190
5289
  fetchDocumentMetadata(docId, function(docData) {
5191
5290
  if (docData) {
5192
5291
  updateCourseInfoFromApi(docData);
5193
5292
  }
5194
- onDocReady();
5293
+ maybeEnrichAndReady();
5195
5294
  });
5196
5295
  }
5197
5296
 
@@ -5211,7 +5310,7 @@ function generateLrsBridgeCode(options) {
5211
5310
  } else {
5212
5311
  warn('Could not fetch document data from shared API - statements may fail aggregation');
5213
5312
  }
5214
- onDocReady();
5313
+ maybeEnrichAndReady();
5215
5314
  });
5216
5315
  } else if (documentId) {
5217
5316
  // No shared link token, try with extracted document ID (requires auth)
@@ -5220,11 +5319,11 @@ function generateLrsBridgeCode(options) {
5220
5319
  } else {
5221
5320
  // No identifiers available
5222
5321
  warn('No shared link token or document ID - statements may fail aggregation');
5223
- onDocReady();
5322
+ maybeEnrichAndReady();
5224
5323
  }
5225
5324
  } else {
5226
- // Already have GUID
5227
- onDocReady();
5325
+ // Already have GUID \u2014 still try to enrich packageName if missing
5326
+ maybeEnrichAndReady();
5228
5327
  }
5229
5328
 
5230
5329
  // Setup beforeunload to send terminated