Files
angular__angular/devtools/projects/ng-devtools-backend/src/lib/serialization-utils.ts
T
Matthieu Riegler d283595bfa fix(devtools): sanitize route data. (#62339)
The serialization of route data does not support cyclic data objects. We sanitize nested route data object by replacing invalid values with a placeholder string.

PR Close #62339
2025-06-30 09:31:18 +00:00

38 lines
1.1 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
export function sanitizeObject(obj: any): any {
if (obj === null || typeof obj !== 'object') {
return isPortSerializable(obj) ? obj : '[Non-serializable data]';
}
if (Array.isArray(obj)) {
return obj.map(sanitizeObject);
}
const result: Record<string, any> = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = isPortSerializable(value) ? value : '[Non-serializable data]';
}
return result;
}
// This is specific to chrome.runtime.Port which like JSON.stringify, cannot serialize cyclic objects
function isPortSerializable(value: any): boolean {
if (typeof value === 'function') {
return false; // Functions are not serializable but JSON.stringify doesn't throw, it strips them out
}
try {
JSON.stringify(value); // This mimics the runtime's limitations closely
return true;
} catch {
return false;
}
}