@fedify/redis 2.0.0-dev.7 → 2.0.0-dev.85

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/kv.cjs CHANGED
@@ -36,6 +36,7 @@ const node_buffer = require_rolldown_runtime.__toESM(require("node:buffer"));
36
36
  var RedisKvStore = class {
37
37
  #redis;
38
38
  #keyPrefix;
39
+ #keyPrefixStr;
39
40
  #codec;
40
41
  #textEncoder = new TextEncoder();
41
42
  /**
@@ -46,6 +47,7 @@ var RedisKvStore = class {
46
47
  constructor(redis, options = {}) {
47
48
  this.#redis = redis;
48
49
  this.#keyPrefix = options.keyPrefix ?? "fedify::";
50
+ this.#keyPrefixStr = typeof this.#keyPrefix === "string" ? this.#keyPrefix : new TextDecoder().decode(new Uint8Array(this.#keyPrefix));
49
51
  this.#codec = options.codec ?? new require_codec.JsonCodec();
50
52
  }
51
53
  #serializeKey(key) {
@@ -70,6 +72,45 @@ var RedisKvStore = class {
70
72
  const serializedKey = this.#serializeKey(key);
71
73
  await this.#redis.del(serializedKey);
72
74
  }
75
+ #deserializeKey(redisKey) {
76
+ const suffix = redisKey.slice(this.#keyPrefixStr.length);
77
+ return suffix.split("::").map((p) => p.replaceAll("_:", ":"));
78
+ }
79
+ /**
80
+ * {@inheritDoc KvStore.list}
81
+ * @since 1.10.0
82
+ */
83
+ async *list(prefix) {
84
+ let pattern;
85
+ let exactKey = null;
86
+ if (prefix == null || prefix.length === 0) pattern = `${this.#keyPrefixStr}*`;
87
+ else {
88
+ const prefixKey = this.#serializeKey(prefix);
89
+ const prefixKeyFullStr = typeof prefixKey === "string" ? prefixKey : new TextDecoder().decode(new Uint8Array(prefixKey));
90
+ exactKey = prefixKey;
91
+ pattern = `${prefixKeyFullStr}::*`;
92
+ }
93
+ if (exactKey != null) {
94
+ const exactValue = await this.#redis.getBuffer(exactKey);
95
+ if (exactValue != null) yield {
96
+ key: prefix,
97
+ value: this.#codec.decode(exactValue)
98
+ };
99
+ }
100
+ let cursor = "0";
101
+ do {
102
+ const [nextCursor, keys] = await this.#redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
103
+ cursor = nextCursor;
104
+ for (const key of keys) {
105
+ const encodedValue = await this.#redis.getBuffer(key);
106
+ if (encodedValue == null) continue;
107
+ yield {
108
+ key: this.#deserializeKey(key),
109
+ value: this.#codec.decode(encodedValue)
110
+ };
111
+ }
112
+ } while (cursor !== "0");
113
+ }
73
114
  };
74
115
 
75
116
  //#endregion
package/dist/kv.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Codec } from "./codec.cjs";
2
- import { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
2
+ import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions } from "@fedify/fedify";
3
3
  import { Cluster, Redis, RedisKey } from "ioredis";
4
4
 
5
5
  //#region src/kv.d.ts
@@ -56,6 +56,11 @@ declare class RedisKvStore implements KvStore {
56
56
  get<T = unknown>(key: KvKey): Promise<T | undefined>;
57
57
  set(key: KvKey, value: unknown, options?: KvStoreSetOptions | undefined): Promise<void>;
58
58
  delete(key: KvKey): Promise<void>;
59
+ /**
60
+ * {@inheritDoc KvStore.list}
61
+ * @since 1.10.0
62
+ */
63
+ list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
59
64
  }
60
65
  //#endregion
61
66
  export { RedisKvStore, RedisKvStoreOptions };
package/dist/kv.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { Codec } from "./codec.js";
3
- import { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
3
+ import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions } from "@fedify/fedify";
4
4
  import { Cluster, Redis, RedisKey } from "ioredis";
5
5
 
6
6
  //#region src/kv.d.ts
