@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.js CHANGED
@@ -1872,23 +1872,96 @@ function generateLrsBridgeCode(options) {
1872
1872
  }
1873
1873
  }
1874
1874
 
1875
+ /**
1876
+ * Fetch document name from our authenticated server (cdsImporter proxy).
1877
+ * Derives server base URL from LRS_PROXY_ENDPOINT.
1878
+ * Falls back gracefully if CORS blocks or server is unavailable.
1879
+ * This is the final fallback when Bravais API and PARAMS are not available.
1880
+ */
1881
+ function fetchDocumentNameFromServer(documentId, callback) {
1882
+ if (!LRS_PROXY_ENDPOINT || !documentId) {
1883
+ callback(null);
1884
+ return;
1885
+ }
1886
+
1887
+ // Derive server base from LRS proxy endpoint
1888
+ // e.g., https://api.example.com/create/statement \u2192 https://api.example.com/create/
1889
+ var serverBase = LRS_PROXY_ENDPOINT.replace(/\\/statement\\/?$/, '/');
1890
+ if (!serverBase || serverBase === LRS_PROXY_ENDPOINT) {
1891
+ log('Could not derive server base URL from LRS_PROXY_ENDPOINT');
1892
+ callback(null);
1893
+ return;
1894
+ }
1895
+
1896
+ var apiUrl = serverBase + 'cdsImporter/api/documents/' + documentId;
1897
+ log('Fetching document name from server:', apiUrl);
1898
+
1899
+ var xhr = new XMLHttpRequest();
1900
+ xhr.open('GET', apiUrl, true);
1901
+ xhr.withCredentials = true;
1902
+ xhr.setRequestHeader('Accept', 'application/json');
1903
+ xhr.timeout = 5000;
1904
+
1905
+ xhr.onreadystatechange = function() {
1906
+ if (xhr.readyState === 4) {
1907
+ if (xhr.status >= 200 && xhr.status < 300) {
1908
+ try {
1909
+ var data = JSON.parse(xhr.responseText);
1910
+ if (data.name) {
1911
+ log('Document name from server:', data.name);
1912
+ callback(data.name);
1913
+ } else {
1914
+ callback(null);
1915
+ }
1916
+ } catch (e) {
1917
+ callback(null);
1918
+ }
1919
+ } else {
1920
+ log('Server document lookup returned:', xhr.status);
1921
+ callback(null);
1922
+ }
1923
+ }
1924
+ };
1925
+
1926
+ xhr.onerror = function() {
1927
+ log('Server document lookup network error (CORS or connectivity)');
1928
+ callback(null);
1929
+ };
1930
+ xhr.ontimeout = function() {
1931
+ log('Server document lookup timed out');
1932
+ callback(null);
1933
+ };
1934
+
1935
+ try { xhr.send(); } catch (e) { callback(null); }
1936
+ }
1937
+
1875
1938
  /**
1876
1939
  * Update course info with data from document API
1877
1940
  */
