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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 |
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x16
x32
x221
x221
x221
x1840
x1840
x221
x221
x221
x1840
x1840
x1840
x221
x221
x16
x109
x109
x109
x109
x109
x109
x331
x331
x331
x331
x331
x331
x331
x331
x331
x331
x109
x109
x60
x60
x60
x676
x169
x169
x169
x169
x169
x497
x781
x781
x169
x60
x49
x49
x49
x49
x147
x49
x291
x291
x291
x16
x25
x25
x57
x57
x25
x25
x343
x343
x343
x343
x343
x343
x705
x362
x362
x362
x688
x345
x345
x345
x345
x343
x345
x687
x344
x344
x344
x343
x347
x343
x343
x443
x343
x734
x391
x391
x392
x391
x392
x391
x437
x391
x391
x343
x345
x695
x352
x352
x352
x352
x343
x344
x343
x344
x344
x343
x385
x343
x439
x343
x343
x16
x16
x60
x16
x16
x16
x16
x16
x60
x60
x60
x16
x100
x100
x100
x16
x16
x16
x16
x89
x162
x162
x89
x89
x16
x24
x24
x16
x16
x16
x48
x81
x82
x48
x16
x16
x16
x16
x19
x22
x22
x22
x19
x19
x16 |
I
|
/**
* The command to send to the Redis server. This should be an
* array of arguments, where the first argument is the command name and the
* remaining arguments are the command's arguments. For the list of commands,
* see {@link https://redis.io/docs/latest/commands/ | Redis commands}.
*/
export type Command = readonly (string | number | Uint8Array)[];
/**
* The reply from the Redis server. This can be a string, number,
* boolean, null, or an array of replies. The type of the reply depends on the
* command sent. For example, the
* {@linkcode https://redis.io/docs/latest/commands/get/ | GET} command
* returns a string, while the
* {@linkcode https://redis.io/docs/latest/commands/mget/ | MGET} command
* returns an array of strings.
*/
export type Reply =
| string
| number
| null
| boolean
| bigint
| Uint8Array<ArrayBuffer>
| Set<Reply>
| ReplyObject
| readonly Reply[];
/**
* A Redis object, which is a key-value store where the keys are strings and
* the values are Redis replies. This is used for commands that return a map
* of key-value pairs, such as the
* {@linkcode https://redis.io/docs/latest/commands/hgetall/ | HGETALL} command.
*/
export interface ReplyObject {
[key: string]: Reply;
}
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const ARRAY_PREFIX = "*".charCodeAt(0);
const ATTRIBUTE_PREFIX = "|".charCodeAt(0);
const BIG_NUMBER_PREFIX = "(".charCodeAt(0);
const BLOB_ERROR_PREFIX = "!".charCodeAt(0);
const BOOLEAN_PREFIX = "#".charCodeAt(0);
const BULK_STRING_PREFIX = "$".charCodeAt(0);
const DOUBLE_PREFIX = ",".charCodeAt(0);
const ERROR_PREFIX = "-".charCodeAt(0);
const INTEGER_PREFIX = ":".charCodeAt(0);
const MAP_PREFIX = "%".charCodeAt(0);
const NULL_PREFIX = "_".charCodeAt(0);
const PUSH_PREFIX = ">".charCodeAt(0);
const SET_PREFIX = "~".charCodeAt(0);
const SIMPLE_STRING_PREFIX = "+".charCodeAt(0);
const VERBATIM_STRING_PREFIX = "=".charCodeAt(0);
const CRLF_BYTES = encoder.encode("\r\n");
const ARRAY_PREFIX_BYTES = encoder.encode("*");
const BULK_STRING_PREFIX_BYTES = encoder.encode("$");
/**
* Error thrown when a Redis reply is an error. Only thrown for commands that
* return an error reply.
*
* @example Basic usage
* ```ts
* import { RedisClient, RedisError } from "@iuioiua/redis";
* import { assertRejects } from "@std/assert/rejects";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* await assertRejects(
* () => redisClient.sendCommand(["Error", "This is a Redis error"]),
* RedisError,
* "ERR unknown command 'Error', with args beginning with: 'This is a Redis error' ",
* );
* ```
*/
export class RedisError extends Error {}
function concat(buffers: readonly Uint8Array[]): Uint8Array {
let length = 0;
for (const buffer of buffers) {
length += buffer.length;
}
const output = new Uint8Array(length);
let index = 0;
for (const buffer of buffers) {
output.set(buffer, index);
index += buffer.length;
}
return output;
}
/**
* Transforms a command, which is an array of arguments, into an RESP request.
*
* @see {@link https://redis.io/docs/reference/protocol-spec/#send-commands-to-a-redis-server}
*/
function createRequest(command: Command): Uint8Array {
const lines = [
ARRAY_PREFIX_BYTES,
encoder.encode(command.length.toString()),
CRLF_BYTES,
];
for (const arg of command) {
const bytes = arg instanceof Uint8Array
? arg
: encoder.encode(arg.toString());
lines.push(
BULK_STRING_PREFIX_BYTES,
encoder.encode(bytes.byteLength.toString()),
CRLF_BYTES,
bytes,
CRLF_BYTES,
);
}
return concat(lines);
}
async function* readLines(readable: ReadableStream<Uint8Array>) {
let chunks: Uint8Array = new Uint8Array();
for await (const chunk of readable) {
chunks = concat([chunks, chunk]);
let index;
while (
(index = chunks.indexOf(CRLF_BYTES[0]!)) !== -1 &&
chunks[index + 1] === CRLF_BYTES[1]
) {
yield chunks.subarray(0, index);
chunks = chunks.subarray(index + 2);
}
}
}
function readNReplies<T extends Reply>(
iterator: AsyncIterableIterator<Uint8Array>,
length: number,
raw = false,
): Promise<T[]> {
return Array.fromAsync({ length }, () => readReply(iterator, raw)) as Promise<
T[]
>;
}
function parseLine(value: Uint8Array): string {
return decoder.decode(value.slice(1));
}
/**
* Chunks an array into smaller arrays of two elements each. Used for map
* replies which are returned as a flat array of key-value pairs.
*
* @param array The array to chunk
* @returns An array of arrays, each containing two elements from the original
* array.
*/
function chunk<T>(array: readonly T[]): T[][] {
const result: T[][] = [];
for (let i = 0; i < array.length; i += 2) {
result.push(array.slice(i, i + 2));
}
return result;
}
async function readReply<T extends Reply>(
iterator: AsyncIterableIterator<Uint8Array>,
raw = false,
): Promise<T> {
const { value } = await iterator.next();
switch (value[0]) {
case ARRAY_PREFIX:
case PUSH_PREFIX: {
const length = Number(parseLine(value));
return (length === -1 ? null : await readNReplies(iterator, length)) as T;
}
case ATTRIBUTE_PREFIX: {
// TODO: include attribute data somehow
const length = Number(parseLine(value)) * 2;
// Read but don't return attribute data
await readNReplies(iterator, length);
return readReply(iterator, raw);
}
case BIG_NUMBER_PREFIX:
return BigInt(parseLine(value)) as T;
case BLOB_ERROR_PREFIX: {
// Skip to reading the next line, which is a string
const { value } = await iterator.next();
throw new RedisError(decoder.decode(value));
}
case BOOLEAN_PREFIX:
return (parseLine(value) === "t") as T;
case BULK_STRING_PREFIX:
case VERBATIM_STRING_PREFIX:
return (parseLine(value) === "-1" ? null : readReply(iterator, raw)) as T;
case DOUBLE_PREFIX:
case INTEGER_PREFIX: {
switch (parseLine(value)) {
case "inf":
return Infinity as T;
case "-inf":
return -Infinity as T;
default:
return Number(parseLine(value)) as T;
}
}
case ERROR_PREFIX:
throw new RedisError(parseLine(value));
case MAP_PREFIX: {
const length = Number(parseLine(value)) * 2;
const array = await readNReplies(iterator, length);
return Object.fromEntries(chunk(array));
}
case NULL_PREFIX:
return null as T;
case SET_PREFIX:
return new Set(
await readNReplies(iterator, Number(parseLine(value)), raw),
) as T;
case SIMPLE_STRING_PREFIX:
return parseLine(value) as T;
// No prefix
default:
return (raw ? value : decoder.decode(value)) as T;
}
}
/**
* A Redis client for interacting with a Redis server.
*
* @example Send RESPv2 commands
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const reply1 = await redisClient.sendCommand(["SET", "hello", "world"]);
* assertEquals(reply1, "OK");
*
* const reply2 = await redisClient.sendCommand(["GET", "hello"]);
* assertEquals(reply2, "world");
* ```
*
* @example Send RESP3 commands
*
* Switch to
* {@link https://github.com/redis/redis-specifications/blob/master/protocol/RESP3.md | RESP3}
* by sending a {@link https://redis.io/docs/latest/commands/hello/ | HELLO}
* command with the version number 3.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* // Switch to RESP3
* await redisClient.sendCommand(["HELLO", 3]);
*
* const reply1 = await redisClient.sendCommand(["HSET", "myhash", "foo", 1, "bar", 2]);
* assertEquals(reply1, 2);
*
* const reply2 = await redisClient.sendCommand(["HGETALL", "myhash"]);
* assertEquals(reply2, { foo: "1", bar: "2" });
* ```
*
* @example Receive raw data
*
* Receive raw data by setting the `raw` parameter to `true` for your given
* method. This functionality is exclusive to bulk string replies.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
*
* const reply1 = await redisClient.sendCommand(["SET", "data", data]);
* assertEquals(reply1, "OK");
*
* const reply2 = await redisClient.sendCommand(["GET", "data"], true);
* assertEquals(reply2, data);
* ```
*
* @example Execute operations with timeouts
*
* See the Deno Standard Library's
* {@linkcode https://jsr.io/@std/async/doc/~/deadline | deadline()} for more
* information. This function can be applied to any asynchronous operation.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { deadline } from "jsr:@std/async/deadline";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* // Rejects with a timeout error if the command takes longer than 100 milliseconds.
* await deadline(redisClient.sendCommand(["GET", "foo"]), 100);
* ```
*
* @example Retry operations
*
* See the Deno Standard Library's
* {@linkcode https://jsr.io/@std/async/doc/~/retry | retry()} for more
* information. This function can be applied to any asynchronous operation.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { retry } from "jsr:@std/async/retry";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* // Retries to connect until successful const the exponential backoff algorithm.
* await retry(() => redisClient.sendCommand(["GET", "foo"]));
* ```
*
* @example Pipeline commands
*
* See
* {@link https://redis.io/docs/latest/develop/use/pipelining/ | Redis pipelining}
* for more information.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const replies = await redisClient.pipelineCommands([
* ["INCR", "Y"],
* ["INCR", "Y"],
* ["INCR", "Y"],
* ["INCR", "Y"],
* ]);
* assertEquals(replies, [1, 2, 3, 4]);
* ```
*
* @example Use pub/sub channels
*
* See
* {@link https://redis.io/docs/latest/develop/interact/pubsub/ | Redis Pub/Sub}
* for more information.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* await redisClient.writeCommand(["SUBSCRIBE", "mychannel"]);
* for await (const reply of redisClient.readReplies()) {
* assertEquals(reply, ["subscribe", "mychannel", 1]);
* break;
* }
* await redisClient.writeCommand(["UNSUBSCRIBE", "mychannel"]);
* ```
*
* @example Perform transaction
*
* See {@link https://redis.io/docs/latest/develop/interact/transactions/ | Transactions}
* for more information.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* assertEquals(await redisClient.sendCommand(["MULTI"]), "OK");
* assertEquals(await redisClient.sendCommand(["INCR", "QUX"]), "QUEUED");
* assertEquals(await redisClient.sendCommand(["INCR", "QUX"]), "QUEUED");
* assertEquals(await redisClient.sendCommand(["EXEC"]), [1, 2]);
* ```
*
* @example Execute Lua scripts
*
* See
* {@link https://redis.io/docs/latest/develop/interact/programmability/eval-intro/ | Scripting with Lua}
* for more information.
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const reply1 = await redisClient.sendCommand(["EVAL", "return ARGV[1]", 0, "hello"]);
* assertEquals(reply1, "hello");
*
* const reply2 = await redisClient.sendCommand([
* "FUNCTION",
* "LOAD",
* "#!lua name=myfunc\nredis.register_function('knock_knock', function() return 'Who\\'s there?' end)",
* ]);
* assertEquals(reply2, "myfunc");
*
* const reply3 = await redisClient.sendCommand(["FCALL", "knock_knock", 0]);
* assertEquals(reply3, "Who's there?");
* ```
*/
export class RedisClient {
#writer: WritableStreamDefaultWriter<Uint8Array>;
#lines: AsyncIterableIterator<Uint8Array>;
#queue: Promise<any> = Promise.resolve();
/**
* Constructs a new instance.
*
* @param conn The connection to the Redis server. This should be a
* {@linkcode ReadableStream} and {@linkcode WritableStream} pair, such as the
* one returned by {@linkcode Deno.connect}.
*/
constructor(
conn: {
readable: ReadableStream<Uint8Array>;
writable: WritableStream<Uint8Array>;
},
) {
this.#writer = conn.writable.getWriter();
this.#lines = readLines(conn.readable);
}
#enqueue<T>(task: () => Promise<T>): Promise<T> {
this.#queue = this.#queue.then(task);
return this.#queue;
}
/**
* Sends a command to the Redis server and returns the reply.
*
* @param command The command to send to the Redis server. This should be an
* array of arguments, where the first argument is the command name and the
* remaining arguments are the command's arguments. For the list of commands,
* see {@link https://redis.io/docs/latest/commands/ | Redis commands}.
* @param raw If `true`, the reply will be returned as a raw
* {@linkcode Uint8Array}. This is useful for commands that return binary
* data, such as {@linkcode https://redis.io/docs/latest/commands/get/ | GET}.
* The default is `false`, which returns the reply as a JavaScript value.
*
* @returns The reply from the Redis server. This can be a string, number,
* boolean, null, or an array of replies. The type of the reply depends on the
* command sent. For example, the
* {@linkcode https://redis.io/docs/latest/commands/get/ | GET} command
* returns a string, while the
* {@linkcode https://redis.io/docs/latest/commands/mget/ | MGET} command
* returns an array of strings.
*
* @example Basic usage
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const reply1 = await redisClient.sendCommand(["SET", "hello", "world"]);
* assertEquals(reply1, "OK");
*
* const reply2 = await redisClient.sendCommand(["GET", "hello"]);
* assertEquals(reply2, "world");
* ```
*/
sendCommand<T extends Reply = Reply>(
command: Command,
raw = false,
): Promise<T> {
return this.#enqueue(() => {
this.#writer.write(createRequest(command));
return readReply<T>(this.#lines, raw);
});
}
/**
* Writes a command to the Redis server without listening for a reply.
*
* @param command The command to send to the Redis server. This should be an
* array of arguments, where the first argument is the command name and the
* remaining arguments are the command's arguments. For the list of commands,
* see {@link https://redis.io/docs/latest/commands/ | Redis commands}.
*
* @example Basic usage
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* await redisClient.writeCommand(["SUBSCRIBE", "mychannel"]);
* for await (const reply of redisClient.readReplies()) {
* assertEquals(reply, ["subscribe", "mychannel", 1]);
* break;
* }
* await redisClient.writeCommand(["UNSUBSCRIBE", "mychannel"]);
* ```
*/
writeCommand(command: Command): Promise<void> {
return this.#enqueue(() => this.#writer.write(createRequest(command)));
}
/**
* Used for pub/sub. Listens for replies from the Redis server.
*
* See
* {@link https://redis.io/docs/latest/develop/interact/pubsub/ | Redis Pub/Sub}
* for more information.
*
* @param raw If `true`, the reply will be yield as a raw
* {@linkcode Uint8Array}. This is useful for commands that return binary
* data, such as {@linkcode https://redis.io/docs/latest/commands/get/ | GET}.
* The default is `false`, which returns the reply as a JavaScript value.
*
* @returns An async iterable iterator that yields replies from the Redis
* server. The replies can be of various types, including strings, numbers,
* booleans, null, and arrays of replies. The type of the reply depends on
* the command sent. For example, the
* {@linkcode https://redis.io/docs/latest/commands/get/ | GET} command
* returns a string, while the
* {@linkcode https://redis.io/docs/latest/commands/mget/ | MGET} command
* returns an array of strings.
*
* @example Basic usage
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* await redisClient.writeCommand(["SUBSCRIBE", "mychannel"]);
* for await (const reply of redisClient.readReplies()) {
* assertEquals(reply, ["subscribe", "mychannel", 1]);
* break;
* }
* await redisClient.writeCommand(["UNSUBSCRIBE", "mychannel"]);
* ```
*/
async *readReplies<T extends Reply = Reply>(
raw = false,
): AsyncIterableIterator<T> {
while (true) {
yield readReply<T>(this.#lines, raw);
}
}
/**
* Pipelines commands to the Redis server and returns the replies.
*
* See
* {@link https://redis.io/docs/latest/develop/use/pipelining/ | Redis pipelining}
* for more information.
*
* @param commands The array of commands to send to the Redis server. Each
* command should be an array of arguments, where the first argument is the
* command name and the remaining arguments are the command's arguments. For
* the list of commands, see
* {@link https://redis.io/docs/latest/commands/ | Redis commands}.
* @param raw If `true`, the reply will be yield as a raw
* {@linkcode Uint8Array}. This is useful for commands that return binary
* data, such as {@linkcode https://redis.io/docs/latest/commands/get/ | GET}.
* The default is `false`, which returns the reply as a JavaScript value.
*
* @returns An array of replies from the Redis server. The replies can be of
* various types, including strings, numbers, booleans, null, and arrays of
* replies. The type of the reply depends on the command sent. For example,
* the
* {@linkcode https://redis.io/docs/latest/commands/get/ | GET} command
* returns a string, while the
* {@linkcode https://redis.io/docs/latest/commands/mget/ | MGET} command
* returns an array of strings.
*
* @example Basic usage
*
* ```ts
* import { RedisClient } from "@iuioiua/redis";
* import { assertEquals } from "@std/assert/equals";
*
* using redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* const replies = await redisClient.pipelineCommands([
* ["INCR", "A"],
* ["INCR", "A"],
* ["INCR", "A"],
* ["INCR", "A"],
* ]);
* assertEquals(replies, [1, 2, 3, 4]);
* ```
*/
pipelineCommands<T extends Reply = Reply>(
commands: readonly Command[],
raw = false,
): Promise<T[]> {
return this.#enqueue(() => {
const bytes = concat(commands.map(createRequest));
this.#writer.write(bytes);
return readNReplies<T>(this.#lines, commands.length, raw);
});
}
}
|