@appconda/nextjs 1.0.33 → 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.
- package/dist/decorators/Cache.js +1 -1
- package/dist/decorators/Invalidate.js +1 -1
- package/dist/{Cache → lib/Cache}/Adapters/Filesystem.js +3 -3
- package/dist/{Cache → lib/Cache}/Adapters/Memory.js +3 -3
- package/dist/{Cache → lib/Cache}/Adapters/None.js +3 -3
- package/dist/{Cache → lib/Cache}/Adapters/Sharding.js +3 -3
- package/dist/{Cache → lib/Cache}/index.d.ts +0 -1
- package/dist/{Cache → lib/Cache}/index.js +0 -1
- package/{types → dist/lib}/Services.d.ts +1 -1
- package/dist/lib/Services.js +13 -0
- package/dist/modules/waitlist/service.js +1 -1
- package/dist/services/node.js +16 -0
- package/package.json +2 -2
- package/publish.sh +1 -1
- package/src/decorators/Cache.ts +103 -0
- package/src/decorators/CacheKey.ts +9 -0
- package/src/decorators/Invalidate.ts +69 -0
- package/{types/Cache/Adapter.d.ts → src/lib/Cache/Adapter.ts} +3 -2
- package/src/lib/Cache/Adapters/Filesystem.ts +126 -0
- package/src/lib/Cache/Adapters/Memory.ts +58 -0
- package/src/lib/Cache/Adapters/None.ts +36 -0
- package/src/lib/Cache/Adapters/Sharding.ts +88 -0
- package/src/lib/Cache/Cache.ts +75 -0
- package/{types/Cache/index.d.ts → src/lib/Cache/index.ts} +1 -2
- package/src/lib/Services.ts +25 -0
- package/src/modules/waitlist/action.ts +2 -0
- package/src/modules/waitlist/service.ts +1 -1
- package/src/services/node.ts +2 -0
- package/dist/Cache/Adapters/Filesystem.d.ts +0 -16
- package/dist/Cache/Adapters/Memory.d.ts +0 -18
- package/dist/Cache/Adapters/None.d.ts +0 -12
- package/dist/Cache/Adapters/Redis.d.ts +0 -14
- package/dist/Cache/Adapters/Redis.js +0 -79
- package/dist/Services.d.ts +0 -7
- package/dist/Services.js +0 -29
- package/dist/services/waitlist.d.ts +0 -5
- package/dist/services/waitlist.js +0 -11
- package/types/Cache/Adapters/Redis.d.ts +0 -14
- package/types/Cache/Adapters/Sharding.d.ts +0 -17
- package/types/Cache/Cache.d.ts +0 -16
- package/types/actions/actionClient.d.ts +0 -4
- package/types/actions/index.d.ts +0 -1
- package/types/actions/nodes.d.ts +0 -4
- package/types/client.d.ts +0 -141
- package/types/decorators/Cache.d.ts +0 -1
- package/types/decorators/CacheKey.d.ts +0 -1
- package/types/decorators/Invalidate.d.ts +0 -1
- package/types/enums/api-service.d.ts +0 -13
- package/types/enums/api.d.ts +0 -5
- package/types/enums/auth-method.d.ts +0 -9
- package/types/enums/authentication-factor.d.ts +0 -6
- package/types/enums/authenticator-type.d.ts +0 -3
- package/types/enums/browser.d.ts +0 -16
- package/types/enums/compression.d.ts +0 -5
- package/types/enums/credit-card.d.ts +0 -18
- package/types/enums/database-usage-range.d.ts +0 -5
- package/types/enums/email-template-locale.d.ts +0 -133
- package/types/enums/email-template-type.d.ts +0 -9
- package/types/enums/entities/EntityLimitType.d.ts +0 -4
- package/types/enums/entities/PropertyAttributeName.d.ts +0 -27
- package/types/enums/entities/PropertyCondition.d.ts +0 -7
- package/types/enums/entities/PropertyType.d.ts +0 -12
- package/types/enums/entities/PropertyValueType.d.ts +0 -6
- package/types/enums/entities/RowAccess.d.ts +0 -2
- package/types/enums/entities/ViewFilterCondition.d.ts +0 -12
- package/types/enums/execution-method.d.ts +0 -8
- package/types/enums/flag.d.ts +0 -197
- package/types/enums/function-usage-range.d.ts +0 -5
- package/types/enums/image-format.d.ts +0 -7
- package/types/enums/image-gravity.d.ts +0 -11
- package/types/enums/index-type.d.ts +0 -5
- package/types/enums/messaging-provider-type.d.ts +0 -5
- package/types/enums/name.d.ts +0 -14
- package/types/enums/o-auth-provider copy.d.ts +0 -41
- package/types/enums/o-auth-provider.d.ts +0 -41
- package/types/enums/password-hash.d.ts +0 -13
- package/types/enums/platform-type.d.ts +0 -17
- package/types/enums/project-usage-range.d.ts +0 -4
- package/types/enums/region.d.ts +0 -4
- package/types/enums/relation-mutate.d.ts +0 -5
- package/types/enums/relationship-type.d.ts +0 -6
- package/types/enums/resource-type.d.ts +0 -4
- package/types/enums/runtime.d.ts +0 -48
- package/types/enums/s-m-t-p-secure.d.ts +0 -3
- package/types/enums/shared/ApplicationLayout.d.ts +0 -4
- package/types/enums/shared/Colors.d.ts +0 -24
- package/types/enums/shared/InputType.d.ts +0 -6
- package/types/enums/shared/Periodicity.d.ts +0 -7
- package/types/enums/shared/SvgIcon.d.ts +0 -37
- package/types/enums/shared/Theme.d.ts +0 -4
- package/types/enums/sms-template-locale.d.ts +0 -133
- package/types/enums/sms-template-type.d.ts +0 -6
- package/types/enums/smtp-encryption.d.ts +0 -5
- package/types/enums/storage-usage-range.d.ts +0 -5
- package/types/enums/subscriptions/PricingModel.d.ts +0 -11
- package/types/enums/subscriptions/SubscriptionBillingPeriod.d.ts +0 -10
- package/types/enums/subscriptions/SubscriptionFeatureLimitType.d.ts +0 -10
- package/types/enums/subscriptions/SubscriptionPriceType.d.ts +0 -7
- package/types/enums/tenants/LinkedAccountStatus.d.ts +0 -5
- package/types/enums/tenants/TenantUserJoined.d.ts +0 -6
- package/types/enums/tenants/TenantUserStatus.d.ts +0 -6
- package/types/enums/tenants/TenantUserType.d.ts +0 -5
- package/types/enums/user-usage-range.d.ts +0 -5
- package/types/getAppcondaClient.d.ts +0 -2
- package/types/getSDKForCurrentUser.d.ts +0 -33
- package/types/id.d.ts +0 -20
- package/types/index.d.ts +0 -39
- package/types/inputFile.d.ts +0 -6
- package/types/lib/Registry/Registry.d.ts +0 -38
- package/types/lib/Registry/index.d.ts +0 -1
- package/types/models.d.ts +0 -3272
- package/types/permission.d.ts +0 -43
- package/types/query.d.ts +0 -194
- package/types/role.d.ts +0 -70
- package/types/service-client.d.ts +0 -7
- package/types/service.d.ts +0 -11
- package/types/services/account.d.ts +0 -530
- package/types/services/acl.d.ts +0 -28
- package/types/services/applets.d.ts +0 -9
- package/types/services/assistant.d.ts +0 -14
- package/types/services/avatars.d.ts +0 -115
- package/types/services/community.d.ts +0 -19
- package/types/services/configuration.d.ts +0 -5
- package/types/services/console.d.ts +0 -15
- package/types/services/databases.d.ts +0 -613
- package/types/services/functions.d.ts +0 -319
- package/types/services/graphql.d.ts +0 -25
- package/types/services/health.d.ts +0 -231
- package/types/services/locale.d.ts +0 -80
- package/types/services/messaging.d.ts +0 -685
- package/types/services/migrations.d.ts +0 -185
- package/types/services/node.d.ts +0 -5
- package/types/services/permissions.d.ts +0 -20
- package/types/services/pricing.d.ts +0 -15
- package/types/services/project.d.ts +0 -70
- package/types/services/projects.d.ts +0 -542
- package/types/services/proxy.d.ts +0 -59
- package/types/services/roles.d.ts +0 -19
- package/types/services/schema.d.ts +0 -17
- package/types/services/storage.d.ts +0 -189
- package/types/services/subscription-product.d.ts +0 -77
- package/types/services/subscription.d.ts +0 -15
- package/types/services/teams.d.ts +0 -167
- package/types/services/tenant-subscription.d.ts +0 -12
- package/types/services/tenant.d.ts +0 -32
- package/types/services/users.d.ts +0 -499
- package/types/services/vcs.d.ts +0 -108
- package/types/services/waitlist.d.ts +0 -5
- package/dist/{Cache → lib/Cache}/Adapter.d.ts +0 -0
- package/dist/{Cache → lib/Cache}/Adapter.js +0 -0
- package/{types → dist/lib}/Cache/Adapters/Filesystem.d.ts +1 -1
- package/{types → dist/lib}/Cache/Adapters/Memory.d.ts +1 -1
- package/{types → dist/lib}/Cache/Adapters/None.d.ts +1 -1
- package/dist/{Cache → lib/Cache}/Adapters/Sharding.d.ts +1 -1
- /package/dist/{Cache → lib/Cache}/Cache.d.ts +0 -0
- /package/dist/{Cache → lib/Cache}/Cache.js +0 -0
- /package/dist/{Cache → lib/Cache}/test.d.ts +0 -0
- /package/dist/{Cache → lib/Cache}/test.js +0 -0
- /package/{types/Cache/test.d.ts → src/lib/Cache/test.ts} +0 -0
package/dist/decorators/Cache.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Services } from
|
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(",")}}`;
|
@@ -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
|
}
|
@@ -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
|
+
}
|
package/dist/services/node.js
CHANGED
@@ -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.
|
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
|
-
"
|
14
|
+
"build": "tsc"
|
15
15
|
},
|
16
16
|
"devDependencies": {
|
17
17
|
"@rollup/plugin-typescript": "8.3.2",
|
package/publish.sh
CHANGED
@@ -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,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
|
-
|
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
|
+
}
|