|
| 1 | +// Copyright (c) 2025 Mahmoud Alghalayini. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Check if a value is a TypedArray |
| 19 | + * @param {*} value - The value to check |
| 20 | + * @returns {boolean} True if the value is a TypedArray |
| 21 | + */ |
| 22 | +function isTypedArray(value) { |
| 23 | + return ArrayBuffer.isView(value) && !(value instanceof DataView); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Check if a value needs JSON conversion (BigInt, functions, etc.) |
| 28 | + * @param {*} value - The value to check |
| 29 | + * @returns {boolean} True if the value needs special JSON handling |
| 30 | + */ |
| 31 | +function needsJSONConversion(value) { |
| 32 | + return ( |
| 33 | + typeof value === 'bigint' || |
| 34 | + typeof value === 'function' || |
| 35 | + typeof value === 'undefined' || |
| 36 | + value === Infinity || |
| 37 | + value === -Infinity || |
| 38 | + (typeof value === 'number' && isNaN(value)) |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Convert a message to plain arrays (TypedArray -> regular Array) |
| 44 | + * @param {*} obj - The object to convert |
| 45 | + * @returns {*} The converted object with plain arrays |
| 46 | + */ |
| 47 | +function toPlainArrays(obj) { |
| 48 | + if (obj === null || obj === undefined) { |
| 49 | + return obj; |
| 50 | + } |
| 51 | + |
| 52 | + if (isTypedArray(obj)) { |
| 53 | + return Array.from(obj); |
| 54 | + } |
| 55 | + |
| 56 | + if (Array.isArray(obj)) { |
| 57 | + return obj.map((item) => toPlainArrays(item)); |
| 58 | + } |
| 59 | + |
| 60 | + if (typeof obj === 'object' && obj !== null) { |
| 61 | + const result = {}; |
| 62 | + for (const key in obj) { |
| 63 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 64 | + result[key] = toPlainArrays(obj[key]); |
| 65 | + } |
| 66 | + } |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + return obj; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Convert a message to be fully JSON-safe |
| 75 | + * @param {*} obj - The object to convert |
| 76 | + * @returns {*} The JSON-safe converted object |
| 77 | + */ |
| 78 | +function toJSONSafe(obj) { |
| 79 | + if (obj === null || obj === undefined) { |
| 80 | + return obj; |
| 81 | + } |
| 82 | + |
| 83 | + if (isTypedArray(obj)) { |
| 84 | + return Array.from(obj).map((item) => toJSONSafe(item)); |
| 85 | + } |
| 86 | + |
| 87 | + if (needsJSONConversion(obj)) { |
| 88 | + if (typeof obj === 'bigint') { |
| 89 | + // Convert BigInt to string with 'n' suffix to indicate it was a BigInt |
| 90 | + return obj.toString() + 'n'; |
| 91 | + } |
| 92 | + if (obj === Infinity) return 'Infinity'; |
| 93 | + if (obj === -Infinity) return '-Infinity'; |
| 94 | + if (typeof obj === 'number' && isNaN(obj)) return 'NaN'; |
| 95 | + if (typeof obj === 'undefined') return null; |
| 96 | + if (typeof obj === 'function') return '[Function]'; |
| 97 | + } |
| 98 | + |
| 99 | + if (Array.isArray(obj)) { |
| 100 | + return obj.map((item) => toJSONSafe(item)); |
| 101 | + } |
| 102 | + |
| 103 | + if (typeof obj === 'object' && obj !== null) { |
| 104 | + const result = {}; |
| 105 | + for (const key in obj) { |
| 106 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { |
| 107 | + result[key] = toJSONSafe(obj[key]); |
| 108 | + } |
| 109 | + } |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | + return obj; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Convert a message to a JSON string |
| 118 | + * @param {*} obj - The object to convert |
| 119 | + * @param {number} [space] - Space parameter for JSON.stringify formatting |
| 120 | + * @returns {string} The JSON string representation |
| 121 | + */ |
| 122 | +function toJSONString(obj, space) { |
| 123 | + const jsonSafeObj = toJSONSafe(obj); |
| 124 | + return JSON.stringify(jsonSafeObj, null, space); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Apply serialization mode conversion to a message object |
| 129 | + * @param {*} message - The message object to convert |
| 130 | + * @param {string} serializationMode - The serialization mode ('default', 'plain', 'json') |
| 131 | + * @returns {*} The converted message |
| 132 | + */ |
| 133 | +function applySerializationMode(message, serializationMode) { |
| 134 | + switch (serializationMode) { |
| 135 | + case 'default': |
| 136 | + // No conversion needed - use native rclnodejs behavior |
| 137 | + return message; |
| 138 | + |
| 139 | + case 'plain': |
| 140 | + // Convert TypedArrays to regular arrays |
| 141 | + return toPlainArrays(message); |
| 142 | + |
| 143 | + case 'json': |
| 144 | + // Convert to fully JSON-safe format |
| 145 | + return toJSONSafe(message); |
| 146 | + |
| 147 | + default: |
| 148 | + throw new TypeError( |
| 149 | + `Invalid serializationMode: ${serializationMode}. Valid modes are: 'default', 'plain', 'json'` |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Validate serialization mode |
| 156 | + * @param {string} mode - The serialization mode to validate |
| 157 | + * @returns {boolean} True if valid |
| 158 | + */ |
| 159 | +function isValidSerializationMode(mode) { |
| 160 | + return ['default', 'plain', 'json'].includes(mode); |
| 161 | +} |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + isTypedArray, |
| 165 | + needsJSONConversion, |
| 166 | + toPlainArrays, |
| 167 | + toJSONSafe, |
| 168 | + toJSONString, |
| 169 | + applySerializationMode, |
| 170 | + isValidSerializationMode, |
| 171 | +}; |
0 commit comments