@@ -57,6 +57,11 @@ declare class RedisKvStore implements KvStore {
57
57
  get<T = unknown>(key: KvKey): Promise<T | undefined>;
58
58
  set(key: KvKey, value: unknown, options?: KvStoreSetOptions | undefined): Promise<void>;
59
59
  delete(key: KvKey): Promise<void>;
60
+ /**
61
+ * {@inheritDoc KvStore.list}
62
+ * @since 1.10.0
63
+ */
64
+ list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
60
65
  }
61
66
  //#endregion
62
67
  export { RedisKvStore, RedisKvStoreOptions };
package/dist/kv.js CHANGED
@@ -35,6 +35,7 @@ import { Buffer } from "node:buffer";
35
35
  var RedisKvStore = class {
36
36
  #redis;
37
37
  #keyPrefix;
38
+ #keyPrefixStr;
38
39
  #codec;
39
40
  #textEncoder = new TextEncoder();
40
41
  /**
@@ -45,6 +46,7 @@ var RedisKvStore = class {
45
46
  constructor(redis, options = {}) {
46
47
  this.#redis = redis;
47
48
  this.#keyPrefix = options.keyPrefix ?? "fedify::";
49
+ this.#keyPrefixStr = typeof this.#keyPrefix === "string" ? this.#keyPrefix : new TextDecoder().decode(new Uint8Array(this.#keyPrefix));
48
50
  this.#codec = options.codec ?? new JsonCodec();
49
51
  }
50
52
  #serializeKey(key) {
@@ -69,6 +71,45 @@ var RedisKvStore = class {
69
71
  const serializedKey = this.#serializeKey(key);
70
72
  await this.#redis.del(serializedKey);
71
73
  }
74
+ #deserializeKey(redisKey) {
75
+ const suffix = redisKey.slice(this.#keyPrefixStr.length);
76
+ return suffix.split("::").map((p) => p.replaceAll("_:", ":"));
77
+ }
78
+ /**
79
+ * {@inheritDoc KvStore.list}
80
+ * @since 1.10.0
81
+ */
82
+ async *list(prefix) {
83
+ let pattern;
84
+ let exactKey = null;
85
+ if (prefix == null || prefix.length === 0) pattern = `${this.#keyPrefixStr}*`;
86
+ else {
87
+ const prefixKey = this.#serializeKey(prefix);
88
+ const prefixKeyFullStr = typeof prefixKey === "string" ? prefixKey : new TextDecoder().decode(new Uint8Array(prefixKey));
89
+ exactKey = prefixKey;
90
+ pattern = `${prefixKeyFullStr}::*`;
91
+ }
92
+ if (exactKey != null) {
93
+ const exactValue = await this.#redis.getBuffer(exactKey);
94
+ if (exactValue != null) yield {
95
+ key: prefix,
96
+ value: this.#codec.decode(exactValue)
97
+ };
98
+ }
99
+ let cursor = "0";
100
+ do {
101
+ const [nextCursor, keys] = await this.#redis.scan(cursor, "MATCH", pattern, "COUNT", 100);
102
+ cursor = nextCursor;
103
+ for (const key of keys) {
104
+ const encodedValue = await this.#redis.getBuffer(key);
105
+ if (encodedValue == null) continue;
106
+ yield {
107
+ key: this.#deserializeKey(key),
108
+ value: this.#codec.decode(encodedValue)
109
+ };
110
+ }
111
+ } while (cursor !== "0");
112
+ }
72
113
  };
73
114
 
74
115
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/redis",
3
- "version": "2.0.0-dev.7+a392e496",
3
+ "version": "2.0.0-dev.85+a55c8362",
4
4
  "description": "Redis drivers for Fedify",
5
5
  "keywords": [
6
6
  "fedify",
@@ -78,11 +78,11 @@
78
78
  ],
79
79
  "dependencies": {
80
80
  "@js-temporal/polyfill": "^0.5.1",
81
- "@logtape/logtape": "^1.2.2"
81
+ "@logtape/logtape": "^1.3.5"
82
82
  },
83
83
  "peerDependencies": {
84
- "ioredis": "^5.6.1",
85
- "@fedify/fedify": "^2.0.0-dev.7+a392e496"
84
+ "ioredis": "^5.8.2",
85
+ "@fedify/fedify": "^2.0.0-dev.85+a55c8362"
86
86
  },
87
87
  "devDependencies": {
88
88
  "@std/async": "npm:@jsr/std__async@^1.0.13",