@fluixi/storage 1.0.0-alpha.108 → 1.0.0-alpha.110
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/dist/adapters/memory.mjs +3 -22
- package/dist/browser/document-cookie.mjs +5 -77
- package/dist/browser/index.cjs +1 -1
- package/dist/browser/index.mjs +11 -182
- package/dist/browser/web-storage.mjs +6 -123
- package/dist/chunk-2JNOWHU2.mjs +13 -0
- package/dist/chunk-3Q725O44.mjs +57 -0
- package/dist/chunk-45W7TSI5.mjs +103 -0
- package/dist/chunk-4VWKLSCF.mjs +8 -0
- package/dist/chunk-7OZ6CS7R.mjs +251 -0
- package/dist/chunk-CEBNMPSS.mjs +18 -0
- package/dist/chunk-DTH527GX.mjs +35 -0
- package/dist/chunk-EBF4FTSI.mjs +32 -0
- package/dist/chunk-GS2CVLL6.mjs +26 -0
- package/dist/chunk-KECKXA6R.mjs +18 -0
- package/dist/chunk-KJGIJJ5C.mjs +74 -0
- package/dist/chunk-LLUKYUEP.mjs +95 -0
- package/dist/chunk-PRDM65CP.mjs +49 -0
- package/dist/chunk-UBKKFGUN.mjs +8 -0
- package/dist/chunk-V6PEKY2O.mjs +18 -0
- package/dist/chunk-VLAJHPYA.mjs +233 -0
- package/dist/chunk-WBLB65GT.mjs +26 -0
- package/dist/chunk-XG366MJ3.mjs +45 -0
- package/dist/chunk-Y5QXBVGQ.mjs +89 -0
- package/dist/chunk-YRPNXR2Q.mjs +16 -0
- package/dist/chunk-ZIQSLESR.mjs +82 -0
- package/dist/core/async-storage.mjs +12 -557
- package/dist/core/bus.mjs +4 -27
- package/dist/core/envelope.mjs +13 -84
- package/dist/core/errors.mjs +17 -77
- package/dist/core/storage.mjs +14 -755
- package/dist/encryption/base64.mjs +4 -13
- package/dist/encryption/keys.mjs +7 -49
- package/dist/encryption/seal.mjs +7 -126
- package/dist/encryption/web-crypto.mjs +4 -55
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +50 -820
- package/dist/serialization/json.mjs +4 -57
- package/dist/serialization/serializer.mjs +3 -4
- package/dist/server/contracts.mjs +4 -11
- package/dist/server/cookie.mjs +6 -102
- package/dist/server/index.cjs +1 -1
- package/dist/server/index.mjs +13 -127
- package/dist/server/request.mjs +3 -22
- package/dist/utils/cookie-names.mjs +6 -11
- package/dist/utils/copy.mjs +3 -14
- package/dist/utils/environment.mjs +3 -4
- package/dist/utils/keys.mjs +4 -8
- package/package.json +3 -3
package/dist/adapters/memory.mjs
CHANGED
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return {
|
|
5
|
-
name: "memory",
|
|
6
|
-
scope: "memory",
|
|
7
|
-
capabilities: {
|
|
8
|
-
synchronous: true,
|
|
9
|
-
persistent: false,
|
|
10
|
-
reactive: false,
|
|
11
|
-
crossContext: false,
|
|
12
|
-
expiration: false,
|
|
13
|
-
transactions: false
|
|
14
|
-
},
|
|
15
|
-
get: (key) => values.get(key) ?? null,
|
|
16
|
-
set: (key, value) => void values.set(key, value),
|
|
17
|
-
remove: (key) => void values.delete(key),
|
|
18
|
-
has: (key) => values.has(key),
|
|
19
|
-
keys: () => [...values.keys()],
|
|
20
|
-
clear: () => values.clear()
|
|
21
|
-
};
|
|
22
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
memoryStorage
|
|
3
|
+
} from "../chunk-WBLB65GT.mjs";
|
|
23
4
|
export {
|
|
24
5
|
memoryStorage
|
|
25
6
|
};
|
|
@@ -1,80 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
constructor(code, message, options = {}) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = new.target.name;
|
|
9
|
-
this.code = code;
|
|
10
|
-
this.key = options.key;
|
|
11
|
-
if (options.cause !== void 0) this.cause = options.cause;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var StorageUnavailableError = class extends StorageError {
|
|
15
|
-
constructor(message, options = {}) {
|
|
16
|
-
super("unavailable", message, options);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
var StorageQuotaError = class extends StorageError {
|
|
20
|
-
constructor(message, options = {}) {
|
|
21
|
-
super("quota", message, options);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
// src/utils/cookie-names.ts
|
|
26
|
-
var encodeCookieName = (key) => encodeURIComponent(key).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
27
|
-
var decodeCookieName = (name) => {
|
|
28
|
-
try {
|
|
29
|
-
return decodeURIComponent(name);
|
|
30
|
-
} catch {
|
|
31
|
-
return name;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var cookiePairSize = (header) => new TextEncoder().encode(header.slice(0, header.indexOf(";"))).length;
|
|
35
|
-
var withEntryExpiry = (cookie, expiresAt) => expiresAt === void 0 ? cookie : { ...cookie, maxAge: void 0, expires: new Date(expiresAt) };
|
|
36
|
-
|
|
37
|
-
// src/browser/document-cookie.ts
|
|
38
|
-
function documentCookieAdapter(options = {}) {
|
|
39
|
-
const { maxSize = 4096, ...cookie } = options;
|
|
40
|
-
const doc = (key) => {
|
|
41
|
-
if (typeof document === "undefined") throw new StorageUnavailableError("document.cookie is not available here; on a server use cookieStorage.", { key });
|
|
42
|
-
return document;
|
|
43
|
-
};
|
|
44
|
-
const all = () => {
|
|
45
|
-
const out = /* @__PURE__ */ new Map();
|
|
46
|
-
for (const [name, value] of Object.entries(parseCookies(doc().cookie))) out.set(decodeCookieName(name), value);
|
|
47
|
-
return out;
|
|
48
|
-
};
|
|
49
|
-
const write = (key, header) => {
|
|
50
|
-
doc(key).cookie = header;
|
|
51
|
-
};
|
|
52
|
-
return {
|
|
53
|
-
name: "documentCookie",
|
|
54
|
-
scope: "cookie",
|
|
55
|
-
capabilities: {
|
|
56
|
-
synchronous: true,
|
|
57
|
-
persistent: true,
|
|
58
|
-
reactive: false,
|
|
59
|
-
crossContext: true,
|
|
60
|
-
expiration: false,
|
|
61
|
-
transactions: false
|
|
62
|
-
},
|
|
63
|
-
get: (key) => all().get(key) ?? null,
|
|
64
|
-
set(key, value, writeOptions) {
|
|
65
|
-
const header = serializeCookie(encodeCookieName(key), value, { ...withEntryExpiry(cookie, writeOptions?.expiresAt), httpOnly: false });
|
|
66
|
-
const size = cookiePairSize(header);
|
|
67
|
-
if (size > maxSize) throw new StorageQuotaError(`Cookie ${key} is ${size} bytes, over the ${maxSize} limit.`, { key });
|
|
68
|
-
write(key, header);
|
|
69
|
-
},
|
|
70
|
-
remove: (key) => write(key, serializeCookie(encodeCookieName(key), "", { ...cookie, httpOnly: false, maxAge: 0, expires: /* @__PURE__ */ new Date(0) })),
|
|
71
|
-
has: (key) => all().has(key),
|
|
72
|
-
keys: () => [...all().keys()],
|
|
73
|
-
clear() {
|
|
74
|
-
for (const key of all().keys()) this.remove(key);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
documentCookieAdapter
|
|
3
|
+
} from "../chunk-3Q725O44.mjs";
|
|
4
|
+
import "../chunk-KECKXA6R.mjs";
|
|
5
|
+
import "../chunk-LLUKYUEP.mjs";
|
|
78
6
|
export {
|
|
79
7
|
documentCookieAdapter
|
|
80
8
|
};
|
package/dist/browser/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @fluixi/storage v1.0.0-alpha.
|
|
1
|
+
/*! @fluixi/storage v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
package/dist/browser/index.mjs
CHANGED
|
@@ -1,185 +1,14 @@
|
|
|
1
|
-
/*! @fluixi/storage v1.0.0-alpha.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
var StorageUnavailableError = class extends StorageError {
|
|
14
|
-
constructor(message, options = {}) {
|
|
15
|
-
super("unavailable", message, options);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
var StorageQuotaError = class extends StorageError {
|
|
19
|
-
constructor(message, options = {}) {
|
|
20
|
-
super("quota", message, options);
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
var StorageSecurityError = class extends StorageError {
|
|
24
|
-
constructor(message, options = {}) {
|
|
25
|
-
super("security", message, options);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
var StorageAdapterError = class extends StorageError {
|
|
29
|
-
constructor(message, options = {}) {
|
|
30
|
-
super("adapter", message, options);
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// src/utils/environment.ts
|
|
35
|
-
function hasWindow() {
|
|
36
|
-
return typeof window !== "undefined";
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// src/browser/web-storage.ts
|
|
40
|
-
function mapError(error, area, key) {
|
|
41
|
-
if (error instanceof StorageError) return error;
|
|
42
|
-
const name = error?.name;
|
|
43
|
-
const code = error?.code;
|
|
44
|
-
if (name === "QuotaExceededError" || name === "NS_ERROR_DOM_QUOTA_REACHED" || code === 22 || code === 1014) {
|
|
45
|
-
return new StorageQuotaError(`${area} is full${key ? ` writing ${key}` : ""}.`, { key, cause: error });
|
|
46
|
-
}
|
|
47
|
-
if (name === "SecurityError") {
|
|
48
|
-
return new StorageSecurityError(`${area} is blocked by the browser (privacy settings or a sandboxed frame).`, { key, cause: error });
|
|
49
|
-
}
|
|
50
|
-
return new StorageAdapterError(`${area} failed${key ? ` on ${key}` : ""}.`, { key, cause: error });
|
|
51
|
-
}
|
|
52
|
-
function webStorage(area, capabilities) {
|
|
53
|
-
const resolve = (key) => {
|
|
54
|
-
let storage;
|
|
55
|
-
try {
|
|
56
|
-
storage = globalThis[area];
|
|
57
|
-
} catch (error) {
|
|
58
|
-
throw mapError(error, area, key);
|
|
59
|
-
}
|
|
60
|
-
if (!storage) throw new StorageUnavailableError(`${area} is not available here (a server, a worker, or a browser without it).`, { key });
|
|
61
|
-
return storage;
|
|
62
|
-
};
|
|
63
|
-
const run = (key, fn) => {
|
|
64
|
-
const storage = resolve(key);
|
|
65
|
-
try {
|
|
66
|
-
return fn(storage);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
throw mapError(error, area, key);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
return {
|
|
72
|
-
name: area,
|
|
73
|
-
scope: area === "localStorage" ? "local" : "session",
|
|
74
|
-
capabilities,
|
|
75
|
-
get: (key) => run(key, (s) => s.getItem(key)),
|
|
76
|
-
set: (key, value) => run(key, (s) => s.setItem(key, value)),
|
|
77
|
-
remove: (key) => run(key, (s) => s.removeItem(key)),
|
|
78
|
-
has: (key) => run(key, (s) => s.getItem(key) !== null),
|
|
79
|
-
keys: () => run(void 0, (s) => {
|
|
80
|
-
const keys = [];
|
|
81
|
-
for (let i = 0; i < s.length; i++) {
|
|
82
|
-
const key = s.key(i);
|
|
83
|
-
if (key !== null) keys.push(key);
|
|
84
|
-
}
|
|
85
|
-
return keys;
|
|
86
|
-
}),
|
|
87
|
-
clear: () => run(void 0, (s) => s.clear()),
|
|
88
|
-
subscribe(listener) {
|
|
89
|
-
if (!hasWindow()) return () => {
|
|
90
|
-
};
|
|
91
|
-
const onStorage = (event) => {
|
|
92
|
-
let storage;
|
|
93
|
-
try {
|
|
94
|
-
storage = resolve();
|
|
95
|
-
} catch {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
if (event.storageArea !== storage) return;
|
|
99
|
-
listener({ key: event.key, value: event.newValue });
|
|
100
|
-
};
|
|
101
|
-
window.addEventListener("storage", onStorage);
|
|
102
|
-
return () => window.removeEventListener("storage", onStorage);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
function localStorageAdapter() {
|
|
107
|
-
return webStorage("localStorage", {
|
|
108
|
-
synchronous: true,
|
|
109
|
-
persistent: true,
|
|
110
|
-
reactive: true,
|
|
111
|
-
crossContext: true,
|
|
112
|
-
expiration: false,
|
|
113
|
-
transactions: false
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
function sessionStorageAdapter() {
|
|
117
|
-
return webStorage("sessionStorage", {
|
|
118
|
-
synchronous: true,
|
|
119
|
-
persistent: false,
|
|
120
|
-
reactive: false,
|
|
121
|
-
crossContext: false,
|
|
122
|
-
expiration: false,
|
|
123
|
-
transactions: false
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// src/browser/document-cookie.ts
|
|
128
|
-
import { parseCookies, serializeCookie } from "@fluixi/utils/cookie";
|
|
129
|
-
|
|
130
|
-
// src/utils/cookie-names.ts
|
|
131
|
-
var encodeCookieName = (key) => encodeURIComponent(key).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
132
|
-
var decodeCookieName = (name) => {
|
|
133
|
-
try {
|
|
134
|
-
return decodeURIComponent(name);
|
|
135
|
-
} catch {
|
|
136
|
-
return name;
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
var cookiePairSize = (header) => new TextEncoder().encode(header.slice(0, header.indexOf(";"))).length;
|
|
140
|
-
var withEntryExpiry = (cookie, expiresAt) => expiresAt === void 0 ? cookie : { ...cookie, maxAge: void 0, expires: new Date(expiresAt) };
|
|
141
|
-
|
|
142
|
-
// src/browser/document-cookie.ts
|
|
143
|
-
function documentCookieAdapter(options = {}) {
|
|
144
|
-
const { maxSize = 4096, ...cookie } = options;
|
|
145
|
-
const doc = (key) => {
|
|
146
|
-
if (typeof document === "undefined") throw new StorageUnavailableError("document.cookie is not available here; on a server use cookieStorage.", { key });
|
|
147
|
-
return document;
|
|
148
|
-
};
|
|
149
|
-
const all = () => {
|
|
150
|
-
const out = /* @__PURE__ */ new Map();
|
|
151
|
-
for (const [name, value] of Object.entries(parseCookies(doc().cookie))) out.set(decodeCookieName(name), value);
|
|
152
|
-
return out;
|
|
153
|
-
};
|
|
154
|
-
const write = (key, header) => {
|
|
155
|
-
doc(key).cookie = header;
|
|
156
|
-
};
|
|
157
|
-
return {
|
|
158
|
-
name: "documentCookie",
|
|
159
|
-
scope: "cookie",
|
|
160
|
-
capabilities: {
|
|
161
|
-
synchronous: true,
|
|
162
|
-
persistent: true,
|
|
163
|
-
reactive: false,
|
|
164
|
-
crossContext: true,
|
|
165
|
-
expiration: false,
|
|
166
|
-
transactions: false
|
|
167
|
-
},
|
|
168
|
-
get: (key) => all().get(key) ?? null,
|
|
169
|
-
set(key, value, writeOptions) {
|
|
170
|
-
const header = serializeCookie(encodeCookieName(key), value, { ...withEntryExpiry(cookie, writeOptions?.expiresAt), httpOnly: false });
|
|
171
|
-
const size = cookiePairSize(header);
|
|
172
|
-
if (size > maxSize) throw new StorageQuotaError(`Cookie ${key} is ${size} bytes, over the ${maxSize} limit.`, { key });
|
|
173
|
-
write(key, header);
|
|
174
|
-
},
|
|
175
|
-
remove: (key) => write(key, serializeCookie(encodeCookieName(key), "", { ...cookie, httpOnly: false, maxAge: 0, expires: /* @__PURE__ */ new Date(0) })),
|
|
176
|
-
has: (key) => all().has(key),
|
|
177
|
-
keys: () => [...all().keys()],
|
|
178
|
-
clear() {
|
|
179
|
-
for (const key of all().keys()) this.remove(key);
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
}
|
|
1
|
+
/*! @fluixi/storage v1.0.0-alpha.110 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
import {
|
|
3
|
+
documentCookieAdapter
|
|
4
|
+
} from "../chunk-3Q725O44.mjs";
|
|
5
|
+
import "../chunk-KECKXA6R.mjs";
|
|
6
|
+
import {
|
|
7
|
+
localStorageAdapter,
|
|
8
|
+
sessionStorageAdapter
|
|
9
|
+
} from "../chunk-45W7TSI5.mjs";
|
|
10
|
+
import "../chunk-UBKKFGUN.mjs";
|
|
11
|
+
import "../chunk-LLUKYUEP.mjs";
|
|
183
12
|
export {
|
|
184
13
|
documentCookieAdapter,
|
|
185
14
|
localStorageAdapter,
|
|
@@ -1,126 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.key = options.key;
|
|
8
|
-
if (options.cause !== void 0) this.cause = options.cause;
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
var StorageUnavailableError = class extends StorageError {
|
|
12
|
-
constructor(message, options = {}) {
|
|
13
|
-
super("unavailable", message, options);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var StorageQuotaError = class extends StorageError {
|
|
17
|
-
constructor(message, options = {}) {
|
|
18
|
-
super("quota", message, options);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
var StorageSecurityError = class extends StorageError {
|
|
22
|
-
constructor(message, options = {}) {
|
|
23
|
-
super("security", message, options);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var StorageAdapterError = class extends StorageError {
|
|
27
|
-
constructor(message, options = {}) {
|
|
28
|
-
super("adapter", message, options);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// src/utils/environment.ts
|
|
33
|
-
function hasWindow() {
|
|
34
|
-
return typeof window !== "undefined";
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// src/browser/web-storage.ts
|
|
38
|
-
function mapError(error, area, key) {
|
|
39
|
-
if (error instanceof StorageError) return error;
|
|
40
|
-
const name = error?.name;
|
|
41
|
-
const code = error?.code;
|
|
42
|
-
if (name === "QuotaExceededError" || name === "NS_ERROR_DOM_QUOTA_REACHED" || code === 22 || code === 1014) {
|
|
43
|
-
return new StorageQuotaError(`${area} is full${key ? ` writing ${key}` : ""}.`, { key, cause: error });
|
|
44
|
-
}
|
|
45
|
-
if (name === "SecurityError") {
|
|
46
|
-
return new StorageSecurityError(`${area} is blocked by the browser (privacy settings or a sandboxed frame).`, { key, cause: error });
|
|
47
|
-
}
|
|
48
|
-
return new StorageAdapterError(`${area} failed${key ? ` on ${key}` : ""}.`, { key, cause: error });
|
|
49
|
-
}
|
|
50
|
-
function webStorage(area, capabilities) {
|
|
51
|
-
const resolve = (key) => {
|
|
52
|
-
let storage;
|
|
53
|
-
try {
|
|
54
|
-
storage = globalThis[area];
|
|
55
|
-
} catch (error) {
|
|
56
|
-
throw mapError(error, area, key);
|
|
57
|
-
}
|
|
58
|
-
if (!storage) throw new StorageUnavailableError(`${area} is not available here (a server, a worker, or a browser without it).`, { key });
|
|
59
|
-
return storage;
|
|
60
|
-
};
|
|
61
|
-
const run = (key, fn) => {
|
|
62
|
-
const storage = resolve(key);
|
|
63
|
-
try {
|
|
64
|
-
return fn(storage);
|
|
65
|
-
} catch (error) {
|
|
66
|
-
throw mapError(error, area, key);
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
return {
|
|
70
|
-
name: area,
|
|
71
|
-
scope: area === "localStorage" ? "local" : "session",
|
|
72
|
-
capabilities,
|
|
73
|
-
get: (key) => run(key, (s) => s.getItem(key)),
|
|
74
|
-
set: (key, value) => run(key, (s) => s.setItem(key, value)),
|
|
75
|
-
remove: (key) => run(key, (s) => s.removeItem(key)),
|
|
76
|
-
has: (key) => run(key, (s) => s.getItem(key) !== null),
|
|
77
|
-
keys: () => run(void 0, (s) => {
|
|
78
|
-
const keys = [];
|
|
79
|
-
for (let i = 0; i < s.length; i++) {
|
|
80
|
-
const key = s.key(i);
|
|
81
|
-
if (key !== null) keys.push(key);
|
|
82
|
-
}
|
|
83
|
-
return keys;
|
|
84
|
-
}),
|
|
85
|
-
clear: () => run(void 0, (s) => s.clear()),
|
|
86
|
-
subscribe(listener) {
|
|
87
|
-
if (!hasWindow()) return () => {
|
|
88
|
-
};
|
|
89
|
-
const onStorage = (event) => {
|
|
90
|
-
let storage;
|
|
91
|
-
try {
|
|
92
|
-
storage = resolve();
|
|
93
|
-
} catch {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
if (event.storageArea !== storage) return;
|
|
97
|
-
listener({ key: event.key, value: event.newValue });
|
|
98
|
-
};
|
|
99
|
-
window.addEventListener("storage", onStorage);
|
|
100
|
-
return () => window.removeEventListener("storage", onStorage);
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
function localStorageAdapter() {
|
|
105
|
-
return webStorage("localStorage", {
|
|
106
|
-
synchronous: true,
|
|
107
|
-
persistent: true,
|
|
108
|
-
reactive: true,
|
|
109
|
-
crossContext: true,
|
|
110
|
-
expiration: false,
|
|
111
|
-
transactions: false
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
function sessionStorageAdapter() {
|
|
115
|
-
return webStorage("sessionStorage", {
|
|
116
|
-
synchronous: true,
|
|
117
|
-
persistent: false,
|
|
118
|
-
reactive: false,
|
|
119
|
-
crossContext: false,
|
|
120
|
-
expiration: false,
|
|
121
|
-
transactions: false
|
|
122
|
-
});
|
|
123
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
localStorageAdapter,
|
|
3
|
+
sessionStorageAdapter
|
|
4
|
+
} from "../chunk-45W7TSI5.mjs";
|
|
5
|
+
import "../chunk-UBKKFGUN.mjs";
|
|
6
|
+
import "../chunk-LLUKYUEP.mjs";
|
|
124
7
|
export {
|
|
125
8
|
localStorageAdapter,
|
|
126
9
|
sessionStorageAdapter
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/utils/keys.ts
|
|
2
|
+
function namespacePrefix(namespace) {
|
|
3
|
+
return namespace ? `${namespace}:` : "";
|
|
4
|
+
}
|
|
5
|
+
function joinNamespace(parent, child) {
|
|
6
|
+
if (!child) throw new TypeError("A namespace needs a name.");
|
|
7
|
+
return parent ? `${parent}:${child}` : child;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
namespacePrefix,
|
|
12
|
+
joinNamespace
|
|
13
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cookiePairSize,
|
|
3
|
+
decodeCookieName,
|
|
4
|
+
encodeCookieName,
|
|
5
|
+
withEntryExpiry
|
|
6
|
+
} from "./chunk-KECKXA6R.mjs";
|
|
7
|
+
import {
|
|
8
|
+
StorageQuotaError,
|
|
9
|
+
StorageUnavailableError
|
|
10
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
11
|
+
|
|
12
|
+
// src/browser/document-cookie.ts
|
|
13
|
+
import { parseCookies, serializeCookie } from "@fluixi/utils/cookie";
|
|
14
|
+
function documentCookieAdapter(options = {}) {
|
|
15
|
+
const { maxSize = 4096, ...cookie } = options;
|
|
16
|
+
const doc = (key) => {
|
|
17
|
+
if (typeof document === "undefined") throw new StorageUnavailableError("document.cookie is not available here; on a server use cookieStorage.", { key });
|
|
18
|
+
return document;
|
|
19
|
+
};
|
|
20
|
+
const all = () => {
|
|
21
|
+
const out = /* @__PURE__ */ new Map();
|
|
22
|
+
for (const [name, value] of Object.entries(parseCookies(doc().cookie))) out.set(decodeCookieName(name), value);
|
|
23
|
+
return out;
|
|
24
|
+
};
|
|
25
|
+
const write = (key, header) => {
|
|
26
|
+
doc(key).cookie = header;
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
name: "documentCookie",
|
|
30
|
+
scope: "cookie",
|
|
31
|
+
capabilities: {
|
|
32
|
+
synchronous: true,
|
|
33
|
+
persistent: true,
|
|
34
|
+
reactive: false,
|
|
35
|
+
crossContext: true,
|
|
36
|
+
expiration: false,
|
|
37
|
+
transactions: false
|
|
38
|
+
},
|
|
39
|
+
get: (key) => all().get(key) ?? null,
|
|
40
|
+
set(key, value, writeOptions) {
|
|
41
|
+
const header = serializeCookie(encodeCookieName(key), value, { ...withEntryExpiry(cookie, writeOptions?.expiresAt), httpOnly: false });
|
|
42
|
+
const size = cookiePairSize(header);
|
|
43
|
+
if (size > maxSize) throw new StorageQuotaError(`Cookie ${key} is ${size} bytes, over the ${maxSize} limit.`, { key });
|
|
44
|
+
write(key, header);
|
|
45
|
+
},
|
|
46
|
+
remove: (key) => write(key, serializeCookie(encodeCookieName(key), "", { ...cookie, httpOnly: false, maxAge: 0, expires: /* @__PURE__ */ new Date(0) })),
|
|
47
|
+
has: (key) => all().has(key),
|
|
48
|
+
keys: () => [...all().keys()],
|
|
49
|
+
clear() {
|
|
50
|
+
for (const key of all().keys()) this.remove(key);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export {
|
|
56
|
+
documentCookieAdapter
|
|
57
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hasWindow
|
|
3
|
+
} from "./chunk-UBKKFGUN.mjs";
|
|
4
|
+
import {
|
|
5
|
+
StorageAdapterError,
|
|
6
|
+
StorageError,
|
|
7
|
+
StorageQuotaError,
|
|
8
|
+
StorageSecurityError,
|
|
9
|
+
StorageUnavailableError
|
|
10
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
11
|
+
|
|
12
|
+
// src/browser/web-storage.ts
|
|
13
|
+
function mapError(error, area, key) {
|
|
14
|
+
if (error instanceof StorageError) return error;
|
|
15
|
+
const name = error?.name;
|
|
16
|
+
const code = error?.code;
|
|
17
|
+
if (name === "QuotaExceededError" || name === "NS_ERROR_DOM_QUOTA_REACHED" || code === 22 || code === 1014) {
|
|
18
|
+
return new StorageQuotaError(`${area} is full${key ? ` writing ${key}` : ""}.`, { key, cause: error });
|
|
19
|
+
}
|
|
20
|
+
if (name === "SecurityError") {
|
|
21
|
+
return new StorageSecurityError(`${area} is blocked by the browser (privacy settings or a sandboxed frame).`, { key, cause: error });
|
|
22
|
+
}
|
|
23
|
+
return new StorageAdapterError(`${area} failed${key ? ` on ${key}` : ""}.`, { key, cause: error });
|
|
24
|
+
}
|
|
25
|
+
function webStorage(area, capabilities) {
|
|
26
|
+
const resolve = (key) => {
|
|
27
|
+
let storage;
|
|
28
|
+
try {
|
|
29
|
+
storage = globalThis[area];
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw mapError(error, area, key);
|
|
32
|
+
}
|
|
33
|
+
if (!storage) throw new StorageUnavailableError(`${area} is not available here (a server, a worker, or a browser without it).`, { key });
|
|
34
|
+
return storage;
|
|
35
|
+
};
|
|
36
|
+
const run = (key, fn) => {
|
|
37
|
+
const storage = resolve(key);
|
|
38
|
+
try {
|
|
39
|
+
return fn(storage);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw mapError(error, area, key);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
name: area,
|
|
46
|
+
scope: area === "localStorage" ? "local" : "session",
|
|
47
|
+
capabilities,
|
|
48
|
+
get: (key) => run(key, (s) => s.getItem(key)),
|
|
49
|
+
set: (key, value) => run(key, (s) => s.setItem(key, value)),
|
|
50
|
+
remove: (key) => run(key, (s) => s.removeItem(key)),
|
|
51
|
+
has: (key) => run(key, (s) => s.getItem(key) !== null),
|
|
52
|
+
keys: () => run(void 0, (s) => {
|
|
53
|
+
const keys = [];
|
|
54
|
+
for (let i = 0; i < s.length; i++) {
|
|
55
|
+
const key = s.key(i);
|
|
56
|
+
if (key !== null) keys.push(key);
|
|
57
|
+
}
|
|
58
|
+
return keys;
|
|
59
|
+
}),
|
|
60
|
+
clear: () => run(void 0, (s) => s.clear()),
|
|
61
|
+
subscribe(listener) {
|
|
62
|
+
if (!hasWindow()) return () => {
|
|
63
|
+
};
|
|
64
|
+
const onStorage = (event) => {
|
|
65
|
+
let storage;
|
|
66
|
+
try {
|
|
67
|
+
storage = resolve();
|
|
68
|
+
} catch {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (event.storageArea !== storage) return;
|
|
72
|
+
listener({ key: event.key, value: event.newValue });
|
|
73
|
+
};
|
|
74
|
+
window.addEventListener("storage", onStorage);
|
|
75
|
+
return () => window.removeEventListener("storage", onStorage);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function localStorageAdapter() {
|
|
80
|
+
return webStorage("localStorage", {
|
|
81
|
+
synchronous: true,
|
|
82
|
+
persistent: true,
|
|
83
|
+
reactive: true,
|
|
84
|
+
crossContext: true,
|
|
85
|
+
expiration: false,
|
|
86
|
+
transactions: false
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function sessionStorageAdapter() {
|
|
90
|
+
return webStorage("sessionStorage", {
|
|
91
|
+
synchronous: true,
|
|
92
|
+
persistent: false,
|
|
93
|
+
reactive: false,
|
|
94
|
+
crossContext: false,
|
|
95
|
+
expiration: false,
|
|
96
|
+
transactions: false
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export {
|
|
101
|
+
localStorageAdapter,
|
|
102
|
+
sessionStorageAdapter
|
|
103
|
+
};
|