@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/index.cjs CHANGED
@@ -1881,6 +1881,69 @@ function generateLrsBridgeCode(options) {
1881
1881
  }
1882
1882
  }
1883
1883
 
1884
+ /**
1885
+ * Fetch document name from our authenticated server (cdsImporter proxy).
1886
+ * Derives server base URL from LRS_PROXY_ENDPOINT.
1887
+ * Falls back gracefully if CORS blocks or server is unavailable.
1888
+ * This is the final fallback when Bravais API and PARAMS are not available.
1889
+ */
1890
+ function fetchDocumentNameFromServer(documentId, callback) {
1891
+ if (!LRS_PROXY_ENDPOINT || !documentId) {
1892
+ callback(null);
1893
+ return;
1894
+ }
1895
+
1896
+ // Derive server base from LRS proxy endpoint
1897
+ // e.g., https://api.example.com/create/statement \u2192 https://api.example.com/create/
1898
+ var serverBase = LRS_PROXY_ENDPOINT.replace(/\\/statement\\/?$/, '/');
1899
+ if (!serverBase || serverBase === LRS_PROXY_ENDPOINT) {
1900
+ log('Could not derive server base URL from LRS_PROXY_ENDPOINT');
1901
+ callback(null);
1902
+ return;
1903
+ }
1904
+
1905
+ var apiUrl = serverBase + 'cdsImporter/api/documents/' + documentId;
1906
+ log('Fetching document name from server:', apiUrl);
1907
+
1908
+ var xhr = new XMLHttpRequest();
1909
+ xhr.open('GET', apiUrl, true);
1910
+ xhr.withCredentials = true;
1911
+ xhr.setRequestHeader('Accept', 'application/json');
1912
+ xhr.timeout = 5000;
1913
+
1914
+ xhr.onreadystatechange = function() {
1915
+ if (xhr.readyState === 4) {
1916
+ if (xhr.status >= 200 && xhr.status < 300) {
1917
+ try {
1918
+ var data = JSON.parse(xhr.responseText);
1919
+ if (data.name) {
1920
+ log('Document name from server:', data.name);
1921
+ callback(data.name);
1922
+ } else {
1923
+ callback(null);
1924
+ }
1925
+ } catch (e) {
1926
+ callback(null);
1927
+ }
1928
+ } else {
1929
+ log('Server document lookup returned:', xhr.status);
1930
+ callback(null);
1931
+ }
1932
+ }
1933
+ };
1934
+
1935
+ xhr.onerror = function() {
1936
+ log('Server document lookup network error (CORS or connectivity)');
1937
+ callback(null);
1938
+ };
1939
+ xhr.ontimeout = function() {
1940
+ log('Server document lookup timed out');
1941
+ callback(null);
1942
+ };
1943
+
1944
+ try { xhr.send(); } catch (e) { callback(null); }
1945
+ }
1946
+
1884
1947
  /**
1885
1948
  * Update course info with data from document API
1886
1949
  */
@@ -1911,6 +1974,19 @@ function generateLrsBridgeCode(options) {
1911
1974
  LRS.courseInfo.resourceType = docData.resourceType;
1912
1975
  }
1913
1976
 
1977
+ // Update packageName from API document name (matches Bravais Analytics object name)
1978
+ // Only set if not already baked in at wrap time
1979
+ if (docData.name && !LRS.courseInfo.packageName) {
1980
+ LRS.courseInfo.packageName = docData.name;
1981
+ log('Updated packageName from API:', docData.name);
1982
+ }
1983
+
1984
+ // Update documentId from API if we didn't have it
1985
+ if (docData.id && !LRS.courseInfo.documentId) {
1986
+ LRS.courseInfo.documentId = String(docData.id);
1987
+ log('Updated documentId from API:', docData.id);
1988
+ }
1989
+
1914
1990
  log('Course info updated from API:', LRS.courseInfo);
1915
1991
  }
1916
1992
 
@@ -2242,7 +2318,61 @@ function generateLrsBridgeCode(options) {
2242
2318
  info.versionId = paMeta.versionId || info.versionId;
2243
2319
  }
2244
2320
 
