• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RobotWebTools / rclnodejs / 19690312781

26 Nov 2025 02:15AM UTC coverage: 82.843% (+1.1%) from 81.767%
19690312781

push

github

minggangw
Pump to 1.7.0 (#1329)

1074 of 1420 branches covered (75.63%)

Branch coverage included in aggregate %.

2446 of 2829 relevant lines covered (86.46%)

488.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.34
/lib/message_serialization.js
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
const { ValidationError } = require('./errors.js');
26✔
18

19
/**
20
 * Check if a value is a TypedArray
21
 * @param {*} value - The value to check
22
 * @returns {boolean} True if the value is a TypedArray
23
 */
24
function isTypedArray(value) {
25
  return ArrayBuffer.isView(value) && !(value instanceof DataView);
24✔
26
}
27

28
/**
29
 * Check if a value needs JSON conversion (BigInt, functions, etc.)
30
 * @param {*} value - The value to check
31
 * @returns {boolean} True if the value needs special JSON handling
32
 */
33
function needsJSONConversion(value) {
34
  return (
21✔
35
    typeof value === 'bigint' ||
130✔
36
    typeof value === 'function' ||
37
    typeof value === 'undefined' ||
38
    value === Infinity ||
39
    value === -Infinity ||
40
    (typeof value === 'number' && isNaN(value))
41
  );
42
}
43

44
/**
45
 * Convert a message to plain arrays (TypedArray -> regular Array)
46
 * @param {*} obj - The object to convert
47
 * @returns {*} The converted object with plain arrays
48
 */
49
function toPlainArrays(obj) {
50
  if (obj === null || obj === undefined) {
×
51
    return obj;
×
52
  }
53

54
  if (isTypedArray(obj)) {
×
55
    return Array.from(obj);
×
56
  }
57

58
  if (Array.isArray(obj)) {
×
59
    return obj.map((item) => toPlainArrays(item));
×
60
  }
61

62
  if (typeof obj === 'object' && obj !== null) {
×
63
    const result = {};
×
64
    for (const key in obj) {
×
65
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
×
66
        result[key] = toPlainArrays(obj[key]);
×
67
      }
68
    }
69
    return result;
×
70
  }
71

72
  return obj;
×
73
}
74

75
/**
76
 * Convert a message to be fully JSON-safe
77
 * @param {*} obj - The object to convert
78
 * @returns {*} The JSON-safe converted object
79
 */
80
function toJSONSafe(obj) {
81
  if (obj === null || obj === undefined) {
24!
82
    return obj;
×
83
  }
84

85
  if (isTypedArray(obj)) {
24✔
86
    return Array.from(obj).map((item) => toJSONSafe(item));
8✔
87
  }
88

89
  if (needsJSONConversion(obj)) {
21✔
90
    if (typeof obj === 'bigint') {
4✔
91
      // Convert BigInt to string with 'n' suffix to indicate it was a BigInt
92
      return obj.toString() + 'n';
1✔
93
    }
94
    if (obj === Infinity) return 'Infinity';
3✔
95
    if (obj === -Infinity) return '-Infinity';
2✔
96
    if (typeof obj === 'number' && isNaN(obj)) return 'NaN';
1!
97
    if (typeof obj === 'undefined') return null;
×
98
    if (typeof obj === 'function') return '[Function]';
×
99
  }
100

101
  if (Array.isArray(obj)) {
17✔
102
    return obj.map((item) => toJSONSafe(item));
3✔
103
  }
104

105
  if (typeof obj === 'object' && obj !== null) {
16✔
106
    const result = {};
4✔
107
    for (const key in obj) {
4✔
108
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
10!
109
        result[key] = toJSONSafe(obj[key]);
10✔
110
      }
111
    }
112
    return result;
4✔
113
  }
114

115
  return obj;
12✔
116
}
117

118
/**
119
 * Convert a message to a JSON string
120
 * @param {*} obj - The object to convert
121
 * @param {number} [space] - Space parameter for JSON.stringify formatting
122
 * @returns {string} The JSON string representation
123
 */
124
function toJSONString(obj, space) {
125
  const jsonSafeObj = toJSONSafe(obj);
1✔
126
  return JSON.stringify(jsonSafeObj, null, space);
1✔
127
}
128

129
/**
130
 * Apply serialization mode conversion to a message object
131
 * @param {*} message - The message object to convert
132
 * @param {string} serializationMode - The serialization mode ('default', 'plain', 'json')
133
 * @returns {*} The converted message
134
 */
135
function applySerializationMode(message, serializationMode) {
136
  switch (serializationMode) {
×
137
    case 'default':
138
      // No conversion needed - use native rclnodejs behavior
139
      return message;
×
140

141
    case 'plain':
142
      // Convert TypedArrays to regular arrays
143
      return toPlainArrays(message);
×
144

145
    case 'json':
146
      // Convert to fully JSON-safe format
147
      return toJSONSafe(message);
×
148

149
    default:
150
      throw new ValidationError(
×
151
        `Invalid serializationMode: ${serializationMode}. Valid modes are: 'default', 'plain', 'json'`,
152
        {
153
          code: 'INVALID_SERIALIZATION_MODE',
154
          argumentName: 'serializationMode',
155
          providedValue: serializationMode,
156
          expectedType: "'default' | 'plain' | 'json'",
157
        }
158
      );
159
  }
160
}
161

162
/**
163
 * Validate serialization mode
164
 * @param {string} mode - The serialization mode to validate
165
 * @returns {boolean} True if valid
166
 */
167
function isValidSerializationMode(mode) {
168
  return ['default', 'plain', 'json'].includes(mode);
16✔
169
}
170

171
module.exports = {
26✔
172
  isTypedArray,
173
  needsJSONConversion,
174
  toPlainArrays,
175
  toJSONSafe,
176
  toJSONString,
177
  applySerializationMode,
178
  isValidSerializationMode,
179
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc