Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions connection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import type { Backoff } from "./backoff.ts";
import type { ConnectionEventMap } from "./events.ts";
import type { ErrorReplyError } from "./errors.ts";
import type {
DefaultPubSubMessageType,
PubSubMessageType,
RedisSubscription,
} from "./subscription.ts";
import type { TypedEventTarget } from "./internal/typed_event_target.ts";
import type {
kUnstableCreateProtocol,
kUnstableCreateSubscription,
kUnstablePipeline,
kUnstableProtover,
kUnstableReadReply,
Expand Down Expand Up @@ -40,6 +46,12 @@ export interface Connection extends TypedEventTarget<ConnectionEventMap> {
args?: Array<RedisValue>,
options?: SendCommandOptions,
): Promise<RedisReply>;
/**
* @private
*/
[kUnstableCreateSubscription]<
TMessage extends PubSubMessageType = DefaultPubSubMessageType,
>(): RedisSubscription<TMessage>;
/**
* @private
*/
Expand Down
8 changes: 4 additions & 4 deletions default_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
SubscribeCommand,
} from "./subscription.ts";
import type { Connection, SendCommandOptions } from "./connection.ts";
import { DefaultRedisSubscription } from "./default_subscription.ts";
import { kUnstableCreateSubscription } from "./internal/symbols.ts";
import type { RedisReply, RedisValue } from "./protocol/shared/types.ts";

