@lakutata/cache 3.0.0-beta.0

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 (116) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +175 -0
  3. package/dist/cjs/cache/Cacher.d.ts +446 -0
  4. package/dist/cjs/cache/Cacher.js +523 -0
  5. package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
  6. package/dist/cjs/cache/exceptions/CacheDriverNotFoundException.js +17 -0
  7. package/dist/cjs/cache/interfaces/CacherOptions.d.ts +42 -0
  8. package/dist/cjs/cache/interfaces/CacherOptions.js +2 -0
  9. package/dist/cjs/cache/lib/LikePrefix.d.ts +5 -0
  10. package/dist/cjs/cache/lib/LikePrefix.js +10 -0
  11. package/dist/cjs/cache/lib/LoadDriver.d.ts +6 -0
  12. package/dist/cjs/cache/lib/LoadDriver.js +66 -0
  13. package/dist/cjs/cache/lib/Serializer.d.ts +18 -0
  14. package/dist/cjs/cache/lib/Serializer.js +46 -0
  15. package/dist/cjs/cache/lib/WithTimeout.d.ts +7 -0
  16. package/dist/cjs/cache/lib/WithTimeout.js +23 -0
  17. package/dist/cjs/cache/options/FileCacheOptions.d.ts +54 -0
  18. package/dist/cjs/cache/options/FileCacheOptions.js +62 -0
  19. package/dist/cjs/cache/options/MemcacheCacheOptions.d.ts +57 -0
  20. package/dist/cjs/cache/options/MemcacheCacheOptions.js +66 -0
  21. package/dist/cjs/cache/options/MemoryCacheOptions.d.ts +39 -0
  22. package/dist/cjs/cache/options/MemoryCacheOptions.js +52 -0
  23. package/dist/cjs/cache/options/MongoCacheOptions.d.ts +72 -0
  24. package/dist/cjs/cache/options/MongoCacheOptions.js +81 -0
  25. package/dist/cjs/cache/options/MysqlCacheOptions.d.ts +73 -0
  26. package/dist/cjs/cache/options/MysqlCacheOptions.js +82 -0
  27. package/dist/cjs/cache/options/PostgresCacheOptions.d.ts +87 -0
  28. package/dist/cjs/cache/options/PostgresCacheOptions.js +92 -0
  29. package/dist/cjs/cache/options/RedisCacheOptions.d.ts +120 -0
  30. package/dist/cjs/cache/options/RedisCacheOptions.js +113 -0
  31. package/dist/cjs/cache/options/SqliteCacheOptions.d.ts +53 -0
  32. package/dist/cjs/cache/options/SqliteCacheOptions.js +61 -0
  33. package/dist/cjs/cache/stores/CacheStore.d.ts +44 -0
  34. package/dist/cjs/cache/stores/CacheStore.js +42 -0
  35. package/dist/cjs/cache/stores/CreateCacheStore.d.ts +7 -0
  36. package/dist/cjs/cache/stores/CreateCacheStore.js +37 -0
  37. package/dist/cjs/cache/stores/FileStore.d.ts +51 -0
  38. package/dist/cjs/cache/stores/FileStore.js +143 -0
  39. package/dist/cjs/cache/stores/MemcacheStore.d.ts +42 -0
  40. package/dist/cjs/cache/stores/MemcacheStore.js +69 -0
  41. package/dist/cjs/cache/stores/MemoryStore.d.ts +34 -0
  42. package/dist/cjs/cache/stores/MemoryStore.js +75 -0
  43. package/dist/cjs/cache/stores/MongoStore.d.ts +55 -0
  44. package/dist/cjs/cache/stores/MongoStore.js +72 -0
  45. package/dist/cjs/cache/stores/MysqlStore.d.ts +27 -0
  46. package/dist/cjs/cache/stores/MysqlStore.js +71 -0
  47. package/dist/cjs/cache/stores/PostgresStore.d.ts +30 -0
  48. package/dist/cjs/cache/stores/PostgresStore.js +85 -0
  49. package/dist/cjs/cache/stores/RedisStore.d.ts +58 -0
  50. package/dist/cjs/cache/stores/RedisStore.js +131 -0
  51. package/dist/cjs/cache/stores/SerializedCacheStore.d.ts +48 -0
  52. package/dist/cjs/cache/stores/SerializedCacheStore.js +80 -0
  53. package/dist/cjs/cache/stores/SqliteStore.d.ts +44 -0
  54. package/dist/cjs/cache/stores/SqliteStore.js +91 -0
  55. package/dist/cjs/cache/types/CacheStoreOptions.d.ts +15 -0
  56. package/dist/cjs/cache/types/CacheStoreOptions.js +2 -0
  57. package/dist/cjs/exports/Cache.d.ts +12 -0
  58. package/dist/cjs/exports/Cache.js +31 -0
  59. package/dist/cjs/package.json +1 -0
  60. package/dist/esm/cache/Cacher.js +518 -0
  61. package/dist/esm/cache/exceptions/CacheDriverNotFoundException.js +13 -0
  62. package/dist/esm/cache/interfaces/CacherOptions.js +1 -0
  63. package/dist/esm/cache/lib/LikePrefix.js +7 -0
  64. package/dist/esm/cache/lib/LoadDriver.js +30 -0
  65. package/dist/esm/cache/lib/Serializer.js +42 -0
  66. package/dist/esm/cache/lib/WithTimeout.js +20 -0
  67. package/dist/esm/cache/options/FileCacheOptions.js +58 -0
  68. package/dist/esm/cache/options/MemcacheCacheOptions.js +62 -0
  69. package/dist/esm/cache/options/MemoryCacheOptions.js +48 -0
  70. package/dist/esm/cache/options/MongoCacheOptions.js +77 -0
  71. package/dist/esm/cache/options/MysqlCacheOptions.js +78 -0
  72. package/dist/esm/cache/options/PostgresCacheOptions.js +88 -0
  73. package/dist/esm/cache/options/RedisCacheOptions.js +109 -0
  74. package/dist/esm/cache/options/SqliteCacheOptions.js +57 -0
  75. package/dist/esm/cache/stores/CacheStore.js +36 -0
  76. package/dist/esm/cache/stores/CreateCacheStore.js +34 -0
  77. package/dist/esm/cache/stores/FileStore.js +136 -0
  78. package/dist/esm/cache/stores/MemcacheStore.js +65 -0
  79. package/dist/esm/cache/stores/MemoryStore.js +71 -0
  80. package/dist/esm/cache/stores/MongoStore.js +68 -0
  81. package/dist/esm/cache/stores/MysqlStore.js +67 -0
  82. package/dist/esm/cache/stores/PostgresStore.js +81 -0
  83. package/dist/esm/cache/stores/RedisStore.js +127 -0
  84. package/dist/esm/cache/stores/SerializedCacheStore.js +76 -0
  85. package/dist/esm/cache/stores/SqliteStore.js +87 -0
  86. package/dist/esm/cache/types/CacheStoreOptions.js +1 -0
  87. package/dist/esm/exports/Cache.js +12 -0
  88. package/dist/types/cache/Cacher.d.ts +446 -0
  89. package/dist/types/cache/exceptions/CacheDriverNotFoundException.d.ts +10 -0
  90. package/dist/types/cache/interfaces/CacherOptions.d.ts +42 -0
  91. package/dist/types/cache/lib/LikePrefix.d.ts +5 -0
  92. package/dist/types/cache/lib/LoadDriver.d.ts +6 -0
  93. package/dist/types/cache/lib/Serializer.d.ts +18 -0
  94. package/dist/types/cache/lib/WithTimeout.d.ts +7 -0
  95. package/dist/types/cache/options/FileCacheOptions.d.ts +54 -0
  96. package/dist/types/cache/options/MemcacheCacheOptions.d.ts +57 -0
  97. package/dist/types/cache/options/MemoryCacheOptions.d.ts +39 -0
  98. package/dist/types/cache/options/MongoCacheOptions.d.ts +72 -0
  99. package/dist/types/cache/options/MysqlCacheOptions.d.ts +73 -0
  100. package/dist/types/cache/options/PostgresCacheOptions.d.ts +87 -0
  101. package/dist/types/cache/options/RedisCacheOptions.d.ts +120 -0
  102. package/dist/types/cache/options/SqliteCacheOptions.d.ts +53 -0
  103. package/dist/types/cache/stores/CacheStore.d.ts +44 -0
  104. package/dist/types/cache/stores/CreateCacheStore.d.ts +7 -0
  105. package/dist/types/cache/stores/FileStore.d.ts +51 -0
  106. package/dist/types/cache/stores/MemcacheStore.d.ts +42 -0
  107. package/dist/types/cache/stores/MemoryStore.d.ts +34 -0
  108. package/dist/types/cache/stores/MongoStore.d.ts +55 -0
  109. package/dist/types/cache/stores/MysqlStore.d.ts +27 -0
  110. package/dist/types/cache/stores/PostgresStore.d.ts +30 -0
  111. package/dist/types/cache/stores/RedisStore.d.ts +58 -0
  112. package/dist/types/cache/stores/SerializedCacheStore.d.ts +48 -0
  113. package/dist/types/cache/stores/SqliteStore.d.ts +44 -0
  114. package/dist/types/cache/types/CacheStoreOptions.d.ts +15 -0
  115. package/dist/types/exports/Cache.d.ts +12 -0
  116. package/package.json +72 -0
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.loadDriver = loadDriver;
37
+ const CacheDriverNotFoundException_js_1 = require("../exceptions/CacheDriverNotFoundException.js");
38
+ /**
39
+ * Whether an error reports the package itself as missing
40
+ * @param error
41
+ * @param packageName
42
+ */
43
+ function isPackageMissing(error, packageName) {
44
+ const { code, message } = error;
45
+ if (code !== 'ERR_MODULE_NOT_FOUND' && code !== 'MODULE_NOT_FOUND')
46
+ return false;
47
+ return !!message && (message.includes(`'${packageName}'`) || message.includes(`"${packageName}"`));
48
+ }
49
+ /**
50
+ * Load the driver package of a store, the first installed of the candidates
51
+ * @param member a member of the driver API, to find it in the ESM namespace or the CommonJS exports
52
+ * @param packageNames
53
+ */
54
+ async function loadDriver(member, ...packageNames) {
55
+ for (const packageName of packageNames) {
56
+ try {
57
+ const driver = await Promise.resolve(`${packageName}`).then(s => __importStar(require(s)));
58
+ return (driver[member] === undefined && driver.default ? driver.default : driver);
59
+ }
60
+ catch (e) {
61
+ if (!isPackageMissing(e, packageName))
62
+ throw e;
63
+ }
64
+ }
65
+ throw new CacheDriverNotFoundException_js_1.CacheDriverNotFoundException('Package "{packageName}" is required for this driver. Run "npm install {packageName}".', { packageName: packageNames[0] });
66
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The stored form of a cache entry
3
+ * The strings starting with a colon get one more colon, the buffers are written as ":base64:" strings (the format of 2.x)
4
+ */
5
+ export type CacheEntry<T = unknown> = {
6
+ value: T;
7
+ expires?: number;
8
+ };
9
+ /**
10
+ * Serialize data
11
+ * @param data
12
+ */
13
+ export declare function serialize(data: unknown): string;
14
+ /**
15
+ * Deserialize data
16
+ * @param text
17
+ */
18
+ export declare function deserialize<T = unknown>(text: string): T;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serialize = serialize;
4
+ exports.deserialize = deserialize;
5
+ const node_buffer_1 = require("node:buffer");
6
+ const COLON = 58;
7
+ const BASE64_PREFIX = ':base64:';
8
+ //A string value starting with a colon follows an unquoted ":", "[" or ","
9
+ const ESCAPED_STRING = /[:[,]\s*":/;
10
+ /**
11
+ * Encode the values the JSON form cannot keep
12
+ * @param this
13
+ * @param key
14
+ * @param value
15
+ */
16
+ function replacer(key, value) {
17
+ if (typeof value === 'string')
18
+ return value.charCodeAt(0) === COLON ? `:${value}` : value;
19
+ if (value !== null && typeof value === 'object' && node_buffer_1.Buffer.isBuffer(this[key]))
20
+ return `${BASE64_PREFIX}${this[key].toString('base64')}`;
21
+ return value;
22
+ }
23
+ /**
24
+ * Decode the values encoded by the replacer
25
+ * @param key
26
+ * @param value
27
+ */
28
+ function reviver(key, value) {
29
+ if (typeof value !== 'string' || value.charCodeAt(0) !== COLON)
30
+ return value;
31
+ return value.startsWith(BASE64_PREFIX) ? node_buffer_1.Buffer.from(value.slice(BASE64_PREFIX.length), 'base64') : value.slice(1);
32
+ }
33
+ /**
34
+ * Serialize data
35
+ * @param data
36
+ */
37
+ function serialize(data) {
38
+ return JSON.stringify(data, replacer) ?? 'null';
39
+ }
40
+ /**
41
+ * Deserialize data
42
+ * @param text
43
+ */
44
+ function deserialize(text) {
45
+ return ESCAPED_STRING.test(text) ? JSON.parse(text, reviver) : JSON.parse(text);
46
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Reject a promise which does not settle in time
3
+ * @param promise
4
+ * @param timeout ms
5
+ * @param message
6
+ */
7
+ export declare function withTimeout<T>(promise: Promise<T>, timeout: number, message: string): Promise<T>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimeout = withTimeout;
4
+ /**
5
+ * Reject a promise which does not settle in time
6
+ * @param promise
7
+ * @param timeout ms
8
+ * @param message
9
+ */
10
+ async function withTimeout(promise, timeout, message) {
11
+ let timer;
12
+ try {
13
+ return await Promise.race([
14
+ promise,
15
+ new Promise((resolve, reject) => {
16
+ timer = setTimeout(() => reject(new Error(message)), timeout);
17
+ })
18
+ ]);
19
+ }
20
+ finally {
21
+ clearTimeout(timer);
22
+ }
23
+ }
@@ -0,0 +1,54 @@
1
+ import { DTO } from '@lakutata/core';
2
+ /**
3
+ * The options of the file store: the entries are kept in memory and saved to one JSON file (loaded when the cache
4
+ * starts, written in batches, flushed when the cache is destroyed), so they survive a restart. It needs no optional
5
+ * dependency.
6
+ *
7
+ * The file is rewritten whole on each save (through a temporary file renamed over it), so it suits small caches;
8
+ * the processes sharing one file overwrite each other's entries. An unreadable file (not JSON) fails the cache's
9
+ * creation; a failed save is emitted as an `'error'` event of the cache. The values are kept as JSON.
10
+ * @example
11
+ * ```typescript
12
+ * import {Application} from 'lakutata'
13
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
14
+ *
15
+ * Application.run(() => ({
16
+ * id: 'file-cache.app',
17
+ * name: 'FileCache',
18
+ * components: {
19
+ * cache: buildCacherOptions({
20
+ * stores: {type: 'file', filename: './cache/data.json', namespace: 'app', writeDelay: 500},
21
+ * ttl: 3600000
22
+ * })
23
+ * }
24
+ * }))
25
+ * ```
26
+ */
27
+ export declare class FileCacheOptions extends DTO {
28
+ /**
29
+ * The store type: `'file'`.
30
+ */
31
+ type: 'file';
32
+ /**
33
+ * The path of the file, relative to the working directory; created (with its directories) at the first save.
34
+ */
35
+ filename: string;
36
+ /**
37
+ * The minimum interval, in milliseconds, between two purges of the expired entries from the file (done at a save).
38
+ * An expired entry is a miss anyway, and is removed when read.
39
+ * @default 3600000
40
+ */
41
+ expiredCheckDelay?: number;
42
+ /**
43
+ * The delay, in milliseconds, between a change and the save of the file: the changes made meanwhile are saved
44
+ * together. `0` saves at the next turn of the event loop.
45
+ * @default 100
46
+ */
47
+ writeDelay?: number;
48
+ /**
49
+ * The namespace of the entries: the keys are saved as `{namespace}:{key}`, and `clear()` deletes only the
50
+ * namespace's entries. Without it, `clear()` deletes all the file's entries.
51
+ * @default none
52
+ */
53
+ namespace?: string;
54
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FileCacheOptions = void 0;
13
+ const core_1 = require("@lakutata/core");
14
+ const dto_1 = require("@lakutata/core/decorator/dto");
15
+ /**
16
+ * The options of the file store: the entries are kept in memory and saved to one JSON file (loaded when the cache
17
+ * starts, written in batches, flushed when the cache is destroyed), so they survive a restart. It needs no optional
18
+ * dependency.
19
+ *
20
+ * The file is rewritten whole on each save (through a temporary file renamed over it), so it suits small caches;
21
+ * the processes sharing one file overwrite each other's entries. An unreadable file (not JSON) fails the cache's
22
+ * creation; a failed save is emitted as an `'error'` event of the cache. The values are kept as JSON.
23
+ * @example
24
+ * ```typescript
25
+ * import {Application} from 'lakutata'
26
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
27
+ *
28
+ * Application.run(() => ({
29
+ * id: 'file-cache.app',
30
+ * name: 'FileCache',
31
+ * components: {
32
+ * cache: buildCacherOptions({
33
+ * stores: {type: 'file', filename: './cache/data.json', namespace: 'app', writeDelay: 500},
34
+ * ttl: 3600000
35
+ * })
36
+ * }
37
+ * }))
38
+ * ```
39
+ */
40
+ class FileCacheOptions extends core_1.DTO {
41
+ }
42
+ exports.FileCacheOptions = FileCacheOptions;
43
+ __decorate([
44
+ (0, dto_1.Expect)(core_1.DTO.String().allow('file').only().required()),
45
+ __metadata("design:type", String)
46
+ ], FileCacheOptions.prototype, "type", void 0);
47
+ __decorate([
48
+ (0, dto_1.Expect)(core_1.DTO.String().required()),
49
+ __metadata("design:type", String)
50
+ ], FileCacheOptions.prototype, "filename", void 0);
51
+ __decorate([
52
+ (0, dto_1.Expect)(core_1.DTO.Number().positive().optional().default(3600 * 1000)),
53
+ __metadata("design:type", Number)
54
+ ], FileCacheOptions.prototype, "expiredCheckDelay", void 0);
55
+ __decorate([
56
+ (0, dto_1.Expect)(core_1.DTO.Number().positive().allow(0).optional().default(100)),
57
+ __metadata("design:type", Number)
58
+ ], FileCacheOptions.prototype, "writeDelay", void 0);
59
+ __decorate([
60
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
61
+ __metadata("design:type", String)
62
+ ], FileCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,57 @@
1
+ import { DTO } from '@lakutata/core';
2
+ /**
3
+ * The options of the Memcache store, a memcached server. It needs the optional peer dependency `memjs`
4
+ * (`npm install memjs`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
5
+ *
6
+ * The client connects on its first command, so an unreachable server does not fail the cache's creation: its reads
7
+ * are misses (emitted as `'error'` events), its writes reject. The TTLs are rounded up to the second by memcached
8
+ * (an entry is a miss once its TTL in milliseconds has passed). memcached cannot list its keys: `clear()` flushes the
9
+ * whole server, the entries of the other namespaces and applications included. The values are kept as JSON.
10
+ * @example
11
+ * ```typescript
12
+ * import {Application} from 'lakutata'
13
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
14
+ *
15
+ * Application.run(() => ({
16
+ * id: 'memcache.app',
17
+ * name: 'Memcache',
18
+ * components: {
19
+ * cache: buildCacherOptions({
20
+ * stores: {type: 'memcache', host: '127.0.0.1', port: 11211, namespace: 'app'},
21
+ * ttl: 60000
22
+ * })
23
+ * }
24
+ * }))
25
+ * ```
26
+ */
27
+ export declare class MemcacheCacheOptions extends DTO {
28
+ /**
29
+ * The store type: `'memcache'`.
30
+ */
31
+ type: 'memcache';
32
+ /**
33
+ * The host name or IP address of the memcached server.
34
+ */
35
+ host: string;
36
+ /**
37
+ * The port of the memcached server.
38
+ * @default 11211
39
+ */
40
+ port?: number;
41
+ /**
42
+ * The SASL user name, for a server requiring authentication.
43
+ * @default none
44
+ */
45
+ username?: string;
46
+ /**
47
+ * The SASL password, for a server requiring authentication.
48
+ * @default none
49
+ */
50
+ password?: string;
51
+ /**
52
+ * The namespace of the entries: the keys are stored as `{namespace}:{namespace}:{key}` (the format of lakutata 2.x).
53
+ * It does not limit `clear()`, which flushes the whole server.
54
+ * @default none
55
+ */
56
+ namespace?: string;
57
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MemcacheCacheOptions = void 0;
13
+ const core_1 = require("@lakutata/core");
14
+ const dto_1 = require("@lakutata/core/decorator/dto");
15
+ /**
16
+ * The options of the Memcache store, a memcached server. It needs the optional peer dependency `memjs`
17
+ * (`npm install memjs`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
18
+ *
19
+ * The client connects on its first command, so an unreachable server does not fail the cache's creation: its reads
20
+ * are misses (emitted as `'error'` events), its writes reject. The TTLs are rounded up to the second by memcached
21
+ * (an entry is a miss once its TTL in milliseconds has passed). memcached cannot list its keys: `clear()` flushes the
22
+ * whole server, the entries of the other namespaces and applications included. The values are kept as JSON.
23
+ * @example
24
+ * ```typescript
25
+ * import {Application} from 'lakutata'
26
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
27
+ *
28
+ * Application.run(() => ({
29
+ * id: 'memcache.app',
30
+ * name: 'Memcache',
31
+ * components: {
32
+ * cache: buildCacherOptions({
33
+ * stores: {type: 'memcache', host: '127.0.0.1', port: 11211, namespace: 'app'},
34
+ * ttl: 60000
35
+ * })
36
+ * }
37
+ * }))
38
+ * ```
39
+ */
40
+ class MemcacheCacheOptions extends core_1.DTO {
41
+ }
42
+ exports.MemcacheCacheOptions = MemcacheCacheOptions;
43
+ __decorate([
44
+ (0, dto_1.Expect)(core_1.DTO.String().allow('memcache').only().required()),
45
+ __metadata("design:type", String)
46
+ ], MemcacheCacheOptions.prototype, "type", void 0);
47
+ __decorate([
48
+ (0, dto_1.Expect)(core_1.DTO.String().required()),
49
+ __metadata("design:type", String)
50
+ ], MemcacheCacheOptions.prototype, "host", void 0);
51
+ __decorate([
52
+ (0, dto_1.Expect)(core_1.DTO.Number().positive().port().optional().default(11211)),
53
+ __metadata("design:type", Number)
54
+ ], MemcacheCacheOptions.prototype, "port", void 0);
55
+ __decorate([
56
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
57
+ __metadata("design:type", String)
58
+ ], MemcacheCacheOptions.prototype, "username", void 0);
59
+ __decorate([
60
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
61
+ __metadata("design:type", String)
62
+ ], MemcacheCacheOptions.prototype, "password", void 0);
63
+ __decorate([
64
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
65
+ __metadata("design:type", String)
66
+ ], MemcacheCacheOptions.prototype, "namespace", void 0);
@@ -0,0 +1,39 @@
1
+ import { DTO } from '@lakutata/core';
2
+ /**
3
+ * The options of the in-memory store: the entries are kept in the process (lost when it ends, not shared with the
4
+ * other processes), the values as they are (not copied, not converted to JSON). It needs no optional dependency.
5
+ *
6
+ * It is the store of a cache without `stores`; declared, it is a tier in front of a slower store (the first store of
7
+ * the list is read first, the hits of a later store are written back to the earlier ones).
8
+ * @example
9
+ * ```typescript
10
+ * import {Application} from 'lakutata'
11
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
12
+ *
13
+ * Application.run(() => ({
14
+ * id: 'tiered-cache.app',
15
+ * name: 'TieredCache',
16
+ * components: {
17
+ * //A memory tier in front of Redis
18
+ * cache: buildCacherOptions({
19
+ * stores: [
20
+ * {type: 'memory', sweepInterval: 30000},
21
+ * {type: 'redis', host: '127.0.0.1', namespace: 'app'}
22
+ * ],
23
+ * ttl: 60000
24
+ * })
25
+ * }
26
+ * }))
27
+ * ```
28
+ */
29
+ export declare class MemoryCacheOptions extends DTO {
30
+ /**
31
+ * The store type: `'memory'`.
32
+ */
33
+ type: 'memory';
34
+ /**
35
+ * The interval, in milliseconds, between two removals of the expired entries (an expired entry is a miss anyway).
36
+ * @default 60000
37
+ */
38
+ sweepInterval?: number;
39
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MemoryCacheOptions = void 0;
13
+ const core_1 = require("@lakutata/core");
14
+ const dto_1 = require("@lakutata/core/decorator/dto");
15
+ /**
16
+ * The options of the in-memory store: the entries are kept in the process (lost when it ends, not shared with the
17
+ * other processes), the values as they are (not copied, not converted to JSON). It needs no optional dependency.
18
+ *
19
+ * It is the store of a cache without `stores`; declared, it is a tier in front of a slower store (the first store of
20
+ * the list is read first, the hits of a later store are written back to the earlier ones).
21
+ * @example
22
+ * ```typescript
23
+ * import {Application} from 'lakutata'
24
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
25
+ *
26
+ * Application.run(() => ({
27
+ * id: 'tiered-cache.app',
28
+ * name: 'TieredCache',
29
+ * components: {
30
+ * //A memory tier in front of Redis
31
+ * cache: buildCacherOptions({
32
+ * stores: [
33
+ * {type: 'memory', sweepInterval: 30000},
34
+ * {type: 'redis', host: '127.0.0.1', namespace: 'app'}
35
+ * ],
36
+ * ttl: 60000
37
+ * })
38
+ * }
39
+ * }))
40
+ * ```
41
+ */
42
+ class MemoryCacheOptions extends core_1.DTO {
43
+ }
44
+ exports.MemoryCacheOptions = MemoryCacheOptions;
45
+ __decorate([
46
+ (0, dto_1.Expect)(core_1.DTO.String().allow('memory').only().required()),
47
+ __metadata("design:type", String)
48
+ ], MemoryCacheOptions.prototype, "type", void 0);
49
+ __decorate([
50
+ (0, dto_1.Expect)(core_1.DTO.Number().positive().optional().default(60000)),
51
+ __metadata("design:type", Number)
52
+ ], MemoryCacheOptions.prototype, "sweepInterval", void 0);
@@ -0,0 +1,72 @@
1
+ import { DTO } from '@lakutata/core';
2
+ /**
3
+ * The options of the MongoDB store: one document per entry in a collection. It needs the optional peer dependency
4
+ * `mongodb` (`npm install mongodb`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
5
+ *
6
+ * When the cache starts, it connects to `mongodb://{username}:{password}@{host}:{port}` (the user authenticated
7
+ * against the `admin` database; `mongodb://{host}:{port}` without a username) and creates the collection's
8
+ * indexes: a unique one on `key`, and a TTL index on
9
+ * `expiresAt` by which MongoDB deletes the expired documents (its TTL monitor runs about once a minute; an expired
10
+ * document is a miss before). A failed connection fails the cache's creation. The values are kept as JSON.
11
+ * @example
12
+ * ```typescript
13
+ * import {Application} from 'lakutata'
14
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
15
+ *
16
+ * Application.run(() => ({
17
+ * id: 'mongo-cache.app',
18
+ * name: 'MongoCache',
19
+ * components: {
20
+ * cache: buildCacherOptions({
21
+ * stores: {
22
+ * type: 'mongo',
23
+ * host: '127.0.0.1',
24
+ * database: 'app',
25
+ * collection: 'cache',
26
+ * username: 'app',
27
+ * password: process.env.MONGO_PASSWORD ?? ''
28
+ * }
29
+ * })
30
+ * }
31
+ * }))
32
+ * ```
33
+ */
34
+ export declare class MongoCacheOptions extends DTO {
35
+ /**
36
+ * The store type: `'mongo'`.
37
+ */
38
+ type: 'mongo';
39
+ /**
40
+ * The host name or IP address of the MongoDB server.
41
+ */
42
+ host: string;
43
+ /**
44
+ * The database holding the collection.
45
+ */
46
+ database: string;
47
+ /**
48
+ * The collection of the entries, created when missing.
49
+ */
50
+ collection: string;
51
+ /**
52
+ * The port of the MongoDB server.
53
+ * @default 27017
54
+ */
55
+ port?: number;
56
+ /**
57
+ * The user name; without it, no authentication.
58
+ * @default none (no authentication)
59
+ */
60
+ username?: string;
61
+ /**
62
+ * The user's password; empty or omitted for a user without password (trust authentication).
63
+ * @default none
64
+ */
65
+ password?: string;
66
+ /**
67
+ * The namespace of the entries: the keys are stored as `{namespace}:{key}`, and `clear()` deletes only the
68
+ * namespace's documents. Without it, `clear()` deletes all the collection's documents.
69
+ * @default none
70
+ */
71
+ namespace?: string;
72
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MongoCacheOptions = void 0;
13
+ const core_1 = require("@lakutata/core");
14
+ const dto_1 = require("@lakutata/core/decorator/dto");
15
+ /**
16
+ * The options of the MongoDB store: one document per entry in a collection. It needs the optional peer dependency
17
+ * `mongodb` (`npm install mongodb`): without it, the cache's creation fails with `CacheDriverNotFoundException`.
18
+ *
19
+ * When the cache starts, it connects to `mongodb://{username}:{password}@{host}:{port}` (the user authenticated
20
+ * against the `admin` database; `mongodb://{host}:{port}` without a username) and creates the collection's
21
+ * indexes: a unique one on `key`, and a TTL index on
22
+ * `expiresAt` by which MongoDB deletes the expired documents (its TTL monitor runs about once a minute; an expired
23
+ * document is a miss before). A failed connection fails the cache's creation. The values are kept as JSON.
24
+ * @example
25
+ * ```typescript
26
+ * import {Application} from 'lakutata'
27
+ * import {buildCacherOptions} from 'lakutata/com/cacher'
28
+ *
29
+ * Application.run(() => ({
30
+ * id: 'mongo-cache.app',
31
+ * name: 'MongoCache',
32
+ * components: {
33
+ * cache: buildCacherOptions({
34
+ * stores: {
35
+ * type: 'mongo',
36
+ * host: '127.0.0.1',
37
+ * database: 'app',
38
+ * collection: 'cache',
39
+ * username: 'app',
40
+ * password: process.env.MONGO_PASSWORD ?? ''
41
+ * }
42
+ * })
43
+ * }
44
+ * }))
45
+ * ```
46
+ */
47
+ class MongoCacheOptions extends core_1.DTO {
48
+ }
49
+ exports.MongoCacheOptions = MongoCacheOptions;
50
+ __decorate([
51
+ (0, dto_1.Expect)(core_1.DTO.String().allow('mongo').only().required()),
52
+ __metadata("design:type", String)
53
+ ], MongoCacheOptions.prototype, "type", void 0);
54
+ __decorate([
55
+ (0, dto_1.Expect)(core_1.DTO.String().required()),
56
+ __metadata("design:type", String)
57
+ ], MongoCacheOptions.prototype, "host", void 0);
58
+ __decorate([
59
+ (0, dto_1.Expect)(core_1.DTO.String().required()),
60
+ __metadata("design:type", String)
61
+ ], MongoCacheOptions.prototype, "database", void 0);
62
+ __decorate([
63
+ (0, dto_1.Expect)(core_1.DTO.String().required()),
64
+ __metadata("design:type", String)
65
+ ], MongoCacheOptions.prototype, "collection", void 0);
66
+ __decorate([
67
+ (0, dto_1.Expect)(core_1.DTO.Number().positive().port().optional().default(27017)),
68
+ __metadata("design:type", Number)
69
+ ], MongoCacheOptions.prototype, "port", void 0);
70
+ __decorate([
71
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
72
+ __metadata("design:type", String)
73
+ ], MongoCacheOptions.prototype, "username", void 0);
74
+ __decorate([
75
+ (0, dto_1.Expect)(core_1.DTO.String().allow('').optional()),
76
+ __metadata("design:type", String)
77
+ ], MongoCacheOptions.prototype, "password", void 0);
78
+ __decorate([
79
+ (0, dto_1.Expect)(core_1.DTO.String().optional()),
80
+ __metadata("design:type", String)
81
+ ], MongoCacheOptions.prototype, "namespace", void 0);