@eluvio/elv-client-js 4.2.19 → 4.2.21

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eluvio/elv-client-js",
3
- "version": "4.2.19",
3
+ "version": "4.2.21",
4
4
  "description": "Javascript client for the Eluvio Content Fabric",
5
5
  "main": "src/index.js",
6
6
  "author": "Kevin Talmadge",
package/src/HttpClient.js CHANGED
@@ -96,6 +96,13 @@ class HttpClient {
96
96
  // Save current URI for all future requests involving this write token
97
97
  this.draftURIs[writeToken] = baseURI;
98
98
  }
99
+
100
+ // If write token is present in headers, we may get redirected even if we try and use the right node.
101
+ // When a network request is redirected, the authorization header is dropped, which will cause issues.
102
+ // Ensure it is present in query params as well
103
+ if((headers.Authorization || headers.authorization) && !queryParams.authorization) {
104
+ queryParams.authorization = headers.Authorization || headers.authorization;
105
+ }
99
106
  }
100
107
 
101
108
  let uri = baseURI
@@ -20,6 +20,8 @@ const {
20
20
  } = require("../Validation");
21
21
 
22
22
  const MergeWith = require("lodash/mergeWith");
23
+ const LodashSet = require("lodash/set");
24
+ const LodashGet = require("lodash/get");
23
25
 
24
26
  // Note: Keep these ordered by most-restrictive to least-restrictive
