-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
358 lines (324 loc) · 9.06 KB
/
mod.ts
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
// deno-lint-ignore-file no-explicit-any
import { writeAll } from "https://deno.land/std@0.160.0/streams/conversion.ts";
import { BufReader } from "https://deno.land/std@0.160.0/io/buffer.ts";
import { chunk } from "https://deno.land/std@0.160.0/collections/chunk.ts";
/**
* Sections:
* 1. Request
* 2. Reply
* 3. Combined
*/
export type Command = (string | number)[];
export type Reply =
| string
| number
| null
| boolean
| BigInt
| Record<string, any>
| Reply[];
const CRLF = "\r\n";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export const ARRAY_PREFIX = "*";
export const ATTRIBUTE_PREFIX = "|";
export const BIG_NUMBER_PREFIX = "(";
export const BLOB_ERROR_PREFIX = "!";
export const BOOLEAN_PREFIX = "#";
export const BULK_STRING_PREFIX = "$";
export const DOUBLE_PREFIX = ",";
export const ERROR_PREFIX = "-";
export const INTEGER_PREFIX = ":";
export const MAP_PREFIX = "%";
export const NULL_PREFIX = "_";
export const PUSH_PREFIX = ">";
export const SET_PREFIX = "~";
export const SIMPLE_STRING_PREFIX = "+";
export const VERBATIM_STRING_PREFIX = "=";
export const STREAMED_REPLY_START_DELIMITER = "?";
export const STREAMED_STRING_END_DELIMITER = ";0";
export const STREAMED_AGGREGATE_END_DELIMITER = ".";
/** 1. Request */
/**
* 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 createCommandString(command: Command): string {
let string = ARRAY_PREFIX + command.length + CRLF;
for (const arg of command) {
string += BULK_STRING_PREFIX + arg.toString().length + CRLF +
arg + CRLF;
}
return string;
}
/**
* Just writes a command to the Redis server.
*
* @example
* ```ts
* import { writeCommand } from "https://deno.land/x/r2d2@$VERSION/mod.ts";
*
* const redisConn = await Deno.connect({ port: 6379 });
*
* await writeCommand(redisConn, ["SHUTDOWN"]);
* ```
*/
export async function writeCommand(
writer: Deno.Writer,
command: Command,
): Promise<void> {
await writeAll(writer, encoder.encode(createCommandString(command)));
}
/** 2. Reply */
function removePrefix(line: string): string {
return line.slice(1);
}
function isSteamedReply(line: string): boolean {
return line.charAt(1) === STREAMED_REPLY_START_DELIMITER;
}
function toObject(array: any[]): Record<string, any> {
return Object.fromEntries(chunk(array, 2));
}
async function readNReplies(
length: number,
bufReader: BufReader,
): Promise<Reply[]> {
const replies: Reply[] = [];
for (let i = 0; i < length; i++) {
replies.push(await readReply(bufReader));
}
return replies;
}
async function readStreamedReply(
delimiter: string,
bufReader: BufReader,
): Promise<Reply[]> {
const replies: Reply[] = [];
while (true) {
const reply = await readReply(bufReader);
if (reply === delimiter) {
break;
}
replies.push(reply);
}
return replies;
}
async function readArray(
line: string,
bufReader: BufReader,
): Promise<null | Reply[]> {
const length = readNumber(line);
return length === -1 ? null : await readNReplies(length, bufReader);
}
/**
* Read but don't return attribute data.
*
* @todo include attribute data somehow
*/
async function readAttribute(
line: string,
bufReader: BufReader,
): Promise<null | Reply> {
await readMap(line, bufReader);
return await readReply(bufReader);
}
function readBigNumber(line: string): BigInt {
return BigInt(removePrefix(line));
}
async function readBlobError(bufReader: BufReader): Promise<never> {
/** Skip to reading the next line, which is a string */
return await Promise.reject(await readReply(bufReader) as string);
}
function readBoolean(line: string): boolean {
return removePrefix(line) === "t";
}
/** Also reads verbatim string */
async function readBulkString(
line: string,
bufReader: BufReader,
): Promise<null | string> {
return removePrefix(line) === "-1"
? null
: await readReply(bufReader) as string;
}
async function readError(line: string): Promise<never> {
return await Promise.reject(removePrefix(line));
}
async function readMap(
line: string,
bufReader: BufReader,
): Promise<Record<string, any>> {
const length = readNumber(line) * 2;
const array = await readNReplies(length, bufReader);
return toObject(array);
}
/** Reads an integer or double */
function readNumber(line: string): number {
const number = removePrefix(line);
switch (number) {
case "inf":
return Infinity;
case "-inf":
return -Infinity;
default:
return Number(number);
}
}
async function readSet(
line: string,
bufReader: BufReader,
): Promise<Set<Reply>> {
return new Set(await readArray(line, bufReader));
}
function readSimpleString(line: string): string {
return removePrefix(line);
}
async function readStreamedArray(bufReader: BufReader): Promise<Reply[]> {
return await readStreamedReply(STREAMED_AGGREGATE_END_DELIMITER, bufReader);
}
async function readStreamedMap(
bufReader: BufReader,
): Promise<Record<string, any>> {
const array = await readStreamedReply(
STREAMED_AGGREGATE_END_DELIMITER,
bufReader,
);
return toObject(array);
}
async function readStreamedSet(bufReader: BufReader): Promise<Set<Reply>> {
return new Set(await readStreamedArray(bufReader));
}
async function readStreamedString(bufReader: BufReader): Promise<string> {
return (await readStreamedReply(STREAMED_STRING_END_DELIMITER, bufReader))
/** Remove byte counts */
.filter((line) => !(line as string).startsWith(";"))
.join("");
}
/**
* Reads and processes the response line-by-line.
*
* See {@link https://redis.io/docs/reference/protocol-spec/#resp-protocol-description}
*/
export async function readReply(bufReader: BufReader): Promise<Reply> {
const result = await bufReader.readLine();
if (!result) {
return await Promise.reject("No reply received from Redis server");
}
const line = decoder.decode(result.line);
switch (line.charAt(0)) {
case ARRAY_PREFIX:
case PUSH_PREFIX:
return isSteamedReply(line)
? await readStreamedArray(bufReader)
: await readArray(line, bufReader);
case ATTRIBUTE_PREFIX:
return await readAttribute(line, bufReader);
case BIG_NUMBER_PREFIX:
return readBigNumber(line);
case BLOB_ERROR_PREFIX:
return readBlobError(bufReader);
case BOOLEAN_PREFIX:
return readBoolean(line);
case BULK_STRING_PREFIX:
case VERBATIM_STRING_PREFIX:
return isSteamedReply(line)
? await readStreamedString(bufReader)
: await readBulkString(line, bufReader);
case DOUBLE_PREFIX:
case INTEGER_PREFIX:
return readNumber(line);
case ERROR_PREFIX:
return readError(line);
case MAP_PREFIX:
return isSteamedReply(line)
? await readStreamedMap(bufReader)
: await readMap(line, bufReader);
case NULL_PREFIX:
return null;
case SET_PREFIX:
return isSteamedReply(line)
? await readStreamedSet(bufReader)
: await readSet(line, bufReader);
case SIMPLE_STRING_PREFIX:
return readSimpleString(line);
/** No prefix */
default:
return line;
}
}
/** 3. Combined */
/**
* Sends a command to the Redis server and returns the parsed reply.
*
* @example
* ```ts
* import { sendCommand } from "https://deno.land/x/r2d2@$VERSION/mod.ts";
*
* const redisConn = await Deno.connect({ port: 6379 });
*
* // Returns "OK"
* await sendCommand(redisConn, ["SET", "hello", "world"]);
*
* // Returns "world"
* await sendCommand(redisConn, ["GET", "hello"]);
* ```
*/
export async function sendCommand(
redisConn: Deno.Conn,
command: Command,
): Promise<Reply> {
await writeCommand(redisConn, command);
return await readReply(new BufReader(redisConn));
}
/**
* Pipelines commands to the Redis server and returns the parsed replies.
*
* @example
* ```ts
* import { pipelineCommands } from "https://deno.land/x/r2d2@$VERSION/mod.ts";
*
* const redisConn = await Deno.connect({ port: 6379 });
*
* // Returns [1, 2, 3, 4]
* await pipelineCommands(redisConn, [
* ["INCR", "X"],
* ["INCR", "X"],
* ["INCR", "X"],
* ["INCR", "X"],
* ]);
* ```
*/
export async function pipelineCommands(
redisConn: Deno.Conn,
commands: Command[],
): Promise<Reply[]> {
const string = commands.map(createCommandString).join("");
await writeAll(redisConn, encoder.encode(string));
return readNReplies(commands.length, new BufReader(redisConn));
}
/**
* Used for pub/sub. Listens for replies from the Redis server.
*
* @example
* ```ts
* import { writeCommand, listenReplies } from "https://deno.land/x/r2d2@$VERSION/mod.ts";
*
* const redisConn = await Deno.connect({ port: 6379 });
*
* await writeCommand(redisConn, ["SUBSCRIBE", "mychannel"]);
*
* for await (const reply of listenReplies(redisConn)) {
* // Prints ["subscribe", "mychannel", 1] first iteration
* console.log(reply);
* }
* ```
*/
export async function* listenReplies(
redisConn: Deno.Conn,
): AsyncIterableIterator<Reply> {
const bufReader = new BufReader(redisConn);
while (true) {
yield await readReply(bufReader);
}
}