2245
- // 8. Build the course ID in Xyleme IRI format
2321
+ // 8. Extract Bravais document name from launch environment (runtime)
2322
+ // This is the name shown in Bravais Analytics and used for search.
2323
+ // Overrides packageName so the statement object name matches the Bravais document.
2324
+ if (!info.packageName) {
2325
+ var bravaisDocName = null;
2326
+
2327
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2328
+ try {
2329
+ var win = window;
2330
+ for (var i = 0; i < 10; i++) {
2331
+ try {
2332
+ if (win.PARAMS) {
2333
+ if (win.PARAMS.documentName && !bravaisDocName) {
2334
+ bravaisDocName = win.PARAMS.documentName;
2335
+ log('Bravais document name from PARAMS:', bravaisDocName);
2336
+ }
2337
+ if (win.PARAMS.did && !info.documentId) {
2338
+ info.documentId = String(win.PARAMS.did);
2339
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2340
+ }
2341
+ }
2342
+ } catch (e) {}
2343
+ if (win === win.parent) break;
2344
+ win = win.parent;
2345
+ }
2346
+ } catch (e) {}
2347
+
2348
+ // Strategy 2: Walk parent frames checking window.name
2349
+ // Bravais sets the iframe name attribute to the document name
2350
+ if (!bravaisDocName) {
2351
+ try {
2352
+ var win = window;
2353
+ for (var i = 0; i < 10; i++) {
2354
+ try {
2355
+ var frameName = win.name;
2356
+ // Document names have hyphens and are long; skip generic frame names
2357
+ if (frameName && frameName.length > 20 && frameName.indexOf('-') > -1 &&
2358
+ frameName.indexOf('scorm') === -1 && frameName.indexOf('API') === -1) {
2359
+ bravaisDocName = frameName;
2360
+ log('Bravais document name from frame name:', bravaisDocName);
2361
+ break;
2362
+ }
2363
+ } catch (e) {}
2364
+ if (win === win.parent) break;
2365
+ win = win.parent;
2366
+ }
2367
+ } catch (e) {}
2368
+ }
2369
+
2370
+ if (bravaisDocName) {
2371
+ info.packageName = bravaisDocName;
2372
+ }
2373
+ }
2374
+
2375
+ // 9. Build the course ID in Xyleme IRI format
2246
2376
  // Native format: http://xyleme.com/bravais/document/{guid}
2247
2377
  if (info.guid) {
2248
2378
  info.id = 'http://xyleme.com/bravais/document/' + info.guid;
@@ -4815,12 +4945,30 @@ function generateLrsBridgeCode(options) {
4815
4945
  tryLaunchEvents();
4816
4946
  }
4817
4947
 
4948
+ // After Bravais API metadata resolves, try our authenticated server as a
4949
+ // final fallback to get the document name for packageName.
4950
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
4951
+ function maybeEnrichAndReady() {
4952
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
4953
+ onDocReady();
4954
+ return;
4955
+ }
4956
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
4957
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
4958
+ if (name) {
4959
+ LRS.courseInfo.packageName = name;
4960
+ log('Set packageName from server lookup:', name);
4961
+ }
4962
+ onDocReady();
4963
+ });
4964
+ }
4965
+
4818
4966
  function fetchDocDataAndReady(docId) {
4819
4967
  fetchDocumentMetadata(docId, function(docData) {
4820
4968
  if (docData) {
4821
4969
  updateCourseInfoFromApi(docData);
4822
4970
  }
4823
- onDocReady();
4971
+ maybeEnrichAndReady();
4824
4972
  });
4825
4973
  }
4826
4974
 
@@ -4840,7 +4988,7 @@ function generateLrsBridgeCode(options) {
4840
4988
  } else {
4841
4989
  warn('Could not fetch document data from shared API - statements may fail aggregation');
4842
4990
  }
4843
- onDocReady();
4991
+ maybeEnrichAndReady();
4844
4992
  });
4845
4993
  } else if (documentId) {
4846
4994
  // No shared link token, try with extracted document ID (requires auth)
@@ -4849,11 +4997,11 @@ function generateLrsBridgeCode(options) {
4849
4997
  } else {
4850
4998
  // No identifiers available
4851
4999
  warn('No shared link token or document ID - statements may fail aggregation');
4852
- onDocReady();
5000
+ maybeEnrichAndReady();
4853
5001
  }
4854
5002
  } else {
4855
- // Already have GUID
4856
- onDocReady();
5003
+ // Already have GUID \u2014 still try to enrich packageName if missing
5004
+ maybeEnrichAndReady();
4857
5005
  }
4858
5006
 
4859
5007
  // Setup beforeunload to send terminated