Skip to content

Commit

Permalink
v0.4.0 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored Sep 26, 2019
1 parent 31f3127 commit f236691
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
- checkout
- run:
name: Install Deno
command: curl -fsSL https://deno.land/x/install/install.sh | sh -s -- v0.17.0
command: curl -fsSL https://deno.land/x/install/install.sh | sh -s -- v0.19.0
- run:
name: Run Tests
command: |
export PATH=$HOME/.deno/bin:$PATH
deno -A test.ts
deno -A test
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
deno.d.ts
.idea
commands
tsconfig.json
commands
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
redis:
docker run -p 6379:6379 -d -t redis:5
test:
deno --allow-net test.ts
docker run -p 6379:6379 -d -t redis:5
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[![CircleCI](https://circleci.com/gh/keroxp/deno-redis.svg?style=svg)](https://circleci.com/gh/keroxp/deno-redis)
![https://img.shields.io/github/tag/keroxp/deno-redis.svg](https://img.shields.io/github/tag/keroxp/deno-redis.svg)
[![license](https://img.shields.io/github/license/keroxp/deno-redis.svg)](https://github.com/keroxp/deno-redis)
[![tag](https://img.shields.io/badge/deno__std-v0.17.0-green.svg)](https://github.com/denoland/deno_std)
[![tag](https://img.shields.io/badge/deno-v0.17.0-green.svg)](https://github.com/denoland/deno)
[![tag](https://img.shields.io/badge/deno__std-v0.18.0-green.svg)](https://github.com/denoland/deno_std)
[![tag](https://img.shields.io/badge/deno-v0.19.0-green.svg)](https://github.com/denoland/deno)

An experimental implementation of redis client for deno

Expand All @@ -16,7 +16,10 @@ needs `--allow-net` privilege

```ts
import { connect } from "https://denopkg.com/keroxp/deno-redis/redis.ts";
const redis = await connect("127.0.0.1:6379");
const redis = await connect({
hostname: "127.0.0.1",
port: 6379
});
const ok = await redis.set("hoge", "fuga");
const fuga = await redis.get("hoge");
```
Expand All @@ -39,7 +42,10 @@ const sub = await redis.subscribe("channel");
https://redis.io/topics/pipelining

```ts
const redis = await connect("127.0.0.1:6379");
const redis = await connect({
hostname: "127.0.0.1",
port: 6379
});
const pl = redis.pipeline();
await Promise.all([
pl.ping(),
Expand Down
11 changes: 11 additions & 0 deletions modules-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"https://deno.land/std": {
"version": "@v0.18.0",
"modules": [
"/testing/mod.ts",
"/testing/asserts.ts",
"/io/bufio.ts",
"/fmt/colors.ts"
]
}
}
9 changes: 7 additions & 2 deletions modules.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"https://deno.land/std": {
"version": "@v0.17.0",
"modules": ["/testing/mod.ts", "/testing/asserts.ts", "/io/bufio.ts"]
"version": "@v0.18.0",
"modules": [
"/testing/mod.ts",
"/testing/asserts.ts",
"/io/bufio.ts",
"/fmt/colors.ts"
]
}
}
4 changes: 2 additions & 2 deletions pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function createRedisPipeline(
reader: BufReader,
opts?: { tx: true }
): RedisPipeline {
let queue = [];
let queue: string[] = [];
const executor = {
enqueue(command: string, ...args) {
const msg = createRequest(command, ...args);
Expand All @@ -29,7 +29,7 @@ export function createRedisPipeline(
const msg = queue.join("");
await writer.write(encoder.encode(msg));
await writer.flush();
const ret = [];
const ret: RedisRawReply[] = [];
for (let i = 0; i < queue.length; i++) {
try {
const rep = await readReply(reader);
Expand Down
6 changes: 5 additions & 1 deletion pipeline_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { test } from "./vendor/https/deno.land/std/testing/mod.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { connect } from "./redis.ts";

const addr = "127.0.0.1:6379";
const addr = {
hostname: "127.0.0.1",
port: 6379
};

test(async function testPipeline() {
const redis = await connect(addr);
const pl = redis.pipeline();
Expand Down
2 changes: 1 addition & 1 deletion pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
import { createRequest, readArrayReply, sendCommand } from "./io.ts";
import { readArrayReply, sendCommand } from "./io.ts";

export type RedisSubscription = {
readonly isClosed: boolean;
Expand Down
5 changes: 4 additions & 1 deletion pubsub_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { connect } from "./redis.ts";
import { RedisPubSubMessage } from "./pubsub.ts";

const addr = "127.0.0.1:6379";
const addr = {
hostname: "127.0.0.1",
port: 6379
};

async function wait(duration) {
return new Promise(resolve => {
Expand Down
59 changes: 55 additions & 4 deletions redis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import DialOptions = Deno.DialOptions;

type Reader = Deno.Reader;
type Writer = Deno.Writer;
type Closer = Deno.Closer;
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";
Expand Down Expand Up @@ -445,12 +448,12 @@ class RedisImpl implements Redis, CommandExecutor {
get isClosed() {
return this._isClosed;
}

private executor: CommandExecutor;
constructor(
private closer: Closer,
private writer: BufWriter,
private reader: BufReader,
private executor?: CommandExecutor
executor?: CommandExecutor
) {
this.executor = executor || this;
}
Expand Down Expand Up @@ -1502,8 +1505,56 @@ class RedisImpl implements Redis, CommandExecutor {
}
}

export async function connect(addr: string): Promise<Redis> {
const conn = await Deno.dial("tcp", addr);
export type RedisConnectOptions = {
hostname: string;
port?: number | string;
tls?: boolean;
};

/**
* Connect to Redis server
* @param opts redis server's url http/https url with port number
* Examples:
* const conn = connect({hostname: "127.0.0.1", port: 6379})// -> tcp, 127.0.0.1:6379
* const conn = connect({hostname: "redis.proxy", port: 443, tls: true}) // -> TLS, redis.proxy:443
*/
export async function connect(
opts: string | RedisConnectOptions
): Promise<Redis> {
let conn: Deno.Conn;
if (typeof opts === "string") {
console.warn(
yellow(
"deno-redis: connect(addr) is now deprecated and will be removed in v0.5.0 (now v0.4.x)"
)
);
const [h, p] = opts.split(":");
if (!p) {
throw new Error("redis: port must be specified");
}
const dialOptions: DialOptions = { port: parseInt(p) };
if (h) {
dialOptions.hostname = h;
}
conn = await Deno.dial(dialOptions);
} else {
const { hostname } = opts;
const port = parseInt(`${opts.port}`);
if (!Number.isSafeInteger(port)) {
throw new Error("deno-redis: opts.port is invalid");
}
if (opts.tls) {
conn = await Deno.dialTLS({
hostname,
port
});
} else {
conn = await Deno.dial({
hostname,
port
});
}
}
return create(conn, conn, conn);
}

Expand Down
26 changes: 23 additions & 3 deletions redis_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { connect } from "./redis.ts";
import { test } from "./vendor/https/deno.land/std/testing/mod.ts";
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import { runIfMain, test } from "./vendor/https/deno.land/std/testing/mod.ts";
import {
assertEquals,
assertThrowsAsync
} from "./vendor/https/deno.land/std/testing/asserts.ts";
// can be substituted with env variable
const addr = "127.0.0.1:6379";
const addr = {
hostname: "127.0.0.1",
port: 6379
};

test(async function beforeAll() {
const redis = await connect(addr);
Expand Down Expand Up @@ -101,3 +107,17 @@ test(async function testDecrby() {
assertEquals(await redis.get("decryby"), "-101");
redis.close();
});

[Infinity, NaN, "", "port"].forEach(v => {
test(`invalid port: ${v}`, () => {
assertThrowsAsync(
async () => {
await connect({ hostname: "127.0.0.1", port: v });
},
Error,
"invalid"
);
});
});

runIfMain(import.meta);
5 changes: 0 additions & 5 deletions test.ts

This file was deleted.

14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"noResolve": true,
"baseUrl": "/Users/keroxp/Library/Caches",
"strictNullChecks": true,
"paths": {
"deno": ["./deno.d.ts"],
"https://*": ["./deno/deps/https/*"],
"http://*": ["./deno/deps/http/*"]
}
}
}
1 change: 1 addition & 0 deletions vendor/https/deno.land/std/fmt/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/fmt/colors.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/io/bufio.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/std@v0.17.0/io/bufio.ts";
export * from "https://deno.land/std@v0.18.0/io/bufio.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/testing/asserts.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/std@v0.17.0/testing/asserts.ts";
export * from "https://deno.land/std@v0.18.0/testing/asserts.ts";
2 changes: 1 addition & 1 deletion vendor/https/deno.land/std/testing/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "https://deno.land/std@v0.17.0/testing/mod.ts";
export * from "https://deno.land/std@v0.18.0/testing/mod.ts";

0 comments on commit f236691

Please sign in to comment.