@appconda/nextjs 1.0.34 → 1.0.35

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 (155) hide show
  1. package/dist/decorators/Cache.js +1 -1
  2. package/dist/decorators/Invalidate.js +1 -1
  3. package/dist/{Cache → lib/Cache}/Adapters/Filesystem.js +3 -3
  4. package/dist/{Cache → lib/Cache}/Adapters/Memory.js +3 -3
  5. package/dist/{Cache → lib/Cache}/Adapters/None.js +3 -3
  6. package/dist/{Cache → lib/Cache}/Adapters/Sharding.js +3 -3
  7. package/dist/{Cache → lib/Cache}/index.d.ts +0 -1
  8. package/dist/{Cache → lib/Cache}/index.js +0 -1
  9. package/{types → dist/lib}/Services.d.ts +1 -1
  10. package/dist/lib/Services.js +13 -0
  11. package/dist/services/node.js +16 -0
  12. package/package.json +2 -2
  13. package/src/decorators/Cache.ts +103 -0
  14. package/src/decorators/CacheKey.ts +9 -0
  15. package/src/decorators/Invalidate.ts +69 -0
  16. package/{types/Cache/Adapter.d.ts → src/lib/Cache/Adapter.ts} +3 -2
  17. package/src/lib/Cache/Adapters/Filesystem.ts +126 -0
  18. package/src/lib/Cache/Adapters/Memory.ts +58 -0
  19. package/src/lib/Cache/Adapters/None.ts +36 -0
  20. package/src/lib/Cache/Adapters/Sharding.ts +88 -0
  21. package/src/lib/Cache/Cache.ts +75 -0
  22. package/{types/Cache/index.d.ts → src/lib/Cache/index.ts} +1 -2
  23. package/src/lib/Services.ts +25 -0
  24. package/src/services/node.ts +2 -0
  25. package/dist/Cache/Adapters/Filesystem.d.ts +0 -16
  26. package/dist/Cache/Adapters/Memory.d.ts +0 -18
  27. package/dist/Cache/Adapters/None.d.ts +0 -12
  28. package/dist/Cache/Adapters/Redis.d.ts +0 -14
  29. package/dist/Cache/Adapters/Redis.js +0 -79
  30. package/dist/Services.d.ts +0 -7
  31. package/dist/Services.js +0 -29
  32. package/dist/services/waitlist.d.ts +0 -5
  33. package/dist/services/waitlist.js +0 -11
  34. package/types/Cache/Adapters/Redis.d.ts +0 -14
  35. package/types/Cache/Adapters/Sharding.d.ts +0 -17
  36. package/types/Cache/Cache.d.ts +0 -16
  37. package/types/actions/actionClient.d.ts +0 -4
  38. package/types/actions/index.d.ts +0 -1
  39. package/types/actions/nodes.d.ts +0 -4
  40. package/types/client.d.ts +0 -141
  41. package/types/decorators/Cache.d.ts +0 -1
  42. package/types/decorators/CacheKey.d.ts +0 -1
  43. package/types/decorators/Invalidate.d.ts +0 -1
  44. package/types/enums/api-service.d.ts +0 -13
  45. package/types/enums/api.d.ts +0 -5
  46. package/types/enums/auth-method.d.ts +0 -9
  47. package/types/enums/authentication-factor.d.ts +0 -6
  48. package/types/enums/authenticator-type.d.ts +0 -3
  49. package/types/enums/browser.d.ts +0 -16
  50. package/types/enums/compression.d.ts +0 -5
  51. package/types/enums/credit-card.d.ts +0 -18
  52. package/types/enums/database-usage-range.d.ts +0 -5
  53. package/types/enums/email-template-locale.d.ts +0 -133
  54. package/types/enums/email-template-type.d.ts +0 -9
  55. package/types/enums/entities/EntityLimitType.d.ts +0 -4
  56. package/types/enums/entities/PropertyAttributeName.d.ts +0 -27
  57. package/types/enums/entities/PropertyCondition.d.ts +0 -7
  58. package/types/enums/entities/PropertyType.d.ts +0 -12
  59. package/types/enums/entities/PropertyValueType.d.ts +0 -6
  60. package/types/enums/entities/RowAccess.d.ts +0 -2
  61. package/types/enums/entities/ViewFilterCondition.d.ts +0 -12
  62. package/types/enums/execution-method.d.ts +0 -8
  63. package/types/enums/flag.d.ts +0 -197
  64. package/types/enums/function-usage-range.d.ts +0 -5
  65. package/types/enums/image-format.d.ts +0 -7
  66. package/types/enums/image-gravity.d.ts +0 -11
  67. package/types/enums/index-type.d.ts +0 -5
  68. package/types/enums/messaging-provider-type.d.ts +0 -5
  69. package/types/enums/name.d.ts +0 -14
  70. package/types/enums/o-auth-provider copy.d.ts +0 -41
  71. package/types/enums/o-auth-provider.d.ts +0 -41
  72. package/types/enums/password-hash.d.ts +0 -13
  73. package/types/enums/platform-type.d.ts +0 -17
  74. package/types/enums/project-usage-range.d.ts +0 -4
  75. package/types/enums/region.d.ts +0 -4
  76. package/types/enums/relation-mutate.d.ts +0 -5
  77. package/types/enums/relationship-type.d.ts +0 -6
  78. package/types/enums/resource-type.d.ts +0 -4
  79. package/types/enums/runtime.d.ts +0 -48
  80. package/types/enums/s-m-t-p-secure.d.ts +0 -3
  81. package/types/enums/shared/ApplicationLayout.d.ts +0 -4
  82. package/types/enums/shared/Colors.d.ts +0 -24
  83. package/types/enums/shared/InputType.d.ts +0 -6
  84. package/types/enums/shared/Periodicity.d.ts +0 -7
  85. package/types/enums/shared/SvgIcon.d.ts +0 -37
  86. package/types/enums/shared/Theme.d.ts +0 -4
  87. package/types/enums/sms-template-locale.d.ts +0 -133
  88. package/types/enums/sms-template-type.d.ts +0 -6
  89. package/types/enums/smtp-encryption.d.ts +0 -5
  90. package/types/enums/storage-usage-range.d.ts +0 -5
  91. package/types/enums/subscriptions/PricingModel.d.ts +0 -11
  92. package/types/enums/subscriptions/SubscriptionBillingPeriod.d.ts +0 -10
  93. package/types/enums/subscriptions/SubscriptionFeatureLimitType.d.ts +0 -10
  94. package/types/enums/subscriptions/SubscriptionPriceType.d.ts +0 -7
  95. package/types/enums/tenants/LinkedAccountStatus.d.ts +0 -5
  96. package/types/enums/tenants/TenantUserJoined.d.ts +0 -6
  97. package/types/enums/tenants/TenantUserStatus.d.ts +0 -6
  98. package/types/enums/tenants/TenantUserType.d.ts +0 -5
  99. package/types/enums/user-usage-range.d.ts +0 -5
  100. package/types/getAppcondaClient.d.ts +0 -2
  101. package/types/getSDKForCurrentUser.d.ts +0 -33
  102. package/types/id.d.ts +0 -20
  103. package/types/index.d.ts +0 -39
  104. package/types/inputFile.d.ts +0 -6
  105. package/types/lib/Registry/Registry.d.ts +0 -38
  106. package/types/lib/Registry/index.d.ts +0 -1
  107. package/types/models.d.ts +0 -3272
  108. package/types/permission.d.ts +0 -43
  109. package/types/query.d.ts +0 -194
  110. package/types/role.d.ts +0 -70
  111. package/types/service-client.d.ts +0 -7
  112. package/types/service.d.ts +0 -11
  113. package/types/services/account.d.ts +0 -530
  114. package/types/services/acl.d.ts +0 -28
  115. package/types/services/applets.d.ts +0 -9
  116. package/types/services/assistant.d.ts +0 -14
  117. package/types/services/avatars.d.ts +0 -115
  118. package/types/services/community.d.ts +0 -19
  119. package/types/services/configuration.d.ts +0 -5
  120. package/types/services/console.d.ts +0 -15
  121. package/types/services/databases.d.ts +0 -613
  122. package/types/services/functions.d.ts +0 -319
  123. package/types/services/graphql.d.ts +0 -25
  124. package/types/services/health.d.ts +0 -231
  125. package/types/services/locale.d.ts +0 -80
  126. package/types/services/messaging.d.ts +0 -685
  127. package/types/services/migrations.d.ts +0 -185
  128. package/types/services/node.d.ts +0 -5
  129. package/types/services/permissions.d.ts +0 -20
  130. package/types/services/pricing.d.ts +0 -15
  131. package/types/services/project.d.ts +0 -70
  132. package/types/services/projects.d.ts +0 -542
  133. package/types/services/proxy.d.ts +0 -59
  134. package/types/services/roles.d.ts +0 -19
  135. package/types/services/schema.d.ts +0 -17
  136. package/types/services/storage.d.ts +0 -189
  137. package/types/services/subscription-product.d.ts +0 -77
  138. package/types/services/subscription.d.ts +0 -15
  139. package/types/services/teams.d.ts +0 -167
  140. package/types/services/tenant-subscription.d.ts +0 -12
  141. package/types/services/tenant.d.ts +0 -32
  142. package/types/services/users.d.ts +0 -499
  143. package/types/services/vcs.d.ts +0 -108
  144. package/types/services/waitlist.d.ts +0 -5
  145. package/dist/{Cache → lib/Cache}/Adapter.d.ts +0 -0
  146. package/dist/{Cache → lib/Cache}/Adapter.js +0 -0
  147. package/{types → dist/lib}/Cache/Adapters/Filesystem.d.ts +1 -1
  148. package/{types → dist/lib}/Cache/Adapters/Memory.d.ts +1 -1
  149. package/{types → dist/lib}/Cache/Adapters/None.d.ts +1 -1
  150. package/dist/{Cache → lib/Cache}/Adapters/Sharding.d.ts +1 -1
  151. /package/dist/{Cache → lib/Cache}/Cache.d.ts +0 -0
  152. /package/dist/{Cache → lib/Cache}/Cache.js +0 -0
  153. /package/dist/{Cache → lib/Cache}/test.d.ts +0 -0
  154. /package/dist/{Cache → lib/Cache}/test.js +0 -0
  155. /package/{types/Cache/test.d.ts → src/lib/Cache/test.ts} +0 -0
