diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-06-26 03:55:56 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-07-02 05:43:16 +0400 |
commit | 481f5f95dca2c664570e4dbcbe4f7e97bbc032b9 (patch) | |
tree | 0066876bab112367b40e9a7e4906b747c748bc2c /node_modules/npm-package-arg | |
parent | f34878fc4cee29901e4daf7bace94be01e25cad7 (diff) |
npm-package-arg@1.0.1
Diffstat (limited to 'node_modules/npm-package-arg')
-rw-r--r-- | node_modules/npm-package-arg/LICENSE | 15 | ||||
-rw-r--r-- | node_modules/npm-package-arg/README.md | 52 | ||||
-rw-r--r-- | node_modules/npm-package-arg/npa.js | 160 | ||||
-rw-r--r-- | node_modules/npm-package-arg/package.json | 52 | ||||
-rw-r--r-- | node_modules/npm-package-arg/test/basic.js | 132 |
5 files changed, 411 insertions, 0 deletions
diff --git a/node_modules/npm-package-arg/LICENSE b/node_modules/npm-package-arg/LICENSE new file mode 100644 index 000000000..05eeeb88c --- /dev/null +++ b/node_modules/npm-package-arg/LICENSE @@ -0,0 +1,15 @@ +The ISC License + +Copyright (c) Isaac Z. Schlueter + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR +IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/node_modules/npm-package-arg/README.md b/node_modules/npm-package-arg/README.md new file mode 100644 index 000000000..48b86efb3 --- /dev/null +++ b/node_modules/npm-package-arg/README.md @@ -0,0 +1,52 @@ +# npm-package-arg + +Parse the things that can be arguments to `npm install` + +Takes an argument like `foo@1.2`, or `foo@user/foo`, or +`http://x.com/foo.tgz`, or `git+https://github.com/user/foo`, and +figures out what type of thing it is. + +## USAGE + +```javascript +var assert = require("assert") +var npa = require("npm-package-arg") + +// Pass in the descriptor, and it'll return an object +var parsed = npa("foo@1.2") + +// Returns an object like: +// { +// name: "foo", // The bit in front of the @ +// type: "range", // the type of descriptor this is +// spec: "1.2" // the specifier for this descriptor +// } + +// Completely unreasonable invalid garbage throws an error +// Make sure you wrap this in a try/catch if you have not +// already sanitized the inputs! +assert.throws(function() { + npa("this is not \0 a valid package name or url") +}) +``` + +For more examples, see the test file. + +## Result Objects + +The objects that are returned by npm-package-arg contain the following +fields: + +* `name` - If known, the `name` field expected in the resulting pkg. +* `type` - One of the following strings: + * `git` - A git repo + * `github` - A github shorthand, like `user/project` + * `tag` - A tagged version, like `"foo@latest"` + * `version` - A specific version number, like `"foo@1.2.3"` + * `range` - A version range, like `"foo@2.x"` + * `local` - A local file or folder path + * `remote` - An http url (presumably to a tgz) +* `spec` - The "thing". URL, the range, git repo, etc. +* `raw` - The original un-modified string that was provided. +* `rawSpec` - The part after the `name@...`, as it was originally + provided. diff --git a/node_modules/npm-package-arg/npa.js b/node_modules/npm-package-arg/npa.js new file mode 100644 index 000000000..e2e3da7c5 --- /dev/null +++ b/node_modules/npm-package-arg/npa.js @@ -0,0 +1,160 @@ +var url = require("url") +var assert = require("assert") +var util = require("util") +var semver = require("semver") +var path = require("path") + +module.exports = npa + +var parseName = /^(?:@([^\/]+?)\/)?([^\/]+?)$/ +var nameAt = /^(@([^\/]+?)\/)?([^\/]+?)@/ +var debug = util.debuglog ? util.debuglog("npa") + : /\bnpa\b/i.test(process.env.NODE_DEBUG || "") + ? function () { + console.error("NPA: " + util.format.apply(util, arguments).split("\n").join("\nNPA: ")) + } : function () {} + +function validName (name) { + if (!name) { + debug("not a name %j", name) + return false + } + var n = name.trim() + if (!n || n.charAt(0) === "." + || !n.match(/^[a-zA-Z0-9]/) + || n.match(/[\/\(\)&\?#\|<>@:%\s\\\*'"!~`]/) + || n.toLowerCase() === "node_modules" + || n !== encodeURIComponent(n) + || n.toLowerCase() === "favicon.ico") { + debug("not a valid name %j", name) + return false + } + return n +} + +function npa (arg) { + assert.equal(typeof arg, "string") + arg = arg.trim() + + var res = new Result + res.raw = arg + res.scope = null + + // See if it's something like foo@... + var nameparse = arg.match(nameAt) + debug("nameparse", nameparse) + if (nameparse && validName(nameparse[3]) && + (!nameparse[2] || validName(nameparse[2]))) { + res.name = (nameparse[1] || "") + nameparse[3] + res.scope = nameparse[2] || null + arg = arg.substr(nameparse[0].length) + } else { + res.name = null + } + + res.rawSpec = arg + res.spec = arg + + var urlparse = url.parse(arg) + debug("urlparse", urlparse) + if (urlparse.protocol) { + return parseUrl(res, arg, urlparse) + } + + // parse git stuff + // parse tag/range/local/remote + + if (maybeGitHubShorthand(arg)) { + res.type = "github" + res.spec = arg + return res + } + + // at this point, it's not a url, and not github + // If it's a valid name, and doesn't already have a name, then assume + // $name@"" range + // + // if it's got / chars in it, then assume that it's local. + + if (res.name) { + var version = semver.valid(arg, true) + var range = semver.validRange(arg, true) + // foo@... + if (version) { + res.spec = version + res.type = "version" + } else if (range) { + res.spec = range + res.type = "range" + } else if (/\//.test(arg)) { + parseLocal(res, arg) + } else { + res.type = "tag" + res.spec = arg + } + } else { + var p = arg.match(parseName) + if (p && validName(p[2]) && + (!p[1] || validName(p[1]))) { + res.type = "range" + res.spec = "*" + res.rawSpec = "" + res.name = arg + res.scope = p[1] || null + } else { + parseLocal(res, arg) + } + } + + return res +} + +function parseLocal (res, arg) { + // turns out nearly every character is allowed in fs paths + if (/\0/.test(arg)) { + throw new Error("Invalid Path: " + JSON.stringify(arg)) + } + res.type = "local" + res.spec = path.resolve(arg) +} + +function maybeGitHubShorthand (arg) { + return /^[^@ \/%]+\/[^@ \/%]+$/.test(arg) +} + +function parseUrl (res, arg, urlparse) { + // check the protocol, and then see if it's git or not + switch (urlparse.protocol) { + case "git:": + case "git+http:": + case "git+https:": + case "git+rsync:": + case "git+ftp:": + case "git+ssh:": + res.type = 'git' + res.spec = arg.replace(/^git\+/, '') + break + + case 'http:': + case 'https:': + case 'npm:': + res.type = 'remote' + res.spec = arg + break + + default: + throw new Error('Unsupported URL Type: ' + arg) + break + } + + return res +} + + +function Result () { + if (!(this instanceof Result)) return new Result +} +Result.prototype.name = null +Result.prototype.type = null +Result.prototype.spec = null +Result.prototype.raw = null diff --git a/node_modules/npm-package-arg/package.json b/node_modules/npm-package-arg/package.json new file mode 100644 index 000000000..e9e4ac4bd --- /dev/null +++ b/node_modules/npm-package-arg/package.json @@ -0,0 +1,52 @@ +{ + "name": "npm-package-arg", + "version": "1.1.0", + "description": "Parse the things that can be arguments to `npm install`", + "main": "npa.js", + "directories": { + "test": "test" + }, + "dependencies": { + "semver": "^2.3.0" + }, + "devDependencies": { + "tap": "^0.4.9" + }, + "scripts": { + "test": "tap test/*.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/npm/npm-package-arg" + }, + "author": { + "name": "Isaac Z. Schlueter", + "email": "i@izs.me", + "url": "http://blog.izs.me/" + }, + "license": "ISC", + "bugs": { + "url": "https://github.com/npm/npm-package-arg/issues" + }, + "homepage": "https://github.com/npm/npm-package-arg", + "gitHead": "3bc0a6195ffb0396e8598fefed54f64b5b40723d", + "_id": "npm-package-arg@1.1.0", + "_shasum": "4546d1cf7d6c25bd1addd605ebc5a040778a2cd6", + "_from": "npm-package-arg@~1.1.0", + "_npmVersion": "1.4.16", + "_npmUser": { + "name": "isaacs", + "email": "i@izs.me" + }, + "maintainers": [ + { + "name": "isaacs", + "email": "i@izs.me" + } + ], + "dist": { + "shasum": "4546d1cf7d6c25bd1addd605ebc5a040778a2cd6", + "tarball": "http://registry.npmjs.org/npm-package-arg/-/npm-package-arg-1.1.0.tgz" + }, + "_resolved": "https://registry.npmjs.org/npm-package-arg/-/npm-package-arg-1.1.0.tgz" +} diff --git a/node_modules/npm-package-arg/test/basic.js b/node_modules/npm-package-arg/test/basic.js new file mode 100644 index 000000000..7c7e9a718 --- /dev/null +++ b/node_modules/npm-package-arg/test/basic.js @@ -0,0 +1,132 @@ +var npa = require("../npa.js") +var path = require("path") + +require("tap").test("basic", function (t) { + t.setMaxListeners(999) + + var tests = { + "foo@1.2": { + name: "foo", + type: "range", + spec: ">=1.2.0-0 <1.3.0-0", + raw: "foo@1.2", + rawSpec: "1.2" + }, + + "@foo/bar": { + raw: "@foo/bar", + name: "@foo/bar", + scope: "foo", + rawSpec: "", + spec: "*", + type: "range" + }, + + "@foo/bar@": { + raw: "@foo/bar@", + name: "@foo/bar", + scope: "foo", + rawSpec: "", + spec: "*", + type: "range" + }, + + "@foo/bar@baz": { + raw: "@foo/bar@baz", + name: "@foo/bar", + scope: "foo", + rawSpec: "baz", + spec: "baz", + type: "tag" + }, + + "@f fo o al/ a d s ;f ": { + raw: "@f fo o al/ a d s ;f", + name: null, + rawSpec: "@f fo o al/ a d s ;f", + spec: path.resolve("@f fo o al/ a d s ;f"), + type: "local" + }, + + "foo@1.2.3": { + name: "foo", + type: "version", + spec: "1.2.3", + raw: "foo@1.2.3" + }, + + "foo@=v1.2.3": { + name: "foo", + type: "version", + spec: "1.2.3", + raw: "foo@=v1.2.3", + rawSpec: "=v1.2.3" + }, + + "git://github.com/user/foo": { + name: null, + type: "git", + spec: "git://github.com/user/foo", + raw: "git://github.com/user/foo" + }, + + "/path/to/foo": { + name: null, + type: "local", + spec: "/path/to/foo", + raw: "/path/to/foo" + }, + + "https://server.com/foo.tgz": { + name: null, + type: "remote", + spec: "https://server.com/foo.tgz", + raw: "https://server.com/foo.tgz" + }, + + "user/foo-js": { + name: null, + type: "github", + spec: "user/foo-js", + raw: "user/foo-js" + }, + + "foo@user/foo-js": { + name: "foo", + type: "github", + spec: "user/foo-js", + raw: "foo@user/foo-js" + }, + + "foo@latest": { + name: "foo", + type: "tag", + spec: "latest", + raw: "foo@latest" + }, + + "foo": { + name: "foo", + type: "range", + spec: "*", + raw: "foo" + } + } + + Object.keys(tests).forEach(function (arg) { + var res = npa(arg) + t.type(res, "Result") + t.has(res, tests[arg]) + }) + + // Completely unreasonable invalid garbage throws an error + t.throws(function() { + npa("this is not a \0 valid package name or url") + }) + + t.throws(function() { + npa("gopher://yea right") + }, "Unsupported URL Type: gopher://yea right") + + t.end() +}) |