@daiso-tech/core 0.38.0 → 0.40.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.
- package/README.md +5 -1
- package/dist/async/backof-policies/constant-backoff-policy/constant-backoff-policy.d.ts +1 -1
- package/dist/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.d.ts +2 -2
- package/dist/async/backof-policies/linear-backoff-policy/linear-backoff-policy.d.ts +1 -1
- package/dist/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.d.ts +2 -2
- package/dist/async/middlewares/bulkhead/bulkhead.middleware.d.ts +2 -2
- package/dist/async/middlewares/hedging/sequential-hedging.middleware.js +2 -2
- package/dist/async/middlewares/hedging/sequential-hedging.middleware.js.map +1 -1
- package/dist/async/middlewares/retry/retry.middleware.js +2 -2
- package/dist/async/middlewares/retry/retry.middleware.js.map +1 -1
- package/dist/async/middlewares/retry/retry.types.d.ts +1 -1
- package/dist/cache/contracts/cache-adapter.contract.d.ts +9 -9
- package/dist/cache/contracts/cache.contract.d.ts +7 -7
- package/dist/cache/contracts/cache.errors.d.ts +3 -26
- package/dist/cache/contracts/cache.errors.js +0 -47
- package/dist/cache/contracts/cache.errors.js.map +1 -1
- package/dist/cache/contracts/database-cache-adapter.contract.d.ts +10 -10
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.d.ts +6 -5
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js +17 -13
- package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.d.ts +15 -3
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +25 -9
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.d.ts +9 -9
- package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.d.ts +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js +2 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +2 -2
- package/dist/cache/implementations/derivables/cache/cache.js +1 -1
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/collection/contracts/async-collection.contract.d.ts +0 -1
- package/dist/collection/contracts/async-collection.contract.js +3 -1
- package/dist/collection/contracts/async-collection.contract.js.map +1 -1
- package/dist/collection/contracts/collection.contract.d.ts +0 -1
- package/dist/collection/contracts/collection.contract.js +3 -1
- package/dist/collection/contracts/collection.contract.js.map +1 -1
- package/dist/collection/contracts/collection.errors.d.ts +1 -26
- package/dist/collection/contracts/collection.errors.js +0 -55
- package/dist/collection/contracts/collection.errors.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-split-iterable.js +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-split-iterable.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-zip-iterable.js +1 -1
- package/dist/collection/implementations/async-iterable-collection/_shared/async-zip-iterable.js.map +1 -1
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +9 -6
- package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/_shared/split-iterable.js +1 -1
- package/dist/collection/implementations/iterable-collection/_shared/split-iterable.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/_shared/zip-iterable.js +1 -1
- package/dist/collection/implementations/iterable-collection/_shared/zip-iterable.js.map +1 -1
- package/dist/collection/implementations/iterable-collection/iterable-collection.js +9 -6
- package/dist/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
- package/dist/collection/implementations/list-collection/list-collection.js +12 -9
- package/dist/collection/implementations/list-collection/list-collection.js.map +1 -1
- package/dist/event-bus/contracts/_module-exports.d.ts +0 -1
- package/dist/event-bus/contracts/_module-exports.js +0 -1
- package/dist/event-bus/contracts/_module-exports.js.map +1 -1
- package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +3 -3
- package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +3 -3
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +2 -2
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +3 -2
- package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +2 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +26 -26
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.d.ts +11 -4
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js +17 -12
- package/dist/event-bus/implementations/derivables/event-bus/listener-store.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +17 -20
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +159 -752
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/database-lock-adapter.contract.d.ts +56 -16
- package/dist/lock/contracts/lock-adapter.contract.d.ts +32 -9
- package/dist/lock/contracts/lock-adapter.contract.js +10 -1
- package/dist/lock/contracts/lock-adapter.contract.js.map +1 -1
- package/dist/lock/contracts/lock.contract.d.ts +30 -25
- package/dist/lock/contracts/lock.errors.d.ts +11 -28
- package/dist/lock/contracts/lock.errors.js +10 -74
- package/dist/lock/contracts/lock.errors.js.map +1 -1
- package/dist/lock/contracts/lock.events.d.ts +15 -3
- package/dist/lock/contracts/lock.events.js +2 -1
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +40 -10
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +135 -37
- package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +20 -7
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +59 -35
- package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +23 -12
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +152 -87
- package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.d.ts +5 -5
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js +3 -2
- package/dist/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +6 -6
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +46 -24
- package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +4 -4
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +45 -12
- package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js +7 -5
- package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +3 -3
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +2 -3
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +4 -3
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-state.d.ts +8 -11
- package/dist/lock/implementations/derivables/lock-provider/lock-state.js +9 -26
- package/dist/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +14 -16
- package/dist/lock/implementations/derivables/lock-provider/lock.js +106 -63
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +2 -2
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +184 -166
- package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +240 -41
- package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +2704 -1514
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/implementations/derivables/serde.d.ts +22 -22
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js +2 -1
- package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js.map +1 -1
- package/dist/utilities/classes/namespace/namespace.d.ts +3 -3
- package/dist/utilities/classes/namespace/namespace.js +2 -2
- package/dist/utilities/classes/namespace/namespace.js.map +1 -1
- package/dist/utilities/classes/time-span/time-span.d.ts +10 -2
- package/dist/utilities/classes/time-span/time-span.js +10 -2
- package/dist/utilities/classes/time-span/time-span.js.map +1 -1
- package/dist/utilities/contracts/deinitizable.contract.d.ts +1 -1
- package/dist/utilities/contracts/initizable.contract.d.ts +1 -1
- package/dist/utilities/contracts/prunable.contract.d.ts +1 -1
- package/dist/utilities/errors.d.ts +8 -0
- package/dist/utilities/errors.js +11 -0
- package/dist/utilities/errors.js.map +1 -1
- package/dist/utilities/functions/_module.d.ts +1 -0
- package/dist/utilities/functions/_module.js +1 -0
- package/dist/utilities/functions/_module.js.map +1 -1
- package/dist/utilities/functions/is-class.d.ts +4 -0
- package/dist/utilities/functions/is-class.js +4 -0
- package/dist/utilities/functions/is-class.js.map +1 -1
- package/dist/utilities/functions/is-positive-nbr.d.ts +8 -0
- package/dist/utilities/functions/is-positive-nbr.js +19 -0
- package/dist/utilities/functions/is-positive-nbr.js.map +1 -0
- package/package.json +1 -1
- package/dist/event-bus/contracts/event-bus.errors.d.ts +0 -40
- package/dist/event-bus/contracts/event-bus.errors.js +0 -62
- package/dist/event-bus/contracts/event-bus.errors.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"memory-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EACH,mBAAmB,GAGtB,MAAM,qCAAqC,CAAC;AAkB7C;;;;;;GAMG;AACH,MAAM,OAAO,iBAAiB;IAiBG;IAhB7B;;;;;;;;;;;;;;;OAeG;IACH,YAA6B,MAAM,IAAI,GAAG,EAA0B;QAAvC,QAAG,GAAH,GAAG,CAAoC;IAAG,CAAC;IAExE,4DAA4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,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAC7B,MAAM,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;YAC9B,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC,EAAE,GAAG,CAAC,cAAc,EAAE,CAAC,CAAC;QACzB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE;YACd,GAAG,IAAI;YACP,SAAS;SACZ,CAAC,CAAC;QAEH,OAAO,mBAAmB,CAAC,SAAS,CAAC;IACzC,CAAC;CACJ"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
5
|
-
import type
|
|
4
|
+
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
5
|
+
import { type IDeinitizable, type IInitizable } from "../../../../utilities/_module-exports.js";
|
|
6
|
+
import { type ILockAdapter, type LockRefreshResult } from "../../../../lock/contracts/_module-exports.js";
|
|
6
7
|
import type { CollectionOptions, Db } from "mongodb";
|
|
8
|
+
import type { ObjectId } from "mongodb";
|
|
7
9
|
/**
|
|
8
10
|
*
|
|
9
11
|
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
@@ -12,20 +14,31 @@ import type { CollectionOptions, Db } from "mongodb";
|
|
|
12
14
|
export type MongodbLockAdapterSettings = {
|
|
13
15
|
database: Db;
|
|
14
16
|
/**
|
|
15
|
-
* @default
|
|
17
|
+
* @default "lock"
|
|
16
18
|
*/
|
|
17
19
|
collectionName?: string;
|
|
18
20
|
collectionSettings?: CollectionOptions;
|
|
19
21
|
};
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
23
|
+
*
|
|
24
|
+
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
25
|
+
* @group Adapters
|
|
26
|
+
*/
|
|
27
|
+
export type MongodbLockDocument = {
|
|
28
|
+
_id: ObjectId;
|
|
29
|
+
key: string;
|
|
30
|
+
owner: string;
|
|
31
|
+
expiration: Date | null;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* To utilize the `MongodbLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package.
|
|
22
35
|
*
|
|
23
36
|
* Note in order to use `MongodbLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
|
|
24
37
|
*
|
|
25
38
|
* IMPORT_PATH: `"@daiso-tech/core/lock/adapters"`
|
|
26
39
|
* @group Adapters
|
|
27
40
|
*/
|
|
28
|
-
export declare class MongodbLockAdapter implements
|
|
41
|
+
export declare class MongodbLockAdapter implements ILockAdapter, IDeinitizable, IInitizable {
|
|
29
42
|
private readonly database;
|
|
30
43
|
private readonly collection;
|
|
31
44
|
private readonly collectionName;
|
|
@@ -44,8 +57,7 @@ export declare class MongodbLockAdapter implements IDatabaseLockAdapter, IDeinit
|
|
|
44
57
|
* await lockAdapter.init()
|
|
45
58
|
* ```
|
|
46
59
|
*/
|
|
47
|
-
constructor(
|
|
48
|
-
removeAllExpired(): Promise<void>;
|
|
60
|
+
constructor(settings: MongodbLockAdapterSettings);
|
|
49
61
|
/**
|
|
50
62
|
* Creates all related indexes.
|
|
51
63
|
* Note the `init` method needs to be called before using the adapter.
|
|
@@ -56,9 +68,8 @@ export declare class MongodbLockAdapter implements IDatabaseLockAdapter, IDeinit
|
|
|
56
68
|
* Note all lock data will be removed.
|
|
57
69
|
*/
|
|
58
70
|
deInit(): Promise<void>;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
refresh(key: string, owner: string,
|
|
63
|
-
find(key: string): Promise<ILockData | null>;
|
|
71
|
+
acquire(key: string, owner: string, ttl: TimeSpan | null): Promise<boolean>;
|
|
72
|
+
release(key: string, owner: string): Promise<boolean>;
|
|
73
|
+
forceRelease(key: string): Promise<boolean>;
|
|
74
|
+
refresh(key: string, owner: string, ttl: TimeSpan): Promise<LockRefreshResult>;
|
|
64
75
|
}
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
4
|
import {} from "../../../../utilities/_module-exports.js";
|
|
5
|
-
import {
|
|
6
|
-
import { ObjectId } from "mongodb";
|
|
5
|
+
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
|
|
7
6
|
/**
|
|
8
|
-
* To utilize the `MongodbLockAdapter`, you must install the `"mongodb"` package.
|
|
7
|
+
* To utilize the `MongodbLockAdapter`, you must install the [`"mongodb"`](https://www.npmjs.com/package/mongodb) package.
|
|
9
8
|
*
|
|
10
9
|
* Note in order to use `MongodbLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
|
|
11
10
|
*
|
|
@@ -31,126 +30,192 @@ export class MongodbLockAdapter {
|
|
|
31
30
|
* await lockAdapter.init()
|
|
32
31
|
* ```
|
|
33
32
|
*/
|
|
34
|
-
constructor(
|
|
33
|
+
constructor(settings) {
|
|
34
|
+
const { collectionName = "lock", collectionSettings, database, } = settings;
|
|
35
35
|
this.collectionName = collectionName;
|
|
36
36
|
this.database = database;
|
|
37
37
|
this.collection = database.collection(collectionName, collectionSettings);
|
|
38
38
|
}
|
|
39
|
-
async removeAllExpired() {
|
|
40
|
-
await this.collection.deleteMany({
|
|
41
|
-
expiresAt: {
|
|
42
|
-
$lte: new Date(),
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
39
|
/**
|
|
47
40
|
* Creates all related indexes.
|
|
48
41
|
* Note the `init` method needs to be called before using the adapter.
|
|
49
42
|
*/
|
|
50
43
|
async init() {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
44
|
+
// Should throw if the index already exists thats why the try catch is used.
|
|
45
|
+
try {
|
|
46
|
+
await this.collection.createIndex({
|
|
47
|
+
key: 1,
|
|
48
|
+
}, {
|
|
49
|
+
unique: true,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
/* EMPTY */
|
|
54
|
+
}
|
|
55
|
+
// Should throw if the index already exists thats why the try catch is used.
|
|
56
|
+
try {
|
|
57
|
+
await this.collection.createIndex("expiration", {
|
|
58
|
+
expireAfterSeconds: 0,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
/* EMPTY */
|
|
63
|
+
}
|
|
59
64
|
}
|
|
60
65
|
/**
|
|
61
66
|
* Removes the collection where the lock keys are stored and all it's related indexes.
|
|
62
67
|
* Note all lock data will be removed.
|
|
63
68
|
*/
|
|
64
69
|
async deInit() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
// Should throw if the collection already does not exists thats why the try catch is used.
|
|
71
|
+
try {
|
|
72
|
+
await this.collection.dropIndexes();
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
/* EMPTY */
|
|
76
|
+
}
|
|
77
|
+
// Should throw if the collection already does not exists thats why the try catch is used.
|
|
78
|
+
try {
|
|
79
|
+
await this.database.dropCollection(this.collectionName);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
/* EMPTY */
|
|
77
83
|
}
|
|
78
84
|
}
|
|
79
|
-
async
|
|
80
|
-
const
|
|
81
|
-
|
|
85
|
+
async acquire(key, owner, ttl) {
|
|
86
|
+
const expiration = ttl?.toEndDate() ?? null;
|
|
87
|
+
const isExpiredQuery = {
|
|
82
88
|
$and: [
|
|
83
89
|
{
|
|
84
|
-
|
|
85
|
-
$ne: null,
|
|
86
|
-
},
|
|
90
|
+
$ne: ["$expiration", null],
|
|
87
91
|
},
|
|
88
92
|
{
|
|
89
|
-
|
|
90
|
-
$lte: new Date(),
|
|
91
|
-
},
|
|
93
|
+
$lte: ["$expiration", new Date()],
|
|
92
94
|
},
|
|
93
95
|
],
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
};
|
|
97
|
+
const lockData = await this.collection.findOneAndUpdate({
|
|
98
|
+
key,
|
|
99
|
+
}, [
|
|
100
|
+
{
|
|
101
|
+
$set: {
|
|
102
|
+
key,
|
|
103
|
+
owner: {
|
|
104
|
+
$ifNull: ["$owner", owner],
|
|
105
|
+
},
|
|
106
|
+
expiration: {
|
|
107
|
+
$ifNull: ["$expiration", expiration],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
$set: {
|
|
113
|
+
owner: {
|
|
114
|
+
$cond: {
|
|
115
|
+
if: isExpiredQuery,
|
|
116
|
+
then: owner,
|
|
117
|
+
else: "$owner",
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
expiration: {
|
|
121
|
+
$cond: {
|
|
122
|
+
if: isExpiredQuery,
|
|
123
|
+
then: expiration,
|
|
124
|
+
else: "$expiration",
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
98
128
|
},
|
|
129
|
+
], {
|
|
130
|
+
upsert: true,
|
|
99
131
|
});
|
|
100
|
-
if (
|
|
101
|
-
|
|
132
|
+
if (lockData === null) {
|
|
133
|
+
return true;
|
|
102
134
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
async remove(key, owner) {
|
|
106
|
-
if (owner === null) {
|
|
107
|
-
const deleteResult = await this.collection.deleteOne({
|
|
108
|
-
key,
|
|
109
|
-
});
|
|
110
|
-
if (!deleteResult.acknowledged) {
|
|
111
|
-
throw new UnexpectedLockError("Mongodb deletion was not acknowledged");
|
|
112
|
-
}
|
|
113
|
-
return;
|
|
135
|
+
if (lockData.expiration === null) {
|
|
136
|
+
return false;
|
|
114
137
|
}
|
|
115
|
-
|
|
138
|
+
return lockData.expiration <= new Date();
|
|
139
|
+
}
|
|
140
|
+
async release(key, owner) {
|
|
141
|
+
const isUnexpirableQuery = {
|
|
142
|
+
expiration: {
|
|
143
|
+
$eq: null,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
const isUnexpiredQuery = {
|
|
147
|
+
expiration: {
|
|
148
|
+
$gt: new Date(),
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
const lockData = await this.collection.findOneAndDelete({
|
|
116
152
|
key,
|
|
117
153
|
owner,
|
|
154
|
+
$or: [isUnexpirableQuery, isUnexpiredQuery],
|
|
118
155
|
});
|
|
119
|
-
if (
|
|
120
|
-
|
|
156
|
+
if (lockData === null) {
|
|
157
|
+
return false;
|
|
121
158
|
}
|
|
159
|
+
const { expiration } = lockData;
|
|
160
|
+
const hasNoExpiration = expiration === null;
|
|
161
|
+
if (hasNoExpiration) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
const { owner: currentOwner } = lockData;
|
|
165
|
+
const isNotExpired = expiration > new Date();
|
|
166
|
+
const isCurrentOwner = owner === currentOwner;
|
|
167
|
+
return isNotExpired && isCurrentOwner;
|
|
122
168
|
}
|
|
123
|
-
async
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
if (!updateResult.acknowledged) {
|
|
133
|
-
throw new UnexpectedLockError("Mongodb update was not acknowledged");
|
|
169
|
+
async forceRelease(key) {
|
|
170
|
+
const lockData = await this.collection.findOneAndDelete({ key });
|
|
171
|
+
if (lockData === null) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
if (lockData.expiration === null) {
|
|
175
|
+
return true;
|
|
134
176
|
}
|
|
135
|
-
|
|
177
|
+
const isNotExpired = lockData.expiration >= new Date();
|
|
178
|
+
return isNotExpired;
|
|
136
179
|
}
|
|
137
|
-
async
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
180
|
+
async refresh(key, owner, ttl) {
|
|
181
|
+
const isUnexpiredQuery = {
|
|
182
|
+
$and: [
|
|
183
|
+
{
|
|
184
|
+
$ne: ["$expiration", null],
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
$gt: ["$expiration", new Date()],
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
const lockData = await this.collection.findOneAndUpdate({
|
|
192
|
+
key,
|
|
193
|
+
}, [
|
|
194
|
+
{
|
|
195
|
+
$set: {
|
|
196
|
+
expiration: {
|
|
197
|
+
$cond: {
|
|
198
|
+
if: isUnexpiredQuery,
|
|
199
|
+
then: ttl.toEndDate(),
|
|
200
|
+
else: "$expiration",
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
145
204
|
},
|
|
146
|
-
|
|
147
|
-
if (
|
|
148
|
-
return
|
|
205
|
+
]);
|
|
206
|
+
if (lockData === null) {
|
|
207
|
+
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
|
|
149
208
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
209
|
+
if (lockData.owner !== owner) {
|
|
210
|
+
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
|
|
211
|
+
}
|
|
212
|
+
if (lockData.expiration === null) {
|
|
213
|
+
return LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY;
|
|
214
|
+
}
|
|
215
|
+
if (lockData.expiration <= new Date()) {
|
|
216
|
+
return LOCK_REFRESH_RESULT.UNOWNED_REFRESH;
|
|
217
|
+
}
|
|
218
|
+
return LOCK_REFRESH_RESULT.REFRESHED;
|
|
154
219
|
}
|
|
155
220
|
}
|
|
156
221
|
//# sourceMappingURL=mongodb-lock-adapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongodb-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"mongodb-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAGN,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACH,mBAAmB,GAGtB,MAAM,qCAAqC,CAAC;AA8B7C;;;;;;;GAOG;AACH,MAAM,OAAO,kBAAkB;IAGV,QAAQ,CAAK;IACb,UAAU,CAAkC;IAC5C,cAAc,CAAS;IAExC;;;;;;;;;;;;;;OAcG;IACH,YAAY,QAAoC;QAC5C,MAAM,EACF,cAAc,GAAG,MAAM,EACvB,kBAAkB,EAClB,QAAQ,GACX,GAAG,QAAQ,CAAC;QACb,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,QAAQ,CAAC,UAAU,CACjC,cAAc,EACd,kBAAkB,CACrB,CAAC;IACN,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,IAAI;QACN,4EAA4E;QAC5E,IAAI,CAAC;YACD,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAC7B;gBACI,GAAG,EAAE,CAAC;aACT,EACD;gBACI,MAAM,EAAE,IAAI;aACf,CACJ,CAAC;QACN,CAAC;QAAC,MAAM,CAAC;YACL,WAAW;QACf,CAAC;QAED,4EAA4E;QAC5E,IAAI,CAAC;YACD,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,YAAY,EAAE;gBAC5C,kBAAkB,EAAE,CAAC;aACxB,CAAC,CAAC;QACP,CAAC;QAAC,MAAM,CAAC;YACL,WAAW;QACf,CAAC;IACL,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM;QACR,0FAA0F;QAC1F,IAAI,CAAC;YACD,MAAM,IAAI,CAAC,U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mBAAmB,CAAC,SAAS,CAAC;IACzC,CAAC;CACJ"}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
4
|
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
5
|
-
import type
|
|
5
|
+
import { type ILockAdapter, type LockRefreshResult } from "../../../../lock/contracts/_module-exports.js";
|
|
6
6
|
/**
|
|
7
7
|
* This `NoOpLockAdapter` will do nothing and is used for easily mocking {@link ILockProvider | `ILockProvider`} for testing.
|
|
8
8
|
*
|
|
@@ -10,8 +10,8 @@ import type { ILockAdapter } from "../../../../lock/contracts/_module-exports.js
|
|
|
10
10
|
* @group Adapters
|
|
11
11
|
*/
|
|
12
12
|
export declare class NoOpLockAdapter implements ILockAdapter {
|
|
13
|
-
acquire(_key: string, _owner: string, _ttl: TimeSpan | null):
|
|
14
|
-
release(_key: string, _owner: string):
|
|
15
|
-
forceRelease(_key: string):
|
|
16
|
-
refresh(_key: string, _owner: string, _ttl: TimeSpan):
|
|
13
|
+
acquire(_key: string, _owner: string, _ttl: TimeSpan | null): Promise<boolean>;
|
|
14
|
+
release(_key: string, _owner: string): Promise<boolean>;
|
|
15
|
+
forceRelease(_key: string): Promise<boolean>;
|
|
16
|
+
refresh(_key: string, _owner: string, _ttl: TimeSpan): Promise<LockRefreshResult>;
|
|
17
17
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
+
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
|
|
4
5
|
/**
|
|
5
6
|
* This `NoOpLockAdapter` will do nothing and is used for easily mocking {@link ILockProvider | `ILockProvider`} for testing.
|
|
6
7
|
*
|
|
@@ -15,10 +16,10 @@ export class NoOpLockAdapter {
|
|
|
15
16
|
return Promise.resolve(true);
|
|
16
17
|
}
|
|
17
18
|
forceRelease(_key) {
|
|
18
|
-
return Promise.resolve();
|
|
19
|
+
return Promise.resolve(true);
|
|
19
20
|
}
|
|
20
21
|
refresh(_key, _owner, _ttl) {
|
|
21
|
-
return Promise.resolve(
|
|
22
|
+
return Promise.resolve(LOCK_REFRESH_RESULT.REFRESHED);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
//# sourceMappingURL=no-op-lock-adapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-op-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"no-op-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/no-op-lock-adapter/no-op-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EACH,mBAAmB,GAGtB,MAAM,qCAAqC,CAAC;AAI7C;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IACxB,OAAO,CACH,IAAY,EACZ,MAAc,EACd,IAAqB;QAErB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,CAAC,IAAY,EAAE,MAAc;QAChC,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,YAAY,CAAC,IAAY;QACrB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,CACH,IAAY,EACZ,MAAc,EACd,IAAc;QAEd,OAAO,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAC1D,CAAC;CACJ"}
|
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
4
|
import type { TimeSpan } from "../../../../utilities/_module-exports.js";
|
|
5
|
-
import type
|
|
5
|
+
import { LOCK_REFRESH_RESULT, type ILockAdapter, type LockRefreshResult } from "../../../../lock/contracts/_module-exports.js";
|
|
6
6
|
import type { Redis } from "ioredis";
|
|
7
7
|
import type { Result } from "ioredis";
|
|
8
8
|
declare module "ioredis" {
|
|
9
9
|
interface RedisCommander<Context> {
|
|
10
|
-
daiso_lock_acquire(key: string, owner: string,
|
|
10
|
+
daiso_lock_acquire(key: string, owner: string, expiration: string): Result<number, Context>;
|
|
11
11
|
daiso_lock_release(key: string, owner: string): Result<number, Context>;
|
|
12
|
-
daiso_lock_refresh(key: string, owner: string,
|
|
12
|
+
daiso_lock_refresh(key: string, owner: string, expiration: string, REFRESHED: typeof LOCK_REFRESH_RESULT.REFRESHED, UNOWNED_REFRESH: typeof LOCK_REFRESH_RESULT.UNOWNED_REFRESH, UNEXPIRABLE_KEY: typeof LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY): Result<LockRefreshResult, Context>;
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
|
-
* To utilize the `RedisLockAdapter`, you must install the `"ioredis"` package.
|
|
16
|
+
* To utilize the `RedisLockAdapter`, you must install the [`"ioredis"`](https://www.npmjs.com/package/ioredis) package.
|
|
17
17
|
*
|
|
18
18
|
* Note in order to use `RedisLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
|
|
19
19
|
*
|
|
@@ -38,6 +38,6 @@ export declare class RedisLockAdapter implements ILockAdapter {
|
|
|
38
38
|
private initRefreshComand;
|
|
39
39
|
acquire(key: string, owner: string, ttl: TimeSpan | null): Promise<boolean>;
|
|
40
40
|
release(key: string, owner: string): Promise<boolean>;
|
|
41
|
-
forceRelease(key: string): Promise<
|
|
42
|
-
refresh(key: string, owner: string, ttl: TimeSpan): Promise<
|
|
41
|
+
forceRelease(key: string): Promise<boolean>;
|
|
42
|
+
refresh(key: string, owner: string, ttl: TimeSpan): Promise<LockRefreshResult>;
|
|
43
43
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module Lock
|
|
3
3
|
*/
|
|
4
|
+
import { LOCK_REFRESH_RESULT, } from "../../../../lock/contracts/_module-exports.js";
|
|
4
5
|
/**
|
|
5
|
-
* To utilize the `RedisLockAdapter`, you must install the `"ioredis"` package.
|
|
6
|
+
* To utilize the `RedisLockAdapter`, you must install the [`"ioredis"`](https://www.npmjs.com/package/ioredis) package.
|
|
6
7
|
*
|
|
7
8
|
* Note in order to use `RedisLockAdapter` correctly, ensure you use a single, consistent database across all server instances.
|
|
8
9
|
*
|
|
@@ -36,21 +37,20 @@ export class RedisLockAdapter {
|
|
|
36
37
|
lua: `
|
|
37
38
|
local key = KEYS[1];
|
|
38
39
|
local owner = ARGV[1];
|
|
39
|
-
local
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if hasKey == 1 then
|
|
40
|
+
local expiration = tonumber(ARGV[2])
|
|
41
|
+
|
|
42
|
+
if redis.call("exists", key) == 1 then
|
|
43
43
|
return 0
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
-
if
|
|
46
|
+
if expiration == nil then
|
|
47
47
|
redis.call("set", key, owner, "nx")
|
|
48
48
|
else
|
|
49
|
-
redis.call("set", key, owner, "px",
|
|
49
|
+
redis.call("set", key, owner, "px", expiration, "nx")
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
return 1
|
|
53
|
-
|
|
53
|
+
`,
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
56
|
initReleaseCommand() {
|
|
@@ -63,14 +63,19 @@ export class RedisLockAdapter {
|
|
|
63
63
|
local key = KEYS[1];
|
|
64
64
|
local owner = ARGV[1];
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
if redis.call("exists", key) == 0 then
|
|
67
|
+
return 0
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
local isNotCurrentOwner = redis.call("get", key) ~= owner
|
|
71
|
+
if isNotCurrentOwner then
|
|
72
|
+
return 0
|
|
70
73
|
end
|
|
74
|
+
|
|
75
|
+
redis.call("del", key)
|
|
71
76
|
|
|
72
|
-
return
|
|
73
|
-
|
|
77
|
+
return 1
|
|
78
|
+
`,
|
|
74
79
|
});
|
|
75
80
|
}
|
|
76
81
|
initRefreshComand() {
|
|
@@ -80,33 +85,50 @@ export class RedisLockAdapter {
|
|
|
80
85
|
this.database.defineCommand("daiso_lock_refresh", {
|
|
81
86
|
numberOfKeys: 1,
|
|
82
87
|
lua: `
|
|
88
|
+
-- Arguments
|
|
83
89
|
local key = KEYS[1];
|
|
84
90
|
local owner = ARGV[1];
|
|
85
|
-
local
|
|
91
|
+
local expiration = ARGV[2]
|
|
86
92
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
93
|
+
-- Constant values
|
|
94
|
+
local REFRESHED = ARGV[3]
|
|
95
|
+
local UNOWNED_REFRESH = ARGV[4]
|
|
96
|
+
local UNEXPIRABLE_KEY = ARGV[5]
|
|
97
|
+
|
|
98
|
+
if redis.call("exists", key) == 0 then
|
|
99
|
+
return UNOWNED_REFRESH
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
local isNotCurrentOwner = redis.call("get", key) ~= owner
|
|
103
|
+
if redis.call("get", key) ~= owner then
|
|
104
|
+
return UNOWNED_REFRESH
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
local currentExpiration = redis.call("pttl", key)
|
|
108
|
+
local isUnexpireable = currentExpiration == -1
|
|
109
|
+
if isUnexpireable then
|
|
110
|
+
return UNEXPIRABLE_KEY
|
|
91
111
|
end
|
|
92
|
-
|
|
112
|
+
|
|
113
|
+
redis.call("pexpire", key, expiration)
|
|
114
|
+
return REFRESHED
|
|
93
115
|
`,
|
|
94
116
|
});
|
|
95
117
|
}
|
|
96
118
|
async acquire(key, owner, ttl) {
|
|
97
119
|
const result = await this.database.daiso_lock_acquire(key, owner, String(ttl?.toMilliseconds() ?? null));
|
|
98
|
-
return result
|
|
120
|
+
return result === 1;
|
|
99
121
|
}
|
|
100
122
|
async release(key, owner) {
|
|
101
123
|
const result = await this.database.daiso_lock_release(key, owner);
|
|
102
124
|
return result === 1;
|
|
103
125
|
}
|
|
104
126
|
async forceRelease(key) {
|
|
105
|
-
await this.database.del(key);
|
|
127
|
+
const result = await this.database.del(key);
|
|
128
|
+
return result > 0;
|
|
106
129
|
}
|
|
107
130
|
async refresh(key, owner, ttl) {
|
|
108
|
-
|
|
109
|
-
return Boolean(result);
|
|
131
|
+
return await this.database.daiso_lock_refresh(key, owner, ttl.toMilliseconds().toString(), LOCK_REFRESH_RESULT.REFRESHED, LOCK_REFRESH_RESULT.UNOWNED_REFRESH, LOCK_REFRESH_RESULT.UNEXPIRABLE_KEY);
|
|
110
132
|
}
|
|
111
133
|
}
|
|
112
134
|
//# sourceMappingURL=redis-lock-adapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;
|
|
1
|
+
{"version":3,"file":"redis-lock-adapter.js","sourceRoot":"","sources":["../../../../../src/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EACH,mBAAmB,GAGtB,MAAM,qCAAqC,CAAC;AAuB7C;;;;;;;GAOG;AACH,MAAM,OAAO,gBAAgB;IAWI;IAV7B;;;;;;;;;OASG;IACH,YAA6B,QAAe;QAAf,aAAQ,GAAR,QAAQ,CAAO;QACxC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACzB,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC1B,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC7B,CAAC;IAEO,iBAAiB;QACrB,IAAI,OAAO,IAAI,CAAC,QAAQ,CAAC,kBAAkB,KAAK,UAAU,EAAE,CAAC;YACzD,OAAO;QACX,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,oBAAoB,EA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