diff --git a/.denov b/.denov index 12b531b5..ab58ac5f 100644 --- a/.denov +++ b/.denov @@ -1 +1 @@ -v0.33.0 \ No newline at end of file +v0.34.0 \ No newline at end of file diff --git a/io.ts b/io.ts index 7af635a0..c07f1e17 100644 --- a/io.ts +++ b/io.ts @@ -2,10 +2,11 @@ import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts"; import Buffer = Deno.Buffer; import { ErrorReplyError } from "./errors.ts"; +export type BulkResult = string | undefined; export type RedisRawReply = | ["status", string] | ["integer", number] - | ["bulk", string] + | ["bulk", BulkResult] | ["array", any[]] | ["error", ErrorReplyError]; @@ -39,7 +40,7 @@ export async function sendCommand( writer: BufWriter, reader: BufReader, command: string, - ...args + ...args: (number | string)[] ): Promise { const msg = createRequest(command, ...args); await writer.write(encoder.encode(msg)); @@ -64,6 +65,7 @@ export async function readReply(reader: BufReader): Promise { case ErrorReplyCode: tryParseErrorReply(await readLine(reader)); } + throw new Error("Invalid state"); } export async function readLine(reader: BufReader): Promise { @@ -81,6 +83,7 @@ export async function readLine(reader: BufReader): Promise { } buf[loc++] = d; } + throw new Error("Invalid state"); } export async function readStatusReply(reader: BufReader): Promise { @@ -100,7 +103,7 @@ export async function readIntegerReply(reader: BufReader): Promise { tryParseErrorReply(line); } -export async function readBulkReply(reader: BufReader): Promise { +export async function readBulkReply(reader: BufReader): Promise { const line = await readLine(reader); if (line[0] !== "$") { tryParseErrorReply(line); @@ -109,7 +112,7 @@ export async function readBulkReply(reader: BufReader): Promise { const size = parseInt(sizeStr); if (size < 0) { // nil bulk reply - return; + return undefined; } const dest = new Uint8Array(size + 2); await reader.readFull(dest); @@ -119,7 +122,7 @@ export async function readBulkReply(reader: BufReader): Promise { export async function readArrayReply(reader: BufReader): Promise { const line = await readLine(reader); const argCount = parseInt(line.substr(1, line.length - 3)); - const result = []; + const result: any[] = []; for (let i = 0; i < argCount; i++) { const res = await reader.peek(1); if (res === Deno.EOF) { @@ -143,7 +146,7 @@ export async function readArrayReply(reader: BufReader): Promise { return result; } -function tryParseErrorReply(line: string) { +function tryParseErrorReply(line: string): never { const code = line[0]; if (code === "-") { throw new ErrorReplyError(line); diff --git a/modules-lock.json b/modules-lock.json index 020db55c..794d48ae 100644 --- a/modules-lock.json +++ b/modules-lock.json @@ -1,6 +1,6 @@ { "https://deno.land/std": { - "version": "@v0.33.0", + "version": "@v0.34.0", "modules": [ "/util/async.ts", "/testing/asserts.ts", diff --git a/modules.json b/modules.json index 81735bae..3bdeda84 100644 --- a/modules.json +++ b/modules.json @@ -1,6 +1,6 @@ { "https://deno.land/std": { - "version": "@v0.33.0", + "version": "@v0.34.0", "modules": [ "/util/async.ts", "/testing/asserts.ts", diff --git a/pipeline.ts b/pipeline.ts index 514eed7c..8a3d00e7 100644 --- a/pipeline.ts +++ b/pipeline.ts @@ -6,7 +6,7 @@ import { deferred, Deferred } from "./vendor/https/deno.land/std/util/async.ts"; const encoder = new TextEncoder(); export type RedisPipeline = { - enqueue(command: string, ...args); + enqueue(command: string, ...args: (number | string)[]): void; flush(): Promise; } & Redis; @@ -54,11 +54,11 @@ export function createRedisPipeline( } const executor = { - enqueue(command: string, ...args) { + enqueue(command: string, ...args: (number | string)[]): void { const msg = createRequest(command, ...args); commands.push(msg); }, - async flush() { + async flush(): Promise { // wrap pipelined commands with MULTI/EXEC if (opts && opts.tx) { commands.splice(0, 0, createRequest("MULTI")); @@ -80,6 +80,19 @@ export function createRedisPipeline( return ["status", "OK"]; } }; - const fakeRedis = create(null, null, null, executor); + const d = dummyReadWriteCloser(); + const fakeRedis = create(d, d, d, executor); return Object.assign(fakeRedis, executor); } + +function dummyReadWriteCloser(): Deno.ReadWriteCloser { + return { + close() {}, + async read(p) { + return 0; + }, + async write(p) { + return 0; + } + }; +} diff --git a/pipeline_test.ts b/pipeline_test.ts index c784bafc..6dc8c215 100644 --- a/pipeline_test.ts +++ b/pipeline_test.ts @@ -91,11 +91,18 @@ test("pipeline in concurrent", async () => { "OK", //set(a) "OK", //set(b) "OK", //set(c) - [["status", "OK"], ["status", "OK"], ["status", "OK"]], //flush() + [ + ["status", "OK"], + ["status", "OK"], + ["status", "OK"] + ], //flush() "OK", // get(a) "OK", // get(b) "OK", //get(c) - [["bulk", "a"], ["bulk", "b"], ["bulk", "c"]] //flush() + [ + ["bulk", "a"], + ["bulk", "b"], + ["bulk", "c"] + ] //flush() ]); }); - diff --git a/pubsub.ts b/pubsub.ts index 9fa71fe3..0a925c65 100644 --- a/pubsub.ts +++ b/pubsub.ts @@ -28,7 +28,7 @@ class RedisSubscriptionImpl implements RedisSubscription { constructor(private writer: BufWriter, private reader: BufReader) {} - async psubscribe(...patterns) { + async psubscribe(...patterns: string[]) { await sendCommand(this.writer, this.reader, "PSUBSCRIBE", ...patterns); for (const pat of patterns) { this.channels[pat] = true; diff --git a/pubsub_test.ts b/pubsub_test.ts index bebc491b..d965c8ba 100644 --- a/pubsub_test.ts +++ b/pubsub_test.ts @@ -7,7 +7,7 @@ const addr = { port: 6379 }; -async function wait(duration) { +async function wait(duration: number) { return new Promise(resolve => { setTimeout(resolve, duration); }); @@ -27,20 +27,19 @@ test(async function testSubscribe2() { const redis = await connect(addr); const pub = await connect(addr); const sub = await redis.subscribe("subsc2"); - let message: RedisPubSubMessage; const p = (async function() { const it = sub.receive(); - message = (await it.next()).value; + return (await it.next()).value; })(); await pub.publish("subsc2", "wayway"); - await p; + const message = await p; assertEquals(message, { channel: "subsc2", message: "wayway" }); await sub.close(); const a = await redis.get("aaa"); - assertEquals(a, void 0); + assertEquals(a, undefined); pub.close(); redis.close(); }); diff --git a/redis.ts b/redis.ts index 083934e1..025b8b4c 100644 --- a/redis.ts +++ b/redis.ts @@ -5,18 +5,17 @@ import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts"; import { yellow } from "./vendor/https/deno.land/std/fmt/colors.ts"; import { ConnectionClosedError } from "./errors.ts"; import { psubscribe, RedisSubscription, subscribe } from "./pubsub.ts"; -import { RedisRawReply, sendCommand } from "./io.ts"; +import { RedisRawReply, sendCommand, BulkResult } from "./io.ts"; import { createRedisPipeline, RedisPipeline } from "./pipeline.ts"; import { deferred, Deferred } from "./vendor/https/deno.land/std/util/async.ts"; - export type Redis = { // Connection - auth(password: string): Promise; - echo(message: string): Promise; + auth(password: string): Promise; + echo(message: string): Promise; ping(message?: string): Promise; quit(): Promise; select(index: number): Promise; - swapdb(index, index2): Promise; + swapdb(index: number, index2: number): Promise; // Keys del(...keys: string[]): Promise; dump(key: string): Promise; @@ -37,11 +36,11 @@ export type Redis = { } ): Promise; move(key: string, db: string): Promise; - object_refcount(key: string); - object_encoding(key: string); - object_ideltime(key: string); - object_freq(key: string); - object_help(); + object_refcount(key: string): Promise; + object_encoding(key: string): Promise; + object_ideltime(key: string): Promise; + object_freq(key: string): Promise; + object_help(): Promise; persist(key: string): Promise; pexpire(key: string, milliseconds: number): Promise; pexpireat(key: string, milliseconds_timestamp: number): Promise; @@ -77,7 +76,11 @@ export type Redis = { bitcount(key: string): Promise; bitcount(key: string, start: number, end: number): Promise; bitfield(): Promise; - bitop(operation, destkey: string, ...keys: string[]): Promise; + bitop( + operation: "AND" | "OR" | "XOR" | "NOT", + destkey: string, + ...keys: string[] + ): Promise; bitpos( key: string, bit: number, @@ -94,7 +97,7 @@ export type Redis = { mset(...key_values: string[]): Promise; msetnx(key: string, value: string): Promise; msetnx(...key_values: string[]): Promise; - psetex(key: string, milliseconds: number, value: string); + psetex(key: string, milliseconds: number, value: string): Promise; set( key: string, value: string, @@ -103,13 +106,13 @@ export type Redis = { px?: number; mode?: "NX" | "XX"; } - ): Promise; + ): Promise; setbit(key: string, offset: number, value: string): Promise; setex(key: string, seconds: number, value: string): Promise; setnx(key: string, value: string): Promise; setrange(key: string, offset: number, value: string): Promise; strlen(key: string): Promise; - get(key: string): Promise; + get(key: string): Promise; getbit(key: string, offset: number): Promise; getrange(key: string, start: number, end: number): Promise; getset(key: string, value: string): Promise; @@ -147,7 +150,7 @@ export type Redis = { store?: string; storeDist?: string; } - ); + ): Promise; georadiusbymember( key: string, member: string, @@ -162,7 +165,7 @@ export type Redis = { store?: string; storeDist?: string; } - ); + ): Promise; // Hash hdel(key: string, ...fields: string[]): Promise; hexists(key: string, field: string): Promise; @@ -235,7 +238,7 @@ export type Redis = { srandmember(key: string, count?: number): Promise; srem(key: string, ...members: string[]): Promise; sunion(...keys: string[]): Promise; - sunionstore(destination, ...keys: string[]): Promise; + sunionstore(destination: string, ...keys: string[]): Promise; // SortedSet bzpopmin(key: string | string[], timeout: number): Promise; bzpopmax(key: string | string[], timeout: number): Promise; @@ -260,7 +263,7 @@ export type Redis = { ): Promise; zcard(key: string): Promise; zcount(key: string, min: number, max: number): Promise; - zincrby(key: string, increment, member: string): Promise; + zincrby(key: string, increment: number, member: string): Promise; zinterstore( destination: string, numkeys: number, @@ -343,22 +346,22 @@ export type Redis = { // Cluster // cluster // // Server - bgrewriteaof(): Promise; - bgsave(): Promise; + bgrewriteaof(): Promise; + bgsave(): Promise; // client // command(): Promise; command_count(): Promise; command_getkeys(): Promise; command_info(...command_names: string[]): Promise; config_get(parameter: string): Promise; - config_rewrite(): Promise; - config_set(parameter: string, value: string): Promise; - config_resetstat(): Promise; + config_rewrite(): Promise; + config_set(parameter: string, value: string): Promise; + config_resetstat(): Promise; dbsize(): Promise; - debug_object(key: string): Promise; - debug_segfault(): Promise; - flushall(async?: boolean): Promise; - flushdb(async?: boolean): Promise; + debug_object(key: string): Promise; + debug_segfault(): Promise; + flushall(async?: boolean): Promise; + flushdb(async?: boolean): Promise; info(section?: string): Promise; lastsave(): Promise; memory_doctor(): Promise; @@ -372,20 +375,20 @@ export type Redis = { samples?: number; } ): Promise; - monitor(); + monitor(): void; role(): Promise; save(): Promise; shutdown(arg: "NOSAVE" | "SAVE"): Promise; slaveof(host: string, port: string | number): Promise; replicaof(host: string, port: string | number): Promise; - slowlog(subcommand: string, ...argument: string[]); - sync(); + slowlog(subcommand: string, ...argument: string[]): Promise; + sync(): void; time(): Promise; // Scripting - eval(script: string, key: string, arg: string); - eval(script: string, keys: string[], args: string[]); - evalsha(sha1: string, key: string, arg: string); - evalsha(sha1: string, keys: string[], args: string[]); + eval(script: string, key: string, arg: string): Promise; + eval(script: string, keys: string[], args: string[]): Promise; + evalsha(sha1: string, key: string, arg: string): Promise; + evalsha(sha1: string, keys: string[], args: string[]): Promise; script_debug(arg: "YES" | "SYNC" | "NO"): Promise; script_exists(...sha1s: string[]): Promise; script_flush(): Promise; @@ -394,7 +397,7 @@ export type Redis = { // multi multi(): Promise; exec(): Promise; - discard(): Promise; + discard(): Promise; watch(...keys: string[]): Promise; unwatch(): Promise; // pipeline @@ -407,7 +410,7 @@ export type Redis = { pattern?: string; count?: number; } - ); + ): Promise; hscan( key: string, cursor: number, @@ -415,7 +418,7 @@ export type Redis = { pattern?: string; count?: number; } - ); + ): Promise; sscan( key: string, cursor: number, @@ -423,17 +426,17 @@ export type Redis = { pattern?: string; count?: number; } - ); + ): Promise; zscan( key: string, cursor: number, opts?: { pattern?: string; } - ); + ): Promise; readonly isClosed: boolean; - close(); + close(): void; }; export interface CommandExecutor { @@ -520,9 +523,12 @@ class RedisImpl implements Redis { async execBulkReply( command: string, ...args: (string | number)[] - ): Promise { + ): Promise { const [_, reply] = await this.executor.execRawReply(command, ...args); - return reply as string; + if (typeof reply !== "string" && reply != null) { + throw new Error(); + } + return reply; } async execArrayReply( @@ -533,11 +539,11 @@ class RedisImpl implements Redis { return reply as any[]; } - append(key, value) { + append(key: string, value: string | number) { return this.execIntegerReply("APPEND", key, value); } - auth(password) { + auth(password: string) { return this.execBulkReply("AUTH", password); } @@ -549,23 +555,33 @@ class RedisImpl implements Redis { return this.execBulkReply("BGSAVE"); } - bitcount(key, start?, end?) { - return this.execIntegerReply("BITCOUNT", key, start, end); + bitcount(key: string, start?: number, end?: number) { + if (start != null && end != null) + return this.execIntegerReply("BITCOUNT", key, start, end); + else if (start != null) + return this.execIntegerReply("BITCOUNT", key, start); + else return this.execIntegerReply("BITCOUNT", key); } bitfield() { return this.execArrayReply("BITFIELD"); } - bitop(operation, destkey, ...keys) { + bitop(operation: string, destkey: string, ...keys: string[]) { return this.execIntegerReply("BITOP", operation, destkey, ...keys); } - bitpos(key, bit, start?, end?) { - return this.execIntegerReply("BITPOS", key, bit, start, end); + bitpos(key: string, bit: number, start?: number, end?: number) { + if (start != null && end != null) { + return this.execIntegerReply("BITPOS", key, bit, start, end); + } else if (start != null) { + return this.execIntegerReply("BITPOS", key, bit, start); + } else { + return this.execIntegerReply("BITPOS", key, bit); + } } - blpop(keys, timeout) { + blpop(keys: string[], timeout: number) { if (typeof keys === "string") { return this.execArrayReply("BLPOP", keys, timeout); } else { @@ -573,7 +589,7 @@ class RedisImpl implements Redis { } } - brpop(keys, timeout) { + brpop(keys: string[], timeout: number) { if (typeof keys === "string") { return this.execArrayReply("BRPOP", keys, timeout); } else { @@ -581,11 +597,11 @@ class RedisImpl implements Redis { } } - brpoplpush(source, destination, timeout) { + brpoplpush(source: string, destination: string, timeout: number) { return this.execStatusReply("BRPOPLPUSH", source, destination, timeout); } - bzpopmin(keys, timeout) { + bzpopmin(keys: string[], timeout: number) { if (typeof keys === "string") { return this.execArrayReply("BZPOPMIN", keys, timeout); } else { @@ -593,7 +609,7 @@ class RedisImpl implements Redis { } } - bzpopmax(keys, timeout) { + bzpopmax(keys: string[], timeout: number) { if (typeof keys === "string") { return this.execArrayReply("BZPOPMAX", keys, timeout); } else { @@ -613,11 +629,11 @@ class RedisImpl implements Redis { return this.execArrayReply("COMMAND", "GETKEYS"); } - command_info(...command_names) { + command_info(...command_names: string[]) { return this.execArrayReply("COMMAND", "INFO", ...command_names); } - config_get(parameter) { + config_get(parameter: string) { return this.execArrayReply("CONFIG", "GET", parameter); } @@ -625,7 +641,7 @@ class RedisImpl implements Redis { return this.execBulkReply("CONFIG", "REWRITE"); } - config_set(parameter, value) { + config_set(parameter: string, value: string | number) { return this.execBulkReply("CONFIG", "SET", parameter, value); } @@ -637,7 +653,7 @@ class RedisImpl implements Redis { return this.execIntegerReply("DBSIZE"); } - debug_object(key) { + debug_object(key: string) { return this.execBulkReply("DEBUG", "OBJECT", key); } @@ -645,15 +661,15 @@ class RedisImpl implements Redis { return this.execBulkReply("DEBUG", "SEGFAULT"); } - decr(key) { + decr(key: string) { return this.execIntegerReply("DECR", key); } - decrby(key, decrement) { + decrby(key: string, decrement: number) { return this.execIntegerReply("DECRBY", key, decrement); } - del(key, ...keys) { + del(key: string, ...keys: string[]) { return this.execIntegerReply("DEL", key, ...keys); } @@ -661,23 +677,28 @@ class RedisImpl implements Redis { return this.execBulkReply("DISCARD"); } - dump(key) { + dump(key: string) { return this.execStatusReply("DUMP", key); } - echo(message) { + echo(message: string) { return this.execStatusReply("ECHO", message); } - eval(script, keys, args) { - return this.doEval("EVAL", script, keys, args); + eval(script: string, keys: string | string[], arg: string | string[]) { + return this.doEval("EVAL", script, keys, arg); } - evalsha(sha1, keys, args) { + evalsha(sha1: string, keys: string | string[], args: string | string[]) { return this.doEval("EVALSHA", sha1, keys, args); } - private doEval(cmd, script, keys, args) { + private doEval( + cmd: string, + script: string, + keys: string | string[], + args: string | string[] + ) { const _args = [script]; if (typeof keys === "string") { _args.push(keys); @@ -696,29 +717,29 @@ class RedisImpl implements Redis { return this.execArrayReply("EXEC"); } - exists(...keys) { + exists(...keys: string[]) { return this.execIntegerReply("EXISTS", ...keys); } - expire(key, seconds) { + expire(key: string, seconds: number) { return this.execIntegerReply("EXPIRE", key, seconds); } - expireat(key, timestamp) { + expireat(key: string, timestamp: string) { return this.execIntegerReply("EXPIREAT", key, timestamp); } - flushall(async) { + flushall(async: boolean) { const args = async ? ["ASYNC"] : []; return this.execBulkReply("FLUSHALL", ...args); } - flushdb(async) { + flushdb(async: boolean) { const args = async ? ["ASYNC"] : []; return this.execBulkReply("FLUSHDB", ...args); } - geoadd(key, ...args) { + geoadd(key: string, ...args: any) { const _args = [key]; if (Array.isArray([args[0]])) { for (const triple of args) { @@ -730,19 +751,36 @@ class RedisImpl implements Redis { return this.execIntegerReply("GEOADD", key, ..._args); } - geohash(key, ...members) { + geohash(key: string, ...members: string[]) { return this.execArrayReply("GEOHASH", key, ...members); } - geopos(key, ...members) { + geopos(key: string, ...members: string[]) { return this.execArrayReply("GEOPOS", key, ...members); } - geodist(key, member1, member2, unit?) { - return this.execStatusReply("GEODIST", key, member1, member2, unit); + geodist(key: string, member1: string, member2: string, unit?: string) { + if (unit) + return this.execStatusReply("GEODIST", key, member1, member2, unit); + else return this.execStatusReply("GEODIST", key, member1, member2); } - georadius(key, longitude, latitude, radius, unit, opts?) { + georadius( + key: string, + longitude: number, + latitude: number, + radius: number, + unit: string, + opts?: { + withCoord?: boolean; + withDist?: boolean; + withHash?: boolean; + count?: number; + sort?: "ASC" | "DESC"; + store?: string; + storeDist?: string; + } + ) { const args = this.pushGeoRadiusOpts( [key, longitude, latitude, radius, unit], opts @@ -750,12 +788,39 @@ class RedisImpl implements Redis { return this.execArrayReply("GEORADIUS", ...args); } - georadiusbymember(key, member, radius, unit, opts?) { + georadiusbymember( + key: string, + member: string, + radius: number, + unit: string, + opts?: { + withCoord?: boolean; + withDist?: boolean; + withHash?: boolean; + count?: number; + sort?: "ASC" | "DESC"; + store?: string; + storeDist?: string; + } + ) { const args = this.pushGeoRadiusOpts([key, member, radius, unit], opts); return this.execArrayReply("GEORADIUSBYMEMBER", ...args); } - private pushGeoRadiusOpts(args: (string | number)[], opts) { + private pushGeoRadiusOpts( + args: (string | number)[], + opts: + | { + withCoord?: boolean; + withDist?: boolean; + withHash?: boolean; + count?: number; + sort?: "ASC" | "DESC"; + store?: string; + storeDist?: string; + } + | undefined + ) { if (!opts) return args; if (opts.withCoord) { args.push("WITHCOORD"); @@ -781,95 +846,99 @@ class RedisImpl implements Redis { return args; } - get(key) { - return this.execStatusReply("GET", key); + get(key: string) { + return this.execBulkReply("GET", key); } - getbit(key, offset) { + getbit(key: string, offset: number) { return this.execIntegerReply("GETBIT", key, offset); } - getrange(key, start, end) { + getrange(key: string, start: number, end: number) { return this.execStatusReply("GETRANGE", key, start, end); } - getset(key, value) { + getset(key: string, value: string) { return this.execStatusReply("GETSET", key, value); } - hdel(key, field, ...fields) { + hdel(key: string, field: string, ...fields: string[]) { return this.execIntegerReply("HDEL", key, field, ...fields); } - hexists(key, field) { + hexists(key: string, field: string) { return this.execIntegerReply("HEXISTS", key, field); } - hget(key, field) { + hget(key: string, field: string) { return this.execStatusReply("HGET", key, field); } - hgetall(key) { + hgetall(key: string) { return this.execArrayReply("HGETALL", key); } - hincrby(key, field, increment) { + hincrby(key: string, field: string, increment: number) { return this.execIntegerReply("HINCRBY", key, field, increment); } - hincrbyfloat(key, field, increment) { + hincrbyfloat(key: string, field: string, increment: number) { return this.execStatusReply("HINCRBYFLOAT", key, field, increment); } - hkeys(key) { + hkeys(key: string) { return this.execArrayReply("HKEYS", key); } - hlen(key) { + hlen(key: string) { return this.execIntegerReply("HLEN", key); } - hmget(key, ...fields) { + hmget(key: string, ...fields: string[]) { return this.execArrayReply("HMGET", key, ...fields); } - hmset(key, ...field_values) { - return this.execBulkReply("HMSET", key, ...field_values); + hmset(key: string, ...field_values: string[]) { + return this.execStatusReply("HMSET", key, ...field_values); } - hset(key, field, value) { + hset(key: string, field: string, value: string) { return this.execIntegerReply("HSET", key, field, value); } - hsetnx(key, field, value) { + hsetnx(key: string, field: string, value: string) { return this.execIntegerReply("HSETNX", key, field, value); } - hstrlen(key, field) { + hstrlen(key: string, field: string) { return this.execIntegerReply("HSTRLEN", key, field); } - hvals(key) { + hvals(key: string) { return this.execArrayReply("HVALS", key); } - incr(key) { + incr(key: string) { return this.execIntegerReply("INCR", key); } - incrby(key, increment) { + incrby(key: string, increment: number) { return this.execIntegerReply("INCRBY", key, increment); } - incrbyfloat(key, increment) { + incrbyfloat(key: string, increment: number) { return this.execStatusReply("INCRBYFLOAT", key, increment); } - info(section?) { - return this.execStatusReply("INFO", section); + info(section?: string) { + if (section) { + return this.execStatusReply("INFO", section); + } else { + return this.execStatusReply("INFO"); + } } - keys(pattern) { + keys(pattern: string) { return this.execArrayReply("KEYS", pattern); } @@ -877,44 +946,44 @@ class RedisImpl implements Redis { return this.execIntegerReply("LASTSAVE"); } - lindex(key, index) { + lindex(key: string, index: number) { return this.execStatusReply("LINDEX", key, index); } - linsert(key, arg: "BEFORE" | "AFTER", pivot, value) { + linsert(key: string, arg: "BEFORE" | "AFTER", pivot: string, value: string) { return this.execIntegerReply("LINSERT", key, arg); } - llen(key) { + llen(key: string) { return this.execIntegerReply("LLEN", key); } - lpop(key) { + lpop(key: string) { return this.execStatusReply("LPOP", key); } - lpush(key, ...values) { + lpush(key: string, ...values: (string | number)[]) { return this.execIntegerReply("LPUSH", key, ...values); } - lpushx(key, value) { + lpushx(key: string, value: string | number) { return this.execIntegerReply("LPUSHX", key, value); } - lrange(key, start, stop) { + lrange(key: string, start: number, stop: number) { return this.execArrayReply("LRANGE", key, start, stop); } - lrem(key, count, value) { + lrem(key: string, count: number, value: string | number) { return this.execIntegerReply("LREM", key, count, value); } - lset(key, index, value) { - return this.execBulkReply("LSET", key, index, value); + lset(key: string, index: number, value: string | number) { + return this.execStatusReply("LSET", key, index, value); } - ltrim(key, start, stop) { - return this.execBulkReply("LTRIM", key, start, stop); + ltrim(key: string, start: number, stop: number) { + return this.execStatusReply("LTRIM", key, start, stop); } memory_doctor() { @@ -930,26 +999,42 @@ class RedisImpl implements Redis { } memory_purge() { - return this.execBulkReply("MEMORY", "PURGE"); + return this.execStatusReply("MEMORY", "PURGE"); } memory_stats() { return this.execArrayReply("MEMORY", "STATS"); } - memory_usage(key, opts?) { - const args = [key]; + memory_usage( + key: string, + opts?: { + samples?: number; + } + ) { + const args: (number | string)[] = [key]; if (opts && typeof opts.samples === "number") { args.push("SAMPLES", opts.samples); } return this.execIntegerReply("MEMORY", "USAGE", ...args); } - mget(...keys) { + mget(...keys: string[]) { return this.execArrayReply("MGET", ...keys); } - migrate(host, port, key, destination_db, timeout, opts?) { + migrate( + host: string, + port: number | string, + key: string, + destination_db: string, + timeout: number, + opts?: { + copy?: boolean; + replace?: boolean; + keys?: string[]; + } + ) { const args = [host, port, key, destination_db, timeout]; if (opts) { if (opts.copy) { @@ -966,23 +1051,23 @@ class RedisImpl implements Redis { } monitor() { - // + throw new Error("not supported yet"); } - move(key, db) { + move(key: string, db: string) { return this.execIntegerReply("MOVE", key, db); } - mset(...key_values) { - return this.execBulkReply("MSET", ...key_values); + mset(...key_values: string[]) { + return this.execStatusReply("MSET", ...key_values); } - msetnx(...key_values) { + msetnx(...key_values: string[]) { return this.execIntegerReply("MSETNX", ...key_values); } multi() { - return this.execBulkReply("MULTI"); + return this.execStatusReply("MULTI"); } object_encoding(key: string) { @@ -1005,49 +1090,50 @@ class RedisImpl implements Redis { return this.execIntegerReply("OBJECT", "REFCOUNT", key); } - persist(key) { + persist(key: string) { return this.execIntegerReply("PERSIST", key); } - pexpire(key, milliseconds) { + pexpire(key: string, milliseconds: number) { return this.execIntegerReply("PEXPIRE", key, milliseconds); } - pexpireat(key, milliseconds_timestamp) { + pexpireat(key: string, milliseconds_timestamp: number) { return this.execIntegerReply("PEXPIREAT", key, milliseconds_timestamp); } - pfadd(key, element, ...elements) { + pfadd(key: string, element: string, ...elements: string[]) { return this.execIntegerReply("PFADD", key, element, ...elements); } - pfcount(key, ...keys) { + pfcount(key: string, ...keys: string[]) { return this.execIntegerReply("PFCOUNT", key, ...keys); } - pfmerge(destkey, ...sourcekeys) { - return this.execBulkReply("PFMERGE", destkey, ...sourcekeys); + pfmerge(destkey: string, ...sourcekeys: string[]) { + return this.execStatusReply("PFMERGE", destkey, ...sourcekeys); } - ping(message?) { - return this.execBulkReply("PING", message); + ping(message?: string) { + if (message) return this.execStatusReply("PING", message); + else return this.execStatusReply("PING"); } - psetex(key, milliseconds, value) { - // + psetex(key: string, milliseconds: number, value: string) { + return this.execStatusReply("PSETEX", key, milliseconds, value); } // PubSub - publish(channel, message) { + publish(channel: string, message: string) { return this.execIntegerReply("PUBLISH", channel, message); } - subscribe(...channels) { + subscribe(...channels: string[]) { return subscribe(this.writer, this.reader, ...channels); } - psubscribe(...patterns) { + psubscribe(...patterns: string[]) { return psubscribe(this.writer, this.reader, ...patterns); } @@ -1063,13 +1149,13 @@ class RedisImpl implements Redis { return this.execArrayReply("PUBSUB", "NUMSUBS", ...channels); } - pttl(key) { + pttl(key: string) { return this.execIntegerReply("PTTL", key); } quit() { try { - return this.execBulkReply("QUIT"); + return this.execStatusReply("QUIT"); } finally { this._isClosed = true; } @@ -1080,95 +1166,108 @@ class RedisImpl implements Redis { } readonly() { - return this.execBulkReply("READONLY"); + return this.execStatusReply("READONLY"); } readwrite() { - return this.execBulkReply("READWRITE"); + return this.execStatusReply("READWRITE"); } - rename(key, newkey) { - return this.execBulkReply("RENAME", key, newkey); + rename(key: string, newkey: string) { + return this.execStatusReply("RENAME", key, newkey); } - renamenx(key, newkey) { + renamenx(key: string, newkey: string) { return this.execIntegerReply("RENAMENX", key, newkey); } - restore(key, ttl, serialized_value, REPLACE?) { + restore( + key: string, + ttl: number, + serialized_value: string, + REPLACE?: boolean + ) { const args = [key, ttl, serialized_value]; if (REPLACE) { args.push("REPLACE"); } - return this.execBulkReply("RESTORE", ...args); + return this.execStatusReply("RESTORE", ...args); } role() { return this.execArrayReply("ROLE"); } - rpop(key) { + rpop(key: string) { return this.execStatusReply("RPOP", key); } - rpoplpush(source, destination) { + rpoplpush(source: string, destination: string) { return this.execStatusReply("RPOPLPUSH", source, destination); } - rpush(key, ...values) { + rpush(key: string, ...values: (string | number)[]) { return this.execIntegerReply("RPUSH", key, ...values); } - rpushx(key, value) { + rpushx(key: string, value: string) { return this.execIntegerReply("RPUSHX", key, value); } - sadd(key, member, ...members) { + sadd(key: string, member: string, ...members: string[]) { return this.execIntegerReply("SADD", key, member, ...members); } save() { - return this.execBulkReply("SAVE"); + return this.execStatusReply("SAVE"); } - scard(key) { + scard(key: string) { return this.execIntegerReply("SCARD", key); } script_debug(arg: "YES" | "SYNC" | "NO") { - return this.execBulkReply("SCRIPT", "DEBUG", arg); + return this.execStatusReply("SCRIPT", "DEBUG", arg); } - script_exists(...sha1s) { + script_exists(...sha1s: string[]) { return this.execArrayReply("SCRIPT", "EXISTS", ...sha1s); } script_flush() { - return this.execBulkReply("SCRIPT", "FLUSH"); + return this.execStatusReply("SCRIPT", "FLUSH"); } script_kill() { - return this.execBulkReply("SCRIPT", "KILL"); + return this.execStatusReply("SCRIPT", "KILL"); } - script_load(script) { + script_load(script: string) { return this.execStatusReply("SCRIPT", "LOAD", script); } - sdiff(...keys) { + sdiff(...keys: string[]) { return this.execArrayReply("SDIFF", ...keys); } - sdiffstore(destination, key, ...keys) { + sdiffstore(destination: string, key: string, ...keys: string[]) { return this.execIntegerReply("SDIFFSTORE", destination, key, ...keys); } - select(index) { - return this.execBulkReply("SELECT", index); + select(index: number) { + return this.execStatusReply("SELECT", index); } - set(key, value, opts?) { - const args = [key, value]; + set( + key: string, + value: string, + opts?: { + ex?: number; + px?: number; + mode?: "NX" | "XX"; + } + ) { + const args: (number | string)[] = [key, value]; if (opts) { if (opts.ex) { args.push("EX", opts.ex); @@ -1182,60 +1281,71 @@ class RedisImpl implements Redis { return this.execBulkReply("SET", ...args); } - setbit(key, offset, value) { + setbit(key: string, offset: number, value: string) { return this.execIntegerReply("SETBIT", key, offset, value); } - setex(key, seconds, value) { - return this.execBulkReply("SETEX", key, seconds, value); + setex(key: string, seconds: number, value: string) { + return this.execStatusReply("SETEX", key, seconds, value); } - setnx(key, value) { + setnx(key: string, value: string) { return this.execIntegerReply("SETNX", key, value); } - setrange(key, offset, value) { + setrange(key: string, offset: number, value: string) { return this.execIntegerReply("SETRANGE", key, offset, value); } - shutdown(arg) { - return this.execBulkReply("SHUTDOWN", arg); + shutdown(arg: string) { + return this.execStatusReply("SHUTDOWN", arg); } - sinter(key, ...keys) { + sinter(key: string, ...keys: string[]) { return this.execArrayReply("SINTER", key, ...keys); } - sinterstore(destination, key, ...keys) { + sinterstore(destination: string, key: string, ...keys: string[]) { return this.execIntegerReply("SINTERSTORE", destination, key, ...keys); } - sismember(key, member) { + sismember(key: string, member: string) { return this.execIntegerReply("SISMEMBER", key, member); } - slaveof(host, port) { - return this.execBulkReply("SLAVEOF", host, port); + slaveof(host: string, port: string | number) { + return this.execStatusReply("SLAVEOF", host, port); } - replicaof(host, port) { - return this.execBulkReply("REPLICAOF", host, port); + replicaof(host: string, port: string | number) { + return this.execStatusReply("REPLICAOF", host, port); } - slowlog(subcommand, ...argument) { + slowlog(subcommand: string, ...argument: string[]) { return this.execRawReply("SLOWLOG", subcommand, ...argument); } - smembers(key) { + smembers(key: string) { return this.execArrayReply("SMEMBERS", key); } - smove(source, destination, member) { + smove(source: string, destination: string, member: string) { return this.execIntegerReply("SMOVE", source, destination, member); } - sort(key, opts?) { - const args = [key]; + sort( + key: string, + opts?: { + by?: string; + offset?: number; + count?: number; + patterns?: string[]; + order: "ASC" | "DESC"; + alpha?: boolean; + destination?: string; + } + ) { + const args: (number | string)[] = [key]; if (opts) { if (opts.by) { args.push("BY", opts.by); @@ -1267,36 +1377,36 @@ class RedisImpl implements Redis { spop(key: string): Promise; spop(key: string, count: number): Promise; - spop(...args): Promise { + spop(...args: (string | number)[]): Promise { return this.execStatusReply("SPOP", ...args); } - srandmember(...args) { - return this.execStatusReply("SRANDMEMBER", ...args); + srandmember(key: string, count?: number) { + if (count != null) return this.execStatusReply("SRANDMEMBER", key, count); + else return this.execStatusReply("SRANDMEMBER", key); } - srem(key, ...members) { + srem(key: string, ...members: string[]) { return this.execIntegerReply("SREM", key, ...members); } - strlen(key) { + strlen(key: string) { return this.execIntegerReply("STRLEN", key); } - sunion(...keys) { + sunion(...keys: string[]) { return this.execArrayReply("SUNION", ...keys); } - sunionstore(destination, ...keys) { + sunionstore(destination: string, ...keys: string[]) { return this.execIntegerReply("SUNIONSTORE", destination, ...keys); } - swapdb(index, index2) { - return this.execBulkReply("SWAPDB", index, index2); + swapdb(index: number, index2: number) { + return this.execStatusReply("SWAPDB", index, index2); } sync() { - // throw new Error("not implemented"); } @@ -1304,36 +1414,36 @@ class RedisImpl implements Redis { return this.execArrayReply("TIME"); } - touch(...keys) { + touch(...keys: string[]) { return this.execIntegerReply("TOUCH", ...keys); } - ttl(key) { + ttl(key: string) { return this.execIntegerReply("TTL", key); } - type(key) { - return this.execBulkReply("TYPE", key); + type(key: string) { + return this.execStatusReply("TYPE", key); } - unlink(...keys) { + unlink(...keys: string[]) { return this.execIntegerReply("UNLINK", ...keys); } unwatch() { - return this.execBulkReply("UNWATCH"); + return this.execStatusReply("UNWATCH"); } - wait(numreplicas, timeout) { + wait(numreplicas: number, timeout: number) { return this.execIntegerReply("WAIT", numreplicas, timeout); } - watch(key, ...keys) { - return this.execBulkReply("WATCH", key, ...keys); + watch(key: string, ...keys: string[]) { + return this.execStatusReply("WATCH", key, ...keys); } - zadd(key, scoreOrArr, memberOrOpts, opts?) { - const args = [key]; + zadd(key: string, scoreOrArr: any, memberOrOpts: any, opts?: any) { + const args: (string | number)[] = [key]; let _opts = opts; if (typeof scoreOrArr === "number") { args.push(scoreOrArr); @@ -1356,19 +1466,25 @@ class RedisImpl implements Redis { return this.execIntegerReply("ZADD", ...args); } - zcard(key) { + zcard(key: string) { return this.execIntegerReply("ZCARD", key); } - zcount(key, min, max) { + zcount(key: string, min: number, max: number) { return this.execIntegerReply("ZCOUNT", key, min, max); } - zincrby(key, increment, member) { + zincrby(key: string, increment: number, member: string) { return this.execStatusReply("ZINCRBY", key, increment, member); } - zinterstore(destination, numkeys, keys, weights?, aggregate?) { + zinterstore( + destination: string, + numkeys: number, + keys: string[], + weights?: number | number[], + aggregate?: string + ) { const args = this.pushZInterStoreArgs( [destination, numkeys], keys, @@ -1378,8 +1494,15 @@ class RedisImpl implements Redis { return this.execIntegerReply("ZINTERSTORE", ...args); } - zunionstore(destination, keys, opts?): Promise { - const args = [destination, keys.length, keys]; + zunionstore( + destination: string, + keys: string[], + opts?: { + weights?: number[]; + aggregate?: "SUM" | "MIN" | "MAX"; + } + ): Promise { + const args: (string | number)[] = [destination, keys.length, ...keys]; if (opts) { if (opts.weights) { args.push("WEIGHTS", ...opts.weights); @@ -1391,7 +1514,12 @@ class RedisImpl implements Redis { return this.execIntegerReply("ZUNIONSTORE", ...args); } - private pushZInterStoreArgs(args, keys, weights?, aggregate?) { + private pushZInterStoreArgs( + args: (number | string)[], + keys: string | string[], + weights?: number | number[], + aggregate?: string + ) { if (typeof keys === "string") { args.push(keys); } else { @@ -1412,39 +1540,79 @@ class RedisImpl implements Redis { return args; } - zlexcount(key, min, max) { + zlexcount(key: string, min: number, max: number) { return this.execIntegerReply("ZLEXCOUNT", key, min, max); } - zpopmax(key, count?) { - return this.execArrayReply("ZPOPMAX", key, count); + zpopmax(key: string, count?: number) { + if (count != null) return this.execArrayReply("ZPOPMAX", key, count); + else return this.execArrayReply("ZPOPMAX", key); } - zpopmin(key, count?) { - return this.execArrayReply("ZPOPMIN", key, count); + zpopmin(key: string, count?: number) { + if (count != null) return this.execArrayReply("ZPOPMIN", key, count); + else return this.execArrayReply("ZPOPMIN", key); } - zrange(key, start, stop, opts?) { + zrange( + key: string, + start: number, + stop: number, + opts?: { + withScore?: boolean; + } + ) { const args = this.pushZrangeOpts([key, start, stop], opts); return this.execArrayReply("ZRANGE", ...args); } - zrangebylex(key, min, max, opts?) { + zrangebylex( + key: string, + min: number, + max: number, + opts?: { + withScore?: boolean; + count?: number; + } + ) { const args = this.pushZrangeOpts([key, min, max], opts); return this.execArrayReply("ZRANGEBYLEX", ...args); } - zrevrangebylex(key, max, min, opts?) { + zrevrangebylex( + key: string, + max: number, + min: number, + opts?: { + withScore?: boolean; + count?: number; + } + ) { const args = this.pushZrangeOpts([key, min, max], opts); return this.execArrayReply("ZREVRANGEBYLEX", ...args); } - zrangebyscore(key, min, max, opts?) { + zrangebyscore( + key: string, + min: number, + max: number, + opts?: { + withScore?: boolean; + count?: number; + } + ) { const args = this.pushZrangeOpts([key, min, max], opts); return this.execArrayReply("ZRANGEBYSCORE", ...args); } - private pushZrangeOpts(args, opts?) { + private pushZrangeOpts( + args: (number | string)[], + opts?: { + withScore?: boolean; + offset?: number; + count?: number; + } + ) { if (opts) { if (opts.withScore) { args.push("WITHSCORES"); @@ -1456,65 +1624,113 @@ class RedisImpl implements Redis { return args; } - zrank(key, member) { + zrank(key: string, member: string) { return this.execIntegerReply("ZRANK", key, member); } - zrem(key, ...members) { + zrem(key: string, ...members: string[]) { return this.execIntegerReply("ZREM", key, ...members); } - zremrangebylex(key, min, max) { + zremrangebylex(key: string, min: number, max: number) { return this.execIntegerReply("ZREMRANGEBYLEX", key, min, max); } - zremrangebyrank(key, start, stop) { + zremrangebyrank(key: string, start: number, stop: number) { return this.execIntegerReply("ZREMRANGEBYRANK", key, start, stop); } - zremrangebyscore(key, min, max) { + zremrangebyscore(key: string, min: number, max: number) { return this.execIntegerReply("ZREMRANGEBYSCORE", key, min, max); } - zrevrange(key, start, stop, opts?) { + zrevrange( + key: string, + start: number, + stop: number, + opts?: { + withScore?: boolean; + } + ) { const args = this.pushZrangeOpts([key, start, stop], opts); return this.execArrayReply("ZREVRANGE", ...args); } - zrevrangebyscore(key, max, min, opts?) { + zrevrangebyscore( + key: string, + max: number, + min: number, + opts?: { + withScore?: boolean; + offset?: number; + count?: number; + } + ) { const args = this.pushZrangeOpts([key, max, min], opts); return this.execArrayReply("ZREVRANGEBYSCORE", ...args); } - zrevrank(key, member) { + zrevrank(key: string, member: string) { return this.execIntegerReply("ZREVRANK", key, member); } - zscore(key, member) { + zscore(key: string, member: string) { return this.execStatusReply("ZSCORE", key, member); } - scan(cursor, opts?) { + scan( + cursor: number, + opts?: { + pattern?: string; + count?: number; + } + ) { const arg = this.pushScanOpts([cursor], opts); return this.execArrayReply("SCAN", ...arg); } - sscan(key, cursor, opts?) { + sscan( + key: string, + cursor: number, + opts?: { + pattern?: string; + count?: number; + } + ) { const arg = this.pushScanOpts([key, cursor], opts); return this.execArrayReply("SSCAN", ...arg); } - hscan(key, cursor, opts?) { + hscan( + key: string, + cursor: number, + opts?: { + pattern?: string; + count?: number; + } + ) { const arg = this.pushScanOpts([key, cursor], opts); return this.execArrayReply("HSCAN", ...arg); } - zscan(key, cursor, opts?) { + zscan( + key: string, + cursor: number, + opts?: { + pattern?: string; + } + ) { const arg = this.pushScanOpts([key, cursor], opts); return this.execArrayReply("ZSCAN", ...arg); } - private pushScanOpts(arg, opts?) { + private pushScanOpts( + arg: (number | string)[], + opts?: { + pattern?: string; + count?: number; + } + ) { if (opts) { if (opts.pattern) { arg.push("MATCH", opts.pattern); diff --git a/redis_test.ts b/redis_test.ts index bf779606..7d3db01c 100644 --- a/redis_test.ts +++ b/redis_test.ts @@ -47,7 +47,7 @@ test(async function testExists() { test(async function testGetWhenNil() { const hoge = await redis.get("none"); - assertEquals(hoge, void 0); + assertEquals(hoge, undefined); }); test(async function testSet() { @@ -238,4 +238,3 @@ test(async function testDb1Option() { ); }); }); - diff --git a/vendor/https/deno.land/std/fmt/colors.ts b/vendor/https/deno.land/std/fmt/colors.ts index ac95f8de..bd50b31f 100644 --- a/vendor/https/deno.land/std/fmt/colors.ts +++ b/vendor/https/deno.land/std/fmt/colors.ts @@ -1 +1 @@ -export * from "https://deno.land/std@v0.33.0/fmt/colors.ts"; +export * from "https://deno.land/std@v0.34.0/fmt/colors.ts"; diff --git a/vendor/https/deno.land/std/io/bufio.ts b/vendor/https/deno.land/std/io/bufio.ts index 8a72323b..3ce22b80 100644 --- a/vendor/https/deno.land/std/io/bufio.ts +++ b/vendor/https/deno.land/std/io/bufio.ts @@ -1 +1 @@ -export * from "https://deno.land/std@v0.33.0/io/bufio.ts"; +export * from "https://deno.land/std@v0.34.0/io/bufio.ts"; diff --git a/vendor/https/deno.land/std/testing/asserts.ts b/vendor/https/deno.land/std/testing/asserts.ts index 0951deaa..c9be16f8 100644 --- a/vendor/https/deno.land/std/testing/asserts.ts +++ b/vendor/https/deno.land/std/testing/asserts.ts @@ -1 +1 @@ -export * from "https://deno.land/std@v0.33.0/testing/asserts.ts"; +export * from "https://deno.land/std@v0.34.0/testing/asserts.ts"; diff --git a/vendor/https/deno.land/std/util/async.ts b/vendor/https/deno.land/std/util/async.ts index 94775aea..2e567ee5 100644 --- a/vendor/https/deno.land/std/util/async.ts +++ b/vendor/https/deno.land/std/util/async.ts @@ -1 +1 @@ -export * from "https://deno.land/std@v0.33.0/util/async.ts"; +export * from "https://deno.land/std@v0.34.0/util/async.ts";