Support https for projections

This commit is contained in:
Josh Wulf 2019-12-11 02:39:37 +10:00
parent 195a6d40ce
commit 7da398eae8

View File

@ -1,7 +1,8 @@
const http = require('http'); const https = require("https");
const url = require('url'); const http = require("http");
const util = require('util'); const url = require("url");
const ProjectionCommandFailedError = require('../errors/projectionCommandFailedError'); const util = require("util");
const ProjectionCommandFailedError = require("../errors/projectionCommandFailedError");
const HTTP_OK = 200; const HTTP_OK = 200;
const HTTP_CREATED = 201; const HTTP_CREATED = 201;
@ -9,7 +10,7 @@ const HTTP_CREATED = 201;
function safeParseJson(json) { function safeParseJson(json) {
try { try {
return JSON.parse(json); return JSON.parse(json);
} catch(e) { } catch (e) {
return null; return null;
} }
} }
@ -19,132 +20,324 @@ function ProjectionsClient(log, operationTimeout) {
this._operationTimeout = operationTimeout; this._operationTimeout = operationTimeout;
} }
ProjectionsClient.prototype.enable = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.enable = function(
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/enable', '', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/enable",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.disable = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.disable = function(
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/disable', '', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/disable",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.abort = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.abort = function(
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/abort', '', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/abort",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.reset = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.reset = function(
return this.sendPost(httpEndPoint + '/projection/' + name + '/command/reset', '', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/reset",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.createOneTime = function(httpEndPoint, query, userCredentials) { ProjectionsClient.prototype.createOneTime = function(
return this.sendPost(httpEndPoint + '/projections/onetime?type=JS', query, userCredentials, HTTP_CREATED); httpEndPoint,
query,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projections/onetime?type=JS",
query,
userCredentials,
HTTP_CREATED
);
}; };
ProjectionsClient.prototype.createTransient = function(httpEndPoint, name, query, userCredentials) { ProjectionsClient.prototype.createTransient = function(
return this.sendPost(httpEndPoint + '/projections/transient?name=' + name + '&type=JS', query, userCredentials, HTTP_CREATED); httpEndPoint,
name,
query,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projections/transient?name=" + name + "&type=JS",
query,
userCredentials,
HTTP_CREATED
);
}; };
ProjectionsClient.prototype.createContinuous = function(httpEndPoint, name, query, trackEmittedStreams, userCredentials) { ProjectionsClient.prototype.createContinuous = function(
return this.sendPost(httpEndPoint + '/projections/continuous?name=' + name + '&type=JS&emit=1&trackemittedstreams=' + trackEmittedStreams, query, userCredentials, HTTP_CREATED); httpEndPoint,
name,
query,
trackEmittedStreams,
userCredentials
) {
return this.sendPost(
httpEndPoint +
"/projections/continuous?name=" +
name +
"&type=JS&emit=1&trackemittedstreams=" +
trackEmittedStreams,
query,
userCredentials,
HTTP_CREATED
);
}; };
ProjectionsClient.prototype.listAll = function(httpEndPoint, userCredentials) { ProjectionsClient.prototype.listAll = function(httpEndPoint, userCredentials) {
return this.sendGet(httpEndPoint + '/projections/any', userCredentials, HTTP_OK) return this.sendGet(
.then(function (json) { httpEndPoint + "/projections/any",
userCredentials,
HTTP_OK
).then(function(json) {
var r = safeParseJson(json); var r = safeParseJson(json);
if (r && r.projections) return r.projections; if (r && r.projections) return r.projections;
return null; return null;
}); });
}; };
ProjectionsClient.prototype.listOneTime = function(httpEndPoint, userCredentials) { ProjectionsClient.prototype.listOneTime = function(
return this.sendGet(httpEndPoint + '/projections/onetime', userCredentials, HTTP_OK) httpEndPoint,
.then(function (json) { userCredentials
) {
return this.sendGet(
httpEndPoint + "/projections/onetime",
userCredentials,
HTTP_OK
).then(function(json) {
var r = safeParseJson(json); var r = safeParseJson(json);
if (r && r.projections) return r.projections; if (r && r.projections) return r.projections;
return null; return null;
}); });
}; };
ProjectionsClient.prototype.listContinuous = function(httpEndPoint, userCredentials) { ProjectionsClient.prototype.listContinuous = function(
return this.sendGet(httpEndPoint + '/projections/continuous', userCredentials, HTTP_OK) httpEndPoint,
.then(function (json) { userCredentials
) {
return this.sendGet(
httpEndPoint + "/projections/continuous",
userCredentials,
HTTP_OK
).then(function(json) {
var r = safeParseJson(json); var r = safeParseJson(json);
if (r && r.projections) return r.projections; if (r && r.projections) return r.projections;
return null; return null;
}); });
}; };
ProjectionsClient.prototype.getStatus = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.getStatus = function(
return this.sendGet(httpEndPoint + '/projection/' + name, userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getState = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.getState = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/state', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/state",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getPartitionState = function(httpEndPoint, name, partitionId, userCredentials) { ProjectionsClient.prototype.getPartitionState = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/state?partition=' + partitionId, userCredentials, HTTP_OK); httpEndPoint,
name,
partitionId,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/state?partition=" + partitionId,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getResult = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.getResult = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/result', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/result",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getPartitionResult = function(httpEndPoint, name, partitionId, userCredentials) { ProjectionsClient.prototype.getPartitionResult = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/result?partition=' + partitionId, userCredentials, HTTP_OK); httpEndPoint,
name,
partitionId,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/result?partition=" + partitionId,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getStatistics = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.getStatistics = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/statistics', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/statistics",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getQuery = function(httpEndPoint, name, userCredentials) { ProjectionsClient.prototype.getQuery = function(
return this.sendGet(httpEndPoint + '/projection/' + name + '/query', userCredentials, HTTP_OK); httpEndPoint,
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/query",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.updateQuery = function(httpEndPoint, name, query, userCredentials) { ProjectionsClient.prototype.updateQuery = function(
return this.sendPut(httpEndPoint + '/projection/' + name + '/query?type=JS', query, userCredentials, HTTP_OK); httpEndPoint,
name,
query,
userCredentials
) {
return this.sendPut(
httpEndPoint + "/projection/" + name + "/query?type=JS",
query,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.delete = function(httpEndPoint, name, deleteEmittedStreams, deleteStateStream, deleteCheckpointStream, userCredentials) { ProjectionsClient.prototype.delete = function(
return this.sendDelete(httpEndPoint + '/projection/' + name + '?deleteStateStream=' + deleteStateStream + '&deleteCheckpointStream=' + deleteCheckpointStream + '&deleteEmittedStreams=' + deleteEmittedStreams, '', userCredentials, HTTP_OK); httpEndPoint,
name,
deleteEmittedStreams,
deleteStateStream,
deleteCheckpointStream,
userCredentials
) {
return this.sendDelete(
httpEndPoint +
"/projection/" +
name +
"?deleteStateStream=" +
deleteStateStream +
"&deleteCheckpointStream=" +
deleteCheckpointStream +
"&deleteEmittedStreams=" +
deleteEmittedStreams,
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.request = function(method, _url, data, userCredentials, expectedCode) { ProjectionsClient.prototype.request = function(
method,
_url,
data,
userCredentials,
expectedCode
) {
const options = url.parse(_url); const options = url.parse(_url);
const httplib = options.protocol === "https" ? https : http;
options.method = method; options.method = method;
if (userCredentials) { if (userCredentials) {
options.auth = [userCredentials.username, userCredentials.password].join(':'); options.auth = [userCredentials.username, userCredentials.password].join(
":"
);
} }
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function(resolve, reject) {
const timeout = setTimeout(function () { const timeout = setTimeout(function() {
reject(new Error(util.format('Request timed out for %s on %s', method, _url))) reject(
new Error(util.format("Request timed out for %s on %s", method, _url))
);
}, self._operationTimeout); }, self._operationTimeout);
const req = http.request(options, function (res) { const req = httplib.request(options, function(res) {
const hasExpectedCode = res.statusCode === expectedCode; const hasExpectedCode = res.statusCode === expectedCode;
var result = ''; var result = "";
res.setEncoding('utf8'); res.setEncoding("utf8");
res.on('data', function (chunk) { res.on("data", function(chunk) {
result += chunk; result += chunk;
}); });
res.on('end', function () { res.on("end", function() {
if (hasExpectedCode) { if (hasExpectedCode) {
clearTimeout(timeout); clearTimeout(timeout);
resolve(result); resolve(result);
} else { } else {
clearTimeout(timeout); clearTimeout(timeout);
reject(new ProjectionCommandFailedError( reject(
new ProjectionCommandFailedError(
res.statusCode, res.statusCode,
util.format('Server returned %d (%s) for %s on %s', res.statusCode, res.statusMessage, method, _url) util.format(
)); "Server returned %d (%s) for %s on %s",
res.statusCode,
res.statusMessage,
method,
_url
)
)
);
} }
}); });
}); });
req.on('error', reject); req.on("error", reject);
if (data) { if (data) {
req.setHeader('Content-Length', data.length); req.setHeader("Content-Length", data.length);
req.setHeader('Content-Type', 'application/json'); req.setHeader("Content-Type", "application/json");
req.write(data); req.write(data);
} }
req.end(); req.end();
@ -153,23 +346,45 @@ ProjectionsClient.prototype.request = function(method, _url, data, userCredentia
function voidResult() {} function voidResult() {}
ProjectionsClient.prototype.sendGet = function(_url, userCredentials, expectedCode) { ProjectionsClient.prototype.sendGet = function(
return this.request('GET', _url, null, userCredentials, expectedCode); _url,
userCredentials,
expectedCode
) {
return this.request("GET", _url, null, userCredentials, expectedCode);
}; };
ProjectionsClient.prototype.sendPost = function(_url, data, userCredentials, expectedCode) { ProjectionsClient.prototype.sendPost = function(
return this.request('POST', _url, data, userCredentials, expectedCode) _url,
.then(voidResult); data,
userCredentials,
expectedCode
) {
return this.request("POST", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
ProjectionsClient.prototype.sendPut = function(_url, data, userCredentials, expectedCode) { ProjectionsClient.prototype.sendPut = function(
return this.request('PUT', _url, data, userCredentials, expectedCode) _url,
.then(voidResult); data,
userCredentials,
expectedCode
) {
return this.request("PUT", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
ProjectionsClient.prototype.sendDelete = function(_url, data, userCredentials, expectedCode) { ProjectionsClient.prototype.sendDelete = function(
return this.request('DELETE', _url, data, userCredentials, expectedCode) _url,
.then(voidResult); data,
userCredentials,
expectedCode
) {
return this.request("DELETE", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
module.exports = ProjectionsClient; module.exports = ProjectionsClient;