jsql-neo 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -94
- package/bin/jsql +138 -0
- package/index.d.ts +351 -0
- package/index.js +18 -0
- package/lib/migrate.js +242 -0
- package/lib/mysql_server.js +335 -6
- package/lib/redis_server.js +448 -0
- package/lib/sql.js +533 -87
- package/lib/table.js +4 -2
- package/lib/web_ui.js +226 -0
- package/package.json +66 -47
- package/test/smoke.js +58 -0
- package/wasm/browser.d.ts +88 -0
- package/wasm/browser.mjs +404 -0
- package/wasm/browser_bg.mjs +462 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* JSQL-NEO Redis-compatible server (RESP2, zero dependencies).
|
|
3
|
+
*
|
|
4
|
+
* const { createRedisServer } = require('jsql-neo');
|
|
5
|
+
* const srv = createRedisServer({ port: 6379, dataDir: './data' });
|
|
6
|
+
* srv.listen();
|
|
7
|
+
*
|
|
8
|
+
* Supported commands:
|
|
9
|
+
* PING, ECHO, SET, GET, SETNX, DEL, EXISTS, KEYS, TYPE, EXPIRE, TTL, PERSIST,
|
|
10
|
+
* INCR, DECR, INCRBY, DECRBY, APPEND, STRLEN,
|
|
11
|
+
* HSET, HGET, HGETALL, HDEL, HEXISTS, HLEN, HKEYS, HVALS,
|
|
12
|
+
* LPUSH, RPUSH, LPOP, RPOP, LLEN, LRANGE, LINDEX, LREM,
|
|
13
|
+
* SADD, SREM, SMEMBERS, SISMEMBER, SCARD,
|
|
14
|
+
* DBSIZE, FLUSHALL, FLUSHDB, SELECT, INFO, AUTH, QUIT
|
|
15
|
+
*
|
|
16
|
+
* Persistence: keys are held in memory and snapshotted to data.rdb.json
|
|
17
|
+
* (debounced 500ms) plus a final snapshot on shutdown.
|
|
18
|
+
*/
|
|
19
|
+
const net = require('net');
|
|
20
|
+
const fs = require('fs');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
|
|
23
|
+
const TYPE_SIGNATURES = {
|
|
24
|
+
PING: 0, ECHO: 1, SET: 2, SETNX: 2, GET: 1, DEL: -1, EXISTS: -1, KEYS: 1,
|
|
25
|
+
TYPE: 1, EXPIRE: 2, TTL: 1, PERSIST: 1,
|
|
26
|
+
INCR: 1, DECR: 1, INCRBY: 2, DECRBY: 2, APPEND: 2, STRLEN: 1,
|
|
27
|
+
HSET: -3, HGET: 2, HGETALL: 1, HDEL: -2, HEXISTS: 2, HLEN: 1, HKEYS: 1, HVALS: 1,
|
|
28
|
+
LPUSH: -2, RPUSH: -2, LPOP: 1, RPOP: 1, LLEN: 1, LRANGE: 3, LINDEX: 2, LREM: 3,
|
|
29
|
+
SADD: -2, SREM: -2, SMEMBERS: 1, SISMEMBER: 2, SCARD: 1,
|
|
30
|
+
DBSIZE: 0, FLUSHALL: 0, FLUSHDB: 0, SELECT: 1, INFO: 0, AUTH: 1, QUIT: 0,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
class RedisServer {
|
|
34
|
+
constructor(opts = {}) {
|
|
35
|
+
this.port = opts.port || 6379;
|
|
36
|
+
this.host = opts.host || '127.0.0.1';
|
|
37
|
+
this.password = opts.password || null;
|
|
38
|
+
this.dataDir = opts.dataDir || null;
|
|
39
|
+
this.onQuery = opts.onQuery || null;
|
|
40
|
+
this.db = new Map();
|
|
41
|
+
this.snapshotTimer = null;
|
|
42
|
+
this._load();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
snapshotPath() {
|
|
46
|
+
return this.dataDir ? path.join(this.dataDir, 'data.rdb.json') : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
_load() {
|
|
50
|
+
const p = this.snapshotPath();
|
|
51
|
+
if (!p || !fs.existsSync(p)) return;
|
|
52
|
+
try {
|
|
53
|
+
const raw = JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
54
|
+
for (const [k, v] of Object.entries(raw)) this.db.set(k, v);
|
|
55
|
+
} catch (_) {}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
_scheduleSnapshot() {
|
|
59
|
+
if (!this.dataDir) return;
|
|
60
|
+
if (this.snapshotTimer) clearTimeout(this.snapshotTimer);
|
|
61
|
+
this.snapshotTimer = setTimeout(() => this._snapshot(), 500);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
_snapshot() {
|
|
65
|
+
if (this.snapshotTimer) { clearTimeout(this.snapshotTimer); this.snapshotTimer = null; }
|
|
66
|
+
const p = this.snapshotPath();
|
|
67
|
+
if (!p) return;
|
|
68
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
69
|
+
const out = {};
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
for (const [k, v] of this.db) {
|
|
72
|
+
if (v.ttl && v.ttl <= now) continue;
|
|
73
|
+
out[k] = v;
|
|
74
|
+
}
|
|
75
|
+
const tmp = p + '.tmp';
|
|
76
|
+
fs.writeFileSync(tmp, JSON.stringify(out));
|
|
77
|
+
fs.renameSync(tmp, p);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
stop() {
|
|
81
|
+
if (this.snapshotTimer) clearTimeout(this.snapshotTimer);
|
|
82
|
+
this._snapshot();
|
|
83
|
+
if (this.server) this.server.close();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* ---------- value helpers ---------- */
|
|
87
|
+
_get(key) {
|
|
88
|
+
const v = this.db.get(key);
|
|
89
|
+
if (!v) return null;
|
|
90
|
+
if (v.ttl && v.ttl <= Date.now()) { this.db.delete(key); return null; }
|
|
91
|
+
return v;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* ---------- command execution ---------- */
|
|
95
|
+
execute(cmd, args) {
|
|
96
|
+
const sig = TYPE_SIGNATURES[cmd];
|
|
97
|
+
if (sig === undefined) throw new Error(`ERR unknown command '${cmd}'`);
|
|
98
|
+
if (sig > 0 && args.length < sig) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
|
|
99
|
+
if (sig === -1 && args.length < 1) throw new Error(`ERR wrong number of arguments for '${cmd}' command`);
|
|
100
|
+
switch (cmd) {
|
|
101
|
+
case 'PING': return args.length ? args[0] : 'PONG';
|
|
102
|
+
case 'ECHO': return args[0];
|
|
103
|
+
case 'AUTH':
|
|
104
|
+
if (!this.password) throw new Error('ERR Client sent AUTH, but no password is set');
|
|
105
|
+
if (args[0] !== this.password) throw new Error('ERR invalid password');
|
|
106
|
+
return 'OK';
|
|
107
|
+
case 'SELECT':
|
|
108
|
+
if (!/^\d+$/.test(args[0])) throw new Error(`ERR invalid DB index`);
|
|
109
|
+
this.selected = parseInt(args[0], 10);
|
|
110
|
+
return 'OK';
|
|
111
|
+
case 'SET': {
|
|
112
|
+
const prev = this._get(args[0]);
|
|
113
|
+
this.db.set(args[0], { type: 'string', val: args[1], ttl: null });
|
|
114
|
+
this._scheduleSnapshot();
|
|
115
|
+
return 'OK';
|
|
116
|
+
}
|
|
117
|
+
case 'SETNX': {
|
|
118
|
+
const prev = this._get(args[0]);
|
|
119
|
+
if (prev) return 0;
|
|
120
|
+
this.db.set(args[0], { type: 'string', val: args[1], ttl: null });
|
|
121
|
+
this._scheduleSnapshot();
|
|
122
|
+
return 1;
|
|
123
|
+
}
|
|
124
|
+
case 'GET': {
|
|
125
|
+
const v = this._get(args[0]);
|
|
126
|
+
if (!v) return null;
|
|
127
|
+
if (v.type !== 'string') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
128
|
+
return v.val;
|
|
129
|
+
}
|
|
130
|
+
case 'APPEND': {
|
|
131
|
+
const v = this._get(args[0]);
|
|
132
|
+
const prev = v && v.type === 'string' ? v.val : '';
|
|
133
|
+
this.db.set(args[0], { type: 'string', val: prev + args[1], ttl: v ? v.ttl : null });
|
|
134
|
+
this._scheduleSnapshot();
|
|
135
|
+
return this.db.get(args[0]).val.length;
|
|
136
|
+
}
|
|
137
|
+
case 'STRLEN': {
|
|
138
|
+
const v = this._get(args[0]);
|
|
139
|
+
if (!v) return 0;
|
|
140
|
+
if (v.type !== 'string') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
141
|
+
return v.val.length;
|
|
142
|
+
}
|
|
143
|
+
case 'INCR': case 'DECR': case 'INCRBY': case 'DECRBY': {
|
|
144
|
+
const by = cmd === 'INCRBY' || cmd === 'DECRBY' ? parseInt(args[1], 10) : 1;
|
|
145
|
+
if (isNaN(by)) throw new Error('ERR value is not an integer or out of range');
|
|
146
|
+
const dir = cmd === 'DECR' || cmd === 'DECRBY' ? -1 : 1;
|
|
147
|
+
const v = this._get(args[0]);
|
|
148
|
+
let n;
|
|
149
|
+
if (!v) { n = 0; }
|
|
150
|
+
else if (v.type === 'string' && /^-?\d+$/.test(v.val)) { n = parseInt(v.val, 10); }
|
|
151
|
+
else throw new Error('ERR value is not an integer or out of range');
|
|
152
|
+
n += dir * by;
|
|
153
|
+
this.db.set(args[0], { type: 'string', val: String(n), ttl: v ? v.ttl : null });
|
|
154
|
+
this._scheduleSnapshot();
|
|
155
|
+
return n;
|
|
156
|
+
}
|
|
157
|
+
case 'DEL': {
|
|
158
|
+
let n = 0;
|
|
159
|
+
for (const k of args) if (this.db.delete(k)) n++;
|
|
160
|
+
this._scheduleSnapshot();
|
|
161
|
+
return n;
|
|
162
|
+
}
|
|
163
|
+
case 'EXISTS': {
|
|
164
|
+
let n = 0;
|
|
165
|
+
for (const k of args) if (this._get(k)) n++;
|
|
166
|
+
return n;
|
|
167
|
+
}
|
|
168
|
+
case 'KEYS': {
|
|
169
|
+
const pat = args[0];
|
|
170
|
+
const re = new RegExp('^' + pat.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.') + '$');
|
|
171
|
+
const keys = [];
|
|
172
|
+
for (const k of this.db.keys()) {
|
|
173
|
+
if (re.test(k) && this._get(k)) keys.push(k);
|
|
174
|
+
}
|
|
175
|
+
return keys;
|
|
176
|
+
}
|
|
177
|
+
case 'TYPE': {
|
|
178
|
+
const v = this._get(args[0]);
|
|
179
|
+
return v ? v.type : 'none';
|
|
180
|
+
}
|
|
181
|
+
case 'EXPIRE': {
|
|
182
|
+
const v = this._get(args[0]);
|
|
183
|
+
if (!v) return 0;
|
|
184
|
+
v.ttl = Date.now() + parseInt(args[1], 10) * 1000;
|
|
185
|
+
this._scheduleSnapshot();
|
|
186
|
+
return 1;
|
|
187
|
+
}
|
|
188
|
+
case 'TTL': {
|
|
189
|
+
const v = this._get(args[0]);
|
|
190
|
+
if (!v) return -2;
|
|
191
|
+
if (!v.ttl) return -1;
|
|
192
|
+
return Math.max(0, Math.round((v.ttl - Date.now()) / 1000));
|
|
193
|
+
}
|
|
194
|
+
case 'PERSIST': {
|
|
195
|
+
const v = this._get(args[0]);
|
|
196
|
+
if (!v || !v.ttl) return 0;
|
|
197
|
+
v.ttl = null;
|
|
198
|
+
return 1;
|
|
199
|
+
}
|
|
200
|
+
case 'HSET': {
|
|
201
|
+
if (args.length % 2 !== 1) throw new Error(`ERR wrong number of arguments for 'hset' command`);
|
|
202
|
+
const v = this._get(args[0]);
|
|
203
|
+
const h = (v && v.type === 'hash') ? { ...v.val } : {};
|
|
204
|
+
let n = 0;
|
|
205
|
+
for (let i = 1; i < args.length; i += 2) {
|
|
206
|
+
if (!(args[i] in h)) n++;
|
|
207
|
+
h[args[i]] = args[i + 1];
|
|
208
|
+
}
|
|
209
|
+
this.db.set(args[0], { type: 'hash', val: h, ttl: v ? v.ttl : null });
|
|
210
|
+
this._scheduleSnapshot();
|
|
211
|
+
return n;
|
|
212
|
+
}
|
|
213
|
+
case 'HGET': {
|
|
214
|
+
const v = this._get(args[0]);
|
|
215
|
+
if (!v) return null;
|
|
216
|
+
if (v.type !== 'hash') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
217
|
+
return v.val[args[1]] === undefined ? null : v.val[args[1]];
|
|
218
|
+
}
|
|
219
|
+
case 'HGETALL': {
|
|
220
|
+
const v = this._get(args[0]);
|
|
221
|
+
if (!v) return [];
|
|
222
|
+
if (v.type !== 'hash') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
223
|
+
return Object.entries(v.val).flat();
|
|
224
|
+
}
|
|
225
|
+
case 'HDEL': {
|
|
226
|
+
const v = this._get(args[0]);
|
|
227
|
+
if (!v) return 0;
|
|
228
|
+
if (v.type !== 'hash') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
229
|
+
let n = 0;
|
|
230
|
+
for (const f of args.slice(1)) if (v.val[f] !== undefined) { delete v.val[f]; n++; }
|
|
231
|
+
this._scheduleSnapshot();
|
|
232
|
+
return n;
|
|
233
|
+
}
|
|
234
|
+
case 'HEXISTS': {
|
|
235
|
+
const v = this._get(args[0]);
|
|
236
|
+
return (v && v.type === 'hash' && v.val[args[1]] !== undefined) ? 1 : 0;
|
|
237
|
+
}
|
|
238
|
+
case 'HLEN': {
|
|
239
|
+
const v = this._get(args[0]);
|
|
240
|
+
return v && v.type === 'hash' ? Object.keys(v.val).length : 0;
|
|
241
|
+
}
|
|
242
|
+
case 'HKEYS': {
|
|
243
|
+
const v = this._get(args[0]);
|
|
244
|
+
return v && v.type === 'hash' ? Object.keys(v.val) : [];
|
|
245
|
+
}
|
|
246
|
+
case 'HVALS': {
|
|
247
|
+
const v = this._get(args[0]);
|
|
248
|
+
return v && v.type === 'hash' ? Object.values(v.val) : [];
|
|
249
|
+
}
|
|
250
|
+
case 'LPUSH': case 'RPUSH': {
|
|
251
|
+
const v = this._get(args[0]);
|
|
252
|
+
const list = (v && v.type === 'list') ? [...v.val] : [];
|
|
253
|
+
if (cmd === 'LPUSH') list.unshift(...args.slice(1));
|
|
254
|
+
else list.push(...args.slice(1));
|
|
255
|
+
this.db.set(args[0], { type: 'list', val: list, ttl: v ? v.ttl : null });
|
|
256
|
+
this._scheduleSnapshot();
|
|
257
|
+
return list.length;
|
|
258
|
+
}
|
|
259
|
+
case 'LPOP': case 'RPOP': {
|
|
260
|
+
const v = this._get(args[0]);
|
|
261
|
+
if (!v) return null;
|
|
262
|
+
if (v.type !== 'list') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
263
|
+
const el = cmd === 'LPOP' ? v.val.shift() : v.val.pop();
|
|
264
|
+
this._scheduleSnapshot();
|
|
265
|
+
return el === undefined ? null : el;
|
|
266
|
+
}
|
|
267
|
+
case 'LLEN': {
|
|
268
|
+
const v = this._get(args[0]);
|
|
269
|
+
return v && v.type === 'list' ? v.val.length : 0;
|
|
270
|
+
}
|
|
271
|
+
case 'LRANGE': {
|
|
272
|
+
const v = this._get(args[0]);
|
|
273
|
+
if (!v) return [];
|
|
274
|
+
if (v.type !== 'list') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
275
|
+
let start = parseInt(args[1], 10), stop = parseInt(args[2], 10);
|
|
276
|
+
const len = v.val.length;
|
|
277
|
+
if (start < 0) start = Math.max(0, len + start);
|
|
278
|
+
if (stop < 0) stop = len + stop;
|
|
279
|
+
if (start > stop || start >= len) return [];
|
|
280
|
+
return v.val.slice(start, Math.min(stop + 1, len));
|
|
281
|
+
}
|
|
282
|
+
case 'LINDEX': {
|
|
283
|
+
const v = this._get(args[0]);
|
|
284
|
+
if (!v) return null;
|
|
285
|
+
if (v.type !== 'list') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
286
|
+
let i = parseInt(args[1], 10);
|
|
287
|
+
if (i < 0) i = v.val.length + i;
|
|
288
|
+
return v.val[i] === undefined ? null : v.val[i];
|
|
289
|
+
}
|
|
290
|
+
case 'LREM': {
|
|
291
|
+
const v = this._get(args[0]);
|
|
292
|
+
if (!v) return 0;
|
|
293
|
+
if (v.type !== 'list') throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
|
294
|
+
const count = parseInt(args[1], 10);
|
|
295
|
+
const target = args[2];
|
|
296
|
+
const out = [];
|
|
297
|
+
let removed = 0;
|
|
298
|
+
const remaining = Math.abs(count);
|
|
299
|
+
for (const el of v.val) {
|
|
300
|
+
if (el === target && (count === 0 || removed < remaining)) { removed++; continue; }
|
|
301
|
+
out.push(el);
|
|
302
|
+
}
|
|
303
|
+
v.val = out;
|
|
304
|
+
this._scheduleSnapshot();
|
|
305
|
+
return removed;
|
|
306
|
+
}
|
|
307
|
+
case 'SADD': case 'SREM': {
|
|
308
|
+
const v = this._get(args[0]);
|
|
309
|
+
const set = (v && v.type === 'set') ? new Set(v.val) : new Set();
|
|
310
|
+
let n = 0;
|
|
311
|
+
for (const m of args.slice(1)) {
|
|
312
|
+
if (cmd === 'SADD') { if (!set.has(m)) { set.add(m); n++; } }
|
|
313
|
+
else { if (set.delete(m)) n++; }
|
|
314
|
+
}
|
|
315
|
+
if (set.size === 0) this.db.delete(args[0]);
|
|
316
|
+
else this.db.set(args[0], { type: 'set', val: [...set], ttl: v ? v.ttl : null });
|
|
317
|
+
this._scheduleSnapshot();
|
|
318
|
+
return n;
|
|
319
|
+
}
|
|
320
|
+
case 'SMEMBERS': {
|
|
321
|
+
const v = this._get(args[0]);
|
|
322
|
+
return v && v.type === 'set' ? [...v.val] : [];
|
|
323
|
+
}
|
|
324
|
+
case 'SISMEMBER': {
|
|
325
|
+
const v = this._get(args[0]);
|
|
326
|
+
return (v && v.type === 'set' && v.val.includes(args[1])) ? 1 : 0;
|
|
327
|
+
}
|
|
328
|
+
case 'SCARD': {
|
|
329
|
+
const v = this._get(args[0]);
|
|
330
|
+
return v && v.type === 'set' ? v.val.length : 0;
|
|
331
|
+
}
|
|
332
|
+
case 'DBSIZE': return this.db.size;
|
|
333
|
+
case 'FLUSHALL': case 'FLUSHDB':
|
|
334
|
+
this.db.clear();
|
|
335
|
+
this._scheduleSnapshot();
|
|
336
|
+
return 'OK';
|
|
337
|
+
case 'INFO': {
|
|
338
|
+
const lines = [
|
|
339
|
+
'# Server',
|
|
340
|
+
'redis_version:7.0.0',
|
|
341
|
+
'jsql_neo_version:' + require('../package.json').version,
|
|
342
|
+
'# Memory',
|
|
343
|
+
'used_memory:' + JSON.stringify([...this.db.values()]).length,
|
|
344
|
+
'# Stats',
|
|
345
|
+
'db_size:' + this.db.size,
|
|
346
|
+
'connected_clients:1',
|
|
347
|
+
];
|
|
348
|
+
return lines.join('\r\n') + '\r\n';
|
|
349
|
+
}
|
|
350
|
+
case 'QUIT': return 'OK';
|
|
351
|
+
default: throw new Error(`ERR unknown command '${cmd}'`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/* ---------- RESP protocol ---------- */
|
|
356
|
+
listen() {
|
|
357
|
+
this.server = net.createServer((socket) => {
|
|
358
|
+
let buf = Buffer.alloc(0);
|
|
359
|
+
socket.setEncoding('utf8');
|
|
360
|
+
socket.on('data', (chunk) => {
|
|
361
|
+
buf += chunk;
|
|
362
|
+
for (;;) {
|
|
363
|
+
const msg = this._parse(buf);
|
|
364
|
+
if (!msg) break;
|
|
365
|
+
buf = buf.slice(msg.consumed);
|
|
366
|
+
this._handle(socket, msg.cmd, msg.args);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
socket.on('error', () => {});
|
|
370
|
+
});
|
|
371
|
+
this.server.listen(this.port, this.host);
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
_parse(buf) {
|
|
376
|
+
if (buf[0] !== '*') {
|
|
377
|
+
const i = buf.indexOf('\r\n');
|
|
378
|
+
if (i < 0) return null;
|
|
379
|
+
const cmd = buf.slice(0, i).trim().split(/\s+/);
|
|
380
|
+
if (!cmd.length) return null;
|
|
381
|
+
return { consumed: i + 2, cmd: cmd.map(c => c.toUpperCase()), args: cmd.slice(1) };
|
|
382
|
+
}
|
|
383
|
+
const i = buf.indexOf('\r\n');
|
|
384
|
+
if (i < 0) return null;
|
|
385
|
+
const n = parseInt(buf.slice(1, i), 10);
|
|
386
|
+
if (isNaN(n)) throw new Error('ERR Protocol error');
|
|
387
|
+
let off = i + 2;
|
|
388
|
+
const parts = [];
|
|
389
|
+
for (let k = 0; k < n; k++) {
|
|
390
|
+
if (buf[off] !== '$') throw new Error('ERR Protocol error: expected bulk string');
|
|
391
|
+
const j = buf.indexOf('\r\n', off);
|
|
392
|
+
if (j < 0) return null;
|
|
393
|
+
const len = parseInt(buf.slice(off + 1, j), 10);
|
|
394
|
+
if (isNaN(len)) throw new Error('ERR Protocol error');
|
|
395
|
+
if (buf.length < j + 2 + len + 2) return null;
|
|
396
|
+
parts.push(buf.slice(j + 2, j + 2 + len));
|
|
397
|
+
off = j + 2 + len + 2;
|
|
398
|
+
}
|
|
399
|
+
const cmd = parts[0].toUpperCase();
|
|
400
|
+
return { consumed: off, cmd, args: parts.slice(1) };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
_handle(socket, cmd, args) {
|
|
404
|
+
try {
|
|
405
|
+
if (this.onQuery) this.onQuery(cmd, args);
|
|
406
|
+
if (cmd === 'AUTH') {
|
|
407
|
+
if (!this.password) return this._send(socket, -new Error('ERR Client sent AUTH, but no password is set'));
|
|
408
|
+
}
|
|
409
|
+
if (this.password && !this._authed) {
|
|
410
|
+
if (cmd === 'AUTH') { const ok = this.execute('AUTH', args); this._authed = ok === 'OK'; return this._send(socket, ok); }
|
|
411
|
+
return this._send(socket, -new Error('NOAUTH Authentication required.'));
|
|
412
|
+
}
|
|
413
|
+
if (cmd === 'QUIT') { this._send(socket, 'OK'); socket.end(); return; }
|
|
414
|
+
const r = this.execute(cmd, args);
|
|
415
|
+
this._send(socket, r);
|
|
416
|
+
} catch (e) {
|
|
417
|
+
this._send(socket, -e);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
_send(socket, val) {
|
|
422
|
+
let out;
|
|
423
|
+
if (val instanceof Error) {
|
|
424
|
+
out = '-' + val.message + '\r\n';
|
|
425
|
+
} else if (val === null) {
|
|
426
|
+
out = '$-1\r\n';
|
|
427
|
+
} else if (typeof val === 'number') {
|
|
428
|
+
out = ':' + val + '\r\n';
|
|
429
|
+
} else if (Array.isArray(val)) {
|
|
430
|
+
out = '*' + val.length + '\r\n' + val.map(v => '$' + String(v).length + '\r\n' + v + '\r\n').join('');
|
|
431
|
+
} else if (typeof val === 'string') {
|
|
432
|
+
if (val === 'OK' || val === 'PONG') {
|
|
433
|
+
out = '+' + val + '\r\n';
|
|
434
|
+
} else {
|
|
435
|
+
out = '$' + Buffer.byteLength(val, 'utf8') + '\r\n' + val + '\r\n';
|
|
436
|
+
}
|
|
437
|
+
} else {
|
|
438
|
+
out = '$-1\r\n';
|
|
439
|
+
}
|
|
440
|
+
socket.write(out);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function createRedisServer(opts = {}) {
|
|
445
|
+
return new RedisServer(opts);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
module.exports = { RedisServer, createRedisServer };
|