@fedify/redis 0.1.1 → 0.2.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/README.md CHANGED
@@ -13,6 +13,17 @@ implementations for Redis:
13
13
  - [`RedisKvStore`]
14
14
  - [`RedisMessageQueue`]
15
15
 
16
+ ~~~~ typescript
17
+ import { createFederation } from "@fedify/fedify";
18
+ import { RedisKvStore, RedisMessageQueue } from "@fedify/redis";
19
+ import { Redis } from "ioredis";
20
+
21
+ const federation = createFederation({
22
+ kv: new RedisKvStore(new Redis()),
23
+ queue: new RedisMessageQueue(() => new Redis()),
24
+ });
25
+ ~~~~
26
+
16
27
  [JSR]: https://jsr.io/@fedify/redis
17
28
  [JSR badge]: https://jsr.io/badges/@fedify/redis
18
29
  [npm]: https://www.npmjs.com/package/@fedify/redis
@@ -26,9 +37,39 @@ implementations for Redis:
26
37
  [`RedisMessageQueue`]: https://jsr.io/@fedify/redis/doc/mq/~/RedisMessageQueue
27
38
 
28
39
 
40
+ Installation
41
+ ------------
42
+
43
+ ### Deno
44
+
45
+ ~~~~ sh
46
+ deno add @fedify/redis
47
+ ~~~~
48
+
49
+ ### Node.js
50
+
51
+ ~~~~ sh
52
+ npm install @fedify/redis
53
+ ~~~~
54
+
55
+ ### Bun
56
+
57
+ ~~~~ sh
58
+ bun add @fedify/redis
59
+ ~~~~
60
+
61
+
29
62
  Changelog
30
63
  ---------
31
64
 
65
+ ### Version 0.2.0
66
+
67
+ Released on September 26, 2024.
68
+
69
+ - Let `RedisMessageQueue` follow up the latest `MessageQueue` interface,
70
+ which was updated in Fedify 1.0.0.
71
+ - Added some example code.
72
+
32
73
  ### Version 0.1.1
33
74
 
34
75
  Released on June 22, 2024.
package/esm/src/kv.js CHANGED
@@ -14,6 +14,18 @@ import { Buffer } from "node:buffer";
14
14
  import { JsonCodec } from "./codec.js";
15
15
  /**
16
16
  * A key-value store that uses Redis as the underlying storage.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * import { createFederation } from "@fedify/fedify";
21
+ * import { RedisKvStore } from "@fedify/redis";
22
+ * import { Redis } from "ioredis";
23
+ *
24
+ * const federation = createFederation({
25
+ * // ...
26
+ * kv: new RedisKvStore(new Redis()),
27
+ * });
28
+ * ```
17
29
  */
