You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
920 B

  1. /**
  2. * Parse "Range" header `str` relative to the given file `size`.
  3. *
  4. * @param {Number} size
  5. * @param {String} str
  6. * @return {Array}
  7. * @api public
  8. */
  9. module.exports = function(size, str){
  10. var valid = true;
  11. var i = str.indexOf('=');
  12. if (-1 == i) return -2;
  13. var arr = str.slice(i + 1).split(',').map(function(range){
  14. var range = range.split('-')
  15. , start = parseInt(range[0], 10)
  16. , end = parseInt(range[1], 10);
  17. // -nnn
  18. if (isNaN(start)) {
  19. start = size - end;
  20. end = size - 1;
  21. // nnn-
  22. } else if (isNaN(end)) {
  23. end = size - 1;
  24. }
  25. // limit last-byte-pos to current length
  26. if (end > size - 1) end = size - 1;
  27. // invalid
  28. if (isNaN(start)
  29. || isNaN(end)
  30. || start > end
  31. || start < 0) valid = false;
  32. return {
  33. start: start,
  34. end: end
  35. };
  36. });
  37. arr.type = str.slice(0, i);
  38. return valid ? arr : -1;
  39. };