pax_global_header00006660000000000000000000000064134156537310014522gustar00rootroot0000000000000052 comment=f0180a159c25ea47adfd46496097ac75ed2285aa node-range-parser-1.2.0/000077500000000000000000000000001341565373100150335ustar00rootroot00000000000000node-range-parser-1.2.0/.eslintignore000066400000000000000000000000261341565373100175340ustar00rootroot00000000000000coverage node_modules node-range-parser-1.2.0/.eslintrc000066400000000000000000000000341341565373100166540ustar00rootroot00000000000000{ "extends": "standard" } node-range-parser-1.2.0/.gitignore000066400000000000000000000000261341565373100170210ustar00rootroot00000000000000node_modules coverage node-range-parser-1.2.0/.travis.yml000066400000000000000000000016641341565373100171530ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.4" - "5.11" - "6.2" sudo: false cache: directories: - node_modules before_install: # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul" - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev eslint eslint-config-standard eslint-plugin-promise eslint-plugin-standard istanbul" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" node-range-parser-1.2.0/HISTORY.md000066400000000000000000000015001341565373100165120ustar00rootroot000000000000001.2.0 / 2016-06-01 ================== * Add `combine` option to combine overlapping ranges 1.1.0 / 2016-05-13 ================== * Fix incorrectly returning -1 when there is at least one valid range * perf: remove internal function 1.0.3 / 2015-10-29 ================== * perf: enable strict mode 1.0.2 / 2014-09-08 ================== * Support Node.js 0.6 1.0.1 / 2014-09-07 ================== * Move repository to jshttp 1.0.0 / 2013-12-11 ================== * Add repository to package.json * Add MIT license 0.0.4 / 2012-06-17 ================== * Change ret -1 for unsatisfiable and -2 when invalid 0.0.3 / 2012-06-17 ================== * Fix last-byte-pos default to len - 1 0.0.2 / 2012-06-14 ================== * Add `.type` 0.0.1 / 2012-06-11 ================== * Initial release node-range-parser-1.2.0/LICENSE000066400000000000000000000022321341565373100160370ustar00rootroot00000000000000(The MIT License) Copyright (c) 2012-2014 TJ Holowaychuk Copyright (c) 2015-2016 Douglas Christopher Wilson [ // { start: 0, end: 10 }, // { start: 50, end: 60 } // ] ``` ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/range-parser.svg [npm-url]: https://npmjs.org/package/range-parser [node-version-image]: https://img.shields.io/node/v/range-parser.svg [node-version-url]: https://nodejs.org/endownload [travis-image]: https://img.shields.io/travis/jshttp/range-parser.svg [travis-url]: https://travis-ci.org/jshttp/range-parser [coveralls-image]: https://img.shields.io/coveralls/jshttp/range-parser.svg [coveralls-url]: https://coveralls.io/r/jshttp/range-parser [downloads-image]: https://img.shields.io/npm/dm/range-parser.svg [downloads-url]: https://npmjs.org/package/range-parser node-range-parser-1.2.0/index.js000066400000000000000000000053651341565373100165110ustar00rootroot00000000000000/*! * range-parser * Copyright(c) 2012-2014 TJ Holowaychuk * Copyright(c) 2015-2016 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = rangeParser /** * Parse "Range" header `str` relative to the given file `size`. * * @param {Number} size * @param {String} str * @param {Object} [options] * @return {Array} * @public */ function rangeParser (size, str, options) { var index = str.indexOf('=') if (index === -1) { return -2 } // split the range string var arr = str.slice(index + 1).split(',') var ranges = [] // add ranges type ranges.type = str.slice(0, index) // parse all ranges for (var i = 0; i < arr.length; i++) { var range = arr[i].split('-') var start = parseInt(range[0], 10) var end = parseInt(range[1], 10) // -nnn if (isNaN(start)) { start = size - end end = size - 1 // nnn- } else if (isNaN(end)) { end = size - 1 } // limit last-byte-pos to current length if (end > size - 1) { end = size - 1 } // invalid or unsatisifiable if (isNaN(start) || isNaN(end) || start > end || start < 0) { continue } // add range ranges.push({ start: start, end: end }) } if (ranges.length < 1) { // unsatisifiable return -1 } return options && options.combine ? combineRanges(ranges) : ranges } /** * Combine overlapping & adjacent ranges. * @private */ function combineRanges (ranges) { var ordered = ranges.map(mapWithIndex).sort(sortByRangeStart) for (var j = 0, i = 1; i < ordered.length; i++) { var range = ordered[i] var current = ordered[j] if (range.start > current.end + 1) { // next range ordered[++j] = range } else if (range.end > current.end) { // extend range current.end = range.end current.index = Math.min(current.index, range.index) } } // trim ordered array ordered.length = j + 1 // generate combined range var combined = ordered.sort(sortByRangeIndex).map(mapWithoutIndex) // copy ranges type combined.type = ranges.type return combined } /** * Map function to add index value to ranges. * @private */ function mapWithIndex (range, index) { return { start: range.start, end: range.end, index: index } } /** * Map function to remove index value from ranges. * @private */ function mapWithoutIndex (range) { return { start: range.start, end: range.end } } /** * Sort function to sort ranges by index. * @private */ function sortByRangeIndex (a, b) { return a.index - b.index } /** * Sort function to sort ranges by start position. * @private */ function sortByRangeStart (a, b) { return a.start - b.start } node-range-parser-1.2.0/package.json000066400000000000000000000021071341565373100173210ustar00rootroot00000000000000{ "name": "range-parser", "author": "TJ Holowaychuk (http://tjholowaychuk.com)", "description": "Range header field string parser", "version": "1.2.0", "contributors": [ "Douglas Christopher Wilson ", "James Wyatt Cready ", "Jonathan Ong (http://jongleberry.com)" ], "license": "MIT", "keywords": [ "range", "parser", "http" ], "repository": "jshttp/range-parser", "devDependencies": { "eslint": "2.11.1", "eslint-config-standard": "5.3.1", "eslint-plugin-promise": "1.1.0", "eslint-plugin-standard": "1.3.2", "istanbul": "0.4.3", "mocha": "1.21.5" }, "files": [ "HISTORY.md", "LICENSE", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "lint": "eslint **/*.js", "test": "mocha --reporter spec", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot" } } node-range-parser-1.2.0/test/000077500000000000000000000000001341565373100160125ustar00rootroot00000000000000node-range-parser-1.2.0/test/.eslintrc000066400000000000000000000000441341565373100176340ustar00rootroot00000000000000{ "env": { "mocha": true } } node-range-parser-1.2.0/test/range-parser.js000066400000000000000000000077241341565373100207500ustar00rootroot00000000000000 var assert = require('assert') var parse = require('..') describe('parseRange(len, str)', function () { it('should return -2 for invalid str', function () { assert.strictEqual(parse(200, 'malformed'), -2) }) it('should return -1 if all specified ranges are invalid', function () { assert.strictEqual(parse(200, 'bytes=500-20'), -1) assert.strictEqual(parse(200, 'bytes=500-999'), -1) assert.strictEqual(parse(200, 'bytes=500-999,1000-1499'), -1) }) it('should parse str', function () { var range = parse(1000, 'bytes=0-499') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 499 }) }) it('should cap end at size', function () { var range = parse(200, 'bytes=0-499') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 199 }) }) it('should parse str', function () { var range = parse(1000, 'bytes=40-80') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 40, end: 80 }) }) it('should parse str asking for last n bytes', function () { var range = parse(1000, 'bytes=-400') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 600, end: 999 }) }) it('should parse str with only start', function () { var range = parse(1000, 'bytes=400-') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 400, end: 999 }) }) it('should parse "bytes=0-"', function () { var range = parse(1000, 'bytes=0-') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 999 }) }) it('should parse str with no bytes', function () { var range = parse(1000, 'bytes=0-0') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 0 }) }) it('should parse str asking for last byte', function () { var range = parse(1000, 'bytes=-1') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 999, end: 999 }) }) it('should parse str with multiple ranges', function () { var range = parse(1000, 'bytes=40-80,81-90,-1') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 3) assert.deepEqual(range[0], { start: 40, end: 80 }) assert.deepEqual(range[1], { start: 81, end: 90 }) assert.deepEqual(range[2], { start: 999, end: 999 }) }) it('should parse str with some invalid ranges', function () { var range = parse(200, 'bytes=0-499,1000-,500-999') assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 199 }) }) it('should parse non-byte range', function () { var range = parse(1000, 'items=0-5') assert.strictEqual(range.type, 'items') assert.strictEqual(range.length, 1) assert.deepEqual(range[0], { start: 0, end: 5 }) }) describe('when combine: true', function () { it('should combine overlapping ranges', function () { var range = parse(150, 'bytes=0-4,90-99,5-75,100-199,101-102', { combine: true }) assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 2) assert.deepEqual(range[0], { start: 0, end: 75 }) assert.deepEqual(range[1], { start: 90, end: 149 }) }) it('should retain original order', function () { var range = parse(150, 'bytes=-1,20-100,0-1,101-120', { combine: true }) assert.strictEqual(range.type, 'bytes') assert.strictEqual(range.length, 3) assert.deepEqual(range[0], { start: 149, end: 149 }) assert.deepEqual(range[1], { start: 20, end: 120 }) assert.deepEqual(range[2], { start: 0, end: 1 }) }) }) })