vscode-uri
Advanced tools
Comparing version 1.0.0 to 1.0.1
/** | ||
* Uniform Resource Identifier (Uri) http://tools.ietf.org/html/rfc3986. | ||
* Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986. | ||
* This class is a simple parser which creates the basic component paths | ||
@@ -17,3 +17,4 @@ * (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation | ||
*/ | ||
export default class Uri { | ||
export default class URI { | ||
static isUri(thing: any): thing is URI; | ||
private static _empty; | ||
@@ -31,3 +32,3 @@ private static _slash; | ||
private _fsPath; | ||
private constructor(); | ||
protected constructor(); | ||
/** | ||
@@ -56,20 +57,8 @@ * scheme is the 'http' part of 'http://www.msft.com/some/path?query#fragment'. | ||
/** | ||
* Returns a string representing the corresponding file system path of this Uri. | ||
* Returns a string representing the corresponding file system path of this URI. | ||
* Will handle UNC paths and normalize windows drive letters to lower-case. Also | ||
* uses the platform specific path separator. Will *not* validate the path for | ||
* invalid characters and semantics. Will *not* look at the scheme of this Uri. | ||
* invalid characters and semantics. Will *not* look at the scheme of this URI. | ||
*/ | ||
readonly fsPath: string; | ||
/** | ||
* Derive a new Uri from this Uri. | ||
* | ||
* @param change An object that describes a change to this Uri. | ||
* @return A new Uri that reflects the given change. Will return `this` Uri if the change | ||
* is not changing anything. | ||
* @sample ``` | ||
let file = Uri.parse('before:some/file/path'); | ||
let other = file.with({ scheme: 'after' }); | ||
assert.ok(other.toString() === 'after:some/file/path'); | ||
* ``` | ||
*/ | ||
with(change: { | ||
@@ -81,9 +70,6 @@ scheme?: string; | ||
fragment?: string; | ||
}): Uri; | ||
/** | ||
* Create an Uri from uri components. | ||
* | ||
* @param components An object containing the Uri components | ||
* @return A new Uri instance | ||
*/ | ||
}): URI; | ||
static parse(value: string): URI; | ||
static file(path: string): URI; | ||
private static _parseComponents(value); | ||
static from(components: { | ||
@@ -95,20 +81,6 @@ scheme?: string; | ||
fragment?: string; | ||
}): Uri; | ||
/** | ||
* Create an Uri from a string. Will throw if the given value is not | ||
* valid. | ||
* | ||
* @param value The string value of an Uri. | ||
* @return A new Uri instance. | ||
*/ | ||
static parse(value: string): Uri; | ||
/** | ||
* Create an Uri from a file system path. The [scheme](#Uri.scheme) | ||
* will be `file`. | ||
* | ||
* @param path A file system or UNC path. | ||
* @return A new Uri instance. | ||
*/ | ||
static file(path: string): Uri; | ||
private static _parseComponents(value); | ||
}): URI; | ||
private static _schemePattern; | ||
private static _singleSlashStart; | ||
private static _doubleSlashStart; | ||
private static _validate(ret); | ||
@@ -122,2 +94,3 @@ /** | ||
toJSON(): any; | ||
static revive(data: any): URI; | ||
} |
317
lib/index.js
(function (factory) { | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
var v = factory(require, exports); if (v !== undefined) module.exports = v; | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === 'function' && define.amd) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
@@ -14,2 +15,3 @@ } | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function _encode(ch) { | ||
@@ -23,6 +25,6 @@ return '%' + ch.charCodeAt(0).toString(16).toUpperCase(); | ||
function encodeNoop(str) { | ||
return str; | ||
return str.replace(/[#?]/, _encode); | ||
} | ||
/** | ||
* Uniform Resource Identifier (Uri) http://tools.ietf.org/html/rfc3986. | ||
* Uniform Resource Identifier (URI) http://tools.ietf.org/html/rfc3986. | ||
* This class is a simple parser which creates the basic component paths | ||
@@ -42,13 +44,26 @@ * (http://tools.ietf.org/html/rfc3986#section-3) with minimal validation | ||
*/ | ||
var Uri = (function () { | ||
function Uri() { | ||
this._scheme = Uri._empty; | ||
this._authority = Uri._empty; | ||
this._path = Uri._empty; | ||
this._query = Uri._empty; | ||
this._fragment = Uri._empty; | ||
var URI = (function () { | ||
function URI() { | ||
this._scheme = URI._empty; | ||
this._authority = URI._empty; | ||
this._path = URI._empty; | ||
this._query = URI._empty; | ||
this._fragment = URI._empty; | ||
this._formatted = null; | ||
this._fsPath = null; | ||
} | ||
Object.defineProperty(Uri.prototype, "scheme", { | ||
URI.isUri = function (thing) { | ||
if (thing instanceof URI) { | ||
return true; | ||
} | ||
if (!thing) { | ||
return false; | ||
} | ||
return typeof thing.authority === 'string' | ||
&& typeof thing.fragment === 'string' | ||
&& typeof thing.path === 'string' | ||
&& typeof thing.query === 'string' | ||
&& typeof thing.scheme === 'string'; | ||
}; | ||
Object.defineProperty(URI.prototype, "scheme", { | ||
/** | ||
@@ -64,3 +79,3 @@ * scheme is the 'http' part of 'http://www.msft.com/some/path?query#fragment'. | ||
}); | ||
Object.defineProperty(Uri.prototype, "authority", { | ||
Object.defineProperty(URI.prototype, "authority", { | ||
/** | ||
@@ -76,3 +91,3 @@ * authority is the 'www.msft.com' part of 'http://www.msft.com/some/path?query#fragment'. | ||
}); | ||
Object.defineProperty(Uri.prototype, "path", { | ||
Object.defineProperty(URI.prototype, "path", { | ||
/** | ||
@@ -87,3 +102,3 @@ * path is the '/some/path' part of 'http://www.msft.com/some/path?query#fragment'. | ||
}); | ||
Object.defineProperty(Uri.prototype, "query", { | ||
Object.defineProperty(URI.prototype, "query", { | ||
/** | ||
@@ -98,3 +113,3 @@ * query is the 'query' part of 'http://www.msft.com/some/path?query#fragment'. | ||
}); | ||
Object.defineProperty(Uri.prototype, "fragment", { | ||
Object.defineProperty(URI.prototype, "fragment", { | ||
/** | ||
@@ -109,9 +124,9 @@ * fragment is the 'fragment' part of 'http://www.msft.com/some/path?query#fragment'. | ||
}); | ||
Object.defineProperty(Uri.prototype, "fsPath", { | ||
Object.defineProperty(URI.prototype, "fsPath", { | ||
// ---- filesystem path ----------------------- | ||
/** | ||
* Returns a string representing the corresponding file system path of this Uri. | ||
* Returns a string representing the corresponding file system path of this URI. | ||
* Will handle UNC paths and normalize windows drive letters to lower-case. Also | ||
* uses the platform specific path separator. Will *not* validate the path for | ||
* invalid characters and semantics. Will *not* look at the scheme of this Uri. | ||
* invalid characters and semantics. Will *not* look at the scheme of this URI. | ||
*/ | ||
@@ -121,7 +136,7 @@ get: function () { | ||
var value; | ||
if (this._authority && this.scheme === 'file') { | ||
if (this._authority && this._path && this.scheme === 'file') { | ||
// unc path: file://shares/c$/far/boo | ||
value = "//" + this._authority + this._path; | ||
} | ||
else if (Uri._driveLetterPath.test(this._path)) { | ||
else if (URI._driveLetterPath.test(this._path)) { | ||
// windows drive letter: file:///c:/far/boo | ||
@@ -145,23 +160,37 @@ value = this._path[1].toLowerCase() + this._path.substr(2); | ||
// ---- modify to new ------------------------- | ||
/** | ||
* Derive a new Uri from this Uri. | ||
* | ||
* @param change An object that describes a change to this Uri. | ||
* @return A new Uri that reflects the given change. Will return `this` Uri if the change | ||
* is not changing anything. | ||
* @sample ``` | ||
let file = Uri.parse('before:some/file/path'); | ||
let other = file.with({ scheme: 'after' }); | ||
assert.ok(other.toString() === 'after:some/file/path'); | ||
* ``` | ||
*/ | ||
Uri.prototype.with = function (change) { | ||
URI.prototype.with = function (change) { | ||
if (!change) { | ||
return this; | ||
} | ||
var scheme = change.scheme || this.scheme; | ||
var authority = change.authority || this.authority; | ||
var path = change.path || this.path; | ||
var query = change.query || this.query; | ||
var fragment = change.fragment || this.fragment; | ||
var scheme = change.scheme, authority = change.authority, path = change.path, query = change.query, fragment = change.fragment; | ||
if (scheme === void 0) { | ||
scheme = this.scheme; | ||
} | ||
else if (scheme === null) { | ||
scheme = ''; | ||
} | ||
if (authority === void 0) { | ||
authority = this.authority; | ||
} | ||
else if (authority === null) { | ||
authority = ''; | ||
} | ||
if (path === void 0) { | ||
path = this.path; | ||
} | ||
else if (path === null) { | ||
path = ''; | ||
} | ||
if (query === void 0) { | ||
query = this.query; | ||
} | ||
else if (query === null) { | ||
query = ''; | ||
} | ||
if (fragment === void 0) { | ||
fragment = this.fragment; | ||
} | ||
else if (fragment === null) { | ||
fragment = ''; | ||
} | ||
if (scheme === this.scheme | ||
@@ -174,3 +203,3 @@ && authority === this.authority | ||
} | ||
var ret = new Uri(); | ||
var ret = new URI(); | ||
ret._scheme = scheme; | ||
@@ -181,28 +210,9 @@ ret._authority = authority; | ||
ret._fragment = fragment; | ||
Uri._validate(ret); | ||
URI._validate(ret); | ||
return ret; | ||
}; | ||
// ---- parse & validate ------------------------ | ||
/** | ||
* Create an Uri from uri components. | ||
* | ||
* @param components An object containing the Uri components | ||
* @return A new Uri instance | ||
*/ | ||
Uri.from = function (components) { | ||
if (!components) { | ||
throw new Error(); | ||
} | ||
return new Uri().with(components); | ||
}; | ||
/** | ||
* Create an Uri from a string. Will throw if the given value is not | ||
* valid. | ||
* | ||
* @param value The string value of an Uri. | ||
* @return A new Uri instance. | ||
*/ | ||
Uri.parse = function (value) { | ||
var ret = new Uri(); | ||
var data = Uri._parseComponents(value); | ||
URI.parse = function (value) { | ||
var ret = new URI(); | ||
var data = URI._parseComponents(value); | ||
ret._scheme = data.scheme; | ||
@@ -213,21 +223,18 @@ ret._authority = decodeURIComponent(data.authority); | ||
ret._fragment = decodeURIComponent(data.fragment); | ||
Uri._validate(ret); | ||
URI._validate(ret); | ||
return ret; | ||
}; | ||
/** | ||
* Create an Uri from a file system path. The [scheme](#Uri.scheme) | ||
* will be `file`. | ||
* | ||
* @param path A file system or UNC path. | ||
* @return A new Uri instance. | ||
*/ | ||
Uri.file = function (path) { | ||
var ret = new Uri(); | ||
URI.file = function (path) { | ||
var ret = new URI(); | ||
ret._scheme = 'file'; | ||
// normalize to fwd-slashes | ||
path = path.replace(/\\/g, Uri._slash); | ||
// normalize to fwd-slashes on windows, | ||
// on other systems bwd-slaches are valid | ||
// filename character, eg /f\oo/ba\r.txt | ||
if (isWindows) { | ||
path = path.replace(/\\/g, URI._slash); | ||
} | ||
// check for authority as used in UNC shares | ||
// or use the path as given | ||
if (path[0] === Uri._slash && path[0] === path[1]) { | ||
var idx = path.indexOf(Uri._slash, 2); | ||
if (path[0] === URI._slash && path[0] === path[1]) { | ||
var idx = path.indexOf(URI._slash, 2); | ||
if (idx === -1) { | ||
@@ -246,17 +253,17 @@ ret._authority = path.substring(2); | ||
// or that it is at least a slash | ||
if (ret._path[0] !== Uri._slash) { | ||
ret._path = Uri._slash + ret._path; | ||
if (ret._path[0] !== URI._slash) { | ||
ret._path = URI._slash + ret._path; | ||
} | ||
Uri._validate(ret); | ||
URI._validate(ret); | ||
return ret; | ||
}; | ||
Uri._parseComponents = function (value) { | ||
URI._parseComponents = function (value) { | ||
var ret = { | ||
scheme: Uri._empty, | ||
authority: Uri._empty, | ||
path: Uri._empty, | ||
query: Uri._empty, | ||
fragment: Uri._empty, | ||
scheme: URI._empty, | ||
authority: URI._empty, | ||
path: URI._empty, | ||
query: URI._empty, | ||
fragment: URI._empty, | ||
}; | ||
var match = Uri._regexp.exec(value); | ||
var match = URI._regexp.exec(value); | ||
if (match) { | ||
@@ -271,15 +278,28 @@ ret.scheme = match[2] || ret.scheme; | ||
}; | ||
Uri._validate = function (ret) { | ||
// validation | ||
URI.from = function (components) { | ||
return new URI().with(components); | ||
}; | ||
URI._validate = function (ret) { | ||
// scheme, https://tools.ietf.org/html/rfc3986#section-3.1 | ||
// ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) | ||
if (ret.scheme && !URI._schemePattern.test(ret.scheme)) { | ||
throw new Error('[UriError]: Scheme contains illegal characters.'); | ||
} | ||
// path, http://tools.ietf.org/html/rfc3986#section-3.3 | ||
// If a Uri contains an authority component, then the path component | ||
// must either be empty or begin with a slash ("/") character. If a Uri | ||
// If a URI contains an authority component, then the path component | ||
// must either be empty or begin with a slash ("/") character. If a URI | ||
// does not contain an authority component, then the path cannot begin | ||
// with two slash characters ("//"). | ||
if (ret.authority && ret.path && ret.path[0] !== '/') { | ||
throw new Error('[UriError]: If a Uri contains an authority component, then the path component must either be empty or begin with a slash ("/") character'); | ||
if (ret.path) { | ||
if (ret.authority) { | ||
if (!URI._singleSlashStart.test(ret.path)) { | ||
throw new Error('[UriError]: If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character'); | ||
} | ||
} | ||
else { | ||
if (URI._doubleSlashStart.test(ret.path)) { | ||
throw new Error('[UriError]: If a URI does not contain an authority component, then the path cannot begin with two slash characters ("//")'); | ||
} | ||
} | ||
} | ||
if (!ret.authority && ret.path.indexOf('//') === 0) { | ||
throw new Error('[UriError]: If a Uri does not contain an authority component, then the path cannot begin with two slash characters ("//")'); | ||
} | ||
}; | ||
@@ -291,7 +311,7 @@ // ---- printing/externalize --------------------------- | ||
*/ | ||
Uri.prototype.toString = function (skipEncoding) { | ||
URI.prototype.toString = function (skipEncoding) { | ||
if (skipEncoding === void 0) { skipEncoding = false; } | ||
if (!skipEncoding) { | ||
if (!this._formatted) { | ||
this._formatted = Uri._asFormatted(this, false); | ||
this._formatted = URI._asFormatted(this, false); | ||
} | ||
@@ -302,17 +322,16 @@ return this._formatted; | ||
// we don't cache that | ||
return Uri._asFormatted(this, true); | ||
return URI._asFormatted(this, true); | ||
} | ||
}; | ||
Uri._asFormatted = function (uri, skipEncoding) { | ||
URI._asFormatted = function (uri, skipEncoding) { | ||
var encoder = !skipEncoding | ||
? encodeURIComponent2 | ||
: encodeNoop; | ||
var result = ''; | ||
var parts = []; | ||
var scheme = uri.scheme, authority = uri.authority, path = uri.path, query = uri.query, fragment = uri.fragment; | ||
if (scheme) { | ||
result += scheme; | ||
result += ':'; | ||
parts.push(scheme, ':'); | ||
} | ||
if (authority || scheme === 'file') { | ||
result += '//'; | ||
parts.push('//'); | ||
} | ||
@@ -323,14 +342,18 @@ if (authority) { | ||
if (idx === -1) { | ||
result += encoder(authority); | ||
parts.push(encoder(authority)); | ||
} | ||
else { | ||
result += encoder(authority.substr(0, idx)); | ||
result += authority.substr(idx); | ||
parts.push(encoder(authority.substr(0, idx)), authority.substr(idx)); | ||
} | ||
} | ||
if (path) { | ||
// lower-case windown drive letters in /C:/fff | ||
var m = Uri._upperCaseDrive.exec(path); | ||
// lower-case windows drive letters in /C:/fff or C:/fff | ||
var m = URI._upperCaseDrive.exec(path); | ||
if (m) { | ||
path = m[1] + m[2].toLowerCase() + path.substr(m[1].length + m[2].length); | ||
if (m[1]) { | ||
path = '/' + m[2].toLowerCase() + path.substr(3); // "/c:".length === 3 | ||
} | ||
else { | ||
path = m[2].toLowerCase() + path.substr(2); // // "c:".length === 2 | ||
} | ||
} | ||
@@ -343,9 +366,8 @@ // encode every segement but not slashes | ||
while (true) { | ||
var idx = path.indexOf(Uri._slash, lastIdx); | ||
var idx = path.indexOf(URI._slash, lastIdx); | ||
if (idx === -1) { | ||
result += encoder(path.substring(lastIdx)).replace(/[#?]/, _encode); | ||
parts.push(encoder(path.substring(lastIdx))); | ||
break; | ||
} | ||
result += encoder(path.substring(lastIdx, idx)).replace(/[#?]/, _encode); | ||
result += Uri._slash; | ||
parts.push(encoder(path.substring(lastIdx, idx)), URI._slash); | ||
lastIdx = idx + 1; | ||
@@ -356,30 +378,55 @@ } | ||
if (query) { | ||
result += '?'; | ||
result += encoder(query); | ||
parts.push('?', encoder(query)); | ||
} | ||
if (fragment) { | ||
result += '#'; | ||
result += encoder(fragment); | ||
parts.push('#', encoder(fragment)); | ||
} | ||
return result; | ||
return parts.join(URI._empty); | ||
}; | ||
Uri.prototype.toJSON = function () { | ||
return { | ||
scheme: this.scheme, | ||
authority: this.authority, | ||
path: this.path, | ||
URI.prototype.toJSON = function () { | ||
var res = { | ||
fsPath: this.fsPath, | ||
query: this.query, | ||
fragment: this.fragment | ||
external: this.toString(), | ||
$mid: 1 | ||
}; | ||
if (this.path) { | ||
res.path = this.path; | ||
} | ||
if (this.scheme) { | ||
res.scheme = this.scheme; | ||
} | ||
if (this.authority) { | ||
res.authority = this.authority; | ||
} | ||
if (this.query) { | ||
res.query = this.query; | ||
} | ||
if (this.fragment) { | ||
res.fragment = this.fragment; | ||
} | ||
return res; | ||
}; | ||
Uri._empty = ''; | ||
Uri._slash = '/'; | ||
Uri._regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; | ||
Uri._driveLetterPath = /^\/[a-zA-z]:/; | ||
Uri._upperCaseDrive = /^(\/)?([A-Z]:)/; | ||
return Uri; | ||
URI.revive = function (data) { | ||
var result = new URI(); | ||
result._scheme = data.scheme || URI._empty; | ||
result._authority = data.authority || URI._empty; | ||
result._path = data.path || URI._empty; | ||
result._query = data.query || URI._empty; | ||
result._fragment = data.fragment || URI._empty; | ||
result._fsPath = data.fsPath; | ||
result._formatted = data.external; | ||
URI._validate(result); | ||
return result; | ||
}; | ||
return URI; | ||
}()); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Uri; | ||
URI._empty = ''; | ||
URI._slash = '/'; | ||
URI._regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; | ||
URI._driveLetterPath = /^\/[a-zA-z]:/; | ||
URI._upperCaseDrive = /^(\/)?([A-Z]:)/; | ||
URI._schemePattern = /^\w[\w\d+.-]*$/; | ||
URI._singleSlashStart = /^\//; | ||
URI._doubleSlashStart = /^\/\//; | ||
exports.default = URI; | ||
var isWindows; | ||
@@ -386,0 +433,0 @@ if (typeof process === 'object') { |
{ | ||
"name": "vscode-uri", | ||
"author": "Microsoft", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "The URI implementation that is used by VS Code and its extensions", | ||
@@ -9,5 +9,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"compile": "tsc && tsc -p spec/tsconfig.json", | ||
"test": "npm run compile && mocha --ui tdd spec/", | ||
"prepublish": "npm run test" | ||
"compile": "tsc && tsc -p spec/tsconfig.json" | ||
}, | ||
@@ -24,5 +22,4 @@ "repository": { | ||
"devDependencies": { | ||
"mocha": "^2.5.3", | ||
"typescript": "^2.0.3" | ||
} | ||
} |
@@ -48,4 +48,8 @@ ## vscode-uri | ||
## Contributing | ||
The source of this module is taken straight from the [vscode](https://github.com/Microsoft/vscode)-sources and because of that issues and pull request should be created in that repository. Thanks and Happy Coding! | ||
## Code of Conduct | ||
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
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
23385
1
512
55