프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / range-parser / index.js

이력 | 보기 | 이력해설 | 다운로드 (2.74 KB)

1 39 HKM
/*!
2
 * range-parser
3
 * Copyright(c) 2012-2014 TJ Holowaychuk
4
 * Copyright(c) 2015-2016 Douglas Christopher Wilson
5
 * MIT Licensed
6
 */
7
8
'use strict'
9
10
/**
11
 * Module exports.
12
 * @public
13
 */
14
15
module.exports = rangeParser
16
17
/**
18
 * Parse "Range" header `str` relative to the given file `size`.
19
 *
20
 * @param {Number} size
21
 * @param {String} str
22
 * @param {Object} [options]
23
 * @return {Array}
24
 * @public
25
 */
26
27
function rangeParser (size, str, options) {
28
  var index = str.indexOf('=')
29
30
  if (index === -1) {
31
    return -2
32
  }
33
34
  // split the range string
35
  var arr = str.slice(index + 1).split(',')
36
  var ranges = []
37
38
  // add ranges type
39
  ranges.type = str.slice(0, index)
40
41
  // parse all ranges
42
  for (var i = 0; i < arr.length; i++) {
43
    var range = arr[i].split('-')
44
    var start = parseInt(range[0], 10)
45
    var end = parseInt(range[1], 10)
46
47
    // -nnn
48
    if (isNaN(start)) {
49
      start = size - end
50
      end = size - 1
51
    // nnn-
52
    } else if (isNaN(end)) {
53
      end = size - 1
54
    }
55
56
    // limit last-byte-pos to current length
57
    if (end > size - 1) {
58
      end = size - 1
59
    }
60
61
    // invalid or unsatisifiable
62
    if (isNaN(start) || isNaN(end) || start > end || start < 0) {
63
      continue
64
    }
65
66
    // add range
67
    ranges.push({
68
      start: start,
69
      end: end
70
    })
71
  }
72
73
  if (ranges.length < 1) {
74
    // unsatisifiable
75
    return -1
76
  }
77
78
  return options && options.combine
79
    ? combineRanges(ranges)
80
    : ranges
81
}
82
83
/**
84
 * Combine overlapping & adjacent ranges.
85
 * @private
86
 */
87
88
function combineRanges (ranges) {
89
  var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart)
90
91
  for (var j = 0, i = 1; i < ordered.length; i++) {
92
    var range = ordered[i]
93
    var current = ordered[j]
94
95
    if (range.start > current.end + 1) {
96
      // next range
97
      ordered[++j] = range
98
    } else if (range.end > current.end) {
99
      // extend range
100
      current.end = range.end
101
      current.index = Math.min(current.index, range.index)
102
    }
103
  }
104
105
  // trim ordered array
106
  ordered.length = j + 1
107
108
  // generate combined range
109
  var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex)
110
111
  // copy ranges type
112
  combined.type = ranges.type
113
114
  return combined
115
}
116
117
/**
118
 * Map function to add index value to ranges.
119
 * @private
120
 */
121
122
function mapWithIndex (range, index) {
123
  return {
124
    start: range.start,
125
    end: range.end,
126
    index: index
127
  }
128
}
129
130
/**
131
 * Map function to remove index value from ranges.
132
 * @private
133
 */
134
135
function mapWithoutIndex (range) {
136
  return {
137
    start: range.start,
138
    end: range.end
139
  }
140
}
141
142
/**
143
 * Sort function to sort ranges by index.
144
 * @private
145
 */
146
147
function sortByRangeIndex (a, b) {
148
  return a.index - b.index
149
}
150
151
/**
152
 * Sort function to sort ranges by start position.
153
 * @private
154
 */
155
156
function sortByRangeStart (a, b) {
157
  return a.start - b.start
158
}