Support https for projections

This commit is contained in:
Josh Wulf 2019-12-11 02:47:41 +10:00
parent 7da398eae8
commit 6b75d241fc

View File

@ -1,8 +1,8 @@
const https = require("https"); const http = require('http');
const http = require("http"); const https = require('https');
const url = require("url"); const url = require('url');
const util = require("util"); const util = require('util');
const ProjectionCommandFailedError = require("../errors/projectionCommandFailedError"); const ProjectionCommandFailedError = require('../errors/projectionCommandFailedError');
const HTTP_OK = 200; const HTTP_OK = 200;
const HTTP_CREATED = 201; const HTTP_CREATED = 201;
@ -10,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;
} }
} }
@ -20,324 +20,133 @@ function ProjectionsClient(log, operationTimeout) {
this._operationTimeout = operationTimeout; this._operationTimeout = operationTimeout;
} }
ProjectionsClient.prototype.enable = function( ProjectionsClient.prototype.enable = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projection/' + name + '/command/enable', '', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/enable",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.disable = function( ProjectionsClient.prototype.disable = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projection/' + name + '/command/disable', '', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/disable",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.abort = function( ProjectionsClient.prototype.abort = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projection/' + name + '/command/abort', '', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/abort",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.reset = function( ProjectionsClient.prototype.reset = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projection/' + name + '/command/reset', '', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projection/" + name + "/command/reset",
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.createOneTime = function( ProjectionsClient.prototype.createOneTime = function(httpEndPoint, query, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projections/onetime?type=JS', query, userCredentials, HTTP_CREATED);
query,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projections/onetime?type=JS",
query,
userCredentials,
HTTP_CREATED
);
}; };
ProjectionsClient.prototype.createTransient = function( ProjectionsClient.prototype.createTransient = function(httpEndPoint, name, query, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projections/transient?name=' + name + '&type=JS', query, userCredentials, HTTP_CREATED);
name,
query,
userCredentials
) {
return this.sendPost(
httpEndPoint + "/projections/transient?name=" + name + "&type=JS",
query,
userCredentials,
HTTP_CREATED
);
}; };
ProjectionsClient.prototype.createContinuous = function( ProjectionsClient.prototype.createContinuous = function(httpEndPoint, name, query, trackEmittedStreams, userCredentials) {
httpEndPoint, return this.sendPost(httpEndPoint + '/projections/continuous?name=' + name + '&type=JS&emit=1&trackemittedstreams=' + trackEmittedStreams, query, userCredentials, HTTP_CREATED);
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( return this.sendGet(httpEndPoint + '/projections/any', userCredentials, HTTP_OK)
httpEndPoint + "/projections/any", .then(function (json) {
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( ProjectionsClient.prototype.listOneTime = function(httpEndPoint, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projections/onetime', userCredentials, HTTP_OK)
userCredentials .then(function (json) {
) {
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( ProjectionsClient.prototype.listContinuous = function(httpEndPoint, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projections/continuous', userCredentials, HTTP_OK)
userCredentials .then(function (json) {
) {
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( ProjectionsClient.prototype.getStatus = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name, userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getState = function( ProjectionsClient.prototype.getState = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/state', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/state",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getPartitionState = function( ProjectionsClient.prototype.getPartitionState = function(httpEndPoint, name, partitionId, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/state?partition=' + partitionId, userCredentials, HTTP_OK);
name,
partitionId,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/state?partition=" + partitionId,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getResult = function( ProjectionsClient.prototype.getResult = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/result', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/result",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getPartitionResult = function( ProjectionsClient.prototype.getPartitionResult = function(httpEndPoint, name, partitionId, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/result?partition=' + partitionId, userCredentials, HTTP_OK);
name,
partitionId,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/result?partition=" + partitionId,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getStatistics = function( ProjectionsClient.prototype.getStatistics = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/statistics', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/statistics",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.getQuery = function( ProjectionsClient.prototype.getQuery = function(httpEndPoint, name, userCredentials) {
httpEndPoint, return this.sendGet(httpEndPoint + '/projection/' + name + '/query', userCredentials, HTTP_OK);
name,
userCredentials
) {
return this.sendGet(
httpEndPoint + "/projection/" + name + "/query",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.updateQuery = function( ProjectionsClient.prototype.updateQuery = function(httpEndPoint, name, query, userCredentials) {
httpEndPoint, return this.sendPut(httpEndPoint + '/projection/' + name + '/query?type=JS', query, userCredentials, HTTP_OK);
name,
query,
userCredentials
) {
return this.sendPut(
httpEndPoint + "/projection/" + name + "/query?type=JS",
query,
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.delete = function( ProjectionsClient.prototype.delete = function(httpEndPoint, name, deleteEmittedStreams, deleteStateStream, deleteCheckpointStream, userCredentials) {
httpEndPoint, return this.sendDelete(httpEndPoint + '/projection/' + name + '?deleteStateStream=' + deleteStateStream + '&deleteCheckpointStream=' + deleteCheckpointStream + '&deleteEmittedStreams=' + deleteEmittedStreams, '', userCredentials, HTTP_OK);
name,
deleteEmittedStreams,
deleteStateStream,
deleteCheckpointStream,
userCredentials
) {
return this.sendDelete(
httpEndPoint +
"/projection/" +
name +
"?deleteStateStream=" +
deleteStateStream +
"&deleteCheckpointStream=" +
deleteCheckpointStream +
"&deleteEmittedStreams=" +
deleteEmittedStreams,
"",
userCredentials,
HTTP_OK
);
}; };
ProjectionsClient.prototype.request = function( ProjectionsClient.prototype.request = function(method, _url, data, userCredentials, expectedCode) {
method,
_url,
data,
userCredentials,
expectedCode
) {
const options = url.parse(_url); const options = url.parse(_url);
const httplib = options.protocol === "https" ? https : http; 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( reject(new Error(util.format('Request timed out for %s on %s', method, _url)))
new Error(util.format("Request timed out for %s on %s", method, _url))
);
}, self._operationTimeout); }, self._operationTimeout);
const req = httplib.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( reject(new ProjectionCommandFailedError(
new ProjectionCommandFailedError(
res.statusCode, res.statusCode,
util.format( util.format('Server returned %d (%s) for %s on %s', res.statusCode, res.statusMessage, method, _url)
"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();
@ -346,45 +155,23 @@ ProjectionsClient.prototype.request = function(
function voidResult() {} function voidResult() {}
ProjectionsClient.prototype.sendGet = function( ProjectionsClient.prototype.sendGet = function(_url, userCredentials, expectedCode) {
_url, return this.request('GET', _url, null, userCredentials, expectedCode);
userCredentials,
expectedCode
) {
return this.request("GET", _url, null, userCredentials, expectedCode);
}; };
ProjectionsClient.prototype.sendPost = function( ProjectionsClient.prototype.sendPost = function(_url, data, userCredentials, expectedCode) {
_url, return this.request('POST', _url, data, userCredentials, expectedCode)
data, .then(voidResult);
userCredentials,
expectedCode
) {
return this.request("POST", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
ProjectionsClient.prototype.sendPut = function( ProjectionsClient.prototype.sendPut = function(_url, data, userCredentials, expectedCode) {
_url, return this.request('PUT', _url, data, userCredentials, expectedCode)
data, .then(voidResult);
userCredentials,
expectedCode
) {
return this.request("PUT", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
ProjectionsClient.prototype.sendDelete = function( ProjectionsClient.prototype.sendDelete = function(_url, data, userCredentials, expectedCode) {
_url, return this.request('DELETE', _url, data, userCredentials, expectedCode)
data, .then(voidResult);
userCredentials,
expectedCode
) {
return this.request("DELETE", _url, data, userCredentials, expectedCode).then(
voidResult
);
}; };
module.exports = ProjectionsClient; module.exports = ProjectionsClient;