18
30
  export class RedisKvStore {
19
31
  /**
package/esm/src/mq.js CHANGED
@@ -15,6 +15,18 @@ import * as dntShim from "../_dnt.shims.js";
15
15
  import { JsonCodec } from "./codec.js";
16
16
  /**
17
17
  * A message queue that uses Redis as the underlying storage.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { createFederation } from "@fedify/fedify";
22
+ * import { RedisMessageQueue } from "@fedify/redis";
23
+ * import { Redis } from "ioredis";
24
+ *
25
+ * const federation = createFederation({
26
+ * // ...
27
+ * queue: new RedisMessageQueue(() => new Redis()),
28
+ * });
29
+ * ```
18
30
  */
19
31
  export class RedisMessageQueue {
20
32
  /**
@@ -51,22 +63,33 @@ export class RedisMessageQueue {
51
63
  if (ts < 1)
52
64
  __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").publish(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), "");
53
65
  }
54
- listen(handler) {
66
+ listen(handler, options = {}) {
55
67
  if (__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f") != null) {
56
68
  throw new Error("Already listening");
57
69
  }
70
+ const signal = options.signal;
58
71
  __classPrivateFieldSet(this, _RedisMessageQueue_loopHandle, setInterval(async () => {
59
72
  const message = await __classPrivateFieldGet(this, _RedisMessageQueue_instances, "m", _RedisMessageQueue_poll).call(this);
60
73
  if (message === undefined)
61
74
  return;
62
75
  await handler(message);
63
76
  }, __classPrivateFieldGet(this, _RedisMessageQueue_loopInterval, "f").total({ unit: "milliseconds" })), "f");
64
- __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").subscribe(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), () => {
65
- __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").on("message", async () => {
77
+ const promise = __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").subscribe(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), () => {
78
+ const onMessage = async () => {
66
79
  const message = await __classPrivateFieldGet(this, _RedisMessageQueue_instances, "m", _RedisMessageQueue_poll).call(this);
67
80
  if (message === undefined)
68
81
  return;
69
82
  await handler(message);
83
+ };
84
+ __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").on("message", onMessage);
85
+ signal?.addEventListener("abort", () => {
86
+ __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").off("message", onMessage);
87
+ });
88
+ });
89
+ return new Promise((resolve) => {
90
+ signal?.addEventListener("abort", () => {
91
+ clearInterval(__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f"));
92
+ promise.then(() => resolve());
70
93
  });
71
94
  });
72
95
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/redis",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Redis drivers for Fedify",
5
5
  "keywords": [
6
6
  "fedify",
@@ -55,8 +55,11 @@
55
55
  }
56
56
  }
57
57
  },
58
+ "funding": [
59
+ "https://github.com/sponsors/dahlia"
60
+ ],
58
61
  "dependencies": {
59
- "@fedify/fedify": "^0.10.0",
62
+ "@fedify/fedify": "^1.0.0",
60
63
  "ioredis": "^5.4.0",
61
64
  "@deno/shim-deno": "~0.18.0",
62
65
  "@js-temporal/polyfill": "^0.4.4"
package/script/src/kv.js CHANGED
@@ -17,6 +17,18 @@ const node_buffer_1 = require("node:buffer");
17
17
  const codec_js_1 = require("./codec.js");
18
18
  /**
19
19
  * A key-value store that uses Redis as the underlying storage.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { createFederation } from "@fedify/fedify";
24
+ * import { RedisKvStore } from "@fedify/redis";
25
+ * import { Redis } from "ioredis";
26
+ *
27
+ * const federation = createFederation({
28
+ * // ...
29
+ * kv: new RedisKvStore(new Redis()),
30
+ * });
31
+ * ```
20
32
  */
21
33
  class RedisKvStore {
22
34
  /**
package/script/src/mq.js CHANGED
@@ -41,6 +41,18 @@ const dntShim = __importStar(require("../_dnt.shims.js"));
41
41
  const codec_js_1 = require("./codec.js");
42
42
  /**
43
43
  * A message queue that uses Redis as the underlying storage.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import { createFederation } from "@fedify/fedify";
48
+ * import { RedisMessageQueue } from "@fedify/redis";
49
+ * import { Redis } from "ioredis";
50
+ *
51
+ * const federation = createFederation({
52
+ * // ...
53
+ * queue: new RedisMessageQueue(() => new Redis()),
54
+ * });
55
+ * ```
44
56
  */
45
57
  class RedisMessageQueue {
46
58
  /**
@@ -77,22 +89,33 @@ class RedisMessageQueue {
77
89
  if (ts < 1)
78
90
  __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").publish(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), "");
79
91
  }
80
- listen(handler) {
92
+ listen(handler, options = {}) {
81
93
  if (__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f") != null) {
82
94
  throw new Error("Already listening");
83
95
  }
96
+ const signal = options.signal;
84
97
  __classPrivateFieldSet(this, _RedisMessageQueue_loopHandle, setInterval(async () => {
85
98
  const message = await __classPrivateFieldGet(this, _RedisMessageQueue_instances, "m", _RedisMessageQueue_poll).call(this);
86
99
  if (message === undefined)
87
100
  return;
88
101
  await handler(message);
89
102
  }, __classPrivateFieldGet(this, _RedisMessageQueue_loopInterval, "f").total({ unit: "milliseconds" })), "f");
90
- __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").subscribe(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), () => {
91
- __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").on("message", async () => {
103
+ const promise = __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").subscribe(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), () => {
104
+ const onMessage = async () => {
92
105
  const message = await __classPrivateFieldGet(this, _RedisMessageQueue_instances, "m", _RedisMessageQueue_poll).call(this);
93
106
  if (message === undefined)
94
107
  return;
95
108
  await handler(message);
109
+ };
110
+ __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").on("message", onMessage);
111
+ signal?.addEventListener("abort", () => {
112
+ __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").off("message", onMessage);
113
+ });
114
+ });
115
+ return new Promise((resolve) => {
116
+ signal?.addEventListener("abort", () => {
117
+ clearInterval(__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f"));
118
+ promise.then(() => resolve());
96
119
  });
97
120
  });
98
121
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Buffer } from "node:buffer";
3
2
  /**
4
3
  * Encode and decodes JavaScript objects to and from binary data.
@@ -1 +1 @@
1
- {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/src/codec.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC;IAE/B;;;;;OAKG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;CAClC;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,UAAU;gBAC/B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,UAAU;gBAC/B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,SAAU,YAAW,KAAK;;IAIrC,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;IAW9B,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;CASjC"}
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC;IAE/B;;;;;OAKG;IACH,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;CAClC;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,UAAU;gBAC/B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,UAAU;gBAC/B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,SAAU,YAAW,KAAK;;IAIrC,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;IAW9B,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;CASjC"}
package/types/src/kv.d.ts CHANGED
@@ -18,6 +18,18 @@ export interface RedisKvStoreOptions {
18
18
  }
19
19
  /**
20
20
  * A key-value store that uses Redis as the underlying storage.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { createFederation } from "@fedify/fedify";
25
+ * import { RedisKvStore } from "@fedify/redis";
26
+ * import { Redis } from "ioredis";
27
+ *
28
+ * const federation = createFederation({
29
+ * // ...
30
+ * kv: new RedisKvStore(new Redis()),
31
+ * });
32
+ * ```
21
33
  */
22
34
  export declare class RedisKvStore implements KvStore {
23
35
  #private;
@@ -1 +1 @@
1
- {"version":3,"file":"kv.d.ts","sourceRoot":"","sources":["../../src/src/kv.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,KA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AAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAOpD,GAAG,CACP,GAAG,EAAE,KAAK,EACV,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,iBAAiB,GAAG,SAAS,GACtC,OAAO,CAAC,IAAI,CAAC;IAcV,MAAM,CAAC,GAAG,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;CAIxC"}
package/types/src/mq.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- /// <reference types="node" />
2
1
  import * as dntShim from "../_dnt.shims.js";
3
- import type { MessageQueue, MessageQueueEnqueueOptions } from "@fedify/fedify";
2
+ import type { MessageQueue, MessageQueueEnqueueOptions, MessageQueueListenOptions } from "@fedify/fedify";
4
3
  import type { Redis, RedisKey } from "ioredis";
5
4
  import { type Codec } from "./codec.js";
6
5
  /**
@@ -45,6 +44,18 @@ export interface RedisMessageQueueOptions {
45
44
  }
46
45
  /**
47
46
  * A message queue that uses Redis as the underlying storage.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * import { createFederation } from "@fedify/fedify";
51
+ * import { RedisMessageQueue } from "@fedify/redis";
52
+ * import { Redis } from "ioredis";
53
+ *
54
+ * const federation = createFederation({
55
+ * // ...
56
+ * queue: new RedisMessageQueue(() => new Redis()),
57
+ * });
58
+ * ```
48
59
  */
49
60
  export declare class RedisMessageQueue implements MessageQueue, Disposable {
50
61
  #private;
@@ -55,7 +66,7 @@ export declare class RedisMessageQueue implements MessageQueue, Disposable {
55
66
  */
56
67
  constructor(redis: () => Redis, options?: RedisMessageQueueOptions);
57
68
  enqueue(message: any, options?: MessageQueueEnqueueOptions): Promise<void>;
58
- listen(handler: (message: any) => void | Promise<void>): void;
69
+ listen(handler: (message: any) => void | Promise<void>, options?: MessageQueueListenOptions): Promise<void>;
59
70
  [Symbol.dispose](): void;
60
71
  }
61
72
  //# sourceMappingURL=mq.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mq.d.ts","sourceRoot":"","sources":["../../src/src/mq.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAE5C,OAAO,KAAK,EAAE,YAAY,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAC;AAC/E,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,EAAE,KAAK,KAAK,EAAa,MAAM,YAAY,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,UAAU,CAAC,EAAE,QAAQ,CAAC;IAEtB;;;OAGG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAEpB;;OAEG;IACH,OAAO,CAAC,EAAE,QAAQ,CAAC;IAEnB;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;IAEd;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC;CAC9C;AAED;;GAEG;AACH,qBAAa,iBAAkB,YAAW,YAAY,EAAE,UAAU;;IAWhE;;;;OAIG;gBACS,KAAK,EAAE,MAAM,KAAK,EAAE,OAAO,GAAE,wBAA6B;IAahE,OAAO,CACX,OAAO,EAAE,GAAG,EACZ,OAAO,CAAC,EAAE,0BAA0B,GACnC,OAAO,CAAC,IAAI,CAAC;IA+BhB,MAAM,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAkB7D,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI;CAKzB"}
1
+ {"version":3,"file":"mq.d.ts","sourceRoot":"","sources":["../../src/src/mq.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,kBAAkB,CAAC;AAE5C,OAAO,KAAK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