* @see https://fetch.spec.whatwg.org/#simple-range-header-value * @param {string} value * @param {boolean} allowWhitespace * @return {RangeHeaderValue|'failure'}
(value, allowWhitespace)
| 1077 | * @return {RangeHeaderValue|'failure'} |
| 1078 | */ |
| 1079 | function simpleRangeHeaderValue (value, allowWhitespace) { |
| 1080 | // 1. Let data be the isomorphic decoding of value. |
| 1081 | // Note: isomorphic decoding takes a sequence of bytes (ie. a Uint8Array) and turns it into a string, |
| 1082 | // nothing more. We obviously don't need to do that if value is a string already. |
| 1083 | const data = value |
| 1084 | |
| 1085 | // 2. If data does not start with "bytes", then return failure. |
| 1086 | if (!data.startsWith('bytes')) { |
| 1087 | return 'failure' |
| 1088 | } |
| 1089 | |
| 1090 | // 3. Let position be a position variable for data, initially pointing at the 5th code point of data. |
| 1091 | const position = { position: 5 } |
| 1092 | |
| 1093 | // 4. If allowWhitespace is true, collect a sequence of code points that are HTTP tab or space, |
| 1094 | // from data given position. |
| 1095 | if (allowWhitespace) { |
| 1096 | collectASequenceOfCodePoints( |
| 1097 | (char) => char === '\t' || char === ' ', |
| 1098 | data, |
| 1099 | position |
| 1100 | ) |
| 1101 | } |
| 1102 | |
| 1103 | // 5. If the code point at position within data is not U+003D (=), then return failure. |
| 1104 | if (data.charCodeAt(position.position) !== 0x3D) { |
| 1105 | return 'failure' |
| 1106 | } |
| 1107 | |
| 1108 | // 6. Advance position by 1. |
| 1109 | position.position++ |
| 1110 | |
| 1111 | // 7. If allowWhitespace is true, collect a sequence of code points that are HTTP tab or space, from |
| 1112 | // data given position. |
| 1113 | if (allowWhitespace) { |
| 1114 | collectASequenceOfCodePoints( |
| 1115 | (char) => char === '\t' || char === ' ', |
| 1116 | data, |
| 1117 | position |
| 1118 | ) |
| 1119 | } |
| 1120 | |
| 1121 | // 8. Let rangeStart be the result of collecting a sequence of code points that are ASCII digits, |
| 1122 | // from data given position. |
| 1123 | const rangeStart = collectASequenceOfCodePoints( |
| 1124 | (char) => { |
| 1125 | const code = char.charCodeAt(0) |
| 1126 | |
| 1127 | return code >= 0x30 && code <= 0x39 |
| 1128 | }, |
| 1129 | data, |
| 1130 | position |
| 1131 | ) |
| 1132 | |
| 1133 | // 9. Let rangeStartValue be rangeStart, interpreted as decimal number, if rangeStart is not the |
| 1134 | // empty string; otherwise null. |
| 1135 | const rangeStartValue = rangeStart.length ? Number(rangeStart) : null |
| 1136 |
no test coverage detected