@privateaim/server-kit 0.8.4 → 0.8.5

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.
Files changed (60) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/core/index.d.ts +0 -1
  3. package/dist/core/index.d.ts.map +1 -1
  4. package/dist/domain-event/publish.d.ts +2 -2
  5. package/dist/domain-event/publish.d.ts.map +1 -1
  6. package/dist/domain-event/redis/publish.d.ts +2 -2
  7. package/dist/domain-event/redis/publish.d.ts.map +1 -1
  8. package/dist/domain-event/socket/publish.d.ts +2 -2
  9. package/dist/domain-event/socket/publish.d.ts.map +1 -1
  10. package/dist/index.cjs +252 -108
  11. package/dist/index.cjs.map +1 -1
  12. package/dist/index.mjs +248 -107
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/services/cache/adapters/factory.d.ts +4 -0
  15. package/dist/services/cache/adapters/factory.d.ts.map +1 -0
  16. package/dist/services/cache/adapters/index.d.ts +5 -0
  17. package/dist/services/cache/adapters/index.d.ts.map +1 -0
  18. package/dist/services/cache/adapters/memory.d.ts +15 -0
  19. package/dist/services/cache/adapters/memory.d.ts.map +1 -0
  20. package/dist/services/cache/adapters/redis.d.ts +16 -0
  21. package/dist/services/cache/adapters/redis.d.ts.map +1 -0
  22. package/dist/services/cache/adapters/types.d.ts +10 -0
  23. package/dist/services/cache/adapters/types.d.ts.map +1 -0
  24. package/dist/services/cache/helper.d.ts +3 -0
  25. package/dist/services/cache/helper.d.ts.map +1 -0
  26. package/dist/services/cache/index.d.ts +6 -0
  27. package/dist/services/cache/index.d.ts.map +1 -0
  28. package/dist/services/cache/module.d.ts +13 -0
  29. package/dist/services/cache/module.d.ts.map +1 -0
  30. package/dist/services/cache/singleton.d.ts +3 -0
  31. package/dist/services/cache/singleton.d.ts.map +1 -0
  32. package/dist/services/cache/types.d.ts +16 -0
  33. package/dist/services/cache/types.d.ts.map +1 -0
  34. package/dist/services/index.d.ts +1 -0
  35. package/dist/services/index.d.ts.map +1 -1
  36. package/package.json +13 -13
  37. package/src/core/index.ts +0 -1
  38. package/src/domain-event/publish.ts +2 -2
  39. package/src/domain-event/redis/publish.ts +2 -2
  40. package/src/domain-event/socket/publish.ts +6 -3
  41. package/src/services/cache/adapters/factory.ts +18 -0
  42. package/src/services/cache/adapters/index.ts +11 -0
  43. package/src/services/cache/adapters/memory.ts +67 -0
  44. package/src/services/cache/adapters/redis.ts +69 -0
  45. package/src/services/cache/adapters/types.ts +22 -0
  46. package/src/services/cache/helper.ts +13 -0
  47. package/src/{core/memory-cache → services/cache}/index.ts +4 -1
  48. package/src/services/cache/module.ts +41 -0
  49. package/src/services/cache/singleton.ts +30 -0
  50. package/src/services/cache/types.ts +24 -0
  51. package/src/services/index.ts +1 -0
  52. package/test/unit/memory-cache.spec.ts +10 -9
  53. package/dist/core/memory-cache/index.d.ts +0 -3
  54. package/dist/core/memory-cache/index.d.ts.map +0 -1
  55. package/dist/core/memory-cache/module.d.ts +0 -15
  56. package/dist/core/memory-cache/module.d.ts.map +0 -1
  57. package/dist/core/memory-cache/singleton.d.ts +0 -3
  58. package/dist/core/memory-cache/singleton.d.ts.map +0 -1
  59. package/src/core/memory-cache/module.ts +0 -48
  60. package/src/core/memory-cache/singleton.ts +0 -18
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright (c) 2024-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { Client } from 'redis-extension';
9
+ import { JsonAdapter } from 'redis-extension';
10
+ import { useRedisClient } from '../../redis';
11
+ import type { CacheClearOptions, CacheSetOptions } from '../types';
12
+ import type { CacheAdapter } from './types';
13
+
14
+ export class RedisCacheAdapter implements CacheAdapter {
15
+ protected client : Client;
16
+
17
+ protected instance : JsonAdapter;
18
+
19
+ constructor() {
20
+ this.client = useRedisClient();
21
+ this.instance = new JsonAdapter(this.client);
22
+ }
23
+
24
+ async get(key: string): Promise<any> {
25
+ return this.instance.get(key);
26
+ }
27
+
28
+ async has(key: string) : Promise<boolean> {
29
+ const exists = await this.client.exists(key);
30
+
31
+ return !!exists;
32
+ }
33
+
34
+ async set(key: string, value: any, options: CacheSetOptions): Promise<void> {
35
+ await this.instance.set(key, value, {
36
+ milliseconds: options.ttl,
37
+ });
38
+ }
39
+
40
+ async drop(key: string): Promise<void> {
41
+ await this.instance.drop(key);
42
+ }
43
+
44
+ async dropMany(keys: string[]) : Promise<void> {
45
+ const pipeline = this.client.pipeline();
46
+
47
+ for (let i = 0; i < keys.length; i++) {
48
+ pipeline.del(keys[i]);
49
+ }
50
+
51
+ await pipeline.exec();
52
+ }
53
+
54
+ async clear(options: CacheClearOptions = {}) : Promise<void> {
55
+ if (options.prefix) {
56
+ const pipeline = this.client.pipeline();
57
+
58
+ const keys = await this.client.keys(`${options.prefix}*`);
59
+ for (let i = 0; i < keys.length; i++) {
60
+ pipeline.del(keys[i]);
61
+ }
62
+
63
+ await pipeline.exec();
64
+
65
+ return;
66
+ }
67
+ await this.client.flushdb();
68
+ }
69
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { CacheClearOptions, CacheSetOptions } from '../types';
9
+
10
+ export interface CacheAdapter {
11
+ set(key: string, value: any, options: CacheSetOptions): Promise<void>;
12
+
13
+ get(key: string): Promise<any | undefined>;
14
+
15
+ has(key: string) : Promise<boolean>;
16
+
17
+ drop(key: string): Promise<void>;
18
+
19
+ dropMany(keys: string[]) : Promise<void>;
20
+
21
+ clear(options: CacheClearOptions) : Promise<void>;
22
+ }
@@ -0,0 +1,13 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { buildRedisKeyPath } from '../redis';
9
+ import type { CacheKeyBuildOptions } from './types';
10
+
11
+ export function buildCacheKey(options: CacheKeyBuildOptions) {
12
+ return buildRedisKeyPath(options);
13
+ }
@@ -5,5 +5,8 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
- export * from './module';
8
+ export * from './adapters';
9
+ export * from './helper';
9
10
  export * from './singleton';
11
+ export * from './module';
12
+ export * from './types';
@@ -0,0 +1,41 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { CacheAdapter } from './adapters';
9
+ import type { CacheClearOptions, CacheSetOptions } from './types';
10
+
11
+ export class Cache {
12
+ protected adapter : CacheAdapter;
13
+
14
+ constructor(adapter: CacheAdapter) {
15
+ this.adapter = adapter;
16
+ }
17
+
18
+ async set(key: string, value: any, options: CacheSetOptions = {}) : Promise<void> {
19
+ await this.adapter.set(key, value, options);
20
+ }
21
+
22
+ async get(key: string) : Promise<any | undefined> {
23
+ return this.adapter.get(key);
24
+ }
25
+
26
+ async has(key: string) : Promise<boolean> {
27
+ return this.adapter.has(key);
28
+ }
29
+
30
+ async drop(key: string) : Promise<void> {
31
+ return this.adapter.drop(key);
32
+ }
33
+
34
+ async dropMany(keys: string[]) : Promise<void> {
35
+ return this.adapter.dropMany(keys);
36
+ }
37
+
38
+ async clear(options: CacheClearOptions = {}) : Promise<void> {
39
+ return this.adapter.clear(options);
40
+ }
41
+ }
@@ -0,0 +1,30 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { singa } from 'singa';
9
+ import { isRedisClientUsable } from '../redis';
10
+ import type { CacheAdapter } from './adapters';
11
+ import { MemoryCacheAdapter, RedisCacheAdapter } from './adapters';
12
+ import { Cache } from './module';
13
+
14
+ const instance = singa<Cache>({
15
+ name: 'cache',
16
+ factory: () => {
17
+ let adapter : CacheAdapter;
18
+ if (isRedisClientUsable()) {
19
+ adapter = new RedisCacheAdapter();
20
+ } else {
21
+ adapter = new MemoryCacheAdapter();
22
+ }
23
+
24
+ return new Cache(adapter);
25
+ },
26
+ });
27
+
28
+ export function useCache() {
29
+ return instance.use();
30
+ }
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export type CacheKeyBuildOptions = {
9
+ key: string,
10
+ prefix?: string,
11
+ suffix?: string
12
+ };
13
+
14
+ export type CacheSetOptions = {
15
+ /**
16
+ * Time to live in milliseconds (ms).
17
+ */
18
+ ttl?: number
19
+ };
20
+
21
+ export type CacheClearOptions = {
22
+ prefix?: string,
23
+ suffix?: string
24
+ };
@@ -7,6 +7,7 @@
7
7
 
8
8
  export * from './amqp';
9
9
  export * from './authup';
10
+ export * from './cache';
10
11
  export * from './logger';
11
12
  export * from './redis';
12
13
  export * from './vault';
@@ -5,25 +5,26 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
- import { MemoryCache } from '../../src';
8
+ import { Cache, MemoryCacheAdapter } from '../../src';
9
9
 
10
10
  describe('memory-cache', () => {
11
- it('should support operations (set,get,has,del)', () => {
12
- const cache = new MemoryCache();
13
- cache.set('foo', { bar: 'baz' }, {
11
+ it('should support operations (set,get,has,del)', async () => {
12
+ const adapter = new MemoryCacheAdapter();
13
+ const cache = new Cache(adapter);
14
+ await cache.set('foo', { bar: 'baz' }, {
14
15
  ttl: 1000 * 60 * 15,
15
16
  });
16
17
 
17
- expect(cache.has('foo')).toBeTruthy();
18
+ expect(await cache.has('foo')).toBeTruthy();
18
19
 
19
- let value = cache.get('foo');
20
+ let value = await cache.get('foo');
20
21
  expect(value).toEqual({ bar: 'baz' });
21
22
 
22
- cache.del('foo');
23
+ await cache.drop('foo');
23
24
 
24
- expect(cache.has('foo')).toBeFalsy();
25
+ expect(await cache.has('foo')).toBeFalsy();
25
26
 
26
- value = cache.get('foo');
27
+ value = await cache.get('foo');
27
28
  expect(value).toBeUndefined();
28
29
  });
29
30
  });
@@ -1,3 +0,0 @@
1
- export * from './module';
2
- export * from './singleton';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/memory-cache/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC"}
@@ -1,15 +0,0 @@
1
- import TTLCache from '@isaacs/ttlcache';
2
- type BlockerOptions = {
3
- ttl: number;
4
- };
5
- export declare class MemoryCache<KEY extends string = string, VALUE = any> {
6
- protected options: BlockerOptions;
7
- protected cache: TTLCache<KEY, VALUE>;
8
- constructor(options?: Partial<BlockerOptions>);
9
- set(id: KEY, value: VALUE, options: BlockerOptions): void;
10
- get(id: KEY): VALUE | undefined;
11
- del(id: KEY): void;
12
- has(id: KEY): boolean;
13
- }
14
- export {};
15
- //# sourceMappingURL=module.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/core/memory-cache/module.ts"],"names":[],"mappings":"AAOA,OAAO,QAAQ,MAAM,kBAAkB,CAAC;AAExC,KAAK,cAAc,GAAG;IAClB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,qBAAa,WAAW,CACpB,GAAG,SAAS,MAAM,GAAG,MAAM,EAC3B,KAAK,GAAG,GAAG;IAEX,SAAS,CAAC,OAAO,EAAG,cAAc,CAAC;IAEnC,SAAS,CAAC,KAAK,EAAG,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;gBAE3B,OAAO,GAAE,OAAO,CAAC,cAAc,CAAM;IASjD,GAAG,CAAC,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,cAAc;IAMlD,GAAG,CAAC,EAAE,EAAE,GAAG,GAAG,KAAK,GAAG,SAAS;IAI/B,GAAG,CAAC,EAAE,EAAE,GAAG,GAAI,IAAI;IAInB,GAAG,CAAC,EAAE,EAAE,GAAG,GAAI,OAAO;CAGzB"}
@@ -1,3 +0,0 @@
1
- import { MemoryCache } from './module';
2
- export declare function useMemoryCache(): MemoryCache<string, any>;
3
- //# sourceMappingURL=singleton.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../../src/core/memory-cache/singleton.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAOvC,wBAAgB,cAAc,6BAE7B"}
@@ -1,48 +0,0 @@
1
- /*
2
- * Copyright (c) 2024.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- import TTLCache from '@isaacs/ttlcache';
9
-
10
- type BlockerOptions = {
11
- ttl: number;
12
- };
13
-
14
- export class MemoryCache<
15
- KEY extends string = string,
16
- VALUE = any,
17
- > {
18
- protected options : BlockerOptions;
19
-
20
- protected cache : TTLCache<KEY, VALUE>;
21
-
22
- constructor(options: Partial<BlockerOptions> = {}) {
23
- this.options = {
24
- // 60 seconds
25
- ttl: 1000 * 60,
26
- ...options,
27
- };
28
- this.cache = new TTLCache<KEY, VALUE>();
29
- }
30
-
31
- set(id: KEY, value: VALUE, options: BlockerOptions) {
32
- this.cache.set(id, value, {
33
- ttl: options.ttl,
34
- });
35
- }
36
-
37
- get(id: KEY): VALUE | undefined {
38
- return this.cache.get(id);
39
- }
40
-
41
- del(id: KEY) : void {
42
- this.cache.delete(id);
43
- }
44
-
45
- has(id: KEY) : boolean {
46
- return this.cache.has(id);
47
- }
48
- }
@@ -1,18 +0,0 @@
1
- /*
2
- * Copyright (c) 2024.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- import { singa } from 'singa';
9
- import { MemoryCache } from './module';
10
-
11
- const instance = singa<MemoryCache>({
12
- name: 'queueRouter',
13
- factory: () => new MemoryCache(),
14
- });
15
-
16
- export function useMemoryCache() {
17
- return instance.use();
18
- }