@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.
@@ -0,0 +1,4 @@
1
+ {
2
+ "**/*.{js,mjs,ts,tsx,md,mdx}": ["eslint --fix", "prettier --check --write"],
3
+ "**/*.{json,yml}": ["prettier --check --write"]
4
+ }
@@ -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
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.5.1
8
+ CLI Using tsup config: /home/runner/work/cookie-store/cookie-store/packages/core/tsup.config.ts
9
+ CLI Target: es2020
10
+ CLI Cleaning output folder
11
+ CJS Build start
12
+ ESM Build start
13
+ ESM dist/index.js 2.88 KB
14
+ ESM ⚡️ Build success in 18ms
15
+ CJS dist/index.cjs 3.94 KB
16
+ CJS ⚡️ Build success in 18ms
17
+ DTS Build start
18
+ DTS ⚡️ Build success in 960ms
19
+ DTS dist/index.d.cts 607.00 B
20
+ DTS dist/index.d.ts 607.00 B
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @cookie-store/core
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`6008502`](https://github.com/junwen-k/cookie-store/commit/60085029b5ef3f426b59e3436a187c013c7fba35) Thanks [@junwen-k](https://github.com/junwen-k)! - feat: initial commit
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
+ });
@@ -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 };
@@ -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
@@ -0,0 +1,3 @@
1
+ /* v8 ignore start */
2
+ export * from './cookie-store-cache';
3
+ /* v8 ignore stop */
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "@cookie-store/typescript-config/browser.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "paths": {
6
+ "@/*": ["src/*"]
7
+ }
8
+ },
9
+ "include": ["src/**/*"],
10
+ "exclude": ["node_modules", "dist", "src/**/*.test.ts"]
11
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@cookie-store/typescript-config/browser.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "paths": {
6
+ "@/*": ["src/*"]
7
+ }
8
+ },
9
+ "include": ["src/**/*"]
10
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['cjs', 'esm'],
6
+ dts: true,
7
+ clean: true,
8
+ });
@@ -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
+ });