* @param {array} dataLocations - all data locations * @param {array} outerRange - range from request, where outerRange[1] * is the inclusive end byte boundary * @return {array} parsedLocations - dataLocations filtered for * what needed and ranges added for particular parts as needed
(dataLocations, outerRange)
| 6 | * what needed and ranges added for particular parts as needed |
| 7 | */ |
| 8 | function setPartRanges(dataLocations, outerRange) { |
| 9 | const parsedLocations = []; |
| 10 | |
| 11 | if (!outerRange) { |
| 12 | return dataLocations.slice(); |
| 13 | } |
| 14 | |
| 15 | const begin = outerRange[0]; |
| 16 | const end = outerRange[1]; |
| 17 | // If have single location, do not need to break up range among parts |
| 18 | // and might not have a start and size property |
| 19 | // on the dataLocation (because might be pre- md-model-version 2), |
| 20 | // so just set range as property |
| 21 | if (dataLocations.length === 1) { |
| 22 | const soleLocation = dataLocations[0]; |
| 23 | soleLocation.range = [begin, end]; |
| 24 | // If missing size, does not impact get range. |
| 25 | // We modify size here in case this function is used for |
| 26 | // object put part copy where will need size. |
| 27 | // If pre-md-model-version 2, object put part copy will not |
| 28 | // be allowed, so not an issue that size not modified here. |
| 29 | if (dataLocations[0].size) { |
| 30 | const partSize = parseInt(dataLocations[0].size, 10); |
| 31 | soleLocation.size = |
| 32 | Math.min(partSize, end - begin + 1).toString(); |
| 33 | } |
| 34 | parsedLocations.push(soleLocation); |
| 35 | return parsedLocations; |
| 36 | } |
| 37 | // Range is inclusive of endpoint so need plus 1 |
| 38 | const max = end - begin + 1; |
| 39 | let total = 0; |
| 40 | for (let i = 0; i < dataLocations.length; i++) { |
| 41 | if (total >= max) { |
| 42 | break; |
| 43 | } |
| 44 | const partStart = parseInt(dataLocations[i].start, 10); |
| 45 | const partSize = parseInt(dataLocations[i].size, 10); |
| 46 | if (partStart + partSize <= begin) { |
| 47 | continue; |
| 48 | } |
| 49 | if (partStart >= begin) { |
| 50 | // If the whole part is in the range, just include it |
| 51 | if (partSize + total <= max) { |
| 52 | const partWithoutRange = dataLocations[i]; |
| 53 | partWithoutRange.size = partSize.toString(); |
| 54 | parsedLocations.push(partWithoutRange); |
| 55 | total += partSize; |
| 56 | // Otherwise set a range limit on the part end |
| 57 | // and we're done |
| 58 | } else { |
| 59 | const partWithRange = dataLocations[i]; |
| 60 | // Need to subtract one from endPart since range |
| 61 | // includes endPart in byte count |
| 62 | const endPart = Math.min(partSize - 1, max - total - 1); |
| 63 | partWithRange.range = [0, endPart]; |
| 64 | // modify size to be stored for object put part copy |
| 65 | partWithRange.size = (endPart + 1).toString(); |
no outgoing calls
no test coverage detected