validate-npm-package-name 源码解析
还是老方式拿到一个包
先到 npm 去看下使用文档,看下事例,api 啥的有个大体了解
到 git 看源码,对项目目录整体有个了解
看看 package.json,对项目依赖有个大体了解
看看 test 测试用例,了解使用方法
clone 到本地断点调试下,看下主体流程,不用太细
然后再去关注局部或者每个方法
看了 npm 的 api 和使用例子如下
javascript
var validate = require('validate-npm-package-name')
validate('some-package')
validate('example.com')
validate('under_score')
validate('123numeric')
validate('excited!')
validate('@npm/thingy')
validate('@jane/foo.js')
由此可以看出,这个包的作用是检测 npm 包名是否符合规范,由于功能也比较简单就直接上源码注释
javascript
'use strict'
var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
// node的内部核心包
var builtins = require('builtins')
var blacklist = ['node_modules', 'favicon.ico']
// 定义导出函数
var validate = (module.exports = function (name) {
var warnings = []
var errors = []
// 不能为null
if (name === null) {
errors.push('name cannot be null')
return done(warnings, errors)
}
// 不能为undefined
if (name === undefined) {
errors.push('name cannot be undefined')
return done(warnings, errors)
}
// 必须是字符串
if (typeof name !== 'string') {
errors.push('name must be a string')
return done(warnings, errors)
}
// 长度不能为0
if (!name.length) {
errors.push('name length must be greater than zero')
}
// 不能以.开头
if (name.match(/^\./)) {
errors.push('name cannot start with a period')
}
// 不能以下滑线开头
if (name.match(/^_/)) {
errors.push('name cannot start with an underscore')
}
// 不能空格
if (name.trim() !== name) {
errors.push('name cannot contain leading or trailing spaces')
}
// No funny business
blacklist.forEach(function (blacklistedName) {
if (name.toLowerCase() === blacklistedName) {
errors.push(blacklistedName + ' is a blacklisted name')
}
})
// Generate warnings for stuff that used to be allowed
// 不能是内部node包
builtins.forEach(function (builtin) {
if (name.toLowerCase() === builtin) {
warnings.push(builtin + ' is a core module name')
}
})
// really-long-package-names-------------------------------such--length-----many---wow
// 长度不能大于214
if (name.length > 214) {
warnings.push('name can no longer contain more than 214 characters')
}
// 不能大小混合
if (name.toLowerCase() !== name) {
warnings.push('name can no longer contain capital letters')
}
// 这些符合不能有
if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) {
warnings.push('name can no longer contain special characters ("~\'!()*")')
}
if (encodeURIComponent(name) !== name) {
// Maybe it's a scoped package name, like @user/package
var nameMatch = name.match(scopedPackagePattern)
if (nameMatch) {
var user = nameMatch[1]
var pkg = nameMatch[2]
if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
return done(warnings, errors)
}
}
errors.push('name can only contain URL-friendly characters')
}
return done(warnings, errors)
})
validate.scopedPackagePattern = scopedPackagePattern
var done = function (warnings, errors) {
var result = {
validForNewPackages: errors.length === 0 && warnings.length === 0,
validForOldPackages: errors.length === 0,
warnings: warnings,
errors: errors,
}
if (!result.warnings.length) delete result.warnings
if (!result.errors.length) delete result.errors
return result
}
小结
整体而言就是对包名的一个规则校验