@cookie-store/core 0.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/.lintstagedrc.json +4 -0
- package/.turbo/turbo-build.log +20 -0
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/index.cjs +99 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +72 -0
- package/package.json +32 -0
- package/src/cookie-store-cache.test.ts +78 -0
- package/src/cookie-store-cache.ts +73 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +11 -0
- package/tsconfig.test.json +10 -0
- package/tsup.config.ts +8 -0
- package/vitest.config.ts +20 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
> @cookie-store/core@0.1.0 build /home/runner/work/cookie-store/cookie-store/packages/core
|
|
3
|
+
> tsup
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.5.1
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/cookie-store/cookie-store/packages/core/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Target: es2020
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mCJS[39m Build start
|
|
12
|
+
[34mESM[39m Build start
|
|
13
|
+
[32mESM[39m [1mdist/index.js [22m[32m2.88 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 18ms
|
|
15
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m3.94 KB[39m
|
|
16
|
+
[32mCJS[39m ⚡️ Build success in 18ms
|
|
17
|
+
[34mDTS[39m Build start
|
|
18
|
+
[32mDTS[39m ⚡️ Build success in 960ms
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m607.00 B[39m
|
|
20
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m607.00 B[39m
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kwan Jun Wen
|
|
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/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @cookie-store/core
|
|
2
|
+
|
|
3
|
+
Synchronous cache for the [Cookie Store API](https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API). Mirrors cookies in memory for synchronous reads so framework bindings can stay reactive. This package is internal and not meant for general or public use.
|
|
4
|
+
|
|
5
|
+
## Scope
|
|
6
|
+
|
|
7
|
+
This package targets **main thread (document)** usage for UI reactivity only. As the [Cookie Store API docs](https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API) state:
|
|
8
|
+
|
|
9
|
+
> "The `url` option enables the modification of a cookie scoped under a particular URL. Service workers can obtain cookies that would be sent to any URL under their scope. From a document you may only obtain the cookies at the current URL, so the only valid URL in a document context is the document's URL."
|
|
10
|
+
|
|
11
|
+
The `url` (`CookieStoreGetOptions`) option is strictly for service workers and is omitted from this API.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { cookieStoreCache } from '@cookie-store/core';
|
|
17
|
+
|
|
18
|
+
// Sync read
|
|
19
|
+
const session = cookieStoreCache.get('session');
|
|
20
|
+
|
|
21
|
+
// Writes use the native API
|
|
22
|
+
await window.cookieStore.set('session', 'token', {
|
|
23
|
+
expires: Date.now() + 86400000,
|
|
24
|
+
});
|
|
25
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __typeError = (msg) => {
|
|
7
|
+
throw TypeError(msg);
|
|
8
|
+
};
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
23
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
24
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
25
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
26
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var index_exports = {};
|
|
30
|
+
__export(index_exports, {
|
|
31
|
+
CookieStoreCache: () => CookieStoreCache,
|
|
32
|
+
cookieStoreCache: () => cookieStoreCache
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/cookie-store-cache.ts
|
|
37
|
+
var _cookies, _CookieStoreCache_instances, initialize_fn, initializeCookies_fn, initializeListeners_fn, _handleChange;
|
|
38
|
+
var CookieStoreCache = class {
|
|
39
|
+
constructor() {
|
|
40
|
+
__privateAdd(this, _CookieStoreCache_instances);
|
|
41
|
+
__privateAdd(this, _cookies, []);
|
|
42
|
+
__privateAdd(this, _handleChange, (event) => {
|
|
43
|
+
let nextCookies = [...__privateGet(this, _cookies)];
|
|
44
|
+
event.changed.forEach((changed) => {
|
|
45
|
+
const index = nextCookies.findIndex((c) => c.name === changed.name);
|
|
46
|
+
if (index !== -1) {
|
|
47
|
+
nextCookies[index] = changed;
|
|
48
|
+
} else {
|
|
49
|
+
nextCookies.push(changed);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
event.deleted.forEach((deleted) => {
|
|
53
|
+
nextCookies = nextCookies.filter((c) => c.name !== deleted.name);
|
|
54
|
+
});
|
|
55
|
+
__privateSet(this, _cookies, nextCookies);
|
|
56
|
+
});
|
|
57
|
+
if (typeof window !== "undefined" && "cookieStore" in window) {
|
|
58
|
+
__privateMethod(this, _CookieStoreCache_instances, initialize_fn).call(this);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
get(name) {
|
|
62
|
+
return __privateGet(this, _cookies).find((cookie) => cookie.name === name) ?? null;
|
|
63
|
+
}
|
|
64
|
+
getAll(name) {
|
|
65
|
+
if (name) {
|
|
66
|
+
return __privateGet(this, _cookies).filter((cookie) => cookie.name === name);
|
|
67
|
+
}
|
|
68
|
+
return __privateGet(this, _cookies);
|
|
69
|
+
}
|
|
70
|
+
addEventListener(...args) {
|
|
71
|
+
window.cookieStore?.addEventListener(...args);
|
|
72
|
+
}
|
|
73
|
+
removeEventListener(...args) {
|
|
74
|
+
window.cookieStore?.removeEventListener(...args);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
_cookies = new WeakMap();
|
|
78
|
+
_CookieStoreCache_instances = new WeakSet();
|
|
79
|
+
initialize_fn = async function() {
|
|
80
|
+
try {
|
|
81
|
+
await __privateMethod(this, _CookieStoreCache_instances, initializeCookies_fn).call(this);
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
__privateMethod(this, _CookieStoreCache_instances, initializeListeners_fn).call(this);
|
|
85
|
+
};
|
|
86
|
+
initializeCookies_fn = async function() {
|
|
87
|
+
const cookies = await window.cookieStore.getAll();
|
|
88
|
+
__privateSet(this, _cookies, [...cookies]);
|
|
89
|
+
};
|
|
90
|
+
initializeListeners_fn = function() {
|
|
91
|
+
window.cookieStore.addEventListener("change", __privateGet(this, _handleChange));
|
|
92
|
+
};
|
|
93
|
+
_handleChange = new WeakMap();
|
|
94
|
+
var cookieStoreCache = new CookieStoreCache();
|
|
95
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
96
|
+
0 && (module.exports = {
|
|
97
|
+
CookieStoreCache,
|
|
98
|
+
cookieStoreCache
|
|
99
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous cache for the Cookie Store API using an immutable-on-write pattern.
|
|
3
|
+
* Maintains an in-memory mirror of cookies for synchronous read access.
|
|
4
|
+
*/
|
|
5
|
+
declare class CookieStoreCache {
|
|
6
|
+
#private;
|
|
7
|
+
constructor();
|
|
8
|
+
get(name: string): CookieListItem | null;
|
|
9
|
+
getAll(name?: string): CookieList;
|
|
10
|
+
addEventListener(...args: Parameters<typeof window.cookieStore.addEventListener>): void;
|
|
11
|
+
removeEventListener(...args: Parameters<typeof window.cookieStore.removeEventListener>): void;
|
|
12
|
+
}
|
|
13
|
+
declare const cookieStoreCache: CookieStoreCache;
|
|
14
|
+
|
|
15
|
+
export { CookieStoreCache, cookieStoreCache };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous cache for the Cookie Store API using an immutable-on-write pattern.
|
|
3
|
+
* Maintains an in-memory mirror of cookies for synchronous read access.
|
|
4
|
+
*/
|
|
5
|
+
declare class CookieStoreCache {
|
|
6
|
+
#private;
|
|
7
|
+
constructor();
|
|
8
|
+
get(name: string): CookieListItem | null;
|
|
9
|
+
getAll(name?: string): CookieList;
|
|
10
|
+
addEventListener(...args: Parameters<typeof window.cookieStore.addEventListener>): void;
|
|
11
|
+
removeEventListener(...args: Parameters<typeof window.cookieStore.removeEventListener>): void;
|
|
12
|
+
}
|
|
13
|
+
declare const cookieStoreCache: CookieStoreCache;
|
|
14
|
+
|
|
15
|
+
export { CookieStoreCache, cookieStoreCache };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
+
|
|
10
|
+
// src/cookie-store-cache.ts
|
|
11
|
+
var _cookies, _CookieStoreCache_instances, initialize_fn, initializeCookies_fn, initializeListeners_fn, _handleChange;
|
|
12
|
+
var CookieStoreCache = class {
|
|
13
|
+
constructor() {
|
|
14
|
+
__privateAdd(this, _CookieStoreCache_instances);
|
|
15
|
+
__privateAdd(this, _cookies, []);
|
|
16
|
+
__privateAdd(this, _handleChange, (event) => {
|
|
17
|
+
let nextCookies = [...__privateGet(this, _cookies)];
|
|
18
|
+
event.changed.forEach((changed) => {
|
|
19
|
+
const index = nextCookies.findIndex((c) => c.name === changed.name);
|
|
20
|
+
if (index !== -1) {
|
|
21
|
+
nextCookies[index] = changed;
|
|
22
|
+
} else {
|
|
23
|
+
nextCookies.push(changed);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
event.deleted.forEach((deleted) => {
|
|
27
|
+
nextCookies = nextCookies.filter((c) => c.name !== deleted.name);
|
|
28
|
+
});
|
|
29
|
+
__privateSet(this, _cookies, nextCookies);
|
|
30
|
+
});
|
|
31
|
+
if (typeof window !== "undefined" && "cookieStore" in window) {
|
|
32
|
+
__privateMethod(this, _CookieStoreCache_instances, initialize_fn).call(this);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
get(name) {
|
|
36
|
+
return __privateGet(this, _cookies).find((cookie) => cookie.name === name) ?? null;
|
|
37
|
+
}
|
|
38
|
+
getAll(name) {
|
|
39
|
+
if (name) {
|
|
40
|
+
return __privateGet(this, _cookies).filter((cookie) => cookie.name === name);
|
|
41
|
+
}
|
|
42
|
+
return __privateGet(this, _cookies);
|
|
43
|
+
}
|
|
44
|
+
addEventListener(...args) {
|
|
45
|
+
window.cookieStore?.addEventListener(...args);
|
|
46
|
+
}
|
|
47
|
+
removeEventListener(...args) {
|
|
48
|
+
window.cookieStore?.removeEventListener(...args);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
_cookies = new WeakMap();
|
|
52
|
+
_CookieStoreCache_instances = new WeakSet();
|
|
53
|
+
initialize_fn = async function() {
|
|
54
|
+
try {
|
|
55
|
+
await __privateMethod(this, _CookieStoreCache_instances, initializeCookies_fn).call(this);
|
|
56
|
+
} catch {
|
|
57
|
+
}
|
|
58
|
+
__privateMethod(this, _CookieStoreCache_instances, initializeListeners_fn).call(this);
|
|
59
|
+
};
|
|
60
|
+
initializeCookies_fn = async function() {
|
|
61
|
+
const cookies = await window.cookieStore.getAll();
|
|
62
|
+
__privateSet(this, _cookies, [...cookies]);
|
|
63
|
+
};
|
|
64
|
+
initializeListeners_fn = function() {
|
|
65
|
+
window.cookieStore.addEventListener("change", __privateGet(this, _handleChange));
|
|
66
|
+
};
|
|
67
|
+
_handleChange = new WeakMap();
|
|
68
|
+
var cookieStoreCache = new CookieStoreCache();
|
|
69
|
+
export {
|
|
70
|
+
CookieStoreCache,
|
|
71
|
+
cookieStoreCache
|
|
72
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cookie-store/core",
|
|
3
|
+
"description": "Simple in-memory mirror of the Cookie Store API for synchronous reads.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^25.2.0",
|
|
19
|
+
"@vitest/browser-playwright": "^4.0.18",
|
|
20
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
21
|
+
"tsup": "^8.5.0",
|
|
22
|
+
"vitest": "^4.0.18",
|
|
23
|
+
"@cookie-store/typescript-config": "0.0.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "turbo watch build",
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"lint": "eslint .",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { CookieStoreCache, cookieStoreCache } from './cookie-store-cache';
|
|
4
|
+
|
|
5
|
+
describe('CookieStoreCache', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
const allCookies = await window.cookieStore.getAll();
|
|
8
|
+
await Promise.all(allCookies.map((cookie) => window.cookieStore.delete(cookie.name!)));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('get', () => {
|
|
12
|
+
it('should return null when cookie does not exist', () => {
|
|
13
|
+
expect(cookieStoreCache.get('nonexistent')).toBeNull();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should return cookie after it is set', async () => {
|
|
17
|
+
await window.cookieStore.set('test', 'value123');
|
|
18
|
+
|
|
19
|
+
await vi.waitFor(() => {
|
|
20
|
+
const cookie = cookieStoreCache.get('test');
|
|
21
|
+
expect(cookie).not.toBeNull();
|
|
22
|
+
expect(cookie?.name).toBe('test');
|
|
23
|
+
expect(cookie?.value).toBe('value123');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should return null after cookie is deleted', async () => {
|
|
28
|
+
await window.cookieStore.set('test', 'value123');
|
|
29
|
+
|
|
30
|
+
await vi.waitFor(() => {
|
|
31
|
+
expect(cookieStoreCache.get('test')).not.toBeNull();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await window.cookieStore.delete('test');
|
|
35
|
+
|
|
36
|
+
await vi.waitFor(() => {
|
|
37
|
+
expect(cookieStoreCache.get('test')).toBeNull();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('getAll', () => {
|
|
43
|
+
it('should return empty array when no cookies exist', () => {
|
|
44
|
+
expect(cookieStoreCache.getAll()).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should return all cookies as array', async () => {
|
|
48
|
+
await window.cookieStore.set('cookie1', 'value1');
|
|
49
|
+
await window.cookieStore.set('cookie2', 'value2');
|
|
50
|
+
await window.cookieStore.set('cookie3', 'value3');
|
|
51
|
+
|
|
52
|
+
await vi.waitFor(() => {
|
|
53
|
+
const cookies = cookieStoreCache.getAll();
|
|
54
|
+
expect(cookies.length).toBe(3);
|
|
55
|
+
|
|
56
|
+
const cookieMap = new Map(cookies.map((c) => [c.name, c]));
|
|
57
|
+
expect(cookieMap.get('cookie1')?.value).toBe('value1');
|
|
58
|
+
expect(cookieMap.get('cookie2')?.value).toBe('value2');
|
|
59
|
+
expect(cookieMap.get('cookie3')?.value).toBe('value3');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('singleton cookieStoreCache', () => {
|
|
65
|
+
it('should export a singleton instance', () => {
|
|
66
|
+
expect(cookieStoreCache).toBeInstanceOf(CookieStoreCache);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should work with the singleton', async () => {
|
|
70
|
+
await window.cookieStore.set('singleton-test', 'singleton-value');
|
|
71
|
+
|
|
72
|
+
await vi.waitFor(() => {
|
|
73
|
+
const cookie = cookieStoreCache.get('singleton-test');
|
|
74
|
+
expect(cookie?.value).toBe('singleton-value');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous cache for the Cookie Store API using an immutable-on-write pattern.
|
|
3
|
+
* Maintains an in-memory mirror of cookies for synchronous read access.
|
|
4
|
+
*/
|
|
5
|
+
export class CookieStoreCache {
|
|
6
|
+
#cookies: CookieList = [];
|
|
7
|
+
|
|
8
|
+
constructor() {
|
|
9
|
+
// Only initialize in browser with Cookie Store API support
|
|
10
|
+
if (typeof window !== 'undefined' && 'cookieStore' in window) {
|
|
11
|
+
this.#initialize();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async #initialize() {
|
|
16
|
+
try {
|
|
17
|
+
await this.#initializeCookies();
|
|
18
|
+
} catch {
|
|
19
|
+
// Do nothing
|
|
20
|
+
}
|
|
21
|
+
this.#initializeListeners();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async #initializeCookies() {
|
|
25
|
+
const cookies = await window.cookieStore.getAll();
|
|
26
|
+
|
|
27
|
+
this.#cookies = [...cookies];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#initializeListeners() {
|
|
31
|
+
window.cookieStore.addEventListener('change', this.#handleChange);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#handleChange = (event: CookieChangeEvent) => {
|
|
35
|
+
let nextCookies = [...this.#cookies];
|
|
36
|
+
|
|
37
|
+
event.changed.forEach((changed) => {
|
|
38
|
+
const index = nextCookies.findIndex((c) => c.name === changed.name);
|
|
39
|
+
if (index !== -1) {
|
|
40
|
+
nextCookies[index] = changed;
|
|
41
|
+
} else {
|
|
42
|
+
nextCookies.push(changed);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
event.deleted.forEach((deleted) => {
|
|
47
|
+
nextCookies = nextCookies.filter((c) => c.name !== deleted.name);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
this.#cookies = nextCookies;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
get(name: string): CookieListItem | null {
|
|
54
|
+
return this.#cookies.find((cookie) => cookie.name === name) ?? null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
getAll(name?: string): CookieList {
|
|
58
|
+
if (name) {
|
|
59
|
+
return this.#cookies.filter((cookie) => cookie.name === name);
|
|
60
|
+
}
|
|
61
|
+
return this.#cookies;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
addEventListener(...args: Parameters<typeof window.cookieStore.addEventListener>): void {
|
|
65
|
+
window.cookieStore?.addEventListener(...args);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
removeEventListener(...args: Parameters<typeof window.cookieStore.removeEventListener>): void {
|
|
69
|
+
window.cookieStore?.removeEventListener(...args);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const cookieStoreCache = new CookieStoreCache();
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { playwright } from '@vitest/browser-playwright';
|
|
2
|
+
import { defineConfig } from 'vitest/config';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
test: {
|
|
6
|
+
browser: {
|
|
7
|
+
enabled: true,
|
|
8
|
+
headless: true,
|
|
9
|
+
provider: playwright(),
|
|
10
|
+
instances: [{ browser: 'chromium' }],
|
|
11
|
+
},
|
|
12
|
+
typecheck: {
|
|
13
|
+
enabled: true,
|
|
14
|
+
tsconfig: './tsconfig.test.json',
|
|
15
|
+
},
|
|
16
|
+
coverage: {
|
|
17
|
+
provider: 'v8',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
});
|