index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict'
  2. var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
  3. var builtins = require('builtins')
  4. var blacklist = [
  5. 'node_modules',
  6. 'favicon.ico'
  7. ]
  8. var validate = module.exports = function (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.forEach(function (builtin) {
  44. if (name.toLowerCase() === builtin) {
  45. warnings.push(builtin + ' is a core module name')
  46. }
  47. })
  48. // really-long-package-names-------------------------------such--length-----many---wow
  49. // the thisisareallyreallylongpackagenameitshouldpublishdowenowhavealimittothelengthofpackagenames-poch.
  50. if (name.length > 214) {
  51. warnings.push('name can no longer contain more than 214 characters')
  52. }
  53. // mIxeD CaSe nAMEs
  54. if (name.toLowerCase() !== name) {
  55. warnings.push('name can no longer contain capital letters')
  56. }
  57. if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) {
  58. warnings.push('name can no longer contain special characters ("~\'!()*")')
  59. }
  60. if (encodeURIComponent(name) !== name) {
  61. // Maybe it's a scoped package name, like @user/package
  62. var nameMatch = name.match(scopedPackagePattern)
  63. if (nameMatch) {
  64. var user = nameMatch[1]
  65. var pkg = nameMatch[2]
  66. if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
  67. return done(warnings, errors)
  68. }
  69. }
  70. errors.push('name can only contain URL-friendly characters')
  71. }
  72. return done(warnings, errors)
  73. }
  74. validate.scopedPackagePattern = scopedPackagePattern
  75. var done = function (warnings, errors) {
  76. var result = {
  77. validForNewPackages: errors.length === 0 && warnings.length === 0,
  78. validForOldPackages: errors.length === 0,
  79. warnings: warnings,
  80. errors: errors
  81. }
  82. if (!result.warnings.length) delete result.warnings
  83. if (!result.errors.length) delete result.errors
  84. return result
  85. }