Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow names of node core modules #2

Merged
merged 4 commits into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ npm install validate-npm-package-name --save
## Usage

```js
var valid = require("validate-npm-package-name")
var validate = require("validate-npm-package-name")

// regular

validate("some-package") // => {valid: true}
validate("example.com") // => {valid: true}
validate("under_score") // => {valid: true}
validate("123numeric") // => {valid: true}
validate("crazy!") // => {valid: true}

// scoped

validate("@npm/thingy") // => {valid: true}
validate("@jane/foo.js") // => {valid: true}

// valid, but reserved by node core

validate("http") // => {valid: true, warnings:["http is a node core module name"]}
validate("url") // => {valid: true, warnings:["url is a node core module name"]}

// invalid

validate("") // => {valid: false, errors:["name length must be greater than zero"]}
validate("ca$h") // => {valid: false, errors:["name can only contain URL-friendly characters"]}
validate("_flodash") // => {valid: false, errors:["name cannot start with an underscore"]}
Expand All @@ -45,7 +57,7 @@ npm test

## Dependencies

None
- [builtins](https://github.com/juliangruber/builtins): List of node.js builtin modules

## Dev Dependencies

Expand Down
14 changes: 13 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
var valid = require("./")
var validate = require("./")

// regular

validate("some-package") // => {valid: true}
validate("example.com") // => {valid: true}
validate("under_score") // => {valid: true}
validate("123numeric") // => {valid: true}
validate("crazy!") // => {valid: true}

// scoped

validate("@npm/thingy") // => {valid: true}
validate("@jane/foo.js") // => {valid: true}

// valid, but reserved by node core

validate("http") // => {valid: true, warnings:["http is a node core module name"]}
validate("url") // => {valid: true, warnings:["url is a node core module name"]}

// invalid

validate("") // => {valid: false, errors:["name length must be greater than zero"]}
validate("ca$h") // => {valid: false, errors:["name can only contain URL-friendly characters"]}
validate("_flodash") // => {valid: false, errors:["name cannot start with an underscore"]}
Expand Down
34 changes: 24 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
var scopedPackagePattern = new RegExp("^(?:@([^/]+?)[/])?([^/]+?)$");
var builtins = require("builtins")
var blacklist = [
"node_modules",
"favicon.ico"
];

var validate = module.exports = function(name, options) {

var warnings = []
var errors = []

if (!options) {
Expand All @@ -16,17 +18,17 @@ var validate = module.exports = function(name, options) {

if (name === null) {
errors.push("name cannot be null")
return done(errors)
return done(warnings, errors)
}

if (name === undefined) {
errors.push("name cannot be undefined")
return done(errors)
return done(warnings, errors)
}

if (typeof name !== "string") {
errors.push("name must be a string")
return done(errors)
return done(warnings, errors)
}

if (!name.length) {
Expand All @@ -53,12 +55,21 @@ var validate = module.exports = function(name, options) {
errors.push("name must be lowercase")
}

// No funny business
blacklist.forEach(function(blacklistedName){
if (name.toLowerCase() === blacklistedName) {
errors.push(blacklistedName + " is a blacklisted name")
}
})

// Warn on core module names
// http, events, util, domain, cluster, etc
builtins.forEach(function(builtin){
if (name.toLowerCase() === builtin) {
warnings.push(builtin + " is a node core module name")
}
})

if (encodeURIComponent(name) !== name) {

// Maybe it's a scoped package name, like @user/package
Expand All @@ -67,23 +78,26 @@ var validate = module.exports = function(name, options) {
var user = nameMatch[1]
var pkg = nameMatch[2]
if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
return done(errors)
return done(warnings, errors)
}
}

errors.push("name can only contain URL-friendly characters")
}

return done(errors)
return done(warnings, errors)

}

validate.scopedPackagePattern = scopedPackagePattern

var done = function (errors) {
if (errors.length) {
return {valid: false, errors: errors}
} else {
return {valid: true}
var done = function (warnings, errors) {
var result = {
valid: errors.length === 0,
warnings: warnings,
errors: errors
}
if (!result.warnings.length) delete result.warnings
if (!result.errors.length) delete result.errors
return result
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"directories": {
"test": "test"
},
"dependencies": {},
"dependencies": {
"builtins": "0.0.7"
},
"devDependencies": {
"tap": "^0.4.13"
},
Expand Down
63 changes: 37 additions & 26 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,83 @@
var valid = require("..")
var validate = require("..")
var test = require("tap").test
var path = require("path")
var fs = require("fs")

test("validate-npm-package-name", function (t) {
t.deepEqual(valid("some-package"), {valid: true})
t.deepEqual(valid("example.com"), {valid: true})
t.deepEqual(valid("under_score"), {valid: true})
t.deepEqual(valid("period.js"), {valid: true})
t.deepEqual(valid("123numeric"), {valid: true})
t.deepEqual(valid("crazy!"), {valid: true})
t.deepEqual(valid("@npm/thingy"), {valid: true})
t.deepEqual(valid("@npm-zors/money!time.js"), {valid: true})

t.deepEqual(valid(""), {

// Traditional

t.deepEqual(validate("some-package"), {valid: true})
t.deepEqual(validate("example.com"), {valid: true})
t.deepEqual(validate("under_score"), {valid: true})
t.deepEqual(validate("period.js"), {valid: true})
t.deepEqual(validate("123numeric"), {valid: true})
t.deepEqual(validate("crazy!"), {valid: true})

// Scoped (npm 2+)

t.deepEqual(validate("@npm/thingy"), {valid: true})
t.deepEqual(validate("@npm-zors/money!time.js"), {valid: true})

// Invalid

t.deepEqual(validate(""), {
valid: false,
errors: ["name length must be greater than zero"]})

t.deepEqual(valid(""), {
t.deepEqual(validate(""), {
valid: false,
errors: ["name length must be greater than zero"]})

t.deepEqual(valid(".start-with-period"), {
t.deepEqual(validate(".start-with-period"), {
valid: false,
errors: ["name cannot start with a period"]})

t.deepEqual(valid("_start-with-underscore"), {
t.deepEqual(validate("_start-with-underscore"), {
valid: false,
errors: ["name cannot start with an underscore"]})

t.deepEqual(valid("contain:colons"), {
t.deepEqual(validate("contain:colons"), {
valid: false,
errors: ["name can only contain URL-friendly characters"]})

t.deepEqual(valid("1234567890123456789012345678901234567890-more-than-fifty"), {
t.deepEqual(validate("1234567890123456789012345678901234567890-more-than-fifty"), {
valid: false,
errors: ["name cannot be longer than 50 characters"]})

t.deepEqual(valid(" leading-space"), {
t.deepEqual(validate(" leading-space"), {
valid: false,
errors: ["name cannot contain leading or trailing spaces", "name can only contain URL-friendly characters"]})

t.deepEqual(valid("trailing-space "), {
t.deepEqual(validate("trailing-space "), {
valid: false,
errors: ["name cannot contain leading or trailing spaces", "name can only contain URL-friendly characters"]})

t.deepEqual(valid("s/l/a/s/h/e/s"), {
t.deepEqual(validate("s/l/a/s/h/e/s"), {
valid: false,
errors: ["name can only contain URL-friendly characters"]})

t.deepEqual(valid("node_modules"), {
t.deepEqual(validate("node_modules"), {
valid: false,
errors: ["node_modules is a blacklisted name"]})

t.deepEqual(valid("favicon.ico"), {
t.deepEqual(validate("favicon.ico"), {
valid: false,
errors: ["favicon.ico is a blacklisted name"]})

// Sketchy

t.deepEqual(validate("http"), {
valid: true,
warnings: ["http is a node core module name"]})

// Legacy Mixed-Case

t.deepEqual(valid("CAPITAL-LETTERS", {allowMixedCase: true}), {valid: true})
t.deepEqual(validate("CAPITAL-LETTERS", {allowMixedCase: true}), {valid: true})

t.deepEqual(valid("CAPITAL-LETTERS"), {
t.deepEqual(validate("CAPITAL-LETTERS"), {
valid: false,
errors: ["name must be lowercase"]})




t.end()
})