From ed8c317472321a3e5a6908e8317132c109fd73e1 Mon Sep 17 00:00:00 2001 From: Christopher Anderson Date: Sat, 22 Sep 2018 11:49:32 -0700 Subject: [PATCH] buffer updates & connection policy fixes (#135) * Update buffer to Buffer methods * Adds support for non-class based ConnectionPolicy fixes 115 --- src/ClientContext.ts | 2 +- src/CosmosClient.ts | 6 +++--- src/CosmosClientOptions.ts | 2 +- src/auth.ts | 4 ++-- src/common/helper.ts | 17 +++++++++++++++++ src/common/platform.ts | 2 +- src/common/resourceId.ts | 5 ++--- src/documents/ConnectionPolicy.ts | 1 - src/request/RequestHandler.ts | 2 +- src/test/functional/client.spec.ts | 16 +++++++++++++--- 10 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/ClientContext.ts b/src/ClientContext.ts index 9e36562..207d1d0 100644 --- a/src/ClientContext.ts +++ b/src/ClientContext.ts @@ -23,7 +23,7 @@ export class ClientContext { private cosmosClientOptions: CosmosClientOptions, private globalEndpointManager: GlobalEndpointManager ) { - this.connectionPolicy = cosmosClientOptions.connectionPolicy || new ConnectionPolicy(); + this.connectionPolicy = Helper.parseConnectionPolicy(cosmosClientOptions.connectionPolicy); this.sessionContainer = new SessionContainer(); this.requestHandler = new RequestHandler( globalEndpointManager, diff --git a/src/CosmosClient.ts b/src/CosmosClient.ts index 1480529..e29e610 100644 --- a/src/CosmosClient.ts +++ b/src/CosmosClient.ts @@ -5,9 +5,9 @@ import { Constants, RequestOptions } from "."; import { Database, Databases } from "./client/Database"; import { Offer, Offers } from "./client/Offer"; import { ClientContext } from "./ClientContext"; -import { Platform } from "./common"; +import { Helper, Platform } from "./common"; import { CosmosClientOptions } from "./CosmosClientOptions"; -import { ConnectionPolicy, DatabaseAccount } from "./documents"; +import { DatabaseAccount } from "./documents"; import { GlobalEndpointManager } from "./globalEndpointManager"; import { CosmosResponse } from "./request"; @@ -57,7 +57,7 @@ export class CosmosClient { constructor(private options: CosmosClientOptions) { options.auth = options.auth || {}; - options.connectionPolicy = options.connectionPolicy || new ConnectionPolicy(); + options.connectionPolicy = Helper.parseConnectionPolicy(options.connectionPolicy); options.defaultHeaders = options.defaultHeaders || {}; options.defaultHeaders[Constants.HttpHeaders.CacheControl] = "no-cache"; diff --git a/src/CosmosClientOptions.ts b/src/CosmosClientOptions.ts index 0d4e4ab..855961d 100644 --- a/src/CosmosClientOptions.ts +++ b/src/CosmosClientOptions.ts @@ -11,7 +11,7 @@ export interface CosmosClientOptions { /** An instance of {@link ConnectionPolicy} class. * This parameter is optional and the default connectionPolicy will be used if omitted. */ - connectionPolicy?: ConnectionPolicy; + connectionPolicy?: ConnectionPolicy | { [P in keyof ConnectionPolicy]?: ConnectionPolicy[P] }; /** An optional parameter that represents the consistency level. * It can take any value from {@link ConsistencyLevel}. */ diff --git a/src/auth.ts b/src/auth.ts index 5294572..25e9b97 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -80,7 +80,7 @@ export class AuthHandler { headers: IHeaders, masterKey: string ) { - const key = new Buffer(masterKey, "base64"); + const key = Buffer.from(masterKey, "base64"); const text = (verb || "").toLowerCase() + @@ -94,7 +94,7 @@ export class AuthHandler { ((headers["date"] as string) || "").toLowerCase() + "\n"; - const body = new Buffer(text, "utf8"); + const body = Buffer.from(text, "utf8"); const signature = crypto .createHmac("sha256", key) .update(body) diff --git a/src/common/helper.ts b/src/common/helper.ts index 170fbbf..be12c46 100644 --- a/src/common/helper.ts +++ b/src/common/helper.ts @@ -1,5 +1,6 @@ import { Constants } from "."; import { IHeaders } from ".."; +import { ConnectionPolicy } from "../documents"; import { RequestContext } from "../request/RequestContext"; /** @hidden */ @@ -308,4 +309,20 @@ export class Helper { return pathSegments[pathSegments.length - 1]; } + + public static parseConnectionPolicy(policy: any): ConnectionPolicy { + if (!policy) { + return new ConnectionPolicy(); + } else if (policy instanceof ConnectionPolicy) { + return policy; + } else { + const connectionPolicy = new ConnectionPolicy(); + for (const key of Object.getOwnPropertyNames(connectionPolicy)) { + if ((policy as any)[key] !== undefined) { + (connectionPolicy as any)[key] = (policy as any)[key]; + } + } + return connectionPolicy; + } + } } diff --git a/src/common/platform.ts b/src/common/platform.ts index 05b3c26..e52428f 100644 --- a/src/common/platform.ts +++ b/src/common/platform.ts @@ -11,7 +11,7 @@ export class Platform { } public static getDecodedDataLength(encodedData: string): number { - const buffer = new Buffer(encodedData, "base64"); + const buffer = Buffer.from(encodedData, "base64"); return buffer.length; } diff --git a/src/common/resourceId.ts b/src/common/resourceId.ts index 57ad32f..8bef0c7 100644 --- a/src/common/resourceId.ts +++ b/src/common/resourceId.ts @@ -218,7 +218,7 @@ export class ResourceId { } public static fromBase64String(s: string) { - return new Buffer(s.replace("-", "/"), "base64"); + return Buffer.from(s.replace("-", "/"), "base64"); } public static toBase64String(buffer: Buffer) { @@ -354,8 +354,7 @@ export class ResourceId { len = len + 4; } - const buffer = new Buffer(len); - buffer.fill(0); + const buffer = Buffer.alloc(len); // Does not require fill(0) if (this.offer !== EMPTY) { buffer.writeIntLE(Number(this.offer), 0, this.offerIdLength); diff --git a/src/documents/ConnectionPolicy.ts b/src/documents/ConnectionPolicy.ts index 6ed13bc..1ec7837 100644 --- a/src/documents/ConnectionPolicy.ts +++ b/src/documents/ConnectionPolicy.ts @@ -1,5 +1,4 @@ import { ConnectionMode, MediaReadMode } from "."; -import { Constants } from ".."; import { RetryOptions } from "../retry"; /** * Represents the Connection policy associated with a CosmosClient in the Azure Cosmos DB database service. diff --git a/src/request/RequestHandler.ts b/src/request/RequestHandler.ts index 59790a1..8e28915 100644 --- a/src/request/RequestHandler.ts +++ b/src/request/RequestHandler.ts @@ -73,7 +73,7 @@ export class RequestHandler { // it is a stream stream = body; } else if (typeof body === "string") { - buffer = new Buffer(body, "utf8"); + buffer = Buffer.from(body, "utf8"); } else { return { result: { diff --git a/src/test/functional/client.spec.ts b/src/test/functional/client.spec.ts index ccdfd6b..72e1613 100644 --- a/src/test/functional/client.spec.ts +++ b/src/test/functional/client.spec.ts @@ -5,9 +5,6 @@ import { getTestDatabase, removeAllDatabases } from "../common/TestHelpers"; describe("NodeJS CRUD Tests", function() { this.timeout(process.env.MOCHA_TIMEOUT || 20000); - beforeEach(async function() { - await removeAllDatabases(); - }); // TODO: disabled tests need to get fixed or deleted describe("Validate client request timeout", function() { @@ -26,4 +23,17 @@ describe("NodeJS CRUD Tests", function() { } }); }); + + describe("Constructor", function() { + it("Should work with a non-class based Connection Policy", function() { + const client = new CosmosClient({ + endpoint: "https://faaaaaake.com", + auth: { masterKey: "" }, + connectionPolicy: { + RequestTimeout: 10000 + } + }); + assert.ok(client !== undefined, "client shouldn't be undefined if it succeeded"); + }); + }); });