25
27
  exports.permissionLevels = {
@@ -583,7 +585,7 @@ exports.ContentObjects = async function({libraryId, filterOptions={}}) {
583
585
  *
584
586
  * @returns {Promise<Object>} - Description of content object
585
587
  */
586
- exports.ContentObject = async function({objectId, versionHash, writeToken, noCache}) {
588
+ exports.ContentObject = async function({objectId, versionHash, writeToken, noCache, authorizationToken}) {
587
589
  this.Log(`Retrieving content object: ${writeToken || versionHash || objectId}`);
588
590
 
589
591
  if(writeToken) {
@@ -598,7 +600,9 @@ exports.ContentObject = async function({objectId, versionHash, writeToken, noCac
598
600
  if(noCache || !this.objectInfo[id] || Date.now() - this.objectInfo[id].retrievedAt > 30000) {
599
601
  let path = UrlJoin("q", id);
600
602
  const info = await this.HttpClient.RequestJsonBody({
601
- headers: await this.authClient.AuthorizationHeader({objectId, versionHash}),
603
+ headers: {
604
+ Authorization: `Bearer ${authorizationToken || await this.authClient.AuthorizationToken({objectId, versionHash})}`,
605
+ },
602
606
  method: "GET",
603
607
  path: path,
604
608
  queryParams: {
@@ -626,7 +630,7 @@ exports.ContentObject = async function({objectId, versionHash, writeToken, noCac
626
630
  *
627
631
  * @returns {Promise<string>} - The account address of the owner
628
632
  */
629
- exports.ContentObjectOwner = async function({objectId, versionHash}) {
633
+ exports.ContentObjectOwner = async function({objectId, versionHash, authorizationToken}) {
630
634
  versionHash ? ValidateVersion(versionHash) : ValidateObject(objectId);
631
635
 
632
636
  if(versionHash) {
@@ -635,7 +639,7 @@ exports.ContentObjectOwner = async function({objectId, versionHash}) {
635
639
 
636
640
  this.Log(`Retrieving content object owner: ${objectId}`);
637
641
 
638
- return this.utils.HashToAddress((await this.ContentObject({objectId, versionHash})).content_profile.owner);
642
+ return this.utils.HashToAddress((await this.ContentObject({objectId, versionHash, authorizationToken})).content_profile.owner);
639
643
  };
640
644
 
641
645
  /**
@@ -649,7 +653,7 @@ exports.ContentObjectOwner = async function({objectId, versionHash}) {
649
653
  *
650
654
  * @returns {Promise<string>} - Tenant ID of the object
651
655
  */
652
- exports.ContentObjectTenantId = async function({objectId, versionHash}) {
656
+ exports.ContentObjectTenantId = async function({objectId, versionHash, authorizationToken}) {
653
657
  versionHash ? ValidateVersion(versionHash) : ValidateObject(objectId);
654
658
 
655
659
  if(versionHash) {
@@ -659,7 +663,7 @@ exports.ContentObjectTenantId = async function({objectId, versionHash}) {
659
663
  // Cache results because they will never change
660
664
  if(!this.objectTenantIds[objectId]) {
661
665
  try {
662
- this.objectTenantIds[objectId] = (await this.ContentObject({objectId, versionHash})).content_profile.tenant_id;
666
+ this.objectTenantIds[objectId] = (await this.ContentObject({objectId, versionHash, authorizationToken})).content_profile.tenant_id;
663
667
  } catch(error) {
664
668
  error.message = `Unable to determine tenant ID for ${versionHash || objectId}`;
665
669
  throw error;
@@ -680,7 +684,7 @@ exports.ContentObjectTenantId = async function({objectId, versionHash}) {
680
684
  *
681
685
  * @returns {Promise<string>} - Library ID of the object
682
686
  */
683
- exports.ContentObjectLibraryId = async function({objectId, versionHash}) {
687
+ exports.ContentObjectLibraryId = async function({objectId, versionHash, authorizationToken}) {
684
688
  versionHash ? ValidateVersion(versionHash) : ValidateObject(objectId);
685
689
 
686
690
  if(versionHash) {
@@ -690,7 +694,7 @@ exports.ContentObjectLibraryId = async function({objectId, versionHash}) {
690
694
  // Cache results because they will never change
691
695
  if(!this.objectLibraryIds[objectId]) {
692
696
  try {
693
- this.objectLibraryIds[objectId] = (await this.ContentObject({objectId, versionHash})).qlib_id;
697
+ this.objectLibraryIds[objectId] = (await this.ContentObject({objectId, versionHash, authorizationToken})).qlib_id;
694
698
  } catch(error) {
695
699
  error.message = `Unable to determine library ID for ${versionHash || objectId}`;
696
700
  throw error;
@@ -707,58 +711,50 @@ exports.ProduceMetadataLinks = async function({
707
711
  path="/",
708
712
  metadata,
709
713
  authorizationToken,
710
- noAuth
714
+ noAuth,
715
+ log=false
711
716
  }) {
712
- // Primitive
713
- if(!metadata || typeof metadata !== "object") { return metadata; }
717
+ path = UrlJoin(path || "").replace(/^\//, "").replace(/\/$/, "");
714
718
 
715
- // Array
716
- if(Array.isArray(metadata)) {
717
- return await this.utils.LimitedMap(
718
- 5,
719
- metadata,
720
- async (entry, i) => await this.ProduceMetadataLinks({
721
- libraryId,
722
- objectId,
723
- versionHash,
724
- path: UrlJoin(path, i.toString()),
725
- metadata: entry,
726
- authorizationToken,
727
- noAuth
728
- })
729
- );
730
- }
719
+ const Traverse = async (currentPath="") => {
720
+ const currentMetadata = !currentPath ? metadata : LodashGet(metadata, currentPath.split("/"));
731
721
 
732
- // Object
733
- if(metadata["/"] &&
734
- (metadata["/"].match(/\.\/(rep|files)\/.+/) ||
735
- metadata["/"].match(/^\/?qfab\/([\w]+)\/?(rep|files)\/.+/)))
736
- {
737
- // Is file or rep link - produce a url
738
- return {
739
- ...metadata,
740
- url: await this.LinkUrl({libraryId, objectId, versionHash, linkPath: path, authorizationToken, noAuth})
741
- };
742
- }
722
+ if(log) {
723
+ console.log(path, currentPath, currentMetadata)
724
+ }
743
725
 
744
- let result = {};
745
- await this.utils.LimitedMap(
746
- 5,
747
- Object.keys(metadata),
748
- async key => {
749
- result[key] = await this.ProduceMetadataLinks({
750
- libraryId,
751
- objectId,
752
- versionHash,
753
- path: UrlJoin(path, key),
754
- metadata: metadata[key],
755
- authorizationToken,
756
- noAuth
757
- });
726
+ // Primitive
727
+ if(!currentMetadata || typeof currentMetadata !== "object") {
728
+ return;
729
+ }
730
+
731
+ // Array
732
+ if(Array.isArray(currentMetadata)) {
733
+ for(let i = 0; i < currentMetadata.length; i++) {
734
+ await Traverse(UrlJoin(currentPath, i.toString()));
735
+ }
758
736
  }
759
- );
760
737
 
761
- return result;
738
+ // Object
739
+ if(currentMetadata["/"] &&
740
+ (currentMetadata["/"].match(/\.\/(rep|files)\/.+/) ||
741
+ currentMetadata["/"].match(/^\/?qfab\/([\w]+)\/?(rep|files)\/.+/))) {
742
+ // Is file or rep link - produce a url
743
+ LodashSet(
744
+ metadata,
745
+ UrlJoin(currentPath, "url").split("/"),
746
+ await this.LinkUrl({libraryId, objectId, versionHash, linkPath: UrlJoin(path, currentPath), authorizationToken, noAuth})
747
+ )
748
+ } else {
749
+ for(let key of Object.keys(currentMetadata)) {
750
+ await Traverse(UrlJoin(currentPath, key))
751
+ }
752
+ }
753
+ }
754
+
755
+ await Traverse();
756
+
757
+ return metadata;
762
758
  };
763
759
 
764
760
  exports.MetadataAuth = async function({
@@ -877,6 +873,7 @@ exports.ContentObjectMetadata = async function({
877
873
  resolveIgnoreErrors=false,
878
874
  linkDepthLimit=1,
879
875
  produceLinkUrls=false,
876
+ log=false
880
877
  }) {
881
878
  ValidateParameters({libraryId, objectId, versionHash});
882
879
 
@@ -947,7 +944,8 @@ exports.ContentObjectMetadata = async function({
947
944
  path: metadataSubtree,
948
945
  metadata,
949
946
  authorizationToken,
950
- noAuth
947
+ noAuth,
948
+ log
951
949
  });
952
950
  }
953
951
 
@@ -1056,7 +1054,7 @@ exports.ContentObjectVersions = async function({libraryId, objectId}) {
1056
1054
  return this.HttpClient.RequestJsonBody({
1057
1055
  headers: await this.authClient.AuthorizationHeader({libraryId, objectId}),
1058
1056
  method: "GET",
1059
- path: path
1057
+ path
1060
1058
  });
1061
1059
  };
1062
1060
 
@@ -1070,13 +1068,13 @@ exports.ContentObjectVersions = async function({libraryId, objectId}) {
1070
1068
  *
1071
1069
  * @returns {Promise<string>} - The latest version hash of the object
1072
1070
  */
1073
- exports.LatestVersionHash = async function({objectId, versionHash}) {
1071
+ exports.LatestVersionHash = async function({objectId, versionHash, authorizationToken}) {
1074
1072
  if(versionHash) {
1075
1073
  objectId = this.utils.DecodeVersionHash(versionHash).objectId;
1076
1074
  }
1077
1075
 
1078
1076
  try {
1079
- return (await this.ContentObject({objectId, noCache: true})).hash;
1077
+ return (await this.ContentObject({objectId, noCache: true, authorizationToken})).hash;
1080
1078
  } catch(error) {
1081
1079
  error.message = `Unable to determine latest version hash for ${versionHash || objectId}`;
1082
1080
  throw error;
@@ -1175,11 +1173,11 @@ exports.PlayoutPathResolution = async function({
1175
1173
  authorizationToken
1176
1174
  }) {
1177
1175
  if(!libraryId) {
1178
- libraryId = await this.ContentObjectLibraryId({objectId});
1176
+ libraryId = await this.ContentObjectLibraryId({objectId, authorizationToken});
1179
1177
  }
1180
1178
 
1181
1179
  if(!versionHash) {
1182
- versionHash = await this.LatestVersionHash({objectId});
1180
+ versionHash = await this.LatestVersionHash({objectId, authorizationToken});
1183
1181
  }
1184
1182
 
1185
1183
  let path = UrlJoin("qlibs", libraryId, "q", writeToken || versionHash, "rep", handler, offering, "options.json");
@@ -1215,7 +1213,7 @@ exports.PlayoutPathResolution = async function({
1215
1213
  authorizationToken
1216
1214
  });
1217
1215
  linkTargetId = this.utils.DecodeVersionHash(linkTargetHash).objectId;
1218
- linkTargetLibraryId = await this.ContentObjectLibraryId({objectId: linkTargetId});
1216
+ linkTargetLibraryId = await this.ContentObjectLibraryId({objectId: linkTargetId, authorizationToken});
1219
1217
 
1220
1218
  if(!multiOfferingLink && !offering) {
1221
1219
  // If the offering is not specified, the intent is to get available offerings. For a single offering link, must
@@ -1272,7 +1270,7 @@ exports.AvailableOfferings = async function({
1272
1270
 
1273
1271
  if(directLink) {
1274
1272
  return await this.ContentObjectMetadata({
1275
- libraryId: await this.ContentObjectLibraryId({objectId}),
1273
+ libraryId: await this.ContentObjectLibraryId({objectId, authorizationToken}),
1276
1274
  objectId,
1277
1275
  versionHash,
1278
1276
  metadataSubtree: linkPath,
@@ -1383,10 +1381,10 @@ exports.PlayoutOptions = async function({
1383
1381
  if(!objectId) {
1384
1382
  objectId = this.utils.DecodeVersionHash(versionHash).objectId;
1385
1383
  } else if(!versionHash) {
1386
- versionHash = await this.LatestVersionHash({objectId});
1384
+ versionHash = await this.LatestVersionHash({objectId, authorizationToken});
1387
1385
  }
1388
1386
 
1389
- const libraryId = await this.ContentObjectLibraryId({objectId});
1387
+ const libraryId = await this.ContentObjectLibraryId({objectId, authorizationToken});
1390
1388
 
1391
1389
  try {
1392
1390
  // If public/asset_metadata/sources/<offering> exists, use that instead of directly calling on object