1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var stylize;
|
7 | ( function ()
|
8 | {
|
9 | var styles =
|
10 | {
|
11 | 'bold' : '1',
|
12 | 'italic' : '3',
|
13 | 'underline' : '4',
|
14 |
|
15 | 'grey' : '90',
|
16 |
|
17 | 'red' : '1;31',
|
18 | 'green' : '1;32',
|
19 | 'yellow' : '1;33',
|
20 | 'blue' : '1;34',
|
21 | 'magenta' : '1;35',
|
22 | 'cyan' : '1;36',
|
23 | 'white' : '1;37'
|
24 | };
|
25 |
|
26 | stylize = function ( str, style )
|
27 | {
|
28 | return '\x1B[' + styles[style] + 'm' + str +
|
29 | '\x1B[0m';
|
30 | };
|
31 | }
|
32 | () );
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.pretty = function ( options )
|
39 | {
|
40 | var pattern, wrap, label;
|
41 |
|
42 | if ( typeof options === 'string' )
|
43 | options = { pattern : options };
|
44 |
|
45 | pattern = ( options && options.pattern ) || "*";
|
46 | wrap = ( options && options.wrap ) || 4;
|
47 | label = ( options && options.label ) || "keyspace " + pattern;
|
48 |
|
49 | this.send_command ( "FAKE_DUMP", [ pattern || "*" ], function ( err, dump )
|
50 | {
|
51 | var i, n = dump && dump.length, style, key, ttl, type, value;
|
52 |
|
53 | if ( err )
|
54 | throw err;
|
55 | if ( label )
|
56 | process.stdout.write ( '\n' + stylize ( label, 'bold' ) + ':\n\n' );
|
57 | else
|
58 | process.stdout.write ( '\n' );
|
59 |
|
60 | for ( i = 0; i < n; i += 4 )
|
61 | {
|
62 | key = dump [ i ];
|
63 | ttl = dump [ i + 1 ];
|
64 | type = dump [ i + 2 ];
|
65 | value = dump [ i + 3 ];
|
66 |
|
67 | style = 'white';
|
68 |
|
69 | if ( type === 'list' )
|
70 | style = 'green';
|
71 |
|
72 | else if ( type === 'hash' )
|
73 | style = 'yellow';
|
74 |
|
75 | else if ( type === 'set' )
|
76 | style = 'cyan';
|
77 |
|
78 | else if ( type === 'zset' )
|
79 | style = 'red';
|
80 |
|
81 | process.stdout.write
|
82 | (
|
83 | stylize ( type, 'bold' )
|
84 | + '\t' + stylize ( key, 'bold' )
|
85 | + '\n' + stylize ( ttl, ttl >= 0 ? 'italic' : 'grey' )
|
86 | + '\t' +
|
87 | (
|
88 | value.map
|
89 |
|
90 | ? value.map ( function ( member, index )
|
91 | {
|
92 | return ( wrap && index && !( ( index ) % wrap ) ? '\n\t' : '' ) + stylize ( member, style );
|
93 | })
|
94 | .join ( ',\t' )
|
95 |
|
96 | : stylize ( value, style )
|
97 | )
|
98 | + '\n\n'
|
99 | );
|
100 | }
|
101 | });
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | exports.getKeyspace = function ( options, callback )
|
109 | {
|
110 | var cb;
|
111 |
|
112 | if ( !callback && typeof options === 'function' )
|
113 | {
|
114 | callback = options;
|
115 | options = null;
|
116 | }
|
117 |
|
118 | if ( typeof options === 'string' )
|
119 | options = { pattern : options };
|
120 | if ( !callback || typeof callback !== 'function' )
|
121 | throw new Error ( "You didn't provide a valid callback." );
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | cb = callback;
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | if ( options && options.map )
|
132 | cb = function ( err, data )
|
133 | {
|
134 | var out, i, n;
|
135 | if ( data )
|
136 | {
|
137 | out = {};
|
138 | n = data.length;
|
139 | for ( i = 0; i < n; i += 4 )
|
140 | out [ data [ i ] ] = data [ i + 3 ];
|
141 | }
|
142 |
|
143 | callback ( err, out );
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | else if ( options && options.group )
|
150 | cb = function ( err, data )
|
151 | {
|
152 | var out, i, n;
|
153 | if ( data )
|
154 | {
|
155 | out = [];
|
156 | n = data.length;
|
157 | for ( i = 0; i < n; i += 4 )
|
158 | out.push ( data.slice ( i, 4 ) );
|
159 | }
|
160 |
|
161 | callback ( err, out );
|
162 | };
|
163 |
|
164 |
|
165 | this.send_command ( "FAKE_DUMP", [ options && options.pattern || "*" ], cb );
|
166 | };
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | exports.serveKeyspace = function ( port )
|
173 | {
|
174 | var self = this,
|
175 | url = require ( "url" );
|
176 |
|
177 | require ( "http" ).createServer
|
178 | (
|
179 | function ( req, res )
|
180 | {
|
181 | var data = url.parse ( req.url, true );
|
182 |
|
183 | if ( data.pathname !== '/keyspace.json' )
|
184 | {
|
185 | res.statusCode = 404;
|
186 | res.end ( "Not found." );
|
187 | return;
|
188 | }
|
189 |
|
190 | if ( req.method !== 'GET' )
|
191 | {
|
192 | res.statusCode = 405;
|
193 | res.end ( "Method not supported." );
|
194 | return;
|
195 | }
|
196 |
|
197 | self.getKeyspace ( data.query, function ( err, data )
|
198 | {
|
199 | if ( err )
|
200 | {
|
201 | res.statusCode = 500;
|
202 | res.end ( err );
|
203 | return;
|
204 | }
|
205 |
|
206 | res.setHeader ( "Content-Type", "application/json" );
|
207 | res.end ( JSON.stringify ( data ) );
|
208 | });
|
209 | }
|
210 | )
|
211 | .listen ( port );
|
212 | };
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|