@raketa-cloud/next 1.0.0 → 1.0.3
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/package.json +8 -5
- package/src/Cache.js +67 -8
- package/src/dxp/public/Articles.js +2 -1
- package/src/dxp/public/Events.js +2 -1
- package/src/dxp/public/Pages.js +2 -1
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raketa-cloud/next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"check-types": "tsc --noEmit"
|
|
6
|
+
"check-types": "tsc --noEmit",
|
|
7
|
+
"format": "prettier --write src/**/*.js"
|
|
7
8
|
},
|
|
8
9
|
"devDependencies": {
|
|
9
10
|
"@repo/typescript-config": "*",
|
|
@@ -11,12 +12,14 @@
|
|
|
11
12
|
"@types/node": "^20.11.24",
|
|
12
13
|
"@types/react": "18.3.0",
|
|
13
14
|
"@types/react-dom": "18.3.1",
|
|
15
|
+
"prettier": "^3.8.3",
|
|
14
16
|
"typescript": "5.5.4"
|
|
15
17
|
},
|
|
16
18
|
"dependencies": {
|
|
17
|
-
"react": "^19.0.0",
|
|
18
|
-
"react-dom": "^19.0.0",
|
|
19
19
|
"graphql-request": "7.1.2",
|
|
20
|
-
"lru-cache": "11.0.2"
|
|
20
|
+
"lru-cache": "11.0.2",
|
|
21
|
+
"object-sizeof": "^2.6.5",
|
|
22
|
+
"react": "^19.0.0",
|
|
23
|
+
"react-dom": "^19.0.0"
|
|
21
24
|
}
|
|
22
25
|
}
|
package/src/Cache.js
CHANGED
|
@@ -1,20 +1,49 @@
|
|
|
1
1
|
import { LRUCache } from "lru-cache";
|
|
2
|
+
import sizeof from "object-sizeof";
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
+
const NULL_SENTINEL = Symbol.for("raketa_cache_null");
|
|
5
|
+
|
|
6
|
+
function getKeySize(key) {
|
|
7
|
+
if (typeof key === 'string') {
|
|
8
|
+
// Account for UTF-16 encoding
|
|
9
|
+
return Buffer.from(key, 'utf16le').length;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof key === 'number') {
|
|
13
|
+
return 8; // 64-bit number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof key === 'object') {
|
|
17
|
+
return sizeof(key);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
4
22
|
|
|
5
23
|
export default class Cache {
|
|
6
24
|
constructor(opts = {}) {
|
|
7
25
|
const cacheOptions = {
|
|
8
|
-
max: opts.max || 5000,
|
|
9
|
-
ttl: opts.ttl || 1000 * 60 *
|
|
26
|
+
max: opts.max || 5000, // Items count
|
|
27
|
+
ttl: opts.ttl || 1000 * 60 * 5, // 5 minutes
|
|
28
|
+
maxSize: opts.maxSize || 500 * 1024 * 1024, // 500 MB
|
|
10
29
|
allowStale: true,
|
|
11
30
|
noDeleteOnStaleGet: true,
|
|
12
31
|
};
|
|
13
32
|
|
|
33
|
+
cacheOptions.sizeCalculation = (value, key) => {
|
|
34
|
+
const keySize = getKeySize(key);
|
|
35
|
+
|
|
36
|
+
if (value === null || value === undefined) {
|
|
37
|
+
return keySize;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (value === NULL_SENTINEL) return keySize + 1;
|
|
41
|
+
|
|
42
|
+
return keySize + sizeof(value);
|
|
43
|
+
};
|
|
44
|
+
|
|
14
45
|
this.debugMode = opts.debugMode || false;
|
|
15
|
-
this.cacheInstance = new LRUCache(
|
|
16
|
-
...cacheOptions,
|
|
17
|
-
});
|
|
46
|
+
this.cacheInstance = new LRUCache(cacheOptions);
|
|
18
47
|
}
|
|
19
48
|
|
|
20
49
|
_debug(msg) {
|
|
@@ -29,12 +58,22 @@ export default class Cache {
|
|
|
29
58
|
const cachedValue = this.cacheInstance.get(key, { status });
|
|
30
59
|
|
|
31
60
|
return {
|
|
32
|
-
cachedValue: cachedValue ===
|
|
61
|
+
cachedValue: cachedValue === NULL_SENTINEL ? null : cachedValue,
|
|
33
62
|
status,
|
|
34
63
|
};
|
|
35
64
|
}
|
|
36
65
|
|
|
37
66
|
_cloneObject(obj) {
|
|
67
|
+
if (obj === null || obj === undefined) {
|
|
68
|
+
return obj;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const type = typeof obj;
|
|
72
|
+
|
|
73
|
+
if (type !== "function" || type !== "obj") {
|
|
74
|
+
return obj;
|
|
75
|
+
}
|
|
76
|
+
|
|
38
77
|
// Ah the JS world! See the official docs https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy.
|
|
39
78
|
// Important note - this works only for objects with primitive types to copy. If an object property is
|
|
40
79
|
// a function - well it will not work correctly...
|
|
@@ -44,10 +83,30 @@ export default class Cache {
|
|
|
44
83
|
_setInCache(key, value) {
|
|
45
84
|
this.cacheInstance.set(
|
|
46
85
|
key,
|
|
47
|
-
value ? this._cloneObject(value) :
|
|
86
|
+
value ? this._cloneObject(value) : NULL_SENTINEL
|
|
48
87
|
);
|
|
49
88
|
}
|
|
50
89
|
|
|
90
|
+
clear() {
|
|
91
|
+
this.cacheInstance.clear();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
forEach(callback) {
|
|
95
|
+
this.cacheInstance.forEach(callback);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getRemainingTTL(key) {
|
|
99
|
+
return this.cacheInstance.getRemainingTTL(key);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get calculatedSize() {
|
|
103
|
+
return this.cacheInstance.calculatedSize;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
get maxSize() {
|
|
107
|
+
return this.cacheInstance.maxSize;
|
|
108
|
+
}
|
|
109
|
+
|
|
51
110
|
async fetch(cacheKey, fetcher) {
|
|
52
111
|
const { cachedValue, status } = this._fetchFromCache(cacheKey);
|
|
53
112
|
|
|
@@ -2,7 +2,7 @@ import DxpModel from "./DxpModel";
|
|
|
2
2
|
import { ArticleBySlugDocument, ArticlesDocument } from "./documents";
|
|
3
3
|
|
|
4
4
|
export default class Articles extends DxpModel {
|
|
5
|
-
async find(locale, slug, opts = {}) {
|
|
5
|
+
async find(locale, slug, resourceType = null, opts = {}) {
|
|
6
6
|
const response = await this.fetch(
|
|
7
7
|
`article-${locale}-${slug}`,
|
|
8
8
|
async () =>
|
|
@@ -12,6 +12,7 @@ export default class Articles extends DxpModel {
|
|
|
12
12
|
input: {
|
|
13
13
|
locale,
|
|
14
14
|
slug,
|
|
15
|
+
resourceType
|
|
15
16
|
},
|
|
16
17
|
},
|
|
17
18
|
opts
|
package/src/dxp/public/Events.js
CHANGED
|
@@ -2,7 +2,7 @@ import DxpModel from "./DxpModel";
|
|
|
2
2
|
import { EventBySlugDocument, EventsDocument } from "./documents";
|
|
3
3
|
|
|
4
4
|
export default class Events extends DxpModel {
|
|
5
|
-
async find(locale, slug, opts = {}) {
|
|
5
|
+
async find(locale, slug, resourceType = null, opts = {}) {
|
|
6
6
|
const response = await this.fetch(
|
|
7
7
|
`event-${locale}-${slug}`,
|
|
8
8
|
async () =>
|
|
@@ -12,6 +12,7 @@ export default class Events extends DxpModel {
|
|
|
12
12
|
input: {
|
|
13
13
|
locale,
|
|
14
14
|
slug,
|
|
15
|
+
resourceType
|
|
15
16
|
},
|
|
16
17
|
},
|
|
17
18
|
opts
|
package/src/dxp/public/Pages.js
CHANGED
|
@@ -2,7 +2,7 @@ import DxpModel from "./DxpModel";
|
|
|
2
2
|
import { PageBySlugDocument, PagesDocument } from "./documents";
|
|
3
3
|
|
|
4
4
|
export default class Pages extends DxpModel {
|
|
5
|
-
async find(locale, slug, opts = {}) {
|
|
5
|
+
async find(locale, slug, resourceType, opts = {}) {
|
|
6
6
|
const response = await this.fetch(
|
|
7
7
|
`page-${locale}-${slug}`,
|
|
8
8
|
async () =>
|
|
@@ -12,6 +12,7 @@ export default class Pages extends DxpModel {
|
|
|
12
12
|
input: {
|
|
13
13
|
locale,
|
|
14
14
|
slug,
|
|
15
|
+
resourceType
|
|
15
16
|
},
|
|
16
17
|
},
|
|
17
18
|
opts
|