@patch-adams/core 1.4.6 → 1.4.8

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
@@ -1986,7 +1986,12 @@ function generateLrsBridgeCode(options) {
1986
1986
  callback(null);
1987
1987
  }
1988
1988
  } else {
1989
- warn('Shared link fetch failed. Status:', xhr.status);
1989
+ // 401/403 is expected - API requires auth that may not be available from iframe
1990
+ if (xhr.status === 401 || xhr.status === 403) {
1991
+ log('Shared link API requires auth (expected), will use fallback');
1992
+ } else {
1993
+ log('Shared link fetch failed. Status:', xhr.status);
1994
+ }
1990
1995
  callback(null);
1991
1996
  }
1992
1997
  }
@@ -2006,15 +2011,11 @@ function generateLrsBridgeCode(options) {
2006
2011
  }
2007
2012
 
2008
2013
  /**
2009
- * Fetch document metadata from Bravais API to get GUIDs
2010
- * Endpoint: /api/v3/documents/{documentId}
2014
+ * Fetch document metadata from Bravais shared API to get GUIDs
2015
+ * PRIMARY endpoint: /api/shared/{token}/documents (does NOT require auth)
2016
+ * FALLBACK endpoint: /api/v3/documents/{documentId} (requires auth)
2011
2017
  */
