@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/index.cjs CHANGED
@@ -1881,23 +1881,96 @@ 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
  */
1887
1950
  function updateCourseInfoFromApi(docData) {
1888
1951
  if (!docData || !LRS.courseInfo) return;
1889
1952
 
1890
- // Document GUID
1891
- if (docData.guid && !LRS.courseInfo.guid) {
1953
+ // Document GUID \u2014 Bravais API GUID ALWAYS overrides baked-in GUID.
1954
+ // The baked-in GUID is a random UUID from wrap time. The Bravais GUID is the
1955
+ // canonical document identifier that Analytics uses to link statements to documents.
1956
+ // Without this override, our statements have a different object.id than cloudplayer
1957
+ // statements, so Analytics treats them as unrelated to the document.
1958
+ if (docData.guid) {
1959
+ if (LRS.courseInfo.guid && LRS.courseInfo.guid !== docData.guid) {
1960
+ log('Overriding baked-in GUID with Bravais GUID:', LRS.courseInfo.guid, '->', docData.guid);
1961
+ }
1892
1962
  LRS.courseInfo.guid = docData.guid;
1893
1963
  LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
1894
- log('Updated course guid from API:', docData.guid);
1964
+ log('Document GUID from API:', docData.guid);
1895
1965
  }
1896
1966
 
1897
- // Version GUID from latestVersion
1898
- if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
1967
+ // Version GUID \u2014 same principle: Bravais version GUID must override baked-in
1968
+ if (docData.latestVersion && docData.latestVersion.guid) {
1969
+ if (LRS.courseInfo.versionGuid && LRS.courseInfo.versionGuid !== docData.latestVersion.guid) {
1970
+ log('Overriding baked-in version GUID:', LRS.courseInfo.versionGuid, '->', docData.latestVersion.guid);
1971
+ }
1899
1972
  LRS.courseInfo.versionGuid = docData.latestVersion.guid;
1900
- log('Updated version guid from API:', docData.latestVersion.guid);
1973
+ log('Version GUID from API:', docData.latestVersion.guid);
1901
1974
  }
1902
1975
 
1903
1976
  // Other metadata
@@ -1911,6 +1984,19 @@ function generateLrsBridgeCode(options) {
1911
1984
  LRS.courseInfo.resourceType = docData.resourceType;
1912
1985
  }
1913
1986
 
1987
+ // Update packageName from API document name (matches Bravais Analytics object name)
1988
+ // Only set if not already baked in at wrap time
1989
+ if (docData.name && !LRS.courseInfo.packageName) {
1990
+ LRS.courseInfo.packageName = docData.name;
1991
+ log('Updated packageName from API:', docData.name);
1992
+ }
1993
+
1994
+ // Update documentId from API if we didn't have it
1995
+ if (docData.id && !LRS.courseInfo.documentId) {
1996
+ LRS.courseInfo.documentId = String(docData.id);
1997
+ log('Updated documentId from API:', docData.id);
1998
+ }
1999
+
1914
2000
  log('Course info updated from API:', LRS.courseInfo);
1915
2001
  }
1916
2002
 
@@ -2248,15 +2334,20 @@ function generateLrsBridgeCode(options) {
2248
2334
  if (!info.packageName) {
2249
2335
  var bravaisDocName = null;
2250
2336
 
2251
- // Strategy 1: Walk parent frames for PARAMS.documentName
2337
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2252
2338
  try {
2253
2339
  var win = window;
2254
2340
  for (var i = 0; i < 10; i++) {
2255
2341
  try {
2256
- if (win.PARAMS && win.PARAMS.documentName) {
2257
- bravaisDocName = win.PARAMS.documentName;
2258
- log('Bravais document name from PARAMS:', bravaisDocName);
2259
- break;
2342
+ if (win.PARAMS) {
2343
+ if (win.PARAMS.documentName && !bravaisDocName) {
2344
+ bravaisDocName = win.PARAMS.documentName;
2345
+ log('Bravais document name from PARAMS:', bravaisDocName);
2346
+ }
2347
+ if (win.PARAMS.did && !info.documentId) {
2348
+ info.documentId = String(win.PARAMS.did);
2349
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2350
+ }
2260
2351
  }
2261
2352
  } catch (e) {}
2262
2353
  if (win === win.parent) break;
@@ -4864,12 +4955,30 @@ function generateLrsBridgeCode(options) {
4864
4955
  tryLaunchEvents();
4865
4956
  }
4866
4957
 
4958
+ // After Bravais API metadata resolves, try our authenticated server as a
4959
+ // final fallback to get the document name for packageName.
4960
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
4961
+ function maybeEnrichAndReady() {
4962
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
4963
+ onDocReady();
4964
+ return;
4965
+ }
4966
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
4967
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
4968
+ if (name) {
4969
+ LRS.courseInfo.packageName = name;
4970
+ log('Set packageName from server lookup:', name);
4971
+ }
4972
+ onDocReady();
4973
+ });
4974
+ }
4975
+
4867
4976
  function fetchDocDataAndReady(docId) {
4868
4977
  fetchDocumentMetadata(docId, function(docData) {
4869
4978
  if (docData) {
4870
4979
  updateCourseInfoFromApi(docData);
4871
4980
  }
4872
- onDocReady();
4981
+ maybeEnrichAndReady();
4873
4982
  });
4874
4983
  }
4875
4984
 
@@ -4889,7 +4998,7 @@ function generateLrsBridgeCode(options) {
4889
4998
  } else {
4890
4999
  warn('Could not fetch document data from shared API - statements may fail aggregation');
4891
5000
  }
4892
- onDocReady();
5001
+ maybeEnrichAndReady();
4893
5002
  });
4894
5003
  } else if (documentId) {
4895
5004
  // No shared link token, try with extracted document ID (requires auth)
@@ -4898,11 +5007,11 @@ function generateLrsBridgeCode(options) {
4898
5007
  } else {
4899
5008
  // No identifiers available
4900
5009
  warn('No shared link token or document ID - statements may fail aggregation');
4901
- onDocReady();
5010
+ maybeEnrichAndReady();
4902
5011
  }
4903
5012
  } else {
4904
- // Already have GUID
4905
- onDocReady();
5013
+ // Already have GUID \u2014 still try to enrich packageName if missing
5014
+ maybeEnrichAndReady();
4906
5015
  }
4907
5016
 
4908
5017
  // Setup beforeunload to send terminated