index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict'
  2. var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
  3. var builtins = require('builtins')
  4. var blacklist = [
  5. 'node_modules',
  6. 'favicon.ico',
  7. ]
  8. function validate (name) {
  9. var warnings = []
  10. var errors = []
  11. if (name === null) {
  12. errors.push('name cannot be null')
  13. return done(warnings, errors)
  14. }
  15. if (name === undefined) {
  16. errors.push('name cannot be undefined')
  17. return done(warnings, errors)
  18. }
  19. if (typeof name !== 'string') {
  20. errors.push('name must be a string')
  21. return done(warnings, errors)
  22. }
  23. if (!name.length) {
  24. errors.push('name length must be greater than zero')
  25. }
  26. if (name.match(/^\./)) {
  27. errors.push('name cannot start with a period')
  28. }
  29. if (name.match(/^_/)) {
  30. errors.push('name cannot start with an underscore')
  31. }
  32. if (name.trim() !== name) {
  33. errors.push('name cannot contain leading or trailing spaces')
  34. }
  35. // No funny business
  36. blacklist.forEach(function (blacklistedName) {
  37. if (name.toLowerCase() === blacklistedName) {
  38. errors.push(blacklistedName + ' is a blacklisted name')
  39. }
  40. })
  41. // Generate warnings for stuff that used to be allowed
  42. // core module names like http, events, util, etc
  43. builtins({ version: '*' }).forEach(function (builtin) {
  44. if (name.toLowerCase() === builtin) {
  45. warnings.push(builtin + ' is a core module name')
  46. }
  47. })
  48. if (name.length > 214) {
  49. warnings.push('name can no longer contain more than 214 characters')
  50. }
  51. // mIxeD CaSe nAMEs
  52. if (name.toLowerCase() !== name) {
  53. warnings.push('name can no longer contain capital letters')
  54. }
  55. if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) {
  56. warnings.push('name can no longer contain special characters ("~\'!()*")')
  57. }
  58. if (encodeURIComponent(name) !== name) {
  59. // Maybe it's a scoped package name, like @user/package
  60. var nameMatch = name.match(scopedPackagePattern)
  61. if (nameMatch) {
  62. var user = nameMatch[1]
  63. var pkg = nameMatch[2]
  64. if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
  65. return done(warnings, errors)
  66. }
  67. }
  68. errors.push('name can only contain URL-friendly characters')
  69. }
  70. return done(warnings, errors)
  71. }
  72. var done = function (warnings, errors) {
  73. var result = {
  74. validForNewPackages: errors.length === 0 && warnings.length === 0,
  75. validForOldPackages: errors.length === 0,
  76. warnings: warnings,
  77. errors: errors,
  78. }
  79. if (!result.warnings.length) {
  80. delete result.warnings
  81. }
  82. if (!result.errors.length) {
  83. delete result.errors
  84. }
  85. return result
  86. }
  87. module.exports = validate