@fluixi/storage 1.0.0-alpha.108 → 1.0.0-alpha.109
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
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import {
|
|
2
|
+
joinNamespace,
|
|
3
|
+
namespacePrefix
|
|
4
|
+
} from "./chunk-2JNOWHU2.mjs";
|
|
5
|
+
import {
|
|
6
|
+
jsonSerializer
|
|
7
|
+
} from "./chunk-PRDM65CP.mjs";
|
|
8
|
+
import {
|
|
9
|
+
open,
|
|
10
|
+
seal
|
|
11
|
+
} from "./chunk-Y5QXBVGQ.mjs";
|
|
12
|
+
import {
|
|
13
|
+
webCryptoProvider
|
|
14
|
+
} from "./chunk-XG366MJ3.mjs";
|
|
15
|
+
import {
|
|
16
|
+
memoryStorage
|
|
17
|
+
} from "./chunk-WBLB65GT.mjs";
|
|
18
|
+
import {
|
|
19
|
+
joinBus,
|
|
20
|
+
publish
|
|
21
|
+
} from "./chunk-EBF4FTSI.mjs";
|
|
22
|
+
import {
|
|
23
|
+
decodeEnvelope,
|
|
24
|
+
deserializeOrThrow,
|
|
25
|
+
isExpired,
|
|
26
|
+
latestVersion,
|
|
27
|
+
metaFor,
|
|
28
|
+
migrate,
|
|
29
|
+
selectItems,
|
|
30
|
+
serializeOrThrow,
|
|
31
|
+
toEntry
|
|
32
|
+
} from "./chunk-ZIQSLESR.mjs";
|
|
33
|
+
import {
|
|
34
|
+
StorageEncryptionKeyError
|
|
35
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
36
|
+
|
|
37
|
+
// src/core/async-storage.ts
|
|
38
|
+
function createAsyncStorage(options = {}) {
|
|
39
|
+
const adapter = options.adapter ?? memoryStorage();
|
|
40
|
+
const serializer = options.serializer ?? jsonSerializer;
|
|
41
|
+
const now = options.now ?? Date.now;
|
|
42
|
+
const migrations = options.migrations ?? {};
|
|
43
|
+
const prefix = namespacePrefix(options.namespace);
|
|
44
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
45
|
+
let leave;
|
|
46
|
+
const sealer = {
|
|
47
|
+
keyProvider: options.encryption?.keyProvider,
|
|
48
|
+
provider: options.encryption?.provider ?? webCryptoProvider()
|
|
49
|
+
};
|
|
50
|
+
const context = (key) => options.namespace ? { key, namespace: options.namespace } : { key };
|
|
51
|
+
const deliver = (event) => {
|
|
52
|
+
for (const { key, fn } of [...listeners]) if (key === void 0 || event.key === null || key === event.key) fn(event);
|
|
53
|
+
};
|
|
54
|
+
const emit = (event) => publish(adapter, event.type, event.key === null ? null : prefix + event.key, prefix, event.value);
|
|
55
|
+
const store = async (key, value, meta, encrypt) => {
|
|
56
|
+
const envelope = { v: 1, ...meta };
|
|
57
|
+
if (encrypt) {
|
|
58
|
+
if (!sealer.keyProvider) throw new StorageEncryptionKeyError(`Encrypting ${key} needs encryption on the storage.`, { key });
|
|
59
|
+
envelope.enc = await seal(sealer, context(key), prefix + key, serializeOrThrow(serializer, key, value), meta);
|
|
60
|
+
} else {
|
|
61
|
+
envelope.value = value;
|
|
62
|
+
}
|
|
63
|
+
await adapter.set(prefix + key, serializeOrThrow(serializer, key, envelope), { expiresAt: meta.expiresAt });
|
|
64
|
+
};
|
|
65
|
+
const write = async (key, value, meta, encrypt) => {
|
|
66
|
+
await store(key, value, meta, encrypt);
|
|
67
|
+
emit({ type: "set", key, value, external: false });
|
|
68
|
+
};
|
|
69
|
+
const metaOf = ({ createdAt, expiresAt, version }) => {
|
|
70
|
+
const meta = { createdAt };
|
|
71
|
+
if (expiresAt !== void 0) meta.expiresAt = expiresAt;
|
|
72
|
+
if (version !== void 0) meta.version = version;
|
|
73
|
+
return meta;
|
|
74
|
+
};
|
|
75
|
+
const read = async (key) => {
|
|
76
|
+
const text = await adapter.get(prefix + key);
|
|
77
|
+
if (text === null) return null;
|
|
78
|
+
let envelope = decodeEnvelope(serializer, key, text);
|
|
79
|
+
if (isExpired(envelope, now())) {
|
|
80
|
+
await adapter.remove(prefix + key);
|
|
81
|
+
emit({ type: "expire", key, external: false });
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
let value = envelope.enc ? deserializeOrThrow(serializer, key, await open(sealer, context(key), prefix + key, envelope.enc, metaOf(envelope))) : envelope.value;
|
|
85
|
+
const migrated = migrate(migrations[key], envelope.version, value);
|
|
86
|
+
if (migrated) {
|
|
87
|
+
value = migrated.value;
|
|
88
|
+
const meta = { ...metaOf(envelope), version: migrated.version };
|
|
89
|
+
await store(key, value, meta, !!envelope.enc);
|
|
90
|
+
envelope = { v: 1, ...meta, ...envelope.enc ? { enc: envelope.enc } : { value } };
|
|
91
|
+
}
|
|
92
|
+
return { envelope, value };
|
|
93
|
+
};
|
|
94
|
+
const ownKeys = async () => (await adapter.keys()).filter((k) => k.startsWith(prefix)).map((k) => k.slice(prefix.length));
|
|
95
|
+
const external = (change) => {
|
|
96
|
+
if (change.key === null) return deliver({ type: "clear", key: null, external: true });
|
|
97
|
+
if (!change.key.startsWith(prefix)) return;
|
|
98
|
+
const key = change.key.slice(prefix.length);
|
|
99
|
+
if (change.value === null) return deliver({ type: "remove", key, external: true });
|
|
100
|
+
try {
|
|
101
|
+
const envelope = decodeEnvelope(serializer, key, change.value);
|
|
102
|
+
deliver({ type: "set", key, value: envelope.enc ? void 0 : envelope.value, external: true });
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const liveItems = async () => {
|
|
107
|
+
const items = [];
|
|
108
|
+
for (const key of (await ownKeys()).sort()) {
|
|
109
|
+
const current = await read(key);
|
|
110
|
+
if (current) items.push({ key, value: current.value, entry: toEntry(current.envelope, current.value) });
|
|
111
|
+
}
|
|
112
|
+
return items;
|
|
113
|
+
};
|
|
114
|
+
const storage = {
|
|
115
|
+
scope: adapter.scope,
|
|
116
|
+
keyPrefix: prefix,
|
|
117
|
+
capabilities: { ...adapter.capabilities, expiration: true },
|
|
118
|
+
encryption: !!sealer.keyProvider,
|
|
119
|
+
async get(key) {
|
|
120
|
+
return (await read(key))?.value ?? null;
|
|
121
|
+
},
|
|
122
|
+
async getEntry(key) {
|
|
123
|
+
const current = await read(key);
|
|
124
|
+
return current ? toEntry(current.envelope, current.value) : null;
|
|
125
|
+
},
|
|
126
|
+
async set(key, value, setOptions = {}) {
|
|
127
|
+
const meta = metaFor(now(), setOptions.ttl, setOptions.version ?? latestVersion(migrations[key]));
|
|
128
|
+
await write(key, value, meta, setOptions.encrypt === true);
|
|
129
|
+
},
|
|
130
|
+
entries: liveItems,
|
|
131
|
+
async search(query) {
|
|
132
|
+
return selectItems(await liveItems(), query);
|
|
133
|
+
},
|
|
134
|
+
async size() {
|
|
135
|
+
let count = 0;
|
|
136
|
+
for (const key of await ownKeys()) {
|
|
137
|
+
const text = await adapter.get(prefix + key);
|
|
138
|
+
if (text !== null && !isExpired(decodeEnvelope(serializer, key, text), now())) count++;
|
|
139
|
+
}
|
|
140
|
+
return count;
|
|
141
|
+
},
|
|
142
|
+
async prune() {
|
|
143
|
+
let removed = 0;
|
|
144
|
+
for (const key of await ownKeys()) {
|
|
145
|
+
const text = await adapter.get(prefix + key);
|
|
146
|
+
if (text === null) continue;
|
|
147
|
+
if (isExpired(decodeEnvelope(serializer, key, text), now())) {
|
|
148
|
+
await adapter.remove(prefix + key);
|
|
149
|
+
emit({ type: "expire", key, external: false });
|
|
150
|
+
removed++;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return removed;
|
|
154
|
+
},
|
|
155
|
+
async usage() {
|
|
156
|
+
let bytes = 0;
|
|
157
|
+
const keys = await ownKeys();
|
|
158
|
+
for (const key of keys) bytes += ((prefix + key).length + ((await adapter.get(prefix + key))?.length ?? 0)) * 2;
|
|
159
|
+
return { keys: keys.length, bytes };
|
|
160
|
+
},
|
|
161
|
+
async update(key, fn, updateOptions = {}) {
|
|
162
|
+
const current = await read(key);
|
|
163
|
+
const value = await fn(current?.value ?? null);
|
|
164
|
+
const meta = metaFor(now(), void 0, current?.envelope.version ?? latestVersion(migrations[key]));
|
|
165
|
+
if (current?.envelope.expiresAt !== void 0) meta.expiresAt = current.envelope.expiresAt;
|
|
166
|
+
await write(key, value, meta, updateOptions.encrypt ?? !!current?.envelope.enc);
|
|
167
|
+
return value;
|
|
168
|
+
},
|
|
169
|
+
async getOrSet(key, factory, setOptions) {
|
|
170
|
+
const current = await read(key);
|
|
171
|
+
if (current) return current.value;
|
|
172
|
+
const value = await factory();
|
|
173
|
+
await storage.set(key, value, setOptions);
|
|
174
|
+
return value;
|
|
175
|
+
},
|
|
176
|
+
async touch(key, ttl) {
|
|
177
|
+
const current = await read(key);
|
|
178
|
+
if (!current) return false;
|
|
179
|
+
await store(key, current.value, { ...metaOf(current.envelope), expiresAt: now() + ttl }, !!current.envelope.enc);
|
|
180
|
+
return true;
|
|
181
|
+
},
|
|
182
|
+
async snapshot() {
|
|
183
|
+
const out = {};
|
|
184
|
+
for (const item of await liveItems()) out[item.key] = item.entry;
|
|
185
|
+
return out;
|
|
186
|
+
},
|
|
187
|
+
async restore(snapshot, restoreOptions = {}) {
|
|
188
|
+
let written = 0;
|
|
189
|
+
for (const [key, entry] of Object.entries(snapshot)) {
|
|
190
|
+
if (!entry) continue;
|
|
191
|
+
if (!restoreOptions.overwrite && await read(key)) continue;
|
|
192
|
+
if (entry.expiresAt !== void 0 && entry.expiresAt <= now()) continue;
|
|
193
|
+
const meta = { createdAt: entry.createdAt };
|
|
194
|
+
if (entry.expiresAt !== void 0) meta.expiresAt = entry.expiresAt;
|
|
195
|
+
if (entry.version !== void 0) meta.version = entry.version;
|
|
196
|
+
await write(key, entry.value, meta, entry.encrypted === true);
|
|
197
|
+
written++;
|
|
198
|
+
}
|
|
199
|
+
return written;
|
|
200
|
+
},
|
|
201
|
+
async remove(key) {
|
|
202
|
+
await adapter.remove(prefix + key);
|
|
203
|
+
emit({ type: "remove", key, external: false });
|
|
204
|
+
},
|
|
205
|
+
async has(key) {
|
|
206
|
+
const text = await adapter.get(prefix + key);
|
|
207
|
+
if (text === null) return false;
|
|
208
|
+
if (!isExpired(decodeEnvelope(serializer, key, text), now())) return true;
|
|
209
|
+
await adapter.remove(prefix + key);
|
|
210
|
+
emit({ type: "expire", key, external: false });
|
|
211
|
+
return false;
|
|
212
|
+
},
|
|
213
|
+
async keys() {
|
|
214
|
+
return await ownKeys();
|
|
215
|
+
},
|
|
216
|
+
async clear() {
|
|
217
|
+
if (prefix) for (const key of await ownKeys()) await adapter.remove(prefix + key);
|
|
218
|
+
else await adapter.clear();
|
|
219
|
+
emit({ type: "clear", key: null, external: false });
|
|
220
|
+
},
|
|
221
|
+
namespace(name) {
|
|
222
|
+
return createAsyncStorage({ ...options, migrations: void 0, namespace: joinNamespace(options.namespace, name) });
|
|
223
|
+
},
|
|
224
|
+
subscribe(keyOrListener, maybeListener) {
|
|
225
|
+
const entry = typeof keyOrListener === "function" ? { fn: keyOrListener } : { key: keyOrListener, fn: maybeListener };
|
|
226
|
+
listeners.add(entry);
|
|
227
|
+
if (listeners.size === 1) {
|
|
228
|
+
leave = joinBus(adapter, {
|
|
229
|
+
prefix,
|
|
230
|
+
external,
|
|
231
|
+
local(type, key, value) {
|
|
232
|
+
const event = { type, key: key === null ? null : key.slice(prefix.length), external: false };
|
|
233
|
+
if (type === "set") event.value = value;
|
|
234
|
+
deliver(event);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return () => {
|
|
239
|
+
if (listeners.delete(entry) && listeners.size === 0) {
|
|
240
|
+
leave?.();
|
|
241
|
+
leave = void 0;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
return storage;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export {
|
|
250
|
+
createAsyncStorage
|
|
251
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/utils/copy.ts
|
|
2
|
+
var isAsync = (storage) => "encryption" in storage;
|
|
3
|
+
var pick = (snapshot, keys) => {
|
|
4
|
+
const out = snapshot;
|
|
5
|
+
if (keys) {
|
|
6
|
+
for (const key of Object.keys(out)) if (!keys.includes(key)) delete out[key];
|
|
7
|
+
}
|
|
8
|
+
return out;
|
|
9
|
+
};
|
|
10
|
+
function copyStorage(from, to, options = {}) {
|
|
11
|
+
const restore = { overwrite: options.overwrite };
|
|
12
|
+
if (!isAsync(from) && !isAsync(to)) return to.restore(pick(from.snapshot(), options.keys), restore);
|
|
13
|
+
return (async () => to.restore(pick(await from.snapshot(), options.keys), restore))();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
copyStorage
|
|
18
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromBase64
|
|
3
|
+
} from "./chunk-V6PEKY2O.mjs";
|
|
4
|
+
import {
|
|
5
|
+
StorageEncryptionUnavailableError
|
|
6
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
7
|
+
|
|
8
|
+
// src/encryption/keys.ts
|
|
9
|
+
var subtleOf = (crypto) => {
|
|
10
|
+
const subtle = (crypto ?? globalThis.crypto)?.subtle;
|
|
11
|
+
if (!subtle) throw new StorageEncryptionUnavailableError("Web Crypto is not available in this runtime.");
|
|
12
|
+
return subtle;
|
|
13
|
+
};
|
|
14
|
+
async function generateEncryptionKey(options = {}) {
|
|
15
|
+
return subtleOf(options.crypto).generateKey({ name: "AES-GCM", length: 256 }, options.extractable ?? false, ["encrypt", "decrypt"]);
|
|
16
|
+
}
|
|
17
|
+
async function importEncryptionKey(raw, options = {}) {
|
|
18
|
+
const bytes = typeof raw === "string" ? fromBase64(raw) : raw;
|
|
19
|
+
if (bytes.length !== 32) throw new TypeError(`An AES-256 key is 32 bytes, got ${bytes.length}.`);
|
|
20
|
+
return subtleOf(options.crypto).importKey("raw", bytes, { name: "AES-GCM" }, options.extractable ?? false, ["encrypt", "decrypt"]);
|
|
21
|
+
}
|
|
22
|
+
function staticKeyProvider(key, id) {
|
|
23
|
+
return {
|
|
24
|
+
async getKey() {
|
|
25
|
+
const resolved = await key;
|
|
26
|
+
return id === void 0 ? resolved : { key: resolved, id };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
generateEncryptionKey,
|
|
33
|
+
importEncryptionKey,
|
|
34
|
+
staticKeyProvider
|
|
35
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/core/bus.ts
|
|
2
|
+
var buses = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function joinBus(adapter, subscriber) {
|
|
4
|
+
let bus = buses.get(adapter);
|
|
5
|
+
if (!bus) buses.set(adapter, bus = { subscribers: /* @__PURE__ */ new Set() });
|
|
6
|
+
const current = bus;
|
|
7
|
+
current.subscribers.add(subscriber);
|
|
8
|
+
if (current.subscribers.size === 1 && adapter.subscribe) {
|
|
9
|
+
current.stop = adapter.subscribe((change) => {
|
|
10
|
+
for (const s of [...current.subscribers]) s.external(change);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return () => {
|
|
14
|
+
current.subscribers.delete(subscriber);
|
|
15
|
+
if (current.subscribers.size === 0) {
|
|
16
|
+
current.stop?.();
|
|
17
|
+
current.stop = void 0;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function publish(adapter, type, key, prefix, value) {
|
|
22
|
+
const bus = buses.get(adapter);
|
|
23
|
+
if (!bus) return;
|
|
24
|
+
for (const s of [...bus.subscribers]) {
|
|
25
|
+
if (key === null ? s.prefix.startsWith(prefix) : key.startsWith(s.prefix)) s.local(type, key, value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
joinBus,
|
|
31
|
+
publish
|
|
32
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/server/request.ts
|
|
2
|
+
function requestStorage() {
|
|
3
|
+
const values = /* @__PURE__ */ new Map();
|
|
4
|
+
return {
|
|
5
|
+
name: "request",
|
|
6
|
+
scope: "request",
|
|
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
|
+
}
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
requestStorage
|
|
26
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/utils/cookie-names.ts
|
|
2
|
+
var encodeCookieName = (key) => encodeURIComponent(key).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
3
|
+
var decodeCookieName = (name) => {
|
|
4
|
+
try {
|
|
5
|
+
return decodeURIComponent(name);
|
|
6
|
+
} catch {
|
|
7
|
+
return name;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var cookiePairSize = (header) => new TextEncoder().encode(header.slice(0, header.indexOf(";"))).length;
|
|
11
|
+
var withEntryExpiry = (cookie, expiresAt) => expiresAt === void 0 ? cookie : { ...cookie, maxAge: void 0, expires: new Date(expiresAt) };
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
encodeCookieName,
|
|
15
|
+
decodeCookieName,
|
|
16
|
+
cookiePairSize,
|
|
17
|
+
withEntryExpiry
|
|
18
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
toStorageRequest,
|
|
3
|
+
toStorageResponse
|
|
4
|
+
} from "./chunk-YRPNXR2Q.mjs";
|
|
5
|
+
import {
|
|
6
|
+
cookiePairSize,
|
|
7
|
+
decodeCookieName,
|
|
8
|
+
encodeCookieName,
|
|
9
|
+
withEntryExpiry
|
|
10
|
+
} from "./chunk-KECKXA6R.mjs";
|
|
11
|
+
import {
|
|
12
|
+
StorageQuotaError,
|
|
13
|
+
StorageUnavailableError
|
|
14
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
15
|
+
|
|
16
|
+
// src/server/cookie.ts
|
|
17
|
+
import { parseCookies, serializeCookie } from "@fluixi/utils/cookie";
|
|
18
|
+
function cookieStorage(options = {}) {
|
|
19
|
+
const { request, response, maxSize = 4096, ...cookie } = options;
|
|
20
|
+
const reader = request ? toStorageRequest(request) : null;
|
|
21
|
+
const writer = response ? toStorageResponse(response) : null;
|
|
22
|
+
let incoming = null;
|
|
23
|
+
const written = /* @__PURE__ */ new Map();
|
|
24
|
+
const read = () => {
|
|
25
|
+
if (!incoming) {
|
|
26
|
+
incoming = /* @__PURE__ */ new Map();
|
|
27
|
+
for (const [name, value] of Object.entries(parseCookies(reader?.getHeader("cookie")))) incoming.set(decodeCookieName(name), value);
|
|
28
|
+
}
|
|
29
|
+
return incoming;
|
|
30
|
+
};
|
|
31
|
+
const emit = (key, header) => {
|
|
32
|
+
if (!writer) throw new StorageUnavailableError("This cookie storage has no response to write to.", { key });
|
|
33
|
+
writer.appendHeader("set-cookie", header);
|
|
34
|
+
};
|
|
35
|
+
const get = (key) => written.has(key) ? written.get(key) : read().get(key) ?? null;
|
|
36
|
+
const keys = () => {
|
|
37
|
+
const all = new Set(read().keys());
|
|
38
|
+
for (const [key, value] of written) value === null ? all.delete(key) : all.add(key);
|
|
39
|
+
return [...all];
|
|
40
|
+
};
|
|
41
|
+
const remove = (key) => {
|
|
42
|
+
if (get(key) === null) return;
|
|
43
|
+
emit(key, serializeCookie(encodeCookieName(key), "", { ...cookie, maxAge: 0, expires: /* @__PURE__ */ new Date(0) }));
|
|
44
|
+
written.set(key, null);
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
name: "cookie",
|
|
48
|
+
scope: "cookie",
|
|
49
|
+
capabilities: {
|
|
50
|
+
synchronous: true,
|
|
51
|
+
persistent: true,
|
|
52
|
+
reactive: false,
|
|
53
|
+
crossContext: true,
|
|
54
|
+
expiration: false,
|
|
55
|
+
transactions: false
|
|
56
|
+
},
|
|
57
|
+
get,
|
|
58
|
+
set(key, value, writeOptions) {
|
|
59
|
+
const header = serializeCookie(encodeCookieName(key), value, withEntryExpiry(cookie, writeOptions?.expiresAt));
|
|
60
|
+
const size = cookiePairSize(header);
|
|
61
|
+
if (size > maxSize) throw new StorageQuotaError(`Cookie ${key} is ${size} bytes, over the ${maxSize} limit.`, { key });
|
|
62
|
+
emit(key, header);
|
|
63
|
+
written.set(key, value);
|
|
64
|
+
},
|
|
65
|
+
remove,
|
|
66
|
+
has: (key) => get(key) !== null,
|
|
67
|
+
keys,
|
|
68
|
+
clear: () => keys().forEach(remove)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export {
|
|
73
|
+
cookieStorage
|
|
74
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/core/errors.ts
|
|
2
|
+
var StorageError = class extends Error {
|
|
3
|
+
constructor(code, message, options = {}) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = new.target.name;
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.key = options.key;
|
|
8
|
+
if (options.cause !== void 0) this.cause = options.cause;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var StorageSerializationError = class extends StorageError {
|
|
12
|
+
constructor(message, options = {}) {
|
|
13
|
+
super("serialization", message, options);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var StorageDeserializationError = class extends StorageError {
|
|
17
|
+
constructor(message, options = {}) {
|
|
18
|
+
super("deserialization", message, options);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var StorageUnavailableError = class extends StorageError {
|
|
22
|
+
constructor(message, options = {}) {
|
|
23
|
+
super("unavailable", message, options);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var StorageQuotaError = class extends StorageError {
|
|
27
|
+
constructor(message, options = {}) {
|
|
28
|
+
super("quota", message, options);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var StorageSecurityError = class extends StorageError {
|
|
32
|
+
constructor(message, options = {}) {
|
|
33
|
+
super("security", message, options);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var StorageAdapterError = class extends StorageError {
|
|
37
|
+
constructor(message, options = {}) {
|
|
38
|
+
super("adapter", message, options);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var StorageEncryptionError = class extends StorageError {
|
|
42
|
+
};
|
|
43
|
+
var StorageEncryptionKeyError = class extends StorageEncryptionError {
|
|
44
|
+
constructor(message, options = {}) {
|
|
45
|
+
super("encryption-key", message, options);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var StorageEncryptionMetadataError = class extends StorageEncryptionError {
|
|
49
|
+
constructor(message, options = {}) {
|
|
50
|
+
super("encryption-metadata", message, options);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var StorageEncryptionVersionError = class extends StorageEncryptionError {
|
|
54
|
+
constructor(message, options = {}) {
|
|
55
|
+
super("encryption-version", message, options);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var StorageEncryptionAlgorithmError = class extends StorageEncryptionError {
|
|
59
|
+
constructor(message, options = {}) {
|
|
60
|
+
super("encryption-algorithm", message, options);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var StorageDecryptionError = class extends StorageEncryptionError {
|
|
64
|
+
constructor(message, options = {}) {
|
|
65
|
+
super("decryption", message, options);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var StorageEncryptedPayloadError = class extends StorageEncryptionError {
|
|
69
|
+
constructor(message, options = {}) {
|
|
70
|
+
super("encryption-payload", message, options);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var StorageEncryptionUnavailableError = class extends StorageEncryptionError {
|
|
74
|
+
constructor(message, options = {}) {
|
|
75
|
+
super("encryption-unavailable", message, options);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export {
|
|
80
|
+
StorageError,
|
|
81
|
+
StorageSerializationError,
|
|
82
|
+
StorageDeserializationError,
|
|
83
|
+
StorageUnavailableError,
|
|
84
|
+
StorageQuotaError,
|
|
85
|
+
StorageSecurityError,
|
|
86
|
+
StorageAdapterError,
|
|
87
|
+
StorageEncryptionError,
|
|
88
|
+
StorageEncryptionKeyError,
|
|
89
|
+
StorageEncryptionMetadataError,
|
|
90
|
+
StorageEncryptionVersionError,
|
|
91
|
+
StorageEncryptionAlgorithmError,
|
|
92
|
+
StorageDecryptionError,
|
|
93
|
+
StorageEncryptedPayloadError,
|
|
94
|
+
StorageEncryptionUnavailableError
|
|
95
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StorageSerializationError
|
|
3
|
+
} from "./chunk-LLUKYUEP.mjs";
|
|
4
|
+
|
|
5
|
+
// src/serialization/json.ts
|
|
6
|
+
function assertJson(value, path, seen) {
|
|
7
|
+
switch (typeof value) {
|
|
8
|
+
case "string":
|
|
9
|
+
case "boolean":
|
|
10
|
+
return;
|
|
11
|
+
case "number":
|
|
12
|
+
if (!Number.isFinite(value)) throw new StorageSerializationError(`${path} is ${value}, which JSON cannot hold.`);
|
|
13
|
+
return;
|
|
14
|
+
case "object":
|
|
15
|
+
break;
|
|
16
|
+
default:
|
|
17
|
+
throw new StorageSerializationError(`${path} is a ${typeof value}, which JSON cannot hold.`);
|
|
18
|
+
}
|
|
19
|
+
if (value === null) return;
|
|
20
|
+
if (seen.has(value)) throw new StorageSerializationError(`${path} refers back to itself.`);
|
|
21
|
+
seen.add(value);
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
value.forEach((item, i) => assertJson(item, `${path}[${i}]`, seen));
|
|
24
|
+
} else {
|
|
25
|
+
const proto = Object.getPrototypeOf(value);
|
|
26
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
27
|
+
const name = value.constructor?.name ?? "an object with a prototype";
|
|
28
|
+
throw new StorageSerializationError(`${path} is ${name}, which does not survive JSON. Convert it to plain data first.`);
|
|
29
|
+
}
|
|
30
|
+
for (const [key, item] of Object.entries(value)) {
|
|
31
|
+
if (item === void 0) throw new StorageSerializationError(`${path}.${key} is undefined, which JSON drops. Omit the key or use null.`);
|
|
32
|
+
assertJson(item, `${path}.${key}`, seen);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
seen.delete(value);
|
|
36
|
+
}
|
|
37
|
+
var jsonSerializer = {
|
|
38
|
+
serialize(value) {
|
|
39
|
+
assertJson(value, "value", /* @__PURE__ */ new Set());
|
|
40
|
+
return JSON.stringify(value);
|
|
41
|
+
},
|
|
42
|
+
deserialize(text) {
|
|
43
|
+
return JSON.parse(text);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
jsonSerializer
|
|
49
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/encryption/base64.ts
|
|
2
|
+
function toBase64(bytes) {
|
|
3
|
+
let binary = "";
|
|
4
|
+
for (let i = 0; i < bytes.length; i += 32768) binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
5
|
+
return btoa(binary);
|
|
6
|
+
}
|
|
7
|
+
function fromBase64(text) {
|
|
8
|
+
if (text.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(text)) throw new TypeError("Not base64.");
|
|
9
|
+
const binary = atob(text);
|
|
10
|
+
const out = new Uint8Array(binary.length);
|
|
11
|
+
for (let i = 0; i < binary.length; i++) out[i] = binary.charCodeAt(i);
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
toBase64,
|
|
17
|
+
fromBase64
|
|
18
|
+
};
|