| 20 | } |
| 21 | |
| 22 | function mimeMatch(expected: string | boolean, actual: string | boolean) { |
| 23 | // invalid type |
| 24 | if (expected === false) return false |
| 25 | |
| 26 | // split types |
| 27 | const actualParts = (actual as string).split('/') |
| 28 | const expectedParts = (expected as string).split('/') |
| 29 | |
| 30 | // invalid format |
| 31 | if (actualParts.length !== 2 || expectedParts.length !== 2) return false |
| 32 | |
| 33 | // validate type |
| 34 | if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) return false |
| 35 | |
| 36 | // validate suffix wildcard |
| 37 | if (expectedParts[1].substr(0, 2) === '*+') |
| 38 | return ( |
| 39 | expectedParts[1].length <= actualParts[1].length + 1 && |
| 40 | expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length) |
| 41 | ) |
| 42 | |
| 43 | // validate subtype |
| 44 | if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) return false |
| 45 | |
| 46 | return true |
| 47 | } |
| 48 | |
| 49 | function normalize(type: string | unknown) { |
| 50 | // invalid type |