Skip to content

Commit

Permalink
Update to ES6 Grammar & Import ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
zsxsoft committed Apr 23, 2016
1 parent a11ac1a commit db84b1b
Show file tree
Hide file tree
Showing 24 changed files with 260 additions and 232 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/http/res/*
27 changes: 27 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"es6": true,
"browser": false,
"node": true
},
"rules": {
"curly": 0,
"comma-dangle": [2, "always-multiline"],
"comma-spacing": 0,
"eqeqeq": [2, "allow-null"],
"key-spacing": 0,
"no-underscore-dangle": 0,
"no-unused-expressions": 0,
"no-shadow": 0,
"no-shadow-restricted-names": 0,
"no-extend-native": 0,
"no-var": 2,
"new-cap": 0,
"quotes": 0,
"semi-spacing": 0,
"space-unary-ops": 0,
"space-infix-ops": 0,
"consistent-return": 0,
"strict": 0
}
}
15 changes: 8 additions & 7 deletions lib/database/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
/* global config */
/* global log */
/* global coordinator */
var fs = require("fs");
var path = require("path");
'use strict';
let fs = require("fs");
let path = require("path");

function formatContent(content) {
return '"' + content.toString().replace(/"/g, '""') + '"';
}
module.exports = {};
module.exports.init = function (callback) {
var savePath = path.resolve(config.database.savedir);
let savePath = path.resolve(config.database.savedir);
log.log("保存位置:" + savePath);
callback(null);
coordinator.on("gotDanmu", function (data) {
var date = new Date();
var joinArray = [];
coordinator.on("gotDanmu", (data) => {
let date = new Date();
let joinArray = [];
joinArray.push(formatContent(Math.round(new Date().getTime() / 1000)));
joinArray.push(formatContent(data.hash));
joinArray.push(formatContent(data.ip));
Expand All @@ -25,7 +26,7 @@ module.exports.init = function (callback) {
fs.appendFile(path.resolve(savePath, data.room + '.csv'), joinArray.join(","));
});

coordinator.on("searchDanmu", function (data) {
coordinator.on("searchDanmu", (data) => {
coordinator.emit("gotSearchDanmu", '[{"user": "ERROR", "text": "Not yet supported", "publish": ""}]');
});
};
3 changes: 2 additions & 1 deletion lib/database/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* global config */
'use strict';
module.exports = {
init: function (callback) {
require("./" + config.database.type + ".js").init(function () {
callback.apply(this, arguments);
});
}
},
};
30 changes: 15 additions & 15 deletions lib/database/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
/* global config */
/* global log */
/* global coordinator */
var SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
var mongodb = require('mongodb');
var server = new mongodb.Server(config.database.server, config.database.port, {
auto_reconnect: true
const SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
let mongodb = require('mongodb');
let server = new mongodb.Server(config.database.server, config.database.port, {
auto_reconnect: true,
});
var db = null;
var errorCounter = 0;
var firstErrorTime = new Date();
let db = null;
let errorCounter = 0;
let firstErrorTime = new Date();

var getConnection = function (callback) {
let getConnection = function (callback) {
db = new mongodb.Db(config.database.db, server, {
w: 1
w: 1,
});
db.open(function (err) {
if (err !== null) {
Expand Down Expand Up @@ -44,13 +44,13 @@ module.exports = {

coordinator.on("gotDanmu", function (data) {

var room = data.room;
let room = data.room;
db.collection(config.rooms[room].table).insert({
user: data.hash,
text: data.text,
publish: Math.round(new Date().getTime() / 1000),
ip: data.ip,
ua: data.ua
ua: data.ua,
}, function (err, results) {
if (err !== null) {
log.log("数据库写入出错");
Expand All @@ -62,11 +62,11 @@ module.exports = {

coordinator.on("searchDanmu", function (data) {

var room = data.room;
let room = data.room;
db.collection(config.rooms[room].table).find({
text: {
$regex: ".*?" + preg_quote(data.key) + ".*?"
}
$regex: ".*?" + preg_quote(data.key) + ".*?",
},
}, null, null).toArray(function (err, results) {
if (err === null) {
results.map(function (object) {
Expand All @@ -83,7 +83,7 @@ module.exports = {
});


}
},
};


Expand Down
41 changes: 21 additions & 20 deletions lib/database/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
/* global config */
/* global log */
/* global coordinator */
var SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
var mysql = require("mysql");
var pool = null;
var connection = null;
var errorCounter = 0;
var firstErrorTime = new Date();
'use strict';
const SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
let mysql = require("mysql");
let pool = null;
let connection = null;
let errorCounter = 0;
let firstErrorTime = new Date();

var createTableSql = [
let createTableSql = [
"CREATE TABLE IF NOT EXISTS `%table%` (",
"danmu_id int(11) NOT NULL AUTO_INCREMENT,",
"danmu_user varchar(255) NOT NULL DEFAULT '',",
Expand All @@ -19,11 +20,11 @@ var createTableSql = [
"danmu_useragent text NOT NULL,",
"PRIMARY KEY (danmu_id),",
"KEY danmu_TPISC (danmu_publish)",
") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;"
") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;",
].join("\n");

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

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

var getConnection = function (callback) {
var called = false;
let getConnection = function (callback) {
let called = false;
pool.getConnection(function (err, privateConnection) {
connection = privateConnection;
if (err) {
Expand Down Expand Up @@ -95,21 +96,21 @@ module.exports = {
port: config.database.port,
database: config.database.db,
acquireTimeout: config.database.timeout,
connectionLimit: 1
connectionLimit: 1,
//debug: true
});
getConnection(callback);

var keepAlive = function () {
let keepAlive = function () {
if (!connection) return;
connection.ping();
};

coordinator.on("gotDanmu", function (data) {

var room = data.room;
let room = data.room;
connection.query("INSERT INTO `%table%` (danmu_user, danmu_text, danmu_publish, danmu_ip, danmu_useragent) VALUES (?, ?, ?, ?, ?)".replace("%table%", config.rooms[room].table), [
data.hash, data.text, Math.round(new Date().getTime() / 1000), data.ip, data.ua
data.hash, data.text, Math.round(new Date().getTime() / 1000), data.ip, data.ua,
], function (err, rows) {
if (err !== null) {
log.log("数据库写入出错");
Expand All @@ -120,12 +121,12 @@ module.exports = {

coordinator.on("searchDanmu", function (data) {

var room = data.room;
let room = data.room;
connection.query('SELECT * from `%table%` where `danmu_text` LIKE ? LIMIT 20'.replace("%table%", config.rooms[room].table), [
'%' + data.key + '%'
'%' + data.key + '%',
], function (err, rows) {
if (err === null) {
var ret = [];
let ret = [];
ret = JSON.stringify(rows).replace(/"danmu_/g, '"');
coordinator.emit("gotSearchDanmu", ret);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/database/none.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module.exports = {
init: function (callback) {
log.log("无数据库");
callback(null);
}
},
};
5 changes: 3 additions & 2 deletions lib/ext/ext/autoban/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/* global config */
/* global coordinator */
/* global filter */
'use strict';
module.exports = function () {
// 弹幕被拦截达到一定次数后封号
coordinator.on("banDanmu", function (danmuData) {
Expand All @@ -13,11 +14,11 @@ module.exports = function () {
if (filter.checkUserBlock(danmuData.room, danmuData.hash)) return;

cache.get("block_" + danmuData.hash, function (err, data) {
if (err !== null && typeof err != "undefined") {
if (err !== null && typeof err !== "undefined") {
log.log("封禁用户查询失败");
console.log(err);
data = 0;
} else if (typeof data == "undefined") {
} else if (typeof data === "undefined") {
data = 0;
} else {
data = parseInt(data);
Expand Down
Loading

0 comments on commit db84b1b

Please sign in to comment.