Skip to content

Commit

Permalink
Merge pull request #168 from iuioiua/jsr
Browse files Browse the repository at this point in the history
feat: JSR support
  • Loading branch information
iuioiua authored Mar 20, 2024
2 parents f3e53bf + 4841eb3 commit 72f035e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# r2d2

[![Docs](https://doc.deno.land/badge.svg)](https://doc.deno.land/https://deno.land/x/r2d2/mod.ts)
[![Docs](https://doc.deno.land/badge.svg)](https://jsr.io/@iuioiua/r2d2)
[![CI](https://github.com/iuioiua/r2d2/actions/workflows/ci.yml/badge.svg)](https://github.com/iuioiua/r2d2/actions/workflows/ci.yml)
[![codecov](https://codecov.io/gh/iuioiua/r2d2/branch/main/graph/badge.svg?token=8IDAVSL014)](https://codecov.io/gh/iuioiua/r2d2)

Expand All @@ -20,12 +20,12 @@ Minimal [Redis](https://redis.io/) client for [Deno](https://deno.land/).
## Usage

Must be run with `--allow-net` permission. Check out the full documentation
[here](https://doc.deno.land/https://deno.land/x/r2d2/mod.ts).
[here](https://jsr.io/@iuioiua/r2d2).

### RESPv2

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -40,7 +40,7 @@ await redisClient.sendCommand(["GET", "hello"]);
If you don't care about the reply:

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -52,7 +52,7 @@ await redisClient.writeCommand(["SHUTDOWN"]);
### RESP3

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -73,7 +73,7 @@ Set the last argument, `raw`, to `true` and bulk string replies will return raw
data instead of strings.

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -90,7 +90,7 @@ await redisClient.sendCommand(["GET", "binary"], true);
### Pipelining

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -107,7 +107,7 @@ await redisClient.pipelineCommands([
### Pub/Sub

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -122,7 +122,7 @@ for await (const reply of redisClient.readReplies()) {
### Transactions

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -143,7 +143,7 @@ await redisClient.sendCommand(["EXEC"]);
### Eval Scripts

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -155,7 +155,7 @@ await redisClient.sendCommand(["EVAL", "return ARGV[1]", 0, "hello"]);
### Lua Scripts

```ts
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -174,11 +174,11 @@ await redisClient.sendCommand(["FCALL", "knockknock", 0]);
### Timeouts

For further details on `deadline()`, see the documentation
[here](https://deno.land/std/async/deadline.ts?s=deadline).
[here](https://jsr.io/@std/async/doc/~/deadline).

```ts
import { deadline } from "https://deno.land/std/async/deadline.ts";
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { deadline } from "jsr:@std/async";
import { RedisClient } from "jsr:@iuioiua/r2d2";

const redisConn = await Deno.connect({ port: 6379 });
const redisClient = new RedisClient(redisConn);
Expand All @@ -192,11 +192,11 @@ await deadline(redisClient.sendCommand(["SLOWLOG", "GET"]), 100);
### Retries

For further details on `retry()`, see the documentation
[here](https://deno.land/std/async/retry.ts?s=retry).
[here](https://jsr.io/@std/async/doc/~/retry).

```ts
import { retry } from "https://deno.land/std/async/retry.ts";
import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
import { retry } from "jsr:@std/async";
import { RedisClient } from "jsr:@iuioiua/r2d2";

// Retries to connect until successful using the exponential backoff algorithm.
const redisConn = await retry(async () => await Deno.connect({ port: 6379 }));
Expand Down
9 changes: 7 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"name": "@iuioiua/r2d2",
"version": "2.0.0",
"version": "2.1.0",
"exports": "./mod.ts",
"imports": {
"https://deno.land/x/r2d2/": "./"
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/bytes": "jsr:@std/bytes@^0.220.1",
"@std/collections": "jsr:@std/collections@^0.220.1",
"@std/fmt": "jsr:@std/fmt@^0.220.1",
"@std/io": "jsr:@std/io@^0.220.1",
"jsr:@iuioiua/r2d2/": "./"
},
"tasks": {
"redis:start": "redis-server --save \"\" --appendonly no --daemonize yes",
Expand Down
55 changes: 47 additions & 8 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
// deno-lint-ignore-file no-explicit-any
import { chunk } from "https://deno.land/[email protected]/collections/chunk.ts";
import { concat } from "https://deno.land/[email protected]/bytes/concat.ts";
import { writeAll } from "https://deno.land/[email protected]/io/write_all.ts";
import { readDelim } from "https://deno.land/[email protected]/io/read_delim.ts";
import { chunk } from "@std/collections/chunk";
import { concat } from "@std/bytes/concat";
import { readDelim } from "@std/io/read_delim";
import { writeAll } from "@std/io/write_all";

/**
* A Redis client that can be used to send commands to a Redis server.
*
* ```ts
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* // Returns "OK"
* await redisClient.sendCommand(["SET", "hello", "world"]);
*
* // Returns "world"
* await redisClient.sendCommand(["GET", "hello"]);
* ```
*
* @module
*/

/** Command sent to a Redis server. */
export type Command = (string | number | Uint8Array)[];
/** Reply received from a Redis server and triggered by a command. */
export type Reply =
| string
| number
Expand Down Expand Up @@ -320,10 +341,28 @@ class AsyncQueue {
}
}

/**
* A Redis client that can be used to send commands to a Redis server.
*
* @example
* ```ts
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
*
* // Returns "OK"
* await redisClient.sendCommand(["SET", "hello", "world"]);
*
* // Returns "world"
* await redisClient.sendCommand(["GET", "hello"]);
* ```
*/
export class RedisClient {
#conn: Deno.TcpConn | Deno.TlsConn;
#queue: AsyncQueue;

/** Constructs a new instance. */
constructor(conn: Deno.TcpConn | Deno.TlsConn) {
this.#conn = conn;
this.#queue = new AsyncQueue();
Expand All @@ -334,7 +373,7 @@ export class RedisClient {
*
* @example
* ```ts
* import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
Expand All @@ -357,7 +396,7 @@ export class RedisClient {
*
* @example
* ```ts
* import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
Expand All @@ -376,7 +415,7 @@ export class RedisClient {
*
* @example
* ```ts
* import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
Expand All @@ -398,7 +437,7 @@ export class RedisClient {
*
* @example
* ```ts
* import { RedisClient } from "https://deno.land/x/r2d2/mod.ts";
* import { RedisClient } from "jsr:@iuioiua/r2d2";
*
* const redisConn = await Deno.connect({ port: 6379 });
* const redisClient = new RedisClient(redisConn);
Expand Down
9 changes: 3 additions & 6 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { StringReader } from "https://deno.land/[email protected]/io/string_reader.ts";
import { readDelim } from "https://deno.land/[email protected]/io/read_delim.ts";
import { assertEquals, assertRejects } from "@std/assert";
import { StringReader } from "@std/io/string_reader";
import { readDelim } from "@std/io/read_delim";
import { type Command, readReply, RedisClient, type Reply } from "./mod.ts";

const encoder = new TextEncoder();
Expand Down

0 comments on commit 72f035e

Please sign in to comment.