@iprokit/service
Version:
Powering distributed systems with simplicity and speed.
164 lines • 4.51 kB
JavaScript
"use strict";
/**
* @iProKit/Service
* Copyright (c) 2019-2025 Rutvik Katuri / iProTechs
* SPDX-License-Identifier: Apache-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Pod is a communication unit used during multicasting
* and represents a network entity.
* It is case-sensitive and formatted as a string.
*
* Example: `ID*A0001$ONE=1&TWO=2`
*
* A Pod consists of an identifier, session, and attributes:
* - Identifier: `ID` (Unique identifier)
* - Session: `A0001` (Session token)
* - Attributes: `ONE=1&TWO=2`
*/
class Pod {
/**
* Unique identifier of the pod.
*/
identifier;
/**
* Session token of the pod.
*/
session;
/**
* Attributes of the pod.
*/
attributes;
/**
* Creates an instance of `Pod`.
*
* @param identifier unique identifier of the pod.
* @param session optional session token of the pod.
* @param attributes optional attributes of the pod.
*/
constructor(identifier, session, attributes) {
this.identifier = identifier;
this.session = session;
this.attributes = attributes ?? {};
}
//////////////////////////////
//////// Gets/Sets
//////////////////////////////
/**
* Gets an attribute value.
*
* @param key attribute key.
*/
get(key) {
return this.attributes[key];
}
/**
* Returns `true` if the attribute exists, `false` otherwise.
*
* @param key attribute key.
*/
has(key) {
return key in this.attributes;
}
/**
* Sets an attribute.
*
* @param key attribute key.
* @param value attribute value.
*/
set(key, value) {
this.attributes[key] = value;
return this;
}
/**
* Removes an attribute.
*
* @param key attribute key.
*/
delete(key) {
delete this.attributes[key];
return this;
}
/**
* Returns an array of attribute keys.
*/
keys() {
return Object.keys(this.attributes);
}
/**
* Returns an array of attribute values.
*/
values() {
return Object.values(this.attributes);
}
/**
* Returns an array of key-value pairs of attributes.
*/
entries() {
return Object.entries(this.attributes);
}
/**
* Returns the number of attributes.
*/
get size() {
return this.keys().length;
}
//////////////////////////////
//////// To/From Helpers
//////////////////////////////
/**
* Returns the stringified version of the `Pod`.
*/
stringify() {
// Combine attributes as a string.
const _attributes = this.entries()
.map(([key, value]) => key + Pod.ATTRIBUTE_DELIMITER + value)
.join(Pod.ATTRIBUTES_DELIMITER);
const attributes = _attributes ? Pod.SESSION_DELIMITER + _attributes : '';
// Return combined identifier, session, and attributes as a string.
return this.identifier + Pod.IDENTIFIER_DELIMITER + this.session + attributes;
}
/**
* Returns the objectified version of a `Pod`.
*
* @param pod stringified version of a `Pod`.
*/
static objectify(pod) {
// Deconstruct identifier, session, and attributes from the string.
const [identifier, _pod] = pod.split(Pod.IDENTIFIER_DELIMITER);
const [session, _attributes] = _pod.split(Pod.SESSION_DELIMITER);
const attributes = _attributes ? Object.fromEntries(_attributes.split(Pod.ATTRIBUTES_DELIMITER).map((attribute) => attribute.split(Pod.ATTRIBUTE_DELIMITER))) : {};
// Return new Pod as an object.
return new Pod(identifier, session, attributes);
}
//////////////////////////////
//////// Delimiter Definitions
//////////////////////////////
/**
* Delimiter for identifier, denoted by `*`.
*
* @example `identifier*session`
*/
static IDENTIFIER_DELIMITER = '*';
/**
* Delimiter for session, denoted by `$`.
*
* @example `session$attributes`
*/
static SESSION_DELIMITER = '$';
/**
* Delimiter for attributes, denoted by `&`.
*
* @example `attribute1&attribute2`
*/
static ATTRIBUTES_DELIMITER = '&';
/**
* Delimiter for a key-value pair, denoted by `=`.
*
* @example `key=value`
*/
static ATTRIBUTE_DELIMITER = '=';
}
exports.default = Pod;
//# sourceMappingURL=pod.js.map