Comparing version 0.11.14 to 0.12.7
@@ -13,3 +13,3 @@ { | ||
"license": "MIT", | ||
"version": "0.11.14", | ||
"version": "0.12.7", | ||
"homepage": "http://nodejs.org/docs/latest/api/path.html", | ||
@@ -20,3 +20,7 @@ "repository": { | ||
}, | ||
"main": "./path.js" | ||
"main": "./path.js", | ||
"dependencies": { | ||
"process": "^0.11.1", | ||
"util": "^0.10.3" | ||
} | ||
} |
844
path.js
@@ -1,5 +0,1 @@ | ||
var process = process || {}; | ||
(function () { | ||
"use strict"; | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
@@ -26,3 +22,5 @@ // | ||
'use strict'; | ||
var isWindows = process.platform === 'win32'; | ||
@@ -33,421 +31,512 @@ var util = require('util'); | ||
// resolves . and .. elements in a path array with directory names there | ||
// must be no slashes, empty elements, or device names (c:\) in the array | ||
// must be no slashes or device names (c:\) in the array | ||
// (so also no leading and trailing slashes - it does not distinguish | ||
// relative and absolute paths) | ||
function normalizeArray(parts, allowAboveRoot) { | ||
// if the path tries to go above the root, `up` ends up > 0 | ||
var up = 0; | ||
for (var i = parts.length - 1; i >= 0; i--) { | ||
var last = parts[i]; | ||
if (last === '.') { | ||
parts.splice(i, 1); | ||
} else if (last === '..') { | ||
parts.splice(i, 1); | ||
up++; | ||
} else if (up) { | ||
parts.splice(i, 1); | ||
up--; | ||
var res = []; | ||
for (var i = 0; i < parts.length; i++) { | ||
var p = parts[i]; | ||
// ignore empty parts | ||
if (!p || p === '.') | ||
continue; | ||
if (p === '..') { | ||
if (res.length && res[res.length - 1] !== '..') { | ||
res.pop(); | ||
} else if (allowAboveRoot) { | ||
res.push('..'); | ||
} | ||
} else { | ||
res.push(p); | ||
} | ||
} | ||
// if the path is allowed to go above the root, restore leading ..s | ||
if (allowAboveRoot) { | ||
for (; up--; up) { | ||
parts.unshift('..'); | ||
} | ||
return res; | ||
} | ||
// returns an array with empty elements removed from either end of the input | ||
// array or the original array if no elements need to be removed | ||
function trimArray(arr) { | ||
var lastIndex = arr.length - 1; | ||
var start = 0; | ||
for (; start <= lastIndex; start++) { | ||
if (arr[start]) | ||
break; | ||
} | ||
return parts; | ||
var end = lastIndex; | ||
for (; end >= 0; end--) { | ||
if (arr[end]) | ||
break; | ||
} | ||
if (start === 0 && end === lastIndex) | ||
return arr; | ||
if (start > end) | ||
return []; | ||
return arr.slice(start, end + 1); | ||
} | ||
// Regex to split a windows path into three parts: [*, device, slash, | ||
// tail] windows-only | ||
var splitDeviceRe = | ||
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; | ||
if (isWindows) { | ||
// Regex to split a windows path into three parts: [*, device, slash, | ||
// tail] windows-only | ||
var splitDeviceRe = | ||
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/; | ||
// Regex to split the tail part of the above into [*, dir, basename, ext] | ||
var splitTailRe = | ||
/^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; | ||
// Regex to split the tail part of the above into [*, dir, basename, ext] | ||
var splitTailRe = | ||
/^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/; | ||
var win32 = {}; | ||
// Function to split a filename into [root, dir, basename, ext] | ||
// windows version | ||
var splitPath = function(filename) { | ||
// Separate device+slash from tail | ||
var result = splitDeviceRe.exec(filename), | ||
device = (result[1] || '') + (result[2] || ''), | ||
tail = result[3] || ''; | ||
// Split the tail into dir, basename and extension | ||
var result2 = splitTailRe.exec(tail), | ||
dir = result2[1], | ||
basename = result2[2], | ||
ext = result2[3]; | ||
return [device, dir, basename, ext]; | ||
}; | ||
// Function to split a filename into [root, dir, basename, ext] | ||
function win32SplitPath(filename) { | ||
// Separate device+slash from tail | ||
var result = splitDeviceRe.exec(filename), | ||
device = (result[1] || '') + (result[2] || ''), | ||
tail = result[3] || ''; | ||
// Split the tail into dir, basename and extension | ||
var result2 = splitTailRe.exec(tail), | ||
dir = result2[1], | ||
basename = result2[2], | ||
ext = result2[3]; | ||
return [device, dir, basename, ext]; | ||
} | ||
var normalizeUNCRoot = function(device) { | ||
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); | ||
function win32StatPath(path) { | ||
var result = splitDeviceRe.exec(path), | ||
device = result[1] || '', | ||
isUnc = !!device && device[1] !== ':'; | ||
return { | ||
device: device, | ||
isUnc: isUnc, | ||
isAbsolute: isUnc || !!result[2], // UNC paths are always absolute | ||
tail: result[3] | ||
}; | ||
} | ||
// path.resolve([from ...], to) | ||
// windows version | ||
exports.resolve = function() { | ||
var resolvedDevice = '', | ||
resolvedTail = '', | ||
resolvedAbsolute = false; | ||
function normalizeUNCRoot(device) { | ||
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); | ||
} | ||
for (var i = arguments.length - 1; i >= -1; i--) { | ||
var path; | ||
if (i >= 0) { | ||
path = arguments[i]; | ||
} else if (!resolvedDevice) { | ||
path = process.cwd(); | ||
} else { | ||
// Windows has the concept of drive-specific current working | ||
// directories. If we've resolved a drive letter but not yet an | ||
// absolute path, get cwd for that drive. We're sure the device is not | ||
// an unc path at this points, because unc paths are always absolute. | ||
path = process.env['=' + resolvedDevice]; | ||
// Verify that a drive-local cwd was found and that it actually points | ||
// to our drive. If not, default to the drive's root. | ||
if (!path || path.substr(0, 3).toLowerCase() !== | ||
resolvedDevice.toLowerCase() + '\\') { | ||
path = resolvedDevice + '\\'; | ||
} | ||
} | ||
// path.resolve([from ...], to) | ||
win32.resolve = function() { | ||
var resolvedDevice = '', | ||
resolvedTail = '', | ||
resolvedAbsolute = false; | ||
// Skip empty and invalid entries | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
} else if (!path) { | ||
continue; | ||
for (var i = arguments.length - 1; i >= -1; i--) { | ||
var path; | ||
if (i >= 0) { | ||
path = arguments[i]; | ||
} else if (!resolvedDevice) { | ||
path = process.cwd(); | ||
} else { | ||
// Windows has the concept of drive-specific current working | ||
// directories. If we've resolved a drive letter but not yet an | ||
// absolute path, get cwd for that drive. We're sure the device is not | ||
// an unc path at this points, because unc paths are always absolute. | ||
path = process.env['=' + resolvedDevice]; | ||
// Verify that a drive-local cwd was found and that it actually points | ||
// to our drive. If not, default to the drive's root. | ||
if (!path || path.substr(0, 3).toLowerCase() !== | ||
resolvedDevice.toLowerCase() + '\\') { | ||
path = resolvedDevice + '\\'; | ||
} | ||
} | ||
var result = splitDeviceRe.exec(path), | ||
device = result[1] || '', | ||
isUnc = device && device.charAt(1) !== ':', | ||
isAbsolute = exports.isAbsolute(path), | ||
tail = result[3]; | ||
// Skip empty and invalid entries | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
} else if (!path) { | ||
continue; | ||
} | ||
if (device && | ||
resolvedDevice && | ||
device.toLowerCase() !== resolvedDevice.toLowerCase()) { | ||
// This path points to another device so it is not applicable | ||
continue; | ||
} | ||
var result = win32StatPath(path), | ||
device = result.device, | ||
isUnc = result.isUnc, | ||
isAbsolute = result.isAbsolute, | ||
tail = result.tail; | ||
if (!resolvedDevice) { | ||
resolvedDevice = device; | ||
} | ||
if (!resolvedAbsolute) { | ||
resolvedTail = tail + '\\' + resolvedTail; | ||
resolvedAbsolute = isAbsolute; | ||
} | ||
if (device && | ||
resolvedDevice && | ||
device.toLowerCase() !== resolvedDevice.toLowerCase()) { | ||
// This path points to another device so it is not applicable | ||
continue; | ||
} | ||
if (resolvedDevice && resolvedAbsolute) { | ||
break; | ||
} | ||
if (!resolvedDevice) { | ||
resolvedDevice = device; | ||
} | ||
if (!resolvedAbsolute) { | ||
resolvedTail = tail + '\\' + resolvedTail; | ||
resolvedAbsolute = isAbsolute; | ||
} | ||
// Convert slashes to backslashes when `resolvedDevice` points to an UNC | ||
// root. Also squash multiple slashes into a single one where appropriate. | ||
if (isUnc) { | ||
resolvedDevice = normalizeUNCRoot(resolvedDevice); | ||
if (resolvedDevice && resolvedAbsolute) { | ||
break; | ||
} | ||
} | ||
// At this point the path should be resolved to a full absolute path, | ||
// but handle relative paths to be safe (might happen when process.cwd() | ||
// fails) | ||
// Convert slashes to backslashes when `resolvedDevice` points to an UNC | ||
// root. Also squash multiple slashes into a single one where appropriate. | ||
if (isUnc) { | ||
resolvedDevice = normalizeUNCRoot(resolvedDevice); | ||
} | ||
// Normalize the tail path | ||
// At this point the path should be resolved to a full absolute path, | ||
// but handle relative paths to be safe (might happen when process.cwd() | ||
// fails) | ||
function f(p) { | ||
return !!p; | ||
} | ||
// Normalize the tail path | ||
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/), | ||
!resolvedAbsolute).join('\\'); | ||
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f), | ||
!resolvedAbsolute).join('\\'); | ||
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || | ||
'.'; | ||
}; | ||
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || | ||
'.'; | ||
}; | ||
// windows version | ||
exports.normalize = function(path) { | ||
var result = splitDeviceRe.exec(path), | ||
device = result[1] || '', | ||
isUnc = device && device.charAt(1) !== ':', | ||
isAbsolute = exports.isAbsolute(path), | ||
tail = result[3], | ||
trailingSlash = /[\\\/]$/.test(tail); | ||
win32.normalize = function(path) { | ||
var result = win32StatPath(path), | ||
device = result.device, | ||
isUnc = result.isUnc, | ||
isAbsolute = result.isAbsolute, | ||
tail = result.tail, | ||
trailingSlash = /[\\\/]$/.test(tail); | ||
// If device is a drive letter, we'll normalize to lower case. | ||
if (device && device.charAt(1) === ':') { | ||
device = device[0].toLowerCase() + device.substr(1); | ||
} | ||
// Normalize the tail path | ||
tail = normalizeArray(tail.split(/[\\\/]+/), !isAbsolute).join('\\'); | ||
// Normalize the tail path | ||
tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) { | ||
return !!p; | ||
}), !isAbsolute).join('\\'); | ||
if (!tail && !isAbsolute) { | ||
tail = '.'; | ||
} | ||
if (tail && trailingSlash) { | ||
tail += '\\'; | ||
} | ||
if (!tail && !isAbsolute) { | ||
tail = '.'; | ||
// Convert slashes to backslashes when `device` points to an UNC root. | ||
// Also squash multiple slashes into a single one where appropriate. | ||
if (isUnc) { | ||
device = normalizeUNCRoot(device); | ||
} | ||
return device + (isAbsolute ? '\\' : '') + tail; | ||
}; | ||
win32.isAbsolute = function(path) { | ||
return win32StatPath(path).isAbsolute; | ||
}; | ||
win32.join = function() { | ||
var paths = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var arg = arguments[i]; | ||
if (!util.isString(arg)) { | ||
throw new TypeError('Arguments to path.join must be strings'); | ||
} | ||
if (tail && trailingSlash) { | ||
tail += '\\'; | ||
if (arg) { | ||
paths.push(arg); | ||
} | ||
} | ||
// Convert slashes to backslashes when `device` points to an UNC root. | ||
// Also squash multiple slashes into a single one where appropriate. | ||
if (isUnc) { | ||
device = normalizeUNCRoot(device); | ||
} | ||
var joined = paths.join('\\'); | ||
return device + (isAbsolute ? '\\' : '') + tail; | ||
}; | ||
// Make sure that the joined path doesn't start with two slashes, because | ||
// normalize() will mistake it for an UNC path then. | ||
// | ||
// This step is skipped when it is very clear that the user actually | ||
// intended to point at an UNC path. This is assumed when the first | ||
// non-empty string arguments starts with exactly two slashes followed by | ||
// at least one more non-slash character. | ||
// | ||
// Note that for normalize() to treat a path as an UNC path it needs to | ||
// have at least 2 components, so we don't filter for that here. | ||
// This means that the user can use join to construct UNC paths from | ||
// a server name and a share name; for example: | ||
// path.join('//server', 'share') -> '\\\\server\\share\') | ||
if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) { | ||
joined = joined.replace(/^[\\\/]{2,}/, '\\'); | ||
} | ||
// windows version | ||
exports.isAbsolute = function(path) { | ||
var result = splitDeviceRe.exec(path), | ||
device = result[1] || '', | ||
isUnc = !!device && device.charAt(1) !== ':'; | ||
// UNC paths are always absolute | ||
return !!result[2] || isUnc; | ||
}; | ||
return win32.normalize(joined); | ||
}; | ||
// windows version | ||
exports.join = function() { | ||
function f(p) { | ||
if (!util.isString(p)) { | ||
throw new TypeError('Arguments to path.join must be strings'); | ||
} | ||
return p; | ||
} | ||
var paths = Array.prototype.filter.call(arguments, f); | ||
var joined = paths.join('\\'); | ||
// path.relative(from, to) | ||
// it will solve the relative path from 'from' to 'to', for instance: | ||
// from = 'C:\\orandea\\test\\aaa' | ||
// to = 'C:\\orandea\\impl\\bbb' | ||
// The output of the function should be: '..\\..\\impl\\bbb' | ||
win32.relative = function(from, to) { | ||
from = win32.resolve(from); | ||
to = win32.resolve(to); | ||
// Make sure that the joined path doesn't start with two slashes, because | ||
// normalize() will mistake it for an UNC path then. | ||
// | ||
// This step is skipped when it is very clear that the user actually | ||
// intended to point at an UNC path. This is assumed when the first | ||
// non-empty string arguments starts with exactly two slashes followed by | ||
// at least one more non-slash character. | ||
// | ||
// Note that for normalize() to treat a path as an UNC path it needs to | ||
// have at least 2 components, so we don't filter for that here. | ||
// This means that the user can use join to construct UNC paths from | ||
// a server name and a share name; for example: | ||
// path.join('//server', 'share') -> '\\\\server\\share\') | ||
if (!/^[\\\/]{2}[^\\\/]/.test(paths[0])) { | ||
joined = joined.replace(/^[\\\/]{2,}/, '\\'); | ||
// windows is not case sensitive | ||
var lowerFrom = from.toLowerCase(); | ||
var lowerTo = to.toLowerCase(); | ||
var toParts = trimArray(to.split('\\')); | ||
var lowerFromParts = trimArray(lowerFrom.split('\\')); | ||
var lowerToParts = trimArray(lowerTo.split('\\')); | ||
var length = Math.min(lowerFromParts.length, lowerToParts.length); | ||
var samePartsLength = length; | ||
for (var i = 0; i < length; i++) { | ||
if (lowerFromParts[i] !== lowerToParts[i]) { | ||
samePartsLength = i; | ||
break; | ||
} | ||
} | ||
return exports.normalize(joined); | ||
}; | ||
if (samePartsLength == 0) { | ||
return to; | ||
} | ||
// path.relative(from, to) | ||
// it will solve the relative path from 'from' to 'to', for instance: | ||
// from = 'C:\\orandea\\test\\aaa' | ||
// to = 'C:\\orandea\\impl\\bbb' | ||
// The output of the function should be: '..\\..\\impl\\bbb' | ||
// windows version | ||
exports.relative = function(from, to) { | ||
from = exports.resolve(from); | ||
to = exports.resolve(to); | ||
var outputParts = []; | ||
for (var i = samePartsLength; i < lowerFromParts.length; i++) { | ||
outputParts.push('..'); | ||
} | ||
// windows is not case sensitive | ||
var lowerFrom = from.toLowerCase(); | ||
var lowerTo = to.toLowerCase(); | ||
outputParts = outputParts.concat(toParts.slice(samePartsLength)); | ||
function trim(arr) { | ||
var start = 0; | ||
for (; start < arr.length; start++) { | ||
if (arr[start] !== '') break; | ||
} | ||
return outputParts.join('\\'); | ||
}; | ||
var end = arr.length - 1; | ||
for (; end >= 0; end--) { | ||
if (arr[end] !== '') break; | ||
} | ||
if (start > end) return []; | ||
return arr.slice(start, end + 1); | ||
} | ||
win32._makeLong = function(path) { | ||
// Note: this will *probably* throw somewhere. | ||
if (!util.isString(path)) | ||
return path; | ||
var toParts = trim(to.split('\\')); | ||
if (!path) { | ||
return ''; | ||
} | ||
var lowerFromParts = trim(lowerFrom.split('\\')); | ||
var lowerToParts = trim(lowerTo.split('\\')); | ||
var resolvedPath = win32.resolve(path); | ||
var length = Math.min(lowerFromParts.length, lowerToParts.length); | ||
var samePartsLength = length; | ||
for (var i = 0; i < length; i++) { | ||
if (lowerFromParts[i] !== lowerToParts[i]) { | ||
samePartsLength = i; | ||
break; | ||
} | ||
} | ||
if (/^[a-zA-Z]\:\\/.test(resolvedPath)) { | ||
// path is local filesystem path, which needs to be converted | ||
// to long UNC path. | ||
return '\\\\?\\' + resolvedPath; | ||
} else if (/^\\\\[^?.]/.test(resolvedPath)) { | ||
// path is network UNC path, which needs to be converted | ||
// to long UNC path. | ||
return '\\\\?\\UNC\\' + resolvedPath.substring(2); | ||
} | ||
if (samePartsLength == 0) { | ||
return to; | ||
} | ||
return path; | ||
}; | ||
var outputParts = []; | ||
for (var i = samePartsLength; i < lowerFromParts.length; i++) { | ||
outputParts.push('..'); | ||
} | ||
outputParts = outputParts.concat(toParts.slice(samePartsLength)); | ||
win32.dirname = function(path) { | ||
var result = win32SplitPath(path), | ||
root = result[0], | ||
dir = result[1]; | ||
return outputParts.join('\\'); | ||
}; | ||
if (!root && !dir) { | ||
// No dirname whatsoever | ||
return '.'; | ||
} | ||
exports.sep = '\\'; | ||
exports.delimiter = ';'; | ||
if (dir) { | ||
// It has a dirname, strip trailing slash | ||
dir = dir.substr(0, dir.length - 1); | ||
} | ||
} else /* posix */ { | ||
return root + dir; | ||
}; | ||
// Split a filename into [root, dir, basename, ext], unix version | ||
// 'root' is just a slash, or nothing. | ||
var splitPathRe = | ||
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; | ||
var splitPath = function(filename) { | ||
return splitPathRe.exec(filename).slice(1); | ||
}; | ||
// path.resolve([from ...], to) | ||
// posix version | ||
exports.resolve = function() { | ||
var resolvedPath = '', | ||
resolvedAbsolute = false; | ||
win32.basename = function(path, ext) { | ||
var f = win32SplitPath(path)[2]; | ||
// TODO: make this comparison case-insensitive on windows? | ||
if (ext && f.substr(-1 * ext.length) === ext) { | ||
f = f.substr(0, f.length - ext.length); | ||
} | ||
return f; | ||
}; | ||
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { | ||
var path = (i >= 0) ? arguments[i] : process.cwd(); | ||
// Skip empty and invalid entries | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
} else if (!path) { | ||
continue; | ||
} | ||
win32.extname = function(path) { | ||
return win32SplitPath(path)[3]; | ||
}; | ||
resolvedPath = path + '/' + resolvedPath; | ||
resolvedAbsolute = path.charAt(0) === '/'; | ||
} | ||
// At this point the path should be resolved to a full absolute path, but | ||
// handle relative paths to be safe (might happen when process.cwd() fails) | ||
win32.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
// Normalize the path | ||
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) { | ||
return !!p; | ||
}), !resolvedAbsolute).join('/'); | ||
var root = pathObject.root || ''; | ||
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; | ||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
); | ||
} | ||
var dir = pathObject.dir; | ||
var base = pathObject.base || ''; | ||
if (!dir) { | ||
return base; | ||
} | ||
if (dir[dir.length - 1] === win32.sep) { | ||
return dir + base; | ||
} | ||
return dir + win32.sep + base; | ||
}; | ||
win32.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
var allParts = win32SplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, -1), | ||
base: allParts[2], | ||
ext: allParts[3], | ||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length) | ||
}; | ||
}; | ||
// path.normalize(path) | ||
// posix version | ||
exports.normalize = function(path) { | ||
var isAbsolute = exports.isAbsolute(path), | ||
trailingSlash = path[path.length - 1] === '/', | ||
segments = path.split('/'), | ||
nonEmptySegments = []; | ||
// Normalize the path | ||
for (var i = 0; i < segments.length; i++) { | ||
if (segments[i]) { | ||
nonEmptySegments.push(segments[i]); | ||
} | ||
} | ||
path = normalizeArray(nonEmptySegments, !isAbsolute).join('/'); | ||
win32.sep = '\\'; | ||
win32.delimiter = ';'; | ||
if (!path && !isAbsolute) { | ||
path = '.'; | ||
} | ||
if (path && trailingSlash) { | ||
path += '/'; | ||
} | ||
return (isAbsolute ? '/' : '') + path; | ||
}; | ||
// Split a filename into [root, dir, basename, ext], unix version | ||
// 'root' is just a slash, or nothing. | ||
var splitPathRe = | ||
/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/; | ||
var posix = {}; | ||
// posix version | ||
exports.isAbsolute = function(path) { | ||
return path.charAt(0) === '/'; | ||
}; | ||
// posix version | ||
exports.join = function() { | ||
var path = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var segment = arguments[i]; | ||
if (!util.isString(segment)) { | ||
throw new TypeError('Arguments to path.join must be strings'); | ||
} | ||
if (segment) { | ||
if (!path) { | ||
path += segment; | ||
} else { | ||
path += '/' + segment; | ||
} | ||
} | ||
function posixSplitPath(filename) { | ||
return splitPathRe.exec(filename).slice(1); | ||
} | ||
// path.resolve([from ...], to) | ||
// posix version | ||
posix.resolve = function() { | ||
var resolvedPath = '', | ||
resolvedAbsolute = false; | ||
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { | ||
var path = (i >= 0) ? arguments[i] : process.cwd(); | ||
// Skip empty and invalid entries | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
} else if (!path) { | ||
continue; | ||
} | ||
return exports.normalize(path); | ||
}; | ||
resolvedPath = path + '/' + resolvedPath; | ||
resolvedAbsolute = path[0] === '/'; | ||
} | ||
// path.relative(from, to) | ||
// posix version | ||
exports.relative = function(from, to) { | ||
from = exports.resolve(from).substr(1); | ||
to = exports.resolve(to).substr(1); | ||
// At this point the path should be resolved to a full absolute path, but | ||
// handle relative paths to be safe (might happen when process.cwd() fails) | ||
function trim(arr) { | ||
var start = 0; | ||
for (; start < arr.length; start++) { | ||
if (arr[start] !== '') break; | ||
} | ||
// Normalize the path | ||
resolvedPath = normalizeArray(resolvedPath.split('/'), | ||
!resolvedAbsolute).join('/'); | ||
var end = arr.length - 1; | ||
for (; end >= 0; end--) { | ||
if (arr[end] !== '') break; | ||
} | ||
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; | ||
}; | ||
if (start > end) return []; | ||
return arr.slice(start, end + 1); | ||
} | ||
// path.normalize(path) | ||
// posix version | ||
posix.normalize = function(path) { | ||
var isAbsolute = posix.isAbsolute(path), | ||
trailingSlash = path && path[path.length - 1] === '/'; | ||
var fromParts = trim(from.split('/')); | ||
var toParts = trim(to.split('/')); | ||
// Normalize the path | ||
path = normalizeArray(path.split('/'), !isAbsolute).join('/'); | ||
var length = Math.min(fromParts.length, toParts.length); | ||
var samePartsLength = length; | ||
for (var i = 0; i < length; i++) { | ||
if (fromParts[i] !== toParts[i]) { | ||
samePartsLength = i; | ||
break; | ||
if (!path && !isAbsolute) { | ||
path = '.'; | ||
} | ||
if (path && trailingSlash) { | ||
path += '/'; | ||
} | ||
return (isAbsolute ? '/' : '') + path; | ||
}; | ||
// posix version | ||
posix.isAbsolute = function(path) { | ||
return path.charAt(0) === '/'; | ||
}; | ||
// posix version | ||
posix.join = function() { | ||
var path = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var segment = arguments[i]; | ||
if (!util.isString(segment)) { | ||
throw new TypeError('Arguments to path.join must be strings'); | ||
} | ||
if (segment) { | ||
if (!path) { | ||
path += segment; | ||
} else { | ||
path += '/' + segment; | ||
} | ||
} | ||
} | ||
return posix.normalize(path); | ||
}; | ||
var outputParts = []; | ||
for (var i = samePartsLength; i < fromParts.length; i++) { | ||
outputParts.push('..'); | ||
// path.relative(from, to) | ||
// posix version | ||
posix.relative = function(from, to) { | ||
from = posix.resolve(from).substr(1); | ||
to = posix.resolve(to).substr(1); | ||
var fromParts = trimArray(from.split('/')); | ||
var toParts = trimArray(to.split('/')); | ||
var length = Math.min(fromParts.length, toParts.length); | ||
var samePartsLength = length; | ||
for (var i = 0; i < length; i++) { | ||
if (fromParts[i] !== toParts[i]) { | ||
samePartsLength = i; | ||
break; | ||
} | ||
} | ||
outputParts = outputParts.concat(toParts.slice(samePartsLength)); | ||
var outputParts = []; | ||
for (var i = samePartsLength; i < fromParts.length; i++) { | ||
outputParts.push('..'); | ||
} | ||
return outputParts.join('/'); | ||
}; | ||
outputParts = outputParts.concat(toParts.slice(samePartsLength)); | ||
exports.sep = '/'; | ||
exports.delimiter = ':'; | ||
} | ||
return outputParts.join('/'); | ||
}; | ||
exports.dirname = function(path) { | ||
var result = splitPath(path), | ||
posix._makeLong = function(path) { | ||
return path; | ||
}; | ||
posix.dirname = function(path) { | ||
var result = posixSplitPath(path), | ||
root = result[0], | ||
@@ -470,4 +559,4 @@ dir = result[1]; | ||
exports.basename = function(path, ext) { | ||
var f = splitPath(path)[2]; | ||
posix.basename = function(path, ext) { | ||
var f = posixSplitPath(path)[2]; | ||
// TODO: make this comparison case-insensitive on windows? | ||
@@ -481,46 +570,63 @@ if (ext && f.substr(-1 * ext.length) === ext) { | ||
exports.extname = function(path) { | ||
return splitPath(path)[3]; | ||
posix.extname = function(path) { | ||
return posixSplitPath(path)[3]; | ||
}; | ||
exports.exists = util.deprecate(function(path, callback) { | ||
require('fs').exists(path, callback); | ||
}, 'path.exists is now called `fs.exists`.'); | ||
posix.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
var root = pathObject.root || ''; | ||
exports.existsSync = util.deprecate(function(path) { | ||
return require('fs').existsSync(path); | ||
}, 'path.existsSync is now called `fs.existsSync`.'); | ||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
); | ||
} | ||
var dir = pathObject.dir ? pathObject.dir + posix.sep : ''; | ||
var base = pathObject.base || ''; | ||
return dir + base; | ||
}; | ||
if (isWindows) { | ||
exports._makeLong = function(path) { | ||
// Note: this will *probably* throw somewhere. | ||
if (!util.isString(path)) | ||
return path; | ||
if (!path) { | ||
return ''; | ||
} | ||
posix.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
var allParts = posixSplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
allParts[1] = allParts[1] || ''; | ||
allParts[2] = allParts[2] || ''; | ||
allParts[3] = allParts[3] || ''; | ||
var resolvedPath = exports.resolve(path); | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, -1), | ||
base: allParts[2], | ||
ext: allParts[3], | ||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length) | ||
}; | ||
}; | ||
if (/^[a-zA-Z]\:\\/.test(resolvedPath)) { | ||
// path is local filesystem path, which needs to be converted | ||
// to long UNC path. | ||
return '\\\\?\\' + resolvedPath; | ||
} else if (/^\\\\[^?.]/.test(resolvedPath)) { | ||
// path is network UNC path, which needs to be converted | ||
// to long UNC path. | ||
return '\\\\?\\UNC\\' + resolvedPath.substring(2); | ||
} | ||
return path; | ||
}; | ||
} else { | ||
exports._makeLong = function(path) { | ||
return path; | ||
}; | ||
} | ||
}()); | ||
posix.sep = '/'; | ||
posix.delimiter = ':'; | ||
if (isWindows) | ||
module.exports = win32; | ||
else /* posix */ | ||
module.exports = posix; | ||
module.exports.posix = posix; | ||
module.exports.win32 = win32; |
@@ -0,0 +0,0 @@ # path |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
18917
5
505
1
2
+ Addedprocess@^0.11.1
+ Addedutil@^0.10.3
+ Addedinherits@2.0.3(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedutil@0.10.4(transitive)