1878
1941
  function updateCourseInfoFromApi(docData) {
1879
1942
  if (!docData || !LRS.courseInfo) return;
1880
1943
 
1881
- // Document GUID
1882
- if (docData.guid && !LRS.courseInfo.guid) {
1944
+ // Document GUID \u2014 Bravais API GUID ALWAYS overrides baked-in GUID.
1945
+ // The baked-in GUID is a random UUID from wrap time. The Bravais GUID is the
1946
+ // canonical document identifier that Analytics uses to link statements to documents.
1947
+ // Without this override, our statements have a different object.id than cloudplayer
1948
+ // statements, so Analytics treats them as unrelated to the document.
1949
+ if (docData.guid) {
1950
+ if (LRS.courseInfo.guid && LRS.courseInfo.guid !== docData.guid) {
1951
+ log('Overriding baked-in GUID with Bravais GUID:', LRS.courseInfo.guid, '->', docData.guid);
1952
+ }
1883
1953
  LRS.courseInfo.guid = docData.guid;
1884
1954
  LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
1885
- log('Updated course guid from API:', docData.guid);
1955
+ log('Document GUID from API:', docData.guid);
1886
1956
  }
1887
1957
 
1888
- // Version GUID from latestVersion
1889
- if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
1958
+ // Version GUID \u2014 same principle: Bravais version GUID must override baked-in
1959
+ if (docData.latestVersion && docData.latestVersion.guid) {
1960
+ if (LRS.courseInfo.versionGuid && LRS.courseInfo.versionGuid !== docData.latestVersion.guid) {
1961
+ log('Overriding baked-in version GUID:', LRS.courseInfo.versionGuid, '->', docData.latestVersion.guid);
1962
+ }
1890
1963
  LRS.courseInfo.versionGuid = docData.latestVersion.guid;
1891
- log('Updated version guid from API:', docData.latestVersion.guid);
1964
+ log('Version GUID from API:', docData.latestVersion.guid);
1892
1965
  }
1893
1966
 
1894
1967
  // Other metadata
@@ -1902,6 +1975,19 @@ function generateLrsBridgeCode(options) {
1902
1975
  LRS.courseInfo.resourceType = docData.resourceType;
1903
1976
  }
1904
1977
 
1978
+ // Update packageName from API document name (matches Bravais Analytics object name)
1979
+ // Only set if not already baked in at wrap time
1980
+ if (docData.name && !LRS.courseInfo.packageName) {
1981
+ LRS.courseInfo.packageName = docData.name;
1982
+ log('Updated packageName from API:', docData.name);
1983
+ }
1984
+
1985
+ // Update documentId from API if we didn't have it
1986
+ if (docData.id && !LRS.courseInfo.documentId) {
1987
+ LRS.courseInfo.documentId = String(docData.id);
1988
+ log('Updated documentId from API:', docData.id);
1989
+ }
1990
+
1905
1991
  log('Course info updated from API:', LRS.courseInfo);
1906
1992
  }
1907
1993
 
@@ -2239,15 +2325,20 @@ function generateLrsBridgeCode(options) {
2239
2325
  if (!info.packageName) {
2240
2326
  var bravaisDocName = null;
2241
2327
 
2242
- // Strategy 1: Walk parent frames for PARAMS.documentName
2328
+ // Strategy 1: Walk parent frames for PARAMS.documentName and PARAMS.did
2243
2329
  try {
2244
2330
  var win = window;
2245
2331
  for (var i = 0; i < 10; i++) {
2246
2332
  try {
2247
- if (win.PARAMS && win.PARAMS.documentName) {
2248
- bravaisDocName = win.PARAMS.documentName;
2249
- log('Bravais document name from PARAMS:', bravaisDocName);
2250
- break;
2333
+ if (win.PARAMS) {
2334
+ if (win.PARAMS.documentName && !bravaisDocName) {
2335
+ bravaisDocName = win.PARAMS.documentName;
2336
+ log('Bravais document name from PARAMS:', bravaisDocName);
2337
+ }
2338
+ if (win.PARAMS.did && !info.documentId) {
2339
+ info.documentId = String(win.PARAMS.did);
2340
+ log('Bravais document ID from PARAMS.did:', info.documentId);
2341
+ }
2251
2342
  }
2252
2343
  } catch (e) {}
2253
2344
  if (win === win.parent) break;
@@ -4855,12 +4946,30 @@ function generateLrsBridgeCode(options) {
4855
4946
  tryLaunchEvents();
4856
4947
  }
4857
4948
 
4949
+ // After Bravais API metadata resolves, try our authenticated server as a
4950
+ // final fallback to get the document name for packageName.
4951
+ // This handles cases where PARAMS is cross-origin blocked and no baked name exists.
4952
+ function maybeEnrichAndReady() {
4953
+ if (LRS.courseInfo.packageName || !LRS.courseInfo.documentId || !LRS_PROXY_ENDPOINT) {
4954
+ onDocReady();
4955
+ return;
4956
+ }
4957
+ log('No packageName yet, trying server-side document lookup for ID:', LRS.courseInfo.documentId);
4958
+ fetchDocumentNameFromServer(LRS.courseInfo.documentId, function(name) {
4959
+ if (name) {
4960
+ LRS.courseInfo.packageName = name;
4961
+ log('Set packageName from server lookup:', name);
4962
+ }
4963
+ onDocReady();
4964
+ });
4965
+ }
4966
+
4858
4967
  function fetchDocDataAndReady(docId) {
4859
4968
  fetchDocumentMetadata(docId, function(docData) {
4860
4969
  if (docData) {
4861
4970
  updateCourseInfoFromApi(docData);
4862
4971
  }
4863
- onDocReady();
4972
+ maybeEnrichAndReady();
4864
4973
  });
4865
4974
  }
4866
4975
 
@@ -4880,7 +4989,7 @@ function generateLrsBridgeCode(options) {
4880
4989
  } else {
4881
4990
  warn('Could not fetch document data from shared API - statements may fail aggregation');
4882
4991
  }
4883
- onDocReady();
4992
+ maybeEnrichAndReady();
4884
4993
  });
4885
4994
  } else if (documentId) {
4886
4995
  // No shared link token, try with extracted document ID (requires auth)
@@ -4889,11 +4998,11 @@ function generateLrsBridgeCode(options) {
4889
4998
  } else {
4890
4999
  // No identifiers available
4891
5000
  warn('No shared link token or document ID - statements may fail aggregation');
4892
- onDocReady();
5001
+ maybeEnrichAndReady();
4893
5002
  }
4894
5003
  } else {
4895
- // Already have GUID
4896
- onDocReady();
5004
+ // Already have GUID \u2014 still try to enrich packageName if missing
5005
+ maybeEnrichAndReady();
4897
5006
  }
4898
5007
 
4899
5008
  // Setup beforeunload to send terminated