@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/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
 
@@ -2248,15 +2324,20 @@ function generateLrsBridgeCode(options) {
2248
2324
  if (!info.packageName) {
2249
2325
  var bravaisDocName = null;
2250
2326
 
2251
- // Strategy 1: Walk parent frames for PARAMS.documentName
2327
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2252
2328
  try {
2253
2329
  var win = window;
2254
2330
  for (var i = 0; i < 10; i++) {
2255
2331
  try {
2256
- if (win.PARAMS && win.PARAMS.documentName) {
2257
- bravaisDocName = win.PARAMS.documentName;
2258
- log('Bravais document name from PARAMS:', bravaisDocName);
2259
- break;
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
+ }
2260
2341
  }
2261
2342
  } catch (e) {}
2262
2343
  if (win === win.parent) break;
@@ -4864,12 +4945,30 @@ function generateLrsBridgeCode(options) {
4864
4945
  tryLaunchEvents();
4865
4946
  }
4866
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
+
4867
4966
  function fetchDocDataAndReady(docId) {
4868
4967
  fetchDocumentMetadata(docId, function(docData) {
4869
4968
  if (docData) {
4870
4969
  updateCourseInfoFromApi(docData);
4871
4970
  }
4872
- onDocReady();
4971
+ maybeEnrichAndReady();
4873
4972
  });
4874
4973
  }
4875
4974
 
@@ -4889,7 +4988,7 @@ function generateLrsBridgeCode(options) {
4889
4988
  } else {
4890
4989
  warn('Could not fetch document data from shared API - statements may fail aggregation');
4891
4990
  }
4892
- onDocReady();
4991
+ maybeEnrichAndReady();
4893
4992
  });
4894
4993
  } else if (documentId) {
4895
4994
  // No shared link token, try with extracted document ID (requires auth)
@@ -4898,11 +4997,11 @@ function generateLrsBridgeCode(options) {
4898
4997
  } else {
4899
4998
  // No identifiers available
4900
4999
  warn('No shared link token or document ID - statements may fail aggregation');
4901
- onDocReady();
5000
+ maybeEnrichAndReady();
4902
5001
  }
4903
5002
  } else {
4904
- // Already have GUID
4905
- onDocReady();
5003
+ // Already have GUID \u2014 still try to enrich packageName if missing
5004
+ maybeEnrichAndReady();
4906
5005
  }
4907
5006
 
4908
5007
  // Setup beforeunload to send terminated