-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.coffee
240 lines (215 loc) · 5.34 KB
/
server.coffee
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
process.title = 'redis-viewer'
argv = require('optimist').argv
ip = argv.ip ? argv.i ? '127.0.0.1'
port = argv.port ? argv.p ? 6379
pass = argv.pass ? argv.x ? null
console.log("observing #{ip}:#{port}")
express = require('express')
redis = require('redis')
io = require('socket.io')
_ = require('underscore')
# redis.debug_mode = true
redis_client = redis.createClient(port, ip)
if pass?
redis_client.auth pass
redis_client.on 'error', (error) ->
console.log error
return
app = express.createServer()
app.use(express.logger({ 'format': ':method :url' })) #'
app.use(express.favicon())
app.use(express.compiler(src: __dirname + '/src', dest: __dirname + '/public', enable: ['less', 'coffeescript']))
app.use(express.static(__dirname + '/public'))
app.listen(3000)
console.log('listening on :3000')
socket = io.listen(app)
# map the command to the type of data it'll return
reply_types =
APPEND: 'integer'
AUTH: 'status'
BGREWRITEAOF: 'status'
BGSAVE: 'status'
BLPOP: null
BRPOP: null
BRPOPLPUSH: null
'CONFIG GET': null
'CONFIG SET': null
'CONFIG RESETSTAT': null
DBSIZE: 'integer'
DECR: 'integer'
DECRBY: 'integer'
DEL: 'integer'
DISCARD: 'status'
ECHO: 'bulk'
EXEC: null
EXISTS: 'integer'
EXPIRE: 'integer'
EXPIREAT: 'integer'
FLUSHALL: 'status'
FLUSHDB: 'status'
GET: 'bulk'
GETBIT: 'integer'
GETRANGE: 'bulk'
GETSET: 'bulk'
HDEL: 'integer'
HEXISTS: 'integer'
HGET: 'bulk'
HGETALL: 'hash'
HINCRBY: 'integer'
HKEYS: 'list'
HLEN: 'integer'
HMGET: 'list'
HMSET: 'status'
HSET: 'integer'
HSETNX: 'integer'
HVALS: 'list'
INCR: 'integer'
INCRBY: 'integer'
INFO: 'bulk'
KEYS: 'keys'
LASTSAVE: 'integer'
LINDEX: 'bulk'
LINSERT: 'integer'
LLEN: 'integer'
LPOP: 'bulk'
LPUSH: 'integer'
LPUSHX: 'integer'
LRANGE: 'list'
LREM: 'integer'
LSET: 'status'
LTRIM: 'status'
MGET: 'list'
MONITOR: null
MOVE: 'integer'
MSET: 'status'
MSETNX: 'integer'
MULTI: 'status'
OBJECT: null
PERSIST: 'integer'
PING: 'status'
PSUBSCRIBE: null
PUBLISH: 'integer'
PUNSUBSCRIBE: null
QUIT: 'status'
RANDOMKEY: 'bulk'
RENAME: 'status'
RENAMENX: 'integer'
RPOP: 'bulk'
RPOPLPUSH: 'bulk'
RPUSH: 'integer'
RPUSHX: 'integer'
SADD: 'integer'
SAVE: null
SCARD: 'integer'
SDIFF: 'list'
SDIFFSTORE: 'integer'
SELECT: 'status'
SET: 'status'
SETBIT: 'integer'
SETEX: 'status'
SETNX: 'integer'
SETRANGE: 'integer'
SHUTDOWN: 'status'
SINTER: 'list'
SINTERSTORE: 'integer'
SISMEMBER: 'integer'
SLAVEOF: 'status'
SMEMBERS: 'set'
SMOVE: 'integer'
SORT: 'list'
SPOP: 'bulk'
SRANDMEMBER: 'bulk'
SREM: 'integer'
STRLEN: 'integer'
SUBSCRIBE: null
SUNION: 'list'
SUNIONSTORE: 'integer'
SYNC: null
TTL: 'integer'
TYPE: 'status'
UNSUBSCRIBE: null
UNWATCH: 'status'
WATCH: 'status'
ZADD: 'integer'
ZCARD: 'integer'
ZCOUNT: 'integer'
ZINCRBY: 'bulk'
ZINTERSTORE: 'integer'
ZRANGE: 'zset'
ZRANGEBYSCORE: 'zset'
ZRANK: 'integer'
ZREM: 'integer'
ZREMRANGEBYRANK: 'integer'
ZREMRANGEBYSCORE: 'integer'
ZREVRANGE: 'zset'
ZREVRANGEBYSCORE: 'zset'
ZREVRANK: 'integer'
ZSCORE: 'bulk'
ZUNIONSTORE: 'integer'
key_command_map =
string: 'GET'
hash: 'HGETALL'
list: 'LRANGE'
set: 'SMEMBERS'
zset: 'ZRANGE'
socket.on 'connection', (client) ->
client.on 'message', (message) ->
args = message.match(/(["'])(?:\\\1|.)*?\1|\S+/g) #"
command = args.shift().toUpperCase()
if command is 'CONFIG'
command += " #{args.shift().toUpperCase()}"
args = _.map args, (arg) ->
return arg.replace(/^(["'])(.*?)\1$/g, '$2') #"
return if not command of reply_types
reply_type = reply_types[command] or 'bulk'
redis_client.send_command command, args, (error, reply) ->
if command is 'KEYS'
_.each reply, (key) ->
redis_client.TYPE key, (error, type) ->
client.send
title: message
reply: key
reply_type: reply_type
key_command: key_command_map[type]
type: type
return
if reply_type is 'zset' and _.include args, 'WITHSCORES' # handle scores in zsets as keys
vals = _.select reply, (val, i) ->
return i % 2 == 0
scores = _.select reply, (score, i) ->
return i % 2 == 1
rows = []
_.each vals, (val, i) ->
rows.push({ index: i, val: val, score: scores[i] })
reply = rows
else if reply_type is 'hash' or reply_type is 'list' or reply_type is 'set' or reply_type is 'zset'
rows = []
_.each reply, (val, key) ->
try
val = val.replace(/</g, '<').replace(/>/g, '>')
val = JSON.stringify(JSON.parse(val), null, 2)
rows.push({ index: key, val: val })
return
reply = rows
else
if reply?
try # attempt to parse bulk values containing JSON
reply = reply.replace(/</g, '<').replace(/>/g, '>')
reply = JSON.stringify(JSON.parse(reply), null, 2)
else
reply = '(nil)'
if not error?
response =
title: message
key: args[0]
reply: reply
reply_type: reply_type
else
response =
title: message
reply: error.message
reply_type: 'error'
client.send response
return
return
return