@fedify/redis 0.1.0-dev.4
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 +20 -0
- package/README.md +17 -0
- package/esm/mod.js +1 -0
- package/esm/package.json +3 -0
- package/esm/src/codec.js +67 -0
- package/esm/src/kv.js +65 -0
- package/esm/src/mod.js +2 -0
- package/package.json +46 -0
- package/script/mod.js +17 -0
- package/script/package.json +3 -0
- package/script/src/codec.js +74 -0
- package/script/src/kv.js +69 -0
- package/script/src/mod.js +18 -0
- package/types/mod.d.ts +2 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/codec.d.ts +48 -0
- package/types/src/codec.d.ts.map +1 -0
- package/types/src/kv.d.ts +34 -0
- package/types/src/kv.d.ts.map +1 -0
- package/types/src/mod.d.ts +3 -0
- package/types/src/mod.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024 Hong Minhee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!-- deno-fmt-ignore-file -->
|
|
2
|
+
|
|
3
|
+
@fedify/redis
|
|
4
|
+
=============
|
|
5
|
+
|
|
6
|
+
[![JSR][JSR badge]][JSR]
|
|
7
|
+
[![npm][npm badge]][npm]
|
|
8
|
+
[![GitHub Actions][GitHub Actions badge]][GitHub Actions]
|
|
9
|
+
|
|
10
|
+
Redis drivers for Fedify.
|
|
11
|
+
|
|
12
|
+
[JSR]: https://jsr.io/@fedify/redis
|
|
13
|
+
[JSR badge]: https://jsr.io/badges/@fedify/redis
|
|
14
|
+
[npm]: https://www.npmjs.com/package/@fedify/redis
|
|
15
|
+
[npm badge]: https://img.shields.io/npm/v/@fedify/redis?logo=npm
|
|
16
|
+
[GitHub Actions]: https://github.com/dahlia/fedify-redis/actions/workflows/main.yaml
|
|
17
|
+
[GitHub Actions badge]: https://github.com/dahlia/fedify-redis/actions/workflows/main.yaml/badge.svg
|
package/esm/mod.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/mod.js";
|
package/esm/package.json
ADDED
package/esm/src/codec.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
export class RedisKvStore {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new Redis key-value store.
|
|
21
|
+
* @param redis The Redis client to use.
|
|
22
|
+
* @param options The options for the key-value store.
|
|
23
|
+
*/
|
|
24
|
+
constructor(redis, options = {}) {
|
|
25
|
+
_RedisKvStore_instances.add(this);
|
|
26
|
+
_RedisKvStore_redis.set(this, void 0);
|
|
27
|
+
_RedisKvStore_keyPrefix.set(this, void 0);
|
|
28
|
+
_RedisKvStore_codec.set(this, void 0);
|
|
29
|
+
_RedisKvStore_textEncoder.set(this, new TextEncoder());
|
|
30
|
+
__classPrivateFieldSet(this, _RedisKvStore_redis, redis, "f");
|
|
31
|
+
__classPrivateFieldSet(this, _RedisKvStore_keyPrefix, options.keyPrefix ?? "fedify::", "f");
|
|
32
|
+
__classPrivateFieldSet(this, _RedisKvStore_codec, options.codec ?? new JsonCodec(), "f");
|
|
33
|
+
}
|
|
34
|
+
async get(key) {
|
|
35
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
36
|
+
const encodedValue = await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").getBuffer(serializedKey);
|
|
37
|
+
if (encodedValue == null)
|
|
38
|
+
return undefined;
|
|
39
|
+
return __classPrivateFieldGet(this, _RedisKvStore_codec, "f").decode(encodedValue);
|
|
40
|
+
}
|
|
41
|
+
async set(key, value, options) {
|
|
42
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
43
|
+
const encodedValue = __classPrivateFieldGet(this, _RedisKvStore_codec, "f").encode(value);
|
|
44
|
+
if (options?.ttl != null) {
|
|
45
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").setex(serializedKey, options.ttl.total("second"), encodedValue);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").set(serializedKey, encodedValue);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async delete(key) {
|
|
52
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
53
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").del(serializedKey);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
_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) {
|
|
57
|
+
const suffix = key
|
|
58
|
+
.map((part) => part.replaceAll(":", "_:"))
|
|
59
|
+
.join("::");
|
|
60
|
+
if (typeof __classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f") === "string") {
|
|
61
|
+
return `${__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")}${suffix}`;
|
|
62
|
+
}
|
|
63
|
+
const suffixBytes = __classPrivateFieldGet(this, _RedisKvStore_textEncoder, "f").encode(suffix);
|
|
64
|
+
return Buffer.concat([__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f"), suffixBytes]);
|
|
65
|
+
};
|
package/esm/src/mod.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fedify/redis",
|
|
3
|
+
"version": "0.1.0-dev.4+f76b1ae6",
|
|
4
|
+
"description": "Redis drivers for Fedify",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"fedify",
|
|
7
|
+
"redis"
|
|
8
|
+
],
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Hong Minhee",
|
|
11
|
+
"email": "hong@minhee.org",
|
|
12
|
+
"url": "https://hongminhee.org/"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/dahlia/fedify-redis",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/dahlia/fedify-redis.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/dahlia/fedify-redis/issues"
|
|
22
|
+
},
|
|
23
|
+
"main": "./script/mod.js",
|
|
24
|
+
"module": "./esm/mod.js",
|
|
25
|
+
"types": "./types/mod.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./types/mod.d.ts",
|
|
30
|
+
"default": "./esm/mod.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./types/mod.d.ts",
|
|
34
|
+
"default": "./script/mod.js"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@fedify/fedify": "^0.10.0",
|
|
40
|
+
"ioredis": "^5.4.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^20.9.0"
|
|
44
|
+
},
|
|
45
|
+
"_generatedBy": "dnt@dev"
|
|
46
|
+
}
|
package/script/mod.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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);
|
|
@@ -0,0 +1,74 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
class RedisKvStore {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new Redis key-value store.
|
|
24
|
+
* @param redis The Redis client to use.
|
|
25
|
+
* @param options The options for the key-value store.
|
|
26
|
+
*/
|
|
27
|
+
constructor(redis, options = {}) {
|
|
28
|
+
_RedisKvStore_instances.add(this);
|
|
29
|
+
_RedisKvStore_redis.set(this, void 0);
|
|
30
|
+
_RedisKvStore_keyPrefix.set(this, void 0);
|
|
31
|
+
_RedisKvStore_codec.set(this, void 0);
|
|
32
|
+
_RedisKvStore_textEncoder.set(this, new TextEncoder());
|
|
33
|
+
__classPrivateFieldSet(this, _RedisKvStore_redis, redis, "f");
|
|
34
|
+
__classPrivateFieldSet(this, _RedisKvStore_keyPrefix, options.keyPrefix ?? "fedify::", "f");
|
|
35
|
+
__classPrivateFieldSet(this, _RedisKvStore_codec, options.codec ?? new codec_js_1.JsonCodec(), "f");
|
|
36
|
+
}
|
|
37
|
+
async get(key) {
|
|
38
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
39
|
+
const encodedValue = await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").getBuffer(serializedKey);
|
|
40
|
+
if (encodedValue == null)
|
|
41
|
+
return undefined;
|
|
42
|
+
return __classPrivateFieldGet(this, _RedisKvStore_codec, "f").decode(encodedValue);
|
|
43
|
+
}
|
|
44
|
+
async set(key, value, options) {
|
|
45
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
46
|
+
const encodedValue = __classPrivateFieldGet(this, _RedisKvStore_codec, "f").encode(value);
|
|
47
|
+
if (options?.ttl != null) {
|
|
48
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").setex(serializedKey, options.ttl.total("second"), encodedValue);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").set(serializedKey, encodedValue);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async delete(key) {
|
|
55
|
+
const serializedKey = __classPrivateFieldGet(this, _RedisKvStore_instances, "m", _RedisKvStore_serializeKey).call(this, key);
|
|
56
|
+
await __classPrivateFieldGet(this, _RedisKvStore_redis, "f").del(serializedKey);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.RedisKvStore = RedisKvStore;
|
|
60
|
+
_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) {
|
|
61
|
+
const suffix = key
|
|
62
|
+
.map((part) => part.replaceAll(":", "_:"))
|
|
63
|
+
.join("::");
|
|
64
|
+
if (typeof __classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f") === "string") {
|
|
65
|
+
return `${__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f")}${suffix}`;
|
|
66
|
+
}
|
|
67
|
+
const suffixBytes = __classPrivateFieldGet(this, _RedisKvStore_textEncoder, "f").encode(suffix);
|
|
68
|
+
return node_buffer_1.Buffer.concat([__classPrivateFieldGet(this, _RedisKvStore_keyPrefix, "f"), suffixBytes]);
|
|
69
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
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);
|
package/types/mod.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
/**
|
|
4
|
+
* Encode and decodes JavaScript objects to and from binary data.
|
|
5
|
+
*/
|
|
6
|
+
export interface Codec {
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a JavaScript object to binary data.
|
|
9
|
+
* @param value The JavaScript object to encode.
|
|
10
|
+
* @returns The encoded binary data.
|
|
11
|
+
* @throws {EncodingError} If the JavaScript object cannot be encoded.
|
|
12
|
+
*/
|
|
13
|
+
encode(value: unknown): Buffer;
|
|
14
|
+
/**
|
|
15
|
+
* Decodes a JavaScript object from binary data.
|
|
16
|
+
* @param encoded The binary data to decode.
|
|
17
|
+
* @returns The decoded JavaScript object.
|
|
18
|
+
* @throws {DecodingError} If the binary data is invalid.
|
|
19
|
+
*/
|
|
20
|
+
decode(encoded: Buffer): unknown;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* An error that occurs when encoding or decoding data.
|
|
24
|
+
*/
|
|
25
|
+
export declare class CodecError extends Error {
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* An error that occurs when encoding data.
|
|
30
|
+
*/
|
|
31
|
+
export declare class EncodingError extends CodecError {
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* An error that occurs when decoding data.
|
|
36
|
+
*/
|
|
37
|
+
export declare class DecodingError extends CodecError {
|
|
38
|
+
constructor(message: string);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A codec that encodes and decodes JavaScript objects to and from JSON.
|
|
42
|
+
*/
|
|
43
|
+
export declare class JsonCodec implements Codec {
|
|
44
|
+
#private;
|
|
45
|
+
encode(value: unknown): Buffer;
|
|
46
|
+
decode(encoded: Buffer): unknown;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
|
|
2
|
+
import type { Redis, RedisKey } from "ioredis";
|
|
3
|
+
import { type Codec } from "./codec.js";
|
|
4
|
+
/**
|
|
5
|
+
* Options for {@link RedisKvStore} class.
|
|
6
|
+
*/
|
|
7
|
+
export interface RedisKvStoreOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The prefix to use for all keys in the key-value store in Redis.
|
|
10
|
+
* Defaults to `"fedify::"`.
|
|
11
|
+
*/
|
|
12
|
+
keyPrefix?: RedisKey;
|
|
13
|
+
/**
|
|
14
|
+
* The codec to use for encoding and decoding values in the key-value store.
|
|
15
|
+
* Defaults to {@link JsonCodec}.
|
|
16
|
+
*/
|
|
17
|
+
codec?: Codec;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A key-value store that uses Redis as the underlying storage.
|
|
21
|
+
*/
|
|
22
|
+
export declare class RedisKvStore implements KvStore {
|
|
23
|
+
#private;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new Redis key-value store.
|
|
26
|
+
* @param redis The Redis client to use.
|
|
27
|
+
* @param options The options for the key-value store.
|
|
28
|
+
*/
|
|
29
|
+
constructor(redis: Redis, options?: RedisKvStoreOptions);
|
|
30
|
+
get<T = unknown>(key: KvKey): Promise<T | undefined>;
|
|
31
|
+
set(key: KvKey, value: unknown, options?: KvStoreSetOptions | undefined): Promise<void>;
|
|
32
|
+
delete(key: KvKey): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=kv.d.ts.map
|
|
@@ -0,0 +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,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,EAAE,KAAK,KAAK,EAAa,MAAM,YAAY,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,SAAS,CAAC,EAAE,QAAQ,CAAC;IAErB;;;OAGG;IACH,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED;;GAEG;AACH,qBAAa,YAAa,YAAW,OAAO;;IAM1C;;;;OAIG;gBACS,KAAK,EAAE,KAAK,EAAE,OAAO,GAAE,mBAAwB;IAiBrD,GAAG,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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
|