resplite 1.2.18 → 1.2.20

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resplite",
3
- "version": "1.2.18",
3
+ "version": "1.2.20",
4
4
  "description": "A RESP2 server with practical Redis compatibility, backed by SQLite",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -16,6 +16,7 @@ Supported:
16
16
 
17
17
  - `GET`
18
18
  - `SET`
19
+ - `SETEX`
19
20
  - `MGET`
20
21
  - `MSET`
21
22
  - `DEL`
@@ -5,7 +5,7 @@
5
5
 
6
6
  /** @type {Set<string>} Commands that modify data (write). */
7
7
  const WRITE_COMMANDS = new Set([
8
- 'SET', 'MSET', 'DEL', 'UNLINK', 'EXPIRE', 'PEXPIRE', 'PERSIST', 'INCR', 'DECR', 'INCRBY', 'DECRBY',
8
+ 'SET', 'SETEX', 'MSET', 'DEL', 'UNLINK', 'EXPIRE', 'PEXPIRE', 'PERSIST', 'INCR', 'DECR', 'INCRBY', 'DECRBY',
9
9
  'HSET', 'HDEL', 'HINCRBY', 'SADD', 'SREM', 'LPUSH', 'RPUSH', 'LPOP', 'RPOP', 'LREM', 'ZADD', 'ZREM',
10
10
  'FT.CREATE', 'FT.ADD', 'FT.DEL', 'FT.SUGADD', 'FT.SUGDEL', 'CLIENT',
11
11
  ]);
@@ -50,6 +50,8 @@ function docFor(name) {
50
50
  step = 1;
51
51
  } else if (['HMGET', 'HGETALL', 'HGET', 'HSET', 'HDEL', 'HEXISTS', 'HINCRBY', 'HLEN'].includes(name)) {
52
52
  arity = (name === 'HGET' || name === 'HLEN' || name === 'HEXISTS') ? 3 : -3;
53
+ } else if (name === 'SETEX') {
54
+ arity = 4;
53
55
  }
54
56
  return [lower, arity, flags, firstKey, lastKey, step, []];
55
57
  }
@@ -8,6 +8,7 @@ import * as echo from './echo.js';
8
8
  import * as quit from './quit.js';
9
9
  import * as get from './get.js';
10
10
  import * as set from './set.js';
11
+ import * as setex from './setex.js';
11
12
  import * as del from './del.js';
12
13
  import * as unlink from './unlink.js';
13
14
  import * as exists from './exists.js';
@@ -80,6 +81,7 @@ const HANDLERS = new Map([
80
81
  ['QUIT', (e, a) => quit.handleQuit()],
81
82
  ['GET', (e, a) => get.handleGet(e, a)],
82
83
  ['SET', (e, a) => set.handleSet(e, a)],
84
+ ['SETEX', (e, a) => setex.handleSetex(e, a)],
83
85
  ['DEL', (e, a) => del.handleDel(e, a)],
84
86
  ['UNLINK', (e, a) => unlink.handleUnlink(e, a)],
85
87
  ['EXISTS', (e, a) => exists.handleExists(e, a)],
@@ -0,0 +1,19 @@
1
+ /**
2
+ * SETEX key seconds value - set key to value with expiration in seconds (atomic).
3
+ * Delegates to SET key value EX seconds to avoid duplicating logic.
4
+ */
5
+
6
+ import * as set from './set.js';
7
+
8
+ export function handleSetex(engine, args) {
9
+ if (!args || args.length !== 3) {
10
+ return { error: 'ERR wrong number of arguments for \'SETEX\' command' };
11
+ }
12
+ const key = args[0];
13
+ const sec = parseInt(args[1].toString(), 10);
14
+ if (Number.isNaN(sec) || sec < 1) {
15
+ return { error: 'ERR invalid expire time in \'SETEX\'' };
16
+ }
17
+ const value = args[2];
18
+ return set.handleSet(engine, [key, value, Buffer.from('EX'), Buffer.from(String(sec))]);
19
+ }
@@ -49,4 +49,24 @@ describe('Strings integration', () => {
49
49
  const r = await sendCommand(port, argv('EXISTS', 'ex1', 'ex2'));
50
50
  assert.equal(r.toString('ascii'), ':1\r\n');
51
51
  });
52
+
53
+ it('SETEX sets key with TTL and returns OK', async () => {
54
+ const setexReply = await sendCommand(port, argv('SETEX', 'setexkey', '60', 'setexval'));
55
+ assert.equal(setexReply.toString('utf8'), '+OK\r\n');
56
+ const getReply = await sendCommand(port, argv('GET', 'setexkey'));
57
+ assert.equal(getReply.toString('utf8'), '$8\r\nsetexval\r\n');
58
+ const ttlReply = await sendCommand(port, argv('TTL', 'setexkey'));
59
+ const t = parseInt(ttlReply.toString('ascii').replace(/\D/g, ''), 10);
60
+ assert.ok(t >= 59 && t <= 60);
61
+ });
62
+
63
+ it('SETEX wrong number of arguments returns error', async () => {
64
+ const reply = await sendCommand(port, argv('SETEX', 'k', '10'));
65
+ assert.ok(reply.toString('utf8').includes('wrong number of arguments'));
66
+ });
67
+
68
+ it('SETEX invalid seconds returns error', async () => {
69
+ const reply = await sendCommand(port, argv('SETEX', 'k', '0', 'v'));
70
+ assert.ok(reply.toString('utf8').includes('invalid expire time'));
71
+ });
52
72
  });