Moved eventStoreConnection in it's onw file

This commit is contained in:
Nicolas Dextraze 2016-03-09 15:37:54 -08:00
parent b5ed21755d
commit 3bf4b92ff3
5 changed files with 77 additions and 70 deletions

View File

@ -8,4 +8,4 @@
* library is heavy on number of files so it could have negative impact on load time
* we need a compiled (single file) version of the library
*/
module.exports = require('./src/main.js');
module.exports = require('./src/client.js');

50
src/client.js Normal file
View File

@ -0,0 +1,50 @@
var EventData = require('./eventData');
var results = require('./results');
const expectedVersion = {
any: -2,
noStream: -1,
emptyStream: -1
};
const positions = {
start: new results.Position(0, 0),
end: new results.Position(-1, -1)
};
/**
* @param {string} eventId
* @param {object} data
* @param {object} [metadata]
* @param {string} [type]
* @returns {EventData}
*/
function jsonEventDataFactory(eventId, data, metadata, type) {
if (!data || typeof data !== 'object') throw new TypeError("data must be an object.");
var d = new Buffer(JSON.stringify(data));
var m = metadata ? new Buffer(JSON.stringify(metadata)) : null;
return new EventData(eventId, type || data.constructor.name, true, d, m);
}
/**
* @param {string} eventId
* @param {string} type
* @param {boolean} isJson
* @param {Buffer} data
* @param {Buffer} [metadata]
* @returns {EventData}
*/
function eventDataFactory(eventId, type, isJson, data, metadata) {
return new EventData(eventId, type, isJson, data, metadata);
}
module.exports.EventStoreConnection = require('./eventStoreConnection');
module.exports.UserCredentials = require('./systemData/userCredentials');
module.exports.EventData = EventData;
module.exports.expectedVersion = expectedVersion;
module.exports.positions = positions;
// Helper functions
module.exports.createConnection = module.exports.EventStoreConnection.create;
module.exports.createEventData = eventDataFactory;
module.exports.createJsonEventData = jsonEventDataFactory;

View File

@ -1,10 +1,6 @@
var uuid = require('uuid');
var EventStoreNodeConnection = require('./eventStoreNodeConnection');
var StaticEndpointDiscoverer = require('./core/staticEndpointDiscoverer');
var NoopLogger = require('./common/log/noopLogger');
var EventData = require('./eventData');
var results = require('./results');
var UserCredentials = require('./systemData/userCredentials');
var defaultConnectionSettings = {
log: new NoopLogger(),
@ -32,42 +28,6 @@ var defaultConnectionSettings = {
clientConnectionTimeout: 1000
};
const expectedVersion = {
any: -2,
noStream: -1,
emptyStream: -1
};
const positions = {
start: new results.Position(0, 0),
end: new results.Position(-1, -1)
};
/**
* @param {string} eventId
* @param {object} data
* @param {object} [metadata]
* @param {string} [type]
* @returns {EventData}
*/
function jsonEventDataFactory(eventId, data, metadata, type) {
if (!data || typeof data !== 'object') throw new TypeError("data must be an object.");
var d = new Buffer(JSON.stringify(data));
var m = metadata ? new Buffer(JSON.stringify(metadata)) : null;
return new EventData(eventId, type || data.constructor.name, true, d, m);
}
/**
* @param {string} eventId
* @param {string} type
* @param {boolean} isJson
* @param {Buffer} data
* @param {Buffer} [metadata]
* @returns {EventData}
*/
function eventDataFactory(eventId, type, isJson, data, metadata) {
return new EventData(eventId, type, isJson, data, metadata);
}
function merge(a,b) {
var c = {};
@ -82,28 +42,14 @@ function merge(a,b) {
/**
* Create an EventStore connection
* @param {object} tcpEndpoint
* @param {object} settings
* @param {object} tcpEndPoint
* @param {string} [connectionName]
* @returns {EventStoreNodeConnection}
*/
function connectionFactory(tcpEndpoint, settings) {
var mergedSettings = merge(defaultConnectionSettings, settings);
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndpoint, settings.useSslConnection);
var connectionName = null;
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName);
}
module.exports = connectionFactory;
module.exports.expectedVersion = expectedVersion;
module.exports.createEventData = eventDataFactory;
module.exports.createJsonEventData = jsonEventDataFactory;
module.exports.positions = positions;
/**
* @param {string} username
* @param {string} password
* @returns {UserCredentials}
*/
module.exports.createUserCredentials = function(username, password) {
return new UserCredentials(username, password);
module.exports.create = function(settings, tcpEndPoint, connectionName) {
//TODO: cluster connection
var mergedSettings = merge(defaultConnectionSettings, settings || {});
var endpointDiscoverer = new StaticEndpointDiscoverer(tcpEndPoint, settings.useSslConnection);
return new EventStoreNodeConnection(mergedSettings, endpointDiscoverer, connectionName || null);
};

View File

@ -1,9 +1,20 @@
function UserCredentials(username, password) {
if (!username || username === '') throw new TypeError("username must be a non-empty string.");
if (!password || password === '') throw new TypeError("password must be a non-empty string.");
var ensure = require('../common/utils/ensure');
this.username = username;
this.password = password;
/**
* @param {string} username
* @param {string} password
* @constructor
* @property {string} username
* @property {string} password
*/
function UserCredentials(username, password) {
ensure.notNullOrEmpty(username, 'username');
ensure.notNullOrEmpty(password, 'password');
Object.defineProperties(this, {
username: {enumerable: true, value: username},
password: {enumerable: true, value: password}
});
}
module.exports = UserCredentials;

View File

@ -1,7 +1,7 @@
var util = require('util');
var when = require('when');
var uuid = require('uuid');
var client = require('../src/main');
var client = require('../src/client');
var NoopLogger = require('../src/common/log/noopLogger');
var consoleLogger = {
@ -18,7 +18,7 @@ function createRandomEvent() {
}
var testStreamName = 'test-' + uuid.v4();
var userCredentialsForAll = client.createUserCredentials("admin", "changeit");
var userCredentialsForAll = new client.UserCredentials("admin", "changeit");
function testEvent(test, event, expectedVersion) {
if (!event) return;
@ -32,7 +32,7 @@ module.exports = {
var tcpEndPoint = {host: 'localhost', port: 1113};
var settings = {verboseLogging: false, log: new NoopLogger()};
//var settings = {verboseLogging: true, log: consoleLogger};
this.conn = client(tcpEndPoint, settings);
this.conn = client.EventStoreConnection.create(settings, tcpEndPoint);
this.connError = null;
var self = this;
this.conn.connect()