(value: Buffer)
| 34 | } |
| 35 | |
| 36 | public decode(value: Buffer): object | string | null | Buffer { |
| 37 | if (isNil(value)) { |
| 38 | return null; |
| 39 | } |
| 40 | // A value with the "leading zero byte" indicates the schema payload. |
| 41 | // The "content" is possibly binary and should not be touched & parsed. |
| 42 | if ( |
| 43 | Buffer.isBuffer(value) && |
| 44 | value.length > 0 && |
| 45 | value.readUInt8(0) === 0 |
| 46 | ) { |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | let result = value.toString(); |
| 51 | const startChar = result.charAt(0); |
| 52 | |
| 53 | // Only try to parse objects and arrays |
| 54 | if (startChar === '{' || startChar === '[') { |
| 55 | try { |
| 56 | result = JSON.parse(value.toString()); |
| 57 | } catch (e) { |
| 58 | // Do nothing |
| 59 | } |
| 60 | } |
| 61 | return result; |
| 62 | } |
| 63 | } |
no test coverage detected