我已经编写了一段Node,它充当了我所拥有的GitHub存储库的web钩子。基本上,当提交到该回购时,会向我的函数发出请求,然后查找已签入的潜在敏感数据。我正在对GitHub API进行多次调用,以提取数据。我试图通过使用异步包的waterfall和each函数来处理所有这些调用。
瀑布调用有两个部分:获取提交数据和获取文件数据。在“文件数据”部分中,我有一个异步each,用于获取每个文件的实际文件内容。
我想知道是否有人能看到我在JavaScript编程中没有遵循的任何明显的最佳实践。我是否以正确的方式处理异步调用?我创建了两个助手函数来尝试保持事物的清洁/可读性--我是否应该将其他任何部分分解成它们自己的函数呢?
var https = require('https')
var async = require('async')
module.exports = function (ctx,cb) {
console.log("**********************************************")
var danger = ['password','pass','pw','pwd','passwd','key','user','username','secret']
var re = new RegExp(danger.join('|'))
async.waterfall([
function(callback){
var options = {
host: 'api.github.com',
path: getApiPath(ctx.data.head_commit.url),
headers: {
'User-Agent': 'Broham'
}
}
getData(options, function(commitData){
// console.log("commit data", commitData)
callback(null,commitData.files)
})
},
//for each file, get the file content and look for potentially sensitive information that has been commited
function(files, callback){
// console.log("files", files)
var review = []
async.each(files, function(file, fileCallback){
var fileOptions = {
host: 'api.github.com',
path: file.contents_url.replace('https://api.github.com', ''),
headers: {
'User-Agent': 'Broham'
}
}
// get the JSON that will have the URL for the raw file content
getData(fileOptions, function(fileData){
// get the raw file content
https.get(fileData.download_url, function(contentData){
contentData.setEncoding('utf-8')
var contents = ''
contentData.on('data', function (data) {
contents += data
})
contentData.on('end', function(){
console.log('contents', contents)
// check to see if there are any matches for the terms we want to review.
matches = contents.match(re)
if(matches.length > 0){
// console.log("matches", matches)
review = review.concat(contents.match(re))
}
fileCallback()
})
})
})
}, function(err){
callback(null, review)
})
}
], function(err, results){
if(results.length > 0){
console.log("For review:", results)
}
cb(null, 'GitEye complete!')
console.log("**********************************************")
})
}
function getData(options, callback){
https.get(options, function(res){
res.setEncoding('utf-8')
var body = ''
res.on('data', function (data) {
body += data
})
res.on('end', function(){
// console.log('body', body)
var data = JSON.parse(body);
callback(data)
})
})
}
function getApiPath(url){
var pieces = url.split('/')
var user = pieces[3]
var repo = pieces[4]
var commit = pieces[6]
// return "https://api.github.com/repos/" + user + "/" + repo + "/commits/" + commit
return "/repos/" + user + "/" + repo + "/commits/" + commit
}发布于 2017-09-24 13:12:23
我看到您根据一系列的术语构建了一个regex:
变量危险= “密码”、“通行证”、“-、”、“passwd”、“键”、“用户”、“用户名”、“保密” var re =新RegExp(danger.join(‘var’))
使用这种方法,danger中的一些项目是多余的,因为“密码”和“密码”已经被"pass“匹配了,"pwd”已经被"pw“匹配,”用户名“已经被”用户“匹配了。
总的来说,这种方法看上去相当原始,我怀疑它会产生太多的假阳性,而不是有用的。
在此代码中,contents.match(re)被执行了两次:
matches = contents.match(re) if(matches.length > 0){ // console.log(“匹配”,matches) review = review.concat(contents.match(re)) }
这是一种浪费,最好使用matches来代替第二个调用。
中的价值
非空数组是真实的,所以您可以编写简单的if(matches.length > 0)而不是if (matches.length),甚至只编写if (matches)。代码后面的if(results.length > 0)也是如此。
https://codereview.stackexchange.com/questions/176171
复制相似问题