Skip to content

Commit

Permalink
Fix all files by eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxsoft committed Aug 19, 2017
1 parent 4f7bcfa commit 8906b5e
Show file tree
Hide file tree
Showing 31 changed files with 378 additions and 424 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.vs
.idea
.vscode
*.csv

# Logs
Expand Down Expand Up @@ -77,4 +80,4 @@ $RECYCLE.BIN/
*.lnk

# Visual Studio Code
typings
typings
6 changes: 2 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// / <reference path="typings/main.d.ts" />
'use strict'
const os = require('os')
const async = require('async')
const fs = require('fs')
Expand Down Expand Up @@ -36,13 +34,13 @@ global.Promise = require('bluebird')
console.log('检测到配置在环境变量内的MongoDB,自动使用之。')
}

// 加载模块
// 加载模块
async.map(['extensions', 'libraries/cache', 'libraries/transfer', 'libraries/database', 'libraries/http', 'libraries/socket'], (mdl, callback) => {
require(`./src/${mdl}`).init(callback)
}, err => {
if (err) throw err
fs.readdir(path.join(__dirname, './src/controllers'), (err, files) => {
err // eat it
if (err) throw err
files.forEach((filename) => require(path.join(__dirname, './src/controllers', filename)))
})
configEvent.updated.emit()
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/DanmuController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ danmuEvent.addSingle.listen((data, inputs = {}, extra = {
const roomConfig = config.rooms[room]
const realFilter = filter(room)
if (!roomConfig.permissions.send) {
return reject('弹幕暂时被关闭')
return reject(new Error('弹幕暂时被关闭'))
}
if (extra.isAdvanced) {
if (extra.password !== roomConfig.advancedpassword) {
return reject('高级弹幕密码错误!')
return reject(new Error('高级弹幕密码错误!'))
}
}
if (!extra.isAdvanced && data.text.length > roomConfig.textlength) {
return reject(`弹幕长度大于${roomConfig.textlength}个字,可能影响弹幕观感,请删减。`)
return reject(new Error(`弹幕长度大于${roomConfig.textlength}个字,可能影响弹幕观感,请删减。`))
}
if (realFilter.checkUserIsBlocked(data.hash) || !realFilter.validateText(data.text)) {
log.log(`拦截 ${data.hash} - ${data.text}`)
danmuEvent.ban.emit(data)
return reject('发送失败!\n请检查你发送的弹幕有无关键词,或确认自己未被封禁。')
return reject(new Error('发送失败!\n请检查你发送的弹幕有无关键词,或确认自己未被封禁。'))
}

permissions.forEach((val) => {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ configEvent.unblockUser.listen((room, username) => {
resolve()
} else {
log.log(`黑名单中未搜索到${username}`)
reject()
reject(new Error(username))
}
})
})
4 changes: 2 additions & 2 deletions src/extensions/audit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ function Audit () {
let danmuKeys = Object.keys(config.rooms)

app.get('/audit', (req, res, next) => fs.readFile(path.join(__dirname, './audit.html'), (err, data) => {
err
if (err) throw err
res.end(data)
}))

// Remove all listeners to gotDanmu and bind to a new listener.
// Remove all listeners to gotDanmu and bind to a new listener.
const danmuEvents = danmuEvent.get.listeners()
danmuEvent.get.removeAllListeners()
danmuEvents.forEach(event => auditEvent.passed.listen(event))
Expand Down
24 changes: 12 additions & 12 deletions src/extensions/weibo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ passport.deserializeUser(function (obj, callback) {
});
*/
Passport.use(new PassportSina(config.ext.weibo,
function (accessToken, refreshToken, profile, callback) {
process.nextTick(function () {
return callback(null, {
accessToken: accessToken,
profile: profile
})
function (accessToken, refreshToken, profile, callback) {
process.nextTick(function () {
return callback(null, {
accessToken: accessToken,
profile: profile
})
}))
})
}))

module.exports = function () {
httpEvent.beforeRoute.listen(app => {
Expand All @@ -42,7 +42,7 @@ module.exports = function () {
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 24 * 60 * 60 * 1000 // 1 day
maxAge: 24 * 60 * 60 * 1000 // 1 day
}
}))
app.use(Passport.initialize())
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = function () {
id: data.profile.id,
nick: data.profile.screen_name
}), 24 * 60 * 60, (err, data) => {
err
err // eslint-disable-line no-unused-expressions
// Do nothing
// eat it
})
Expand All @@ -84,7 +84,7 @@ module.exports = function () {
})(req, res, next)
})
app.use(function (req, res, next) {
// 这里用来给req添加函数
// 这里用来给req添加函数
req.getSina = function (callback) {
if (typeof req.cookies.weibo !== 'undefined') {
cache.cache().get('weibo_' + req.cookies.weibo, function (err, data) {
Expand All @@ -103,7 +103,7 @@ module.exports = function () {
next()
})

// 未登录时直接跳转到新浪微博
// 未登录时直接跳转到新浪微博
app.get('/', (req, res, next) => {
async.waterfall([
function (callback) {
Expand All @@ -112,7 +112,7 @@ module.exports = function () {
function (data, callback) {
if (data === false) {
fs.readFile(path.join(__dirname, './login.html'), function (err, data) {
err // eat error
err // eslint-disable-line no-unused-expressions
res.end(data)
})
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Base {
listen: fun => {
callback = fun
},
wait: function () {
return callback(...arguments)
wait: function (...args) {
return callback(...args) // eslint-disable-line standard/no-callback-literal
}
}
return callbackObject[eventName]
Expand Down
14 changes: 6 additions & 8 deletions src/libraries/cache/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// / <reference path="../../typings/main.d.ts" />
'use strict'
var cache = null
let config = require('../../../config')
let memoryCacheMap = new Map()
let cache = null
const config = require('../../../config')
const memoryCacheMap = new Map()

module.exports = {
init: function (callback) {
Expand All @@ -11,9 +10,9 @@ module.exports = {
cache = new (require('memcached'))(config.cache.host)
break
case 'aliyun':
let ALY = require('aliyun-sdk')
let PORT = config.cache.host.split(':')[1]
let HOST = config.cache.host.split(':')[0]
const ALY = require('aliyun-sdk')
const PORT = config.cache.host.split(':')[1]
const HOST = config.cache.host.split(':')[0]
cache = ALY.MEMCACHED.createClient(PORT, HOST, {
username: config.cache.authUser,
password: config.cache.authPassword
Expand All @@ -36,4 +35,3 @@ module.exports = {
module.exports.cache = () => {
return cache
}

8 changes: 2 additions & 6 deletions src/libraries/database/csv.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// / <reference path="../../typings/main.d.ts" />

'use strict'

const fs = require('fs')
const path = require('path')
const danmuEvent = require('../../interfaces/Danmu')
const log = require('../../utilities/log')
let config = require('../../../config')
const config = require('../../../config')

function formatContent (content) {
return '"' + content.toString().replace(/"/g, '""') + '"'
Expand All @@ -25,7 +21,7 @@ module.exports.init = function (callback) {
joinArray.push(formatContent(data.text))
joinArray.push('\r\n')
fs.appendFile(path.resolve(savePath, data.room + '.csv'), joinArray.join(','), err => {
err // Do nothing here
log.log(err)
})
})

Expand Down
4 changes: 1 addition & 3 deletions src/libraries/database/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// / <reference path="../../typings/main.d.ts" />
'use strict'
let config = require('../../../config')
const config = require('../../../config')

module.exports = {
init: function (callback) {
Expand Down
32 changes: 15 additions & 17 deletions src/libraries/database/mongo.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// / <reference path="../../typings/main.d.ts" />

const mongodb = require('mongodb')
const danmuEvent = require('../../interfaces/Danmu')
const log = require('../../utilities/log')
let config = require('../../../config')
const config = require('../../../config')
let db = null

const server = new mongodb.Server(config.database.server, config.database.port, {
Expand Down Expand Up @@ -32,7 +30,7 @@ const getConnection = function (callback) {
callback.apply(callback, arguments)
log.log('数据库连接成功')
})
// callback(null);
// callback(null);
}

module.exports = {
Expand Down Expand Up @@ -78,19 +76,19 @@ module.exports = {
}

function pregQuote (str, delimiter) {
// discuss at: http://phpjs.org/functions/preg_quote/
// original by: booeyOH
// improved by: Ates Goral (http://magnetiq.com)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Onno Marsman
// example 1: preg_quote("$40");
// returns 1: '\\$40'
// example 2: preg_quote("*RRRING* Hello?");
// returns 2: '\\*RRRING\\* Hello\\?'
// example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
// returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:'
// discuss at: http://phpjs.org/functions/preg_quote/
// original by: booeyOH
// improved by: Ates Goral (http://magnetiq.com)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Onno Marsman
// example 1: preg_quote("$40");
// returns 1: '\\$40'
// example 2: preg_quote("*RRRING* Hello?");
// returns 2: '\\*RRRING\\* Hello\\?'
// example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
// returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:'

return String(str)
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&')
.replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&')
}
21 changes: 9 additions & 12 deletions src/libraries/database/mysql.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// / <reference path="../../typings/main.d.ts" />

'use strict'
const SECONDS_IN_DAY = 24 * 60 * 60 * 1000
const mysql = require('mysql')
const async = require('async')
const danmuEvent = require('../../interfaces/Danmu')
const log = require('../../utilities/log')
let config = require('../../../config')
const config = require('../../../config')

let pool = null
let connection = null
let errorCounter = 0
let firstErrorTime = new Date()

let createTableSql = [
const createTableSql = [
'CREATE TABLE IF NOT EXISTS `%table%` (',
'danmu_id int(11) NOT NULL AUTO_INCREMENT,',
"danmu_user varchar(255) NOT NULL DEFAULT '',",
Expand All @@ -26,8 +23,8 @@ let createTableSql = [
') ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;'
].join('\n')

let createDatabase = function (callbackOrig) {
let asyncList = Object.keys(config.rooms)
const createDatabase = function (callbackOrig) {
const asyncList = Object.keys(config.rooms)
async.each(asyncList, (room, callback) => {
connection.query('SELECT MAX(danmu_id) FROM `' + config.rooms[room].table + '`', function (err, rows) {
if (err !== null) {
Expand All @@ -44,7 +41,7 @@ let createDatabase = function (callbackOrig) {
})
}

let dbErrorHandler = function (err) {
const dbErrorHandler = function (err) {
if (err !== null) {
if (err.errno !== 'ECONNRESET') { // 部分MySQL会自动超时,此时要重连但不计errorCounter
if (errorCounter === 0 || new Date() - firstErrorTime >= SECONDS_IN_DAY) {
Expand All @@ -66,7 +63,7 @@ let dbErrorHandler = function (err) {
}
}

let getConnection = function (callback) {
const getConnection = function (callback) {
let called = false
pool.getConnection((err, privateConnection) => {
connection = privateConnection
Expand Down Expand Up @@ -99,7 +96,7 @@ module.exports = {
database: config.database.db,
acquireTimeout: config.database.timeout,
connectionLimit: 1
// debug: true
// debug: true
})
getConnection(callback)

Expand Down Expand Up @@ -139,7 +136,7 @@ module.exports = {

getConnection()
setInterval(keepAlive, config.database.timeout)
// connection.on("error", dbErrorHandler);
// connectDataBase(callback);
// connection.on("error", dbErrorHandler);
// connectDataBase(callback);
}
}
24 changes: 10 additions & 14 deletions src/libraries/http/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// / <reference path="../../typings/node/node.d.ts"/>
'use strict'
const fs = require('fs')
const express = require('express')
const logger = require('morgan')
const errorHandler = require('errorhandler')
const path = require('path')
const app = express()
Expand All @@ -14,16 +11,16 @@ let config = require('../../../config')
module.exports = {
init: function (callback) {
app
.engine('.html', require('ejs').__express)
// .use(logger('dev'))
.use(bodyParser.json())
.use(bodyParser.urlencoded({
extended: true
}))
.use(errorHandler())
.set('view engine', 'html')
.set('views', path.join(__dirname, './view/'))
.use(express.static(path.join(__dirname, './res/')))
.engine('.html', require('ejs').__express)
// .use(logger('dev'))
.use(bodyParser.json())
.use(bodyParser.urlencoded({
extended: true
}))
.use(errorHandler())
.set('view engine', 'html')
.set('views', path.join(__dirname, './view/'))
.use(express.static(path.join(__dirname, './res/')))

httpEvent.beforeRoute.emit(app)

Expand All @@ -45,4 +42,3 @@ module.exports = {
})
}
}

Loading

0 comments on commit 8906b5e

Please sign in to comment.