@xlt-token/store-contract 2.1.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/LICENSE +21 -0
- package/dist/index.cjs +41 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +41 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 xltorg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let vitest = require("vitest");
|
|
3
|
+
let _xlt_token_core = require("@xlt-token/core");
|
|
4
|
+
|
|
5
|
+
//#region src/store-contract.ts
|
|
6
|
+
function defineStoreContract(name, createStore) {
|
|
7
|
+
(0, vitest.describe)(name, () => {
|
|
8
|
+
(0, vitest.it)("keeps one winner for concurrent setIfAbsent", async () => {
|
|
9
|
+
const store = await createStore();
|
|
10
|
+
(0, vitest.expect)((await Promise.all(Array.from({ length: 20 }, (_, index) => store.setIfAbsent("{contract}:lock", String(index), (0, _xlt_token_core.finiteTtl)(30))))).filter(Boolean)).toHaveLength(1);
|
|
11
|
+
await (0, vitest.expect)(store.get("{contract}:lock")).resolves.toMatchObject({ value: vitest.expect.any(String) });
|
|
12
|
+
});
|
|
13
|
+
(0, vitest.it)("keeps value and ttl unchanged when compareAndSet misses", async () => {
|
|
14
|
+
const store = await createStore();
|
|
15
|
+
await store.set("{contract}:cas", "v1", (0, _xlt_token_core.finiteTtl)(60));
|
|
16
|
+
await (0, vitest.expect)(store.compareAndSet("{contract}:cas", "wrong", "v2", (0, _xlt_token_core.keepTtl)())).resolves.toBe(false);
|
|
17
|
+
await (0, vitest.expect)(store.get("{contract}:cas")).resolves.toMatchObject({ value: "v1" });
|
|
18
|
+
});
|
|
19
|
+
(0, vitest.it)("updates and deletes only when the current value matches", async () => {
|
|
20
|
+
const store = await createStore();
|
|
21
|
+
await store.set("{contract}:mutable", "v1", (0, _xlt_token_core.persistentTtl)());
|
|
22
|
+
await (0, vitest.expect)(store.compareAndSet("{contract}:mutable", "v1", "v2", (0, _xlt_token_core.keepTtl)())).resolves.toBe(true);
|
|
23
|
+
await (0, vitest.expect)(store.get("{contract}:mutable")).resolves.toMatchObject({ value: "v2" });
|
|
24
|
+
await (0, vitest.expect)(store.compareAndDelete("{contract}:mutable", "wrong")).resolves.toBe(false);
|
|
25
|
+
await (0, vitest.expect)(store.compareAndDelete("{contract}:mutable", "v2")).resolves.toBe(true);
|
|
26
|
+
await (0, vitest.expect)(store.get("{contract}:mutable")).resolves.toBeNull();
|
|
27
|
+
});
|
|
28
|
+
(0, vitest.it)("touches ttl without changing value", async () => {
|
|
29
|
+
const store = await createStore();
|
|
30
|
+
await store.set("{contract}:touch", "v1", (0, _xlt_token_core.finiteTtl)(30));
|
|
31
|
+
await (0, vitest.expect)(store.touch("{contract}:touch", (0, _xlt_token_core.persistentTtl)())).resolves.toBe(true);
|
|
32
|
+
await (0, vitest.expect)(store.get("{contract}:touch")).resolves.toMatchObject({
|
|
33
|
+
value: "v1",
|
|
34
|
+
expiresAt: null
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
exports.defineStoreContract = defineStoreContract;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { XltTokenStore } from "@xlt-token/core";
|
|
2
|
+
|
|
3
|
+
//#region src/store-contract.d.ts
|
|
4
|
+
declare function defineStoreContract(name: string, createStore: () => Promise<XltTokenStore> | XltTokenStore): void;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { defineStoreContract };
|
|
7
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/store-contract.ts"],"mappings":";;;iBAGgB,mBAAA,CACd,IAAA,UACA,WAAA,QAAmB,OAAA,CAAQ,aAAA,IAAiB,aAAA"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { XltTokenStore } from "@xlt-token/core";
|
|
2
|
+
|
|
3
|
+
//#region src/store-contract.d.ts
|
|
4
|
+
declare function defineStoreContract(name: string, createStore: () => Promise<XltTokenStore> | XltTokenStore): void;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { defineStoreContract };
|
|
7
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/store-contract.ts"],"mappings":";;;iBAGgB,mBAAA,CACd,IAAA,UACA,WAAA,QAAmB,OAAA,CAAQ,aAAA,IAAiB,aAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { finiteTtl, keepTtl, persistentTtl } from "@xlt-token/core";
|
|
3
|
+
|
|
4
|
+
//#region src/store-contract.ts
|
|
5
|
+
function defineStoreContract(name, createStore) {
|
|
6
|
+
describe(name, () => {
|
|
7
|
+
it("keeps one winner for concurrent setIfAbsent", async () => {
|
|
8
|
+
const store = await createStore();
|
|
9
|
+
expect((await Promise.all(Array.from({ length: 20 }, (_, index) => store.setIfAbsent("{contract}:lock", String(index), finiteTtl(30))))).filter(Boolean)).toHaveLength(1);
|
|
10
|
+
await expect(store.get("{contract}:lock")).resolves.toMatchObject({ value: expect.any(String) });
|
|
11
|
+
});
|
|
12
|
+
it("keeps value and ttl unchanged when compareAndSet misses", async () => {
|
|
13
|
+
const store = await createStore();
|
|
14
|
+
await store.set("{contract}:cas", "v1", finiteTtl(60));
|
|
15
|
+
await expect(store.compareAndSet("{contract}:cas", "wrong", "v2", keepTtl())).resolves.toBe(false);
|
|
16
|
+
await expect(store.get("{contract}:cas")).resolves.toMatchObject({ value: "v1" });
|
|
17
|
+
});
|
|
18
|
+
it("updates and deletes only when the current value matches", async () => {
|
|
19
|
+
const store = await createStore();
|
|
20
|
+
await store.set("{contract}:mutable", "v1", persistentTtl());
|
|
21
|
+
await expect(store.compareAndSet("{contract}:mutable", "v1", "v2", keepTtl())).resolves.toBe(true);
|
|
22
|
+
await expect(store.get("{contract}:mutable")).resolves.toMatchObject({ value: "v2" });
|
|
23
|
+
await expect(store.compareAndDelete("{contract}:mutable", "wrong")).resolves.toBe(false);
|
|
24
|
+
await expect(store.compareAndDelete("{contract}:mutable", "v2")).resolves.toBe(true);
|
|
25
|
+
await expect(store.get("{contract}:mutable")).resolves.toBeNull();
|
|
26
|
+
});
|
|
27
|
+
it("touches ttl without changing value", async () => {
|
|
28
|
+
const store = await createStore();
|
|
29
|
+
await store.set("{contract}:touch", "v1", finiteTtl(30));
|
|
30
|
+
await expect(store.touch("{contract}:touch", persistentTtl())).resolves.toBe(true);
|
|
31
|
+
await expect(store.get("{contract}:touch")).resolves.toMatchObject({
|
|
32
|
+
value: "v1",
|
|
33
|
+
expiresAt: null
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { defineStoreContract };
|
|
41
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/store-contract.ts"],"sourcesContent":["import { describe, expect, it } from \"vitest\";\nimport { finiteTtl, keepTtl, persistentTtl, type XltTokenStore } from \"@xlt-token/core\";\n\nexport function defineStoreContract(\n name: string,\n createStore: () => Promise<XltTokenStore> | XltTokenStore,\n): void {\n describe(name, () => {\n it(\"keeps one winner for concurrent setIfAbsent\", async () => {\n const store = await createStore();\n\n const results = await Promise.all(\n Array.from({ length: 20 }, (_, index) =>\n store.setIfAbsent(\"{contract}:lock\", String(index), finiteTtl(30)),\n ),\n );\n\n expect(results.filter(Boolean)).toHaveLength(1);\n await expect(store.get(\"{contract}:lock\")).resolves.toMatchObject({\n value: expect.any(String),\n });\n });\n\n it(\"keeps value and ttl unchanged when compareAndSet misses\", async () => {\n const store = await createStore();\n\n await store.set(\"{contract}:cas\", \"v1\", finiteTtl(60));\n\n await expect(store.compareAndSet(\"{contract}:cas\", \"wrong\", \"v2\", keepTtl())).resolves.toBe(\n false,\n );\n await expect(store.get(\"{contract}:cas\")).resolves.toMatchObject({ value: \"v1\" });\n });\n\n it(\"updates and deletes only when the current value matches\", async () => {\n const store = await createStore();\n\n await store.set(\"{contract}:mutable\", \"v1\", persistentTtl());\n\n await expect(store.compareAndSet(\"{contract}:mutable\", \"v1\", \"v2\", keepTtl())).resolves.toBe(\n true,\n );\n await expect(store.get(\"{contract}:mutable\")).resolves.toMatchObject({ value: \"v2\" });\n\n await expect(store.compareAndDelete(\"{contract}:mutable\", \"wrong\")).resolves.toBe(false);\n await expect(store.compareAndDelete(\"{contract}:mutable\", \"v2\")).resolves.toBe(true);\n await expect(store.get(\"{contract}:mutable\")).resolves.toBeNull();\n });\n\n it(\"touches ttl without changing value\", async () => {\n const store = await createStore();\n\n await store.set(\"{contract}:touch\", \"v1\", finiteTtl(30));\n\n await expect(store.touch(\"{contract}:touch\", persistentTtl())).resolves.toBe(true);\n await expect(store.get(\"{contract}:touch\")).resolves.toMatchObject({\n value: \"v1\",\n expiresAt: null,\n });\n });\n });\n}\n"],"mappings":";;;;AAGA,SAAgB,oBACd,MACA,aACM;AACN,UAAS,YAAY;AACnB,KAAG,+CAA+C,YAAY;GAC5D,MAAM,QAAQ,MAAM,aAAa;AAQjC,WANgB,MAAM,QAAQ,IAC5B,MAAM,KAAK,EAAE,QAAQ,IAAI,GAAG,GAAG,UAC7B,MAAM,YAAY,mBAAmB,OAAO,MAAM,EAAE,UAAU,GAAG,CAAC,CACnE,CACF,EAEc,OAAO,QAAQ,CAAC,CAAC,aAAa,EAAE;AAC/C,SAAM,OAAO,MAAM,IAAI,kBAAkB,CAAC,CAAC,SAAS,cAAc,EAChE,OAAO,OAAO,IAAI,OAAO,EAC1B,CAAC;IACF;AAEF,KAAG,2DAA2D,YAAY;GACxE,MAAM,QAAQ,MAAM,aAAa;AAEjC,SAAM,MAAM,IAAI,kBAAkB,MAAM,UAAU,GAAG,CAAC;AAEtD,SAAM,OAAO,MAAM,cAAc,kBAAkB,SAAS,MAAM,SAAS,CAAC,CAAC,CAAC,SAAS,KACrF,MACD;AACD,SAAM,OAAO,MAAM,IAAI,iBAAiB,CAAC,CAAC,SAAS,cAAc,EAAE,OAAO,MAAM,CAAC;IACjF;AAEF,KAAG,2DAA2D,YAAY;GACxE,MAAM,QAAQ,MAAM,aAAa;AAEjC,SAAM,MAAM,IAAI,sBAAsB,MAAM,eAAe,CAAC;AAE5D,SAAM,OAAO,MAAM,cAAc,sBAAsB,MAAM,MAAM,SAAS,CAAC,CAAC,CAAC,SAAS,KACtF,KACD;AACD,SAAM,OAAO,MAAM,IAAI,qBAAqB,CAAC,CAAC,SAAS,cAAc,EAAE,OAAO,MAAM,CAAC;AAErF,SAAM,OAAO,MAAM,iBAAiB,sBAAsB,QAAQ,CAAC,CAAC,SAAS,KAAK,MAAM;AACxF,SAAM,OAAO,MAAM,iBAAiB,sBAAsB,KAAK,CAAC,CAAC,SAAS,KAAK,KAAK;AACpF,SAAM,OAAO,MAAM,IAAI,qBAAqB,CAAC,CAAC,SAAS,UAAU;IACjE;AAEF,KAAG,sCAAsC,YAAY;GACnD,MAAM,QAAQ,MAAM,aAAa;AAEjC,SAAM,MAAM,IAAI,oBAAoB,MAAM,UAAU,GAAG,CAAC;AAExD,SAAM,OAAO,MAAM,MAAM,oBAAoB,eAAe,CAAC,CAAC,CAAC,SAAS,KAAK,KAAK;AAClF,SAAM,OAAO,MAAM,IAAI,mBAAmB,CAAC,CAAC,SAAS,cAAc;IACjE,OAAO;IACP,WAAW;IACZ,CAAC;IACF;GACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xlt-token/store-contract",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Shared XltTokenStore contract tests for xlt-token stores",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"auth",
|
|
7
|
+
"contract",
|
|
8
|
+
"store",
|
|
9
|
+
"token"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://xiaolangtou.github.io/xlt-token/",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/xiaoLangtou/xlt-token/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "xltorg",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/xiaoLangtou/xlt-token.git",
|
|
20
|
+
"directory": "packages/store-contract"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.cts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"default": "./dist/index.mjs"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/index.d.cts",
|
|
40
|
+
"default": "./dist/index.cjs"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@xlt-token/core": "^2.1.0",
|
|
50
|
+
"vitest": "^2.0.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@vitest/coverage-v8": "^2.1.9",
|
|
54
|
+
"tsdown": "^0.20.0",
|
|
55
|
+
"typescript": "^5.1.0"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"build:watch": "tsdown --watch",
|
|
60
|
+
"typecheck": "tsc --noEmit"
|
|
61
|
+
}
|
|
62
|
+
}
|