zen-code 4.3.0 → 4.4.1

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.
@@ -1,87 +0,0 @@
1
- import { a as h, C as u } from "./app-Dy9hkwWD.mjs";
2
- import { createClient as c } from "redis";
3
- class n extends h {
4
- constructor(s, e = !0, i = 300) {
5
- super(s, !0, i), this.id = s, this.compressMessages = e, this.ttl = i, this.queueKey = `queue:${this.id}`, this.channelKey = `channel:${this.id}`, this.redis = n.redis, this.subscriberRedis = n.subscriberRedis, this.cancelSignal = new AbortController(), this.redis.isOpen || this.redis.connect(), this.subscriberRedis.isOpen || this.subscriberRedis.connect(), this.isConnected = !0;
6
- }
7
- static redis = c({ url: process.env.REDIS_URL });
8
- static subscriberRedis = c({ url: process.env.REDIS_URL });
9
- static isQueueExist(s) {
10
- return this.redis.exists(`queue:${s}`).then((e) => e > 0);
11
- }
12
- redis;
13
- subscriberRedis;
14
- queueKey;
15
- channelKey;
16
- isConnected = !1;
17
- cancelSignal;
18
- /**
19
- * 推送消息到 Redis 队列
20
- */
21
- async push(s) {
22
- const e = await this.encodeData(s), i = Buffer.from(e);
23
- await this.redis.rPush(this.queueKey, i), await this.redis.expire(this.queueKey, this.ttl), await this.redis.publish(this.channelKey, i), this.emit("dataChange", e);
24
- }
25
- /**
26
- * 异步生成器:支持 for await...of 方式消费队列数据
27
- */
28
- async *onDataReceive() {
29
- let s = [], e = null, i = !1;
30
- if (this.cancelSignal.signal.aborted)
31
- return;
32
- const l = async (t) => {
33
- const a = await this.decodeData(t);
34
- s.push(a), (a.event === "__stream_end__" || a.event === "__stream_error__" || a.event === "__stream_cancel__") && (setTimeout(() => {
35
- i = !0, e && (e(), e = null);
36
- }, 300), a.event === "__stream_cancel__" && await this.cancel()), e && (e(), e = null);
37
- };
38
- await this.subscriberRedis.subscribe(this.channelKey, (t) => {
39
- l(t);
40
- });
41
- const r = () => {
42
- i = !0, e && (e(), e = null);
43
- };
44
- this.cancelSignal.signal.addEventListener("abort", r);
45
- try {
46
- for (; !i && !this.cancelSignal.signal.aborted; )
47
- if (s.length > 0) {
48
- for (const t of s)
49
- yield t;
50
- s = [];
51
- } else
52
- await new Promise((t) => {
53
- e = t;
54
- });
55
- } finally {
56
- await this.subscriberRedis.unsubscribe(this.channelKey), this.cancelSignal.signal.removeEventListener("abort", r);
57
- }
58
- }
59
- /**
60
- * 获取队列中的所有数据
61
- */
62
- async getAll() {
63
- const s = await this.redis.lRange(this.queueKey, 0, -1);
64
- return !s || s.length === 0 ? [] : this.compressMessages ? await Promise.all(
65
- s.map((e) => this.decodeData(e))
66
- ) : s.map((e) => JSON.parse(e));
67
- }
68
- /**
69
- * 清空队列
70
- */
71
- clear() {
72
- this.isConnected && this.redis.del(this.queueKey);
73
- }
74
- /**
75
- * 取消操作
76
- */
77
- async cancel() {
78
- this.cancelSignal.abort("user cancel this run"), await this.push(new u());
79
- }
80
- async copyToQueue(s, e) {
81
- const i = new n(s, this.compressMessages, e ?? this.ttl);
82
- return await this.redis.copy(this.queueKey, i.queueKey), await this.redis.expire(i.queueKey, e ?? this.ttl), i;
83
- }
84
- }
85
- export {
86
- n as RedisStreamQueue
87
- };