From 3bf4b92ff313f31d05671c7b6348384695fbc197 Mon Sep 17 00:00:00 2001 From: Nicolas Dextraze Date: Wed, 9 Mar 2016 15:37:54 -0800 Subject: [PATCH] Moved eventStoreConnection in it's onw file --- index.js | 2 +- src/client.js | 50 +++++++++++++++++++++++ src/eventStoreConnection.js | 68 ++++--------------------------- src/systemData/userCredentials.js | 21 +++++++--- test/client_test.js | 6 +-- 5 files changed, 77 insertions(+), 70 deletions(-) create mode 100644 src/client.js diff --git a/index.js b/index.js index 2b9205e..51e39cb 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/src/client.js b/src/client.js new file mode 100644 index 0000000..0ff0c30 --- /dev/null +++ b/src/client.js @@ -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; \ No newline at end of file diff --git a/src/eventStoreConnection.js b/src/eventStoreConnection.js index 8b7a472..518e088 100644 --- a/src/eventStoreConnection.js +++ b/src/eventStoreConnection.js @@ -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); }; \ No newline at end of file diff --git a/src/systemData/userCredentials.js b/src/systemData/userCredentials.js index 7035911..0e6a6bd 100644 --- a/src/systemData/userCredentials.js +++ b/src/systemData/userCredentials.js @@ -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; \ No newline at end of file diff --git a/test/client_test.js b/test/client_test.js index e4ca3e1..4d6fa35 100644 --- a/test/client_test.js +++ b/test/client_test.js @@ -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()