forked from audreyt/ethercalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
175 lines (175 loc) · 5.93 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Generated by LiveScript 1.2.0
(function(){
var slice$ = [].slice;
this.__DB__ = null;
this.include = function(){
var env, ref$, redisPort, redisHost, redisPass, dataDir, services, name, items, ref1$, redis, makeClient, RedisStore, db, EXPIRE, this$ = this;
if (this.__DB__) {
return this.__DB__;
}
env = process.env;
ref$ = [env['REDIS_PORT'], env['REDIS_HOST'], env['REDIS_PASS'], env['OPENSHIFT_DATA_DIR']], redisPort = ref$[0], redisHost = ref$[1], redisPass = ref$[2], dataDir = ref$[3];
services = JSON.parse(process.env.VCAP_SERVICES || '{}');
for (name in services) {
items = services[name];
if (/^redis/.test(name) && (items != null && items.length)) {
ref1$ = [(ref$ = items[0].credentials)['port'], ref$['hostname'], ref$['password']], redisPort = ref1$[0], redisHost = ref1$[1], redisPass = ref1$[2];
}
}
redisHost == null && (redisHost = 'localhost');
redisPort == null && (redisPort = 6379);
dataDir == null && (dataDir = process.cwd());
redis = require('redis');
makeClient = function(cb){
var client;
client = redis.createClient(redisPort, redisHost);
if (redisPass) {
client.auth(redisPass, function(){
return console.log.apply(console, arguments);
});
}
if (cb) {
client.on('connect', cb);
}
return client;
};
try {
RedisStore = require('zappajs/node_modules/socket.io/lib/stores/redis');
this.io.configure(function(){
var redisClient;
redisClient = makeClient(function(){
var redisPub, redisSub, store;
redisPub = makeClient();
redisSub = makeClient();
store = new RedisStore({
redis: redis,
redisPub: redisPub,
redisSub: redisSub,
redisClient: redisClient
});
this$.io.set('store', store);
this$.io.enable('browser client etag');
this$.io.enable('browser client gzip');
this$.io.enable('browser client minification');
return this$.io.set('log level', 5);
});
return redisClient.on('error', function(){});
});
} catch (e$) {}
db = makeClient(function(){
db.DB = true;
return console.log("Connected to Redis Server: " + redisHost + ":" + redisPort);
});
EXPIRE = this.EXPIRE;
db.on('error', function(err){
var fs, Commands;
switch (false) {
case db.DB !== true:
return console.log("==> Lost connection to Redis Server - attempting to reconnect...");
case !db.DB:
return false;
}
console.log(err);
console.log("==> Falling back to JSON storage: " + dataDir + "/dump.json");
if (EXPIRE) {
console.log("==> The --expire <seconds> option requires a Redis server; stopping!");
process.exit();
}
fs = require('fs');
db.DB = {};
try {
db.DB = JSON.parse(require('fs').readFileSync(dataDir + "/dump.json", 'utf8'));
console.log("==> Restored previous session from JSON file");
} catch (e$) {}
Commands = {
bgsave: function(cb){
fs.writeFileSync(dataDir + "/dump.json", JSON.stringify(db.DB), 'utf8');
return typeof cb === 'function' ? cb() : void 8;
},
get: function(key, cb){
return typeof cb === 'function' ? cb(null, db.DB[key]) : void 8;
},
set: function(key, val, cb){
db.DB[key] = val;
return typeof cb === 'function' ? cb() : void 8;
},
rpush: function(key, val, cb){
var ref$, ref1$;
((ref1$ = (ref$ = db.DB)[key]) != null
? ref1$
: ref$[key] = []).push(val);
return typeof cb === 'function' ? cb() : void 8;
},
lrange: function(key, from, to, cb){
var ref$, ref1$;
return typeof cb === 'function' ? cb(null, (ref1$ = (ref$ = db.DB)[key]) != null
? ref1$
: ref$[key] = []) : void 8;
},
hset: function(key, idx, val){
var ref$, ref1$;
((ref1$ = (ref$ = db.DB)[key]) != null
? ref1$
: ref$[key] = [])[idx] = val;
return typeof cb === 'function' ? cb() : void 8;
},
hgetall: function(key, cb){
var ref$, ref1$;
return typeof cb === 'function' ? cb(null, (ref1$ = (ref$ = db.DB)[key]) != null
? ref1$
: ref$[key] = {}) : void 8;
},
del: function(keys, cb){
var i$, len$, yet$, key;
if (Array.isArray(keys)) {
for (yet$ = true, i$ = 0, len$ = keys.length; i$ < len$; ++i$) {
key = keys[i$];
yet$ = false;
delete db.DB[key];
} if (yet$) {
delete db.DB[keys];
}
}
return typeof cb === 'function' ? cb() : void 8;
}
};
importAll$(db, Commands);
return db.multi = function(){
var cmds, name;
cmds = slice$.call(arguments);
for (name in Commands) {
(fn$.call(this, name));
}
cmds.results = [];
cmds.exec = function(cb){
var ref$, cmd, args, this$ = this;
switch (false) {
case !this.length:
ref$ = this.shift(), cmd = ref$[0], args = ref$[1];
db[cmd].apply(db, slice$.call(args).concat([function(_, result){
this$.results.push(result);
this$.exec(cb);
}]));
break;
default:
cb(null, this.results);
}
};
return cmds;
function fn$(name){
cmds[name] = function(){
var args;
args = slice$.call(arguments);
this.push([name, args]);
return this;
};
}
};
});
return this.__DB__ = db;
};
function importAll$(obj, src){
for (var key in src) obj[key] = src[key];
return obj;
}
}).call(this);