@azure/cosmos 3.16.3 → 3.16.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Release History
2
2
 
3
+ ## 3.16.4 (2022-08-05)
4
+
5
+ ### Bugs Fixed
6
+ - Reverts changes of [PR 22548](https://github.com/Azure/azure-sdk-for-js/pull/22548) to avoid possible regression when customers use id with special characters and their account is on ComputeGateway already. - See [PR 22818](https://github.com/Azure/azure-sdk-for-js/pull/22818)
7
+
3
8
  ## 3.16.3 (2022-07-13)
4
9
 
5
10
  ### Bugs Fixed
package/dist/index.js CHANGED
@@ -179,7 +179,7 @@ const Constants = {
179
179
  AzureNamespace: "Azure.Cosmos",
180
180
  AzurePackageName: "@azure/cosmos",
181
181
  SDKName: "azure-cosmos-js",
182
- SDKVersion: "3.16.3",
182
+ SDKVersion: "3.16.4",
183
183
  Quota: {
184
184
  CollectionSize: "collectionSize",
185
185
  },
@@ -412,6 +412,7 @@ exports.SasTokenPermissionKind = void 0;
412
412
  const trimLeftSlashes = new RegExp("^[/]+");
413
413
  const trimRightSlashes = new RegExp("[/]+$");
414
414
  const illegalResourceIdCharacters = new RegExp("[/\\\\?#]");
415
+ const illegalItemResourceIdCharacters = new RegExp("[/\\\\#]");
415
416
  /** @hidden */
416
417
  function jsonStringifyAndEscapeNonASCII(arg) {
417
418
  // TODO: better way for this? Not sure.
@@ -564,7 +565,33 @@ function isResourceValid(resource, err) {
564
565
  err.message = "Id must be a string.";
565
566
  return false;
566
567
  }
567
- if (resource.id.indexOf("/") !== -1 || resource.id.indexOf("\\") !== -1) {
568
+ if (resource.id.indexOf("/") !== -1 ||
569
+ resource.id.indexOf("\\") !== -1 ||
570
+ resource.id.indexOf("?") !== -1 ||
571
+ resource.id.indexOf("#") !== -1) {
572
+ err.message = "Id contains illegal chars.";
573
+ return false;
574
+ }
575
+ if (resource.id[resource.id.length - 1] === " ") {
576
+ err.message = "Id ends with a space.";
577
+ return false;
578
+ }
579
+ }
580
+ return true;
581
+ }
582
+ /**
583
+ * @hidden
584
+ */
585
+ function isItemResourceValid(resource, err) {
586
+ // TODO: fix strictness issues so that caller contexts respects the types of the functions
587
+ if (resource.id) {
588
+ if (typeof resource.id !== "string") {
589
+ err.message = "Id must be a string.";
590
+ return false;
591
+ }
592
+ if (resource.id.indexOf("/") !== -1 ||
593
+ resource.id.indexOf("\\") !== -1 ||
594
+ resource.id.indexOf("#") !== -1) {
568
595
  err.message = "Id contains illegal chars.";
569
596
  return false;
570
597
  }
@@ -608,11 +635,25 @@ function trimSlashFromLeftAndRight(inputString) {
608
635
  function validateResourceId(resourceId) {
609
636
  // if resourceId is not a string or is empty throw an error
610
637
  if (typeof resourceId !== "string" || isStringNullOrEmpty(resourceId)) {
611
- throw new Error("Resource Id must be a string and cannot be undefined, null or empty");
638
+ throw new Error("Resource ID must be a string and cannot be undefined, null or empty");
612
639
  }
613
640
  // if resource id contains illegal characters throw an error
614
641
  if (illegalResourceIdCharacters.test(resourceId)) {
615
- throw new Error("Illegal characters ['/', '\\'] cannot be used in resourceId");
642
+ throw new Error("Illegal characters ['/', '\\', '#', '?'] cannot be used in Resource ID");
643
+ }
644
+ return true;
645
+ }
646
+ /**
647
+ * @hidden
648
+ */
649
+ function validateItemResourceId(resourceId) {
650
+ // if resourceId is not a string or is empty throw an error
651
+ if (typeof resourceId !== "string" || isStringNullOrEmpty(resourceId)) {
652
+ throw new Error("Resource ID must be a string and cannot be undefined, null or empty");
653
+ }
654
+ // if resource id contains illegal characters throw an error
655
+ if (illegalItemResourceIdCharacters.test(resourceId)) {
656
+ throw new Error("Illegal characters ['/', '\\', '#'] cannot be used in Resource ID");
616
657
  }
617
658
  return true;
618
659
  }
@@ -760,7 +801,7 @@ function createUserUri(databaseId, userId) {
760
801
  */
761
802
  function createDocumentUri(databaseId, collectionId, documentId) {
762
803
  documentId = trimSlashFromLeftAndRight(documentId);
763
- validateResourceId(documentId);
804
+ validateItemResourceId(documentId);
764
805
  return (createDocumentCollectionUri(databaseId, collectionId) +
765
806
  "/" +
766
807
  Constants.Path.DocumentsPathSegment +
@@ -908,17 +949,6 @@ async function generateHeaders(masterKey, method, resourceType = exports.Resourc
908
949
  [Constants.HttpHeaders.XDate]: date.toUTCString(),
909
950
  };
910
951
  }
911
- function getEffectiveResourceIdForSignature(resourceId) {
912
- const lastSlashPosition = resourceId.lastIndexOf("/");
913
- if (lastSlashPosition <= 0) {
914
- return resourceId;
915
- }
916
- const prefix = resourceId.substring(0, lastSlashPosition);
917
- if (!prefix.endsWith("/docs")) {
918
- return resourceId;
919
- }
920
- return prefix + "/" + decodeURIComponent(resourceId.substring(lastSlashPosition + 1));
921
- }
922
952
  async function signature(masterKey, method, resourceType, resourceId = "", date = new Date()) {
923
953
  const type = "master";
924
954
  const version = "1.0";
@@ -926,7 +956,7 @@ async function signature(masterKey, method, resourceType, resourceId = "", date
926
956
  "\n" +
927
957
  resourceType.toLowerCase() +
928
958
  "\n" +
929
- getEffectiveResourceIdForSignature(resourceId) +
959
+ resourceId +
930
960
  "\n" +
931
961
  date.toUTCString().toLowerCase() +
932
962
  "\n" +
@@ -4372,7 +4402,7 @@ class Item {
4372
4402
  * Returns a reference URL to the resource. Used for linking in Permissions.
4373
4403
  */
4374
4404
  get url() {
4375
- return createDocumentUri(this.container.database.id, this.container.id, encodeURIComponent(this.id));
4405
+ return createDocumentUri(this.container.database.id, this.container.id, this.id);
4376
4406
  }
4377
4407
  /**
4378
4408
  * Read the item's definition.
@@ -4403,9 +4433,8 @@ class Item {
4403
4433
  const { resource: partitionKeyDefinition } = await this.container.readPartitionKeyDefinition();
4404
4434
  this.partitionKey = undefinedPartitionKey(partitionKeyDefinition);
4405
4435
  }
4406
- const resourceUri = this.url;
4407
- const path = getPathFromLink(resourceUri);
4408
- const id = getIdFromLink(resourceUri);
4436
+ const path = getPathFromLink(this.url);
4437
+ const id = getIdFromLink(this.url);
4409
4438
  let response;
4410
4439
  try {
4411
4440
  response = await this.clientContext.read({
@@ -4430,12 +4459,11 @@ class Item {
4430
4459
  this.partitionKey = extractPartitionKey(body, partitionKeyDefinition);
4431
4460
  }
4432
4461
  const err = {};
4433
- if (!isResourceValid(body, err)) {
4462
+ if (!isItemResourceValid(body, err)) {
4434
4463
  throw err;
4435
4464
  }
4436
- const resourceUri = this.url;
4437
- const path = getPathFromLink(resourceUri);
4438
- const id = getIdFromLink(resourceUri);
4465
+ const path = getPathFromLink(this.url);
4466
+ const id = getIdFromLink(this.url);
4439
4467
  const response = await this.clientContext.replace({
4440
4468
  body,
4441
4469
  path,
@@ -4459,9 +4487,8 @@ class Item {
4459
4487
  const { resource: partitionKeyDefinition } = await this.container.readPartitionKeyDefinition();
4460
4488
  this.partitionKey = undefinedPartitionKey(partitionKeyDefinition);
4461
4489
  }
4462
- const resourceUri = this.url;
4463
- const path = getPathFromLink(resourceUri);
4464
- const id = getIdFromLink(resourceUri);
4490
+ const path = getPathFromLink(this.url);
4491
+ const id = getIdFromLink(this.url);
4465
4492
  const response = await this.clientContext.delete({
4466
4493
  path,
4467
4494
  resourceType: exports.ResourceType.item,
@@ -4484,9 +4511,8 @@ class Item {
4484
4511
  const { resource: partitionKeyDefinition } = await this.container.readPartitionKeyDefinition();
4485
4512
  this.partitionKey = extractPartitionKey(body, partitionKeyDefinition);
4486
4513
  }
4487
- const resourceUri = this.url;
4488
- const path = getPathFromLink(resourceUri);
4489
- const id = getIdFromLink(resourceUri);
4514
+ const path = getPathFromLink(this.url);
4515
+ const id = getIdFromLink(this.url);
4490
4516
  const response = await this.clientContext.patch({
4491
4517
  body,
4492
4518
  path,
@@ -5416,7 +5442,7 @@ class Items {
5416
5442
  const { resource: partitionKeyDefinition } = await this.container.readPartitionKeyDefinition();
5417
5443
  const partitionKey = extractPartitionKey(body, partitionKeyDefinition);
5418
5444
  const err = {};
5419
- if (!isResourceValid(body, err)) {
5445
+ if (!isItemResourceValid(body, err)) {
5420
5446
  throw err;
5421
5447
  }
5422
5448
  const path = getPathFromLink(this.container.url, exports.ResourceType.item);
@@ -5441,7 +5467,7 @@ class Items {
5441
5467
  body.id = uuid$1();
5442
5468
  }
5443
5469
  const err = {};
5444
- if (!isResourceValid(body, err)) {
5470
+ if (!isItemResourceValid(body, err)) {
5445
5471
  throw err;
5446
5472
  }
5447
5473
  const path = getPathFromLink(this.container.url, exports.ResourceType.item);