@iprokit/service
Version:
Powering distributed systems with simplicity and speed.
121 lines (120 loc) • 3 kB
TypeScript
/**
* @iProKit/Service
* Copyright (c) 2019-2025 Rutvik Katuri / iProTechs
* SPDX-License-Identifier: Apache-2.0
*/
/**
* 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`
*/
export default class Pod {
/**
* Unique identifier of the pod.
*/
readonly identifier: string;
/**
* Session token of the pod.
*/
session: string;
/**
* Attributes of the pod.
*/
readonly attributes: 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: string, session: string, attributes?: Attributes);
/**
* Gets an attribute value.
*
* @param key attribute key.
*/
get(key: string): string | undefined;
/**
* Returns `true` if the attribute exists, `false` otherwise.
*
* @param key attribute key.
*/
has(key: string): boolean;
/**
* Sets an attribute.
*
* @param key attribute key.
* @param value attribute value.
*/
set(key: string, value: string): this;
/**
* Removes an attribute.
*
* @param key attribute key.
*/
delete(key: string): this;
/**
* Returns an array of attribute keys.
*/
keys(): string[];
/**
* Returns an array of attribute values.
*/
values(): (string | undefined)[];
/**
* Returns an array of key-value pairs of attributes.
*/
entries(): [string, string | undefined][];
/**
* Returns the number of attributes.
*/
get size(): number;
/**
* Returns the stringified version of the `Pod`.
*/
stringify(): string;
/**
* Returns the objectified version of a `Pod`.
*
* @param pod stringified version of a `Pod`.
*/
static objectify(pod: string): Pod;
/**
* Delimiter for identifier, denoted by `*`.
*
* @example `identifier*session`
*/
static readonly IDENTIFIER_DELIMITER = "*";
/**
* Delimiter for session, denoted by `$`.
*
* @example `session$attributes`
*/
static readonly SESSION_DELIMITER = "$";
/**
* Delimiter for attributes, denoted by `&`.
*
* @example `attribute1&attribute2`
*/
static readonly ATTRIBUTES_DELIMITER = "&";
/**
* Delimiter for a key-value pair, denoted by `=`.
*
* @example `key=value`
*/
static readonly ATTRIBUTE_DELIMITER = "=";
}
/**
* Attributes associated with a `Pod`.
*/
export interface Attributes {
[key: string]: string | undefined;
}