@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
69 lines (68 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.strToUint8 = exports.uint8ToStr = exports.deserializeObject = exports.serializeObject = void 0;
/**
* Helper function to convert objects to Uint8Array
* @param {Record<string, any>} object - The object to serialize
* @returns {Uint8Array} object serialized to `Uint8Array`
*/
function serializeObject(object) {
if (object === null || typeof object !== 'object') {
throw new TypeError('Input must be a non-null object.');
}
const encoder = new TextEncoder();
const parsed = JSON.stringify(object);
return encoder.encode(parsed);
}
exports.serializeObject = serializeObject;
/**
* Helper function to convert Uint8Array to objects
* @param {Uint8Array} data - The data to deserialize
* @returns {Record<string, any>} parsed JSON object
*/
function deserializeObject(data) {
if (!(data instanceof Uint8Array)) {
throw new TypeError('Input must be a Uint8Array.');
}
const decoder = new TextDecoder();
const decodedData = decoder.decode(data);
if (decodedData.trim() === '') {
return {};
}
try {
return JSON.parse(decodedData);
}
catch (error) {
if (error instanceof SyntaxError) {
throw new SyntaxError('Failed to parse JSON: ' + error.message);
}
else {
throw new Error('An unexpected error occurred: ' + String(error));
}
}
}
exports.deserializeObject = deserializeObject;
/**
* Convert Uint8Array to a string
* @param {Uint8Array} arr - The array to convert
* @returns {string} The resulting string
*/
function uint8ToStr(arr) {
if (!(arr instanceof Uint8Array)) {
throw new TypeError('Input must be a Uint8Array.');
}
return new TextDecoder().decode(arr);
}
exports.uint8ToStr = uint8ToStr;
/**
* Convert a string to Uint8Array
* @param {string} text - The text to convert
* @returns {Uint8Array} The resulting Uint8Array
*/
function strToUint8(text) {
if (typeof text !== 'string') {
throw new TypeError('Input must be a string.');
}
return new TextEncoder().encode(text);
}
exports.strToUint8 = strToUint8;