2012
- function fetchDocumentMetadata(documentId, callback) {
2013
- if (!documentId) {
2014
- callback(null);
2015
- return;
2016
- }
2017
-
2018
+ function fetchDocumentMetadata(documentIdOrToken, callback) {
2018
2019
  if (documentApiFetched) {
2019
2020
  callback(documentApiData);
2020
2021
  return;
@@ -2028,8 +2029,24 @@ function generateLrsBridgeCode(options) {
2028
2029
  return;
2029
2030
  }
2030
2031
 
2031
- var apiUrl = coreUrl + '/api/v3/documents/' + documentId;
2032
- log('Fetching document metadata from:', apiUrl);
2032
+ // Determine which endpoint to use
2033
+ // If we have a shared link token, use /api/shared/{token}/documents (no auth required)
2034
+ // This is the same endpoint Xyleme Cloud Player uses
2035
+ var sharedToken = LRS.courseInfo ? LRS.courseInfo.sharedLinkToken : null;
2036
+ var apiUrl;
2037
+
2038
+ if (sharedToken) {
2039
+ apiUrl = coreUrl + '/api/shared/' + sharedToken + '/documents';
2040
+ log('Fetching document via shared API (no auth required):', apiUrl);
2041
+ } else if (documentIdOrToken) {
2042
+ apiUrl = coreUrl + '/api/v3/documents/' + documentIdOrToken;
2043
+ log('Fetching document via v3 API (requires auth):', apiUrl);
2044
+ } else {
2045
+ log('No shared token or document ID available for metadata fetch');
2046
+ documentApiFetched = true;
2047
+ callback(null);
2048
+ return;
2049
+ }
2033
2050
 
2034
2051
  var xhr = new XMLHttpRequest();
2035
2052
  xhr.open('GET', apiUrl, true);
@@ -2061,7 +2078,12 @@ function generateLrsBridgeCode(options) {
2061
2078
  callback(null);
2062
2079
  }
2063
2080
  } else {
2064
- warn('Document metadata fetch failed. Status:', xhr.status, 'Response:', xhr.responseText);
2081
+ // 401/403 is expected - API requires auth that may not be available from iframe
2082
+ if (xhr.status === 401 || xhr.status === 403) {
2083
+ log('Document API requires auth (expected), will use fallback');
2084
+ } else {
2085
+ log('Document metadata fetch failed. Status:', xhr.status);
2086
+ }
2065
2087
  callback(null);
2066
2088
  }
2067
2089
  }
@@ -2224,6 +2246,95 @@ function generateLrsBridgeCode(options) {
2224
2246
  return null;
2225
2247
  }
2226
2248
 
2249
+ /**
2250
+ * Try to extract document data from Xyleme Cloud Player's internal state
2251
+ * The Cloud Player stores this after fetching /api/shared/{token}/documents
2252
+ */
2253
+ function extractFromXylemeCloudPlayer() {
2254
+ try {
2255
+ var win = window;
2256
+ for (var level = 0; level < 10; level++) {
2257
+ try {
2258
+ // Check for Xyleme Cloud Player's document data in various locations
2259
+ // The player stores data in multiple places depending on version
2260
+
2261
+ // Check for CdsDataService data
2262
+ if (win._cdsDataService && win._cdsDataService.documentData) {
2263
+ var docData = win._cdsDataService.documentData;
2264
+ log('Found document data in _cdsDataService at level', level);
2265
+ return extractGuidsFromDocumentData(docData);
2266
+ }
2267
+
2268
+ // Check for window.documentData
2269
+ if (win.documentData && win.documentData.guid) {
2270
+ log('Found document data in window.documentData at level', level);
2271
+ return extractGuidsFromDocumentData(win.documentData);
2272
+ }
2273
+
2274
+ // Check for PlayerIntegration's internal data
2275
+ if (win.playerIntegration && win.playerIntegration._documentData) {
2276
+ log('Found document data in playerIntegration at level', level);
2277
+ return extractGuidsFromDocumentData(win.playerIntegration._documentData);
2278
+ }
2279
+
2280
+ // Check for __xyleme_data
2281
+ if (win.__xyleme_data && win.__xyleme_data.document) {
2282
+ log('Found document data in __xyleme_data at level', level);
2283
+ return extractGuidsFromDocumentData(win.__xyleme_data.document);
2284
+ }
2285
+
2286
+ // Check for documentVersionData which has the versionGuid
2287
+ if (win.documentVersionData) {
2288
+ log('Found documentVersionData at level', level);
2289
+ return {
2290
+ versionGuid: win.documentVersionData.guid || win.documentVersionData.versionGuid,
2291
+ documentId: win.documentVersionData.documentId
2292
+ };
2293
+ }
2294
+
2295
+ } catch (e) {}
2296
+
2297
+ if (win.parent && win.parent !== win) {
2298
+ win = win.parent;
2299
+ } else {
2300
+ break;
2301
+ }
2302
+ }
2303
+ } catch (e) {}
2304
+ return null;
2305
+ }
2306
+
2307
+ function extractGuidsFromDocumentData(data) {
2308
+ if (!data) return null;
2309
+
2310
+ var result = {};
2311
+
2312
+ // Document GUID
2313
+ if (data.guid) result.guid = data.guid;
2314
+ if (data.documentGuid) result.guid = data.documentGuid;
2315
+
2316
+ // Version GUID - usually in latestVersion or currentVersion
2317
+ if (data.latestVersion && data.latestVersion.guid) {
2318
+ result.versionGuid = data.latestVersion.guid;
2319
+ }
2320
+ if (data.currentVersion && data.currentVersion.guid) {
2321
+ result.versionGuid = data.currentVersion.guid;
2322
+ }
2323
+ if (data.versionGuid) result.versionGuid = data.versionGuid;
2324
+
2325
+ // Numeric IDs
2326
+ if (data.id) result.documentId = data.id;
2327
+ if (data.documentId) result.documentId = data.documentId;
2328
+ if (data.cdsId) result.documentId = data.cdsId;
2329
+
2330
+ // Title
2331
+ if (data.name) result.title = data.name;
2332
+ if (data.title) result.title = data.title;
2333
+
2334
+ log('Extracted GUIDs from Cloud Player:', result);
2335
+ return Object.keys(result).length > 0 ? result : null;
2336
+ }
2337
+
2227
2338
  function extractCourseInfo() {
2228
2339
  var info = {
2229
2340
  // Xyleme IRI format: http://xyleme.com/bravais/document/{guid}
@@ -2279,6 +2390,23 @@ function generateLrsBridgeCode(options) {
2279
2390
  info.sharedLinkName = launchInfo.sharedLinkName || info.sharedLinkName;
2280
2391
  }
2281
2392
 
2393
+ // 3b. Try to extract GUIDs from Xyleme Cloud Player's internal data
2394
+ // This is more reliable than API calls which may fail with 401
2395
+ if (!info.guid || !info.versionGuid) {
2396
+ var cloudPlayerData = extractFromXylemeCloudPlayer();
2397
+ if (cloudPlayerData) {
2398
+ info.guid = cloudPlayerData.guid || info.guid;
2399
+ info.versionGuid = cloudPlayerData.versionGuid || info.versionGuid;
2400
+ info.documentId = cloudPlayerData.documentId || info.documentId;
2401
+ info.title = cloudPlayerData.title || info.title;
2402
+ log('Updated course info from Cloud Player:', {
2403
+ guid: info.guid,
2404
+ versionGuid: info.versionGuid,
2405
+ documentId: info.documentId
2406
+ });
2407
+ }
2408
+ }
2409
+
2282
2410
  // 4. Try getCourseTitle() from preloadIntegrity.js
2283
2411
  var preloadTitle = extractCourseTitleFromPreload();
2284
2412
  if (preloadTitle) {
@@ -3784,7 +3912,7 @@ function generateLrsBridgeCode(options) {
3784
3912
  // ========================================================================
3785
3913
 
3786
3914
  function init() {
3787
- log('Initializing LRS bridge v2.5.1...');
3915
+ log('Initializing LRS bridge v2.6.0...');
3788
3916
 
3789
3917
  // Extract course info early
3790
3918
  extractCourseInfo();
@@ -3853,32 +3981,25 @@ function generateLrsBridgeCode(options) {
3853
3981
  }
3854
3982
 
3855
3983
  if (!LRS.courseInfo.guid) {
3856
- // First, try to get real document ID from shared link token
3857
- // The URL path contains a thin pack file ID, not the real document ID
3984
+ // When we have a shared link token, use /api/shared/{token}/documents directly
3985
+ // This endpoint does NOT require authentication and returns both document GUID and version GUID
3858
3986
  if (sharedLinkToken) {
3859
- log('Fetching shared link data to get real document ID...');
3860
- fetchSharedLinkData(sharedLinkToken, function(linkData) {
3861
- if (linkData && linkData.documentId) {
3862
- // Got the real document ID from shared link
3863
- log('Got real document ID from shared link:', linkData.documentId);
3864
- LRS.courseInfo.documentId = linkData.documentId;
3865
- // Update shared link name if available
3866
- if (linkData.documentName) {
3867
- LRS.courseInfo.sharedLinkName = linkData.documentName;
3987
+ log('Have shared link token, fetching document data via shared API...');
3988
+ // fetchDocumentMetadata will use /api/shared/{token}/documents when sharedLinkToken is present
3989
+ fetchDocumentMetadata(null, function(docData) {
3990
+ if (docData) {
3991
+ updateCourseInfoFromApi(docData);
3992
+ // Also update document name if available
3993
+ if (docData.name) {
3994
+ LRS.courseInfo.sharedLinkName = docData.name;
3868
3995
  }
3869
- fetchDocDataAndSend(linkData.documentId);
3870
- } else if (documentId) {
3871
- // Fallback to extracted document ID (may be thin pack ID)
3872
- warn('Could not get document ID from shared link, using extracted ID:', documentId);
3873
- fetchDocDataAndSend(documentId);
3874
3996
  } else {
3875
- // No document ID available
3876
- warn('No document ID available - statements may fail aggregation');
3877
- setTimeout(sendLaunchEvents, 100);
3997
+ warn('Could not fetch document data from shared API - statements may fail aggregation');
3878
3998
  }
3999
+ setTimeout(sendLaunchEvents, 100);
3879
4000
  });
3880
4001
  } else if (documentId) {
3881
- // No shared link token, try with extracted document ID
4002
+ // No shared link token, try with extracted document ID (requires auth)
3882
4003
  log('No shared link token, fetching document data with ID:', documentId);
3883
4004
  fetchDocDataAndSend(documentId);
3884
4005
  } else {