@vite-hub/kv 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +9 -1
- package/dist/{config-C1ofyzSt.js → config-aM9Ae7tH.js} +9 -3
- package/dist/{deno-kv-JnitV_jG.js → deno-kv-cJx3jU10.js} +16 -1
- package/dist/dist-DDW1iC5f.js +447 -0
- package/dist/driver-BT9WefKi.d.ts +64 -0
- package/dist/errors.d.ts +1 -1
- package/dist/fs-lite-BxzdiJSi.js +79 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +51 -9
- package/dist/runtime/cloudflare-kv.d.ts +7 -0
- package/dist/runtime/cloudflare-kv.js +24 -0
- package/dist/runtime/upstash-driver.d.ts +7 -2
- package/dist/runtime/upstash-driver.js +121 -2
- package/dist/{types-Dg4l318i.d.ts → types-CO0DRjJa.d.ts} +20 -2
- package/dist/virtual-module.d.ts +6 -0
- package/dist/virtual.d.ts +2 -6
- package/dist/vite.d.ts +17 -1
- package/dist/vite.js +4512 -193
- package/package.json +5 -3
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.7_@azure+identity@4.13.1_@azure+storage-blob@12.31.0_@netlify+blo_8194820fdb6706e28a1ff20070ef4217/node_modules/unstorage/dist/_chunks/libs/destr.mjs
|
|
2
|
+
const suspectProtoRx = /"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/;
|
|
3
|
+
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
4
|
+
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/;
|
|
5
|
+
function jsonParseTransform(key, value) {
|
|
6
|
+
if (key === "__proto__" || key === "constructor" && value && typeof value === "object" && "prototype" in value) {
|
|
7
|
+
warnKeyDropped(key);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
function warnKeyDropped(key) {
|
|
13
|
+
console.warn(`[destr] Dropping "${key}" key to prevent prototype pollution.`);
|
|
14
|
+
}
|
|
15
|
+
function destr(value, options = {}) {
|
|
16
|
+
if (typeof value !== "string") return value;
|
|
17
|
+
if (value[0] === "\"" && value[value.length - 1] === "\"" && value.indexOf("\\") === -1) return value.slice(1, -1);
|
|
18
|
+
const _value = value.trim();
|
|
19
|
+
if (_value.length <= 9) switch (_value.toLowerCase()) {
|
|
20
|
+
case "true": return true;
|
|
21
|
+
case "false": return false;
|
|
22
|
+
case "undefined": return;
|
|
23
|
+
case "null": return null;
|
|
24
|
+
case "nan": return NaN;
|
|
25
|
+
case "infinity": return Number.POSITIVE_INFINITY;
|
|
26
|
+
case "-infinity": return Number.NEGATIVE_INFINITY;
|
|
27
|
+
}
|
|
28
|
+
if (!JsonSigRx.test(value)) {
|
|
29
|
+
if (options.strict) throw new SyntaxError("[destr] Invalid JSON");
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
if (suspectProtoRx.test(value) || suspectConstructorRx.test(value)) {
|
|
34
|
+
if (options.strict) throw new Error("[destr] Possible prototype pollution");
|
|
35
|
+
return JSON.parse(value, jsonParseTransform);
|
|
36
|
+
}
|
|
37
|
+
return JSON.parse(value);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (options.strict) throw error;
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.7_@azure+identity@4.13.1_@azure+storage-blob@12.31.0_@netlify+blo_8194820fdb6706e28a1ff20070ef4217/node_modules/unstorage/dist/_chunks/_utils.mjs
|
|
45
|
+
function wrapToPromise(value) {
|
|
46
|
+
if (!value || typeof value.then !== "function") return Promise.resolve(value);
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
function asyncCall(function_, ...arguments_) {
|
|
50
|
+
try {
|
|
51
|
+
return wrapToPromise(function_(...arguments_));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return Promise.reject(error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function isPrimitive(value) {
|
|
57
|
+
const type = typeof value;
|
|
58
|
+
return value === null || type !== "object" && type !== "function";
|
|
59
|
+
}
|
|
60
|
+
function isPureObject(value) {
|
|
61
|
+
const proto = Object.getPrototypeOf(value);
|
|
62
|
+
return !proto || proto.isPrototypeOf(Object);
|
|
63
|
+
}
|
|
64
|
+
function stringify(value) {
|
|
65
|
+
if (isPrimitive(value)) return String(value);
|
|
66
|
+
if (isPureObject(value) || Array.isArray(value)) return JSON.stringify(value);
|
|
67
|
+
if (typeof value.toJSON === "function") return stringify(value.toJSON());
|
|
68
|
+
throw new Error("[unstorage] Cannot stringify value!");
|
|
69
|
+
}
|
|
70
|
+
const BASE64_PREFIX = "base64:";
|
|
71
|
+
function serializeRaw(value) {
|
|
72
|
+
if (typeof value === "string") return value;
|
|
73
|
+
return BASE64_PREFIX + base64Encode(value);
|
|
74
|
+
}
|
|
75
|
+
function deserializeRaw(value) {
|
|
76
|
+
if (typeof value !== "string") return value;
|
|
77
|
+
if (!value.startsWith("base64:")) return value;
|
|
78
|
+
return base64Decode(value.slice(7));
|
|
79
|
+
}
|
|
80
|
+
function base64Decode(input) {
|
|
81
|
+
if (globalThis.Buffer) return Buffer.from(input, "base64");
|
|
82
|
+
return Uint8Array.from(globalThis.atob(input), (c) => c.codePointAt(0));
|
|
83
|
+
}
|
|
84
|
+
function base64Encode(input) {
|
|
85
|
+
if (globalThis.Buffer) return Buffer.from(input).toString("base64");
|
|
86
|
+
return globalThis.btoa(String.fromCodePoint(...input));
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.7_@azure+identity@4.13.1_@azure+storage-blob@12.31.0_@netlify+blo_8194820fdb6706e28a1ff20070ef4217/node_modules/unstorage/dist/_chunks/utils.mjs
|
|
90
|
+
function normalizeKey(key) {
|
|
91
|
+
if (!key) return "";
|
|
92
|
+
return key.split("?")[0]?.replace(/[/\\]/g, ":").replace(/:+/g, ":").replace(/^:|:$/g, "") || "";
|
|
93
|
+
}
|
|
94
|
+
function joinKeys(...keys) {
|
|
95
|
+
return normalizeKey(keys.join(":"));
|
|
96
|
+
}
|
|
97
|
+
function normalizeBaseKey(base) {
|
|
98
|
+
base = normalizeKey(base);
|
|
99
|
+
return base ? base + ":" : "";
|
|
100
|
+
}
|
|
101
|
+
function filterKeyByDepth(key, depth) {
|
|
102
|
+
if (depth === void 0) return true;
|
|
103
|
+
let substrCount = 0;
|
|
104
|
+
let index = key.indexOf(":");
|
|
105
|
+
while (index > -1) {
|
|
106
|
+
substrCount++;
|
|
107
|
+
index = key.indexOf(":", index + 1);
|
|
108
|
+
}
|
|
109
|
+
return substrCount <= depth;
|
|
110
|
+
}
|
|
111
|
+
function filterKeyByBase(key, base) {
|
|
112
|
+
if (base) return key.startsWith(base) && key[key.length - 1] !== "$";
|
|
113
|
+
return key[key.length - 1] !== "$";
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.7_@azure+identity@4.13.1_@azure+storage-blob@12.31.0_@netlify+blo_8194820fdb6706e28a1ff20070ef4217/node_modules/unstorage/dist/index.mjs
|
|
117
|
+
const DRIVER_NAME = "memory";
|
|
118
|
+
const driver = () => {
|
|
119
|
+
const data = /* @__PURE__ */ new Map();
|
|
120
|
+
const timers = /* @__PURE__ */ new Map();
|
|
121
|
+
return {
|
|
122
|
+
name: DRIVER_NAME,
|
|
123
|
+
getInstance: () => data,
|
|
124
|
+
hasItem(key) {
|
|
125
|
+
return data.has(key);
|
|
126
|
+
},
|
|
127
|
+
getItem(key) {
|
|
128
|
+
return data.get(key) ?? null;
|
|
129
|
+
},
|
|
130
|
+
getItemRaw(key) {
|
|
131
|
+
return data.get(key) ?? null;
|
|
132
|
+
},
|
|
133
|
+
setItem(key, value, opts) {
|
|
134
|
+
_clearTimer(timers, key);
|
|
135
|
+
data.set(key, value);
|
|
136
|
+
_scheduleExpiry(data, timers, key, opts?.ttl);
|
|
137
|
+
},
|
|
138
|
+
setItemRaw(key, value, opts) {
|
|
139
|
+
_clearTimer(timers, key);
|
|
140
|
+
data.set(key, value);
|
|
141
|
+
_scheduleExpiry(data, timers, key, opts?.ttl);
|
|
142
|
+
},
|
|
143
|
+
removeItem(key) {
|
|
144
|
+
_clearTimer(timers, key);
|
|
145
|
+
data.delete(key);
|
|
146
|
+
},
|
|
147
|
+
getKeys() {
|
|
148
|
+
return [...data.keys()];
|
|
149
|
+
},
|
|
150
|
+
clear() {
|
|
151
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
152
|
+
timers.clear();
|
|
153
|
+
data.clear();
|
|
154
|
+
},
|
|
155
|
+
dispose() {
|
|
156
|
+
for (const timer of timers.values()) clearTimeout(timer);
|
|
157
|
+
timers.clear();
|
|
158
|
+
data.clear();
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
function _clearTimer(timers, key) {
|
|
163
|
+
const existing = timers.get(key);
|
|
164
|
+
if (existing !== void 0) {
|
|
165
|
+
clearTimeout(existing);
|
|
166
|
+
timers.delete(key);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function _scheduleExpiry(data, timers, key, ttl) {
|
|
170
|
+
if (!ttl) return;
|
|
171
|
+
const ttlMs = ttl * 1e3;
|
|
172
|
+
const timer = setTimeout(() => {
|
|
173
|
+
data.delete(key);
|
|
174
|
+
timers.delete(key);
|
|
175
|
+
}, ttlMs);
|
|
176
|
+
if (timer && typeof timer === "object" && "unref" in timer) timer.unref();
|
|
177
|
+
timers.set(key, timer);
|
|
178
|
+
}
|
|
179
|
+
function createStorage(options = {}) {
|
|
180
|
+
const context = {
|
|
181
|
+
mounts: { "": options.driver || driver() },
|
|
182
|
+
mountpoints: [""],
|
|
183
|
+
watching: false,
|
|
184
|
+
watchListeners: [],
|
|
185
|
+
unwatch: {}
|
|
186
|
+
};
|
|
187
|
+
const getMount = (key) => {
|
|
188
|
+
for (const base of context.mountpoints) if (key.startsWith(base)) return {
|
|
189
|
+
base,
|
|
190
|
+
relativeKey: key.slice(base.length),
|
|
191
|
+
driver: context.mounts[base]
|
|
192
|
+
};
|
|
193
|
+
return {
|
|
194
|
+
base: "",
|
|
195
|
+
relativeKey: key,
|
|
196
|
+
driver: context.mounts[""]
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
const getMounts = (base, includeParent) => {
|
|
200
|
+
return context.mountpoints.filter((mountpoint) => mountpoint.startsWith(base) || includeParent && base.startsWith(mountpoint)).map((mountpoint) => ({
|
|
201
|
+
relativeBase: base.length > mountpoint.length ? base.slice(mountpoint.length) : void 0,
|
|
202
|
+
mountpoint,
|
|
203
|
+
driver: context.mounts[mountpoint]
|
|
204
|
+
}));
|
|
205
|
+
};
|
|
206
|
+
const onChange = (event, key) => {
|
|
207
|
+
if (!context.watching) return;
|
|
208
|
+
key = normalizeKey(key);
|
|
209
|
+
for (const listener of context.watchListeners) listener(event, key);
|
|
210
|
+
};
|
|
211
|
+
const startWatch = async () => {
|
|
212
|
+
if (context.watching) return;
|
|
213
|
+
context.watching = true;
|
|
214
|
+
for (const mountpoint in context.mounts) context.unwatch[mountpoint] = await watch(context.mounts[mountpoint], onChange, mountpoint);
|
|
215
|
+
};
|
|
216
|
+
const stopWatch = async () => {
|
|
217
|
+
if (!context.watching) return;
|
|
218
|
+
for (const mountpoint in context.unwatch) await context.unwatch[mountpoint]();
|
|
219
|
+
context.unwatch = {};
|
|
220
|
+
context.watching = false;
|
|
221
|
+
};
|
|
222
|
+
const runBatch = (items, commonOptions, cb) => {
|
|
223
|
+
const batches = /* @__PURE__ */ new Map();
|
|
224
|
+
const getBatch = (mount) => {
|
|
225
|
+
let batch = batches.get(mount.base);
|
|
226
|
+
if (!batch) {
|
|
227
|
+
batch = {
|
|
228
|
+
driver: mount.driver,
|
|
229
|
+
base: mount.base,
|
|
230
|
+
items: []
|
|
231
|
+
};
|
|
232
|
+
batches.set(mount.base, batch);
|
|
233
|
+
}
|
|
234
|
+
return batch;
|
|
235
|
+
};
|
|
236
|
+
for (const item of items) {
|
|
237
|
+
const isStringItem = typeof item === "string";
|
|
238
|
+
const key = normalizeKey(isStringItem ? item : item.key);
|
|
239
|
+
const value = isStringItem ? void 0 : item.value;
|
|
240
|
+
const options = isStringItem || !item.options ? commonOptions : {
|
|
241
|
+
...commonOptions,
|
|
242
|
+
...item.options
|
|
243
|
+
};
|
|
244
|
+
const mount = getMount(key);
|
|
245
|
+
getBatch(mount).items.push({
|
|
246
|
+
key,
|
|
247
|
+
value,
|
|
248
|
+
relativeKey: mount.relativeKey,
|
|
249
|
+
options
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
return Promise.all([...batches.values()].map((batch) => cb(batch))).then((r) => r.flat());
|
|
253
|
+
};
|
|
254
|
+
const storage = {
|
|
255
|
+
hasItem(key, opts = {}) {
|
|
256
|
+
key = normalizeKey(key);
|
|
257
|
+
const { relativeKey, driver } = getMount(key);
|
|
258
|
+
return asyncCall(driver.hasItem, relativeKey, opts);
|
|
259
|
+
},
|
|
260
|
+
getItem(key, opts = {}) {
|
|
261
|
+
key = normalizeKey(key);
|
|
262
|
+
const { relativeKey, driver } = getMount(key);
|
|
263
|
+
return asyncCall(driver.getItem, relativeKey, opts).then((value) => destr(value));
|
|
264
|
+
},
|
|
265
|
+
getItems(items, commonOptions = {}) {
|
|
266
|
+
return runBatch(items, commonOptions, (batch) => {
|
|
267
|
+
if (batch.driver.getItems) return asyncCall(batch.driver.getItems, batch.items.map((item) => ({
|
|
268
|
+
key: item.relativeKey,
|
|
269
|
+
options: item.options
|
|
270
|
+
})), commonOptions).then((r) => r.map((item) => ({
|
|
271
|
+
key: joinKeys(batch.base, item.key),
|
|
272
|
+
value: destr(item.value)
|
|
273
|
+
})));
|
|
274
|
+
return Promise.all(batch.items.map((item) => {
|
|
275
|
+
return asyncCall(batch.driver.getItem, item.relativeKey, item.options).then((value) => ({
|
|
276
|
+
key: item.key,
|
|
277
|
+
value: destr(value)
|
|
278
|
+
}));
|
|
279
|
+
}));
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
getItemRaw(key, opts = {}) {
|
|
283
|
+
key = normalizeKey(key);
|
|
284
|
+
const { relativeKey, driver } = getMount(key);
|
|
285
|
+
if (driver.getItemRaw) return asyncCall(driver.getItemRaw, relativeKey, opts);
|
|
286
|
+
return asyncCall(driver.getItem, relativeKey, opts).then((value) => deserializeRaw(value));
|
|
287
|
+
},
|
|
288
|
+
async setItem(key, value, opts = {}) {
|
|
289
|
+
if (value === void 0) return storage.removeItem(key);
|
|
290
|
+
key = normalizeKey(key);
|
|
291
|
+
const { relativeKey, driver } = getMount(key);
|
|
292
|
+
if (!driver.setItem) return;
|
|
293
|
+
await asyncCall(driver.setItem, relativeKey, stringify(value), opts);
|
|
294
|
+
if (!driver.watch) onChange("update", key);
|
|
295
|
+
},
|
|
296
|
+
async setItems(items, commonOptions) {
|
|
297
|
+
await runBatch(items, commonOptions, async (batch) => {
|
|
298
|
+
if (batch.driver.setItems) return asyncCall(batch.driver.setItems, batch.items.map((item) => ({
|
|
299
|
+
key: item.relativeKey,
|
|
300
|
+
value: stringify(item.value),
|
|
301
|
+
options: item.options
|
|
302
|
+
})), commonOptions);
|
|
303
|
+
if (!batch.driver.setItem) return;
|
|
304
|
+
await Promise.all(batch.items.map((item) => {
|
|
305
|
+
return asyncCall(batch.driver.setItem, item.relativeKey, stringify(item.value), item.options);
|
|
306
|
+
}));
|
|
307
|
+
});
|
|
308
|
+
},
|
|
309
|
+
async setItemRaw(key, value, opts = {}) {
|
|
310
|
+
if (value === void 0) return storage.removeItem(key, opts);
|
|
311
|
+
key = normalizeKey(key);
|
|
312
|
+
const { relativeKey, driver } = getMount(key);
|
|
313
|
+
if (driver.setItemRaw) await asyncCall(driver.setItemRaw, relativeKey, value, opts);
|
|
314
|
+
else if (driver.setItem) await asyncCall(driver.setItem, relativeKey, serializeRaw(value), opts);
|
|
315
|
+
else return;
|
|
316
|
+
if (!driver.watch) onChange("update", key);
|
|
317
|
+
},
|
|
318
|
+
async removeItem(key, opts = {}) {
|
|
319
|
+
if (typeof opts === "boolean") opts = { removeMeta: opts };
|
|
320
|
+
key = normalizeKey(key);
|
|
321
|
+
const { relativeKey, driver } = getMount(key);
|
|
322
|
+
if (!driver.removeItem) return;
|
|
323
|
+
await asyncCall(driver.removeItem, relativeKey, opts);
|
|
324
|
+
if (opts.removeMeta || opts.removeMata) await asyncCall(driver.removeItem, relativeKey + "$", opts);
|
|
325
|
+
if (!driver.watch) onChange("remove", key);
|
|
326
|
+
},
|
|
327
|
+
async getMeta(key, opts = {}) {
|
|
328
|
+
if (typeof opts === "boolean") opts = { nativeOnly: opts };
|
|
329
|
+
key = normalizeKey(key);
|
|
330
|
+
const { relativeKey, driver } = getMount(key);
|
|
331
|
+
const meta = Object.create(null);
|
|
332
|
+
if (driver.getMeta) Object.assign(meta, await asyncCall(driver.getMeta, relativeKey, opts));
|
|
333
|
+
if (!opts.nativeOnly) {
|
|
334
|
+
const value = await asyncCall(driver.getItem, relativeKey + "$", opts).then((value_) => destr(value_));
|
|
335
|
+
if (value && typeof value === "object") {
|
|
336
|
+
if (typeof value.atime === "string") value.atime = new Date(value.atime);
|
|
337
|
+
if (typeof value.mtime === "string") value.mtime = new Date(value.mtime);
|
|
338
|
+
Object.assign(meta, value);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return meta;
|
|
342
|
+
},
|
|
343
|
+
setMeta(key, value, opts = {}) {
|
|
344
|
+
return this.setItem(key + "$", value, opts);
|
|
345
|
+
},
|
|
346
|
+
removeMeta(key, opts = {}) {
|
|
347
|
+
return this.removeItem(key + "$", opts);
|
|
348
|
+
},
|
|
349
|
+
async getKeys(base, opts = {}) {
|
|
350
|
+
base = normalizeBaseKey(base);
|
|
351
|
+
const mounts = getMounts(base, true);
|
|
352
|
+
let maskedMounts = [];
|
|
353
|
+
const allKeys = [];
|
|
354
|
+
let allMountsSupportMaxDepth = true;
|
|
355
|
+
for (const mount of mounts) {
|
|
356
|
+
if (!mount.driver.flags?.maxDepth) allMountsSupportMaxDepth = false;
|
|
357
|
+
const rawKeys = await asyncCall(mount.driver.getKeys, mount.relativeBase, opts);
|
|
358
|
+
for (const key of rawKeys) {
|
|
359
|
+
const fullKey = mount.mountpoint + normalizeKey(key);
|
|
360
|
+
if (!maskedMounts.some((p) => fullKey.startsWith(p))) allKeys.push(fullKey);
|
|
361
|
+
}
|
|
362
|
+
maskedMounts = [mount.mountpoint, ...maskedMounts.filter((p) => !p.startsWith(mount.mountpoint))];
|
|
363
|
+
}
|
|
364
|
+
const shouldFilterByDepth = opts.maxDepth !== void 0 && !allMountsSupportMaxDepth;
|
|
365
|
+
return allKeys.filter((key) => (!shouldFilterByDepth || filterKeyByDepth(key, opts.maxDepth)) && filterKeyByBase(key, base));
|
|
366
|
+
},
|
|
367
|
+
async clear(base, opts = {}) {
|
|
368
|
+
base = normalizeBaseKey(base);
|
|
369
|
+
await Promise.all(getMounts(base, false).map(async (m) => {
|
|
370
|
+
if (m.driver.clear) return asyncCall(m.driver.clear, m.relativeBase, opts);
|
|
371
|
+
if (m.driver.removeItem) {
|
|
372
|
+
const keys = await m.driver.getKeys(m.relativeBase || "", opts);
|
|
373
|
+
return Promise.all(keys.map((key) => m.driver.removeItem(key, opts)));
|
|
374
|
+
}
|
|
375
|
+
}));
|
|
376
|
+
},
|
|
377
|
+
async dispose() {
|
|
378
|
+
await Promise.all(Object.values(context.mounts).map((driver) => dispose(driver)));
|
|
379
|
+
},
|
|
380
|
+
async watch(callback) {
|
|
381
|
+
await startWatch();
|
|
382
|
+
context.watchListeners.push(callback);
|
|
383
|
+
return async () => {
|
|
384
|
+
context.watchListeners = context.watchListeners.filter((listener) => listener !== callback);
|
|
385
|
+
if (context.watchListeners.length === 0) await stopWatch();
|
|
386
|
+
};
|
|
387
|
+
},
|
|
388
|
+
async unwatch() {
|
|
389
|
+
context.watchListeners = [];
|
|
390
|
+
await stopWatch();
|
|
391
|
+
},
|
|
392
|
+
mount(base, driver) {
|
|
393
|
+
base = normalizeBaseKey(base);
|
|
394
|
+
if (base && context.mounts[base]) throw new Error(`already mounted at ${base}`);
|
|
395
|
+
if (base) {
|
|
396
|
+
context.mountpoints.push(base);
|
|
397
|
+
context.mountpoints.sort((a, b) => b.length - a.length);
|
|
398
|
+
}
|
|
399
|
+
context.mounts[base] = driver;
|
|
400
|
+
if (context.watching) Promise.resolve(watch(driver, onChange, base)).then((unwatcher) => {
|
|
401
|
+
context.unwatch[base] = unwatcher;
|
|
402
|
+
}).catch(console.error);
|
|
403
|
+
return storage;
|
|
404
|
+
},
|
|
405
|
+
async unmount(base, _dispose = true) {
|
|
406
|
+
base = normalizeBaseKey(base);
|
|
407
|
+
if (!base || !context.mounts[base]) return;
|
|
408
|
+
if (context.watching && base in context.unwatch) {
|
|
409
|
+
context.unwatch[base]?.();
|
|
410
|
+
delete context.unwatch[base];
|
|
411
|
+
}
|
|
412
|
+
if (_dispose) await dispose(context.mounts[base]);
|
|
413
|
+
context.mountpoints = context.mountpoints.filter((key) => key !== base);
|
|
414
|
+
delete context.mounts[base];
|
|
415
|
+
},
|
|
416
|
+
getMount(key = "") {
|
|
417
|
+
key = normalizeKey(key) + ":";
|
|
418
|
+
const m = getMount(key);
|
|
419
|
+
return {
|
|
420
|
+
driver: m.driver,
|
|
421
|
+
base: m.base
|
|
422
|
+
};
|
|
423
|
+
},
|
|
424
|
+
getMounts(base = "", opts = {}) {
|
|
425
|
+
base = normalizeKey(base);
|
|
426
|
+
return getMounts(base, opts.parents).map((m) => ({
|
|
427
|
+
driver: m.driver,
|
|
428
|
+
base: m.mountpoint
|
|
429
|
+
}));
|
|
430
|
+
},
|
|
431
|
+
keys: (base, opts = {}) => storage.getKeys(base, opts),
|
|
432
|
+
get: (key, opts = {}) => storage.getItem(key, opts),
|
|
433
|
+
set: (key, value, opts = {}) => storage.setItem(key, value, opts),
|
|
434
|
+
has: (key, opts = {}) => storage.hasItem(key, opts),
|
|
435
|
+
del: (key, opts = {}) => storage.removeItem(key, opts),
|
|
436
|
+
remove: (key, opts = {}) => storage.removeItem(key, opts)
|
|
437
|
+
};
|
|
438
|
+
return storage;
|
|
439
|
+
}
|
|
440
|
+
function watch(driver, onChange, base) {
|
|
441
|
+
return driver.watch ? driver.watch((event, key) => onChange(event, base + key)) : () => {};
|
|
442
|
+
}
|
|
443
|
+
async function dispose(driver) {
|
|
444
|
+
if (typeof driver.dispose === "function") await asyncCall(driver.dispose);
|
|
445
|
+
}
|
|
446
|
+
//#endregion
|
|
447
|
+
export { normalizeKey as n, createStorage as t };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { o as KVListOptions, s as KVListPage } from "./types-CO0DRjJa.js";
|
|
2
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.7_@azure+identity@4.13.1_@azure+storage-blob@12.31.0_@netlify+blo_8194820fdb6706e28a1ff20070ef4217/node_modules/unstorage/dist/_chunks/types.d.mts
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type StorageValue = null | string | number | boolean | object;
|
|
5
|
+
type WatchEvent = "update" | "remove";
|
|
6
|
+
type WatchCallback = (event: WatchEvent, key: string) => any;
|
|
7
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
8
|
+
type Unwatch = () => MaybePromise<void>;
|
|
9
|
+
interface StorageMeta {
|
|
10
|
+
atime?: Date;
|
|
11
|
+
mtime?: Date;
|
|
12
|
+
ttl?: number;
|
|
13
|
+
[key: string]: StorageValue | Date | undefined;
|
|
14
|
+
}
|
|
15
|
+
type TransactionOptions = Record<string, any>;
|
|
16
|
+
type GetKeysOptions = TransactionOptions & {
|
|
17
|
+
maxDepth?: number;
|
|
18
|
+
};
|
|
19
|
+
interface DriverFlags {
|
|
20
|
+
maxDepth?: boolean;
|
|
21
|
+
ttl?: boolean;
|
|
22
|
+
}
|
|
23
|
+
interface Driver<OptionsT = any, InstanceT = any> {
|
|
24
|
+
name?: string;
|
|
25
|
+
flags?: DriverFlags;
|
|
26
|
+
options?: OptionsT;
|
|
27
|
+
getInstance?: () => InstanceT;
|
|
28
|
+
hasItem: (key: string, opts: TransactionOptions) => MaybePromise<boolean>;
|
|
29
|
+
getItem: (key: string, opts?: TransactionOptions) => MaybePromise<StorageValue>;
|
|
30
|
+
/** @experimental */
|
|
31
|
+
getItems?: (items: {
|
|
32
|
+
key: string;
|
|
33
|
+
options?: TransactionOptions;
|
|
34
|
+
}[], commonOptions?: TransactionOptions) => MaybePromise<{
|
|
35
|
+
key: string;
|
|
36
|
+
value: StorageValue;
|
|
37
|
+
}[]>;
|
|
38
|
+
/** @experimental */
|
|
39
|
+
getItemRaw?: (key: string, opts: TransactionOptions) => MaybePromise<unknown>;
|
|
40
|
+
setItem?: (key: string, value: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
41
|
+
/** @experimental */
|
|
42
|
+
setItems?: (items: {
|
|
43
|
+
key: string;
|
|
44
|
+
value: string;
|
|
45
|
+
options?: TransactionOptions;
|
|
46
|
+
}[], commonOptions?: TransactionOptions) => MaybePromise<void>;
|
|
47
|
+
/** @experimental */
|
|
48
|
+
setItemRaw?: (key: string, value: any, opts: TransactionOptions) => MaybePromise<void>;
|
|
49
|
+
removeItem?: (key: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
50
|
+
getMeta?: (key: string, opts: TransactionOptions) => MaybePromise<StorageMeta | null>;
|
|
51
|
+
getKeys: (base: string, opts: GetKeysOptions) => MaybePromise<string[]>;
|
|
52
|
+
clear?: (base: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
53
|
+
dispose?: () => MaybePromise<void>;
|
|
54
|
+
watch?: (callback: WatchCallback) => MaybePromise<Unwatch>;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/runtime/driver.d.ts
|
|
58
|
+
type KVRuntimeDriver = Driver & Record<string, unknown> & {
|
|
59
|
+
getAndDeleteItem?: <T = unknown>(key: string) => Promise<T | null>;
|
|
60
|
+
incrementItem?: (key: string, ttl: number) => Promise<number>;
|
|
61
|
+
listKeys: (options: KVListOptions) => Promise<KVListPage>;
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
export { KVRuntimeDriver as t };
|
package/dist/errors.d.ts
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { opendir } from "node:fs/promises";
|
|
2
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import createDriver from "unstorage/drivers/fs-lite";
|
|
4
|
+
//#region src/runtime/fs-lite.ts
|
|
5
|
+
function createFsLiteKVDriver(options) {
|
|
6
|
+
const driver = createDriver(options);
|
|
7
|
+
const continuations = /* @__PURE__ */ new Map();
|
|
8
|
+
const maximumContinuations = 32;
|
|
9
|
+
function releaseContinuation(cursor) {
|
|
10
|
+
const continuation = continuations.get(cursor);
|
|
11
|
+
if (!continuation) return;
|
|
12
|
+
clearTimeout(continuation.timeout);
|
|
13
|
+
continuations.delete(cursor);
|
|
14
|
+
continuation.iterator.return(void 0);
|
|
15
|
+
}
|
|
16
|
+
driver.listKeys = async ({ cursor, limit, prefix = "" }) => {
|
|
17
|
+
const root = resolve(options.base);
|
|
18
|
+
const keys = [];
|
|
19
|
+
async function* walk() {
|
|
20
|
+
const pendingDirectories = [root];
|
|
21
|
+
while (pendingDirectories.length > 0) {
|
|
22
|
+
const directory = pendingDirectories.pop();
|
|
23
|
+
let entries;
|
|
24
|
+
try {
|
|
25
|
+
entries = await opendir(directory);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (directory === root && error instanceof Error && "code" in error && error.code === "ENOENT") return;
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
for await (const entry of entries) {
|
|
31
|
+
const path = join(directory, entry.name);
|
|
32
|
+
if (entry.isDirectory()) {
|
|
33
|
+
pendingDirectories.push(path);
|
|
34
|
+
yield void 0;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (entry.isFile()) yield relative(root, path);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const continuation = cursor ? continuations.get(cursor) : void 0;
|
|
42
|
+
const iterator = continuation?.iterator ?? (cursor ? void 0 : walk());
|
|
43
|
+
if (!iterator) throw Object.assign(/* @__PURE__ */ new TypeError("Invalid or expired fs-lite KV cursor."), { code: "KV_CURSOR_EXPIRED" });
|
|
44
|
+
if (cursor && continuation) {
|
|
45
|
+
clearTimeout(continuation.timeout);
|
|
46
|
+
continuations.delete(cursor);
|
|
47
|
+
}
|
|
48
|
+
let scanned = 0;
|
|
49
|
+
while (scanned < limit) {
|
|
50
|
+
const entry = await iterator.next();
|
|
51
|
+
if (entry.done) return { keys };
|
|
52
|
+
scanned++;
|
|
53
|
+
const path = entry.value;
|
|
54
|
+
if (path === void 0) continue;
|
|
55
|
+
const key = path.split(sep).join(":");
|
|
56
|
+
if (key.startsWith(prefix)) keys.push(key);
|
|
57
|
+
}
|
|
58
|
+
const nextCursor = crypto.randomUUID();
|
|
59
|
+
const timeout = setTimeout(() => {
|
|
60
|
+
releaseContinuation(nextCursor);
|
|
61
|
+
}, 15 * 6e4);
|
|
62
|
+
timeout.unref();
|
|
63
|
+
continuations.set(nextCursor, {
|
|
64
|
+
iterator,
|
|
65
|
+
timeout
|
|
66
|
+
});
|
|
67
|
+
while (continuations.size > maximumContinuations) {
|
|
68
|
+
const oldestCursor = continuations.keys().next().value;
|
|
69
|
+
if (oldestCursor) releaseContinuation(oldestCursor);
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
keys,
|
|
73
|
+
cursor: nextCursor
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
return driver;
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
export { createFsLiteKVDriver as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { _ as
|
|
1
|
+
import { _ as ResolvedKVStoreConfig, a as KVErrorDetails, c as KVModuleOptions, d as KVStorage, f as KVStoreConfig, g as ResolvedKVModuleOptions, h as ResolvedFsLiteKVStoreConfig, i as KVDriver, l as KVOperation, m as ResolvedDenoKVStoreConfig, n as DenoKVStoreConfig, o as KVListOptions, p as ResolvedCloudflareKVStoreConfig, r as FsLiteKVStoreConfig, s as KVListPage, t as CloudflareKVStoreConfig, u as KVResult, v as ResolvedUpstashKVStoreConfig, y as UpstashKVStoreConfig } from "./types-CO0DRjJa.js";
|
|
2
2
|
|
|
3
3
|
//#region src/runtime/storage.d.ts
|
|
4
4
|
declare const kv: KVStorage;
|
|
5
5
|
//#endregion
|
|
6
|
-
export { type CloudflareKVStoreConfig, type DenoKVStoreConfig, type FsLiteKVStoreConfig, type KVDriver, type KVErrorDetails, type KVModuleOptions, type KVOperation, type KVResult, type KVStorage, type KVStoreConfig, type ResolvedCloudflareKVStoreConfig, type ResolvedDenoKVStoreConfig, type ResolvedFsLiteKVStoreConfig, type ResolvedKVModuleOptions, type ResolvedKVStoreConfig, type ResolvedUpstashKVStoreConfig, type UpstashKVStoreConfig, kv };
|
|
6
|
+
export { type CloudflareKVStoreConfig, type DenoKVStoreConfig, type FsLiteKVStoreConfig, type KVDriver, type KVErrorDetails, type KVListOptions, type KVListPage, type KVModuleOptions, type KVOperation, type KVResult, type KVStorage, type KVStoreConfig, type ResolvedCloudflareKVStoreConfig, type ResolvedDenoKVStoreConfig, type ResolvedFsLiteKVStoreConfig, type ResolvedKVModuleOptions, type ResolvedKVStoreConfig, type ResolvedUpstashKVStoreConfig, type UpstashKVStoreConfig, kv };
|