Skip to content

Commit

Permalink
Fix browser atob errors (Azure#363)
Browse files Browse the repository at this point in the history
* Fix errors where browser atob was hitting invalid characters decoding collection rid

* Remove only
  • Loading branch information
southpolesteve authored and christopheranderson committed Jul 2, 2019
1 parent fe7816b commit 49d5310
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/session/sessionContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export class SessionContainer {
private validateOwnerID(ownerId: string) {
// If ownerId contains exactly 8 bytes it represents a unique database+collection identifier. Otherwise it represents another resource
// The first 4 bytes are the database. The last 4 bytes are the collection.
return atob(ownerId).length === 8;
// Cosmos rids potentially contain "-" which is an invalid character in the browser atob implementation
// See https://en.wikipedia.org/wiki/Base64#Filenames
return atob(ownerId.replace(/-/g, "/")).length === 8;
}

private getPartitionKeyRangeIdToTokenMap(collectionName: string): Map<string, VectorSessionToken> {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/sessionContainer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SessionContext } from "../../dist-esm/session/SessionContext";

describe("SessionContainer", function() {
const collectionLink = "dbs/testDatabase/colls/testCollection";
const collectionId = "oWxIAN48yN0=";
const collectionRid = "-EdBAKsiRLM=";

it("set/get/delete", function() {
const sc = new SessionContainer();
Expand All @@ -15,15 +15,15 @@ describe("SessionContainer", function() {

const nameBasedRequest: SessionContext = {
isNameBased: true,
resourceId: null,
resourceId: collectionRid,
resourceAddress: "/" + collectionLink + "/",
resourceType: ResourceType.item,
operationType: OperationType.Create
};

const resHeadersNameBased: CosmosHeaders = {};
resHeadersNameBased[Constants.HttpHeaders.OwnerFullName] = collectionLink;
resHeadersNameBased[Constants.HttpHeaders.OwnerId] = collectionId;
resHeadersNameBased[Constants.HttpHeaders.OwnerId] = collectionRid;
resHeadersNameBased[Constants.HttpHeaders.SessionToken] = tokenString;

// Add a token and get new token, should be equal
Expand Down

0 comments on commit 49d5310

Please sign in to comment.