export function createDefaultClient(connection: Connection): Client {
Expand Down Expand Up @@ -37,9 +37,9 @@ class DefaultClient implements Client {
command: SubscribeCommand,
...channelsOrPatterns: Array<string>
): Promise<RedisSubscription<TMessage>> {
const subscription = new DefaultRedisSubscription<TMessage>(
this.connection,
);
const subscription = this.connection[kUnstableCreateSubscription]<
TMessage
>();
switch (command) {
case "SUBSCRIBE":
await subscription.subscribe(...channelsOrPatterns);
Expand Down
29 changes: 25 additions & 4 deletions default_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import type {
RedisConnectionOptions,
SendCommandOptions,
} from "./connection.ts";
import { DefaultRedisSubscription } from "./default_subscription.ts";
import {
ErrorReplyError,
InvalidStateError,
isRetriableError,
} from "./errors.ts";
import type { ConnectionEventMap } from "./events.ts";
import type {
DefaultPubSubMessageType,
PubSubMessageType,
RedisSubscription,
} from "./subscription.ts";
import {
kUnstableCreateProtocol,
kUnstableCreateSubscription,
kUnstablePipeline,
kUnstableProtover,
kUnstableReadReply,
Expand All @@ -27,7 +34,11 @@ import {
import { kEmptyRedisArgs } from "./protocol/shared/command.ts";
import type { Command, Protocol } from "./protocol/shared/protocol.ts";
import { Protocol as DenoStreamsProtocol } from "./protocol/deno_streams/mod.ts";
import type { RedisReply, RedisValue } from "./protocol/shared/types.ts";
import type {
Protover,
RedisReply,
RedisValue,
} from "./protocol/shared/types.ts";
import { delay } from "./deps/std/async.ts";

export function createRedisConnection(
Expand Down Expand Up @@ -58,6 +69,8 @@ class RedisConnection
private commandQueue: PendingCommand[] = [];
#conn!: Deno.Conn;
#protocol!: Protocol;
/** @default {2} */
#protover?: Protover;
#eventTarget = createTypedEventTarget<ConnectionEventMap>();
#connectingPromise?: PromiseWithResolvers<void>;

Expand Down Expand Up @@ -191,6 +204,12 @@ class RedisConnection
);
}

[kUnstableCreateSubscription]<
TMessage extends PubSubMessageType = DefaultPubSubMessageType,
>(): RedisSubscription<TMessage> {
return new DefaultRedisSubscription<TMessage>(this);
}

[kUnstableReadReply](returnsUint8Arrays?: boolean): Promise<RedisReply> {
return this.#protocol.readReply(returnsUint8Arrays);
}
Expand Down Expand Up @@ -292,9 +311,11 @@ class RedisConnection
await this.authenticate(this.options.username, this.options.password);
}
if (this.options[kUnstableProtover] != null) {
await this.#sendCommandImmediately("HELLO", [
this.options[kUnstableProtover],
]);
const protover = this.options[kUnstableProtover];
await this.#sendCommandImmediately("HELLO", [protover]);
if (protover !== 2) {
this.#protover = protover;
}
}
if (this.options.db) {
await this.selectDb(this.options.db);
Expand Down
7 changes: 7 additions & 0 deletions internal/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ export const kUnstableProtover = Symbol("deno-redis.protover");
* @private
*/
export const kUnstableStartReadLoop = Symbol("deno-redis.startReadLoop");

/**
* @private
*/
export const kUnstableCreateSubscription = Symbol(
"deno-redis.createSubscription",
);
2 changes: 2 additions & 0 deletions pool/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from "../subscription.ts";
import { createDefaultClient } from "../default_client.ts";
import {
kUnstableCreateSubscription,
kUnstablePipeline,
kUnstableReadReply,
kUnstableStartReadLoop,
Expand Down Expand Up @@ -100,6 +101,7 @@ function createPoolConnection(
"addEventListener",
"removeEventListener",
Symbol.dispose,
kUnstableCreateSubscription,
kUnstableReadReply,
kUnstableWriteCommand,
kUnstablePipeline,
Expand Down
16 changes: 14 additions & 2 deletions protocol/deno_streams/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BufReader, BufWriter } from "../../deps/std/io.ts";
import { readReply } from "./reply.ts";
import { readOrEmitReply, readReply } from "./reply.ts";
import { sendCommand, sendCommands, writeCommand } from "./command.ts";

import type { Command, Protocol as BaseProtocol } from "../shared/protocol.ts";
import type { RedisReply, RedisValue } from "../shared/types.ts";
import type {
ProtocolEvents,
RedisReply,
RedisValue,
} from "../shared/types.ts";
import type { ErrorReplyError } from "../../errors.ts";
import type { TypedEventTarget } from "../../internal/typed_event_target.ts";

export class Protocol implements BaseProtocol {
#reader: BufReader;
Expand Down Expand Up @@ -33,6 +38,13 @@ export class Protocol implements BaseProtocol {
return readReply(this.#reader, returnsUint8Arrays);
}

readOrEmitReply(
eventTarget: TypedEventTarget<ProtocolEvents>,
returnsUint8Arrays?: boolean,
): Promise<RedisReply> {
return readOrEmitReply(this.#reader, eventTarget, returnsUint8Arrays);
}

async writeCommand(command: Command): Promise<void> {
await writeCommand(this.#writer, command.command, command.args);
await this.#writer.flush();
Expand Down
35 changes: 30 additions & 5 deletions protocol/deno_streams/reply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BufReader } from "../../deps/std/io.ts";
import type * as types from "../shared/types.ts";
import type { ProtocolEvents } from "../shared/types.ts";
import {
ArrayReplyCode,
AttributeReplyCode,
Expand All @@ -19,17 +20,32 @@ import {
} from "../shared/reply.ts";
import { EOFError, ErrorReplyError, InvalidStateError } from "../../errors.ts";
import { decoder } from "../../internal/encoding.ts";
import type { TypedEventTarget } from "../../internal/typed_event_target.ts";
import { dispatchEvent } from "../../internal/typed_event_target.ts";

export async function readReply(
export async function readOrEmitReply(
reader: BufReader,
eventTarget: TypedEventTarget<ProtocolEvents>,
returnUint8Arrays?: boolean,
): Promise<types.RedisReply> {
const res = await reader.peek(1);
if (res == null) {
throw new EOFError();
const code = await peekReplyCode(reader);
if (code === ErrorReplyCode) {
await readErrorReplyOrFail(reader);
}

const code = res[0];
if (code === PushReplyCode) {
const reply = await readPushReply(reader, returnUint8Arrays);
dispatchEvent(eventTarget, "push", reply ?? []);
return readOrEmitReply(reader, eventTarget, returnUint8Arrays);
}
return readReply(reader, returnUint8Arrays);
}

export async function readReply(
reader: BufReader,
returnUint8Arrays?: boolean,
): Promise<types.RedisReply> {
const code = await peekReplyCode(reader);
if (code === ErrorReplyCode) {
await readErrorReplyOrFail(reader);
}
Expand Down Expand Up @@ -73,6 +89,15 @@ export async function readReply(
}
}

async function peekReplyCode(reader: BufReader): Promise<number> {
const res = await reader.peek(1);
if (res == null) {
throw new EOFError();
}
const code = res[0];
return code;
}

async function readIntegerReply(
reader: BufReader,
): Promise<number> {
Expand Down
6 changes: 6 additions & 0 deletions protocol/shared/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { RedisReply, RedisValue } from "./types.ts";
import type { ErrorReplyError } from "../../errors.ts";
import type { TypedEventTarget } from "../../internal/typed_event_target.ts";
import type { ProtocolEvents } from "./types.ts";

export interface Command {
command: string;
Expand All @@ -14,6 +16,10 @@ export interface Protocol {
returnsUint8Arrays?: boolean,
): Promise<RedisReply>;
readReply(returnsUint8Array?: boolean): Promise<RedisReply>;
readOrEmitReply(
eventTarget: TypedEventTarget<ProtocolEvents>,
returnsUint8Array?: boolean,
): Promise<RedisReply>;
writeCommand(command: Command): Promise<void>;
pipeline(
commands: Array<Command>,
Expand Down
16 changes: 16 additions & 0 deletions protocol/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ export type RawOrError = Raw | ErrorReplyError;
export const okReply = "OK";

export type Protover = 2 | 3;

export type ProtocolEvents = {
push: Array<RedisReply>;
};

/**
* TODO: This is a dirty hack to distinguish push replies from regular array replies.
*/
class PushReply extends Array<RedisReply> {}

export function newPushReply(size: number): Array<RedisReply> {
return new PushReply(size);
}
export function isPushReply(reply: RedisReply): reply is Array<RedisReply> {
return reply instanceof PushReply;
}
38 changes: 38 additions & 0 deletions protocol/shared/types_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { RedisReply } from "./types.ts";
import { isPushReply, newPushReply } from "./types.ts";
import { assertEquals } from "../../deps/std/assert.ts";

Deno.test({
name: "isPushReply",
permissions: "none",
fn: async (t) => {
await t.step("returns false for a regular array", () => {
const given: Array<RedisReply> = [];
const expected = false;
const actual = isPushReply(given);
assertEquals(actual, expected);
});

await t.step("returns true for an array created by newPushReply", () => {
const given = newPushReply(0);
const expected = true;
const actual = isPushReply(given);
assertEquals(actual, expected);
});
},
});

Deno.test({
name: "newPushReply",
permissions: "none",
fn: async (t) => {
await t.step("works just like a regular array", () => {
const subject = newPushReply(2);
assertEquals(subject.length, 2);
subject[0] = 1;
subject[1] = 2;
subject.push(3);
assertEquals([...subject], [1, 2, 3]);
});
},
});
16 changes: 14 additions & 2 deletions protocol/web_streams/mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { sendCommand, sendCommands, writeCommand } from "./command.ts";
import { readReply } from "./reply.ts";
import { readOrEmitReply, readReply } from "./reply.ts";
import type { Command, Protocol as BaseProtocol } from "../shared/protocol.ts";
import type { RedisReply, RedisValue } from "../shared/types.ts";
import type {
ProtocolEvents,
RedisReply,
RedisValue,
} from "../shared/types.ts";
import type { ErrorReplyError } from "../../errors.ts";
import { BufferedReadableStream } from "../../internal/buffered_readable_stream.ts";
import type { TypedEventTarget } from "../../internal/typed_event_target.ts";

export class Protocol implements BaseProtocol {
#readable: BufferedReadableStream;
Expand All @@ -30,6 +35,13 @@ export class Protocol implements BaseProtocol {
return readReply(this.#readable, returnsUint8Arrays);
}

readOrEmitReply(
eventTarget: TypedEventTarget<ProtocolEvents>,
returnsUint8Arrays?: boolean,
): Promise<RedisReply> {
return readOrEmitReply(this.#readable, eventTarget, returnsUint8Arrays);
}

writeCommand(command: Command): Promise<void> {
return writeCommand(this.#writable, command.command, command.args);
}
Expand Down
Loading