| 2 | , Promise = require('bluebird'); |
| 3 | |
| 4 | function normalizeArray(parts, allowAboveRoot) { |
| 5 | // if the path tries to go above the root, `up` ends up > 0 |
| 6 | var up = 0; |
| 7 | for (var i = parts.length - 1; i >= 0; i--) { |
| 8 | var last = parts[i]; |
| 9 | if (last == '.') { |
| 10 | parts.splice(i, 1); |
| 11 | } else if (last === '..') { |
| 12 | parts.splice(i, 1); |
| 13 | up++; |
| 14 | } else if (up) { |
| 15 | parts.splice(i, 1); |
| 16 | up--; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // if the path is allowed to go above the root, restore leading ..s |
| 21 | if (allowAboveRoot) { |
| 22 | for (; up--; up) { |
| 23 | parts.unshift('..'); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return parts; |
| 28 | } |
| 29 | |
| 30 | var normalizePath = function(path) { |
| 31 | var isAbsolute = path.charAt(0) === '/', |