diff --git a/lib/connection.js b/lib/connection.js index 3148f6b..8c95f78 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1,8 +1,9 @@ 'use strict'; const net = require('net'); -const { uuid, serialization } = require('./common'); -const collectMessage = require('./chunkProcessor'); const { EventEmitter } = require('stream'); +const { uuid, serialization, WrappedError } = require('./common'); +const collectMessage = require('./chunkProcessor'); +const { EVENTS, ERRORS } = require('./constants'); class Connection extends EventEmitter { #connections = new Map(); @@ -21,8 +22,6 @@ class Connection extends EventEmitter { const connectionId = uuid(); socket.setNoDelay(false); socket.on('close', () => this.#deleteConnection(connectionId)); - socket.on('end', () => console.log('end')); - socket.on('timeout', () => console.log('timeout')); socket.on('error', error => this.#errorHandler(error)); socket .pipe(collectMessage()) @@ -63,8 +62,8 @@ class Connection extends EventEmitter { if (targetNode) this.connect(targetNode); } - #errorHandler(err) { - console.log(err); + #errorHandler(error) { + this.emit(EVENTS.ERROR, new WrappedError(ERRORS.SOCKET_ERROR, error)); } } diff --git a/lib/constants.js b/lib/constants.js index 47762aa..634ff29 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,36 +1,48 @@ +const TTL = 300; + const CHECK_INTERVAL = 500; const TIMEOUT_DURATION = 2 * 60000; const TIME_CLEAN_MSG = 30 * 60000; -const TTL = 300; +const DEFAULT_TIMEOUT = 10000; +const TWO_DAYS = 2 * 24 * 60 * 60 * 1000; + +const ERROR = 'ERROR'; const HANDSHAKE = 'HANDSHAKE'; const DM = 'MESSAGE'; const CRYPTO_DM = 'CRYPTO_DM'; -const ERROR = 'ERROR'; const PUBLIC_KEY_REQUEST = 'PUBLIC_KEY_REQUEST'; const PUBLIC_KEY_RESPONSE = 'PUBLIC_KEY_RESPONSE'; + const TIMEOUT_ERROR_MESSAGE = `Neighbor check timed out after ${TIMEOUT_DURATION / 1000} seconds`; const TIMEOUT_ERROR_REQUEST = 'RSA key retrieval timed out.'; const DECRYPT_ERROR = 'Unable to decrypt the message.'; const SEND_ERROR = 'Failed to send the data.'; const PUBLISH_ERROR = 'Failed to publish the data.'; -const DEFAULT_TIMEOUT = 10000; -const TWO_DAYS = 2 * 24 * 60 * 60 * 1000; -module.exports = { - PUBLIC_KEY_REQUEST, - PUBLIC_KEY_RESPONSE, - HANDSHAKE, - DEFAULT_TIMEOUT, - TIMEOUT_ERROR_REQUEST, - TIME_CLEAN_MSG, - CHECK_INTERVAL, - TIMEOUT_DURATION, - TIMEOUT_ERROR_MESSAGE, +const SOCKET_ERROR = 'An error occurred with the socket connection.'; + +const CONSTANTS = { + TIMEOUTS: { + CHECK_INTERVAL, + TIMEOUT_DURATION, + TIME_CLEAN_MSG, + DEFAULT_TIMEOUT, + TWO_DAYS, + }, + EVENTS: { DM, CRYPTO_DM, ERROR }, + MESSAGES: { + HANDSHAKE, + PUBLIC_KEY_REQUEST, + PUBLIC_KEY_RESPONSE, + }, + ERRORS: { + TIMEOUT_ERROR_MESSAGE, + TIMEOUT_ERROR_REQUEST, + DECRYPT_ERROR, + SEND_ERROR, + PUBLISH_ERROR, + SOCKET_ERROR, + }, TTL, - CRYPTO_DM, - DM, - TWO_DAYS, - ERROR, - SEND_ERROR, - DECRYPT_ERROR, - PUBLISH_ERROR, }; + +module.exports = CONSTANTS; diff --git a/lib/cryptoRsa.js b/lib/cryptoRsa.js index ed108f4..d63199d 100644 --- a/lib/cryptoRsa.js +++ b/lib/cryptoRsa.js @@ -3,17 +3,7 @@ const crypto = require('crypto'); const EventEmitter = require('events'); const Connection = require('./connection'); const { createMessage, uuid, WrappedError } = require('./common'); -const { - PUBLIC_KEY_RESPONSE, - TTL, - PUBLIC_KEY_REQUEST, - DEFAULT_TIMEOUT, - CRYPTO_DM, - ERROR, - TIMEOUT_ERROR_REQUEST, - DECRYPT_ERROR, - TWO_DAYS, -} = require('./constants'); +const { TIMEOUTS, MESSAGES, ERRORS, TTL, EVENTS } = require('./constants'); class CryptoRSA extends Connection { publicKey = ''; @@ -32,21 +22,20 @@ class CryptoRSA extends Connection { if (nodePublic) return resolve(nodePublic.publicKey); const messageId = uuid(); const timerId = setTimeout( - () => reject(new Error(TIMEOUT_ERROR_REQUEST)), - DEFAULT_TIMEOUT, + () => reject(new Error(ERRORS.TIMEOUT_ERROR_REQUEST)), + TIMEOUTS.DEFAULT_TIMEOUT, ); this.#responseEmitter.once(messageId, key => { clearTimeout(timerId); resolve(key); }); const options = createMessage( - PUBLIC_KEY_REQUEST, + MESSAGES.PUBLIC_KEY_REQUEST, targetNode, this.nodeId, messageId, TTL, ); - console.log(options); this._broadcast(options); }); } @@ -66,26 +55,29 @@ class CryptoRSA extends Connection { return this._nodePublicKeys.delete(nodeId); } - [CRYPTO_DM](_, messageData) { - const processedMessage = this._processMessage(messageData, CRYPTO_DM); + [EVENTS.CRYPTO_DM](_, messageData) { + const processedMessage = this._processMessage( + messageData, + EVENTS.CRYPTO_DM, + ); if (processedMessage) { try { const message = this.#decryptMessage(processedMessage.message); - this.emit(CRYPTO_DM, { + this.emit(EVENTS.CRYPTO_DM, { ...processedMessage, message: JSON.parse(message), }); } catch (error) { this.emit( - ERROR, - new WrappedError(DECRYPT_ERROR, error), + EVENTS.ERROR, + new WrappedError(ERRORS.DECRYPT_ERROR, error), processedMessage, ); } } } - clearPublicKeys(dayInMillis = TWO_DAYS) { + clearPublicKeys(dayInMillis = TIMEOUTS.TWO_DAYS) { const now = Date.now(); this._nodePublicKeys.forEach(({ timestamp }, key) => { const timestampMillis = new Date(timestamp).getTime(); @@ -99,7 +91,7 @@ class CryptoRSA extends Connection { } #timerClearKeys() { - setInterval(this.clearPublicKeys.bind(this), TWO_DAYS); + setInterval(this.clearPublicKeys.bind(this), TIMEOUTS.TWO_DAYS); } #decryptMessage(message) { @@ -135,10 +127,10 @@ class CryptoRSA extends Connection { this.#privateKey = privateKey; } - [PUBLIC_KEY_REQUEST](_, messageData) { + [MESSAGES.PUBLIC_KEY_REQUEST](_, messageData) { const processedMessage = this._processMessage( messageData, - PUBLIC_KEY_REQUEST, + MESSAGES.PUBLIC_KEY_REQUEST, ); if (!processedMessage) return; const fromNode = processedMessage.fromNode; @@ -148,7 +140,7 @@ class CryptoRSA extends Connection { publicKey: this.publicKey, }; const options = createMessage( - PUBLIC_KEY_RESPONSE, + MESSAGES.PUBLIC_KEY_RESPONSE, fromNode, this.nodeId, messageId, @@ -158,10 +150,10 @@ class CryptoRSA extends Connection { this._broadcast(options); } - [PUBLIC_KEY_RESPONSE](_, messageData) { + [MESSAGES.PUBLIC_KEY_RESPONSE](_, messageData) { const processedMessage = this._processMessage( messageData, - PUBLIC_KEY_RESPONSE, + MESSAGES.PUBLIC_KEY_RESPONSE, ); if (processedMessage) { const { messageId, publicKey } = messageData.message; diff --git a/lib/messaging.js b/lib/messaging.js index fa8af54..d291078 100644 --- a/lib/messaging.js +++ b/lib/messaging.js @@ -1,19 +1,7 @@ 'use strict'; const CryptoRSA = require('./cryptoRsa'); const { createData, uuid, createMessage } = require('./common'); -const { - HANDSHAKE, - DM, - CHECK_INTERVAL, - TIMEOUT_DURATION, - TIMEOUT_ERROR_MESSAGE, - TTL, - TIME_CLEAN_MSG, - CRYPTO_DM, - ERROR, - SEND_ERROR, - PUBLISH_ERROR, -} = require('./constants'); +const { TIMEOUTS, MESSAGES, ERRORS, TTL, EVENTS } = require('./constants'); const WrappedError = require('./common/errorWrapper'); class Messaging extends CryptoRSA { @@ -30,7 +18,7 @@ class Messaging extends CryptoRSA { try { await this.#neighborCheck(); const messageForm = createMessage( - DM, + EVENTS.DM, targetNode, this.#NODE_ID, uuid(), @@ -39,8 +27,8 @@ class Messaging extends CryptoRSA { ); this._broadcast(messageForm); } catch (error) { - const wrappedError = new WrappedError(PUBLISH_ERROR, error); - this.emit(ERROR, wrappedError); + const wrappedError = new WrappedError(ERRORS.PUBLISH_ERROR, error); + this.emit(EVENTS.ERROR, wrappedError); throw wrappedError; } } @@ -51,7 +39,7 @@ class Messaging extends CryptoRSA { const publicKey = await this._requestPublicKey(targetNode); const cryptMessage = this._generateCryptoMessage(message, publicKey); const messageForm = createMessage( - CRYPTO_DM, + EVENTS.CRYPTO_DM, targetNode, this.#NODE_ID, uuid(), @@ -60,8 +48,8 @@ class Messaging extends CryptoRSA { ); this._broadcast(messageForm); } catch (error) { - const wrappedError = new WrappedError(SEND_ERROR, error); - this.emit(ERROR, wrappedError); + const wrappedError = new WrappedError(ERRORS.SEND_ERROR, error); + this.emit(EVENTS.ERROR, wrappedError); throw wrappedError; } } @@ -79,7 +67,7 @@ class Messaging extends CryptoRSA { } _newConnection(connectionId) { - this._publish(connectionId, createData(HANDSHAKE, this.#NODE_ID)); + this._publish(connectionId, createData(MESSAGES.HANDSHAKE, this.#NODE_ID)); } _onMessage(connectionId, messageData) { @@ -119,8 +107,8 @@ class Messaging extends CryptoRSA { return new Promise((resolve, reject) => { if (this.neighbors.size > 0) return resolve(); const timer = setTimeout( - () => reject(new Error(TIMEOUT_ERROR_MESSAGE)), - TIMEOUT_DURATION, + () => reject(new Error(ERRORS.TIMEOUT_ERROR_MESSAGE)), + TIMEOUTS.TIMEOUT_DURATION, ); const interval = setInterval(() => { if (this.neighbors.size > 0) { @@ -128,7 +116,7 @@ class Messaging extends CryptoRSA { clearInterval(interval); resolve(); } - }, CHECK_INTERVAL); + }, TIMEOUTS.CHECK_INTERVAL); }); } @@ -146,17 +134,17 @@ class Messaging extends CryptoRSA { } #cleanSeenMessages() { - setInterval(() => this.#seenMessages.clear(), TIME_CLEAN_MSG); + setInterval(() => this.#seenMessages.clear(), TIMEOUTS.TIME_CLEAN_MSG); } - [HANDSHAKE](connectionId, messageData) { + [MESSAGES.HANDSHAKE](connectionId, messageData) { const { message } = messageData; this.#addNeighbor(connectionId, message); } - [DM](_, messageData) { - const processedMessage = this._processMessage(messageData, DM); - if (processedMessage) this.emit(DM, processedMessage); + [EVENTS.DM](_, messageData) { + const processedMessage = this._processMessage(messageData, EVENTS.DM); + if (processedMessage) this.emit(EVENTS.DM, processedMessage); } }