@scayle/storefront-core 7.22.1
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/CHANGELOG.md +480 -0
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/bapi/init.cjs +30 -0
- package/dist/bapi/init.d.ts +14 -0
- package/dist/bapi/init.mjs +23 -0
- package/dist/cache/cache.cjs +1 -0
- package/dist/cache/cache.d.ts +8 -0
- package/dist/cache/cache.mjs +0 -0
- package/dist/cache/cached.cjs +89 -0
- package/dist/cache/cached.d.ts +24 -0
- package/dist/cache/cached.mjs +92 -0
- package/dist/cache/index.cjs +38 -0
- package/dist/cache/index.d.ts +3 -0
- package/dist/cache/index.mjs +3 -0
- package/dist/cache/providers/redis.cjs +94 -0
- package/dist/cache/providers/redis.d.ts +28 -0
- package/dist/cache/providers/redis.mjs +86 -0
- package/dist/cache/providers/unstorage.cjs +54 -0
- package/dist/cache/providers/unstorage.d.ts +16 -0
- package/dist/cache/providers/unstorage.mjs +49 -0
- package/dist/cache/types.cjs +11 -0
- package/dist/cache/types.d.ts +6 -0
- package/dist/cache/types.mjs +4 -0
- package/dist/constants/basket.cjs +13 -0
- package/dist/constants/basket.d.ts +8 -0
- package/dist/constants/basket.mjs +6 -0
- package/dist/constants/cache.cjs +8 -0
- package/dist/constants/cache.d.ts +1 -0
- package/dist/constants/cache.mjs +1 -0
- package/dist/constants/hash.cjs +11 -0
- package/dist/constants/hash.d.ts +6 -0
- package/dist/constants/hash.mjs +4 -0
- package/dist/constants/httpStatus.cjs +636 -0
- package/dist/constants/httpStatus.d.ts +632 -0
- package/dist/constants/httpStatus.mjs +629 -0
- package/dist/constants/index.cjs +93 -0
- package/dist/constants/index.d.ts +8 -0
- package/dist/constants/index.mjs +8 -0
- package/dist/constants/product.cjs +11 -0
- package/dist/constants/product.d.ts +6 -0
- package/dist/constants/product.mjs +4 -0
- package/dist/constants/promotion.cjs +10 -0
- package/dist/constants/promotion.d.ts +2 -0
- package/dist/constants/promotion.mjs +2 -0
- package/dist/constants/sorting.cjs +24 -0
- package/dist/constants/sorting.d.ts +20 -0
- package/dist/constants/sorting.mjs +17 -0
- package/dist/constants/withParameters.cjs +138 -0
- package/dist/constants/withParameters.d.ts +30 -0
- package/dist/constants/withParameters.mjs +132 -0
- package/dist/errors/BAPIError.cjs +16 -0
- package/dist/errors/BAPIError.d.ts +6 -0
- package/dist/errors/BAPIError.mjs +9 -0
- package/dist/errors/baseError.cjs +17 -0
- package/dist/errors/baseError.d.ts +5 -0
- package/dist/errors/baseError.mjs +10 -0
- package/dist/errors/index.cjs +27 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.mjs +2 -0
- package/dist/helpers/advancedAttributeHelpers.cjs +26 -0
- package/dist/helpers/advancedAttributeHelpers.d.ts +12 -0
- package/dist/helpers/advancedAttributeHelpers.mjs +21 -0
- package/dist/helpers/arrayHelpers.cjs +10 -0
- package/dist/helpers/arrayHelpers.d.ts +1 -0
- package/dist/helpers/arrayHelpers.mjs +3 -0
- package/dist/helpers/attributeHelpers.cjs +24 -0
- package/dist/helpers/attributeHelpers.d.ts +5 -0
- package/dist/helpers/attributeHelpers.mjs +17 -0
- package/dist/helpers/autobind.cjs +44 -0
- package/dist/helpers/autobind.d.ts +39 -0
- package/dist/helpers/autobind.mjs +35 -0
- package/dist/helpers/basketHelpers.cjs +12 -0
- package/dist/helpers/basketHelpers.d.ts +2 -0
- package/dist/helpers/basketHelpers.mjs +5 -0
- package/dist/helpers/categoryHelper.cjs +61 -0
- package/dist/helpers/categoryHelper.d.ts +7 -0
- package/dist/helpers/categoryHelper.mjs +44 -0
- package/dist/helpers/filterHelper.cjs +156 -0
- package/dist/helpers/filterHelper.d.ts +82 -0
- package/dist/helpers/filterHelper.mjs +147 -0
- package/dist/helpers/formHelpers.cjs +114 -0
- package/dist/helpers/formHelpers.d.ts +48 -0
- package/dist/helpers/formHelpers.mjs +106 -0
- package/dist/helpers/imageHelpers.cjs +20 -0
- package/dist/helpers/imageHelpers.d.ts +4 -0
- package/dist/helpers/imageHelpers.mjs +14 -0
- package/dist/helpers/index.cjs +192 -0
- package/dist/helpers/index.d.ts +17 -0
- package/dist/helpers/index.mjs +17 -0
- package/dist/helpers/localization.cjs +13 -0
- package/dist/helpers/localization.d.ts +7 -0
- package/dist/helpers/localization.mjs +6 -0
- package/dist/helpers/objectHelpers.cjs +19 -0
- package/dist/helpers/objectHelpers.d.ts +1 -0
- package/dist/helpers/objectHelpers.mjs +6 -0
- package/dist/helpers/orderHelpers.cjs +10 -0
- package/dist/helpers/orderHelpers.d.ts +67 -0
- package/dist/helpers/orderHelpers.mjs +4 -0
- package/dist/helpers/product.fixture.cjs +770 -0
- package/dist/helpers/product.fixture.d.ts +415 -0
- package/dist/helpers/product.fixture.mjs +838 -0
- package/dist/helpers/productDisruptorHelper.cjs +37 -0
- package/dist/helpers/productDisruptorHelper.d.ts +10 -0
- package/dist/helpers/productDisruptorHelper.mjs +23 -0
- package/dist/helpers/productHelpers.cjs +188 -0
- package/dist/helpers/productHelpers.d.ts +41 -0
- package/dist/helpers/productHelpers.mjs +170 -0
- package/dist/helpers/sanitizationHelpers.cjs +15 -0
- package/dist/helpers/sanitizationHelpers.d.ts +2 -0
- package/dist/helpers/sanitizationHelpers.mjs +6 -0
- package/dist/helpers/sortingHelper.cjs +67 -0
- package/dist/helpers/sortingHelper.d.ts +69 -0
- package/dist/helpers/sortingHelper.mjs +66 -0
- package/dist/helpers/stringHelpers.cjs +8 -0
- package/dist/helpers/stringHelpers.d.ts +2 -0
- package/dist/helpers/stringHelpers.mjs +2 -0
- package/dist/index.cjs +85 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.mjs +8 -0
- package/dist/rpc/methods/basket/basket.cjs +183 -0
- package/dist/rpc/methods/basket/basket.d.ts +23 -0
- package/dist/rpc/methods/basket/basket.mjs +181 -0
- package/dist/rpc/methods/brands.cjs +34 -0
- package/dist/rpc/methods/brands.d.ts +5 -0
- package/dist/rpc/methods/brands.mjs +16 -0
- package/dist/rpc/methods/categories.cjs +102 -0
- package/dist/rpc/methods/categories.d.ts +21 -0
- package/dist/rpc/methods/categories.mjs +64 -0
- package/dist/rpc/methods/cbd.cjs +45 -0
- package/dist/rpc/methods/cbd.d.ts +4 -0
- package/dist/rpc/methods/cbd.mjs +36 -0
- package/dist/rpc/methods/checkout/index.cjs +38 -0
- package/dist/rpc/methods/checkout/index.d.ts +3 -0
- package/dist/rpc/methods/checkout/index.mjs +3 -0
- package/dist/rpc/methods/checkout/order.cjs +55 -0
- package/dist/rpc/methods/checkout/order.d.ts +4 -0
- package/dist/rpc/methods/checkout/order.mjs +43 -0
- package/dist/rpc/methods/checkout/shopUser.cjs +126 -0
- package/dist/rpc/methods/checkout/shopUser.d.ts +7 -0
- package/dist/rpc/methods/checkout/shopUser.mjs +109 -0
- package/dist/rpc/methods/checkout/shopUserAddresses.cjs +25 -0
- package/dist/rpc/methods/checkout/shopUserAddresses.d.ts +3 -0
- package/dist/rpc/methods/checkout/shopUserAddresses.mjs +19 -0
- package/dist/rpc/methods/index.cjs +159 -0
- package/dist/rpc/methods/index.d.ts +14 -0
- package/dist/rpc/methods/index.mjs +14 -0
- package/dist/rpc/methods/navigationTrees.cjs +33 -0
- package/dist/rpc/methods/navigationTrees.d.ts +5 -0
- package/dist/rpc/methods/navigationTrees.mjs +10 -0
- package/dist/rpc/methods/products.cjs +261 -0
- package/dist/rpc/methods/products.d.ts +13 -0
- package/dist/rpc/methods/products.mjs +217 -0
- package/dist/rpc/methods/promotion.cjs +53 -0
- package/dist/rpc/methods/promotion.d.ts +4 -0
- package/dist/rpc/methods/promotion.mjs +38 -0
- package/dist/rpc/methods/search.cjs +38 -0
- package/dist/rpc/methods/search.d.ts +10 -0
- package/dist/rpc/methods/search.mjs +22 -0
- package/dist/rpc/methods/session.cjs +274 -0
- package/dist/rpc/methods/session.d.ts +20 -0
- package/dist/rpc/methods/session.mjs +309 -0
- package/dist/rpc/methods/shopConfiguration.cjs +15 -0
- package/dist/rpc/methods/shopConfiguration.d.ts +4 -0
- package/dist/rpc/methods/shopConfiguration.mjs +6 -0
- package/dist/rpc/methods/user.cjs +89 -0
- package/dist/rpc/methods/user.d.ts +20 -0
- package/dist/rpc/methods/user.mjs +56 -0
- package/dist/rpc/methods/variants.cjs +29 -0
- package/dist/rpc/methods/variants.d.ts +8 -0
- package/dist/rpc/methods/variants.mjs +12 -0
- package/dist/rpc/methods/wishlist.cjs +85 -0
- package/dist/rpc/methods/wishlist.d.ts +13 -0
- package/dist/rpc/methods/wishlist.mjs +61 -0
- package/dist/server.cjs +39 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.mjs +4 -0
- package/dist/types/api/auth.cjs +1 -0
- package/dist/types/api/auth.d.ts +63 -0
- package/dist/types/api/auth.mjs +0 -0
- package/dist/types/api/context.cjs +1 -0
- package/dist/types/api/context.d.ts +61 -0
- package/dist/types/api/context.mjs +0 -0
- package/dist/types/api/rpc.cjs +1 -0
- package/dist/types/api/rpc.d.ts +4 -0
- package/dist/types/api/rpc.mjs +0 -0
- package/dist/types/bapi/basket.cjs +1 -0
- package/dist/types/bapi/basket.d.ts +25 -0
- package/dist/types/bapi/basket.mjs +0 -0
- package/dist/types/bapi/brand.cjs +1 -0
- package/dist/types/bapi/brand.d.ts +2 -0
- package/dist/types/bapi/brand.mjs +0 -0
- package/dist/types/bapi/category.cjs +1 -0
- package/dist/types/bapi/category.d.ts +6 -0
- package/dist/types/bapi/category.mjs +0 -0
- package/dist/types/bapi/filter.cjs +1 -0
- package/dist/types/bapi/filter.d.ts +49 -0
- package/dist/types/bapi/filter.mjs +0 -0
- package/dist/types/bapi/navigation.cjs +1 -0
- package/dist/types/bapi/navigation.d.ts +10 -0
- package/dist/types/bapi/navigation.mjs +0 -0
- package/dist/types/bapi/order.cjs +1 -0
- package/dist/types/bapi/order.d.ts +554 -0
- package/dist/types/bapi/order.mjs +0 -0
- package/dist/types/bapi/pagination.cjs +1 -0
- package/dist/types/bapi/pagination.d.ts +13 -0
- package/dist/types/bapi/pagination.mjs +0 -0
- package/dist/types/bapi/product.cjs +1 -0
- package/dist/types/bapi/product.d.ts +84 -0
- package/dist/types/bapi/product.mjs +0 -0
- package/dist/types/bapi/productFilter.cjs +1 -0
- package/dist/types/bapi/productFilter.d.ts +7 -0
- package/dist/types/bapi/productFilter.mjs +0 -0
- package/dist/types/bapi/promotion.cjs +1 -0
- package/dist/types/bapi/promotion.d.ts +2 -0
- package/dist/types/bapi/promotion.mjs +0 -0
- package/dist/types/bapi/redirects.cjs +1 -0
- package/dist/types/bapi/redirects.d.ts +1 -0
- package/dist/types/bapi/redirects.mjs +0 -0
- package/dist/types/bapi/router.cjs +1 -0
- package/dist/types/bapi/router.d.ts +1 -0
- package/dist/types/bapi/router.mjs +0 -0
- package/dist/types/bapi/search.cjs +1 -0
- package/dist/types/bapi/search.d.ts +21 -0
- package/dist/types/bapi/search.mjs +0 -0
- package/dist/types/bapi/sorting.cjs +5 -0
- package/dist/types/bapi/sorting.d.ts +10 -0
- package/dist/types/bapi/sorting.mjs +1 -0
- package/dist/types/bapi/wishlist.cjs +1 -0
- package/dist/types/bapi/wishlist.d.ts +9 -0
- package/dist/types/bapi/wishlist.mjs +0 -0
- package/dist/types/breadcrumb.cjs +1 -0
- package/dist/types/breadcrumb.d.ts +4 -0
- package/dist/types/breadcrumb.mjs +0 -0
- package/dist/types/index.cjs +236 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/index.mjs +21 -0
- package/dist/types/promises.cjs +1 -0
- package/dist/types/promises.d.ts +2 -0
- package/dist/types/promises.mjs +0 -0
- package/dist/types/user.cjs +1 -0
- package/dist/types/user.d.ts +100 -0
- package/dist/types/user.mjs +0 -0
- package/dist/utils/compression.cjs +24 -0
- package/dist/utils/compression.d.ts +2 -0
- package/dist/utils/compression.mjs +16 -0
- package/dist/utils/hash.cjs +68 -0
- package/dist/utils/hash.d.ts +8 -0
- package/dist/utils/hash.mjs +61 -0
- package/dist/utils/index.cjs +27 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.mjs +2 -0
- package/dist/utils/keys.cjs +67 -0
- package/dist/utils/keys.d.ts +24 -0
- package/dist/utils/keys.mjs +56 -0
- package/dist/utils/log.cjs +93 -0
- package/dist/utils/log.d.ts +43 -0
- package/dist/utils/log.mjs +75 -0
- package/dist/utils/rpc.cjs +41 -0
- package/dist/utils/rpc.d.ts +6 -0
- package/dist/utils/rpc.mjs +35 -0
- package/dist/utils/timeout.cjs +17 -0
- package/dist/utils/timeout.d.ts +2 -0
- package/dist/utils/timeout.mjs +9 -0
- package/dist/utils/user.cjs +75 -0
- package/dist/utils/user.d.ts +14 -0
- package/dist/utils/user.mjs +65 -0
- package/package.json +97 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Log } from '../utils';
|
|
2
|
+
import { Cache as CacheInterface } from './cache';
|
|
3
|
+
export type CacheOptions = Partial<{
|
|
4
|
+
ttl: number;
|
|
5
|
+
cacheKey: string;
|
|
6
|
+
cacheKeyPrefix: string;
|
|
7
|
+
timeout: number;
|
|
8
|
+
}>;
|
|
9
|
+
type Fn<TArgs extends unknown[], TResult> = (...args: TArgs) => Promise<TResult>;
|
|
10
|
+
export declare class Cached {
|
|
11
|
+
private cache;
|
|
12
|
+
private log;
|
|
13
|
+
private prefix;
|
|
14
|
+
private enabled;
|
|
15
|
+
constructor(cache: CacheInterface, log: Log, prefix?: string | number, enabled?: boolean);
|
|
16
|
+
get isCacheEnabled(): boolean | undefined;
|
|
17
|
+
execute<TArgs extends unknown[], TResult>(fn: Fn<TArgs, TResult>, options?: CacheOptions): Awaited<Fn<TArgs, TResult>>;
|
|
18
|
+
private getCacheValue;
|
|
19
|
+
private setCacheValue;
|
|
20
|
+
private createCacheKey;
|
|
21
|
+
private handleError;
|
|
22
|
+
}
|
|
23
|
+
export type { Cache as CacheInterface } from './cache';
|
|
24
|
+
export type CachedType = Cached['execute'];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { tryit } from "radash";
|
|
2
|
+
import { autobind } from "../helpers/autobind.mjs";
|
|
3
|
+
import { timeout } from "../utils/index.mjs";
|
|
4
|
+
import { sha256 } from "../utils/hash.mjs";
|
|
5
|
+
import { CACHE_TIMEOUT } from "../constants/index.mjs";
|
|
6
|
+
const CACHE_NOT_INITIALIZED_MSG = "Cache is not initialized";
|
|
7
|
+
export class Cached {
|
|
8
|
+
cache;
|
|
9
|
+
log;
|
|
10
|
+
prefix;
|
|
11
|
+
enabled;
|
|
12
|
+
constructor(cache, log, prefix = "", enabled = process.env.AY_CACHE_DISABLED !== "true") {
|
|
13
|
+
if (!cache) {
|
|
14
|
+
log.error(CACHE_NOT_INITIALIZED_MSG);
|
|
15
|
+
throw new Error(CACHE_NOT_INITIALIZED_MSG);
|
|
16
|
+
}
|
|
17
|
+
this.cache = cache;
|
|
18
|
+
this.log = log.space("cached.init");
|
|
19
|
+
this.prefix = prefix;
|
|
20
|
+
this.enabled = enabled;
|
|
21
|
+
autobind(this);
|
|
22
|
+
}
|
|
23
|
+
get isCacheEnabled() {
|
|
24
|
+
return this.enabled;
|
|
25
|
+
}
|
|
26
|
+
execute(fn, options) {
|
|
27
|
+
return async (...args) => {
|
|
28
|
+
const prefix = options?.cacheKeyPrefix ?? fn.name;
|
|
29
|
+
const params = [...args, this?.prefix];
|
|
30
|
+
const cacheKey = options?.cacheKey ?? await this.createCacheKey(params, prefix);
|
|
31
|
+
const [cacheGetError, cachedResponse] = await tryit(this.getCacheValue)(
|
|
32
|
+
cacheKey,
|
|
33
|
+
options
|
|
34
|
+
);
|
|
35
|
+
if (cachedResponse) {
|
|
36
|
+
return cachedResponse;
|
|
37
|
+
}
|
|
38
|
+
if (cacheGetError) {
|
|
39
|
+
this.handleError(cacheGetError, false);
|
|
40
|
+
}
|
|
41
|
+
const [error, response] = await tryit(fn)(...args);
|
|
42
|
+
if (error) {
|
|
43
|
+
this.handleError(error);
|
|
44
|
+
}
|
|
45
|
+
if (cacheGetError) {
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
const [cacheSetError] = await tryit(this.setCacheValue)(
|
|
49
|
+
cacheKey,
|
|
50
|
+
response,
|
|
51
|
+
options
|
|
52
|
+
);
|
|
53
|
+
if (cacheSetError) {
|
|
54
|
+
this.handleError(cacheSetError);
|
|
55
|
+
}
|
|
56
|
+
return response;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async getCacheValue(cacheKey, options) {
|
|
60
|
+
if (!this.isCacheEnabled) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const data = await timeout(
|
|
64
|
+
options?.timeout ?? CACHE_TIMEOUT,
|
|
65
|
+
this.cache.get(cacheKey)
|
|
66
|
+
);
|
|
67
|
+
if (data !== null) {
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async setCacheValue(cacheKey, value, options) {
|
|
72
|
+
if (!this.isCacheEnabled) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const ttl = options?.ttl || 60 * 60;
|
|
76
|
+
if (ttl) {
|
|
77
|
+
await timeout(CACHE_TIMEOUT, this.cache.set(cacheKey, value, ttl));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async createCacheKey(params, prefix = "") {
|
|
81
|
+
return [prefix, await sha256(JSON.stringify(params))].join(":");
|
|
82
|
+
}
|
|
83
|
+
handleError(error, shouldThrow = true) {
|
|
84
|
+
if (error.message === "timeout") {
|
|
85
|
+
return this.log.error("Cache timeout");
|
|
86
|
+
}
|
|
87
|
+
this.log.error(error);
|
|
88
|
+
if (shouldThrow) {
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _cache = require("./cache.cjs");
|
|
7
|
+
Object.keys(_cache).forEach(function (key) {
|
|
8
|
+
if (key === "default" || key === "__esModule") return;
|
|
9
|
+
if (key in exports && exports[key] === _cache[key]) return;
|
|
10
|
+
Object.defineProperty(exports, key, {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _cache[key];
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
var _cached = require("./cached.cjs");
|
|
18
|
+
Object.keys(_cached).forEach(function (key) {
|
|
19
|
+
if (key === "default" || key === "__esModule") return;
|
|
20
|
+
if (key in exports && exports[key] === _cached[key]) return;
|
|
21
|
+
Object.defineProperty(exports, key, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () {
|
|
24
|
+
return _cached[key];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
var _types = require("./types.cjs");
|
|
29
|
+
Object.keys(_types).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
32
|
+
Object.defineProperty(exports, key, {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () {
|
|
35
|
+
return _types[key];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createClient = exports.RedisCache = void 0;
|
|
7
|
+
var _redis = require("redis");
|
|
8
|
+
var _compression = require("../../utils/compression.cjs");
|
|
9
|
+
const connections = {};
|
|
10
|
+
const createClient = async (options, log) => {
|
|
11
|
+
log = log.space("redis.createClient");
|
|
12
|
+
const connectionIndex = [options.host, options.database].join("_");
|
|
13
|
+
let client;
|
|
14
|
+
if (connections[connectionIndex] !== void 0) {
|
|
15
|
+
client = connections[connectionIndex];
|
|
16
|
+
} else {
|
|
17
|
+
client = connections[connectionIndex] = (0, _redis.createClient)({
|
|
18
|
+
...options,
|
|
19
|
+
...(options && "username" in options && {
|
|
20
|
+
username: options.username
|
|
21
|
+
}),
|
|
22
|
+
socket: {
|
|
23
|
+
host: options.host,
|
|
24
|
+
port: options.port,
|
|
25
|
+
...(options.sslTransit === true ? {
|
|
26
|
+
tls: true,
|
|
27
|
+
checkServerIdentity: () => void 0
|
|
28
|
+
} : {
|
|
29
|
+
tls: false
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
client.on("error", error => {
|
|
34
|
+
log.error("Redis connection error", error);
|
|
35
|
+
});
|
|
36
|
+
await client.connect();
|
|
37
|
+
}
|
|
38
|
+
return client;
|
|
39
|
+
};
|
|
40
|
+
exports.createClient = createClient;
|
|
41
|
+
class RedisCache {
|
|
42
|
+
client;
|
|
43
|
+
prefix;
|
|
44
|
+
constructor(client, prefix = "") {
|
|
45
|
+
this.client = client;
|
|
46
|
+
this.prefix = prefix;
|
|
47
|
+
}
|
|
48
|
+
async get(key) {
|
|
49
|
+
const value = await this.client.get(this.getKey(key));
|
|
50
|
+
if (value === null) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
return this.deserialize(value);
|
|
54
|
+
}
|
|
55
|
+
async has(key) {
|
|
56
|
+
return (await this.client.exists(this.getKey(key))) !== 0;
|
|
57
|
+
}
|
|
58
|
+
async purgeTags(tags) {
|
|
59
|
+
await Promise.all(tags.map(tag => this.purgeTag(tag)));
|
|
60
|
+
}
|
|
61
|
+
async purgeTag(tag) {
|
|
62
|
+
const keys = await this.client.sMembers(this.getKey(`tag:${tag}`));
|
|
63
|
+
await this.client.del(keys);
|
|
64
|
+
await this.client.del(this.getKey(`tag:${tag}`));
|
|
65
|
+
}
|
|
66
|
+
async purgeKeys(keys) {
|
|
67
|
+
await this.client.del(keys);
|
|
68
|
+
}
|
|
69
|
+
async purgeAll() {
|
|
70
|
+
await this.client.flushDb();
|
|
71
|
+
}
|
|
72
|
+
async set(key, value, ttl, tags = []) {
|
|
73
|
+
const _value = await this.serialize(value);
|
|
74
|
+
await this.client.set(this.getKey(key), _value, {
|
|
75
|
+
EX: ttl
|
|
76
|
+
});
|
|
77
|
+
await Promise.all(tags.map(tag => this.addKeyToTag(tag, this.getKey(key))));
|
|
78
|
+
}
|
|
79
|
+
async addKeyToTag(tag, key) {
|
|
80
|
+
await this.client.sAdd(this.getKey(`tag:${tag}`), key);
|
|
81
|
+
}
|
|
82
|
+
getKey(key) {
|
|
83
|
+
return [this.prefix, key].join(":");
|
|
84
|
+
}
|
|
85
|
+
async deserialize(value) {
|
|
86
|
+
const data = await (0, _compression.decompress)(value);
|
|
87
|
+
return JSON.parse(data);
|
|
88
|
+
}
|
|
89
|
+
async serialize(value) {
|
|
90
|
+
const data = JSON.stringify(value);
|
|
91
|
+
return await (0, _compression.compress)(data);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.RedisCache = RedisCache;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { RedisClientType } from 'redis';
|
|
2
|
+
import { Log } from '../../utils';
|
|
3
|
+
import { Cache as CacheInterface } from '../../cache/cache';
|
|
4
|
+
export interface RedisOptions {
|
|
5
|
+
username?: string;
|
|
6
|
+
password?: string;
|
|
7
|
+
host?: string;
|
|
8
|
+
port?: number;
|
|
9
|
+
database?: number;
|
|
10
|
+
sslTransit?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare const createClient: (options: RedisOptions, log: Log) => Promise<RedisClientType>;
|
|
13
|
+
export declare class RedisCache implements CacheInterface {
|
|
14
|
+
client: RedisClientType;
|
|
15
|
+
prefix: string;
|
|
16
|
+
constructor(client: RedisClientType, prefix?: string);
|
|
17
|
+
get(key: string): Promise<any>;
|
|
18
|
+
has(key: string): Promise<boolean>;
|
|
19
|
+
purgeTags(tags: string[]): Promise<void>;
|
|
20
|
+
purgeTag(tag: string): Promise<void>;
|
|
21
|
+
purgeKeys(keys: string[]): Promise<void>;
|
|
22
|
+
purgeAll(): Promise<void>;
|
|
23
|
+
set(key: string, value: any, ttl: number, tags?: string[]): Promise<void>;
|
|
24
|
+
private addKeyToTag;
|
|
25
|
+
private getKey;
|
|
26
|
+
private deserialize;
|
|
27
|
+
private serialize;
|
|
28
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createClient as createRedisClient } from "redis";
|
|
2
|
+
import { compress, decompress } from "../../utils/compression.mjs";
|
|
3
|
+
const connections = {};
|
|
4
|
+
export const createClient = async (options, log) => {
|
|
5
|
+
log = log.space("redis.createClient");
|
|
6
|
+
const connectionIndex = [options.host, options.database].join("_");
|
|
7
|
+
let client;
|
|
8
|
+
if (connections[connectionIndex] !== void 0) {
|
|
9
|
+
client = connections[connectionIndex];
|
|
10
|
+
} else {
|
|
11
|
+
client = connections[connectionIndex] = createRedisClient({
|
|
12
|
+
...options,
|
|
13
|
+
...options && "username" in options && { username: options.username },
|
|
14
|
+
socket: {
|
|
15
|
+
host: options.host,
|
|
16
|
+
port: options.port,
|
|
17
|
+
...options.sslTransit === true ? {
|
|
18
|
+
tls: true,
|
|
19
|
+
checkServerIdentity: () => void 0
|
|
20
|
+
} : {
|
|
21
|
+
tls: false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
client.on("error", (error) => {
|
|
26
|
+
log.error("Redis connection error", error);
|
|
27
|
+
});
|
|
28
|
+
await client.connect();
|
|
29
|
+
}
|
|
30
|
+
return client;
|
|
31
|
+
};
|
|
32
|
+
export class RedisCache {
|
|
33
|
+
client;
|
|
34
|
+
prefix;
|
|
35
|
+
constructor(client, prefix = "") {
|
|
36
|
+
this.client = client;
|
|
37
|
+
this.prefix = prefix;
|
|
38
|
+
}
|
|
39
|
+
async get(key) {
|
|
40
|
+
const value = await this.client.get(this.getKey(key));
|
|
41
|
+
if (value === null) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return this.deserialize(value);
|
|
45
|
+
}
|
|
46
|
+
async has(key) {
|
|
47
|
+
return await this.client.exists(this.getKey(key)) !== 0;
|
|
48
|
+
}
|
|
49
|
+
async purgeTags(tags) {
|
|
50
|
+
await Promise.all(tags.map((tag) => this.purgeTag(tag)));
|
|
51
|
+
}
|
|
52
|
+
async purgeTag(tag) {
|
|
53
|
+
const keys = await this.client.sMembers(this.getKey(`tag:${tag}`));
|
|
54
|
+
await this.client.del(keys);
|
|
55
|
+
await this.client.del(this.getKey(`tag:${tag}`));
|
|
56
|
+
}
|
|
57
|
+
async purgeKeys(keys) {
|
|
58
|
+
await this.client.del(keys);
|
|
59
|
+
}
|
|
60
|
+
async purgeAll() {
|
|
61
|
+
await this.client.flushDb();
|
|
62
|
+
}
|
|
63
|
+
async set(key, value, ttl, tags = []) {
|
|
64
|
+
const _value = await this.serialize(value);
|
|
65
|
+
await this.client.set(this.getKey(key), _value, {
|
|
66
|
+
EX: ttl
|
|
67
|
+
});
|
|
68
|
+
await Promise.all(
|
|
69
|
+
tags.map((tag) => this.addKeyToTag(tag, this.getKey(key)))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
async addKeyToTag(tag, key) {
|
|
73
|
+
await this.client.sAdd(this.getKey(`tag:${tag}`), key);
|
|
74
|
+
}
|
|
75
|
+
getKey(key) {
|
|
76
|
+
return [this.prefix, key].join(":");
|
|
77
|
+
}
|
|
78
|
+
async deserialize(value) {
|
|
79
|
+
const data = await decompress(value);
|
|
80
|
+
return JSON.parse(data);
|
|
81
|
+
}
|
|
82
|
+
async serialize(value) {
|
|
83
|
+
const data = JSON.stringify(value);
|
|
84
|
+
return await compress(data);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.UnstorageCache = void 0;
|
|
7
|
+
class UnstorageCache {
|
|
8
|
+
storage;
|
|
9
|
+
prefix;
|
|
10
|
+
constructor(storage, prefix = "") {
|
|
11
|
+
this.storage = storage;
|
|
12
|
+
this.prefix = prefix;
|
|
13
|
+
}
|
|
14
|
+
async get(key) {
|
|
15
|
+
const value = await this.storage.getItem(this.getKey(key));
|
|
16
|
+
if (value === null) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
async has(key) {
|
|
22
|
+
return await this.storage.hasItem(this.getKey(key));
|
|
23
|
+
}
|
|
24
|
+
async purgeTags(tags) {
|
|
25
|
+
await Promise.all(tags.map(tag => this.purgeTag(tag)));
|
|
26
|
+
}
|
|
27
|
+
async purgeTag(tag) {
|
|
28
|
+
const keys = await this.storage.getItem(this.getKey(`tag:${tag}`));
|
|
29
|
+
await this.purgeKeys(keys ?? []);
|
|
30
|
+
await this.storage.removeItem(this.getKey(`tag:${tag}`));
|
|
31
|
+
}
|
|
32
|
+
async purgeKeys(keys) {
|
|
33
|
+
await Promise.all(keys.map(k => this.storage.removeItem(k)));
|
|
34
|
+
}
|
|
35
|
+
async purgeAll() {
|
|
36
|
+
await this.storage.clear();
|
|
37
|
+
}
|
|
38
|
+
async set(key, value, ttl, tags = []) {
|
|
39
|
+
await this.storage.setItem(this.getKey(key), value, {
|
|
40
|
+
ttl
|
|
41
|
+
});
|
|
42
|
+
await Promise.all(tags.map(tag => this.addKeyToTag(tag, this.getKey(key))));
|
|
43
|
+
}
|
|
44
|
+
async addKeyToTag(tag, key) {
|
|
45
|
+
const keys = await this.storage.getItem(tag);
|
|
46
|
+
const set = new Set(keys ?? []);
|
|
47
|
+
set.add(key);
|
|
48
|
+
await this.storage.setItem(this.getKey(`tag:${tag}`), Array.from(set));
|
|
49
|
+
}
|
|
50
|
+
getKey(key) {
|
|
51
|
+
return [this.prefix, key].join(":");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.UnstorageCache = UnstorageCache;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Storage } from 'unstorage';
|
|
2
|
+
import { Cache as CacheInterface } from '../cache';
|
|
3
|
+
export declare class UnstorageCache implements CacheInterface {
|
|
4
|
+
storage: Storage;
|
|
5
|
+
prefix: string;
|
|
6
|
+
constructor(storage: Storage, prefix?: string);
|
|
7
|
+
get(key: string): Promise<any | null>;
|
|
8
|
+
has(key: string): Promise<boolean>;
|
|
9
|
+
purgeTags(tags: string[]): Promise<void>;
|
|
10
|
+
purgeTag(tag: string): Promise<void>;
|
|
11
|
+
purgeKeys(keys: string[]): Promise<void>;
|
|
12
|
+
purgeAll(): Promise<void>;
|
|
13
|
+
set(key: string, value: any, ttl: number, tags?: string[]): Promise<void>;
|
|
14
|
+
private addKeyToTag;
|
|
15
|
+
private getKey;
|
|
16
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export class UnstorageCache {
|
|
2
|
+
storage;
|
|
3
|
+
prefix;
|
|
4
|
+
constructor(storage, prefix = "") {
|
|
5
|
+
this.storage = storage;
|
|
6
|
+
this.prefix = prefix;
|
|
7
|
+
}
|
|
8
|
+
async get(key) {
|
|
9
|
+
const value = await this.storage.getItem(this.getKey(key));
|
|
10
|
+
if (value === null) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
async has(key) {
|
|
16
|
+
return await this.storage.hasItem(this.getKey(key));
|
|
17
|
+
}
|
|
18
|
+
async purgeTags(tags) {
|
|
19
|
+
await Promise.all(tags.map((tag) => this.purgeTag(tag)));
|
|
20
|
+
}
|
|
21
|
+
async purgeTag(tag) {
|
|
22
|
+
const keys = await this.storage.getItem(this.getKey(`tag:${tag}`));
|
|
23
|
+
await this.purgeKeys(keys ?? []);
|
|
24
|
+
await this.storage.removeItem(this.getKey(`tag:${tag}`));
|
|
25
|
+
}
|
|
26
|
+
async purgeKeys(keys) {
|
|
27
|
+
await Promise.all(keys.map((k) => this.storage.removeItem(k)));
|
|
28
|
+
}
|
|
29
|
+
async purgeAll() {
|
|
30
|
+
await this.storage.clear();
|
|
31
|
+
}
|
|
32
|
+
async set(key, value, ttl, tags = []) {
|
|
33
|
+
await this.storage.setItem(this.getKey(key), value, {
|
|
34
|
+
ttl
|
|
35
|
+
});
|
|
36
|
+
await Promise.all(
|
|
37
|
+
tags.map((tag) => this.addKeyToTag(tag, this.getKey(key)))
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
async addKeyToTag(tag, key) {
|
|
41
|
+
const keys = await this.storage.getItem(tag);
|
|
42
|
+
const set = new Set(keys ?? []);
|
|
43
|
+
set.add(key);
|
|
44
|
+
await this.storage.setItem(this.getKey(`tag:${tag}`), Array.from(set));
|
|
45
|
+
}
|
|
46
|
+
getKey(key) {
|
|
47
|
+
return [this.prefix, key].join(":");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ExistingItemHandling = void 0;
|
|
7
|
+
const ExistingItemHandling = {
|
|
8
|
+
KeepExisting: 0,
|
|
9
|
+
AddQuantityToExisting: 1,
|
|
10
|
+
ReplaceExisting: 2,
|
|
11
|
+
ReplaceExistingWithCombinedQuantity: 3
|
|
12
|
+
};
|
|
13
|
+
exports.ExistingItemHandling = ExistingItemHandling;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ValuesType } from 'utility-types';
|
|
2
|
+
export declare const ExistingItemHandling: {
|
|
3
|
+
readonly KeepExisting: 0;
|
|
4
|
+
readonly AddQuantityToExisting: 1;
|
|
5
|
+
readonly ReplaceExisting: 2;
|
|
6
|
+
readonly ReplaceExistingWithCombinedQuantity: 3;
|
|
7
|
+
};
|
|
8
|
+
export type ExistingItemHandling = ValuesType<typeof ExistingItemHandling>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CACHE_TIMEOUT = 500;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CACHE_TIMEOUT = 500;
|