buffer updates & connection policy fixes (#135)

* Update buffer to Buffer methods

* Adds support for non-class based ConnectionPolicy

fixes 115
This commit is contained in:
Christopher Anderson 2018-09-22 11:49:32 -07:00 коммит произвёл GitHub
Родитель db499e49c3
Коммит ed8c317472
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 41 добавлений и 16 удалений

Просмотреть файл

@ -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,

Просмотреть файл

@ -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";

Просмотреть файл

@ -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}.
*/

Просмотреть файл

@ -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)

Просмотреть файл

@ -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;
}
}
}

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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);

Просмотреть файл

@ -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.

Просмотреть файл

@ -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: {

Просмотреть файл

@ -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");
});
});
});