All files / src/command/dappServer utils.js

100% Statements 17/17
100% Branches 9/9
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43          6x 25x 25x 2x   25x               6x 9x 9x 9x   9x               6x 4x 4x 1x   3x 3x 3x        
/**
 * Serializes a message object to a string.
 * @param {any} data - The message object to serialize.
 * @returns {string} The serialized message as a string.
 */
const serializeMessage = data => {
  let result = JSON.stringify(data);
  if (data === null || data === undefined) {
    result = '';
  }
  return Buffer.from(encodeURIComponent(result)).toString('base64');
};
 
/**
 * Deserializes a string to a message object.
 * @param {string} str - The string to deserialize.
 * @returns {any} The deserialized message object.
 */
const deserializeMessage = str => {
  let result = decodeURIComponent(Buffer.from(str, 'base64').toString());
  try {
    result = JSON.parse(result);
  } catch (e) {}
  return result;
};
/**
 * Checks if the given timestamp is within the specified time buffer from the current time.
 * @param {string} time - The timestamp to check, either as a number or a string.
 * @param {number} [timeBuffer=300] - The time buffer in seconds. Default is 300 seconds.
 * @returns {boolean} True if the timestamp is within the time buffer, false otherwise.
 */
const checkTimestamp = (time, timeBuffer = 4 * 60) => {
  const checkTime = parseInt(time, 10);
  if (!checkTime) {
    return false;
  }
  const now = Math.ceil(new Date().getTime() / 1000);
  const diff = now - checkTime;
  return diff >= 0 && diff <= timeBuffer;
};
 
export { serializeMessage, deserializeMessage, checkTimestamp };