| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | export function getIODescription(value: mixed): string { |
| 11 | if (!__DEV__) { |
| 12 | return ''; |
| 13 | } |
| 14 | try { |
| 15 | switch (typeof value) { |
| 16 | case 'function': |
| 17 | return value.name || ''; |
| 18 | case 'object': |
| 19 | // Test the object for a bunch of common property names that are useful identifiers. |
| 20 | // While we only have the return value here, it should ideally be a name that |
| 21 | // describes the arguments requested. |
| 22 | if (value === null) { |
| 23 | return ''; |
| 24 | } else if (value instanceof Error) { |
| 25 | // eslint-disable-next-line react-internal/safe-string-coercion |
| 26 | return String(value.message); |
| 27 | } else if (typeof value.url === 'string') { |
| 28 | return value.url; |
| 29 | } else if (typeof value.href === 'string') { |
| 30 | return value.href; |
| 31 | } else if (typeof value.src === 'string') { |
| 32 | return value.src; |
| 33 | } else if (typeof value.currentSrc === 'string') { |
| 34 | return value.currentSrc; |
| 35 | } else if (typeof value.command === 'string') { |
| 36 | return value.command; |
| 37 | } else if ( |
| 38 | typeof value.request === 'object' && |
| 39 | value.request !== null && |
| 40 | typeof value.request.url === 'string' |
| 41 | ) { |
| 42 | return value.request.url; |
| 43 | } else if ( |
| 44 | typeof value.response === 'object' && |
| 45 | value.response !== null && |
| 46 | typeof value.response.url === 'string' |
| 47 | ) { |
| 48 | return value.response.url; |
| 49 | } else if ( |
| 50 | typeof value.id === 'string' || |
| 51 | typeof value.id === 'number' || |
| 52 | typeof value.id === 'bigint' |
| 53 | ) { |
| 54 | // eslint-disable-next-line react-internal/safe-string-coercion |
| 55 | return String(value.id); |
| 56 | } else if (typeof value.name === 'string') { |
| 57 | return value.name; |
| 58 | } else { |
| 59 | const str = value.toString(); |
| 60 | if ( |
| 61 | str.startsWith('[object ') || |
| 62 | str.length < 5 || |
| 63 | str.length > 500 |
| 64 | ) { |
| 65 | // This is probably not a useful description. |
| 66 | return ''; |
| 67 | } |
| 68 | return str; |
| 69 | } |
| 70 | case 'string': |
| 71 | if (value.length < 5 || value.length > 500) { |
| 72 | return ''; |
| 73 | } |
| 74 | return value; |
| 75 | case 'number': |
| 76 | case 'bigint': |
| 77 | // eslint-disable-next-line react-internal/safe-string-coercion |
| 78 | return String(value); |
| 79 | default: |
| 80 | // Not useful descriptors. |
| 81 | return ''; |
| 82 | } |
| 83 | } catch (x) { |
| 84 | return ''; |
| 85 | } |
| 86 | } |