Skip to content

Commit

Permalink
refactor: constants structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DIY0R committed Dec 5, 2024
1 parent 810c897 commit dbd9431
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 76 deletions.
54 changes: 33 additions & 21 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 19 additions & 27 deletions lib/cryptoRsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand All @@ -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);
});
}
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand Down
44 changes: 16 additions & 28 deletions lib/messaging.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -30,7 +18,7 @@ class Messaging extends CryptoRSA {
try {
await this.#neighborCheck();
const messageForm = createMessage(
DM,
EVENTS.DM,
targetNode,
this.#NODE_ID,
uuid(),
Expand All @@ -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;
}
}
Expand All @@ -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(),
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -119,16 +107,16 @@ 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) {
clearTimeout(timer);
clearInterval(interval);
resolve();
}
}, CHECK_INTERVAL);
}, TIMEOUTS.CHECK_INTERVAL);
});
}

Expand All @@ -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);
}
}

Expand Down

0 comments on commit dbd9431

Please sign in to comment.