@nemigo/server 2.3.1 → 2.3.2

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.
@@ -0,0 +1,28 @@
1
+ import type { IStorage, IStoragePanic, IStorageValue } from "@nemigo/storage/types";
2
+ import type { RedisClient } from "bun";
3
+ export interface ConstructRedisStorage {
4
+ client: RedisClient;
5
+ panic?: IStoragePanic<boolean>;
6
+ prefix?: string;
7
+ ttl_seconds?: number;
8
+ }
9
+ export interface IRedisStorageSetOptions {
10
+ /**
11
+ * - при `undefined` будет использован `ttl_seconds` хранилища
12
+ * - при `null` элементы не устаревают
13
+ */
14
+ ttl_seconds?: number | null;
15
+ prefix?: string;
16
+ }
17
+ export declare class BunRedisStorage implements IStorage<boolean> {
18
+ ttl_seconds?: number;
19
+ prefix: string;
20
+ panic?: IStoragePanic<boolean>;
21
+ client: RedisClient;
22
+ constructor(ctx: ConstructRedisStorage);
23
+ __toKey(name: string, prefix?: string): string;
24
+ has(name: string, prefix?: string): Promise<boolean>;
25
+ get<T extends IStorageValue>(name: string, prefix?: string): Promise<T | undefined>;
26
+ set(name: string, value: IStorageValue, options?: IRedisStorageSetOptions): Promise<void>;
27
+ delete(name: string, prefix?: string): Promise<boolean>;
28
+ }
@@ -0,0 +1,56 @@
1
+ export class BunRedisStorage {
2
+ ttl_seconds;
3
+ prefix;
4
+ panic;
5
+ client;
6
+ constructor(ctx) {
7
+ this.prefix = ctx.prefix ?? "";
8
+ this.ttl_seconds = ctx.ttl_seconds;
9
+ this.panic = ctx.panic;
10
+ this.client = ctx.client;
11
+ }
12
+ __toKey(name, prefix = this.prefix) {
13
+ return prefix + name;
14
+ }
15
+ async has(name, prefix) {
16
+ try {
17
+ return await this.client.exists(this.__toKey(name, prefix));
18
+ }
19
+ catch (error) {
20
+ return this.panic?.onhas?.(error) ?? false;
21
+ }
22
+ }
23
+ async get(name, prefix) {
24
+ try {
25
+ const cached = await this.client.get(this.__toKey(name, prefix));
26
+ if (cached)
27
+ return JSON.parse(cached);
28
+ }
29
+ catch (error) {
30
+ return this.panic?.onget?.(error);
31
+ }
32
+ }
33
+ async set(name, value, options = {}) {
34
+ try {
35
+ const key = this.__toKey(name, options.prefix);
36
+ const ttl_seconds = options.ttl_seconds === null ? undefined : (options.ttl_seconds ?? options.ttl_seconds);
37
+ if (ttl_seconds) {
38
+ return (await this.client.set(key, JSON.stringify(value), "EX", ttl_seconds));
39
+ }
40
+ return (await this.client.set(key, JSON.stringify(value)));
41
+ }
42
+ catch (error) {
43
+ await this.panic?.onset?.(error);
44
+ }
45
+ }
46
+ async delete(name, prefix) {
47
+ try {
48
+ const result = await this.client.del(this.__toKey(name, prefix));
49
+ // DEL возвращает количество удаленных ключей
50
+ return result > 0;
51
+ }
52
+ catch (error) {
53
+ return this.panic?.ondelete?.(error) ?? false;
54
+ }
55
+ }
56
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/server",
3
- "version": "2.3.1",
3
+ "version": "2.3.2",
4
4
  "private": false,
5
5
  "license": "MPL-2.0",
6
6
  "author": {
@@ -23,8 +23,12 @@
23
23
  "default": "./dist/jwt.js"
24
24
  },
25
25
  "./redis": {
26
- "types": "./dist/redis.d.ts",
27
- "default": "./dist/redis.js"
26
+ "types": "./dist/redis/index.d.ts",
27
+ "default": "./dist/redis/index.js"
28
+ },
29
+ "./redis/bun": {
30
+ "types": "./dist/redis/bun.d.ts",
31
+ "default": "./dist/redis/bun.js"
28
32
  }
29
33
  },
30
34
  "peerDependencies": {
@@ -32,14 +36,19 @@
32
36
  "@nemigo/storage": ">=2.3.0",
33
37
  "@redis/client": ">=5.10.0"
34
38
  },
39
+ "peerDependenciesMeta": {
40
+ "@redis/client": {
41
+ "optional": true
42
+ }
43
+ },
35
44
  "dependencies": {
36
45
  "@types/jsonwebtoken": "9.0.10",
37
46
  "jsonwebtoken": "9.0.3"
38
47
  },
39
48
  "devDependencies": {
40
49
  "@redis/client": "5.10.0",
41
- "@nemigo/configs": "2.3.3",
42
- "@nemigo/helpers": "2.3.1",
50
+ "@nemigo/configs": "2.3.5",
51
+ "@nemigo/helpers": "2.3.2",
43
52
  "@nemigo/storage": "2.3.1"
44
53
  }
45
54
  }
File without changes
File without changes