Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.

Commit 1837d8d

Browse files
author
Paul Korzhyk
authored
Merge pull request #27 from shaneu/feature/adding-tls-support
Feature/adding tls support
2 parents e53d999 + a2c286a commit 1837d8d

File tree

6 files changed

+94
-37
lines changed

6 files changed

+94
-37
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ you need to `login` again on a periodic basis:
116116
await clientStub.login();
117117
```
118118

119+
### Create https connection
120+
121+
If your cluster is using tls/mtls you can pass a node `https.Agent` configured with you
122+
certificates as follows:
123+
124+
```js
125+
const https = require('https');
126+
const fs = require('fs');
127+
// read your certificates
128+
const cert = fs.readFileSync('./certs/client.crt', 'utf8');
129+
const ca = fs.readFileSync('./certs/ca.crt', 'utf8');
130+
const key = fs.readFileSync('./certs/client.key', 'utf8');
131+
132+
// create your https.Agent
133+
const agent = https.Agent({
134+
cert,
135+
ca,
136+
key,
137+
})
138+
139+
const clientStub = new dgraph.DgraphClientStub('https://localhost:8080', false, {agent});
140+
const dgraphClient = new dgraph.DgraphClient(clientStub);
141+
```
142+
119143

120144
### Alter the database
121145

lib/clientStub.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { Assigned, Mutation, Operation, Payload, Request, Response, TxnContext, UiKeywords } from "./types";
1+
import { Assigned, Mutation, Operation, Options, Payload, Request, Response, TxnContext, UiKeywords } from "./types";
22
export declare class DgraphClientStub {
33
private readonly addr;
4+
private readonly options;
45
private legacyApi;
56
private accessToken;
67
private refreshToken;
78
private autoRefresh;
89
private autoRefreshTimer?;
9-
constructor(addr?: string, legacyApi?: boolean);
10+
constructor(addr?: string, legacyApi?: boolean, options?: Options);
1011
detectApiVersion(): Promise<string>;
1112
alter(op: Operation): Promise<Payload>;
1213
query(req: Request): Promise<Response>;

lib/clientStub.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
"use strict";
2+
var __assign = (this && this.__assign) || function () {
3+
__assign = Object.assign || function(t) {
4+
for (var s, i = 1, n = arguments.length; i < n; i++) {
5+
s = arguments[i];
6+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7+
t[p] = s[p];
8+
}
9+
return t;
10+
};
11+
return __assign.apply(this, arguments);
12+
};
213
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
314
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
415
return new (P || (P = Promise))(function (resolve, reject) {
@@ -41,13 +52,19 @@ var jwt = require("jsonwebtoken");
4152
var errors_1 = require("./errors");
4253
var AUTO_REFRESH_PREFETCH_TIME = 5000;
4354
var DgraphClientStub = (function () {
44-
function DgraphClientStub(addr, legacyApi) {
55+
function DgraphClientStub(addr, legacyApi, options) {
4556
if (addr === undefined) {
4657
this.addr = "http://localhost:8080";
4758
}
4859
else {
4960
this.addr = addr;
5061
}
62+
if (options === undefined) {
63+
this.options = {};
64+
}
65+
else {
66+
this.options = options;
67+
}
5168
this.legacyApi = !!legacyApi;
5269
}
5370
DgraphClientStub.prototype.detectApiVersion = function () {
@@ -82,10 +99,7 @@ var DgraphClientStub = (function () {
8299
else {
83100
return Promise.reject("Invalid op argument in alter");
84101
}
85-
return this.callAPI("alter", {
86-
method: "POST",
87-
body: body,
88-
});
102+
return this.callAPI("alter", __assign(__assign({}, this.options), { method: "POST", body: body }));
89103
};
90104
DgraphClientStub.prototype.query = function (req) {
91105
var headers = {};
@@ -155,11 +169,7 @@ var DgraphClientStub = (function () {
155169
.join("&");
156170
}
157171
}
158-
return this.callAPI(url, {
159-
method: "POST",
160-
body: req.query,
161-
headers: headers,
162-
});
172+
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST", body: req.query, headers: headers }));
163173
};
164174
DgraphClientStub.prototype.mutate = function (mu) {
165175
var body;
@@ -222,11 +232,8 @@ var DgraphClientStub = (function () {
222232
headers["X-Dgraph-CommitNow"] = "true";
223233
}
224234
}
225-
return this.callAPI(url, {
226-
method: "POST",
227-
body: body,
228-
headers: headers,
229-
});
235+
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST", body: body,
236+
headers: headers }));
230237
};
231238
DgraphClientStub.prototype.commit = function (ctx) {
232239
var body;
@@ -239,16 +246,13 @@ var DgraphClientStub = (function () {
239246
var url = !this.legacyApi
240247
? "commit?startTs=" + ctx.start_ts
241248
: "commit/" + ctx.start_ts;
242-
return this.callAPI(url, {
243-
method: "POST",
244-
body: body,
245-
});
249+
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST", body: body }));
246250
};
247251
DgraphClientStub.prototype.abort = function (ctx) {
248252
var url = !this.legacyApi
249253
? "commit?startTs=" + ctx.start_ts + "&abort=true"
250254
: "/abort/" + ctx.start_ts;
251-
return this.callAPI(url, { method: "POST" });
255+
return this.callAPI(url, __assign(__assign({}, this.options), { method: "POST" }));
252256
};
253257
DgraphClientStub.prototype.login = function (userid, password, refreshToken) {
254258
return __awaiter(this, void 0, void 0, function () {
@@ -300,24 +304,22 @@ var DgraphClientStub = (function () {
300304
DgraphClientStub.prototype.fetchUiKeywords = function () {
301305
return __awaiter(this, void 0, void 0, function () {
302306
return __generator(this, function (_a) {
303-
return [2, this.callAPI("ui/keywords", {})];
307+
return [2, this.callAPI("ui/keywords", this.options)];
304308
});
305309
});
306310
};
307311
DgraphClientStub.prototype.getHealth = function (all) {
308312
if (all === void 0) { all = false; }
309313
return __awaiter(this, void 0, void 0, function () {
310314
return __generator(this, function (_a) {
311-
return [2, this.callAPI("health" + (all ? "?all" : ""), {
312-
acceptRawText: true,
313-
})];
315+
return [2, this.callAPI("health" + (all ? "?all" : ""), __assign(__assign({}, this.options), { acceptRawText: true }))];
314316
});
315317
});
316318
};
317319
DgraphClientStub.prototype.getState = function () {
318320
return __awaiter(this, void 0, void 0, function () {
319321
return __generator(this, function (_a) {
320-
return [2, this.callAPI("state", {})];
322+
return [2, this.callAPI("state", this.options)];
321323
});
322324
});
323325
};

lib/types.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// <reference types="node" />
2+
import * as https from "https";
13
export interface Operation {
24
schema?: string | null;
35
dropAttr?: string | null;
@@ -76,3 +78,10 @@ export interface TxnOptions {
7678
export interface ErrorNonJson extends Error {
7779
responseText?: string;
7880
}
81+
export interface Options extends https.RequestOptions {
82+
agent?: https.Agent;
83+
}
84+
export interface Config extends Options {
85+
acceptRawText?: boolean;
86+
body?: string;
87+
}

src/clientStub.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import * as jwt from "jsonwebtoken";
44
import { APIError, APIResultError } from "./errors";
55
import {
66
Assigned,
7+
Config,
78
ErrorNonJson,
89
LoginResponse,
910
Mutation,
1011
Operation,
12+
Options,
1113
Payload,
1214
Request,
1315
Response,
@@ -23,18 +25,26 @@ const AUTO_REFRESH_PREFETCH_TIME = 5000;
2325
*/
2426
export class DgraphClientStub {
2527
private readonly addr: string;
28+
private readonly options: Options;
2629
private legacyApi: boolean;
2730
private accessToken: string;
2831
private refreshToken: string;
2932
private autoRefresh: boolean;
3033
private autoRefreshTimer?: number;
3134

32-
constructor(addr?: string, legacyApi?: boolean) {
35+
constructor(addr?: string, legacyApi?: boolean, options?: Options) {
3336
if (addr === undefined) {
3437
this.addr = "http://localhost:8080"; // tslint:disable-line no-http-string
3538
} else {
3639
this.addr = addr;
3740
}
41+
42+
if (options === undefined) {
43+
this.options = {};
44+
} else {
45+
this.options = options;
46+
}
47+
3848
this.legacyApi = !!legacyApi;
3949
}
4050

@@ -62,6 +72,7 @@ export class DgraphClientStub {
6272
}
6373

6474
return this.callAPI("alter", {
75+
...this.options,
6576
method: "POST",
6677
body,
6778
});
@@ -142,6 +153,7 @@ export class DgraphClientStub {
142153
}
143154

144155
return this.callAPI(url, {
156+
...this.options,
145157
method: "POST",
146158
body: req.query,
147159
headers,
@@ -224,6 +236,7 @@ export class DgraphClientStub {
224236
}
225237

226238
return this.callAPI(url, {
239+
...this.options,
227240
method: "POST",
228241
body,
229242
headers,
@@ -243,6 +256,7 @@ export class DgraphClientStub {
243256
: `commit/${ctx.start_ts}`;
244257

245258
return this.callAPI(url, {
259+
...this.options,
246260
method: "POST",
247261
body,
248262
});
@@ -253,7 +267,7 @@ export class DgraphClientStub {
253267
? `commit?startTs=${ctx.start_ts}&abort=true`
254268
: `/abort/${ctx.start_ts}`;
255269

256-
return this.callAPI(url, { method: "POST" });
270+
return this.callAPI(url, { ...this.options, method: "POST" });
257271
}
258272

259273
public async login(
@@ -307,14 +321,15 @@ export class DgraphClientStub {
307321
}
308322

309323
public async fetchUiKeywords(): Promise<UiKeywords> {
310-
return this.callAPI("ui/keywords", {});
324+
return this.callAPI("ui/keywords", this.options);
311325
}
312326

313327
/**
314328
* Gets instance or cluster health, based on the all param
315329
*/
316330
public async getHealth(all: boolean = false): Promise<Response> {
317331
return this.callAPI(`health${all ? "?all" : ""}`, {
332+
...this.options,
318333
acceptRawText: true,
319334
});
320335
}
@@ -323,7 +338,7 @@ export class DgraphClientStub {
323338
* Gets the state of the cluster
324339
*/
325340
public async getState(): Promise<Response> {
326-
return this.callAPI("state", {});
341+
return this.callAPI("state", this.options);
327342
}
328343

329344
public setAutoRefresh(val: boolean) {
@@ -369,12 +384,7 @@ export class DgraphClientStub {
369384

370385
private async callAPI<T>(
371386
path: string,
372-
config: {
373-
acceptRawText?: boolean;
374-
method?: string;
375-
body?: string;
376-
headers?: { [k: string]: string };
377-
},
387+
config: Config,
378388
): Promise<T> {
379389
const url = this.getURL(path);
380390
if (this.accessToken !== undefined && path !== "login") {

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as https from "https";
2+
13
export interface Operation {
24
schema?: string | null;
35
dropAttr?: string | null;
@@ -85,3 +87,12 @@ export interface TxnOptions {
8587
export interface ErrorNonJson extends Error {
8688
responseText?: string;
8789
}
90+
91+
export interface Options extends https.RequestOptions {
92+
agent?: https.Agent;
93+
}
94+
95+
export interface Config extends Options {
96+
acceptRawText?: boolean;
97+
body?: string;
98+
}

0 commit comments

Comments
 (0)