@appweaver/redis 2.4.1 → 2.6.0
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/config.d.ts +3 -0
- package/config.js +5 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/module.d.ts +1 -1
- package/module.js +8 -3
- package/package.json +3 -3
- package/redis-pubsub.d.ts +12 -0
- package/redis-pubsub.js +101 -0
package/config.d.ts
CHANGED
package/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.redisConfig = void 0;
|
|
3
|
+
exports.redisPubSubConfig = exports.redisConfig = void 0;
|
|
4
4
|
const typebox_1 = require("@sinclair/typebox");
|
|
5
5
|
const common_1 = require("@appweaver/common");
|
|
6
6
|
exports.redisConfig = (0, common_1.defineConfig)('memory.redis', {
|
|
@@ -13,3 +13,7 @@ exports.redisConfig = (0, common_1.defineConfig)('memory.redis', {
|
|
|
13
13
|
/** Milliseconds the open circuit breaker skips Redis before probing it. Default: `10000` (10 sec). */
|
|
14
14
|
CIRCUIT_BREAKER_COOLDOWN: typebox_1.Type.Integer({ default: 10000, minimum: 0 })
|
|
15
15
|
});
|
|
16
|
+
exports.redisPubSubConfig = (0, common_1.defineConfig)('pubsub.redis', {
|
|
17
|
+
/** Prefix of the Redis channels the messages are published to. Default: `'appweaver:'`. */
|
|
18
|
+
CHANNEL_PREFIX: typebox_1.Type.String({ default: 'appweaver:' })
|
|
19
|
+
});
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./config"), exports);
|
|
18
18
|
__exportStar(require("./redis-memory"), exports);
|
|
19
|
+
__exportStar(require("./redis-pubsub"), exports);
|
package/module.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/** The Redis memory, also the store of the cache, the rate limit and the BullMQ queue. */
|
|
1
|
+
/** The Redis memory, also the store of the cache, the rate limit and the BullMQ queue, and the Redis pub/sub. */
|
|
2
2
|
declare const _default: import("@appweaver/common").Module;
|
|
3
3
|
export default _default;
|
package/module.js
CHANGED
|
@@ -35,16 +35,21 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
const common_1 = require("@appweaver/common");
|
|
37
37
|
const config_1 = require("./config");
|
|
38
|
-
/** The Redis memory, also the store of the cache, the rate limit and the BullMQ queue. */
|
|
38
|
+
/** The Redis memory, also the store of the cache, the rate limit and the BullMQ queue, and the Redis pub/sub. */
|
|
39
39
|
exports.default = (0, common_1.defineModule)({
|
|
40
40
|
name: '@appweaver/redis',
|
|
41
|
-
config: [config_1.redisConfig],
|
|
42
|
-
loggers: ['memory'],
|
|
41
|
+
config: [config_1.redisConfig, config_1.redisPubSubConfig],
|
|
42
|
+
loggers: ['memory', 'pubsub'],
|
|
43
43
|
providers: [
|
|
44
44
|
{
|
|
45
45
|
contract: common_1.Memory,
|
|
46
46
|
name: 'redis',
|
|
47
47
|
load: () => Promise.resolve().then(() => __importStar(require('./redis-memory')))
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
contract: common_1.PubSub,
|
|
51
|
+
name: 'redis',
|
|
52
|
+
load: () => Promise.resolve().then(() => __importStar(require('./redis-pubsub')))
|
|
48
53
|
}
|
|
49
54
|
]
|
|
50
55
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appweaver/redis",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Appweaver - the backend framework for AI-first development (@redis)",
|
|
5
5
|
"author": "Luka Matosevic",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"flatted": "3.4.2"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"@appweaver/common": "^2.
|
|
48
|
-
"@appweaver/core": "^2.
|
|
47
|
+
"@appweaver/common": "^2.6.0",
|
|
48
|
+
"@appweaver/core": "^2.6.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@appweaver/common": "^1.12.2",
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PubSub, PubSubHandler } from '@appweaver/common';
|
|
2
|
+
/**
|
|
3
|
+
* The `redis` driver of `PubSub`, publishing the messages on the Redis server of the memory (`memory.redis.url`), so
|
|
4
|
+
* the subscribers of every process receive them. A subscribed connection runs no other command, so the driver opens
|
|
5
|
+
* one for publishing and one for subscribing, connected on the first use.
|
|
6
|
+
*/
|
|
7
|
+
export declare class RedisPubSub extends PubSub {
|
|
8
|
+
onDestroy(): Promise<void>;
|
|
9
|
+
publish<T>(channel: string, message: T): Promise<void>;
|
|
10
|
+
subscribe<T>(channel: string, handler: PubSubHandler<T>): Promise<string>;
|
|
11
|
+
unsubscribe(subscriptionId: string): Promise<boolean>;
|
|
12
|
+
}
|
package/redis-pubsub.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisPubSub = void 0;
|
|
4
|
+
const ioredis_1 = require("ioredis");
|
|
5
|
+
const common_1 = require("@appweaver/common");
|
|
6
|
+
const config_1 = require("./config");
|
|
7
|
+
const logger = (0, common_1.createLogger)('pubsub');
|
|
8
|
+
/**
|
|
9
|
+
* The `redis` driver of `PubSub`, publishing the messages on the Redis server of the memory (`memory.redis.url`), so
|
|
10
|
+
* the subscribers of every process receive them. A subscribed connection runs no other command, so the driver opens
|
|
11
|
+
* one for publishing and one for subscribing, connected on the first use.
|
|
12
|
+
*/
|
|
13
|
+
class RedisPubSub extends common_1.PubSub {
|
|
14
|
+
/** @internal */
|
|
15
|
+
_subscriptions = new Map();
|
|
16
|
+
/** @internal */
|
|
17
|
+
_publisher;
|
|
18
|
+
/** @internal */
|
|
19
|
+
_subscriber;
|
|
20
|
+
async onDestroy() {
|
|
21
|
+
this._subscriptions.clear();
|
|
22
|
+
const clients = [this._publisher, this._subscriber].filter((client) => client !== undefined);
|
|
23
|
+
await Promise.all(clients.map((client) => client.quit().catch(() => client.disconnect())));
|
|
24
|
+
this._publisher = undefined;
|
|
25
|
+
this._subscriber = undefined;
|
|
26
|
+
}
|
|
27
|
+
async publish(channel, message) {
|
|
28
|
+
this._publisher ??= this.createClient('appweaver-publisher');
|
|
29
|
+
await this._publisher.publish(this.channelName(channel), JSON.stringify(message ?? null));
|
|
30
|
+
}
|
|
31
|
+
async subscribe(channel, handler) {
|
|
32
|
+
const subscriber = this.subscriber();
|
|
33
|
+
const subscriptionId = (0, common_1.uuid)();
|
|
34
|
+
const subscribed = this.isSubscribed(channel);
|
|
35
|
+
this._subscriptions.set(subscriptionId, { channel, handler });
|
|
36
|
+
if (!subscribed) {
|
|
37
|
+
await subscriber.subscribe(this.channelName(channel));
|
|
38
|
+
}
|
|
39
|
+
return subscriptionId;
|
|
40
|
+
}
|
|
41
|
+
async unsubscribe(subscriptionId) {
|
|
42
|
+
const subscription = this._subscriptions.get(subscriptionId);
|
|
43
|
+
if (!subscription) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
this._subscriptions.delete(subscriptionId);
|
|
47
|
+
if (!this.isSubscribed(subscription.channel)) {
|
|
48
|
+
await this._subscriber?.unsubscribe(this.channelName(subscription.channel));
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
/** @internal */
|
|
53
|
+
subscriber() {
|
|
54
|
+
if (!this._subscriber) {
|
|
55
|
+
this._subscriber = this.createClient('appweaver-subscriber');
|
|
56
|
+
this._subscriber.on('message', (name, json) => this.dispatch(name, json));
|
|
57
|
+
}
|
|
58
|
+
return this._subscriber;
|
|
59
|
+
}
|
|
60
|
+
/** @internal */
|
|
61
|
+
createClient(connectionName) {
|
|
62
|
+
const client = new ioredis_1.Redis(config_1.redisConfig.URL, { connectionName });
|
|
63
|
+
client.on('error', (e) => logger.error(e, 'Redis pub/sub error'));
|
|
64
|
+
return client;
|
|
65
|
+
}
|
|
66
|
+
/** @internal */
|
|
67
|
+
dispatch(name, json) {
|
|
68
|
+
const channel = name.slice(config_1.redisPubSubConfig.CHANNEL_PREFIX.length);
|
|
69
|
+
let message;
|
|
70
|
+
try {
|
|
71
|
+
message = JSON.parse(json);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
logger.error(e, `Invalid message of the '${channel}' channel`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (const subscription of this._subscriptions.values()) {
|
|
78
|
+
if (subscription.channel === channel) {
|
|
79
|
+
// Every subscriber gets its own copy, as with the memory driver
|
|
80
|
+
const copy = structuredClone(message);
|
|
81
|
+
Promise.resolve()
|
|
82
|
+
.then(() => subscription.handler(copy))
|
|
83
|
+
.catch((e) => logger.error(e, `Subscriber of the '${channel}' channel failed`));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** @internal */
|
|
88
|
+
isSubscribed(channel) {
|
|
89
|
+
for (const subscription of this._subscriptions.values()) {
|
|
90
|
+
if (subscription.channel === channel) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/** @internal */
|
|
97
|
+
channelName(channel) {
|
|
98
|
+
return config_1.redisPubSubConfig.CHANNEL_PREFIX + channel;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.RedisPubSub = RedisPubSub;
|