effect-redis 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/redis.d.ts +21 -3
- package/package.json +31 -34
- package/dist/examples/stream-read-write.d.ts +0 -1
- package/dist/examples/stream.d.ts +0 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/redis.d.ts +0 -58
- package/src/index.ts +0 -1
- package/src/redis.ts +0 -370
package/dist/redis.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Effect, Layer } from 'effect';
|
|
2
|
-
import { createClient } from 'redis';
|
|
2
|
+
import { type RedisArgument, createClient } from 'redis';
|
|
3
3
|
declare const RedisError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
4
4
|
readonly _tag: "RedisError";
|
|
5
5
|
} & Readonly<A>;
|
|
@@ -21,7 +21,6 @@ interface RedisShape {
|
|
|
21
21
|
declare const Redis_base: Context.TagClass<Redis, "Redis", RedisShape>;
|
|
22
22
|
declare class Redis extends Redis_base {
|
|
23
23
|
}
|
|
24
|
-
declare const RedisLive: Layer.Layer<Redis, RedisError, RedisConnectionOptions>;
|
|
25
24
|
interface RedisPubSubShape {
|
|
26
25
|
publish: (channel: string, message: string) => Effect.Effect<void, RedisError, never>;
|
|
27
26
|
subscribe: (channel: string, handler: (message: string) => void) => Effect.Effect<void, RedisError, never>;
|
|
@@ -31,10 +30,29 @@ declare class RedisPubSub extends RedisPubSub_base {
|
|
|
31
30
|
}
|
|
32
31
|
interface RedisPersistenceShape {
|
|
33
32
|
setValue: (key: string, value: string) => Effect.Effect<void, RedisError, never>;
|
|
33
|
+
sAdd: (key: string, members: string[]) => Effect.Effect<void, RedisError, never>;
|
|
34
34
|
}
|
|
35
35
|
declare const RedisPersistence_base: Context.TagClass<RedisPersistence, "RedisPersistence", RedisPersistenceShape>;
|
|
36
36
|
declare class RedisPersistence extends RedisPersistence_base {
|
|
37
37
|
}
|
|
38
|
+
export interface StreamEntry {
|
|
39
|
+
id: RedisArgument;
|
|
40
|
+
message: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
interface RedisStreamShape {
|
|
43
|
+
xadd: (key: RedisArgument, id: RedisArgument | '*', message: Record<string, RedisArgument>) => Effect.Effect<string, RedisError, never>;
|
|
44
|
+
xread: (key: RedisArgument, id: RedisArgument, // Use '$' to read only new entries from now
|
|
45
|
+
block?: number, // Block in milliseconds, 0 for indefinite
|
|
46
|
+
count?: number) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
47
|
+
xrange: (key: RedisArgument, start: RedisArgument, // '-' for earliest available
|
|
48
|
+
end: RedisArgument, // '+' for latest available
|
|
49
|
+
count?: number) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
50
|
+
}
|
|
51
|
+
declare const RedisStream_base: Context.TagClass<RedisStream, "RedisStream", RedisStreamShape>;
|
|
52
|
+
declare class RedisStream extends RedisStream_base {
|
|
53
|
+
}
|
|
54
|
+
declare const RedisLive: Layer.Layer<Redis, RedisError, RedisConnectionOptions>;
|
|
38
55
|
declare const RedisPersistenceLive: Layer.Layer<RedisPersistence, RedisError, RedisConnectionOptions>;
|
|
39
56
|
declare const RedisPubSubLive: Layer.Layer<RedisPubSub, RedisError, RedisConnectionOptions>;
|
|
40
|
-
|
|
57
|
+
declare const RedisStreamLive: Layer.Layer<RedisStream, RedisError, RedisConnectionOptions>;
|
|
58
|
+
export { RedisPersistence, RedisPubSub, RedisConnectionOptions, Redis, RedisStream, RedisPersistenceLive, RedisPubSubLive, RedisConnectionOptionsLive, RedisLive, RedisStreamLive, };
|
package/package.json
CHANGED
|
@@ -1,34 +1,31 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "effect-redis",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Simple Effect wrapper for Redis.",
|
|
5
|
-
"module": "dist/index.js",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"type": "module",
|
|
9
|
-
"files": [
|
|
10
|
-
|
|
11
|
-
"src",
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
},
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"lint": "biome lint ."
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "effect-redis",
|
|
3
|
+
"version": "0.0.10",
|
|
4
|
+
"description": "Simple Effect wrapper for Redis.",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"files": ["dist", "README.md"],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build:main": "bun build --minify-syntax --minify-whitespace ./src/index.ts --outdir ./dist --target node --format esm",
|
|
12
|
+
"build:types": "bun tsc --emitDeclarationOnly --outDir dist",
|
|
13
|
+
"build": "bun run build:main && bun run build:types",
|
|
14
|
+
"prepublishOnly": "bun run build",
|
|
15
|
+
"test": "bun test",
|
|
16
|
+
"format": "biome format --write ./src",
|
|
17
|
+
"lint": "biome lint ."
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@biomejs/biome": "^1.9.4",
|
|
21
|
+
"@types/bun": "latest"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"effect": "^3.16.2",
|
|
25
|
+
"redis": "^5.1.0",
|
|
26
|
+
"typescript": "^5"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@effect/platform-bun": "^0.69.2"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/src/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './redis';
|
package/dist/src/redis.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Context, Effect, Layer } from 'effect';
|
|
2
|
-
import { type RedisArgument, createClient } from 'redis';
|
|
3
|
-
declare const RedisError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").Equals<A, {}> extends true ? void : { readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }) => import("effect/Cause").YieldableError & {
|
|
4
|
-
readonly _tag: "RedisError";
|
|
5
|
-
} & Readonly<A>;
|
|
6
|
-
export declare class RedisError extends RedisError_base<{
|
|
7
|
-
cause: unknown;
|
|
8
|
-
message: string;
|
|
9
|
-
}> {
|
|
10
|
-
}
|
|
11
|
-
interface RedisConnectionOptionsShape {
|
|
12
|
-
options?: Parameters<typeof createClient>[0];
|
|
13
|
-
}
|
|
14
|
-
declare const RedisConnectionOptions_base: Context.TagClass<RedisConnectionOptions, "RedisConnectionOptions", RedisConnectionOptionsShape>;
|
|
15
|
-
declare class RedisConnectionOptions extends RedisConnectionOptions_base {
|
|
16
|
-
}
|
|
17
|
-
declare const RedisConnectionOptionsLive: (options?: Parameters<typeof createClient>[0]) => Layer.Layer<RedisConnectionOptions, never, never>;
|
|
18
|
-
interface RedisShape {
|
|
19
|
-
use: <T>(fn: (client: ReturnType<typeof createClient>) => T) => Effect.Effect<Awaited<T>, RedisError, never>;
|
|
20
|
-
}
|
|
21
|
-
declare const Redis_base: Context.TagClass<Redis, "Redis", RedisShape>;
|
|
22
|
-
declare class Redis extends Redis_base {
|
|
23
|
-
}
|
|
24
|
-
interface RedisPubSubShape {
|
|
25
|
-
publish: (channel: string, message: string) => Effect.Effect<void, RedisError, never>;
|
|
26
|
-
subscribe: (channel: string, handler: (message: string) => void) => Effect.Effect<void, RedisError, never>;
|
|
27
|
-
}
|
|
28
|
-
declare const RedisPubSub_base: Context.TagClass<RedisPubSub, "RedisPubSub", RedisPubSubShape>;
|
|
29
|
-
declare class RedisPubSub extends RedisPubSub_base {
|
|
30
|
-
}
|
|
31
|
-
interface RedisPersistenceShape {
|
|
32
|
-
setValue: (key: string, value: string) => Effect.Effect<void, RedisError, never>;
|
|
33
|
-
sAdd: (key: string, members: string[]) => Effect.Effect<void, RedisError, never>;
|
|
34
|
-
}
|
|
35
|
-
declare const RedisPersistence_base: Context.TagClass<RedisPersistence, "RedisPersistence", RedisPersistenceShape>;
|
|
36
|
-
declare class RedisPersistence extends RedisPersistence_base {
|
|
37
|
-
}
|
|
38
|
-
export interface StreamEntry {
|
|
39
|
-
id: RedisArgument;
|
|
40
|
-
message: Record<string, string>;
|
|
41
|
-
}
|
|
42
|
-
interface RedisStreamShape {
|
|
43
|
-
xadd: (key: RedisArgument, id: RedisArgument | '*', message: Record<string, RedisArgument>) => Effect.Effect<string, RedisError, never>;
|
|
44
|
-
xread: (key: RedisArgument, id: RedisArgument, // Use '$' to read only new entries from now
|
|
45
|
-
block?: number, // Block in milliseconds, 0 for indefinite
|
|
46
|
-
count?: number) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
47
|
-
xrange: (key: RedisArgument, start: RedisArgument, // '-' for earliest available
|
|
48
|
-
end: RedisArgument, // '+' for latest available
|
|
49
|
-
count?: number) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
50
|
-
}
|
|
51
|
-
declare const RedisStream_base: Context.TagClass<RedisStream, "RedisStream", RedisStreamShape>;
|
|
52
|
-
declare class RedisStream extends RedisStream_base {
|
|
53
|
-
}
|
|
54
|
-
declare const RedisLive: Layer.Layer<Redis, RedisError, RedisConnectionOptions>;
|
|
55
|
-
declare const RedisPersistenceLive: Layer.Layer<RedisPersistence, RedisError, RedisConnectionOptions>;
|
|
56
|
-
declare const RedisPubSubLive: Layer.Layer<RedisPubSub, RedisError, RedisConnectionOptions>;
|
|
57
|
-
declare const RedisStreamLive: Layer.Layer<RedisStream, RedisError, RedisConnectionOptions>;
|
|
58
|
-
export { RedisPersistence, RedisPubSub, RedisConnectionOptions, Redis, RedisStream, RedisPersistenceLive, RedisPubSubLive, RedisConnectionOptionsLive, RedisLive, RedisStreamLive, };
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './redis';
|
package/src/redis.ts
DELETED
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
import { Context, Data, Effect, Layer, type Scope } from 'effect';
|
|
2
|
-
import { type RedisArgument, createClient } from 'redis';
|
|
3
|
-
|
|
4
|
-
export class RedisError extends Data.TaggedError('RedisError')<{
|
|
5
|
-
cause: unknown;
|
|
6
|
-
message: string;
|
|
7
|
-
}> {}
|
|
8
|
-
|
|
9
|
-
interface RedisConnectionOptionsShape {
|
|
10
|
-
options?: Parameters<typeof createClient>[0];
|
|
11
|
-
}
|
|
12
|
-
class RedisConnectionOptions extends Context.Tag('RedisConnectionOptions')<
|
|
13
|
-
RedisConnectionOptions,
|
|
14
|
-
RedisConnectionOptionsShape
|
|
15
|
-
>() {}
|
|
16
|
-
|
|
17
|
-
const RedisConnectionOptionsLive = (
|
|
18
|
-
options?: Parameters<typeof createClient>[0],
|
|
19
|
-
) =>
|
|
20
|
-
Layer.succeed(
|
|
21
|
-
RedisConnectionOptions,
|
|
22
|
-
RedisConnectionOptions.of({
|
|
23
|
-
options,
|
|
24
|
-
}),
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
interface RedisShape {
|
|
28
|
-
use: <T>(
|
|
29
|
-
fn: (client: ReturnType<typeof createClient>) => T,
|
|
30
|
-
) => Effect.Effect<Awaited<T>, RedisError, never>;
|
|
31
|
-
}
|
|
32
|
-
class Redis extends Context.Tag('Redis')<Redis, RedisShape>() {}
|
|
33
|
-
|
|
34
|
-
interface RedisPubSubShape {
|
|
35
|
-
publish: (
|
|
36
|
-
channel: string,
|
|
37
|
-
message: string,
|
|
38
|
-
) => Effect.Effect<void, RedisError, never>;
|
|
39
|
-
subscribe: (
|
|
40
|
-
channel: string,
|
|
41
|
-
handler: (message: string) => void,
|
|
42
|
-
) => Effect.Effect<void, RedisError, never>;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
class RedisPubSub extends Context.Tag('RedisPubSub')<
|
|
46
|
-
RedisPubSub,
|
|
47
|
-
RedisPubSubShape
|
|
48
|
-
>() {}
|
|
49
|
-
|
|
50
|
-
interface RedisPersistenceShape {
|
|
51
|
-
setValue: (
|
|
52
|
-
key: string,
|
|
53
|
-
value: string,
|
|
54
|
-
) => Effect.Effect<void, RedisError, never>;
|
|
55
|
-
sAdd: (
|
|
56
|
-
key: string,
|
|
57
|
-
members: string[],
|
|
58
|
-
) => Effect.Effect<void, RedisError, never>;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
class RedisPersistence extends Context.Tag('RedisPersistence')<
|
|
62
|
-
RedisPersistence,
|
|
63
|
-
RedisPersistenceShape
|
|
64
|
-
>() {}
|
|
65
|
-
|
|
66
|
-
// Stream related types
|
|
67
|
-
export interface StreamEntry {
|
|
68
|
-
id: RedisArgument;
|
|
69
|
-
message: Record<string, string>;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
interface RedisStreamShape {
|
|
73
|
-
// Add an entry to a stream
|
|
74
|
-
xadd: (
|
|
75
|
-
key: RedisArgument,
|
|
76
|
-
id: RedisArgument | '*',
|
|
77
|
-
message: Record<string, RedisArgument>,
|
|
78
|
-
) => Effect.Effect<string, RedisError, never>;
|
|
79
|
-
|
|
80
|
-
// Read from a stream with ability to block and wait for new entries
|
|
81
|
-
xread: (
|
|
82
|
-
key: RedisArgument,
|
|
83
|
-
id: RedisArgument, // Use '$' to read only new entries from now
|
|
84
|
-
block?: number, // Block in milliseconds, 0 for indefinite
|
|
85
|
-
count?: number, // Max number of entries to return
|
|
86
|
-
) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
87
|
-
|
|
88
|
-
// Read a range of entries from a stream
|
|
89
|
-
xrange: (
|
|
90
|
-
key: RedisArgument,
|
|
91
|
-
start: RedisArgument, // '-' for earliest available
|
|
92
|
-
end: RedisArgument, // '+' for latest available
|
|
93
|
-
count?: number,
|
|
94
|
-
) => Effect.Effect<StreamEntry[], RedisError, never>;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
class RedisStream extends Context.Tag('RedisStream')<
|
|
98
|
-
RedisStream,
|
|
99
|
-
RedisStreamShape
|
|
100
|
-
>() {}
|
|
101
|
-
|
|
102
|
-
// Common code for redis client creation
|
|
103
|
-
const redisClientEffect: Effect.Effect<
|
|
104
|
-
ReturnType<typeof createClient>,
|
|
105
|
-
RedisError,
|
|
106
|
-
Scope.Scope | RedisConnectionOptions
|
|
107
|
-
> = Effect.gen(function* () {
|
|
108
|
-
const { options } = yield* RedisConnectionOptions;
|
|
109
|
-
|
|
110
|
-
return yield* Effect.acquireRelease(
|
|
111
|
-
Effect.tryPromise({
|
|
112
|
-
try: () =>
|
|
113
|
-
createClient(options)
|
|
114
|
-
.connect()
|
|
115
|
-
.then((r) => {
|
|
116
|
-
console.log('Connected to Redis');
|
|
117
|
-
r.on('error', (e) => {
|
|
118
|
-
console.log('Redis error(on error):', e.message);
|
|
119
|
-
r.destroy();
|
|
120
|
-
});
|
|
121
|
-
r.on('end', () => {
|
|
122
|
-
console.log('Connection to Redis ended');
|
|
123
|
-
});
|
|
124
|
-
return r;
|
|
125
|
-
}),
|
|
126
|
-
catch: (e) =>
|
|
127
|
-
new RedisError({
|
|
128
|
-
cause: e,
|
|
129
|
-
message: 'Error while connecting to Redis',
|
|
130
|
-
}),
|
|
131
|
-
}),
|
|
132
|
-
(client) =>
|
|
133
|
-
Effect.sync(() => {
|
|
134
|
-
if (client.isReady) {
|
|
135
|
-
client.quit();
|
|
136
|
-
}
|
|
137
|
-
}),
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
const bootstrapRedisServiceEffect = Effect.gen(function* () {
|
|
142
|
-
const client: ReturnType<typeof createClient> = yield* redisClientEffect;
|
|
143
|
-
return Redis.of({
|
|
144
|
-
use: (fn) =>
|
|
145
|
-
Effect.gen(function* () {
|
|
146
|
-
const result = yield* Effect.try({
|
|
147
|
-
try: () => fn(client),
|
|
148
|
-
catch: (e) =>
|
|
149
|
-
new RedisError({
|
|
150
|
-
cause: e,
|
|
151
|
-
message: 'Synchronous error in `Redis.use`',
|
|
152
|
-
}),
|
|
153
|
-
});
|
|
154
|
-
if (result instanceof Promise) {
|
|
155
|
-
return yield* Effect.tryPromise({
|
|
156
|
-
try: () => result,
|
|
157
|
-
catch: (e) =>
|
|
158
|
-
new RedisError({
|
|
159
|
-
cause: e,
|
|
160
|
-
message: 'Asynchronous error in `Redis.use`',
|
|
161
|
-
}),
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
return result;
|
|
165
|
-
}),
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const RedisLive = Layer.scoped(Redis, bootstrapRedisServiceEffect);
|
|
170
|
-
|
|
171
|
-
const bootstrapRedisPersistenceServiceEffect = Effect.gen(function* () {
|
|
172
|
-
const client: ReturnType<typeof createClient> = yield* redisClientEffect;
|
|
173
|
-
|
|
174
|
-
return RedisPersistence.of({
|
|
175
|
-
setValue: (key, value) =>
|
|
176
|
-
Effect.tryPromise({
|
|
177
|
-
try: () => client.set(key, value),
|
|
178
|
-
catch: (e) =>
|
|
179
|
-
new RedisError({
|
|
180
|
-
cause: e,
|
|
181
|
-
message: 'Error in `Redis.setValue`',
|
|
182
|
-
}),
|
|
183
|
-
}),
|
|
184
|
-
sAdd: (key, members) =>
|
|
185
|
-
Effect.tryPromise({
|
|
186
|
-
try: () => client.sAdd(key, members),
|
|
187
|
-
catch: (e) =>
|
|
188
|
-
new RedisError({
|
|
189
|
-
cause: e,
|
|
190
|
-
message: 'Error in `Redis.sAdd`',
|
|
191
|
-
}),
|
|
192
|
-
}),
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
const RedisPersistenceLive = Layer.scoped(
|
|
197
|
-
RedisPersistence,
|
|
198
|
-
bootstrapRedisPersistenceServiceEffect,
|
|
199
|
-
);
|
|
200
|
-
|
|
201
|
-
const bootstrapRedisPubSubServiceEffect = Effect.gen(function* () {
|
|
202
|
-
const clientPublish: ReturnType<typeof createClient> =
|
|
203
|
-
yield* redisClientEffect;
|
|
204
|
-
const clientSubscribe: ReturnType<typeof createClient> =
|
|
205
|
-
yield* redisClientEffect;
|
|
206
|
-
|
|
207
|
-
return RedisPubSub.of({
|
|
208
|
-
publish: (channel, message) =>
|
|
209
|
-
Effect.tryPromise({
|
|
210
|
-
try: () => clientPublish.publish(channel, message),
|
|
211
|
-
catch: (e) =>
|
|
212
|
-
new RedisError({
|
|
213
|
-
cause: e,
|
|
214
|
-
message: 'Error in `Redis.publish`',
|
|
215
|
-
}),
|
|
216
|
-
}),
|
|
217
|
-
subscribe: (channel, handler) =>
|
|
218
|
-
Effect.tryPromise({
|
|
219
|
-
try: () => clientSubscribe.subscribe(channel, handler),
|
|
220
|
-
catch: (e) =>
|
|
221
|
-
new RedisError({
|
|
222
|
-
cause: e,
|
|
223
|
-
message: 'Error in `Redis.subscribe`',
|
|
224
|
-
}),
|
|
225
|
-
}),
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
const RedisPubSubLive = Layer.scoped(
|
|
230
|
-
RedisPubSub,
|
|
231
|
-
bootstrapRedisPubSubServiceEffect,
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
// Redis Stream implementation
|
|
235
|
-
const bootstrapRedisStreamServiceEffect = Effect.gen(function* () {
|
|
236
|
-
// Separate connections: one dedicated to writes (producer) and one to reads (consumer)
|
|
237
|
-
const clientProducer = yield* redisClientEffect;
|
|
238
|
-
const clientConsumer = yield* redisClientEffect;
|
|
239
|
-
|
|
240
|
-
return RedisStream.of({
|
|
241
|
-
xadd: (
|
|
242
|
-
key: RedisArgument,
|
|
243
|
-
id: RedisArgument,
|
|
244
|
-
message: Record<string, RedisArgument>,
|
|
245
|
-
) =>
|
|
246
|
-
Effect.tryPromise({
|
|
247
|
-
try: async () => {
|
|
248
|
-
// Pass the message object directly to xAdd
|
|
249
|
-
return await clientProducer.xAdd(key, id, message);
|
|
250
|
-
},
|
|
251
|
-
catch: (e) =>
|
|
252
|
-
new RedisError({
|
|
253
|
-
cause: e,
|
|
254
|
-
message: 'Error in `RedisStream.xadd`',
|
|
255
|
-
}),
|
|
256
|
-
}),
|
|
257
|
-
|
|
258
|
-
xread: (
|
|
259
|
-
key: RedisArgument,
|
|
260
|
-
id: RedisArgument,
|
|
261
|
-
block?: number,
|
|
262
|
-
count?: number,
|
|
263
|
-
) =>
|
|
264
|
-
Effect.tryPromise({
|
|
265
|
-
try: async () => {
|
|
266
|
-
const options: Record<string, number> = {};
|
|
267
|
-
|
|
268
|
-
if (block !== undefined) {
|
|
269
|
-
options.BLOCK = block;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (count !== undefined) {
|
|
273
|
-
options.COUNT = count;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// Create proper XReadStream objects instead of arrays
|
|
277
|
-
const streams = [{ key, id }];
|
|
278
|
-
const result = await clientConsumer.xRead(streams, options);
|
|
279
|
-
|
|
280
|
-
if (!result) {
|
|
281
|
-
return [];
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
// Transform result into StreamEntry[] format
|
|
285
|
-
if (!Array.isArray(result)) {
|
|
286
|
-
return [];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return result.flatMap((stream) => {
|
|
290
|
-
// Type guard to check if stream has the expected structure
|
|
291
|
-
if (
|
|
292
|
-
stream &&
|
|
293
|
-
typeof stream === 'object' &&
|
|
294
|
-
'messages' in stream &&
|
|
295
|
-
Array.isArray(stream.messages)
|
|
296
|
-
) {
|
|
297
|
-
return stream.messages.map((msg) => {
|
|
298
|
-
// Add type guard for msg
|
|
299
|
-
if (msg && typeof msg === 'object' && 'id' in msg) {
|
|
300
|
-
return {
|
|
301
|
-
id: String(msg.id), // Convert to string explicitly
|
|
302
|
-
message: msg.message as Record<string, string>,
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
// Return a default or handle error case
|
|
306
|
-
return {
|
|
307
|
-
id: '',
|
|
308
|
-
message: {} as Record<string, string>,
|
|
309
|
-
};
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return [];
|
|
314
|
-
});
|
|
315
|
-
},
|
|
316
|
-
catch: (e) =>
|
|
317
|
-
new RedisError({
|
|
318
|
-
cause: e,
|
|
319
|
-
message: 'Error in `RedisStream.xread`',
|
|
320
|
-
}),
|
|
321
|
-
}),
|
|
322
|
-
|
|
323
|
-
xrange: (
|
|
324
|
-
key: RedisArgument,
|
|
325
|
-
start: RedisArgument,
|
|
326
|
-
end: RedisArgument,
|
|
327
|
-
count?: number,
|
|
328
|
-
) =>
|
|
329
|
-
Effect.tryPromise({
|
|
330
|
-
try: async () => {
|
|
331
|
-
const options: Record<string, number> = {};
|
|
332
|
-
|
|
333
|
-
if (count !== undefined) {
|
|
334
|
-
options.COUNT = count;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const result = await clientConsumer.xRange(key, start, end, options);
|
|
338
|
-
|
|
339
|
-
// Transform result into StreamEntry[] format
|
|
340
|
-
return result.map((msg) => ({
|
|
341
|
-
id: msg.id,
|
|
342
|
-
message: msg.message as Record<string, string>,
|
|
343
|
-
}));
|
|
344
|
-
},
|
|
345
|
-
catch: (e) =>
|
|
346
|
-
new RedisError({
|
|
347
|
-
cause: e,
|
|
348
|
-
message: 'Error in `RedisStream.xrange`',
|
|
349
|
-
}),
|
|
350
|
-
}),
|
|
351
|
-
});
|
|
352
|
-
});
|
|
353
|
-
|
|
354
|
-
const RedisStreamLive = Layer.scoped(
|
|
355
|
-
RedisStream,
|
|
356
|
-
bootstrapRedisStreamServiceEffect,
|
|
357
|
-
);
|
|
358
|
-
|
|
359
|
-
export {
|
|
360
|
-
RedisPersistence,
|
|
361
|
-
RedisPubSub,
|
|
362
|
-
RedisConnectionOptions,
|
|
363
|
-
Redis,
|
|
364
|
-
RedisStream,
|
|
365
|
-
RedisPersistenceLive,
|
|
366
|
-
RedisPubSubLive,
|
|
367
|
-
RedisConnectionOptionsLive,
|
|
368
|
-
RedisLive,
|
|
369
|
-
RedisStreamLive,
|
|
370
|
-
};
|