@@ -1,4 +1,4 @@
1
- import { Services } from '../Services';
1
+ import { Services } from "lib/Services";
2
2
  function stableStringify(obj) {
3
3
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
4
4
  return `{${Object.keys(obj).sort().map(key => `"${key}":${stableStringify(obj[key])}`).join(",")}}`;
@@ -1,4 +1,4 @@
1
- import { Services } from '../Services';
1
+ import { Services } from "lib/Services";
2
2
  export function Invalidate(keys) {
3
3
  return function (target, propertyKey, descriptor) {
4
4
  const originalMethod = descriptor.value;
@@ -6,9 +6,6 @@ export class Filesystem {
6
6
  this.path = '';
7
7
  this.path = path;
8
8
  }
9
- delWithStart(pattern) {
10
- throw new Error("Method not implemented.");
11
- }
12
9
  async load(key, ttl, hash = '') {
13
10
  const file = this.getPath(key);
14
11
  if (fs.existsSync(file) && (fs.statSync(file).mtime.getTime() + ttl * 1000 > Date.now())) {
@@ -99,4 +96,7 @@ export class Filesystem {
99
96
  }
100
97
  return fs.rmdirSync(dirPath);
101
98
  }
99
+ async delWithStart(pattern) {
100
+ return 0;
101
+ }
102
102
  }
@@ -2,9 +2,6 @@ export class Memory {
2
2
  constructor() {
3
3
  this.store = {};
4
4
  }
5
- delWithStart(pattern) {
6
- throw new Error("Method not implemented.");
7
- }
8
5
  async load(key, ttl, hash = '') {
9
6
  if (key && this.store[key]) {
10
7
  const saved = this.store[key];
@@ -43,4 +40,7 @@ export class Memory {
43
40
  async getSize() {
44
41
  return Object.keys(this.store).length;
45
42
  }
43
+ async delWithStart(pattern) {
44
+ return 0;
45
+ }
46
46
  }
@@ -1,8 +1,5 @@
1
1
  export class None {
2
2
  constructor() { }
3
- delWithStart(pattern) {
4
- throw new Error("Method not implemented.");
5
- }
6
3
  async load(key, ttl, hash = '') {
7
4
  return false;
8
5
  }
@@ -24,4 +21,7 @@ export class None {
24
21
  async getSize() {
25
22
  return 0;
26
23
  }
24
+ async delWithStart(pattern) {
25
+ return 0;
26
+ }
27
27
  }
@@ -19,9 +19,6 @@ export class Sharding {
19
19
  this.count = adapters.length;
20
20
  this.adapters = adapters;
21
21
  }
22
- delWithStart(pattern) {
23
- throw new Error("Method not implemented.");
24
- }
25
22
  async load(key, ttl, hash = '') {
26
23
  const adapter = this.getAdapter(key);
27
24
  return await adapter.load(key, ttl, hash);
@@ -69,4 +66,7 @@ export class Sharding {
69
66
  }
70
67
  return (crc ^ (-1)) >>> 0;
71
68
  }
69
+ async delWithStart(pattern) {
70
+ return 0;
71
+ }
72
72
  }
@@ -1,5 +1,4 @@
1
1
  export * from './Cache';
2
2
  export * from './Adapters/None';
3
- export * from './Adapters/Redis';
4
3
  export * from './Adapters/Sharding';
5
4
  export * from './Adapters/Memory';
@@ -1,5 +1,4 @@
1
1
  export * from './Cache';
2
2
  export * from './Adapters/None';
3
- export * from './Adapters/Redis';
4
3
  export * from './Adapters/Sharding';
5
4
  export * from './Adapters/Memory';
@@ -1,5 +1,5 @@
1
- import { Registry } from "./lib/Registry";
2
1
  import { Cache } from "./Cache";
2
+ import { Registry } from "./Registry";
3
3
  export declare const registry: Registry;
4
4
  export declare class Services {
5
5
  static get Cache(): Cache;
@@ -0,0 +1,13 @@
1
+ import { Cache, Memory } from "./Cache";
2
+ import { Registry } from "./Registry";
3
+ export const registry = new Registry();
4
+ registry.set('cache', () => {
5
+ const memoryAdapter = new Memory();
6
+ const cacheProvider = new Cache(memoryAdapter);
7
+ return cacheProvider;
8
+ });
9
+ export class Services {
10
+ static get Cache() {
11
+ return registry.get('cache');
12
+ }
13
+ }
@@ -1,3 +1,13 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Cache } from "decorators/Cache";
1
11
  import { ServiceClient } from "../service-client";
2
12
  export class Node extends ServiceClient {
3
13
  getServiceName() {
@@ -8,3 +18,9 @@ export class Node extends ServiceClient {
8
18
  return await this.actionCall('GetAllNodes', payload);
9
19
  }
10
20
  }
21
+ __decorate([
22
+ Cache(),
23
+ __metadata("design:type", Function),
24
+ __metadata("design:paramtypes", [Boolean]),
25
+ __metadata("design:returntype", Promise)
26
+ ], Node.prototype, "GetAllNodes", null);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@appconda/nextjs",
3
3
  "homepage": "https://appconda.io/support",
4
4
  "description": "Appconda is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API",
5
- "version": "1.0.34",
5
+ "version": "1.0.35",
6
6
  "license": "BSD-3-Clause",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/appconda/sdk-for-console"
12
12
  },
13
13
  "scripts": {
14
- "tsc-build": "tsc"
14
+ "build": "tsc"
15
15
  },
16
16
  "devDependencies": {
17
17
  "@rollup/plugin-typescript": "8.3.2",
@@ -0,0 +1,103 @@
1
+ import { Services } from "lib/Services";
2
+
3
+
4
+ function stableStringify(obj: any): string {
5
+ if (obj && typeof obj === "object" && !Array.isArray(obj)) {
6
+ return `{${Object.keys(obj).sort().map(key => `"${key}":${stableStringify(obj[key])}`).join(",")}}`;
7
+ }
8
+ return JSON.stringify(obj);
9
+ }
10
+
11
+ function generateCacheKey(...args: any[]): string {
12
+ return stableStringify(args);
13
+ }
14
+
15
+
16
+ // console.log(generateCacheKey("user", 123, true, { role: "admin", permissions: ["read", "write"] }));
17
+ // console.log(generateCacheKey("user", 123, true, { permissions: ["read", "write"], role: "admin" }));
18
+
19
+
20
+
21
+ export function Cache(/* cacheKey?: string | string[] */) {
22
+ return function (
23
+ target: any,
24
+ propertyKey: string,
25
+ descriptor: PropertyDescriptor
26
+ ) {
27
+ const originalMethod = descriptor.value;
28
+
29
+ descriptor.value = async function (...args: any[]) {
30
+
31
+ const className = target.constructor.name.toLowerCase();
32
+
33
+ // Metod adı
34
+ const methodName = propertyKey.toLowerCase();
35
+
36
+ const cacheKeys: any[] = [target.__cacheKeys || []].reverse();
37
+ console.log("Defined cache keys:", cacheKeys);
38
+
39
+ const cacheKey = ['admin', className, propertyKey];
40
+
41
+ if (cacheKeys.length > 0) {
42
+ for (let i = 0; i < cacheKeys.length; i++) {
43
+ const keyIndex = cacheKeys[i]['parameterIndex'];
44
+ if (args != null && args[keyIndex] != null) {
45
+ const key = args[keyIndex].toString();
46
+ cacheKey.push(key);
47
+ }
48
+ }
49
+ } else if (args.length > 0) { //cacheKey belirtilmemisse tum parametreleri kullan
50
+ for (let i = 0; i < args.length; i++) {
51
+ const key = args[i].toString();
52
+ cacheKey.push(key);
53
+ }
54
+ }
55
+
56
+ // Cache key'i dizeye dönüştür
57
+ // const key = Array.isArray(cacheKey) ? cacheKey.join(':') : cacheKey;
58
+
59
+ // Cache'ten veri al
60
+ let cachedData = await Services.Cache.load(cacheKey);
61
+
62
+ if (cachedData != null) {
63
+ return cachedData; // Cache'teki veriyi döndür
64
+ }
65
+
66
+ // Orijinal metodu çağır ve dönen değeri cache'e kaydet
67
+ const result = await originalMethod.apply(this, args);
68
+
69
+ // Cache'e kaydet (asenkron yapılır, beklenmez)
70
+ Services.Cache.save(cacheKey, result);
71
+ console.log('------------------------------Cache executed..')
72
+ return result;
73
+ };
74
+
75
+ return descriptor;
76
+ };
77
+ }
78
+
79
+
80
+ /* const cacheKeys: any[] = target.__cacheKeys || [];
81
+ console.log("Defined cache keys:", [...cacheKeys].reverse());
82
+
83
+ const className = target.constructor.name.toLowerCase();
84
+
85
+ // Metod adı
86
+ const methodName = propertyKey.toLowerCase();
87
+
88
+ debugger
89
+ const cacheKey = ['admin', className, propertyKey];
90
+ if (cacheKey.length > 0) {
91
+ for (let i = 0; i < cacheKeys.length; i++) {
92
+ const keyIndex = cacheKeys[i]['parameterIndex'];
93
+ if (args != null && args[keyIndex] != null) {
94
+ const key = args[keyIndex].toString();
95
+ cacheKey.push(key);
96
+ }
97
+ }
98
+ } else if (args.length > 0) { //cacheKey belirtilmemisse tum parametreleri kullan
99
+ for (let i = 0; i < args.length; i++) {
100
+ const key = args[i].toString();
101
+ cacheKey.push(key);
102
+ }
103
+ } */
@@ -0,0 +1,9 @@
1
+
2
+ export function CacheKey() {
3
+ return function (target: any, propertyKey: string | symbol, parameterIndex: number): void {
4
+ if (!target.__cacheKeys) {
5
+ target.__cacheKeys = [];
6
+ }
7
+ target.__cacheKeys.push({ propertyKey, parameterIndex });
8
+ };
9
+ }
@@ -0,0 +1,69 @@
1
+ import { Services } from "lib/Services";
2
+
3
+
4
+ export function Invalidate(keys: string | string[]) {
5
+ return function (
6
+ target: any,
7
+ propertyKey: string,
8
+ descriptor: PropertyDescriptor
9
+ ) {
10
+ const originalMethod = descriptor.value;
11
+
12
+ descriptor.value = async function (...args: any[]) {
13
+
14
+ const className = target.constructor.name.toLowerCase();
15
+
16
+ // Metod adı
17
+ const methodName = propertyKey.toLowerCase();
18
+
19
+
20
+
21
+ const cacheKey = ['admin', className];
22
+
23
+
24
+
25
+
26
+ // Orijinal metodu çağır ve dönen değeri cache'e kaydet
27
+ const result = await originalMethod.apply(this, args);
28
+
29
+ // Cache'e kaydet (asenkron yapılır, beklenmez)
30
+
31
+ for (let key of keys) {
32
+ Services.Cache.purge([...cacheKey, key]);
33
+ Services.Cache.delWithStart([...cacheKey, key]);
34
+ }
35
+
36
+
37
+ console.log('------------------------------Cache deleted.')
38
+ return result;
39
+ };
40
+
41
+ return descriptor;
42
+ };
43
+ }
44
+
45
+
46
+ /* const cacheKeys: any[] = target.__cacheKeys || [];
47
+ console.log("Defined cache keys:", [...cacheKeys].reverse());
48
+
49
+ const className = target.constructor.name.toLowerCase();
50
+
51
+ // Metod adı
52
+ const methodName = propertyKey.toLowerCase();
53
+
54
+ debugger
55
+ const cacheKey = ['admin', className, propertyKey];
56
+ if (cacheKey.length > 0) {
57
+ for (let i = 0; i < cacheKeys.length; i++) {
58
+ const keyIndex = cacheKeys[i]['parameterIndex'];
59
+ if (args != null && args[keyIndex] != null) {
60
+ const key = args[keyIndex].toString();
61
+ cacheKey.push(key);
62
+ }
63
+ }
64
+ } else if (args.length > 0) { //cacheKey belirtilmemisse tum parametreleri kullan
65
+ for (let i = 0; i < args.length; i++) {
66
+ const key = args[i].toString();
67
+ cacheKey.push(key);
68
+ }
69
+ } */
@@ -1,3 +1,4 @@
1
+
1
2
  export interface Adapter {
2
3
  load(key: string, ttl: number, hash?: string): Promise<any>;
3
4
  save(key: string, data: any, hash?: string): Promise<boolean | string | any[]>;
@@ -6,5 +7,5 @@ export interface Adapter {
6
7
  flush(): Promise<boolean>;
7
8
  ping(): Promise<boolean>;
8
9
  getSize(): Promise<number>;
9
- delWithStart(pattern: string): Promise<number>;
10
- }
10
+ delWithStart(pattern: string): Promise<number>;
11
+ }
@@ -0,0 +1,126 @@
1
+ import { Adapter } from "../Adapter";
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const glob = require('glob');
5
+
6
+ export class Filesystem implements Adapter {
7
+ protected path: string = '';
8
+
9
+ constructor(path: string) {
10
+ this.path = path;
11
+ }
12
+
13
+ async load(key: string, ttl: number, hash: string = ''): Promise<any> {
14
+ const file = this.getPath(key);
15
+
16
+ if (fs.existsSync(file) && (fs.statSync(file).mtime.getTime() + ttl * 1000 > Date.now())) {
17
+ return fs.readFileSync(file, 'utf8');
18
+ }
19
+
20
+ return false;
21
+ }
22
+
23
+ async save(key: string, data: any, hash: string = ''): Promise<boolean | string | any[]> {
24
+ if (!data) {
25
+ return false;
26
+ }
27
+
28
+ const file = this.getPath(key);
29
+ const dir = path.dirname(file);
30
+
31
+ try {
32
+ if (!fs.existsSync(dir)) {
33
+ fs.mkdirSync(dir, { recursive: true });
34
+ }
35
+
36
+ fs.writeFileSync(file, data, { flag: 'w' });
37
+ return data;
38
+ } catch (e: any) {
39
+ throw new Error(e.message);
40
+ }
41
+ }
42
+
43
+ async list(key: string): Promise<string[]> {
44
+ return [];
45
+ }
46
+
47
+ async purge(key: string, hash: string = ''): Promise<boolean> {
48
+ const file = this.getPath(key);
49
+
50
+ if (fs.existsSync(file)) {
51
+ fs.unlinkSync(file);
52
+ return true;
53
+ }
54
+
55
+ return false;
56
+ }
57
+
58
+ async flush(): Promise<boolean> {
59
+ return this.deleteDirectory(this.path);
60
+ }
61
+
62
+ async ping(): Promise<boolean> {
63
+ return fs.existsSync(this.path) && fs.accessSync(this.path, fs.constants.W_OK | fs.constants.R_OK);
64
+ }
65
+
66
+ async getSize(): Promise<number> {
67
+ try {
68
+ return this.getDirectorySize(path.dirname(this.path));
69
+ } catch {
70
+ return 0;
71
+ }
72
+ }
73
+
74
+ private getDirectorySize(dir: string): number {
75
+ let size = 0;
76
+ const normalizedPath = path.join(dir, '*');
77
+
78
+ const paths = glob.sync(normalizedPath, { nodir: false });
79
+ if (!paths) {
80
+ return size;
81
+ }
82
+
83
+ for (const p of paths) {
84
+ if (fs.statSync(p).isFile()) {
85
+ size += fs.statSync(p).size;
86
+ } else if (fs.statSync(p).isDirectory()) {
87
+ size += this.getDirectorySize(p);
88
+ }
89
+ }
90
+
91
+ return size;
92
+ }
93
+
94
+ getPath(filename: string): string {
95
+ return path.join(this.path, filename);
96
+ }
97
+
98
+ protected deleteDirectory(dirPath: string): boolean {
99
+ if (!fs.statSync(dirPath).isDirectory()) {
100
+ throw new Error(`${dirPath} must be a directory`);
101
+ }
102
+
103
+ if (!dirPath.endsWith('/')) {
104
+ dirPath += '/';
105
+ }
106
+
107
+ const files = glob.sync(dirPath + '*', { mark: true });
108
+
109
+ if (!files) {
110
+ throw new Error('Error happened during glob');
111
+ }
112
+
113
+ for (const file of files) {
114
+ if (fs.statSync(file).isDirectory()) {
115
+ this.deleteDirectory(file);
116
+ } else {
117
+ fs.unlinkSync(file);
118
+ }
119
+ }
120
+
121
+ return fs.rmdirSync(dirPath);
122
+ }
123
+ async delWithStart(pattern: string): Promise<number> {
124
+ return 0;
125
+ }
126
+ }
@@ -0,0 +1,58 @@
1
+ import { Adapter } from "../Adapter";
2
+
3
+ export class Memory implements Adapter {
4
+ public store: { [key: string]: { time: number, data: any } } = {};
5
+
6
+ constructor() {}
7
+
8
+ async load(key: string, ttl: number, hash: string = ''): Promise<any> {
9
+ if (key && this.store[key]) {
10
+ const saved = this.store[key];
11
+ return (saved.time + ttl > Date.now() / 1000) ? saved.data : false;
12
+ }
13
+ return false;
14
+ }
15
+
16
+ async save(key: string, data: any, hash: string = ''): Promise<boolean | string | any[]> {
17
+ if (!key || !data) {
18
+ return false;
19
+ }
20
+
21
+ const saved = {
22
+ time: Date.now() / 1000,
23
+ data: data,
24
+ };
25
+
26
+ this.store[key] = saved;
27
+ return data;
28
+ }
29
+
30
+ async list(key: string): Promise<string[]> {
31
+ return [];
32
+ }
33
+
34
+ async purge(key: string, hash: string = ''): Promise<boolean> {
35
+ if (key && this.store[key]) {
36
+ delete this.store[key];
37
+ return true;
38
+ }
39
+ return false;
40
+ }
41
+
42
+ async flush(): Promise<boolean> {
43
+ this.store = {};
44
+ return true;
45
+ }
46
+
47
+ async ping(): Promise<boolean> {
48
+ return true;
49
+ }
50
+
51
+ async getSize(): Promise<number> {
52
+ return Object.keys(this.store).length;
53
+ }
54
+
55
+ async delWithStart(pattern: string): Promise<number> {
56
+ return 0;
57
+ }
58
+ }
@@ -0,0 +1,36 @@
1
+ import { Adapter } from "../Adapter";
2
+
3
+ export class None implements Adapter {
4
+ constructor() {}
5
+
6
+ async load(key: string, ttl: number, hash: string = ''): Promise<any> {
7
+ return false;
8
+ }
9
+
10
+ async save(key: string, data: any, hash: string = ''): Promise<boolean | string | any[]> {
11
+ return false;
12
+ }
13
+
14
+ async list(key: string): Promise<string[]> {
15
+ return [];
16
+ }
17
+
18
+ async purge(key: string, hash: string = ''): Promise<boolean> {
19
+ return true;
20
+ }
21
+
22
+ async flush(): Promise<boolean> {
23
+ return true;
24
+ }
25
+
26
+ async ping(): Promise<boolean> {
27
+ return true;
28
+ }
29
+
30
+ async getSize(): Promise<number> {
31
+ return 0;
32
+ }
33
+ async delWithStart(pattern: string): Promise<number> {
34
+ return 0;
35
+ }
36
+ }
@@ -0,0 +1,88 @@
1
+ import { Adapter } from "../Adapter";
2
+
3
+ export class Sharding implements Adapter {
4
+ protected adapters: Adapter[];
5
+ protected count: number = 0;
6
+
7
+ constructor(adapters: Adapter[]) {
8
+ if (adapters.length === 0) {
9
+ throw new Error('No adapters provided');
10
+ }
11
+
12
+ this.count = adapters.length;
13
+ this.adapters = adapters;
14
+ }
15
+
16
+ async load(key: string, ttl: number, hash: string = ''): Promise<any> {
17
+ const adapter = this.getAdapter(key);
18
+ return await adapter.load(key, ttl, hash);
19
+ }
20
+
21
+ async save(key: string, data: any, hash: string = ''): Promise<boolean | string | any[]> {
22
+ return this.getAdapter(key).save(key, data, hash);
23
+ }
24
+
25
+ async list(key: string): Promise<string[]> {
26
+ return this.getAdapter(key).list(key);
27
+ }
28
+
29
+ async purge(key: string, hash: string = ''): Promise<boolean> {
30
+ return this.getAdapter(key).purge(key, hash);
31
+ }
32
+
33
+ async flush(): Promise<boolean> {
34
+ let result = true;
35
+ for (const adapter of this.adapters) {
36
+ result = await adapter.flush() ? result : false;
37
+ }
38
+ return result;
39
+ }
40
+
41
+ async ping(): Promise<boolean> {
42
+ for (const adapter of this.adapters) {
43
+ if (!adapter.ping()) {
44
+ return false;
45
+ }
46
+ }
47
+ return true;
48
+ }
49
+
50
+ async getSize(): Promise<number> {
51
+ let size = 0;
52
+ for (const adapter of this.adapters) {
53
+ size += await adapter.getSize();
54
+ }
55
+ return size;
56
+ }
57
+
58
+ protected getAdapter(key: string): Adapter {
59
+ const hash = this.crc32(key);
60
+ const index = hash % this.count;
61
+ return this.adapters[index];
62
+ }
63
+
64
+ private crc32(str: string): number {
65
+ let crc = 0 ^ (-1);
66
+ for (let i = 0; i < str.length; i++) {
67
+ crc = (crc >>> 8) ^ this.crc32Table[(crc ^ str.charCodeAt(i)) & 0xff];
68
+ }
69
+ return (crc ^ (-1)) >>> 0;
70
+ }
71
+
72
+ async delWithStart(pattern: string): Promise<number> {
73
+ return 0;
74
+ }
75
+
76
+ private crc32Table = (() => {
77
+ let c: number;
78
+ const table: number[] = [];
79
+ for (let n = 0; n < 256; n++) {
80
+ c = n;
81
+ for (let k = 0; k < 8; k++) {
82
+ c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
83
+ }
84
+ table[n] = c;
85
+ }
86
+ return table;
87
+ })();
88
+ }