@fedify/redis 0.4.0 → 1.8.0-dev.952
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/LICENSE +1 -1
- package/README.md +5 -55
- package/dist/codec.d.ts +51 -0
- package/dist/codec.js +62 -0
- package/dist/kv.d.ts +50 -0
- package/dist/kv.js +63 -0
- package/dist/mod.d.ts +5 -0
- package/dist/mod.js +8 -0
- package/dist/mq.d.ts +76 -0
- package/dist/mq.js +144 -0
- package/package.json +49 -43
- package/esm/_dnt.shims.js +0 -64
- package/esm/mod.js +0 -1
- package/esm/package.json +0 -3
- package/esm/src/codec.js +0 -67
- package/esm/src/kv.js +0 -77
- package/esm/src/mod.js +0 -3
- package/esm/src/mq.js +0 -170
- package/script/_dnt.shims.js +0 -69
- package/script/mod.js +0 -17
- package/script/package.json +0 -3
- package/script/src/codec.js +0 -74
- package/script/src/kv.js +0 -81
- package/script/src/mod.js +0 -19
- package/script/src/mq.js +0 -197
- package/types/_dnt.shims.d.ts +0 -9
- package/types/_dnt.shims.d.ts.map +0 -1
- package/types/mod.d.ts +0 -2
- package/types/mod.d.ts.map +0 -1
- package/types/src/codec.d.ts +0 -47
- package/types/src/codec.d.ts.map +0 -1
- package/types/src/kv.d.ts +0 -46
- package/types/src/kv.d.ts.map +0 -1
- package/types/src/mod.d.ts +0 -4
- package/types/src/mod.d.ts.map +0 -1
- package/types/src/mq.d.ts +0 -73
- package/types/src/mq.d.ts.map +0 -1
package/esm/_dnt.shims.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { Deno } from "@deno/shim-deno";
|
|
2
|
-
export { Deno } from "@deno/shim-deno";
|
|
3
|
-
import { Temporal as Temporal } from "@js-temporal/polyfill";
|
|
4
|
-
export { Temporal as Temporal } from "@js-temporal/polyfill";
|
|
5
|
-
const dntGlobals = {
|
|
6
|
-
Deno,
|
|
7
|
-
Temporal,
|
|
8
|
-
};
|
|
9
|
-
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
10
|
-
function createMergeProxy(baseObj, extObj) {
|
|
11
|
-
return new Proxy(baseObj, {
|
|
12
|
-
get(_target, prop, _receiver) {
|
|
13
|
-
if (prop in extObj) {
|
|
14
|
-
return extObj[prop];
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
return baseObj[prop];
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
set(_target, prop, value) {
|
|
21
|
-
if (prop in extObj) {
|
|
22
|
-
delete extObj[prop];
|
|
23
|
-
}
|
|
24
|
-
baseObj[prop] = value;
|
|
25
|
-
return true;
|
|
26
|
-
},
|
|
27
|
-
deleteProperty(_target, prop) {
|
|
28
|
-
let success = false;
|
|
29
|
-
if (prop in extObj) {
|
|
30
|
-
delete extObj[prop];
|
|
31
|
-
success = true;
|
|
32
|
-
}
|
|
33
|
-
if (prop in baseObj) {
|
|
34
|
-
delete baseObj[prop];
|
|
35
|
-
success = true;
|
|
36
|
-
}
|
|
37
|
-
return success;
|
|
38
|
-
},
|
|
39
|
-
ownKeys(_target) {
|
|
40
|
-
const baseKeys = Reflect.ownKeys(baseObj);
|
|
41
|
-
const extKeys = Reflect.ownKeys(extObj);
|
|
42
|
-
const extKeysSet = new Set(extKeys);
|
|
43
|
-
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
44
|
-
},
|
|
45
|
-
defineProperty(_target, prop, desc) {
|
|
46
|
-
if (prop in extObj) {
|
|
47
|
-
delete extObj[prop];
|
|
48
|
-
}
|
|
49
|
-
Reflect.defineProperty(baseObj, prop, desc);
|
|
50
|
-
return true;
|
|
51
|
-
},
|
|
52
|
-
getOwnPropertyDescriptor(_target, prop) {
|
|
53
|
-
if (prop in extObj) {
|
|
54
|
-
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
has(_target, prop) {
|
|
61
|
-
return prop in extObj || prop in baseObj;
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
}
|
package/esm/mod.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./src/mod.js";
|
package/esm/package.json
DELETED
package/esm/src/codec.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var _JsonCodec_textEncoder, _JsonCodec_textDecoder;
|
|
7
|
-
import { Buffer } from "node:buffer";
|
|
8
|
-
/**
|
|
9
|
-
* An error that occurs when encoding or decoding data.
|
|
10
|
-
*/
|
|
11
|
-
export class CodecError extends Error {
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.name = "CodecError";
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* An error that occurs when encoding data.
|
|
19
|
-
*/
|
|
20
|
-
export class EncodingError extends CodecError {
|
|
21
|
-
constructor(message) {
|
|
22
|
-
super(message);
|
|
23
|
-
this.name = "EncodingError";
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* An error that occurs when decoding data.
|
|
28
|
-
*/
|
|
29
|
-
export class DecodingError extends CodecError {
|
|
30
|
-
constructor(message) {
|
|
31
|
-
super(message);
|
|
32
|
-
this.name = "DecodingError";
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* A codec that encodes and decodes JavaScript objects to and from JSON.
|
|
37
|
-
*/
|
|
38
|
-
export class JsonCodec {
|
|
39
|
-
constructor() {
|
|
40
|
-
_JsonCodec_textEncoder.set(this, new TextEncoder());
|
|
41
|
-
_JsonCodec_textDecoder.set(this, new TextDecoder());
|
|
42
|
-
}
|
|
43
|
-
encode(value) {
|
|
44
|
-
let json;
|
|
45
|
-
try {
|
|
46
|
-
json = JSON.stringify(value);
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
if (e instanceof TypeError)
|
|
50
|
-
throw new EncodingError(e.message);
|
|
51
|
-
throw e;
|
|
52
|
-
}
|
|
53
|
-
return Buffer.from(__classPrivateFieldGet(this, _JsonCodec_textEncoder, "f").encode(json));
|
|
54
|
-
}
|
|
55
|
-
decode(encoded) {
|
|
56
|
-
const json = __classPrivateFieldGet(this, _JsonCodec_textDecoder, "f").decode(encoded);
|
|
57
|
-
try {
|
|
58
|
-
return JSON.parse(json);
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
if (e instanceof SyntaxError)
|
|
62
|
-
throw new DecodingError(e.message);
|
|
63
|
-
throw e;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
_JsonCodec_textEncoder = new WeakMap(), _JsonCodec_textDecoder = new WeakMap();
|
package/esm/src/kv.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _RedisKvStore_instances, _RedisKvStore_redis, _RedisKvStore_keyPrefix, _RedisKvStore_codec, _RedisKvStore_textEncoder, _RedisKvStore_serializeKey;
|
|
13
|
-
import { Buffer } from "node:buffer";
|
|
14
|
-
import { JsonCodec } from "./codec.js";
|
|
15
|
-
/**
|
|
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
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export class RedisKvStore {
|
|
31
|
-
/**
|
|
32
|
-
* Creates a new Redis key-value store.
|
|
33
|
-
* @param redis The Redis client to use.
|
|
34
|
-
* @param options The options for the key-value store.
|
|
35
|
-
*/
|
|
36
|
-
constructor(redis, options = {}) {
|
|
37
|
-
_RedisKvStore_instances.add(this);
|
|
38
|
-
_RedisKvStore_redis.set(this, void 0);
|
|
39
|
-
_RedisKvStore_keyPrefix.set(this, void 0);
|
|
40
|
-
_RedisKvStore_codec.set(this, void 0);
|
|
41
|
-
_RedisKvStore_textEncoder.set(this, new TextEncoder());
|
|
42
|
-
__classPrivateFieldSet(this, _RedisKvStore_redis, redis, "f");
|
|
43
|
-
__classPrivateFieldSet(this, _RedisKvStore_keyPrefix, options.keyPrefix ?? "fedify::", "f");
|
|
44
|
-
__classPrivateFieldSet(this, _RedisKvStore_codec, options.codec ?? new JsonCodec(), "f");
|
|
45
|
-
}
|
|
46
|
-
async get(key) {
|
|
47
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
48
|
-
const encodedValue = await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").getBuffer(serializedKey);
|
|
49
|
-
if (encodedValue == null)
|
|
50
|
-
return undefined;
|
|
51
|
-
return __classPrivateFieldGet(this, _RedisKvStore_codec, "f").decode(encodedValue);
|
|
52
|
-
}
|
|
53
|
-
async set(key, value, options) {
|
|
54
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
55
|
-
const encodedValue = __classPrivateFieldGet(this, _RedisKvStore_codec, "f").encode(value);
|
|
56
|
-
if (options?.ttl != null) {
|
|
57
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").setex(serializedKey, options.ttl.total("second"), encodedValue);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").set(serializedKey, encodedValue);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
async delete(key) {
|
|
64
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
65
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").del(serializedKey);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
_RedisKvStore_redis = new WeakMap(), _RedisKvStore_keyPrefix = new WeakMap(), _RedisKvStore_codec = new WeakMap(), _RedisKvStore_textEncoder = new WeakMap(), _RedisKvStore_instances = new WeakSet(), _RedisKvStore_serializeKey = function _RedisKvStore_serializeKey(key) {
|
|
69
|
-
const suffix = key
|
|
70
|
-
.map((part) => part.replaceAll(":", "_:"))
|
|
71
|
-
.join("::");
|
|
72
|
-
if (typeof __classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f") === "string") {
|
|
73
|
-
return `${__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")}${suffix}`;
|
|
74
|
-
}
|
|
75
|
-
const suffixBytes = __classPrivateFieldGet(this, _RedisKvStore_textEncoder, "f").encode(suffix);
|
|
76
|
-
return Buffer.concat([new Uint8Array(__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")), suffixBytes]);
|
|
77
|
-
};
|
package/esm/src/mod.js
DELETED
package/esm/src/mq.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _RedisMessageQueue_instances, _RedisMessageQueue_redis, _RedisMessageQueue_subRedis, _RedisMessageQueue_workerId, _RedisMessageQueue_channelKey, _RedisMessageQueue_queueKey, _RedisMessageQueue_lockKey, _RedisMessageQueue_codec, _RedisMessageQueue_pollIntervalMs, _RedisMessageQueue_loopHandle, _RedisMessageQueue_poll;
|
|
13
|
-
// deno-lint-ignore-file no-explicit-any
|
|
14
|
-
import * as dntShim from "../_dnt.shims.js";
|
|
15
|
-
import { getLogger } from "@logtape/logtape";
|
|
16
|
-
import { JsonCodec } from "./codec.js";
|
|
17
|
-
const logger = getLogger(["fedify", "redis", "mq"]);
|
|
18
|
-
/**
|
|
19
|
-
* A message queue that uses Redis as the underlying storage.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* import { createFederation } from "@fedify/fedify";
|
|
24
|
-
* import { RedisMessageQueue } from "@fedify/redis";
|
|
25
|
-
* import { Redis } from "ioredis";
|
|
26
|
-
*
|
|
27
|
-
* const federation = createFederation({
|
|
28
|
-
* // ...
|
|
29
|
-
* queue: new RedisMessageQueue(() => new Redis()),
|
|
30
|
-
* });
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export class RedisMessageQueue {
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new Redis message queue.
|
|
36
|
-
* @param redis The Redis client factory.
|
|
37
|
-
* @param options The options for the message queue.
|
|
38
|
-
*/
|
|
39
|
-
constructor(redis, options = {}) {
|
|
40
|
-
_RedisMessageQueue_instances.add(this);
|
|
41
|
-
_RedisMessageQueue_redis.set(this, void 0);
|
|
42
|
-
_RedisMessageQueue_subRedis.set(this, void 0);
|
|
43
|
-
_RedisMessageQueue_workerId.set(this, void 0);
|
|
44
|
-
_RedisMessageQueue_channelKey.set(this, void 0);
|
|
45
|
-
_RedisMessageQueue_queueKey.set(this, void 0);
|
|
46
|
-
_RedisMessageQueue_lockKey.set(this, void 0);
|
|
47
|
-
_RedisMessageQueue_codec.set(this, void 0);
|
|
48
|
-
_RedisMessageQueue_pollIntervalMs.set(this, void 0);
|
|
49
|
-
_RedisMessageQueue_loopHandle.set(this, void 0);
|
|
50
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_redis, redis(), "f");
|
|
51
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_subRedis, redis(), "f");
|
|
52
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_workerId, options.workerId ?? crypto.randomUUID(), "f");
|
|
53
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_channelKey, options.channelKey ?? "fedify_channel", "f");
|
|
54
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_queueKey, options.queueKey ?? "fedify_queue", "f");
|
|
55
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_lockKey, options.lockKey ?? "fedify_lock", "f");
|
|
56
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_codec, options.codec ?? new JsonCodec(), "f");
|
|
57
|
-
__classPrivateFieldSet(this, _RedisMessageQueue_pollIntervalMs, dntShim.Temporal.Duration.from(options.pollInterval ?? { seconds: 5 }).total("millisecond"), "f");
|
|
58
|
-
}
|
|
59
|
-
async enqueue(message, options) {
|
|
60
|
-
const ts = options?.delay == null
|
|
61
|
-
? 0
|
|
62
|
-
: dntShim.Temporal.Now.instant().add(options.delay).epochMilliseconds;
|
|
63
|
-
const encodedMessage = __classPrivateFieldGet(this, _RedisMessageQueue_codec, "f").encode([
|
|
64
|
-
crypto.randomUUID(),
|
|
65
|
-
message,
|
|
66
|
-
]);
|
|
67
|
-
await __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").zadd(__classPrivateFieldGet(this, _RedisMessageQueue_queueKey, "f"), ts, encodedMessage);
|
|
68
|
-
if (ts < 1)
|
|
69
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").publish(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), "");
|
|
70
|
-
}
|
|
71
|
-
async enqueueMany(messages, options) {
|
|
72
|
-
if (messages.length === 0)
|
|
73
|
-
return;
|
|
74
|
-
const ts = options?.delay == null
|
|
75
|
-
? 0
|
|
76
|
-
: dntShim.Temporal.Now.instant().add(options.delay).epochMilliseconds;
|
|
77
|
-
// Use multi to batch multiple ZADD commands:
|
|
78
|
-
const multi = __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").multi();
|
|
79
|
-
for (const message of messages) {
|
|
80
|
-
const encodedMessage = __classPrivateFieldGet(this, _RedisMessageQueue_codec, "f").encode([
|
|
81
|
-
crypto.randomUUID(),
|
|
82
|
-
message,
|
|
83
|
-
]);
|
|
84
|
-
multi.zadd(__classPrivateFieldGet(this, _RedisMessageQueue_queueKey, "f"), ts, encodedMessage);
|
|
85
|
-
}
|
|
86
|
-
// Execute all commands in a single transaction:
|
|
87
|
-
await multi.exec();
|
|
88
|
-
// Notify only if there's no delay:
|
|
89
|
-
if (ts < 1)
|
|
90
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").publish(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), "");
|
|
91
|
-
}
|
|
92
|
-
async listen(handler, options = {}) {
|
|
93
|
-
if (__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f") != null) {
|
|
94
|
-
throw new Error("Already listening");
|
|
95
|
-
}
|
|
96
|
-
const signal = options.signal;
|
|
97
|
-
const poll = async () => {
|
|
98
|
-
while (!signal?.aborted) {
|
|
99
|
-
let message;
|
|
100
|
-
try {
|
|
101
|
-
message = await __classPrivateFieldGet(this, _RedisMessageQueue_instances, "m", _RedisMessageQueue_poll).call(this);
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
logger.error("Error polling for messages: {error}", { error });
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
if (message === undefined)
|
|
108
|
-
return;
|
|
109
|
-
await handler(message);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
const promise = __classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").subscribe(__classPrivateFieldGet(this, _RedisMessageQueue_channelKey, "f"), () => {
|
|
113
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").on("message", poll);
|
|
114
|
-
signal?.addEventListener("abort", () => {
|
|
115
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").off("message", poll);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
signal?.addEventListener("abort", () => {
|
|
119
|
-
for (const timeout of timeouts)
|
|
120
|
-
clearTimeout(timeout);
|
|
121
|
-
});
|
|
122
|
-
const timeouts = new Set();
|
|
123
|
-
while (!signal?.aborted) {
|
|
124
|
-
let timeout;
|
|
125
|
-
await new Promise((resolve) => {
|
|
126
|
-
signal?.addEventListener("abort", resolve);
|
|
127
|
-
timeout = setTimeout(() => {
|
|
128
|
-
signal?.removeEventListener("abort", resolve);
|
|
129
|
-
resolve(0);
|
|
130
|
-
}, __classPrivateFieldGet(this, _RedisMessageQueue_pollIntervalMs, "f"));
|
|
131
|
-
timeouts.add(timeout);
|
|
132
|
-
});
|
|
133
|
-
if (timeout != null)
|
|
134
|
-
timeouts.delete(timeout);
|
|
135
|
-
await poll();
|
|
136
|
-
}
|
|
137
|
-
return await new Promise((resolve) => {
|
|
138
|
-
signal?.addEventListener("abort", () => {
|
|
139
|
-
promise.catch(() => resolve()).then(() => resolve());
|
|
140
|
-
});
|
|
141
|
-
promise.catch(() => resolve()).then(() => resolve());
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
[(_RedisMessageQueue_redis = new WeakMap(), _RedisMessageQueue_subRedis = new WeakMap(), _RedisMessageQueue_workerId = new WeakMap(), _RedisMessageQueue_channelKey = new WeakMap(), _RedisMessageQueue_queueKey = new WeakMap(), _RedisMessageQueue_lockKey = new WeakMap(), _RedisMessageQueue_codec = new WeakMap(), _RedisMessageQueue_pollIntervalMs = new WeakMap(), _RedisMessageQueue_loopHandle = new WeakMap(), _RedisMessageQueue_instances = new WeakSet(), _RedisMessageQueue_poll = async function _RedisMessageQueue_poll() {
|
|
145
|
-
logger.debug("Polling for messages...");
|
|
146
|
-
const result = await __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").set(__classPrivateFieldGet(this, _RedisMessageQueue_lockKey, "f"), __classPrivateFieldGet(this, _RedisMessageQueue_workerId, "f"), "EX", Math.floor(__classPrivateFieldGet(this, _RedisMessageQueue_pollIntervalMs, "f") / 1000 * 2), "NX");
|
|
147
|
-
if (result == null) {
|
|
148
|
-
logger.debug("Another worker is already processing messages; skipping...");
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
logger.debug("Acquired lock; processing messages...");
|
|
152
|
-
const messages = await __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").zrangebyscoreBuffer(__classPrivateFieldGet(this, _RedisMessageQueue_queueKey, "f"), 0, dntShim.Temporal.Now.instant().epochMilliseconds);
|
|
153
|
-
logger.debug("Found {messages} messages to process.", { messages: messages.length });
|
|
154
|
-
try {
|
|
155
|
-
if (messages.length < 1)
|
|
156
|
-
return;
|
|
157
|
-
const encodedMessage = messages[0];
|
|
158
|
-
await __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").zrem(__classPrivateFieldGet(this, _RedisMessageQueue_queueKey, "f"), encodedMessage);
|
|
159
|
-
const [_, message] = __classPrivateFieldGet(this, _RedisMessageQueue_codec, "f").decode(encodedMessage);
|
|
160
|
-
return message;
|
|
161
|
-
}
|
|
162
|
-
finally {
|
|
163
|
-
await __classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").del(__classPrivateFieldGet(this, _RedisMessageQueue_lockKey, "f"));
|
|
164
|
-
}
|
|
165
|
-
}, Symbol.dispose)]() {
|
|
166
|
-
clearInterval(__classPrivateFieldGet(this, _RedisMessageQueue_loopHandle, "f"));
|
|
167
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_redis, "f").disconnect();
|
|
168
|
-
__classPrivateFieldGet(this, _RedisMessageQueue_subRedis, "f").disconnect();
|
|
169
|
-
}
|
|
170
|
-
}
|
package/script/_dnt.shims.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.dntGlobalThis = exports.Temporal = exports.Deno = void 0;
|
|
4
|
-
const shim_deno_1 = require("@deno/shim-deno");
|
|
5
|
-
var shim_deno_2 = require("@deno/shim-deno");
|
|
6
|
-
Object.defineProperty(exports, "Deno", { enumerable: true, get: function () { return shim_deno_2.Deno; } });
|
|
7
|
-
const polyfill_1 = require("@js-temporal/polyfill");
|
|
8
|
-
var polyfill_2 = require("@js-temporal/polyfill");
|
|
9
|
-
Object.defineProperty(exports, "Temporal", { enumerable: true, get: function () { return polyfill_2.Temporal; } });
|
|
10
|
-
const dntGlobals = {
|
|
11
|
-
Deno: shim_deno_1.Deno,
|
|
12
|
-
Temporal: polyfill_1.Temporal,
|
|
13
|
-
};
|
|
14
|
-
exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
15
|
-
function createMergeProxy(baseObj, extObj) {
|
|
16
|
-
return new Proxy(baseObj, {
|
|
17
|
-
get(_target, prop, _receiver) {
|
|
18
|
-
if (prop in extObj) {
|
|
19
|
-
return extObj[prop];
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
return baseObj[prop];
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
set(_target, prop, value) {
|
|
26
|
-
if (prop in extObj) {
|
|
27
|
-
delete extObj[prop];
|
|
28
|
-
}
|
|
29
|
-
baseObj[prop] = value;
|
|
30
|
-
return true;
|
|
31
|
-
},
|
|
32
|
-
deleteProperty(_target, prop) {
|
|
33
|
-
let success = false;
|
|
34
|
-
if (prop in extObj) {
|
|
35
|
-
delete extObj[prop];
|
|
36
|
-
success = true;
|
|
37
|
-
}
|
|
38
|
-
if (prop in baseObj) {
|
|
39
|
-
delete baseObj[prop];
|
|
40
|
-
success = true;
|
|
41
|
-
}
|
|
42
|
-
return success;
|
|
43
|
-
},
|
|
44
|
-
ownKeys(_target) {
|
|
45
|
-
const baseKeys = Reflect.ownKeys(baseObj);
|
|
46
|
-
const extKeys = Reflect.ownKeys(extObj);
|
|
47
|
-
const extKeysSet = new Set(extKeys);
|
|
48
|
-
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
49
|
-
},
|
|
50
|
-
defineProperty(_target, prop, desc) {
|
|
51
|
-
if (prop in extObj) {
|
|
52
|
-
delete extObj[prop];
|
|
53
|
-
}
|
|
54
|
-
Reflect.defineProperty(baseObj, prop, desc);
|
|
55
|
-
return true;
|
|
56
|
-
},
|
|
57
|
-
getOwnPropertyDescriptor(_target, prop) {
|
|
58
|
-
if (prop in extObj) {
|
|
59
|
-
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
has(_target, prop) {
|
|
66
|
-
return prop in extObj || prop in baseObj;
|
|
67
|
-
},
|
|
68
|
-
});
|
|
69
|
-
}
|
package/script/mod.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./src/mod.js"), exports);
|
package/script/package.json
DELETED
package/script/src/codec.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var _JsonCodec_textEncoder, _JsonCodec_textDecoder;
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.JsonCodec = exports.DecodingError = exports.EncodingError = exports.CodecError = void 0;
|
|
10
|
-
const node_buffer_1 = require("node:buffer");
|
|
11
|
-
/**
|
|
12
|
-
* An error that occurs when encoding or decoding data.
|
|
13
|
-
*/
|
|
14
|
-
class CodecError extends Error {
|
|
15
|
-
constructor(message) {
|
|
16
|
-
super(message);
|
|
17
|
-
this.name = "CodecError";
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
exports.CodecError = CodecError;
|
|
21
|
-
/**
|
|
22
|
-
* An error that occurs when encoding data.
|
|
23
|
-
*/
|
|
24
|
-
class EncodingError extends CodecError {
|
|
25
|
-
constructor(message) {
|
|
26
|
-
super(message);
|
|
27
|
-
this.name = "EncodingError";
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.EncodingError = EncodingError;
|
|
31
|
-
/**
|
|
32
|
-
* An error that occurs when decoding data.
|
|
33
|
-
*/
|
|
34
|
-
class DecodingError extends CodecError {
|
|
35
|
-
constructor(message) {
|
|
36
|
-
super(message);
|
|
37
|
-
this.name = "DecodingError";
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.DecodingError = DecodingError;
|
|
41
|
-
/**
|
|
42
|
-
* A codec that encodes and decodes JavaScript objects to and from JSON.
|
|
43
|
-
*/
|
|
44
|
-
class JsonCodec {
|
|
45
|
-
constructor() {
|
|
46
|
-
_JsonCodec_textEncoder.set(this, new TextEncoder());
|
|
47
|
-
_JsonCodec_textDecoder.set(this, new TextDecoder());
|
|
48
|
-
}
|
|
49
|
-
encode(value) {
|
|
50
|
-
let json;
|
|
51
|
-
try {
|
|
52
|
-
json = JSON.stringify(value);
|
|
53
|
-
}
|
|
54
|
-
catch (e) {
|
|
55
|
-
if (e instanceof TypeError)
|
|
56
|
-
throw new EncodingError(e.message);
|
|
57
|
-
throw e;
|
|
58
|
-
}
|
|
59
|
-
return node_buffer_1.Buffer.from(__classPrivateFieldGet(this, _JsonCodec_textEncoder, "f").encode(json));
|
|
60
|
-
}
|
|
61
|
-
decode(encoded) {
|
|
62
|
-
const json = __classPrivateFieldGet(this, _JsonCodec_textDecoder, "f").decode(encoded);
|
|
63
|
-
try {
|
|
64
|
-
return JSON.parse(json);
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
if (e instanceof SyntaxError)
|
|
68
|
-
throw new DecodingError(e.message);
|
|
69
|
-
throw e;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
exports.JsonCodec = JsonCodec;
|
|
74
|
-
_JsonCodec_textEncoder = new WeakMap(), _JsonCodec_textDecoder = new WeakMap();
|
package/script/src/kv.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
-
};
|
|
8
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
-
};
|
|
13
|
-
var _RedisKvStore_instances, _RedisKvStore_redis, _RedisKvStore_keyPrefix, _RedisKvStore_codec, _RedisKvStore_textEncoder, _RedisKvStore_serializeKey;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RedisKvStore = void 0;
|
|
16
|
-
const node_buffer_1 = require("node:buffer");
|
|
17
|
-
const codec_js_1 = require("./codec.js");
|
|
18
|
-
/**
|
|
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
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
class RedisKvStore {
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new Redis key-value store.
|
|
36
|
-
* @param redis The Redis client to use.
|
|
37
|
-
* @param options The options for the key-value store.
|
|
38
|
-
*/
|
|
39
|
-
constructor(redis, options = {}) {
|
|
40
|
-
_RedisKvStore_instances.add(this);
|
|
41
|
-
_RedisKvStore_redis.set(this, void 0);
|
|
42
|
-
_RedisKvStore_keyPrefix.set(this, void 0);
|
|
43
|
-
_RedisKvStore_codec.set(this, void 0);
|
|
44
|
-
_RedisKvStore_textEncoder.set(this, new TextEncoder());
|
|
45
|
-
__classPrivateFieldSet(this, _RedisKvStore_redis, redis, "f");
|
|
46
|
-
__classPrivateFieldSet(this, _RedisKvStore_keyPrefix, options.keyPrefix ?? "fedify::", "f");
|
|
47
|
-
__classPrivateFieldSet(this, _RedisKvStore_codec, options.codec ?? new codec_js_1.JsonCodec(), "f");
|
|
48
|
-
}
|
|
49
|
-
async get(key) {
|
|
50
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
51
|
-
const encodedValue = await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").getBuffer(serializedKey);
|
|
52
|
-
if (encodedValue == null)
|
|
53
|
-
return undefined;
|
|
54
|
-
return __classPrivateFieldGet(this, _RedisKvStore_codec, "f").decode(encodedValue);
|
|
55
|
-
}
|
|
56
|
-
async set(key, value, options) {
|
|
57
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
58
|
-
const encodedValue = __classPrivateFieldGet(this, _RedisKvStore_codec, "f").encode(value);
|
|
59
|
-
if (options?.ttl != null) {
|
|
60
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").setex(serializedKey, options.ttl.total("second"), encodedValue);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").set(serializedKey, encodedValue);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async delete(key) {
|
|
67
|
-
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
68
|
-
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").del(serializedKey);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.RedisKvStore = RedisKvStore;
|
|
72
|
-
_RedisKvStore_redis = new WeakMap(), _RedisKvStore_keyPrefix = new WeakMap(), _RedisKvStore_codec = new WeakMap(), _RedisKvStore_textEncoder = new WeakMap(), _RedisKvStore_instances = new WeakSet(), _RedisKvStore_serializeKey = function _RedisKvStore_serializeKey(key) {
|
|
73
|
-
const suffix = key
|
|
74
|
-
.map((part) => part.replaceAll(":", "_:"))
|
|
75
|
-
.join("::");
|
|
76
|
-
if (typeof __classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f") === "string") {
|
|
77
|
-
return `${__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")}${suffix}`;
|
|
78
|
-
}
|
|
79
|
-
const suffixBytes = __classPrivateFieldGet(this, _RedisKvStore_textEncoder, "f").encode(suffix);
|
|
80
|
-
return node_buffer_1.Buffer.concat([new Uint8Array(__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")), suffixBytes]);
|
|
81
|
-
};
|
package/script/src/mod.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./codec.js"), exports);
|
|
18
|
-
__exportStar(require("./kv.js"), exports);
|
|
19
|
-
__exportStar(require("./mq.js"), exports);
|